Add bounds checking for output tensor buffer in wasi-nn llama.cpp#4847
Open
sumleo wants to merge 1 commit intobytecodealliance:mainfrom
Open
Add bounds checking for output tensor buffer in wasi-nn llama.cpp#4847sumleo wants to merge 1 commit intobytecodealliance:mainfrom
sumleo wants to merge 1 commit intobytecodealliance:mainfrom
Conversation
The get_output function copies LLM output into output_tensor->buf without checking against output_tensor->size, allowing writes past the buffer when the model generates output longer than the caller-provided buffer. Add size checks for both the metadata path and the token output loop.
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.
Summary
The
get_outputfunction in the wasi-nn llama.cpp backend copies data intooutput_tensor->bufwithout checking againstoutput_tensor->size. This can lead to out-of-bounds writes when the model generates output longer than the caller-provided buffer.Two vulnerable paths were identified:
Metadata path (index == 1):
memcpy(output_tensor->buf, output_metadata, strlen(output_metadata))copies up to 127 bytes of metadata JSON with no size check against the destination buffer.Token output loop (index == 0):
memcpy(output_tensor->buf + end_pos, buf, strlen(buf))accumulates token pieces in a loop with no bounds checking, allowing unbounded writes past the buffer.Fix
output_tensor->sizebefore callingmemcpy.output_tensor->size, and break out of the loop if so.Both fixes use the existing
output_tensor->sizefield from thetensor_datastruct defined inwasi_nn_types.h.Test plan