| 1 |
|
| 2 |
"""Coverage: the generator/delete chmod-around-restrictive-perms paths. |
| 3 |
|
| 4 |
generator.c gen_entry_chmod() and delete.c del_chmod() only fire when |
| 5 |
`!am_root`: a non-root receiver creating files inside a directory whose |
| 6 |
incoming mode lacks S_IRWXU has to temporarily widen it |
| 7 |
(generator.c:~1725, restored at ~2387 in the retouch pass), and a |
| 8 |
non-root receiver deleting a non-writable file it owns adds S_IWUSR |
| 9 |
first (delete.c:~156). Every other test runs the receiver as root in |
| 10 |
the container, so these never trip. |
| 11 |
|
| 12 |
Uses a daemon module with `uid = <non-root>` so the receiver child is |
| 13 |
non-root; the dest tree is chowned to that uid up front. Skips when |
| 14 |
chown is unavailable. |
| 15 |
""" |
| 16 |
|
| 17 |
import os |
| 18 |
import pwd |
| 19 |
import stat |
| 20 |
import subprocess |
| 21 |
|
| 22 |
from rsyncfns import ( |
| 23 |
SCRATCHDIR, |
| 24 |
makepath, owners_supported, rmtree, rsync_argv, start_test_daemon, |
| 25 |
test_fail, test_skipped, write_daemon_conf, |
| 26 |
) |
| 27 |
|
| 28 |
DAEMON_PORT = 12893 |
| 29 |
|
| 30 |
if not owners_supported(): |
| 31 |
test_skipped("needs chown to set up a non-root-owned dest tree") |
| 32 |
|
| 33 |
U = next((p for p in pwd.getpwall() if p.pw_uid != 0), None) |
| 34 |
if U is None: |
| 35 |
test_skipped("no non-root passwd entry") |
| 36 |
|
| 37 |
src = SCRATCHDIR / 'nrp-src' |
| 38 |
dest = SCRATCHDIR / 'nrp-dest' |
| 39 |
rmtree(src); rmtree(dest); makepath(src, dest) |
| 40 |
os.chown(dest, U.pw_uid, U.pw_gid) |
| 41 |
|
| 42 |
conf = write_daemon_conf([ |
| 43 |
('nrp', { |
| 44 |
'path': str(dest), 'read only': 'no', 'use chroot': 'no', |
| 45 |
'uid': str(U.pw_uid), 'gid': str(U.pw_gid), |
| 46 |
}), |
| 47 |
], name='nonroot-perms.conf') |
| 48 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 49 |
|
| 50 |
|
| 51 |
def push(*extra): |
| 52 |
r = subprocess.run( |
| 53 |
rsync_argv('-rp', *extra, f'{src}/', f'{url}nrp/'), |
| 54 |
capture_output=True, text=True, |
| 55 |
) |
| 56 |
if r.returncode != 0: |
| 57 |
test_fail(f"push -rp {' '.join(extra)} -> rc={r.returncode}\n{r.stderr}") |
| 58 |
return r |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
(src / 'restricted').mkdir() |
| 65 |
(src / 'restricted' / 'inner').write_bytes(b'x') |
| 66 |
os.chmod(src / 'restricted', 0o500) |
| 67 |
|
| 68 |
push() |
| 69 |
|
| 70 |
st = os.stat(dest / 'restricted') |
| 71 |
if stat.S_IMODE(st.st_mode) != 0o500: |
| 72 |
test_fail(f"restricted/ perms not restored after retouch: " |
| 73 |
f"{oct(stat.S_IMODE(st.st_mode))} (expected 0o500)") |
| 74 |
if not (dest / 'restricted' / 'inner').is_file(): |
| 75 |
test_fail("restricted/inner not received (gen_entry_chmod widen failed?)") |
| 76 |
if st.st_uid != U.pw_uid: |
| 77 |
test_fail(f"restricted/ not owned by the module uid ({st.st_uid} != {U.pw_uid})") |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
gone = dest / 'gone' |
| 86 |
gone.mkdir() |
| 87 |
ro = gone / 'ro_file' |
| 88 |
ro.write_bytes(b'x') |
| 89 |
os.chmod(ro, 0o444) |
| 90 |
os.chown(ro, U.pw_uid, U.pw_gid) |
| 91 |
os.chown(gone, U.pw_uid, U.pw_gid) |
| 92 |
|
| 93 |
|
| 94 |
os.chmod(src / 'restricted', 0o500) |
| 95 |
|
| 96 |
push('--delete') |
| 97 |
|
| 98 |
if (dest / 'gone').exists(): |
| 99 |
test_fail("--delete did not remove gone/ (del_chmod path)") |
| 100 |
if not (dest / 'restricted' / 'inner').is_file(): |
| 101 |
test_fail("restricted/inner lost on the second pass") |
| 102 |
if stat.S_IMODE(os.stat(dest / 'restricted').st_mode) != 0o500: |
| 103 |
test_fail("restricted/ perms not re-restored on the second pass") |
| 104 |
|
| 105 |
|
| 106 |
print(f"nonroot-restrictive-perms: gen_entry_chmod widen+restore (0500 dir), " |
| 107 |
f"del_chmod on 0444 file, as uid={U.pw_uid}({U.pw_name})") |