The vulnerability is a Time-of-check Time-of-use (TOCTOU) race condition in the mkdir utility of uutils/coreutils. The patch addresses this by changing how directories are created when a specific mode is requested with the -m flag.
The analysis of the commit 037b9583bc03d814e8516df54ebcda6f681fe1f8 reveals the core of the issue. The vulnerable logic was located in the create_single_dir function in src/uu/mkdir/src/mkdir.rs. Previously, this function would call std::fs::create_dir() to create the directory, which would use the system's umask for initial permissions, and then it would call a chmod function to apply the mode specified by the user. This creates a small time window where the directory exists with incorrect, potentially less restrictive, permissions.
The fix involves introducing a new function, create_dir_with_mode, which wraps the directory creation in a UmaskGuard. This guard temporarily sets the umask to 0, ensuring that the directory is created atomically with the exact permissions specified by the user, thus eliminating the race condition. The create_single_dir function was then updated to use this new atomic creation method.
Based on this, the function create_single_dir is identified as the vulnerable function because it contained the flawed logic of creating and then changing permissions, which is the root cause of the TOCTOU vulnerability.