Skip to content

Fix NPZ buffer lifetime to prevent use-after-free - #1607

Merged
mthrok merged 6 commits into
facebookresearch:mainfrom
gagandhakrey:fix/retain-npz-buffer-lifetime
Aug 15, 2026
Merged

Fix NPZ buffer lifetime to prevent use-after-free#1607
mthrok merged 6 commits into
facebookresearch:mainfrom
gagandhakrey:fix/retain-npz-buffer-lifetime

Conversation

@gagandhakrey

Copy link
Copy Markdown
Contributor

Purpose

Fix a lifetime issue in NpzFile that could result in a dangling pointer when loading NPZ data from an in-memory buffer.

Issue

NpzFile only stores the raw memory address of the input buffer:

self._data = _get_pointer(data)

The original data object is not retained after load_npz() returns. As a result, the backing buffer may be garbage-collected while NpzFile continues 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:

data = spdl.io.load_npz(f.read())
array = data["x"]

After load_npz() returns, the temporary buffer can be freed even though NpzFile still references its address.

Fix

Retain a strong reference to the original input buffer for the lifetime of the NpzFile instance by storing it as an instance attribute.

This ensures the underlying memory remains valid until the NpzFile object is destroyed, preventing dangling pointers during subsequent array access.

Post-fix Impact

  • Eliminates a potential use-after-free when accessing NPZ entries.
  • Prevents undefined behavior caused by reading freed memory.
  • Ensures returned NumPy arrays always reference valid backing storage.
  • Preserves the existing API and runtime behavior with negligible memory overhead beyond retaining the original input buffer for the lifetime of the NpzFile.

Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
@meta-cla

meta-cla Bot commented Aug 2, 2026

Copy link
Copy Markdown

Hi @gagandhakrey!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 3, 2026
@meta-cla

meta-cla Bot commented Aug 3, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@mthrok

mthrok commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Hi @gagandhakrey

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>
@meta-codesync

meta-codesync Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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.)

Comment thread tests/io/array_test.py
# unless `NpzFile` retains it.
npz = spdl.io.load_npz(dump(x=ref))
gc.collect()
clobber = _reuse_freed_memory(size)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, exactly added comments explaining the cpython allocator reuse and the intent. thanks for pointing it out

@mthrok mthrok left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for providing the unit test. I left one comment. Otherwise looks good.

gagandhakrey and others added 3 commits August 13, 2026 04:29
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
@gagandhakrey

Copy link
Copy Markdown
Contributor Author

Thank you for providing the unit test. I left one comment. Otherwise looks good.

Thank you @mthrok for your review ,I have addressed that comment

@mthrok
mthrok merged commit 168e653 into facebookresearch:main Aug 15, 2026
108 checks passed
@mthrok

mthrok commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

@gagandhakrey Thank you for your contribution. Please feel free to open an issue/PR if you should encounter any issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants