Fix NPZ buffer lifetime to prevent use-after-free - #1607
Conversation
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
|
Hi @gagandhakrey! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thanks for the PR. It looks good. I'm away from my laptop for a week or so, so I'll merge it when I come back. Meanwhile if you can add a unit test, that'll be great. |
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
|
This pull request has been imported. If you are a Meta employee, you can view this in D114881820. (Because this pull request was imported automatically, there will not be any future comments.) |
| # unless `NpzFile` retains it. | ||
| npz = spdl.io.load_npz(dump(x=ref)) | ||
| gc.collect() | ||
| clobber = _reuse_freed_memory(size) |
There was a problem hiding this comment.
IIUC, this _reuse_freed_memory takes advantage of the implementation detail of CPython which reuses pointers of deallocated objects, so if the pointer to the temporary is not retained by npz object, then _reuse_freed_memory can overrides the data and the following assert_array_equal will fail. and the _reuse_freed_memory increases the chance of pointer being reused by repeating the allocation 2000 times.
This is a bit elaborated logic that uses internal details of the CPython interpreter. Can you add a comment of the intention here so that it's easier for future maintainer to get what is happening?
There was a problem hiding this comment.
yes, exactly added comments explaining the cpython allocator reuse and the intent. thanks for pointing it out
mthrok
left a comment
There was a problem hiding this comment.
Thank you for providing the unit test. I left one comment. Otherwise looks good.
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
Thank you @mthrok for your review ,I have addressed that comment |
|
@gagandhakrey Thank you for your contribution. Please feel free to open an issue/PR if you should encounter any issue. |
Purpose
Fix a lifetime issue in
NpzFilethat could result in a dangling pointer when loading NPZ data from an in-memory buffer.Issue
NpzFileonly stores the raw memory address of the input buffer:The original
dataobject is not retained afterload_npz()returns. As a result, the backing buffer may be garbage-collected whileNpzFilecontinues to hold a pointer to its memory.Subsequent accesses through
__getitem__use this stale pointer to read NPZ entries, resulting in a classic use-after-free.Example:
After
load_npz()returns, the temporary buffer can be freed even thoughNpzFilestill references its address.Fix
Retain a strong reference to the original input buffer for the lifetime of the
NpzFileinstance by storing it as an instance attribute.This ensures the underlying memory remains valid until the
NpzFileobject is destroyed, preventing dangling pointers during subsequent array access.Post-fix Impact
NpzFile.