Skip to content

Fix race condition in copyfile() causing inotify permission errors#68860

Open
wblinux wants to merge 1 commit intosaltstack:masterfrom
wblinux:fix/copyfile-set-perms-before-move
Open

Fix race condition in copyfile() causing inotify permission errors#68860
wblinux wants to merge 1 commit intosaltstack:masterfrom
wblinux:fix/copyfile-set-perms-before-move

Conversation

@wblinux
Copy link
Copy Markdown

@wblinux wblinux commented Mar 28, 2026

What does this PR do?

Moves chown/chmod from after shutil.move() to before it in
salt.utils.files.copyfile(), applying the destination file's
ownership and permissions to the temp file before the atomic rename.

What issues does this PR fix or reference?

Fixes #65651

Previous Behavior

mkstemp() created a temp file with default 0600 permissions.
shutil.move() atomically replaced the destination, then chown/chmod
was applied afterward. This left a brief window where the destination
file had restrictive permissions. Services using inotify (e.g. nscd
watching /etc/resolv.conf) would detect the replacement via
IN_DELETE_SELF, fail to re-add a file watch with "Permission denied",
and lose functionality (e.g. DNS resolution failure).

New Behavior

Ownership and permissions are applied to the temp file before
shutil.move(), so the destination file is never visible with
incorrect permissions. If chown/chmod fails, the temp file is
cleaned up and the error is propagated.

Merge requirements satisfied?

  • Docs
  • Changelog
  • Tests written/updated

Commits signed with GPG?

No

Apply the destination file's ownership and permissions to the temp
file BEFORE the atomic move, instead of after.

Previously, mkstemp() created the temp file with default 0600
permissions. After shutil.move() atomically replaced the destination,
there was a brief window where the file was visible with restrictive
permissions. Services using inotify to watch managed files (e.g. nscd
watching /etc/resolv.conf) would detect the replacement via
IN_DELETE_SELF, then fail to re-add a file watch with "Permission
denied" due to the 0600 permissions, leading to service failures
such as DNS resolution loss.

By setting chown/chmod on the temp file before shutil.move(), the
destination file is never visible with incorrect permissions,
eliminating the race condition entirely.

Fixes saltstack#65651
@wblinux wblinux requested a review from a team as a code owner March 28, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][DISCUSSION] file.manage_file might cause race condition due to permission config

1 participant