From 40992fd4c30b72de053c3b4d2b252bf850bd8554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Thu, 19 Nov 2026 22:41:43 -0800 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20fix(strict):=20reject=20fore?= =?UTF-8?q?ign-owned=20sentinels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An attacker who owns the compatibility sentinel in a shared sticky directory can remove it during a strict hold. A legacy soft lock can then enter while the strict lock remains held. Refuse that sentinel before treating it as part of a strict acquisition. --- .../+strict-foreign-sentinel.bugfix.rst | 3 +++ docs/concepts.rst | 3 +++ docs/how-to.rst | 3 ++- src/filelock/_strict.py | 8 +++++++- tests/test_strict_soft_failures.py | 17 +++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/+strict-foreign-sentinel.bugfix.rst 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..12fae14ea 100644 --- a/docs/concepts.rst +++ b/docs/concepts.rst @@ -181,6 +181,9 @@ 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. + 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..bb73b5ecd 100644 --- a/src/filelock/_strict.py +++ b/src/filelock/_strict.py @@ -232,7 +232,13 @@ def _acquire(self) -> None: if sentinel_fd is None: return try: - sentinel_identity = _file_identity(os.fstat(sentinel_fd)) + sentinel_stat = os.fstat(sentinel_fd) + if ( + sys.platform != "win32" and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat())) 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) # ruff:ignore[raise-within-try] # close descriptor below + sentinel_identity = _file_identity(sentinel_stat) except BaseException as inspection_error: # preserve inspection and descriptor cleanup errors try: os.close(sentinel_fd) diff --git a/tests/test_strict_soft_failures.py b/tests/test_strict_soft_failures.py index 66c9ee1c4..e009744af 100644 --- a/tests/test_strict_soft_failures.py +++ b/tests/test_strict_soft_failures.py @@ -1869,6 +1869,23 @@ def test_strict_soft_refuses_coordination_directory_of_another_account( # pragm assert list(coordination.iterdir()) == [] +@NEEDS_FOREIGN_STICKY_TMP +@pytest.mark.usefixtures("directories_of_another_account") +def test_strict_soft_refuses_sentinel_of_another_account( # pragma: needs foreign-sticky-tmp + lock_path_in_shared_tmp: Path, +) -> None: + sentinel = b"1\nfilelock-strict-v1\x00\n0\n" + lock_path_in_shared_tmp.write_bytes(sentinel) + 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" From 08dcef6a45a8bbf7752e873440a000e184afb487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Thu, 19 Nov 2026 22:45:56 -0800 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=92=84=20style(strict):=20shorten=20c?= =?UTF-8?q?leanup=20suppression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/filelock/_strict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filelock/_strict.py b/src/filelock/_strict.py index bb73b5ecd..a229f3492 100644 --- a/src/filelock/_strict.py +++ b/src/filelock/_strict.py @@ -237,7 +237,7 @@ def _acquire(self) -> None: sys.platform != "win32" and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat())) 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) # ruff:ignore[raise-within-try] # close descriptor below + raise SoftFileLockProtocolError(self.lock_file, None, reason) # ruff:ignore[raise-within-try] # close sentinel_identity = _file_identity(sentinel_stat) except BaseException as inspection_error: # preserve inspection and descriptor cleanup errors try: From 07a63d9455f241a80ee85a50f4922f57b6254bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Fri, 20 Nov 2026 07:48:00 -0800 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20fix(strict):=20verify=20the?= =?UTF-8?q?=20opened=20sentinel's=20UID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass the open sentinel to the sticky-directory ownership check so a caller whose UID matches the overflow value can use its own sentinel. --- docs/concepts.rst | 3 +++ src/filelock/_strict.py | 3 ++- tests/test_strict_soft_failures.py | 12 +++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/concepts.rst b/docs/concepts.rst index 12fae14ea..69a682b48 100644 --- a/docs/concepts.rst +++ b/docs/concepts.rst @@ -184,6 +184,9 @@ A symlink swapped in for either directory then fails the operation instead of re 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. On the next acquire, ``StrictSoftFileLock`` creates one 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/src/filelock/_strict.py b/src/filelock/_strict.py index a229f3492..3fa0cdf99 100644 --- a/src/filelock/_strict.py +++ b/src/filelock/_strict.py @@ -234,7 +234,8 @@ def _acquire(self) -> None: try: sentinel_stat = os.fstat(sentinel_fd) if ( - sys.platform != "win32" and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat())) is not None + sys.platform != "win32" + and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat(), fd=sentinel_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) # ruff:ignore[raise-within-try] # close diff --git a/tests/test_strict_soft_failures.py b/tests/test_strict_soft_failures.py index e009744af..352adcfd9 100644 --- a/tests/test_strict_soft_failures.py +++ b/tests/test_strict_soft_failures.py @@ -1870,12 +1870,22 @@ def test_strict_soft_refuses_coordination_directory_of_another_account( # pragm @NEEDS_FOREIGN_STICKY_TMP -@pytest.mark.usefixtures("directories_of_another_account") 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: From 2e076f7435b8171128554ca145b4c77e8693e48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Fri, 20 Nov 2026 07:50:55 -0800 Subject: [PATCH 4/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(strict):=20is?= =?UTF-8?q?olate=20sentinel=20ownership?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/filelock/_strict.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/filelock/_strict.py b/src/filelock/_strict.py index 3fa0cdf99..c01bf1472 100644 --- a/src/filelock/_strict.py +++ b/src/filelock/_strict.py @@ -232,14 +232,7 @@ def _acquire(self) -> None: if sentinel_fd is None: return try: - sentinel_stat = os.fstat(sentinel_fd) - if ( - sys.platform != "win32" - and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat(), fd=sentinel_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) # ruff:ignore[raise-within-try] # close - sentinel_identity = _file_identity(sentinel_stat) + 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) @@ -285,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. From 442f18415117fd126c6e158e036270e05de54265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Fri, 20 Nov 2026 11:52:16 -0800 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9D=20docs(strict):=20explain=20se?= =?UTF-8?q?ntinel=20replacement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/concepts.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/concepts.rst b/docs/concepts.rst index 69a682b48..1a9ec3376 100644 --- a/docs/concepts.rst +++ b/docs/concepts.rst @@ -185,7 +185,8 @@ On POSIX, acquisition also refuses a strict sentinel that another account owns u 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. On the next acquire, ``StrictSoftFileLock`` creates one without group or other write permission. +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``