Debian Codegraph @

testsuite/acls-unpinnable_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --acls must update a destination ACL even when the destination entry cannot be
3 # pinned with O_RDONLY|O_NOFOLLOW -- e.g. a pre-existing no-owner-read (0300)
4 # directory, whose held_fd open fails for a non-root receiver.
5 #
6 # The receiver's hardened ACL path applies via a held fd, or via the *xattrat
7 # syscalls (dirfd+leaf, AT_SYMLINK_NOFOLLOW) on Linux 6.13+. Where neither is
8 # available -- no held fd (un-pinnable entry) AND no *xattrat (pre-6.13 / BSD) --
9 # rsync falls back to the path-based sys_acl_set_file() so --acls stays
10 # functional (project policy: prefer functionality where the OS cannot offer the
11 # race-safe primitive; the parent-symlink race is an accepted residual there,
12 # and a current kernel takes the secure xattrat path).
13 #
14 # An earlier revision skipped that fallback with a warning + success, silently
15 # leaving the destination ACL STALE -- so a revoked/changed source ACL did not
16 # propagate while rsync reported success. This asserts the new ACL lands and the
17 # stale entry is gone. It exercises the path-based fallback when run non-root on
18 # a no-*xattrat kernel; elsewhere it goes through the held-fd / xattrat path and
19 # still asserts the same functional outcome.
20
21 import os
22 import platform
23 import shutil
24 import subprocess
25
26 from rsyncfns import (
27 SCRATCHDIR, forced_protocol, rmtree, run_rsync, test_fail, test_skipped,
28 )
29
30 NEW_UID = 60002 # the source ACL grant that must propagate
31 STALE_UID = 60009 # a different grant pre-seeded on the dest; must be gone
32
33 if platform.system() != 'Linux':
34 test_skipped("POSIX ACL test is Linux-only (setfacl/getfacl semantics)")
35 if not shutil.which('setfacl') or not shutil.which('getfacl'):
36 test_skipped("setfacl/getfacl not available")
37 if '"ACLs": true' not in run_rsync('-VV', check=True, capture_output=True).stdout:
38 test_skipped("rsync built without ACL support")
39 proto = forced_protocol()
40 if proto is not None and proto < 30:
41 test_skipped(f"ACL transfer requires protocol 30+ (negotiated {proto})")
42
43
44 def setfacl(spec, path):
45 if subprocess.run(['setfacl', '-m', spec, str(path)]).returncode != 0:
46 test_skipped("filesystem has ACLs disabled (setfacl failed)")
47
48
49 def getfacl(path) -> str:
50 # POSIXLY_CORRECT in the suite: plain getfacl only (no -c/-E).
51 return subprocess.run(['getfacl', str(path)],
52 capture_output=True, text=True).stdout
53
54
55 base = SCRATCHDIR / 'acls-unpinnable'
56 src = base / 'src'
57 dest = base / 'dest'
58 rmtree(base)
59 (src / 'dropbox').mkdir(parents=True)
60 (src / 'top.txt').write_text('top\n')
61 (dest / 'dropbox').mkdir(parents=True)
62
63 # Source: the grant that must propagate. Dest: a pre-existing 0300 directory
64 # carrying a *different* (stale) grant, so the sync must change its ACL -- and
65 # the dir is un-pinnable (no owner read) for a non-root receiver at apply time.
66 setfacl(f'user:{NEW_UID}:rwx', src / 'dropbox')
67 os.chmod(src / 'dropbox', 0o300)
68 setfacl(f'user:{STALE_UID}:rwx', dest / 'dropbox')
69 os.chmod(dest / 'dropbox', 0o300)
70
71 # The 0300 dirs are unreadable, so listing them errors (exit 23); the directory
72 # entry + its ACL still transfer, which is what we assert. Accept 0 or 23.
73 proc = run_rsync('-aA', f'{src}/', f'{dest}/', check=False, capture_output=True)
74 if proc.returncode not in (0, 23):
75 test_fail(f"rsync exited {proc.returncode}:\n{proc.stderr}")
76
77 acl = getfacl(dest / 'dropbox')
78 if f'user:{NEW_UID}:' not in acl:
79 test_fail("--acls did not apply the source ACL to the un-pinnable (0300) "
80 f"destination directory (stale/missing ACL); getfacl:\n{acl}")
81 if f'user:{STALE_UID}:' in acl:
82 test_fail("--acls left the stale destination ACL entry in place on the "
83 f"un-pinnable (0300) directory -- a revocation did not propagate; "
84 f"getfacl:\n{acl}")
85
86 print("acls-unpinnable: --acls updates the ACL of a no-owner-read (0300) dir")