Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/+strict-foreign-sentinel.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
On POSIX, :class:`StrictSoftFileLock <filelock.StrictSoftFileLock>` now refuses a compatibility sentinel owned by
another account under a sticky parent. Its owner could remove the sentinel during a strict hold, letting a
:class:`SoftFileLock <filelock.SoftFileLock>` enter despite the strict claims.
7 changes: 7 additions & 0 deletions docs/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ as ``/tmp``, acquisition raises :class:`SoftFileLockProtocolError <filelock.Soft
``dir_fd``, the lock reaches ``claims`` through a descriptor of ``<path>.filelock`` opened without following symlinks.
A symlink swapped in for either directory then fails the operation instead of redirecting it.

On POSIX, acquisition also refuses a strict sentinel that another account owns under a sticky parent. That account
could remove the sentinel during a strict hold and let a legacy soft lock enter despite the strict claims.

If an older release created a sentinel that other accounts can write, stop all clients using that path before replacing
the sentinel. Changing its mode leaves existing writable descriptors usable. On the next acquire,
``StrictSoftFileLock`` creates a sentinel without group or other write permission.

An unrelated directory of the locking account can also be renamed to either protocol name where another account can
write the lock's parent. Outside Windows, acquisition checks that ``<path>.filelock`` holds only a real ``claims``
directory, and that ``claims`` holds only claim records, their private records, claim probes or temporary ``.nfs``
Expand Down
3 changes: 2 additions & 1 deletion docs/how-to.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,8 @@ blocks rather than breaking in. Only these combinations hold:
- Until a claim expires; then the old holder overlaps its successor.
- - ``SoftFileLock`` with ``StrictSoftFileLock``
- Yes, from filelock 3.30.0 on: the strict sentinel is recognized, so the legacy contender waits. A pre-3.30
``SoftFileLock`` misreads the sentinel and can break in.
``SoftFileLock`` misreads the sentinel and can break in. On POSIX, strict acquisition refuses a sentinel owned
by another account under a sticky parent, since its owner could remove it during the hold.
- - ``SoftFileLock`` with ``SoftFileLease``
- No. The legacy contender reads the protocol 2 marker as malformed and evicts it.
- - ``StrictSoftFileLock`` with ``SoftFileLease``
Expand Down
11 changes: 10 additions & 1 deletion src/filelock/_strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _acquire(self) -> None:
if sentinel_fd is None:
return
try:
sentinel_identity = _file_identity(os.fstat(sentinel_fd))
sentinel_identity = self._sentinel_identity(sentinel_fd, lock_path)
except BaseException as inspection_error: # preserve inspection and descriptor cleanup errors
try:
os.close(sentinel_fd)
Expand Down Expand Up @@ -278,6 +278,15 @@ def _occupant_unchanged_for(self, occupant: os.stat_result) -> float:
return 0.0
return now - seen[1]

def _sentinel_identity(self, fd: int, path: Path) -> tuple[int, int]:
found = os.fstat(fd)
if (
sys.platform != "win32" and (owner := foreign_owner(found, path.parent.stat(), fd=fd)) is not None
): # pragma: needs foreign-sticky-tmp
reason = f"sentinel belongs to uid {owner}, not to this process's account or the directory's owner"
raise SoftFileLockProtocolError(self.lock_file, None, reason)
return _file_identity(found)

def _attempt_doorway(self, claim_directory: Path) -> bool:
try:
# Leave room for both public claims, the private record and a filesystem clock probe.
Expand Down
27 changes: 27 additions & 0 deletions tests/test_strict_soft_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,33 @@ def test_strict_soft_refuses_coordination_directory_of_another_account( # pragm
assert list(coordination.iterdir()) == []


@NEEDS_FOREIGN_STICKY_TMP
def test_strict_soft_refuses_sentinel_of_another_account( # pragma: needs foreign-sticky-tmp
lock_path_in_shared_tmp: Path,
mocker: MockerFixture,
) -> None:
sentinel = b"1\nfilelock-strict-v1\x00\n0\n"
lock_path_in_shared_tmp.write_bytes(sentinel)
sentinel_stat = lock_path_in_shared_tmp.stat()
fstat = os.fstat

def owner_of_fd(fd: int) -> os.stat_result:
info = fstat(fd)
if os.path.samestat(info, sentinel_stat):
return os.stat_result((*info[:4], info.st_uid + 1, *info[5:]))
return info

mocker.patch.object(os, "fstat", autospec=True, side_effect=owner_of_fd)
lock = StrictSoftFileLock(lock_path_in_shared_tmp, timeout=0)

try:
with pytest.raises(SoftFileLockProtocolError, match="sentinel belongs to uid"):
lock.acquire()
finally:
lock.release(force=True)
assert lock_path_in_shared_tmp.read_bytes() == sentinel


@pytest.mark.skipif(sys.platform == "win32", reason="Windows has no directory mode to settle")
def test_strict_soft_refuses_unrelated_directory_at_coordination_name(tmp_path: Path) -> None: # pragma: win32 no cover
lock_path: Final[Path] = tmp_path / "resource.lock"
Expand Down
Loading