Skip to content

Commit e6408fc

Browse files
committed
🐛 fix(strict): verify the opened sentinel's UID
Pass the open sentinel to the sticky-directory ownership check so a caller whose UID matches the overflow value can use its own sentinel.
1 parent aa3eff1 commit e6408fc

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎docs/concepts.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ A symlink swapped in for either directory then fails the operation instead of re
184184
On POSIX, acquisition also refuses a strict sentinel that another account owns under a sticky parent. That account
185185
could remove the sentinel during a strict hold and let a legacy soft lock enter despite the strict claims.
186186

187+
If an older release created a sentinel that other accounts can write, stop all clients using that path before replacing
188+
the sentinel. On the next acquire, ``StrictSoftFileLock`` creates one without group or other write permission.
189+
187190
An unrelated directory of the locking account can also be renamed to either protocol name where another account can
188191
write the lock's parent. Outside Windows, acquisition checks that ``<path>.filelock`` holds only a real ``claims``
189192
directory, and that ``claims`` holds only claim records, their private records, claim probes or temporary ``.nfs``

‎src/filelock/_strict.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def _acquire(self) -> None:
216216
try:
217217
sentinel_stat = os.fstat(sentinel_fd)
218218
if (
219-
sys.platform != "win32" and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat())) is not None
219+
sys.platform != "win32"
220+
and (owner := foreign_owner(sentinel_stat, lock_path.parent.stat(), fd=sentinel_fd)) is not None
220221
): # pragma: needs foreign-sticky-tmp
221222
reason = f"sentinel belongs to uid {owner}, not to this process's account or the directory's owner"
222223
raise SoftFileLockProtocolError(self.lock_file, None, reason) # ruff:ignore[raise-within-try] # close

‎tests/test_strict_soft_failures.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,12 +1629,22 @@ def test_strict_soft_refuses_coordination_directory_of_another_account( # pragm
16291629

16301630

16311631
@NEEDS_FOREIGN_STICKY_TMP
1632-
@pytest.mark.usefixtures("directories_of_another_account")
16331632
def test_strict_soft_refuses_sentinel_of_another_account( # pragma: needs foreign-sticky-tmp
16341633
lock_path_in_shared_tmp: Path,
1634+
mocker: MockerFixture,
16351635
) -> None:
16361636
sentinel = b"1\nfilelock-strict-v1\x00\n0\n"
16371637
lock_path_in_shared_tmp.write_bytes(sentinel)
1638+
sentinel_stat = lock_path_in_shared_tmp.stat()
1639+
fstat = os.fstat
1640+
1641+
def owner_of_fd(fd: int) -> os.stat_result:
1642+
info = fstat(fd)
1643+
if os.path.samestat(info, sentinel_stat):
1644+
return os.stat_result((*info[:4], info.st_uid + 1, *info[5:]))
1645+
return info
1646+
1647+
mocker.patch.object(os, "fstat", autospec=True, side_effect=owner_of_fd)
16381648
lock = StrictSoftFileLock(lock_path_in_shared_tmp, timeout=0)
16391649

16401650
try:

0 commit comments

Comments
 (0)