Solidity: Computing gas stipend for calls to precompiles could use an underflow guard (post-Byzantium)

Created on 8 Mar 2018  路  10Comments  路  Source: ethereum/solidity

Has been brought up on /r/ethdev; most relevant to solc is likely this sub-thread.

In short: when preparing for a CALL to a precompile (ecrecover in this case), the compiler generates code that allocates a gas stipend of post_compile_const - GAS, and then doesn't check if the result wrapped around 0 by chance (i.e. post_compile_const < GAS).

@chriseth says that pre-Byzantium, this would fail.

/u/Figs999 links the code block where the computation is done.

bug

All 10 comments

Duplicate of https://github.com/ethereum/solidity/issues/3680 :)

X_X

Closing as duplicate.

Actually this one has much more details :)

It turns out that this is already correct for byzantium VMs. So if you compile for pre-Tangerine Whistle and run it on pre-Tangerine Whistle, it works fine, if you compile for Tangerine Whistle and run it on Tangerine Whistle it is fine, the only problem is if you compile for pre-Tangerine Whistle and run it on Tangerine Whistle.

What we could do is implement a check if the gas computaiton wraps around, but the cost for that computation would have to be included in the number that is subtracted, so it gets even more messy.

I would just ignore this very specific problem. It is fixed in latest solidity because it defaults to a byzantium VM.

Are there any situations where this could be used to fool users?

@axic what do you think?

Somewhat OT:

@axic mentions in https://github.com/ethereum/solidity/pull/2673#issuecomment-319183436:

I think the best solution would be forwarding the maximum gas possible to ecrecover/sha256/ripemd160 and disallowing overriding them.

Is this worth re-considering?..

@veox this is the case with a post-TangerineWhistle VM.

If we can trust that the compiler calculates that value properly, then no matter if it wrapped or not, it will run out of gas. Doesn't it?

the only problem is if you compile for pre-Tangerine Whistle and run it on Tangerine Whistle.

If it wraps, then it will be a very large number, which tangerine whistle VMs just truncate to the max available gas, so it should be working?

Yes, I also think it is fine.

We could consider though adding test cases with limited gas supplied to verify these assumptions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

axic picture axic  路  3Comments

chriseth picture chriseth  路  4Comments

chriseth picture chriseth  路  3Comments

ddeclerck picture ddeclerck  路  3Comments

VoR0220 picture VoR0220  路  4Comments