Skip to content

πŸ”’ fix(unix): refuse another account's lock file - #968

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/foreign-lock-file-owner
Nov 17, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/foreign-lock-file-owner

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

On Unix, UnixFileLock._finalize_locked_fd recorded a hold on whatever regular file sat at the lock path, checking only that the file still had a link. Under a sticky directory such as /tmp, a file another account created stays that account's to remove. If it removed the file during a hold, the next process created a new file at the path and locked that, so two processes held the same exclusive lock. πŸ”’ The lock now raises PermissionError for such a file. This addresses draft advisory GHSA-386w-3w6q-9c53.

The lock trusted a file another account could remove

_open_lock_file falls back to opening an existing file when its exclusive create fails, including the EACCES Linux returns for another account's file in a sticky directory (fs.protected_regular), a path #317 added. The sticky bit stops other accounts from removing a file but not its owner (unlink(2)), and flock stays with the unlinked inode, so a later O_CREAT | O_EXCL at the same path yields a second, unrelated lock.

_finalize_locked_fd now compares the owner of the opened file with the caller's account and the parent directory's owner while it holds the flock, and raises before the hold is recorded; its caller then closes the descriptor, which drops the flock. The rule is the one StrictSoftFileLock and SoftReadWriteLock apply to their coordination directories (#839), so the helper they share becomes foreign_owner and judges files as well as directories.

sequenceDiagram
    participant O as Other account
    participant P as lock path in /tmp
    participant A as Process A
    participant B as Process B
    O->>P: create the file first
    A->>+P: open existing file, flock
    rect rgb(255, 214, 214)
    P-->>A: before: hold recorded
    O->>P: unlink its own file
    B->>P: create a new file, flock
    P-->>B: hold recorded while A still holds
    end
    rect rgb(216, 243, 220)
    P-->>-A: now: PermissionError, no hold
    end
Loading

Behavior changes

  • Sticky parent, file of another account: acquire() raises PermissionError (EACCES) naming the owning uid, without waiting for the timeout, and takes no hold. This narrows [BugFix] fix permission denied error when lock file is placed in /tmpΒ #317: accounts that share one lock file in /tmp need the directory's owner, usually root, to create it, or a directory of their own.
  • Sticky parent, file of the caller or of the directory's owner: unchanged.
  • Parent without the sticky bit: unchanged. Where other accounts can write such a directory they can remove any file in it, which the directory boundary documents as outside what the lock can defend.
  • Cost: one extra stat of the parent directory per acquisition.
  • Windows, the soft locks and the flock-less fallback: unchanged.

Reported by oss-security-shopify: GHSA-386w-3w6q-9c53.

Under a sticky parent such as /tmp the owner of a lock file can unlink it
while another account holds its flock. The next contender then creates and
locks a new inode at the path, so two processes hold the exclusive lock.

Refuse, with PermissionError, a lock file owned by neither this account nor
the directory's owner, as the soft locks already do for their directories.
The shared helper becomes foreign_owner, since it now judges files too.
@gaborbernat
gaborbernat enabled auto-merge (squash) November 17, 2026 18:02
@gaborbernat
gaborbernat merged commit 571eb99 into tox-dev:main Nov 17, 2026
49 checks passed
@gaborbernat gaborbernat added the security Fixes a published or draft security advisory label Nov 17, 2026
gaborbernat added a commit to tox-dev/github-workflows that referenced this pull request Nov 17, 2026
Adding or removing a label on a merged pull request turned its πŸ“
Changelog check red, even when the pull request carried a fragment ([run
on
tox-dev/filelock#968](https://github.com/tox-dev/filelock/actions/runs/44169907520),
labeled `security` after it merged). Once this is released and callers
bump the pin, those post-merge label changes pass instead of leaving a
failed check on the pull request.

The news check counts fragments between the pull request head and its
merge base with the base branch. After a merge the head is already on
the base branch, so that count is zero. The step that already skips
labels set at opening now also skips any label change on a pull request
that is not open, which covers merged and closed-unmerged ones alike. A
closed pull request gets the full check again from its `reopened` run,
so callers must keep `reopened` in their trigger types, as the docs now
say.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug security Fixes a published or draft security advisory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant