Temporary storage low gas reentrancy
ETHCC 7 · 8 Jul 2024 · 18:00
Enis Ulqinaku, an auditor at ChainSecurity, explains a reentrancy risk introduced by transient storage (EIP-1153) after the Cancun hard fork. After recapping Cancun's EIPs and how EVM storage, memory and execution contexts work, he shows how transient storage can cut the cost of a reentrancy guard to roughly 300 gas. His point is that contracts safe before can become exploitable when refactored to use transient storage, for example a vault's transfer with temporary allowances, and he links this to the Constantinople delay and EIP-2200. Q&A covers Vyper support and why transient storage is cheaper.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Hi everyone, uh I am Ennis from Chain Security, and in this presentation I will be talking about uh re-entrancy opportunity in the after the Cancun hard fork. And this was basically about a curious case of EIP 1153, which is also referred to as the transient storage. Yeah. As a background, I am an auditor at Chain Security, and the Chain Security is an auditing firm which does uh smart contrast reviews since uh 2017. We have work been working with the major players in the industry, and we have a long-term partnerships with many of them, and we publish reports uh frequently. All of our reports are available in our website, chainsecurity.com.
Uh about me. Uh I am as I mentioned, I am an auditor at Chain Security. I have around 10 years of uh cybersecurity experience. Uh initially I started with uh Web2 security where I did my PhD and then postdoc studies uh regarding the user authentication and cryptic cloud storage and then moved into uh blockchain space. Yeah, so in this presentation, uh this is the agenda. So I will start with a recap of the Cancun hard fork, what were the main IPs included in this uh hard fork, and then I will explain I will describe the problem of the message passing between execution context in the EVM chains, and then I will explain how transient uh storage works, and then finally I will uh describe uh re-entrancy attack vector that might be possible with the transient storage. So uh the Cancun hard fork is the latest one, and this was uh activated roughly four months ago.
So this is the one that is running, and it's uh it included multiple EIPs where many of them were more famous than the others. And I think for this hard fork, the most of the attention went into the shard blob transactions, right? So this is the EIP which makes the uh it makes it the gas cost cheaper for the L2s that want to publish data frequently on the uh Ethereum mainnet. But also there were other EIPs included into this hard fork. The first one was the now the block route of the beacon chain is available in the EVM, it can be accessed from the smart contract.
And as I mentioned, it includes also the shard blob transactions. Uh the M copy opcode was made more efficient in terms of gas. And then also there is the EIP for self-destruct, which now has changed the behavior. So now luckily we don't have any more like a self-destruct, it doesn't uh uh destroy the code, doesn't clear the code anymore unless the contract creation and the self-destruct is called in the same transaction, but otherwise self-destructs uh were behaves similarly to a uh simple ETH transfer. And then there is the blob-based safe opcode, which is uh related to the uh uh EIP above.
And then finally we have the EIP 1153, which includes uh uh introduces two new uh opcodes for the transient storage. So uh how did the data layout in Ethereum look before Cancun? So basically we have the Ethereum virtual machine, and in this we have the virtual ROM which stores the immutable code there, all the code is being stored there, and then we have the world state, which is persistent, and this is where we store everything like the storage. Like you can think in ERC20, this is where we keep track of the balances, allowances and everything like this.
And then every time we call a smart contract, actually a new context is created in EVM, and this is the machine state. So in the machine state, we have the program counter, we have the gas available, we have the stack and the memory. And this the memory is a very gas efficient. So to make it more simpler and more concrete, like why this is important. Let's assume we have two smart contracts. We have the smart contract C and we have a smart contract A.
And if we call a smart contract C, then a new execution context is created. And here we have this is a C1. And let's say this contract might be a vault, for example, where you deposit ETH and USDC, and then you can you want to withdraw these uh tokens from the vault, then the vault will send you ETH, which means that it will pass execution to you, and later it will send the USDC. And whenever it is calling a new contract A, and A might be trusted or untrusted, so we don't know, then the A can actually call back to C and then can re-enter, right? And because when we call back, it creates a new execution context.
And because C1 and C2 they cannot easily communicate with each other, so they cannot use memory because each of them, each of them has their own memory, so they cannot pass information to from one context to the other. They could use call data, but this is really not secure because if A is untrusted, it could just modify this call data. And the way how this is done currently is they use storage. And this works, but it's expensive on terms of gas. So and you can think about this as the most simple example as the non-reentrant modifier, right? So it is setting a flag in storage in C1 so C2 can read and behave like correctly if this flag is set or not set.
And this is possible, but it has some significant guess cost, like it has it can be something like between uh 2900 guests up to 20,000 guess, depending on the state uh if it's uh the storage slot is warm or cold. And I guess most of you are familiar with this uh code here, like this is the standard non-re-entrant uh modifier. So basically we have the we check initially if the lock is set, and if the if it's not set, then we set this lock to entered and we execute the operation and then we reset the lock to non-re-entered. And as you may notice here, whenever we are reading the lock, initially we do an S load opcode, which costs like 2100 gas, and then we do an S store, which costs 2900 gas, and then we do a warm S store, which costs 100 gas, and then resets the value, which gives us some refund.
Like and this depending, but this refund is kept right to 100 uh to 20% of the transaction cost. So this operation only, like the non-reentrant modifier can cost something between two twenty-three hundred up to fifty one hundred gas. And the EIP 1153 was proposed to make this operation is this use cases much more gas efficient and cheaper. And so this is meant to be used for re-entrancy lock on-chain computer will like create two uh addresses, single transaction ERC20 approvals. This is meant like for example, you want to give some approval to a contract, but you want to this approval to be valid only in the lifespan of a transaction.
So you want everything to be reset afterwards. And this can be used also for fee on transfer contracts. Maybe some contract may implement some logic where they charge a fee whenever the contract the tokens are being transferred in a in the same transaction. Or like there might be other use cases. And here now this becomes more interesting because we have two new opcodes. We have the T load and T store. And whenever the C we have the first execution context, C1, whenever it uh uh calls the contract A, it can just store some variable in the transient storage, and then it can read later from the uh other execution context, which is C2. And both T load and T store, they cost like 100 gas, so which is very cheap compared to the storage read and storage uh write.
So now here we have like as of now, like the uh transient storage is not like supported natively by Solidity, so we don't have yet the transient uh keyword, but we can access it in the assembly, and this is like if we refactor the non-reentrant modifier for to use the transient storage, it would look like as this code. And as you can see here, the gas cost is is much lower now, right? This costs only like 300 gas. And some might even go further and optimize even more this gas cost and omit the last T store, because if the storage is being reset anyways, like also we don't need to really set it to zero. But this might break some composability because then the batch operations and multicall wouldn't really work.3.8.
So there is a keyword like transient that can be used to declare the uh some variables in the transient storage. And actually, behind the scenes, Viper is implemented the non-re-entrant uh decorator with the transient storage. So in Viper, actually the non-re-entrant is quite uh uh it is much cheaper. Uh yeah, here is a vault, and basically it's a very simple vault, like it allows people to deposit and increases the balance of the message sender, then people can approve, and then they also can withdraw from the vault, right? And and as many of you might have noticed, like that there is an issue in the transfer because there is a re-entrance point where basically the caller, which in this case is the message sender, untrusted, could re-enter the in the contract to reset the elluents, and then the balance of would not be reduced as intended.
And luckily, like this was not an issue because before the Constantinople uh hard fork was applied. Actually, one of my colleagues at Chain Security, they uh he investigated this uh hard fork and he found out that uh actually this would introduce many uh re-entrancy issues and break some contracts, so the hard fork was delayed until a new EIP was uh included as well, the IP2200. Yeah, now why this is uh interesting because like if we have the previous code which is not uh it's it's not uh vulnerable, it was not vulnerable before the uh using the transient storage.
So someone might just fork this contract, for example, and then make it more gas efficient and then use the transient storage, right? And so we have the same contract, but now this is more gas efficient, spends less gas, like we don't uh store the temporary allowances in storage, we only store them in uh in transient storage, and now actually the bug this code is vulnerable, right? Because now we can if we re-enter into this transfer, then we can reset now. We can do a tier write, and then we could resend the temporary allowance so the balance of the uh source actually is not reduced, like it's just reduced by zero, although we receive some ETH. Yeah, and uh so another case might be uh here.
So this is a smart contract which is uh already deployed. I think this is from Euler V1, which is the smart contract itself, it's safe, like as it is. But if someone like was to uh get this for this smart contract and like modify to make it more gas efficient, and then basically, the re-entrancy issue would be introduced also in this case. So, as a summary, basically, this the transient storage is a new data location for which has a lifespan with a transaction, so we can store some data, and this lives only for the lifespan of this transaction. And it is mostly equivalent in behavior with the storage operations.
And this is what uh the EIP says, but actually it's not completely true because as I showed this caveat, like it when you write in the transient storage, actually doesn't perform this check that if you are in a low gas uh execution context, then it would revert. Like if you do it S store, but if you do a T store, it doesn't revert. And yeah, so basically the main takeaway from this presentation is that the T store will work during address transfer or the address. While the S store would revert in this cases. Yeah, and with that I conclude my presentation.
So thank you. Um I'm trying to get my head around a little bit about where exactly the temporary storage lives and its lifecycle. Because before that, you know, we had memory where you could, you know, it's in the it only has the lifecycle of the function, and then you have storage which has the life cycle of the whole contract. In that temp allowance, like if you put something in that mapping, is it gone after that function is finished? No, it's gone after the transaction finishes executing.
Okay. So basically this is leaving like it's a new place, it's a new data location. And then whenever you start a transaction, then you can use similarly as the storage. Yeah. But in the end of the transaction, once it uh completes, then basically this uh data location is reset. And that's why also it is cheap, right? Because we don't need to store in it into the storage, and we don't need to compute any proof or anything, like it's just reset in the end all the transaction. Okay, so if I've got like a mapping address to you in and I put an address and I put like a hundred in it, and then after that transaction is done, that address now the value is zero because exactly after this transaction, yeah. Right.
And this it makes more uh gas efficient in some use cases, and especially like for the uh non-re-enterant modifier, right? So basically here we have the T load, which are we are also reading from this temporary data location, and then also you are writing the T store we are writing in the slot zero here. Yeah. We're writing to the slot zero and we are saying that now this flag is set to one. So whenever we read, then also we are reading from this uh data location and then we revert if it's a re-entrant call. And do you know if there's because right now you can only access it using assembly?
Is there any like plans to make it accessible like from regular Solidity? Yeah, for for sure, right? I I would bet so. So basically this is uh accessible in Viper, as I said. But in Solidv, it's not uh supported yet, but I think in the near future it should be supported, yeah. Okay, great. Thanks. Um what is the transition data storage uh organized on I mean that on which data structure?
Uh on which is a transient. Uh I and uh we all know that um uh the stor the permanent storage is based on Mercle part participator try and then uh updating and now uh uh under updating to um uh under update to uh uh uh virtual tree. But um I wonder that uh because the trial is transient storage and uh can reach uh l uh lower gas cost. So uh what was the base data structure? Yeah, uh uh exactly.
So basically the um transient storage is implemented very similar to the uh normal storage, but it is not included in the proof, so it's not included in the Merkle tree, like by the end of the transaction. It is just reset to zero. Uh and basically we have before the transaction everything set to zero, after the transaction everything set to zero. Yeah, and that's why it's not costly like to compute any proof or anything for it, right? Oh it's because it's not included in the Merkle tree, like and that and also it's not stored in the disk, and that's why like it's it can be cheaper in terms of gas, like it's only hundred. Okay, does that make sense?
Uh just uh uh smaller that's uh just a mini version of the uh Mercopatures trying or Virgo tree. Thank you very much. Thank you.
Related talks
- Talk: EVM and Solidity: Same but Different
- Reversing Ethereum Smart Contracts to find out what's behind EVM bytecode (Devcon4)
- Securing Ethereum Pectra before mainnet - Zigtur | Spearbit & Cantina
- Pietro Carta - Reentrancy in Cancun hardfork: the curious case of EIP1153 (transient storage)
- Control Flow Graph Reconstruction from EVM bytecode
- Smart Contract Security Tips - Joseph Chow
- Live Smart Contract Hacking (Devcon4)
- Securing Ethereum
- All Your Contract Are Belong to Us: Analyzing All Deployed SCs | Yannis Smaragdakis - Dedaub