Skip to content

Add bounds check for function body size in binary reader#2693

Merged
sbc100 merged 1 commit intoWebAssembly:mainfrom
sumleo:fix/binary-reader-body-size-check
Feb 13, 2026
Merged

Add bounds check for function body size in binary reader#2693
sbc100 merged 1 commit intoWebAssembly:mainfrom
sumleo:fix/binary-reader-body-size-check

Conversation

@sumleo
Copy link
Contributor

@sumleo sumleo commented Feb 12, 2026

Summary

  • ReadCodeSection computed end_offset = body_start_offset + body_size without validating that end_offset falls within the code section boundary (read_end_).
  • A crafted module with an oversized body_size could cause the reader to parse past the section end.
  • Add an ERROR_UNLESS check that validates end_offset against read_end_, matching the validation pattern used elsewhere for section and subsection sizes.

Details

In binary-reader.cc, ReadCodeSection reads body_size via LEB128 and computes the expected end offset:

uint32_t body_size;
CHECK_RESULT(ReadU32Leb128(&body_size, "function body size"));
Offset body_start_offset = state_.offset;
Offset end_offset = body_start_offset + body_size;
// No validation that end_offset <= read_end_
CALLBACK(BeginFunctionBody, func_index, body_size);

The end_offset is later used to set state_.offset (when skip_function_bodies is true) and passed to ReadFunctionBody as the expected end position. Without bounds validation, a malformed body_size allows reading past the code section boundary.

Fix: Add ERROR_UNLESS(end_offset >= body_start_offset && end_offset <= read_end_, ...) immediately after computing end_offset.

Test plan

  • New test BinaryReader.InvalidFunctionBodySize exercises the fix with a wasm module whose function body size (255 bytes) exceeds the code section
  • All existing unit tests pass (128 tests)

TEST(BinaryReader, InvalidFunctionBodySize) {
// A wasm module where the function body size extends past the end of the
// code section. Without the bounds check this would allow the binary reader
// to read past the section boundary.
Copy link
Member

Choose a reason for hiding this comment

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

Presumably this means we are lacking an upstream spec test for this case?

Maybe add a TODO here to move this test upstream into the spec repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, added a TODO to move this test upstream into the spec repo.

ReadCodeSection computed end_offset = body_start_offset + body_size
without validating that end_offset falls within the code section
boundary.  A crafted module with an oversized body_size could cause
the reader to parse past the section end, reading into subsequent
sections or past the end of the buffer.

Add an ERROR_UNLESS check that end_offset does not overflow and does
not exceed read_end_, matching the validation pattern used elsewhere
in the binary reader for section and subsection sizes.

Add a regression test with a wasm module whose function body size
exceeds the code section boundary.
@sumleo sumleo force-pushed the fix/binary-reader-body-size-check branch from 86e5173 to b6aefa2 Compare February 12, 2026 23:52
@sbc100 sbc100 enabled auto-merge (squash) February 12, 2026 23:55
@sbc100 sbc100 merged commit 1298485 into WebAssembly:main Feb 13, 2026
17 checks passed
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.

2 participants