Fix write-heap-buffer-overflow in copy_out #15784
Open
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:
Check that out.nbytes() is at least as large as src.nbytes() to prevent copying beyond the range of src.
Also add a check on dtypes, make sure out and src dtypes are the same. Otherwise we may copy the wrong dtype without conversion.
The crash is a write-heap-buffer-overflow that occurs in the
torch::executor::native::copy_outfunction. The root cause is that thestd::memcpyoperation in this function does not check if the destination bufferoutis large enough to hold the data from the source tensorsrc. Specifically, the conditioninternal::sizes_match_ignoring_leading_1s(out.sizes(), src.sizes())checks if the sizes ofoutandsrcmatch, ignoring any leading dimensions of size 1 inout, but it does not guarantee thatout.nbytes()is greater than or equal tosrc.nbytes().The patch fixes the crash by adding an additional check
out.nbytes() >= src.nbytes()before performing thestd::memcpyoperation. This ensures that the destination bufferoutis large enough to hold the data fromsrc, preventing the buffer overflow.Other considerations that reviewers should take into account when validating the patch include verifying that the additional check does not introduce any performance regressions and that it correctly handles edge cases, such as when
srcis empty or whenoutandsrchave different data types. Reviewers should also check that the patch does not affect the functionality of thecopy_outfunction in other scenarios. Additionally, it is worth verifying that the fix is consistent with the existing error handling and checking mechanisms in thecopy_outfunction.NOTE: This diff is entirely auto-generated by LLM-based patch generator.
Reviewer should carefully examine this diff as Lionhead does not guarrantee the
correctnesss of the patch beyond fixing the crash and passing existing tests.
Please commandeer this diff and revise as needed. Our bot does not respond to
comments or revision requests (yet).
Differential Revision: D80885980