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]: integer overflow in JumpTable.SubStr #3496

Open
wants to merge 28 commits into
base: HF_Echidna
Choose a base branch
from

Conversation

nan01ab
Copy link
Contributor

@nan01ab nan01ab commented Sep 21, 2024

Description

Fix integer overflow in JumpTable.SubStr

Fixes #3495

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Jim8y and others added 21 commits August 8, 2024 20:09
* null operation

* fix array

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Christopher Schuchardt <cschuchardt88@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
* Set `Password` to `SecureString` for Wallet information

* Added `UnitTest` for `SecureStringExtensions`

* Update tests/Neo.Extensions.Tests/UT_SecureStringExtensions.cs

Co-authored-by: Shargon <shargon@gmail.com>

* Update src/Neo.Extensions/SecureStringExtensions.cs

---------

Co-authored-by: Shargon <shargon@gmail.com>
* rpc parameter parse

* update blockchain related apis.

* Update src/Plugins/RpcServer/RpcMethodWithParamsAttribute.cs

* Delete src/Plugins/RpcServer/JsonPropertyNameAttribute.cs

* udpate contract model

* Update src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

* Apply suggestions from code review

Remove comments

* Update src/Plugins/RpcServer/RpcServer.cs

* fix warnings

* ensure it can load both true/false and 1/0

* optimize the pr and add unit tests

* add more tests and check the safe max value and safe min value

* remove format

* remove unused

* format

* Apply suggestions from code review

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
* test RpcServer.Utilities

* test invoke function and script

* TestTraverseIterator

* TestGetUnclaimedGas

* RpcServerSettings.Default with { SessionEnabled = true }

* test call with storage changes and events

* use wallet in invokefunction

* use invalid wallet

* invoke without signer

* all cases for TraverseIterator

* traversing same session twice; not expired session

* cover OnTimer

* test deserializing complex signers

* use Assert.ThrowsException

* TestSendFrom and TestSendMany

* apply code review with `nameof`

* test cancel transaction

* TestInvokeContractVerify

* improve error message

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
…o-project#3408)

* Part-1 `Neo.IO` - move

* Part-2

* Added `BigInteger` to `Neo.Extensions`

* Found more `BigInteger`

* Added `ByteArray` to `Neo.Extensions`

* Added `DateTime` Extensions to `Neo.Extensions`

* Added `HashSetExtensions`, `HashSetExtensions2`, `IpAddressExtensions`, `AssemblyExtensions`, `StringExtensdions`
Deleted `Helper.cs` file

* Added Tests

* Added `tests` from `Part-2`

* Added `tests` for `PART-4`

* Add `using Neo.Extensions` for unit tests

* Change `HashSetExtensions2` to `HashSetExtensions`

* Update tests/Neo.Extensions.Tests/UT_BigIntegerExtensions.cs

* Update and rename StringExtensdions.cs to StringExtensdios.cs

* Rename StringExtensdios.cs to StringExtensions.cs

* `dotnet format`

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
* UT_KeyedCollectionSlim

* Update UT_KeyedCollectionSlim.cs

* Update UT_MemoryReader.cs

* Update UT_MemoryReader.cs
* improve error message when wrong wallet is opened

* update tests
* Update UT_RpcServer.Blockchain.cs

* Update UT_RpcServer.Blockchain.cs

* update

* fixed bug

* format

* update

* Update NativeContractExtensions.cs

* update

* Remove conflicting files

* update

* format
* Fixed `UInt160` and expanded class

* Cleaned up code for `TryParse`

* Fixed `TryParse`

* Fixed small bug with `TryParse`

* Change `UInt160.Zero` to `static readonly`

* benchmark UInt160

* Fix benchmark

* Fixed bugs and added features for `UInt160` class

* Revert and just keep bug fixes

* Made @shargon changes

* Set `InvariantCultureIgnoreCase` back for `0x` and `0X`

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: Jimmy <jimmy@r3e.network>
* test GetApplicationLog

* filter execution type

* Test_Commands; refactor

* apply review suggestions

---------

Co-authored-by: Shargon <shargon@gmail.com>
* Add UT Neo.Extensions

* resolve conflicts

* Revert "resolve conflicts"

This reverts commit 6d0a61b.

* add edge case

* update mod test

---------

Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: Jimmy <jimmy@r3e.network>
* Update UT_Utility.cs

* TestGetContractState
- Fix typo

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
* trigger https oracle

* make it internal

* return Task.CompletedTask

---------

Co-authored-by: Jimmy <jimmy@r3e.network>
Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: Shargon <shargon@gmail.com>
…arameter types. (neo-project#3479)

* update rpc node methods signatures to use explicit parameter types.

* fix attribute place

---------

Co-authored-by: Christopher Schuchardt <cschuchardt88@gmail.com>
Co-authored-by: Shargon <shargon@gmail.com>
* Update OpCode.cs

* Update JumpTable.Compound.cs

* Update OpCode.cs
* parse nef file scripts

* nef file path support

---------

Co-authored-by: Vitor Nazário Coelho <vncoelho@gmail.com>
…es (neo-project#3486)

* fix push integer

* Update src/Neo.VM/ScriptBuilder.cs

* Update tests/Neo.VM.Tests/UT_ScriptBuilder.cs

* Update tests/Neo.VM.Tests/UT_ScriptBuilder.cs
@nan01ab nan01ab changed the title fix: integer overflow in JumpTable.SubStr [Fix]: integer overflow in JumpTable.SubStr Sep 21, 2024
Copy link
Member

@cschuchardt88 cschuchardt88 left a comment

Choose a reason for hiding this comment

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

@shargon why isn't there a vm limit in this

if (index >= x.Length)
throw new InvalidOperationException($"The value of index {index} is out of range.");

if (checked(index + count) > x.Length)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (checked(index + count) > x.Length)
if (unchecked(index + count) > x.Length)

This would be better. If it overflows it will be bigger than the length. This way we don't have dotnet generic overflow exception.

Copy link
Contributor Author

@nan01ab nan01ab Sep 21, 2024

Choose a reason for hiding this comment

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

This would be better. If it overflows it will be bigger than the length. This way we don't have dotnet generic overflow exception.

checked here behaves the same as Memcpy.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we need a HF

Copy link
Contributor

Choose a reason for hiding this comment

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

Echidna, yes. But checked is likely needed. The only reason this works in NeoGo is because int is mostly 64-bit (on all officially supported NeoGo platforms it is, but in theory NeoGo can be compiled for 32-bit platforms as well).

@@ -95,13 +95,22 @@ public virtual void SubStr(ExecutionEngine engine, Instruction instruction)
{
Copy link
Member

Choose a reason for hiding this comment

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

@shargon why isn't there a vm limit in this?

Copy link
Member

Choose a reason for hiding this comment

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

Because a substr can't be longer than the previous item.. theoretically

@@ -473,6 +473,50 @@
}
}
]
},
{
"name": "Count Exceed Range Test",
Copy link
Contributor

Choose a reason for hiding this comment

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

at instruction 18 (SUBSTR): invalid offset in NeoGo.

]
},
{
"name": "Index Exceed Range Test",
Copy link
Contributor

Choose a reason for hiding this comment

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

at instruction 18 (SUBSTR): invalid offset in NeoGo.

"0x0a",
"0x00010203040506070809",
"PUSHINT32",
"0x7FFFFFFF",
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also add some tests for INT64, like:

                byte(opcode.PUSHINT64), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
                byte(opcode.PUSH2),

It'll fail (in NeoGo it's at instruction 22 (SUBSTR): not an int32), but just to make sure.

if (index >= x.Length)
throw new InvalidOperationException($"The value of index {index} is out of range.");

if (checked(index + count) > x.Length)
Copy link
Contributor

Choose a reason for hiding this comment

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

Echidna, yes. But checked is likely needed. The only reason this works in NeoGo is because int is mostly 64-bit (on all officially supported NeoGo platforms it is, but in theory NeoGo can be compiled for 32-bit platforms as well).

nan01ab and others added 4 commits September 23, 2024 20:46
* fix: concurrency conflict in HeaderCache.Count

* Update tests/Neo.UnitTests/Ledger/UT_HeaderCache.cs

* Update tests/Neo.UnitTests/Ledger/UT_HeaderCache.cs

---------

Co-authored-by: Shargon <shargon@gmail.com>
@Jim8y Jim8y changed the base branch from master to HF_Echidna September 27, 2024 01:24
cschuchardt88 and others added 2 commits September 27, 2024 10:48
* Added Builders with tests

* Added SignerBuilder and started WitnessRuleBuilder

* Added `WitnessConditionBuilder` with tests

* Added more logic

* Fixed `SignerBuilder` class

* Code touch ups

* Added more tests

* Update src/Neo/Builders/TransactionBuilder.cs

* Fixed `And` `Or` and `Not` conditions

* Fixed Memory leak

* Added error message for Witness scripts

---------

Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
* Fixed Delete packages for github

* Automatic

* added continue-on-error: true

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
@shargon
Copy link
Member

shargon commented Sep 27, 2024

Rebase needed

@shargon shargon added hardfork waiting-hardfork bug Used to tag confirmed bugs critical Issues (bugs) that need to be fixed ASAP and removed hardfork labels Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Used to tag confirmed bugs critical Issues (bugs) that need to be fixed ASAP waiting for review waiting-hardfork
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: integer overflow in JumpTable.SubStr
8 participants