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/slice overflow #30

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions tests/functional/builtins/codegen/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,66 @@ def test_slice_bytes32_calldata_extended(get_contract, code, result):
c.bar(3, "0x0001020304050607080910111213141516171819202122232425262728293031", 5).hex()
== result
)


oob_fail_list = [
"""
d: public(Bytes[256])

@external
def do_slice():
x : uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 # 2**256-1
cyberthirst marked this conversation as resolved.
Show resolved Hide resolved
self.d = b"\x01\x02\x03\x04\x05\x06"
assert len(slice(self.d, 1, x))==115792089237316195423570985008687907853269984665640564039457584007913129639935
""",
"""
@external
def do_slice():
x: uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 # 2**256 - 1
y: uint256 = 22704331223003175573249212746801550559464702875615796870481879217237868556850 # 0x3232323232323232323232323232323232323232323232323232323232323232
z: uint96 = 1
if True:
placeholder : uint256[16] = [y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y]
s :String[32] = slice(uint2str(z), 1, x) # uint2str(z) == "1"
#print(len(s))
assert slice(s, 1, 2) == "22"
""",
"""
x: public(Bytes[64])
secret: uint256

@deploy
def __init__():
self.x = empty(Bytes[64])
self.secret = 42

@external
def do_slice() -> Bytes[64]:
# max - 63
start: uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639872
return slice(self.x, start, 64)
""",
# tests bounds check in adhoc location calldata
"""
interface IFace:
def choose_value(_x: uint256, _y: uint256, _z: uint256, idx: uint256) -> Bytes[32]: nonpayable

@external
def choose_value(_x: uint256, _y: uint256, _z: uint256, idx: uint256) -> Bytes[32]:
assert idx % 32 == 4
return slice(msg.data, idx, 32)

@external
def do_slice():
idx: uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639908
ret: uint256 = _abi_decode(extcall IFace(self).choose_value(1, 2, 3, idx), uint256)
assert ret == 0
"""
]


@pytest.mark.parametrize("bad_code", oob_fail_list)
def test_slice_buffer_oob_reverts(bad_code, get_contract, tx_failed):
c = get_contract(bad_code)
with tx_failed():
c.do_slice()
30 changes: 15 additions & 15 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ def build_IR(self, expr, context):
ADHOC_SLICE_NODE_MACROS = ["~calldata", "~selfcode", "~extcode"]


# make sure we don't overrun the source buffer, checking for
# overflow:
# valid inputs satisfy: `assert start+length <= src_len && start+length > start`
def _make_slice_bounds_check(start, length, src_len):
return [
Copy link
Owner

Choose a reason for hiding this comment

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

i'd like to see sanity check assertions that start, length and src_len all have .is_complex_ir is False

"with",
"end",
["add", start, length],
["assert", ["iszero", ["or", ["gt", "end", src_len], ["lt", "end", start]]]],
]

def _build_adhoc_slice_node(sub: IRnode, start: IRnode, length: IRnode, context: Context) -> IRnode:
assert length.is_literal, "typechecker failed"
assert isinstance(length.value, int) # mypy hint
Expand All @@ -241,7 +252,7 @@ def _build_adhoc_slice_node(sub: IRnode, start: IRnode, length: IRnode, context:
if sub.value == "~calldata":
node = [
"seq",
["assert", ["le", ["add", start, length], "calldatasize"]], # runtime bounds check
_make_slice_bounds_check(start, length, "calldatasize"),
["mstore", np, length],
["calldatacopy", np + 32, start, length],
np,
Expand All @@ -251,7 +262,7 @@ def _build_adhoc_slice_node(sub: IRnode, start: IRnode, length: IRnode, context:
elif sub.value == "~selfcode":
node = [
"seq",
["assert", ["le", ["add", start, length], "codesize"]], # runtime bounds check
_make_slice_bounds_check(start, length, "codesize"),
["mstore", np, length],
["codecopy", np + 32, start, length],
np,
Expand All @@ -266,8 +277,7 @@ def _build_adhoc_slice_node(sub: IRnode, start: IRnode, length: IRnode, context:
sub.args[0],
[
"seq",
# runtime bounds check
["assert", ["le", ["add", start, length], ["extcodesize", "_extcode_address"]]],
_make_slice_bounds_check(start, length, ["extcodesize", "_extcode_address"]),
["mstore", np, length],
["extcodecopy", "_extcode_address", np + 32, start, length],
np,
Expand Down Expand Up @@ -438,19 +448,9 @@ def build_IR(self, expr, args, kwargs, context):

do_copy = copy_bytes(copy_dst, copy_src, copy_len, copy_maxlen)

# make sure we don't overrun the source buffer, checking for
# overflow:
# `assert start+length <= src_len && start+length <= length`
bounds_check = [
"with",
"end",
["add", start, length],
["assert", ["iszero", ["or", ["gt", "end", src_len], ["gt", "end", length]]]],
]

ret = [
"seq",
bounds_check,
_make_slice_bounds_check(start, length, src_len),
do_copy,
["mstore", dst, length], # set length
dst, # return pointer to dst
Expand Down
Loading