Skip to content

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

Open
wblinux wants to merge 2 commits into
saltstack:masterfrom
wblinux:fix/copyfile-set-perms-before-move
Open

Fix race condition in copyfile() causing inotify permission errors#68860
wblinux wants to merge 2 commits into
saltstack:masterfrom
wblinux:fix/copyfile-set-perms-before-move

Conversation

@wblinux

@wblinux wblinux commented Mar 28, 2026

Copy link
Copy Markdown

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

dwoz
dwoz previously approved these changes Apr 5, 2026
@dwoz
dwoz force-pushed the fix/copyfile-set-perms-before-move branch from ae8cd5f to e410856 Compare April 5, 2026 23:38
@dwoz dwoz added this to the Potassium v3009.0 milestone Jun 7, 2026
@dwoz
dwoz force-pushed the fix/copyfile-set-perms-before-move branch from 6077600 to 49c8e00 Compare June 7, 2026 05:56
twangboy
twangboy previously approved these changes Jul 13, 2026
@twangboy

Copy link
Copy Markdown
Contributor

Please rebase and address conflicts

@wblinux
wblinux dismissed stale reviews from twangboy and dwoz via c65a11c July 14, 2026 12:28
@wblinux
wblinux force-pushed the fix/copyfile-set-perms-before-move branch from 197b3a9 to c65a11c Compare July 14, 2026 12:28
@wblinux
wblinux requested review from dwoz and twangboy July 15, 2026 06:30
Wang Bo added 2 commits July 15, 2026 11:33
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
- Replace open() with salt.utils.files.fopen() to satisfy W8470 and W1514
- Reformat multi-line with-patch statement for black compliance
@twangboy
twangboy force-pushed the fix/copyfile-set-perms-before-move branch from c65a11c to 266e3aa Compare July 15, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

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

3 participants