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: Modules/posixmodule.c
Commit date: 2024-05-02 19:43:54 +0100
Commit message: gh-118486: Switch mkdir(mode=0o700) on Windows to use OWNER RIGHTS instead of CURRENT_USER (GH-118515)


Diff: diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index f9533577a8fa..e1a14e772c4b 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5585,10 +5585,11 @@ Typical use will look like:
 struct _Py_SECURITY_ATTRIBUTE_DATA {
     SECURITY_ATTRIBUTES securityAttributes;
     PACL acl;
     SECURITY_DESCRIPTOR sd;
     EXPLICIT_ACCESS_W ea[4];
+    char sid[64];
 };
 
 static int
 initializeDefaultSecurityAttributes(
     PSECURITY_ATTRIBUTES *securityAttributes,
@@ -5614,17 +5615,29 @@ initializeMkdir700SecurityAttributes(
     if (!InitializeSecurityDescriptor(&data->sd, SECURITY_DESCRIPTOR_REVISION)
         || !SetSecurityDescriptorGroup(&data->sd, NULL, TRUE)) {
         return GetLastError();
     }
 
+    int use_alias = 0;
+    DWORD cbSid = sizeof(data->sid);
+    if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, NULL, (PSID)data->sid, &cbSid)) {
+        use_alias = 1;
+    }
+
     data->securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
     data->ea[0].grfAccessPermissions = GENERIC_ALL;
     data->ea[0].grfAccessMode = SET_ACCESS;
     data->ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
-    data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
-    data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_ALIAS;
-    data->ea[0].Trustee.ptstrName = L"CURRENT_USER";
+    if (use_alias) {
+        data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
+        data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_ALIAS;
+        data->ea[0].Trustee.ptstrName = L"CURRENT_USER";
+    } else {
+        data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
+        data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
+        data->ea[0].Trustee.ptstrName = (LPWCH)(SID*)data->sid;
+    }
 
     data->ea[1].grfAccessPermissions = GENERIC_ALL;
     data->ea[1].grfAccessMode = SET_ACCESS;
     data->ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
     data->ea[1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;

Vulnerable code:     data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
    data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_ALIAS;
    data->ea[0].Trustee.ptstrName = L"CURRENT_USER";
Fixed code:     char sid[64];
    int use_alias = 0;
    DWORD cbSid = sizeof(data->sid);
    if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, NULL, (PSID)data->sid, &cbSid)) {
        use_alias = 1;
    }

    if (use_alias) {
        data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
        data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_ALIAS;
        data->ea[0].Trustee.ptstrName = L"CURRENT_USER";
    } else {
        data->ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
        data->ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
        data->ea[0].Trustee.ptstrName = (LPWCH)(SID*)data->sid;
    }