]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix #8105. Add validation to mmap.mmap so invalid file descriptors
authorBrian Curtin <brian.curtin@gmail.com>
Sun, 1 Aug 2010 15:26:26 +0000 (15:26 +0000)
committerBrian Curtin <brian.curtin@gmail.com>
Sun, 1 Aug 2010 15:26:26 +0000 (15:26 +0000)
don't cause a crash on Windows.

Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 68af00e3a29a63c70ebbfe8072aebd7c1e7955fb..5906c02ea96da14485d0903186d55a6a2dde111d 100644 (file)
@@ -1,6 +1,6 @@
 from test.support import TESTFN, run_unittest, import_module
 import unittest
-import os, re, itertools
+import os, re, itertools, socket
 
 # Skip test if we can't import mmap.
 mmap = import_module('mmap')
@@ -586,6 +586,17 @@ class MmapTests(unittest.TestCase):
                 pass
             m.close()
 
+        def test_invalid_descriptor(self):
+            # socket file descriptors are valid, but out of range
+            # for _get_osfhandle, causing a crash when validating the
+            # parameters to _get_osfhandle.
+            s = socket.socket()
+            try:
+                with self.assertRaises(mmap.error):
+                    m = mmap.mmap(s.fileno(), 10)
+            finally:
+                s.close()
+
     def test_context_manager(self):
         with mmap.mmap(-1, 10) as m:
             self.assertFalse(m.closed)
index 2affe70da59a2c5c9969c6e6e9065c34298adcfe..0fa78458f52b04645d28bcb53fe4c1ed5274fd7f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,8 @@ Core and Builtins
 Extensions
 ----------
 
+- Issue #8105: Validate file descriptor passed to mmap.mmap on Windows.
+
 - Issue #8046: Add context manager protocol support and .closed property
   to mmap objects.
 
index de527ce846cbe89dc87398be31e3d61b89b555a8..3973124e14b9544f2d77fe07f104f76ee25a76b9 100644 (file)
@@ -1236,6 +1236,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
                      1);
      */
     if (fileno != -1 && fileno != 0) {
+        /* Ensure that fileno is within the CRT's valid range */
+        if (_PyVerify_fd(fileno) == 0) {
+            PyErr_SetFromErrno(mmap_module_error);
+            return NULL;
+        }
         fh = (HANDLE)_get_osfhandle(fileno);
         if (fh==(HANDLE)-1) {
             PyErr_SetFromErrno(mmap_module_error);