diff --git a/docs/changelog/+strict-foreign-sentinel.bugfix.rst b/docs/changelog/+strict-foreign-sentinel.bugfix.rst new file mode 100644 index 000000000..bfaaeb1bc --- /dev/null +++ b/docs/changelog/+strict-foreign-sentinel.bugfix.rst @@ -0,0 +1,3 @@ +On POSIX, :class:`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 ` enter despite the strict claims. diff --git a/docs/concepts.rst b/docs/concepts.rst index 4df9a91e5..1a9ec3376 100644 --- a/docs/concepts.rst +++ b/docs/concepts.rst @@ -181,6 +181,13 @@ as ``/tmp``, acquisition raises :class:`SoftFileLockProtocolError .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 ``.filelock`` holds only a real ``claims`` directory, and that ``claims`` holds only claim records, their private records, claim probes or temporary ``.nfs`` diff --git a/docs/how-to.rst b/docs/how-to.rst index 75e43c1c3..127b05dc4 100644 --- a/docs/how-to.rst +++ b/docs/how-to.rst @@ -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`` diff --git a/src/filelock/_strict.py b/src/filelock/_strict.py index 663c8afb5..c01bf1472 100644 --- a/src/filelock/_strict.py +++ b/src/filelock/_strict.py @@ -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) @@ -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. diff --git a/tests/test_strict_soft_failures.py b/tests/test_strict_soft_failures.py index 66c9ee1c4..352adcfd9 100644 --- a/tests/test_strict_soft_failures.py +++ b/tests/test_strict_soft_failures.py @@ -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"