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

Test encoding raw slices does not allows buffer overflows #6699

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Changes from all 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
30 changes: 30 additions & 0 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -950,3 +950,33 @@ fn ok_bytes_buffer_ownership() {
let mut bytes = abi_decode::<Bytes>(encoded_slice);
assert(bytes.get(0) == Some(5));
}

#[test]
fn ok_bytes_bigger_than_3064() {
let mut v: Bytes = Bytes::new();

// We allocate 1024 bytes initially, this is throw away because
// it is not big enough for the buffer.
// Then we used to double the buffer to 2048.
// Then we write an `u64` with the length of the buffer.
// Then we write the buffer itself.
// (1024 + 2048) - 8 = 3064
// Thus, we need a buffer with 3065 bytes to write into the red zone
let mut a = 3065;
while a > 0 {
v.push(1u8);
a -= 1;
}

// This red zone should not be overwritten
let red_zone = asm(size: 1024) {
aloc size;
hp: raw_ptr
};
red_zone.write(0xFFFFFFFFFFFFFFFF);
assert(red_zone.read::<u64>() == 0xFFFFFFFFFFFFFFFF);

let _ = encode(v);

assert(red_zone.read::<u64>() == 0xFFFFFFFFFFFFFFFF);
}
Loading