Skip to content

Commit 707802f

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping (GH-154259) (GH-154262)
NetBSD mremap() returns a mapping whose grown region is not backed when growing a shared anonymous mapping, so accessing it crashes. Reject it with ValueError, as is already done on Linux. (cherry picked from commit 542b982) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 1ad8381 commit 707802f

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,10 @@ def test_resize_up_anonymous_mapping(self):
907907

908908
with mmap.mmap(-1, start_size) as m:
909909
m[:] = data
910-
if sys.platform.startswith(('linux', 'android')):
911-
# Can't expand a shared anonymous mapping on Linux.
912-
# See https://bugzilla.kernel.org/show_bug.cgi?id=8691
910+
if sys.platform.startswith(('linux', 'android', 'netbsd')):
911+
# Can't expand a shared anonymous mapping on Linux
912+
# (see https://bugzilla.kernel.org/show_bug.cgi?id=8691)
913+
# or NetBSD.
913914
with self.assertRaises(ValueError):
914915
m.resize(new_size)
915916
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :meth:`mmap.mmap.resize` on NetBSD when growing a shared
2+
anonymous mapping. :meth:`!resize` now raises :exc:`ValueError` in this
3+
case, as it already did on Linux.

Modules/mmapmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,13 @@ mmap_mmap_resize_impl(mmap_object *self, Py_ssize_t new_size)
977977
#ifdef UNIX
978978
void *newmap;
979979

980-
#ifdef __linux__
980+
#if defined(__linux__) || defined(__NetBSD__)
981+
// Linux mremap() refuses to grow a shared anonymous mapping, and
982+
// NetBSD mremap() returns a mapping whose grown region is not backed,
983+
// so accessing it crashes. Reject it here in both cases.
981984
if (self->fd == -1 && !(self->flags & MAP_PRIVATE) && new_size > self->size) {
982985
PyErr_Format(PyExc_ValueError,
983-
"mmap: can't expand a shared anonymous mapping on Linux");
986+
"mmap: can't expand a shared anonymous mapping");
984987
return NULL;
985988
}
986989
#endif

0 commit comments

Comments
 (0)