Files
mtusi2026lab7/student-tests/secret_file_permissions.py
2026-11-07 22:00:19 +03:00

36 lines
1.3 KiB
Python

"""Linux DAC regression; run as root in a disposable container with synthetic data."""
import os
from pathlib import Path
import subprocess
import sys
import tempfile
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
root.chmod(0o755)
mounted = root / 'mounted'
mounted.write_text('synthetic-secret')
os.chown(mounted, 1000, 12345)
mounted.chmod(0o640)
host = root / 'operator'
host.mkdir(mode=0o700)
os.chown(host, 1000, 12345)
protected = host / 'secret'
protected.write_text('synthetic-secret')
os.chown(protected, 1000, 12345)
protected.chmod(0o640)
def can_read(path, groups):
def identity():
os.setgroups(groups)
os.setgid(65534)
os.setuid(65534)
return subprocess.run([sys.executable, '-c',
'from pathlib import Path; import sys; Path(sys.argv[1]).read_bytes()', str(path)],
preexec_fn=identity, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
assert not can_read(mounted, [])
assert can_read(mounted, [12345])
assert not can_read(protected, [12345])
mounted.chmod(0o600)
assert not can_read(mounted, [12345])
print('PASS Linux DAC: original failure reproduced; group access works; host directory protected')