Skip to content

Commit 7daf40d

Browse files
authored
🐛 fix(strict): reject foreign-owned sentinels (#992)
1 parent a8a2b5a commit 7daf40d

5 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On POSIX, :class:`StrictSoftFileLock <filelock.StrictSoftFileLock>` now refuses a compatibility sentinel owned by
2+
another account under a sticky parent. Its owner could remove the sentinel during a strict hold, letting a
3+
:class:`SoftFileLock <filelock.SoftFileLock>` enter despite the strict claims.

‎docs/concepts.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ as ``/tmp``, acquisition raises :class:`SoftFileLockProtocolError <filelock.Soft
181181
``dir_fd``, the lock reaches ``claims`` through a descriptor of ``<path>.filelock`` opened without following symlinks.
182182
A symlink swapped in for either directory then fails the operation instead of redirecting it.
183183

184+
On POSIX, acquisition also refuses a strict sentinel that another account owns under a sticky parent. That account
185+
could remove the sentinel during a strict hold and let a legacy soft lock enter despite the strict claims.
186+
187+
If an older release created a sentinel that other accounts can write, stop all clients using that path before replacing
188+
the sentinel. Changing its mode leaves existing writable descriptors usable. On the next acquire,
189+
``StrictSoftFileLock`` creates a sentinel without group or other write permission.
190+
184191
An unrelated directory of the locking account can also be renamed to either protocol name where another account can
185192
write the lock's parent. Outside Windows, acquisition checks that ``<path>.filelock`` holds only a real ``claims``
186193
directory, and that ``claims`` holds only claim records, their private records, claim probes or temporary ``.nfs``

‎docs/how-to.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,8 @@ blocks rather than breaking in. Only these combinations hold:
12001200
- Until a claim expires; then the old holder overlaps its successor.
12011201
- - ``SoftFileLock`` with ``StrictSoftFileLock``
12021202
- Yes, from filelock 3.30.0 on: the strict sentinel is recognized, so the legacy contender waits. A pre-3.30
1203-
``SoftFileLock`` misreads the sentinel and can break in.
1203+
``SoftFileLock`` misreads the sentinel and can break in. On POSIX, strict acquisition refuses a sentinel owned
1204+
by another account under a sticky parent, since its owner could remove it during the hold.
12041205
- - ``SoftFileLock`` with ``SoftFileLease``
12051206
- No. The legacy contender reads the protocol 2 marker as malformed and evicts it.
12061207
- - ``StrictSoftFileLock`` with ``SoftFileLease``

‎src/filelock/_strict.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def _acquire(self) -> None:
232232
if sentinel_fd is None:
233233
return
234234
try:
235-
sentinel_identity = _file_identity(os.fstat(sentinel_fd))
235+
sentinel_identity = self._sentinel_identity(sentinel_fd, lock_path)
236236
except BaseException as inspection_error: # preserve inspection and descriptor cleanup errors
237237
try:
238238
os.close(sentinel_fd)
@@ -278,6 +278,15 @@ def _occupant_unchanged_for(self, occupant: os.stat_result) -> float:
278278
return 0.0
279279
return now - seen[1]
280280

281+
def _sentinel_identity(self, fd: int, path: Path) -> tuple[int, int]:
282+
found = os.fstat(fd)
283+
if (
284+
sys.platform != "win32" and (owner := foreign_owner(found, path.parent.stat(), fd=fd)) is not None
285+
): # pragma: needs foreign-sticky-tmp
286+
reason = f"sentinel belongs to uid {owner}, not to this process's account or the directory's owner"
287+
raise SoftFileLockProtocolError(self.lock_file, None, reason)
288+
return _file_identity(found)
289+
281290
def _attempt_doorway(self, claim_directory: Path) -> bool:
282291
try:
283292
# Leave room for both public claims, the private record and a filesystem clock probe.

‎tests/test_strict_soft_failures.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,33 @@ def test_strict_soft_refuses_coordination_directory_of_another_account( # pragm
18691869
assert list(coordination.iterdir()) == []
18701870

18711871

1872+
@NEEDS_FOREIGN_STICKY_TMP
1873+
def test_strict_soft_refuses_sentinel_of_another_account( # pragma: needs foreign-sticky-tmp
1874+
lock_path_in_shared_tmp: Path,
1875+
mocker: MockerFixture,
1876+
) -> None:
1877+
sentinel = b"1\nfilelock-strict-v1\x00\n0\n"
1878+
lock_path_in_shared_tmp.write_bytes(sentinel)
1879+
sentinel_stat = lock_path_in_shared_tmp.stat()
1880+
fstat = os.fstat
1881+
1882+
def owner_of_fd(fd: int) -> os.stat_result:
1883+
info = fstat(fd)
1884+
if os.path.samestat(info, sentinel_stat):
1885+
return os.stat_result((*info[:4], info.st_uid + 1, *info[5:]))
1886+
return info
1887+
1888+
mocker.patch.object(os, "fstat", autospec=True, side_effect=owner_of_fd)
1889+
lock = StrictSoftFileLock(lock_path_in_shared_tmp, timeout=0)
1890+
1891+
try:
1892+
with pytest.raises(SoftFileLockProtocolError, match="sentinel belongs to uid"):
1893+
lock.acquire()
1894+
finally:
1895+
lock.release(force=True)
1896+
assert lock_path_in_shared_tmp.read_bytes() == sentinel
1897+
1898+
18721899
@pytest.mark.skipif(sys.platform == "win32", reason="Windows has no directory mode to settle")
18731900
def test_strict_soft_refuses_unrelated_directory_at_coordination_name(tmp_path: Path) -> None: # pragma: win32 no cover
18741901
lock_path: Final[Path] = tmp_path / "resource.lock"

0 commit comments

Comments
 (0)