Skip to content
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

Merged
merged 2 commits into from Mar 23, 2021
Merged

Fix keccak bug #11134

merged 2 commits into from Mar 23, 2021

Conversation

hrkrshnn
Copy link
Member

Closes #11131

I'll prepare bugs.json

@hrkrshnn hrkrshnn force-pushed the fix-keccak-bug branch 2 times, most recently from 9508d13 to c3becff Compare March 22, 2021 11:04
Changelog.md Outdated Show resolved Hide resolved
docs/bugs.json Outdated Show resolved Hide resolved
docs/bugs.json Outdated Show resolved Hide resolved
docs/bugs.json Outdated Show resolved Hide resolved
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.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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.",

Copy link
Contributor

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.

Copy link
Member Author

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

@chriseth
Copy link
Contributor

Code looks good!

Copy link
Contributor

@bshastry bshastry left a 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

@@ -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(); }
Copy link
Member Author

@hrkrshnn hrkrshnn Mar 22, 2021

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Changelog.md Outdated Show resolved Hide resolved
chriseth
chriseth previously approved these changes Mar 23, 2021
bshastry
bshastry previously approved these changes Mar 23, 2021
Copy link
Contributor

@bshastry bshastry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hrkrshnn hrkrshnn dismissed stale reviews from bshastry and chriseth via f82fbe8 March 23, 2021 09:58
@hrkrshnn hrkrshnn force-pushed the fix-keccak-bug branch 2 times, most recently from f82fbe8 to a509983 Compare March 23, 2021 09:58
docs/bugs.json Outdated Show resolved Hide resolved
@chriseth chriseth merged commit 88d340f into develop Mar 23, 2021
@chriseth chriseth deleted the fix-keccak-bug branch March 23, 2021 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants