-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed as not planned
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Issue Description:
The vulnerability lies in the misuse of ctypes
to write beyond the allocated memory buffer. This can lead to memory corruption, crashes, or potentially exploit arbitrary memory locations. The issue is not specific to NumPy but demonstrates how unsafe memory manipulation with ctypes
can lead to a buffer overflow.
Steps to Reproduce:
import numpy as np
import ctypes
# Step 1: Create a small NumPy array
arr = np.zeros(10, dtype=np.uint8)
print(f"Original array: {arr}")
# Step 2: Use ctypes to manipulate memory directly
# Get a pointer to the array's data buffer
buffer_ptr = ctypes.cast(arr.ctypes.data, ctypes.POINTER(ctypes.c_uint8))
# Step 3: Simulate a buffer overflow by writing beyond the allocated memory
for i in range(20): # Writing past the 10-element boundary
buffer_ptr[i] = 42
print(f"Array after simulated buffer overflow: {arr}")
# Step 4: Confirm memory corruption
# Observe how writing beyond the bounds corrupts memory without NumPy's knowledge.
print("Overflow writing beyond allocated memory completed.")
Expected Output:
Original array: [0 0 0 0 0 0 0 0 0 0]
Array after simulated buffer overflow: [42 42 42 42 42 42 42 42 42 42]
Overflow writing beyond allocated memory completed.
Impact and Severity:
-
Severity: Moderate to High, depending on the context.
- This demonstrates memory corruption in Python applications when using
ctypes
. - It bypasses Python's safety features and can lead to undefined behavior or potential security risks.
- This demonstrates memory corruption in Python applications when using
-
Scope:
- This vulnerability highlights unsafe practices in handling memory with
ctypes
. - It is not a vulnerability of NumPy but the misuse of
ctypes
alongside it.
- This vulnerability highlights unsafe practices in handling memory with
Thank you and Let me know if you have any questions
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Windows
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error