Skip to content

Commit

Permalink
Add missing validations in cases where assembly import would silently…
Browse files Browse the repository at this point in the history
… truncate the value
  • Loading branch information
cameel committed Nov 29, 2024
1 parent 1cce974 commit 2c3935e
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 0 deletions.
21 changes: 21 additions & 0 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector
);
};

auto requireValueInRange = [&](std::string const& _name, std::string const& _value, u256 _min, u256 _max)
{
bigint parsedValue(_value);
solRequire(
_min <= parsedValue && parsedValue <= _max,
AssemblyImportException,
"Value provided for instruction '" + _name + "' is out of the allowed range "
"[" + formatNumber(_min) + ", " + formatNumber(_max) + "]."
);
};

solRequire(srcIndex >= -1 && srcIndex < static_cast<int>(_sourceList.size()), AssemblyImportException, "Source index out of bounds.");
if (srcIndex != -1)
location.sourceName = sharedSourceName(_sourceList[static_cast<size_t>(srcIndex)]);
Expand Down Expand Up @@ -217,6 +228,7 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector
if (name == "PUSH")
{
requireValueDefinedForInstruction(name, value);
requireValueInRange(name, "0x" + value, 0, std::numeric_limits<u256>::max());
result = {AssemblyItemType::Push, u256("0x" + value)};
}
else if (name == "PUSH [ErrorTag]")
Expand All @@ -227,16 +239,19 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector
else if (name == "PUSH [tag]")
{
requireValueDefinedForInstruction(name, value);
requireValueInRange(name, value, 0, std::numeric_limits<unsigned>::max());
result = {AssemblyItemType::PushTag, updateUsedTags(u256(value))};
}
else if (name == "PUSH [$]")
{
requireValueDefinedForInstruction(name, value);
requireValueInRange(name, "0x" + value, 0, std::numeric_limits<size_t>::max());
result = {AssemblyItemType::PushSub, u256("0x" + value)};
}
else if (name == "PUSH #[$]")
{
requireValueDefinedForInstruction(name, value);
requireValueInRange(name, "0x" + value, 0, std::numeric_limits<size_t>::max());
result = {AssemblyItemType::PushSubSize, u256("0x" + value)};
}
else if (name == "PUSHSIZE")
Expand Down Expand Up @@ -267,11 +282,17 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector
else if (name == "tag")
{
requireValueDefinedForInstruction(name, value);
requireValueInRange(name, value, 0, std::numeric_limits<unsigned>::max());
result = {AssemblyItemType::Tag, updateUsedTags(u256(value))};
}
else if (name == "PUSH data")
{
requireValueDefinedForInstruction(name, value);
solRequire(
value.size() == 64 && value.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos,
AssemblyImportException,
"Value provided for instruction 'PUSH data' is not a valid 256-bit hexadecimal number."
);
result = {AssemblyItemType::PushData, u256("0x" + value)};
}
else if (name == "VERBATIM")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "PUSH data",
"source": 0,
"value": "112233445566778899aabbccddeeff00112233445566778899aabbccddeeffgg"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH data' is not a valid 32-bit hash.",
"message": "Assembly import error: Value provided for instruction 'PUSH data' is not a valid 32-bit hash.",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "PUSH data",
"source": 0,
"value": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH data' is not a valid 32-bit hash.",
"message": "Assembly import error: Value provided for instruction 'PUSH data' is not a valid 32-bit hash.",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "PUSH #[$]",
"source": 0,
"value": "10000000000000000"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH #[$]' is out of the allowed range [0, 0xffffffffffffffff].",
"message": "Assembly import error: Value provided for instruction 'PUSH #[$]' is out of the allowed range [0, 0xffffffffffffffff].",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "PUSH [$]",
"source": 0,
"value": "10000000000000000"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH [$]' is out of the allowed range [0, 0xffffffffffffffff].",
"message": "Assembly import error: Value provided for instruction 'PUSH [$]' is out of the allowed range [0, 0xffffffffffffffff].",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "PUSH [tag]",
"source": 0,
"value": "0x100000000"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH [tag]' is out of the allowed range [0, 0xffffffff].",
"message": "Assembly import error: Value provided for instruction 'PUSH [tag]' is out of the allowed range [0, 0xffffffff].",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 10,
"name": "PUSH",
"source": 0,
"value": "00112233445566778899001122334455667788990011223344556677889900112233445566778899"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'PUSH' is out of the allowed range [0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff].",
"message": "Assembly import error: Value provided for instruction 'PUSH' is out of the allowed range [0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff].",
"severity": "error",
"type": "Exception"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"language": "EVMAssembly",
"sources": {
"A": {
"assemblyJson": {
".code": [
{
"begin": 0,
"end": 0,
"name": "tag",
"source": 0,
"value": "0x100000000"
}
],
"sourceList": [
"<stdin>"
]
}
}
},
"settings": {
"outputSelection": {
"*": {
"": ["evm.bytecode"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: Value provided for instruction 'tag' is out of the allowed range [0, 0xffffffff].",
"message": "Assembly import error: Value provided for instruction 'tag' is out of the allowed range [0, 0xffffffff].",
"severity": "error",
"type": "Exception"
}
]
}

0 comments on commit 2c3935e

Please sign in to comment.