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: when decoding addresses restrict to 20 bytes #56

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

SamMayWork
Copy link
Contributor

ref: https://discordapp.com/channels/905194001349627914/928377875827154984/1194272891957694614

When indexing the BSC chain, it's been reported that the EVM connector crashes when indexing this transaction. This is because the padding scheme used in the transaction is not using zero-bytes to pad out the value. When we parse the entire 32 bytes from the chunk into an integer, the padding non-zero bytes are contributing to the integer value, causing the parsed address value to overflow what can be actually been shown in 20 bytes. This issue is causing golang to panic and the EVM connector to crash.

The proposed fix is to decode addresses only on the right-most 20 bytes, and ignore the front 12 bytes from the chunk.

Signed-off-by: SamMayWork <sam.may@kaleido.io>
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (78ea6df) 99.90% compared to head (e47e379) 99.90%.
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #56   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files          38       38           
  Lines        3317     3323    +6     
=======================================
+ Hits         3314     3320    +6     
  Misses          2        2           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EnriqueL8
Copy link
Contributor

Approach looks good to me

@SamMayWork
Copy link
Contributor Author

For sake of consistency, I've checked the behaviour when someone tries to force an encode operation with an address value in bytes that would exceed the 20 bytes buffer and an error is thrown if the value doesn't fit into a uint160 type as expected.

Copy link
Contributor

@nguyer nguyer left a comment

Choose a reason for hiding this comment

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

This looks good and makes sense to me. It is consistent with how we decode other types. I think it would be good to have @peterbroadhurst take a quick look at this also to make sure I'm not missing something here as this is pretty important, low level code.

@wpater
Copy link

wpater commented Jan 16, 2024

Is there a chance to build a docker image from this branch?

@nguyer
Copy link
Contributor

nguyer commented Jan 17, 2024

Is there a chance to build a docker image from this branch?

I think we can get this merged today which will automatically trigger a snapshot image build

Copy link
Contributor

@peterbroadhurst peterbroadhurst left a comment

Choose a reason for hiding this comment

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

Per the above, might need a bit more insight into the justification for the change you've made here @SamMayWork (vs. fixing decodeABIUnsignedInt)

@@ -227,7 +227,7 @@ var (
},
jsonEncodingType: JSONEncodingTypeBytes,
encodeABIData: encodeABIUnsignedInteger,
decodeABIData: decodeABIUnsignedInt,
decodeABIData: decodeABIAddress,
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry @SamMayWork - I think I need to understand this better.
Maybe I need to work through this from base principals.

decodeABIUnsignedInt should be processing exactly 20 bytes.

The ABI specification states that address is processed identically to uint256.

So on the face of it, your change seems to sidestep a bug in decodeABIUnsignedInt by introducing a new function... but I don't understand why.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, skipping working here before writing comment.
address should be processed identically to uint160

So I think the point here is that uintXXX processing is not correctly trimming before processing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a fair point - address is just a unit256 under the hood, so this fixed the problem seen on the BSC chain here, but if someone padded a normal unit256 with non-zero bytes, we'd actually hit the same issue.

Taking the fixed code and moving it into the baseline unit256 function...

Copy link
Contributor

Choose a reason for hiding this comment

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

This issue is causing golang to panic

Then just as important. A uint256 should not panic the VM, including ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 (which to my knowledge is a perfectly valid uint256)

Copy link
Contributor

Choose a reason for hiding this comment

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

So I'm expecting to see @SamMayWork :

  • Processing of ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 as a uint160 resulting in 0xab0974bbed8afc5212e951c8498873319d02d025 as a number
  • Processing of ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 as a uint8 resulting in 0x25 as a number
  • Processing of ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 as a uint256 processing 0xffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 as a number
  • Processing of ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 as an address processing 0xab0974bbed8afc5212e951c8498873319d02d025 with the semantic that uint160 and address are equivalent at this layer of code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be resolved by 711fd0f

Signed-off-by: SamMayWork <sam.may@kaleido.io>
cv.Value = new(big.Int).SetBytes(block[headPosition : headPosition+32])

// When we're reading bytes, need to make sure we're reading the correct size number of bytes for the uint size
cv.Value = new(big.Int).SetBytes(block[headPosition+(32-(int(component.m/8))) : headPosition+32])
Copy link
Contributor

Choose a reason for hiding this comment

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

@peterbroadhurst peterbroadhurst merged commit ede49b9 into hyperledger:main Jan 17, 2024
2 checks passed
@peterbroadhurst
Copy link
Contributor

@wpater - this docker tag should have the fix now:
https://github.com/hyperledger/firefly-signer/pkgs/container/firefly-signer/168104554?tag=v1.1.11-20240117-47

@wpater
Copy link

wpater commented Jan 18, 2024

It's up and running for over 30mins - looks fine from my perspective

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants