CVE-2024-4030: Insufficient Information (NVD-CWE-noinfo)
value: On Windows a directory returned by tempfile.mkdtemp() would not always have permissions set to restrict reading and writing to the temporary directory by other users, instead usually inheriting the correct permissions from the default location. Alternate configurations or users without a profile directory may not have the intended permissions.

If you’re not using Windows or haven’t changed the temporary directory location then you aren’t affected by this vulnerability. On other platforms the returned directory is consistently readable and writable only by the current user.

This issue was caused by Python not supporting Unix permissions on Windows. The fix adds support for Unix “700” for the mkdir function on Windows which is used by mkdtemp() to ensure the newly created directory has the proper permissions.

CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
Published: 2024-05-07T21:15Z
Repository: https://github.com/python/cpython
Language: C
Files changed: Lib/test/test_os.py, Modules/posixmodule.c
Commit date: 2024-05-15 11:59:41 +0100
Commit message: gh-118486: Simplify test_win32_mkdir_700 to check the exact ACL (GH-119056)


Diff: diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 9088318600f4..941fa2b2c5c8 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -1812,25 +1812,18 @@ def test_exist_ok_existing_regular_file(self):
         os.remove(path)
 
     @unittest.skipUnless(os.name == 'nt', "requires Windows")
     def test_win32_mkdir_700(self):
         base = os_helper.TESTFN
-        path1 = os.path.join(os_helper.TESTFN, 'dir1')
-        path2 = os.path.join(os_helper.TESTFN, 'dir2')
-        # mode=0o700 is special-cased to override ACLs on Windows
-        # There's no way to know exactly how the ACLs will look, so we'll
-        # check that they are different from a regularly created directory.
-        os.mkdir(path1, mode=0o700)
-        os.mkdir(path2, mode=0o777)
-
-        out1 = subprocess.check_output(["icacls.exe", path1], encoding="oem")
-        out2 = subprocess.check_output(["icacls.exe", path2], encoding="oem")
-        os.rmdir(path1)
-        os.rmdir(path2)
-        out1 = out1.replace(path1, "<PATH>")
-        out2 = out2.replace(path2, "<PATH>")
-        self.assertNotEqual(out1, out2)
+        path = os.path.abspath(os.path.join(os_helper.TESTFN, 'dir'))
+        os.mkdir(path, mode=0o700)
+        out = subprocess.check_output(["cacls.exe", path, "/s"], encoding="oem")
+        os.rmdir(path)
+        self.assertEqual(
+            out.strip(),
+            f'{path} "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)"',
+        )
 
     def tearDown(self):
         path = os.path.join(os_helper.TESTFN, 'dir1', 'dir2', 'dir3',
                             'dir4', 'dir5', 'dir6')
         # If the tests failed, the bottom-most directory ('../dir6')
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 5fe6036b3817..710a171580a4 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5588,11 +5588,11 @@ os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
 #ifdef MS_WINDOWS
     Py_BEGIN_ALLOW_THREADS
     if (mode == 0700 /* 0o700 */) {
         ULONG sdSize;
         pSecAttr = &secAttr;
-        // Set a discreationary ACL (D) that is protected (P) and includes
+        // Set a discretionary ACL (D) that is protected (P) and includes
         // inheritable (OICI) entries that allow (A) full control (FA) to
         // SYSTEM (SY), Administrators (BA), and the owner (OW).
         if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(
             L"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)",
             SDDL_REVISION_1,

Vulnerable code:         path1 = os.path.join(os_helper.TESTFN, 'dir1')
        path2 = os.path.join(os_helper.TESTFN, 'dir2')
        # mode=0o700 is special-cased to override ACLs on Windows
        # There's no way to know exactly how the ACLs will look, so we'll
        # check that they are different from a regularly created directory.
        os.mkdir(path1, mode=0o700)
        os.mkdir(path2, mode=0o777)

        out1 = subprocess.check_output(["icacls.exe", path1], encoding="oem")
        out2 = subprocess.check_output(["icacls.exe", path2], encoding="oem")
        os.rmdir(path1)
        os.rmdir(path2)
        out1 = out1.replace(path1, "<PATH>")
        out2 = out2.replace(path2, "<PATH>")
        self.assertNotEqual(out1, out2)
        // Set a discreationary ACL (D) that is protected (P) and includes
Fixed code:         path = os.path.abspath(os.path.join(os_helper.TESTFN, 'dir'))
        os.mkdir(path, mode=0o700)
        out = subprocess.check_output(["cacls.exe", path, "/s"], encoding="oem")
        os.rmdir(path)
        self.assertEqual(
            out.strip(),
            f'{path} "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)"',
        )
        // Set a discretionary ACL (D) that is protected (P) and includes