]> granicus.if.org Git - python/commitdiff
Close #13022: _multiprocessing.recvfd() doesn't check that file descriptor was actual...
authorJesus Cea <jcea@jcea.es>
Wed, 21 Sep 2011 01:53:25 +0000 (03:53 +0200)
committerJesus Cea <jcea@jcea.es>
Wed, 21 Sep 2011 01:53:25 +0000 (03:53 +0200)
Lib/test/test_multiprocessing.py
Misc/NEWS
Modules/_multiprocessing/multiprocessing.c

index 5d734ed8a0ff1150211bd3bb7bb0391c2069c9d4..1d84ca874940a426c24ade3c8beb228f1cafb7d9 100644 (file)
@@ -1632,6 +1632,23 @@ class _TestConnection(BaseTestCase):
         with open(test.support.TESTFN, "rb") as f:
             self.assertEqual(f.read(), b"bar")
 
+    @classmethod
+    def _send_data_without_fd(self, conn):
+        os.write(conn.fileno(), b"\0")
+
+    @unittest.skipIf(sys.platform == "win32", "doesn't make sense on Windows")
+    def test_missing_fd_transfer(self):
+        # Check that exception is raised when received data is not
+        # accompanied by a file descriptor in ancillary data.
+        if self.TYPE != 'processes':
+            self.skipTest("only makes sense with processes")
+        conn, child_conn = self.Pipe(duplex=True)
+
+        p = self.Process(target=self._send_data_without_fd, args=(child_conn,))
+        p.daemon = True
+        p.start()
+        self.assertRaises(RuntimeError, reduction.recv_handle, conn)
+        p.join()
 
 class _TestListenerClient(BaseTestCase):
 
index 1d0338524697c14f686a3773d11a73b681507b40..6f4cb0a3b30ae7bb3c8c836f1151efedebf42af4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -78,6 +78,9 @@ Tests
 Extension Modules
 -----------------
  
+- Issue #13022: Fix: _multiprocessing.recvfd() doesn't check that
+  file descriptor was actually received.
+
 - Issue #12483: ctypes: Fix a crash when the destruction of a callback
   object triggers the garbage collector.
 
index bc635da74da7553ce5eb750b44ada96e38b7202b..83df1eb7503a1bd07a3ad27542dc886a5473dbe6 100644 (file)
@@ -177,6 +177,17 @@ multiprocessing_recvfd(PyObject *self, PyObject *args)
     if (res < 0)
         return PyErr_SetFromErrno(PyExc_OSError);
 
+    if (msg.msg_controllen < CMSG_LEN(sizeof(int)) ||
+        (cmsg = CMSG_FIRSTHDR(&msg)) == NULL ||
+        cmsg->cmsg_level != SOL_SOCKET ||
+        cmsg->cmsg_type != SCM_RIGHTS ||
+        cmsg->cmsg_len < CMSG_LEN(sizeof(int))) {
+        /* If at least one control message is present, there should be
+           no room for any further data in the buffer. */
+        PyErr_SetString(PyExc_RuntimeError, "No file descriptor received");
+        return NULL;
+    }
+
     fd = * (int *) CMSG_DATA(cmsg);
     return Py_BuildValue("i", fd);
 }