Fix integer-overflow bypass of bounds check in get_safeptr()#11
Merged
ringtailsoftware merged 1 commit intoJul 16, 2026
Merged
Conversation
get_safeptr() is the bounds-checking primitive behind uvm32_arg_getslice()/uvm32_arg_getslice_fixed(), used by a host to safely turn a (ptr, len) syscall argument pair supplied by VM bytecode into a slice. Both addr and len come directly from VM registers a0/a1, so are fully attacker-controlled if the running bytecode is untrusted. The guard computed ptrstart + len in uint32_t and compared the sum against the memory limit. If len is chosen so that ptrstart + len wraps past 2^32 (e.g. ptrstart=1, len=0xFFFFFFFF), the wrapped sum can land back at or below the limit, bypassing the check while returning a slice whose declared len vastly exceeds the real buffer. Any host code that subsequently trusts that len for a copy/read/write will overrun its buffer. Fix by comparing len against (limit - ptrstart) after confirming ptrstart <= limit, which cannot overflow. Applies to both the main memory path and the extram path. Added a regression test (test_syscall_args_bufrd_overflow_bypass) that requests a slice with a real in-bounds pointer but len=0xFFFFFFFF and asserts it is now rejected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
get_safeptr()is the bounds-checking primitive behinduvm32_arg_getslice()/uvm32_arg_getslice_fixed(), which the docs describe as the way a host should safely turn a(ptr, len)syscall argument pair supplied by VM bytecode into a slice. Bothaddrandlencome directly from VM registers a0/a1, so they are fully attacker-controlled if the running bytecode is untrusted — which is the whole point of the library ("bad code running in the VM should never be able to crash the host").The guard is:
ptrstart + lenis computed inuint32_t. Iflenis chosen so the sum wraps past 2^32 (e.g.ptrstart = 1,len = 0xFFFFFFFF), the wrapped sum lands back at/belowlimit, so the check is bypassed and the function returns a slice whose declaredlenis far larger than the real buffer. Any host code that trusts thatlenfor a subsequent copy/read/write will overrun its buffer.I verified this by compiling the unmodified
uvm32/uvm32.c(UVM32_MEMORY_SIZE=64) and calling only the public API: settingregs[10]/regs[11](a0/a1) toaddr = RAM_IMAGE_OFFSET+1andlen = 0xFFFFFFFF, wiring_params[0]/[1]to those registers the same wayuvm32_run()does for a real syscall, then callinguvm32_arg_getslice(). It returns a valid-looking pointer withlen == 0xFFFFFFFFagainst a real 64-byte memory buffer. The same happens on theextrampath.Fix: compare
lenagainstlimit - ptrstartafter first confirmingptrstart <= limit(which cannot overflow), instead of comparing the (overflow-prone) sumptrstart + lenagainstlimit. Applied to both the main-memory branch and theextrambranch.Added
test_syscall_args_bufrd_overflow_bypassintest/syscall_args/test/tests.c, following the existing style in that file: it triggers the existingSYSCALL_Cpath (real, in-bounds pointer from the rom), overridesARG1to0xFFFFFFFFto simulate attacker-controlled bytecode, and assertsuvm32_arg_getslice()now rejects it (len == 0, subsequent run ends inUVM32_ERR_MEM_RD) rather than silently overflowing.I don't have a RISC-V cross-compiler / working Docker daemon in my local environment to run the full
make cisuite end-to-end here, but the added test mirrors the exact structure of the neighboringtest_syscall_args_bufrd_toolarge/test_syscall_args_bufrd_bad_addrtests, and I separately confirmed with a standalone harness (compiling the exact sameget_safeptr()logic) that the fix preserves every existing boundary case (exact-fit read, one-byte-over rejection, in-bounds slice, too-large length, bad address, zero-length-at-boundary) while closing the overflow bypass.