Skip to content

Commit 686ee4f

Browse files
committed
Merged revisions 83407 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83407 | brian.curtin | 2010-08-01 10:26:26 -0500 (Sun, 01 Aug 2010) | 3 lines Fix #8105. Add validation to mmap.mmap so invalid file descriptors don't cause a crash on Windows. ........
1 parent fc07031 commit 686ee4f

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/test/test_mmap.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from test.support import TESTFN, run_unittest, import_module
22
import unittest
3-
import os, re, itertools
3+
import os, re, itertools, socket
44

55
# Skip test if we can't import mmap.
66
mmap = import_module('mmap')
@@ -586,6 +586,16 @@ def test_crasher_on_windows(self):
586586
pass
587587
m.close()
588588

589+
def test_invalid_descriptor(self):
590+
# socket file descriptors are valid, but out of range
591+
# for _get_osfhandle, causing a crash when validating the
592+
# parameters to _get_osfhandle.
593+
s = socket.socket()
594+
try:
595+
with self.assertRaises(mmap.error):
596+
m = mmap.mmap(s.fileno(), 10)
597+
finally:
598+
s.close()
589599

590600
def test_main():
591601
run_unittest(MmapTests)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ Library
332332
Extension Modules
333333
-----------------
334334

335+
- Issue #8105: Validate file descriptor passed to mmap.mmap on Windows.
336+
335337
- Issue #9422: Fix memory leak when re-initializing a struct.Struct object.
336338

337339
- Issue #7900: The getgroups(2) system call on MacOSX behaves rather oddly

Modules/mmapmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
12031203
1);
12041204
*/
12051205
if (fileno != -1 && fileno != 0) {
1206+
/* Ensure that fileno is within the CRT's valid range */
1207+
if (_PyVerify_fd(fileno) == 0) {
1208+
PyErr_SetFromErrno(mmap_module_error);
1209+
return NULL;
1210+
}
12061211
fh = (HANDLE)_get_osfhandle(fileno);
12071212
if (fh==(HANDLE)-1) {
12081213
PyErr_SetFromErrno(mmap_module_error);

0 commit comments

Comments
 (0)