Skip to content

Commit 1ef605c

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.13] gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping (GH-154259) (GH-154264)
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 76b5867 commit 1ef605c

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
@@ -909,9 +909,10 @@ def test_resize_up_anonymous_mapping(self):
909909

910910
with mmap.mmap(-1, start_size) as m:
911911
m[:] = data
912-
if sys.platform.startswith(('linux', 'android')):
913-
# Can't expand a shared anonymous mapping on Linux.
914-
# See https://bugzilla.kernel.org/show_bug.cgi?id=8691
912+
if sys.platform.startswith(('linux', 'android', 'netbsd')):
913+
# Can't expand a shared anonymous mapping on Linux
914+
# (see https://bugzilla.kernel.org/show_bug.cgi?id=8691)
915+
# or NetBSD.
915916
with self.assertRaises(ValueError):
916917
m.resize(new_size)
917918
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
@@ -872,10 +872,13 @@ mmap_resize_method(mmap_object *self,
872872
#else
873873
void *newmap;
874874

875-
#ifdef __linux__
875+
#if defined(__linux__) || defined(__NetBSD__)
876+
// Linux mremap() refuses to grow a shared anonymous mapping, and
877+
// NetBSD mremap() returns a mapping whose grown region is not backed,
878+
// so accessing it crashes. Reject it here in both cases.
876879
if (self->fd == -1 && !(self->flags & MAP_PRIVATE) && new_size > self->size) {
877880
PyErr_Format(PyExc_ValueError,
878-
"mmap: can't expand a shared anonymous mapping on Linux");
881+
"mmap: can't expand a shared anonymous mapping");
879882
return NULL;
880883
}
881884
#endif

0 commit comments

Comments
 (0)