Tcl Source Code

View Ticket
Login
Ticket UUID: cd609219f5522f3191a68a84deb51a968dfb8cac
Title: directory creation temporarily changes the umask
Type: Bug Created on: 2023-04-05 18:01:59
Submitter: christian.nassau Assigned to: nobody
Subsystem: 16. Commands A-H Severity: Important
Priority: 5 Medium Last modified: 2026-10-17 09:00:11
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:

In unix/tclUnixFCmd.c the following code is used to determine the process's current "umask":

mode = umask(0);
umask(mode)
In the (usually miniscule) period between the two calls, the umask is temporarily set to 0 which results in elevated permissions for newly created files at that point. As noted in the umask man page for modern Linux systems with a /proc filesystem there is a better way to determine the umask. It might make sense to use this when available.

User Comments:
christian.nassau added on 2023-04-06 10:43:17:

Note that in a multi-threaded application this code can be highly problematic: if two threads simultaneously try to create a directory, both might be stepping through these lines at the same time. If it happens in this sequence the processes' umask will end up permanently set to "0":

thread 1           | thread 2
mode = umask(0);   |
                   | mode = umask(0);
umask(mode)        |
                   | umask(mode)
If a multithreaded process spawns a pool of threads which all try to create the same directory on startup that might not be an unlikely coincidence.


serhiy.storchaka added on 2026-10-17 09:00:11:

Confirmed, and worse than temporary: with several threads running [file mkdir], the process umask ends up permanently at 0, so later files are created world-writable (main, 9.0, 8.6). Proposed fix in branch mkdir-no-umask-call, with [623585ffff]: call mkdir(path, 0777) and let the kernel apply the umask; umask() is not called any more. The permissions of new directories do not change. Test unixFCmd-5.2. The old code also meant to always add u+rwx, but that never worked, because mkdir() applies the umask once more (umask 0700 gives 077). It could be done with chmod() after mkdir(), but that would change the permissions of new directories under such umasks, and it is not documented, so the branch does not do it.