Repository navigation
ggml: fix OOB write in ggml_acc / ggml_set with negative offset - #30135
Conversation
ggml_acc_impl narrowed a size_t offset to int32_t without checking that it fits, so a large offset could truncate to a negative int32_t. The forward then sign-extended it to a huge size_t and the bounds assertion wrapped, allowing an OOB write below the dst buffer. Check the offset before the narrowing, matching the existing check in ggml_set_impl.
|
Hi @Jasmine-tim, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
The narrowing can still end up being non-negative, so albeit this fixes some cases there are still many more inputs that will not be detected. |
|
I’ll figure out a better way. |
|
Or do you have any ideas? |
|
Add asserts in |
|
Ok,i'll do that |
I've finished the edits. Please review. |
|
ggml_set_impl already has this check, so I added the same |
how can i get the otherone's review to merge this pr? |
|
حالا بهتر شده . قبل از این پیج فایلهای ویندوز پر میشد ولی الان این مشکل حل شده و پایدارتر شده |
ggml_acc_impl narrowed a size_t offset to int32_t without checking that it fits, so a large offset could truncate to a negative int32_t. The forward then sign-extended it to a huge size_t and the bounds assertion wrapped, allowing an OOB write below the dst buffer. Check the offset before the narrowing, matching the existing check in ggml_set_impl.
ggml_acc_impl narrowed a size_t offset to int32_t without checking that it fits, so a large offset could truncate to a negative int32_t. The forward then sign-extended it to a huge size_t and the bounds assertion wrapped, allowing an OOB write below the dst buffer. Check the offset before the narrowing, matching the existing check in ggml_set_impl. (cherry picked from commit 8ae3867)
ggml_acc_impl narrowed a size_t offset to int32_t without checking that it fits, so a large offset could truncate to a negative int32_t. The forward then sign-extended it to a huge size_t and the bounds assertion wrapped, allowing an OOB write below the dst buffer. Check the offset before the narrowing, matching the existing check in ggml_set_impl.
ggml_acc_impl narrowed a size_t offset to int32_t without checking that it fits, so a large offset could truncate to a negative int32_t. The forward then sign-extended it to a huge size_t and the bounds assertion wrapped, allowing an OOB write below the dst buffer. Check the offset before the narrowing, matching the existing check in ggml_set_impl.
Overview
ggml_acc and ggml_set read the op offset from op_params[3] into a size_t.
op_params[3] is an int32_t. A negative value sign-extends to a huge size_t.
The existing bounds assertion then wraps around and passes. The write lands
before the dst buffer. This is an out-of-bounds write.
The fix reads the offset as int32_t, asserts it is non-negative, then casts
it to size_t. This applies to acc_f32, set_f32 and set_i32.
Additional information
The negative offset can come from the size_t to int32_t narrowing in
ggml_acc_impl and ggml_set_impl, or from the RPC GRAPH_COMPUTE path that
copies op_params from the wire without validation.
An ASan build reports heap-buffer-overflow (WRITE/READ of size 4) four bytes
before the dst buffer when the forward runs with op_params[3] = -4. With this
patch, the same input trips the GGML_ASSERT instead.
Requirements