Huny's Dev Blog
huny.dev

Linux umask and Unexpected File Permissions: Creation Modes, Reopening, and fchmod

Reproduce file and directory permissions under umask 022 and 077, verify reopening versus fchmod with ten checks, and distinguish ACL and process-wide mask limits.

Hun Jang
Hun Jang Nov 29, 2026

If the same Linux program creates mode-644 files in a terminal but mode-600 files as a service, check both the requested creation mode and the process umask. umask restricts permissions at creation; it does not reset permissions on an existing file. This article compares creation, reopening, and explicit mode changes through Python's low-level file APIs, then considers a design that avoids temporarily changing the mask in a multithreaded service.

A translucent stencil covers selected openings in a document-shaped plate beside a closed folder. A conceptual illustration of umask restricting permissions during creation.

Written and documentation checked: 2026-11-29. Tested environment: Linux x86_64, CPython 3.12.14. Standard library only, tested in temporary directories without a default ACL.

Separate requested mode from resulting permissions

Without a default ACL, ordinary permission bits for a new file are calculated as requested_mode & ~umask. Set bits in the mask are removed from the request. This is not octal subtraction. A default ACL introduces different inheritance rules. See the Linux umask(2) manual.

These are the results required by the executable example. It requests 666 for files and 777 for directories, so one mask produces different results. The mask does not add execute bits absent from the request.

Request
umask 022
umask 077
New file, mode 666
644
600
New directory, mode 777
755
700
New private file, mode 600
600
600

Mode 600 on a file gives its owner read and write permissions; directory mode 700 gives the owner read, write, and search permissions. This table compares ordinary mode bits, rather than promising access outcomes on every filesystem.

Reopening does not update permissions

Passing O_CREAT and mode 666 for an existing file does not update its permissions to that mode. open(2) describes the creation mode as applying when a new file is created. Changing an existing file requires a separate operation.

The example first creates a mode-600 file, reopens it, and calls os.fchmod(fd, 0o640) on the open descriptor. fchmod(2) changes the mode of the file referred to by that descriptor. This is an explicit change separate from the creation mask and requires the applicable ownership or privilege conditions.

Reproduce the behavior with mask changes isolated in children

Save this as umask_demo.py. Each mask is set in a separate Python process, leaving the parent program's mask unchanged. The program creates only temporary files and cleans them up on exit.

New file creation also uses O_EXCL, causing an existing name to fail. Reopening an existing file is a separate step. If the default-ACL check fails or the verification assumptions do not hold, the program raises an exception instead of continuing as though the mask formula always applied.

Actual output:

Five modes were asserted for each of two masks: a file requested as 666, a directory requested as 777, a private file requested as 600, the reopened existing file, and the result of fchmod. Even with mask 077, the explicit change produced 640. Verification inspected mode bits; it did not test access from other user accounts or default-ACL inheritance behavior.

Choose an approach for a service

For a private file, requesting a restrictive mode such as 600 at creation makes the intent clear. Do not make creation with broad permissions followed by a later reduction the default design. If an exact final mode is required, inspect the open descriptor after creation and decide whether an explicit change is needed. Check default ACLs and ownership policy too.

Changing a process mask and restoring it are not an atomic pair of calls. The NOTES in umask(2) describe the possibility of multithreaded races. Avoid putting per-request mask changes in a server's file-saving helper. Set a process-wide default during initialization, and express individual file requirements through creation modes or an isolated worker process. This example reproduces that isolation approach; it does not measure a thread race.

The code uses a Linux extended-attribute name for POSIX ACLs and is not a Windows example. It was not executed against NFS, default ACLs, container volumes, or actual service-manager settings. Mode bits alone also do not establish additional controls such as SELinux, parent-directory access, or ownership. When the same path appears to receive different permissions, narrow the cause by checking the requested mode, process mask, whether the file already existed, and inheritance policy.

한국어 버전: Linux umask로 파일 권한이 달라질 때: 생성 모드·재열기·fchmod 구분하기Linux umask로 파일 권한이 달라질 때: 생성 모드·재열기·fchmod 구분하기

추천 글