Commit e01f2f6
committed
gh-146525: Add ndim validation to PyBuffer_ToContiguous for defense-in-depth
Add validation of the ndim parameter in PyBuffer_ToContiguous() and
buffer_to_contiguous() to prevent potential integer overflow in memory
allocation calculations.
While Python-level code already enforces PyBUF_MAX_NDIM (64), C extensions
implementing custom getbufferproc could potentially provide invalid ndim
values. This change adds defense-in-depth validation to ensure ndim is
within the valid range before performing allocations.
The allocation calculation \3 * src->ndim * sizeof(Py_ssize_t)\ could
theoretically overflow if ndim exceeds ~3.8e17 on 64-bit systems, though
this is not practically exploitable. This patch adds explicit validation
as a hardening measure.
Changes:
- PyBuffer_ToContiguous(): Add runtime check for ndim range
- buffer_to_contiguous(): Add assertion for ndim <= PyBUF_MAX_NDIM
- Add test case in test_memoryview.py
This is a hardening improvement, not a fix for an actively exploitable
vulnerability.
Co-authored-by: Lakshmikanthan K <badassletchu@gmail.com>1 parent 5466f57 commit e01f2f6
File tree
3 files changed
+30
-0
lines changed- Lib/test
- Misc/NEWS.d/next/C_API
- Objects
3 files changed
+30
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
861 | 861 | | |
862 | 862 | | |
863 | 863 | | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
864 | 878 | | |
865 | 879 | | |
866 | 880 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
500 | 500 | | |
501 | 501 | | |
502 | 502 | | |
| 503 | + | |
503 | 504 | | |
504 | 505 | | |
505 | 506 | | |
| |||
1059 | 1060 | | |
1060 | 1061 | | |
1061 | 1062 | | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
1062 | 1073 | | |
1063 | 1074 | | |
1064 | 1075 | | |
| |||
0 commit comments