New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix keccak bug #11134
Fix keccak bug #11134
Conversation
9508d13
to
c3becff
Compare
test/libsolidity/semanticTests/functionCall/mapping_array_internal_argument.sol
Outdated
Show resolved
Hide resolved
c3becff
to
97593f6
Compare
ad7be64
to
4481c27
Compare
4481c27
to
2357cfc
Compare
2357cfc
to
3504d12
Compare
docs/bugs.json
Outdated
{ | ||
"name": "KeccakCaching", | ||
"summary": "The bytecode optimizer incorrectly re-used previously evaluated Keccak-256 hashes. You are unlikely to be affected if you do not compute Keccak-256 hashes in inline assembly.", | ||
"description": "Solidity's bytecode optimizer has a step that can compute Keccak-256 hashes, if the contents of the memory are known during compilation time. This step also has a mechanism to determine that two Keccak-256 hashes are equal even if the values in memory are not known during compile time. This mechanism had a bug where, Keccak-256 of the same memory content, but different sizes were considered equal. More specifically, ``keccak256(mpos1, length1)`` and ``keccak256(mpos2, length2)`` in some cases were considered equal if ``length1`` and ``length2``, when rounded up to nearest multiple of 32 were the same, and when the memory contents at ``mpos1`` and ``mpos2`` can be deduced to be equal. You maybe affected if you compute multiple Keccak-256 hashes of the same content, but with different length inside inline assembly.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"description": "Solidity's bytecode optimizer has a step that can compute Keccak-256 hashes, if the contents of the memory are known during compilation time. This step also has a mechanism to determine that two Keccak-256 hashes are equal even if the values in memory are not known during compile time. This mechanism had a bug where, Keccak-256 of the same memory content, but different sizes were considered equal. More specifically, ``keccak256(mpos1, length1)`` and ``keccak256(mpos2, length2)`` in some cases were considered equal if ``length1`` and ``length2``, when rounded up to nearest multiple of 32 were the same, and when the memory contents at ``mpos1`` and ``mpos2`` can be deduced to be equal. You maybe affected if you compute multiple Keccak-256 hashes of the same content, but with different length inside inline assembly.", | |
"description": "Solidity's bytecode optimizer has a step that can compute Keccak-256 hashes, if the contents of the memory are known during compilation time. This step also has a mechanism to determine that two Keccak-256 hashes are equal even if the values in memory are not known during compile time. This mechanism had a bug where Keccak-256 of the same memory content, but different sizes were considered equal. More specifically, ``keccak256(mpos1, length1)`` and ``keccak256(mpos2, length2)`` in some cases were considered equal if ``length1`` and ``length2``, when rounded up to nearest multiple of 32 were the same, and when the memory contents at ``mpos1`` and ``mpos2`` can be deduced to be equal. You maybe affected if you compute multiple Keccak-256 hashes of the same content, but with different length inside inline assembly.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh maybe also add some exclusion facts: Code is unaffected if it uses keccak256 with a length that is not a compile-time constant or if it is always a multiple of 32.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the comma and added the exclusion fact as the last line
Code looks good! |
...bsolidity/semanticTests/inlineAssembly/keccak256_optimizer_bug_different_memory_location.sol
Show resolved
Hide resolved
3504d12
to
22b910b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comment about positive test case
test/libsolidity/semanticTests/inlineAssembly/keccak256_optimizer_cache_bug.sol
Outdated
Show resolved
Hide resolved
...bsolidity/semanticTests/inlineAssembly/keccak256_optimizer_bug_different_memory_location.sol
Show resolved
Hide resolved
22b910b
to
9c6ab49
Compare
@@ -100,10 +100,12 @@ class KnownState | |||
void resetStorage() { m_storageContent.clear(); } | |||
/// Resets any knowledge about storage. | |||
void resetMemory() { m_memoryContent.clear(); } | |||
/// Resets known Keccak-256 hashes | |||
void resetKnownKeccak256Hashes() { m_knownKeccak256Hashes.clear(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a new function to clear the m_knownKeccak256Hashes
whenever memory is reset. Forcefull reset makes the code 'look' safer, even though I think it makes no difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed: loadFromMemory
returns "unknown" after a memory clear and the sequence number is correctly incremented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not needed, either. But doesn't it make sense to keep it for consistency with the rest of the resets? I also think it feels safer to reset the keccak hashes when memory is reset.
9c6ab49
to
2f8724f
Compare
2f8724f
to
243347e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
f82fbe8
to
a509983
Compare
a509983
to
30e08ee
Compare
Closes #11131
I'll prepare
bugs.json