Bind Chrony preflight to actual service and command socket holder
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -53,9 +53,13 @@ fn main() {
|
||||
'''
|
||||
|
||||
|
||||
class OperatorDenied(RuntimeError):
|
||||
"""Only explicit operator guards may produce a public failure reason."""
|
||||
|
||||
|
||||
def require(ok, reason):
|
||||
if not ok:
|
||||
raise RuntimeError(reason)
|
||||
raise OperatorDenied(reason)
|
||||
|
||||
|
||||
def digest(raw):
|
||||
@@ -103,7 +107,7 @@ def run(args, timeout=15, limit=1024 * 1024):
|
||||
result = subprocess.run(args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, timeout=timeout, check=False)
|
||||
require(len(result.stdout) <= limit and len(result.stderr) <= limit, 'bounded command output')
|
||||
require(result.returncode == 0, 'fixed command failed: ' + args[0])
|
||||
require(result.returncode == 0, 'fixed command failed')
|
||||
return result.stdout
|
||||
|
||||
|
||||
@@ -341,50 +345,218 @@ def runtime_witness():
|
||||
return result
|
||||
|
||||
|
||||
def fd_metadata(fd):
|
||||
item = os.fstat(fd)
|
||||
return {key: getattr(item, 'st_' + key) for key in
|
||||
('dev', 'ino', 'uid', 'gid', 'mode', 'nlink', 'size', 'mtime_ns', 'ctime_ns')}
|
||||
|
||||
|
||||
def held_bytes(fd, limit):
|
||||
before = fd_metadata(fd)
|
||||
require(stat.S_ISREG(before['mode']) and before['size'] <= limit, 'bounded held regular input')
|
||||
raw = os.pread(fd, limit + 1, 0)
|
||||
require(len(raw) <= limit and fd_metadata(fd) == before, 'stable held input')
|
||||
return raw
|
||||
|
||||
|
||||
def chrony_unit():
|
||||
fields = ('MainPID', 'ControlPID', 'ActiveState', 'SubState', 'Type', 'ControlGroup', 'InvocationID', 'Job')
|
||||
raw = run(['systemctl', 'show', '--all', '--property=' + ','.join(fields), '--', 'chrony.service'], limit=4096)
|
||||
values = {}
|
||||
for line in raw.decode('ascii').splitlines():
|
||||
key, separator, value = line.partition('=')
|
||||
require(separator and key in fields and key not in values, 'complete fixed Chrony unit properties')
|
||||
values[key] = value
|
||||
require(set(values) == set(fields) and values['MainPID'].isdigit() and int(values['MainPID']) > 1 and
|
||||
values['ControlPID'] == '0' and values['ActiveState'] == 'active' and values['SubState'] == 'running' and
|
||||
values['Type'] == 'notify' and values['ControlGroup'] == '/system.slice/chrony.service' and
|
||||
re.fullmatch('[0-9a-f]{32}', values['InvocationID']) and int(values['InvocationID'], 16) > 0 and
|
||||
not values['Job'], 'actual fixed active notify Chrony service')
|
||||
return values
|
||||
|
||||
|
||||
def named_matches(path, held):
|
||||
named = metadata(path)
|
||||
return all(held.get(key) == value for key, value in named.items())
|
||||
|
||||
|
||||
def cgroup_members(path):
|
||||
raw = regular(path / 'cgroup.procs', 256)
|
||||
require(re.fullmatch(rb'(?:[1-9][0-9]*\n){3}', raw) is not None, 'exact three Chrony cgroup processes')
|
||||
members = sorted(int(value) for value in raw.splitlines())
|
||||
require(len(set(members)) == 3 and all(value > 1 for value in members), 'unique Chrony cgroup processes')
|
||||
# No silently ignored descendant cgroup can hide additional service members.
|
||||
with os.scandir(path) as entries:
|
||||
require(not any(entry.is_dir(follow_symlinks=False) for entry in entries),
|
||||
'no descendant Chrony cgroup')
|
||||
return members
|
||||
|
||||
|
||||
class ChronyProcess:
|
||||
def __init__(self, pid):
|
||||
self.pid, self.fds = pid, []
|
||||
try:
|
||||
self.pidfd = os.pidfd_open(pid)
|
||||
self.fds.append(self.pidfd)
|
||||
self.proc = Path('/proc') / str(pid)
|
||||
self.procfd = os.open(self.proc, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
||||
self.fds.append(self.procfd)
|
||||
# The fixed proc exe magic link is followed once, then held and
|
||||
# compared with the current executable before and after hashing.
|
||||
self.image = os.open('exe', os.O_RDONLY | os.O_CLOEXEC, dir_fd=self.procfd)
|
||||
self.fds.append(self.image)
|
||||
self.original = self.observe()
|
||||
except Exception:
|
||||
self.close()
|
||||
raise
|
||||
|
||||
def close(self):
|
||||
for fd in reversed(self.fds):
|
||||
os.close(fd)
|
||||
self.fds = []
|
||||
|
||||
def observe(self):
|
||||
poll = __import__('select').poll()
|
||||
poll.register(self.pidfd, __import__('select').POLLIN)
|
||||
require(not poll.poll(0), 'held Chrony process alive')
|
||||
status = regular(self.proc / 'status', 65536).decode('ascii')
|
||||
ids = {}
|
||||
for label in ('Uid', 'Gid'):
|
||||
match = re.search('^' + label + r':\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$', status, re.MULTILINE)
|
||||
require(match and len(set(match.groups())) == 1, 'fixed real effective process IDs')
|
||||
ids[label.lower()] = int(match[1])
|
||||
first = regular(self.proc / 'stat', 8192).decode('ascii').rsplit(')', 1)[1].split()
|
||||
require(len(first) >= 20 and first[19].isdigit() and first[1].isdigit(), 'process birth and parent')
|
||||
require(regular(self.proc / 'cgroup', 4096) == b'0::/system.slice/chrony.service\n',
|
||||
'actual unified Chrony cgroup membership')
|
||||
image = fd_metadata(self.image)
|
||||
current = os.stat('exe', dir_fd=self.procfd)
|
||||
require(stat.S_ISREG(image['mode']) and image['uid'] == 0 and image['mode'] & 0o022 == 0 and
|
||||
(current.st_dev, current.st_ino) == (image['dev'], image['ino']), 'held protected actual process image')
|
||||
image_hash = digest(held_bytes(self.image, 16 * 1024 * 1024))
|
||||
after = os.stat('exe', dir_fd=self.procfd)
|
||||
require((after.st_dev, after.st_ino) == (image['dev'], image['ino']) and
|
||||
fd_metadata(self.image) == image and regular(self.proc / 'stat', 8192).decode('ascii').rsplit(')', 1)[1].split()[19] == first[19]
|
||||
and not poll.poll(0), 'actual process image and birth unchanged')
|
||||
return {'pid': self.pid, 'birth_ticks': int(first[19]), 'parent_pid': int(first[1]),
|
||||
'uid': ids['uid'], 'gid': ids['gid'], 'image': image, 'image_sha256': image_hash}
|
||||
|
||||
def socket_fds(self, inode):
|
||||
fd = os.open('fd', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=self.procfd)
|
||||
try:
|
||||
names = os.listdir(fd)
|
||||
require(len(names) <= 128 and all(value.isdigit() for value in names), 'bounded process descriptor set')
|
||||
return sorted(int(value) for value in names if os.readlink(value, dir_fd=fd) == 'socket:[' + str(inode) + ']')
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def command_socket_inode():
|
||||
raw = regular(Path('/proc/net/unix'), 1024 * 1024)
|
||||
rows = [line.split() for line in raw.decode('ascii').splitlines()[1:]]
|
||||
matches = [row for row in rows if len(row) == 8 and row[7] == '/run/chrony/chronyd.sock']
|
||||
require(len(matches) == 1 and matches[0][4] == '0002' and matches[0][6].isdigit(),
|
||||
'unique fixed Unix datagram command socket')
|
||||
return int(matches[0][6])
|
||||
|
||||
|
||||
def chrony_observation():
|
||||
unit = chrony_unit()
|
||||
cgroup = Path('/sys/fs/cgroup/system.slice/chrony.service')
|
||||
protected_dir(cgroup)
|
||||
group_identity = metadata(cgroup)
|
||||
members = cgroup_members(cgroup)
|
||||
processes = []
|
||||
pidfile = directory = -1
|
||||
try:
|
||||
for pid in members:
|
||||
processes.append(ChronyProcess(pid))
|
||||
main = int(unit['MainPID'])
|
||||
by_pid = {process.pid: process for process in processes}
|
||||
require(main in by_pid, 'unit wrapper belongs to actual service cgroup')
|
||||
# /run/chrony is daemon-owned: a root-owned pidfile alone is NOT
|
||||
# sufficient namespace authority. Bind the actual command-socket FD too.
|
||||
protected_dir(Path('/run'))
|
||||
directory = os.open('/run/chrony', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
||||
dir_identity = fd_metadata(directory)
|
||||
require(stat.S_ISDIR(dir_identity['mode']) and dir_identity['uid'] == 989 and
|
||||
dir_identity['gid'] == 989 and stat.S_IMODE(dir_identity['mode']) == 0o755,
|
||||
'actual fixed protected Chrony socket directory')
|
||||
pidfile = os.open('chronyd.pid', os.O_RDONLY | os.O_NOFOLLOW, dir_fd=directory)
|
||||
pid_identity = fd_metadata(pidfile)
|
||||
require(pid_identity['uid'] == 0 and pid_identity['gid'] == 0 and pid_identity['nlink'] == 1 and
|
||||
stat.S_IMODE(pid_identity['mode']) == 0o644, 'root single link fixed Chrony pidfile')
|
||||
pid_raw = held_bytes(pidfile, 32)
|
||||
require(re.fullmatch(rb'[1-9][0-9]*\n', pid_raw) is not None, 'canonical actual Chrony pidfile PID')
|
||||
daemon = int(pid_raw.strip())
|
||||
require(daemon in by_pid and daemon != main, 'pidfile daemon belongs to service cgroup')
|
||||
child = next(pid for pid in members if pid not in (main, daemon))
|
||||
wrapper, actual, helper = (by_pid[pid].original for pid in (main, daemon, child))
|
||||
require(wrapper['uid'] == wrapper['gid'] == 0 and actual['uid'] == actual['gid'] == 989 and
|
||||
helper['uid'] == helper['gid'] == 989 and actual['parent_pid'] == main and helper['parent_pid'] == daemon,
|
||||
'actual fixed wrapper daemon child identity chain')
|
||||
fixed_images = {}
|
||||
for role, path, observed in (('wrapper', '/usr/bin/dash', wrapper), ('daemon', '/usr/sbin/chronyd', actual)):
|
||||
protected_dir(Path(path).parent)
|
||||
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
|
||||
try:
|
||||
fixed = fd_metadata(fd)
|
||||
require(fixed == observed['image'] and digest(held_bytes(fd, 16 * 1024 * 1024)) == observed['image_sha256']
|
||||
and named_matches(Path(path), fixed),
|
||||
'actual process equals protected fixed executable')
|
||||
fixed_images[role] = fixed
|
||||
finally:
|
||||
os.close(fd)
|
||||
require(helper['image'] == actual['image'] and helper['image_sha256'] == actual['image_sha256'],
|
||||
'actual Chrony child shares protected daemon image')
|
||||
socket_path = Path('/run/chrony/chronyd.sock')
|
||||
socket_identity = metadata(socket_path)
|
||||
require(stat.S_ISSOCK(socket_identity['mode']) and socket_identity['uid'] == socket_identity['gid'] == 989 and
|
||||
stat.S_IMODE(socket_identity['mode']) == 0o755, 'actual fixed Chrony filesystem socket')
|
||||
inode = command_socket_inode()
|
||||
holders = {process.pid: process.socket_fds(inode) for process in processes}
|
||||
require(bool(holders[daemon]) and all(not fds for pid, fds in holders.items() if pid != daemon),
|
||||
'unique actual service command socket holder')
|
||||
for process in processes:
|
||||
require(process.observe() == process.original, 'service process tuple unchanged')
|
||||
require(chrony_unit() == unit and cgroup_members(cgroup) == members and metadata(cgroup) == group_identity and
|
||||
fd_metadata(directory) == dir_identity and named_matches(Path('/run/chrony'), dir_identity) and
|
||||
held_bytes(pidfile, 32) == pid_raw and fd_metadata(pidfile) == pid_identity and
|
||||
named_matches(Path('/run/chrony/chronyd.pid'), pid_identity) and
|
||||
metadata(socket_path) == socket_identity and command_socket_inode() == inode and
|
||||
named_matches(Path('/usr/bin/dash'), fixed_images['wrapper']) and
|
||||
named_matches(Path('/usr/sbin/chronyd'), fixed_images['daemon']) and
|
||||
{process.pid: process.socket_fds(inode) for process in processes} == holders,
|
||||
'Chrony unit namespace process pidfile and socket unchanged')
|
||||
return {'pin': {'schema_version': 1, 'daemon_pid': daemon, 'daemon_birth_ticks': actual['birth_ticks'],
|
||||
'daemon_uid': actual['uid'], 'daemon_gid': actual['gid'], 'daemon_image_sha256': actual['image_sha256']},
|
||||
'unit': unit, 'processes': [process.original for process in processes],
|
||||
'cgroup_metadata': group_identity, 'pidfile_metadata': pid_identity, 'pidfile_sha256': digest(pid_raw),
|
||||
'directory_metadata': dir_identity, 'socket_metadata': socket_identity,
|
||||
'socket_kernel_inode': inode, 'service_socket_fds': holders, 'fixed_images': fixed_images}
|
||||
finally:
|
||||
if pidfile >= 0:
|
||||
os.close(pidfile)
|
||||
if directory >= 0:
|
||||
os.close(directory)
|
||||
for process in processes:
|
||||
process.close()
|
||||
|
||||
|
||||
def preflight():
|
||||
engine = run(['docker', 'version', '--format', '{{json .Server.Version}}']).decode().strip()
|
||||
image_values = json.loads(run(['docker', 'image', 'inspect', '--format',
|
||||
'[{{json .Id}},{{json .Architecture}},{{json .Os}}]', BUILDER]))
|
||||
image = dict(zip(('Id', 'Architecture', 'Os'), image_values, strict=True))
|
||||
# Inspect only public immutable image metadata, never Config/Env.
|
||||
require(image['Architecture'] == 'arm64' and image['Os'] == 'linux', 'cached ARM64 builder required')
|
||||
pid = int(run(['systemctl', 'show', 'chrony.service', '--property=MainPID', '--value']).strip())
|
||||
require(pid > 1, 'existing Chrony service PID')
|
||||
proc = Path('/proc') / str(pid)
|
||||
pidfd = os.pidfd_open(pid)
|
||||
try:
|
||||
status = regular(proc / 'status', 65536).decode()
|
||||
ids = {}
|
||||
for label in ('Uid', 'Gid'):
|
||||
match = re.search('^' + label + r':\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$', status, re.MULTILINE)
|
||||
require(match and len(set(match.groups())) == 1, 'fixed real/effective daemon IDs')
|
||||
ids[label.lower()] = int(match[1])
|
||||
birth = regular(proc / 'stat', 8192).decode().rsplit(')', 1)[1].split()[19]
|
||||
image_path = Path('/usr/sbin/chronyd')
|
||||
fixed = metadata(image_path)
|
||||
require(stat.S_ISREG(fixed['mode']) and fixed['uid'] == 0 and fixed['mode'] & 0o022 == 0,
|
||||
'protected Chrony executable')
|
||||
require(os.stat(proc / 'exe').st_ino == fixed['ino'] and os.stat(proc / 'exe').st_dev == fixed['dev'],
|
||||
'actual daemon fixed executable inode')
|
||||
image_hash = digest(regular(image_path))
|
||||
require(metadata(image_path) == fixed and
|
||||
regular(proc / 'stat', 8192).decode().rsplit(')', 1)[1].split()[19] == birth and
|
||||
int(run(['systemctl', 'show', 'chrony.service', '--property=MainPID', '--value']).strip()) == pid,
|
||||
'Chrony identity unchanged during probe')
|
||||
poll = __import__('select').poll()
|
||||
poll.register(pidfd, __import__('select').POLLIN)
|
||||
require(not poll.poll(0), 'Chrony process still alive')
|
||||
finally:
|
||||
os.close(pidfd)
|
||||
observed = chrony_observation()
|
||||
boot = regular(Path('/proc/sys/kernel/random/boot_id'), 128).decode().strip()
|
||||
require(str(uuid.UUID(boot)) == boot, 'kernel boot identity')
|
||||
return {'docker_engine_version': json.loads(engine),
|
||||
'builder': {key: image[key] for key in ('Id', 'Architecture', 'Os')},
|
||||
'runtime': runtime_witness(), 'boot_id': boot,
|
||||
'chrony_observation': {'schema_version': 1, 'daemon_pid': pid,
|
||||
'daemon_birth_ticks': int(birth), 'daemon_uid': ids['uid'], 'daemon_gid': ids['gid'],
|
||||
'daemon_image_sha256': image_hash},
|
||||
'chrony_image_metadata': fixed, 'pins_provisioned': False,
|
||||
'chrony_observation': observed['pin'], 'chrony_binding': observed,
|
||||
'chrony_image_metadata': observed['fixed_images']['daemon'], 'pins_provisioned': False,
|
||||
'quality_sampled': False, 'registry_cache_verified': False}
|
||||
|
||||
|
||||
@@ -637,8 +809,12 @@ def host(args):
|
||||
except Exception as error:
|
||||
# Fixed controlled messages only; never write exception repr, paths from secrets, nested errors or env.
|
||||
exclusive(phase / 'failure.json', encoded({'failed': True, 'exception_type': type(error).__name__,
|
||||
'failure_reason': str(error) if isinstance(error, OperatorDenied) else 'unexpected_operator_exception',
|
||||
'read_original_case_only': True}))
|
||||
raise
|
||||
print(json.dumps({'case': case.name, 'phase': name, 'failed': True,
|
||||
'failure_reason': str(error) if isinstance(error, OperatorDenied) else 'unexpected_operator_exception',
|
||||
'read_original_case_only': True}, sort_keys=True))
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
def main():
|
||||
@@ -662,4 +838,10 @@ def main():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
try:
|
||||
main()
|
||||
except Exception:
|
||||
# A failure writing the private receipt must not print nested exceptions.
|
||||
print(json.dumps({'failed': True, 'failure_reason': 'unexpected_operator_exception',
|
||||
'read_original_case_only': True}, sort_keys=True))
|
||||
raise SystemExit(1)
|
||||
|
||||
Reference in New Issue
Block a user