482 lines
41 KiB
Python
482 lines
41 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only post-start evidence; Root executes after a separate source review.
|
|
|
|
No target writes, service commands, helper IPC, enrollment, jobs or approval.
|
|
Three separate observe invocations are required: baseline and two subsequent
|
|
stored heartbeat advances. Missing evidence fails closed; never retry an install.
|
|
"""
|
|
import argparse
|
|
import ast
|
|
import base64
|
|
import hashlib
|
|
import inspect
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PREFLIGHT_SHA = '0e210b857d3f25e0ff228bbbd4a6533dadd91c9d21dbcba5923b26c2fe72eec9'
|
|
if hashlib.sha256((ROOT/'deploy/script_console_bootstrap.py').read_bytes()).hexdigest() != PREFLIGHT_SHA:
|
|
raise RuntimeError('Reviewed preflight source changed')
|
|
import script_console_bootstrap as pre
|
|
support = pre.support
|
|
CASE = '2e27c312-58cc-444d-92a6-863a11ac6ffd'
|
|
STATE = ROOT/'devstate/script-console-post-attestation'
|
|
DROPIN = b'[Service]\nEnvironment=RMM_FULL_MAINTENANCE_HELPER_SOCKET=/run/rmm-helper-maintenance/control.sock\nEnvironment=RMM_FULL_MAINTENANCE_POLICY_FILE=/etc/rmm-remote/full-maintenance-helper-proof.json\n'
|
|
|
|
|
|
def require(value, code):
|
|
if not value:
|
|
raise RuntimeError(code)
|
|
|
|
|
|
def vm(payload):
|
|
"""Only fixed file/SQLite reads, process metadata and public-key verification."""
|
|
import contextlib
|
|
import datetime as dt
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import stat
|
|
import subprocess
|
|
import base64
|
|
import time
|
|
read_transactions={}
|
|
require(os.geteuid()==0 and os.uname().machine=='x86_64' and payload['case_id']=='2e27c312-58cc-444d-92a6-863a11ac6ffd', 'fixed root read-only case')
|
|
intent=payload['install']; before=Path('/var/lib/rmm-bootstrap-history/script-console-v1-'+payload['case_id'])
|
|
state=Path('/var/lib/rmm-agent-remote'); helper=Path('/var/lib/rmm-helper')
|
|
def sha(raw):return hashlib.sha256(raw).hexdigest()
|
|
def encode(value):return json.dumps(value,sort_keys=True,separators=(',',':')).encode()
|
|
def protected(path,limit=8*1024*1024):
|
|
m=path.lstat();require(stat.S_ISREG(m.st_mode) and not path.is_symlink() and m.st_uid==0
|
|
and not m.st_mode&0o022 and m.st_nlink==1 and m.st_size<=limit,'protected bounded public/control evidence')
|
|
return path.read_bytes()
|
|
require(protected(before/'intent.json')==encode(intent),'original immutable installation intent')
|
|
old_sql=json.loads(protected(before/'sql-before.json',64*1024*1024))
|
|
old_files=json.loads(protected(before/'files-before.json'))
|
|
original_report=protected(before/'report.json')
|
|
historical=json.loads(original_report)
|
|
require(historical['preserved_before_restart'] is True and historical['full_pin']['helper_binary_sha256']==intent['artifacts']['helper']['sha256'], 'original preserved file installation')
|
|
continuation=json.loads(protected(before/'continuation-start-intent.json'))
|
|
require(continuation['original_case']==payload['case_id'],'same original first-start case')
|
|
first_start=dt.datetime.fromisoformat(continuation['first_start_not_before'].replace('Z','+00:00'))
|
|
require(first_start.tzinfo is not None,'explicit durable first-start timestamp')
|
|
require(Path('/proc/sys/kernel/random/boot_id').read_text().strip()==intent['preflight']['boot_id'],'same boot')
|
|
observed_from=dt.datetime.now(dt.timezone.utc).isoformat()
|
|
processes={}
|
|
for name,key,uid in (('rmm-agent-remote.service','agent',993),('rmm-helper-cloud.service','general',0),('rmm-full-maintenance-helper.service','full',0)):
|
|
raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','MainPID'],capture_output=True,check=True,timeout=15).stdout
|
|
unit=dict(line.split('=',1) for line in raw.decode().splitlines());pid=int(unit['MainPID'])
|
|
require(unit['ActiveState']=='active' and pid>1,'all three actual services active')
|
|
proc=Path('/proc',str(pid));ticks=(proc/'stat').read_text().rsplit(') ',1)[1].split()[19]
|
|
status=(proc/'status').read_text().splitlines()
|
|
uids=next(x.split()[1:] for x in status if x.startswith('Uid:'));gids=next(x.split()[1:] for x in status if x.startswith('Gid:'))
|
|
image=sha((proc/'exe').read_bytes());expected=intent['artifacts']['agent' if key=='agent' else 'helper']['sha256']
|
|
require(uids==[str(uid)]*4 and gids==['984']*4 and image==expected
|
|
and (proc/'stat').read_text().rsplit(') ',1)[1].split()[19]==ticks,'actual same process birth/account/image')
|
|
processes[key]=dict(pid=pid,start_ticks=ticks,uids=uids,gids=gids,sha256=image)
|
|
for name in ('rmm-apt-helper.service','rmm-agent.service','rmm-helper-dev-test.service'):
|
|
raw=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','UnitFileState'],capture_output=True,check=True,timeout=15).stdout
|
|
unit=dict(line.split('=',1) for line in raw.decode().splitlines())
|
|
require(unit['ActiveState']=='inactive' and unit['UnitFileState']=='disabled','APT/legacy remain closed')
|
|
for key,path in (('agent',Path('/usr/local/libexec/rmm-agent-remote')),('helper',Path('/usr/local/libexec/rmm-privileged-helper'))):
|
|
raw=protected(path,256*1024*1024)
|
|
require(sha(raw)==intent['artifacts'][key]['sha256'] and len(raw)==intent['artifacts'][key]['bytes']
|
|
and stat.S_IMODE(path.lstat().st_mode)==0o755,'qualified installed executable bytes/mode')
|
|
prior=before/(key+'-previous');m=prior.lstat();old=intent['baseline']['controls'][str(path)]
|
|
require(dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(prior,256*1024*1024)))==old,'original binary backup identities/code preserved')
|
|
for path,old in intent['baseline']['controls'].items():
|
|
if path.startswith('/usr/local/libexec/'):continue
|
|
path=Path(path);m=path.lstat();actual=dict(device=m.st_dev,inode=m.st_ino,mode=stat.S_IMODE(m.st_mode),uid=m.st_uid,gid=m.st_gid,bytes=m.st_size,sha256=sha(protected(path)))
|
|
require(actual==old,'unchanged General/APT/package control bytes and identities')
|
|
full=base64.b64decode(intent['baseline']['full_config_candidate']['bytes_b64'],validate=True)
|
|
config=Path('/etc/rmm-remote/helper-full-maintenance.json');pinpath=Path('/etc/rmm-remote/full-maintenance-helper-proof.json')
|
|
require(protected(config)==full and stat.S_IMODE(config.lstat().st_mode)==0o600 and config.lstat().st_gid==0,'exact Full configuration')
|
|
pin=json.loads(protected(pinpath));require(pin==historical['full_pin'] and stat.S_IMODE(pinpath.lstat().st_mode)==0o640
|
|
and pinpath.lstat().st_gid==984,'exact protected Full policy/image/identity pin')
|
|
ordered_pin={k:pin[k] for k in ('schema_version','profile','identity','policy_sha256','helper_binary_sha256')}
|
|
ordered_pin['identity']={k:pin['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','signing_key_id')}
|
|
require(protected(pinpath)==json.dumps(ordered_pin,separators=(',',':')).encode(),'exact original inspector pin bytes')
|
|
for path,raw in ((Path('/etc/systemd/system/rmm-full-maintenance-helper.service'),base64.b64decode(intent['unit_bytes_b64'],validate=True)),
|
|
(Path('/etc/systemd/system/rmm-agent-remote.service.d/60-full-maintenance.conf'),base64.b64decode(payload['dropin_b64'],validate=True))):
|
|
require(protected(path)==raw and stat.S_IMODE(path.lstat().st_mode)==0o644 and path.lstat().st_gid==0,'exact Full unit and additive agent dropin bytes/modes')
|
|
# Do not open a private key. Database bytes legitimately change under Sync;
|
|
# every preexisting non-DB file retains its complete metadata witness.
|
|
known_databases={str(state/'journal.sqlite'),str(state/'consumption.sqlite'),str(helper/'consumption.sqlite')}
|
|
require(set(old_sql)==known_databases,'exact three known DELETE-mode databases')
|
|
known_journals={name+'-journal':name for name in known_databases}
|
|
journal_observations=[]
|
|
def await_known_delete_journal(name):
|
|
require(name in known_databases,'only known DELETE rollback journal paths')
|
|
started=time.monotonic_ns();observed=False
|
|
while True:
|
|
require(all(not os.path.lexists(name+s) for s in ('-wal','-shm')),'WAL/SHM sidecars always denied')
|
|
journal=Path(name+'-journal')
|
|
if not os.path.lexists(journal):
|
|
elapsed=time.monotonic_ns()-started
|
|
require(not observed or elapsed<=250_000_000,'known DELETE rollback journal absent within250ms')
|
|
if observed:journal_observations.append(dict(database=name,wait_ns=elapsed,observed_naturally_absent=True))
|
|
return
|
|
observed=True
|
|
try:m=journal.lstat()
|
|
except FileNotFoundError:continue
|
|
owner=old_files[name]
|
|
require(stat.S_ISREG(m.st_mode) and not journal.is_symlink() and m.st_nlink==1
|
|
and m.st_uid==owner[2] and m.st_gid==owner[3] and not m.st_mode&0o022,'known regular protected DELETE rollback journal only')
|
|
elapsed=time.monotonic_ns()-started
|
|
require(elapsed<250_000_000,'known DELETE rollback journal remained250ms')
|
|
time.sleep(min(0.005,(250_000_000-elapsed)/1_000_000_000))
|
|
current_paths=set()
|
|
for root in (state,helper):
|
|
for path in [root,*sorted(root.rglob('*'))]:
|
|
if str(path) in known_journals:
|
|
await_known_delete_journal(known_journals[str(path)])
|
|
require(not os.path.lexists(path),'known DELETE rollback journal still absent before state-path exclusion')
|
|
continue
|
|
if str(path).endswith(('-wal','-shm','-journal')):raise RuntimeError('Settled DELETE-mode snapshot required')
|
|
m=path.lstat();require(not path.is_symlink() and (stat.S_ISDIR(m.st_mode) or stat.S_ISREG(m.st_mode)),'retained state object type')
|
|
current_paths.add(str(path));old=old_files.get(str(path))
|
|
require(old is not None,'no unexplained new state path')
|
|
value=[m.st_dev,m.st_ino,m.st_uid,m.st_gid,m.st_mode,m.st_size,m.st_mtime_ns,m.st_ctime_ns]
|
|
require(value[:5]==old[:5] if str(path) in old_sql or stat.S_ISDIR(m.st_mode) else value==old,'state/PKI/key metadata and inodes preserved')
|
|
require(current_paths==set(old_files),'all old state paths retained')
|
|
@contextlib.contextmanager
|
|
def database(name):
|
|
path=Path(name);m=path.lstat();require(not path.is_symlink() and stat.S_ISREG(m.st_mode) and m.st_nlink==1
|
|
and [m.st_dev,m.st_ino]==old_sql[name]['identity'],'same actual SQLite inode')
|
|
await_known_delete_journal(name)
|
|
require(all(not os.path.lexists(name+s) for s in ('-wal','-shm','-journal')),'no SQLite sidecars')
|
|
# Never hold a shared DELETE-mode read lock while hashing rows or
|
|
# running OpenSSL. A cooperative one-second SQLite progress budget
|
|
# limits reads; this is not a hard real-time OS/I/O guarantee.
|
|
started=time.monotonic()
|
|
with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro',uri=True,timeout=0.25)) as db:
|
|
require(all(not os.path.lexists(name+s) for s in ('-wal','-shm','-journal')),'no SQLite sidecars before any SQL after readonly open')
|
|
db.set_progress_handler(lambda:int(time.monotonic()-started>1.0),1000)
|
|
try:
|
|
db.execute('PRAGMA query_only=ON');db.execute('BEGIN')
|
|
require(db.execute('PRAGMA journal_mode').fetchone()[0]=='delete','qualified DELETE journal')
|
|
yield db
|
|
finally:
|
|
db.rollback()
|
|
elapsed=int((time.monotonic()-started)*1000)
|
|
timing=read_transactions.setdefault(name,dict(count=0,total_ms=0,max_ms=0))
|
|
timing['count']+=1;timing['total_ms']+=elapsed;timing['max_ms']=max(timing['max_ms'],elapsed)
|
|
require([path.stat().st_dev,path.stat().st_ino]==old_sql[name]['identity'],'SQLite inode unchanged through read')
|
|
def row_hash(row):return sha(encode([dict(blob_sha256=sha(v),bytes=len(v)) if isinstance(v,bytes) else v for v in row]))
|
|
journal=str(state/'journal.sqlite');ledger=str(helper/'consumption.sqlite');public=json.loads((state/'binding.json').read_bytes())
|
|
require(all(public[k]==intent['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')),'retained enrollment/binding')
|
|
key=bytes.fromhex(public['public_key_hex']);require(len(key)==32 and sha(key)==pin['identity']['signing_key_id'],'same public signing key')
|
|
verified={}
|
|
def signed(decision_id):
|
|
if decision_id in verified:return verified[decision_id]
|
|
with database(ledger) as db:row=db.execute('SELECT rowid,* FROM helper_sync_decisions WHERE decision_id=?',[decision_id]).fetchone()
|
|
require(row is not None,'retained helper-applied signed decision')
|
|
response=json.loads(row[3]);raw=bytes(response['decision_bytes']);signature=bytes(response['signature']);require(0<len(raw)<=512*1024 and len(signature)==64,'bounded original Ed25519 response')
|
|
descriptors=[]
|
|
try:
|
|
for value in (bytes.fromhex('302a300506032b6570032100')+key,raw,signature):
|
|
fd=os.memfd_create('rmm-readonly-public-verification',os.MFD_CLOEXEC);descriptors.append(fd)
|
|
require(os.write(fd,value)==len(value),'complete public verification input');os.lseek(fd,0,0)
|
|
result=subprocess.run(['/usr/bin/openssl','pkeyutl','-verify','-pubin','-keyform','DER','-inkey','/proc/self/fd/'+str(descriptors[0]),'-rawin','-in','/proc/self/fd/'+str(descriptors[1]),'-sigfile','/proc/self/fd/'+str(descriptors[2])],pass_fds=tuple(descriptors),capture_output=True,timeout=5)
|
|
require(result.returncode==0,'original signed decision verifies')
|
|
finally:
|
|
for fd in descriptors:os.close(fd)
|
|
d=json.loads(raw)
|
|
require(d['decision_id']==decision_id and d['domain']=='rmm.reconciliation-decision' and d['schema_version']==1
|
|
and d['key_id']==sha(key) and all(d[k]==public[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')),'signed domain/key/device/generations')
|
|
def varint(v):
|
|
out=bytearray()
|
|
while v>=128:out.append((v&127)|128);v>>=7
|
|
out.append(v);return bytes(out)
|
|
wire=b'\x0a'+varint(len(raw))+raw+b'\x12\x40'+signature
|
|
with database(journal) as db:req=db.execute('SELECT request,generation,response FROM reconcile_requests WHERE id=?',[d['request_id']]).fetchone()
|
|
require(req is not None and req[0]==row[4] and sha(req[0])==d['request_hash'] and req[1]==d['connection_generation'] and req[2]==wire,'same agent/helper-applied request and signed response bytes')
|
|
original=row_hash(row) in old_sql[ledger]['rows']['helper_sync_decisions']
|
|
verified[decision_id]=dict(decision=d,payload_sha256=sha(raw),signature_sha256=sha(signature),original=original)
|
|
return verified[decision_id]
|
|
def original_rows(name,table,count):
|
|
# There is deliberately no encompassing transaction. Each page closes
|
|
# before yield, so hashing/crypto cannot block a DELETE-mode writer.
|
|
require(table.replace('_','').isalnum(),'fixed SQLite identifier')
|
|
last=None;remaining=count
|
|
while remaining:
|
|
size=min(16,remaining)
|
|
with database(name) as db:
|
|
if last is None:page=db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid LIMIT ?',[size]).fetchall()
|
|
else:page=db.execute('SELECT rowid,* FROM "'+table+'" WHERE rowid>? ORDER BY rowid LIMIT ?',[last,size]).fetchall()
|
|
require(len(page)==size,'all original SQL prefix rows retained')
|
|
for row in page:
|
|
require(last is None or row[0]>last,'monotonic original SQLite rowid traversal')
|
|
last=row[0];remaining-=1;yield row
|
|
old_candidates=[]
|
|
# Only original IDs are materialized, never all historical request/response
|
|
# BLOBs or new append-only Sync rows. Their full hashes are checked below.
|
|
total_original=len(old_sql[ledger]['rows']['helper_sync_decisions']);last=None
|
|
while len(old_candidates)<total_original:
|
|
with database(ledger) as db:
|
|
size=min(16,total_original-len(old_candidates))
|
|
if last is None:page=db.execute('SELECT rowid,decision_id FROM helper_sync_decisions ORDER BY rowid LIMIT ?',[size]).fetchall()
|
|
else:page=db.execute('SELECT rowid,decision_id FROM helper_sync_decisions WHERE rowid>? ORDER BY rowid LIMIT ?',[last,size]).fetchall()
|
|
require(len(page)==size,'all original signed decision IDs retained')
|
|
old_candidates.extend(row[1] for row in page);last=page[-1][0]
|
|
old_candidates.reverse()
|
|
reference_changes=[];summary={}
|
|
for name,previous in old_sql.items():
|
|
with database(name) as db:
|
|
schema=dict(db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name"));require(schema==previous['schema'],'exact unchanged SQL schema')
|
|
counts={}
|
|
for table,old_hashes in previous['rows'].items():
|
|
require(table.replace('_','').isalnum(),'fixed SQLite identifier')
|
|
with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0]
|
|
counts[table]=count
|
|
require(count>=len(old_hashes),'all original SQL rows retained')
|
|
if table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'):
|
|
require(count==len(old_hashes),'no new job/effect/history rows during bootstrap')
|
|
if table in ('reconcile_meta','helper_sync_meta'):continue
|
|
if table=='counter':
|
|
with database(name) as db:current=db.execute('SELECT rowid,* FROM counter ORDER BY rowid LIMIT 2').fetchall()
|
|
require(len(current)==len(old_hashes)==1 and any(row_hash((1,1,n))==old_hashes[0] for n in range(max(0,current[0][2]-4096),current[0][2]+1)),'monotonic counter reconstructed against exact old hash within4096 bound');continue
|
|
if table in ('pending','job_events'):
|
|
require(not old_hashes,'no ambiguous preexisting queued messages');continue
|
|
for index,row in enumerate(original_rows(name,table,len(old_hashes))):
|
|
old_hash=old_hashes[index]
|
|
if row_hash(row)==old_hash:continue
|
|
if table=='reconcile_requests':
|
|
require(row[-1] is not None and row_hash((*row[:-1],None))==old_hash,'only original NULL response may fill')
|
|
with database(ledger) as helper_db:candidates=list(helper_db.execute('SELECT decision_id FROM helper_sync_decisions WHERE request=?',[row[2]]))
|
|
require(len(candidates)==1,'filled response has independent helper-applied original request')
|
|
signed(candidates[0][0])
|
|
continue
|
|
require(table=='reconcile_outcomes' and name==journal,'immutable original SQL row changed')
|
|
# Keep original rowid/job/attempt/outcome bytes; only its
|
|
# reference to an independently applied signed decision renews.
|
|
original_id=next((d for d in old_candidates if row_hash((*row[:-1],d))==old_hash),None)
|
|
require(original_id is not None,'exact old complete outcome-row witness')
|
|
old=signed(original_id);new=signed(row[-1]);require(old['original'],'original decision existed before restart')
|
|
selected=[]
|
|
for proof in (old,new):
|
|
found=[a for a in proof['decision']['attempts'] if a['job_id']==row[1] and a['attempt_id']==row[2]]
|
|
require(len(found)==1 and found[0]['outcome_sha256']==sha(row[3]) and (not found[0]['outcome_bytes'] or bytes(found[0]['outcome_bytes'])==row[3]),'original signed outcome bytes');selected.append(found[0])
|
|
require(all(selected[0][k]==selected[1][k] for k in ('job_payload_hash','grant_id','authorization_id','plan_hash','action','execution_identity','outcome_state','outcome_sha256','original_boot_id')),'same exact authorized outcome')
|
|
a=old['decision'];b=new['decision'];require(a['journal_id']==b['journal_id'] and a['helper_journal_id']==b['helper_journal_id'] and b['checkpoint']>=a['checkpoint'] and b['helper_checkpoint']>=a['helper_checkpoint'] and b['connection_generation']>=a['connection_generation'],'monotonic signed reference renewal')
|
|
reference_changes.append(dict(rowid=row[0],original_sha256=old_hash,old_decision_id=original_id,new_decision_id=row[-1]))
|
|
summary[name]=dict(identity=previous['identity'],schema_preserved=True,initial_row_counts=counts,
|
|
original_row_counts={t:len(v) for t,v in previous['rows'].items()})
|
|
# Recheck fixed effect/job counts after the whole paged witness. New passive
|
|
# captures may append throughout; a cross-table snapshot is not claimed.
|
|
for name,previous in old_sql.items():
|
|
for table in ('job_attempts','consumed','execution_evidence','reconcile_outcomes'):
|
|
if table in previous['rows']:
|
|
with database(name) as db:count=db.execute('SELECT count(*) FROM "'+table+'"').fetchone()[0]
|
|
require(count==len(previous['rows'][table]),'final exact no-new-effect/job row counts')
|
|
with database(ledger) as db:
|
|
roots=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id LIMIT 19').fetchall()
|
|
names=[r[1] for r in db.execute('PRAGMA table_info(helper_sync_meta)')];helper_meta=dict(zip(names,db.execute('SELECT * FROM helper_sync_meta WHERE id=1').fetchone()))
|
|
exact={r[0]:sha(json.dumps([dict(sha256=sha(v),size=len(v)) if isinstance(v,bytes) else v for v in r],separators=(',',':')).encode()) for r in roots}
|
|
require(exact==intent['preflight']['root_rows'] and len(exact)==18 and all(r[5] is not None for r in roots),'final exact original18Root request/grant/signature/start/outcome rows')
|
|
with database(journal) as db:
|
|
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0]==0,'no unfinished agent job')
|
|
names=[r[1] for r in db.execute('PRAGMA table_info(reconcile_meta)')];agent_meta=dict(zip(names,db.execute('SELECT * FROM reconcile_meta WHERE id=1').fetchone()))
|
|
inventory=payload.get('inventory')
|
|
inventory_queued=bool(db.execute('SELECT EXISTS(SELECT 1 FROM job_events WHERE id=?)',[inventory['message_id']]).fetchone()[0]) if inventory is not None else None
|
|
require(agent_meta['fault'] is None and helper_meta['fault'] is None and agent_meta['last_decision']==helper_meta['last_decision']
|
|
and agent_meta['generation']==helper_meta['generation'] and agent_meta['journal_id']==helper_meta['agent_journal_id']
|
|
and agent_meta['helper_id']==helper_meta['journal_id'],'fault-free shared current applied decision and journal lineage')
|
|
ordered_binding={k:public[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','public_key_hex')}
|
|
require(helper_meta['binding_sha256']==sha(json.dumps(ordered_binding,separators=(',',':')).encode()),'helper ledger independently bound to exact same enrollment')
|
|
proof=signed(agent_meta['last_decision']);decision=proof['decision'];now=dt.datetime.now(dt.timezone.utc)
|
|
issued=dt.datetime.fromisoformat(decision['issued_at'].replace('Z','+00:00'));until=dt.datetime.fromisoformat(decision['valid_until'].replace('Z','+00:00'))
|
|
require(issued<=now<until and 0<(until-issued).total_seconds()<=60 and decision['ready'] is True and decision['helper_ready'] is True
|
|
and decision['boot_id']==intent['preflight']['boot_id'] and decision['connection_generation']==agent_meta['generation']
|
|
and decision['journal_id']==agent_meta['journal_id'] and decision['helper_journal_id']==helper_meta['journal_id'],'current bounded normal applied signed Sync')
|
|
original_basis=next((signed(d)['decision'] for d in old_candidates if signed(d)['original']),None)
|
|
require(original_basis is not None and original_basis['journal_id']==decision['journal_id']
|
|
and original_basis['helper_journal_id']==decision['helper_journal_id']
|
|
and all(decision[k]>=original_basis[k] for k in ('checkpoint','helper_checkpoint','connection_generation')),'original signed journal lineage retained with monotonic checkpoint/generation')
|
|
# Specific server-persisted inventory must have left the durable local queue;
|
|
# this is code-bound queue convergence, not a captured network ACK frame.
|
|
inventory=payload.get('inventory')
|
|
inventory_ack=None
|
|
if inventory is not None:
|
|
require(inventory_queued is False and inventory['boot_id']==decision['boot_id'],'specific stored inventory no longer queued')
|
|
inventory_ack=dict(message_id=inventory['message_id'],server_payload_sha256=inventory['payload_sha256'],local_queue_absent=True,wire_ack_captured=False)
|
|
require(protected(before/'report.json')==original_report,'original interrupted report retained unchanged')
|
|
return dict(case_id=payload['case_id'],observed_from=observed_from,observed_until=now.isoformat(),processes=processes,
|
|
boot_id=decision['boot_id'],generation=decision['connection_generation'],first_start_not_before=first_start.isoformat(),root_consumed=18,root_unresolved=0,
|
|
verified_state_preservation=True,snapshot_consistency='per_page_then_final_same_decision_and_fixed_effect_counts',sqlite_integrity_scan_performed=False,database_witness=summary,verified_outcome_reference_changes=reference_changes,
|
|
agent_meta=agent_meta,helper_meta=helper_meta,signed_sync={k:proof[k] for k in ('payload_sha256','signature_sha256')},
|
|
decision={k:decision[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')},
|
|
full_pin=pin,inventory_queue_convergence=inventory_ack,read_transactions=read_transactions,read_transaction_budget_seconds=1.0,
|
|
transient_delete_journal_observations=journal_observations,rollback_journal_observation_budget_ms=250,
|
|
read_only=True,product_state_writes=False,rmm_jobs=False,approvals=False)
|
|
|
|
|
|
def server(payload):
|
|
"""Stored server state and RLS denial only; no HTTP/live collection or writes."""
|
|
import datetime as dt
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import uuid
|
|
tenant=str(uuid.UUID(payload['install']['target']['tenant_id']))
|
|
agent=str(uuid.UUID(payload['install']['target']['agent_id']))
|
|
require(tenant=='11111111-1111-4111-8111-111111111111','fixed development tenant')
|
|
foreign='22222222-2222-4222-8222-222222222222'
|
|
scope="tenant_id='"+tenant+"' AND agent_id='"+agent+"'"
|
|
query=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SELECT json_build_object('observed_at',now(),"
|
|
"'installation',(SELECT json_build_object('connection_generation',connection_generation,'connection_boot_id',connection_boot_id,"
|
|
"'last_contact_at',last_contact_at,'last_sequence',last_sequence,'last_authenticated_at',last_authenticated_at,"
|
|
"'certificate_sha256',certificate_sha256,'last_authenticated_certificate_sha256',last_authenticated_certificate_sha256,"
|
|
"'revoked_at',revoked_at,'capabilities',capabilities,'sync_ready',sync_ready,'helper_sync_ready',helper_sync_ready,"
|
|
"'restore_blocked',restore_blocked,'sequence_regressed',sequence_regressed,'sync_recovery_required',sync_recovery_required) FROM agent_installations WHERE "+scope+"),"
|
|
"'head',(SELECT row_to_json(s) FROM agent_sync_state s WHERE "+scope+"),"
|
|
"'decisions',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT decision_id,request_id,request_hash,connection_generation,"
|
|
"encode(payload,'hex') AS payload_hex,encode(signature,'hex') AS signature_hex FROM sync_checkpoints WHERE "+scope+" ORDER BY created_at DESC,decision_id DESC LIMIT 16) x),"
|
|
"'inventories',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT message_id,boot_id,sequence,observed_at,received_at,source_id,payload_sha256 FROM observations WHERE "+scope+" ORDER BY received_at DESC LIMIT 16) x),"
|
|
"'jobs',(SELECT COALESCE(json_agg(x),'[]'::json) FROM (SELECT job_id,attempt_id,state,encode(sha256(convert_to(to_jsonb(j)::text,'UTF8')),'hex') AS row_sha256 FROM jobs j WHERE "+scope+" ORDER BY job_id,attempt_id LIMIT 31) x),"
|
|
"'outbox_count',(SELECT count(*) FROM outbox o JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+"),"
|
|
"'permit_count',(SELECT count(*) FROM execution_permits e JOIN jobs j USING(tenant_id,job_id,attempt_id) WHERE "+scope.replace('tenant_id=','j.tenant_id=').replace('agent_id=','j.agent_id=')+")); COMMIT;")
|
|
root=Path('/opt/rust-discovery')
|
|
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
|
|
result=json.loads(subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=25).stdout)
|
|
require(len(result['jobs'])==30 and result['outbox_count']==30 and result['permit_count']==26
|
|
and all(j['state'] in ('succeeded','failed','cancelled','expired') for j in result['jobs']),'same closed30/30/26 central job counts')
|
|
policy=root/'devstate/root-policy/policy.json';m=policy.lstat();raw=policy.read_bytes()
|
|
require(not policy.is_symlink() and m.st_uid==10001 and m.st_mode&0o777==0o600
|
|
and hashlib.sha256(raw).hexdigest()==payload['install']['central_policy_sha256']
|
|
and dt.datetime.fromisoformat(json.loads(raw)['expires_at'])<=dt.datetime.now(dt.timezone.utc),'central Root scope remains protected and closed')
|
|
for row in result['decisions']:
|
|
raw=bytes.fromhex(row.pop('payload_hex'));signature=bytes.fromhex(row.pop('signature_hex'));d=json.loads(raw)
|
|
require(len(raw)<=512*1024 and len(signature)==64,'bounded stored signed decision')
|
|
row['payload_sha256']=hashlib.sha256(raw).hexdigest();row['signature_sha256']=hashlib.sha256(signature).hexdigest()
|
|
row['decision']={k:d[k] for k in ('decision_id','request_id','request_hash','connection_generation','boot_id','journal_id','helper_journal_id','checkpoint','helper_checkpoint','issued_at','valid_until','ready','helper_ready')}
|
|
denial=("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY; SET LOCAL ROLE rmm_app; SET LOCAL app.tenant_id='"+foreign+"'; "
|
|
"SELECT json_build_object('installations',(SELECT count(*) FROM agent_installations WHERE agent_id='"+agent+"'),"
|
|
"'observations',(SELECT count(*) FROM observations WHERE agent_id='"+agent+"'),"
|
|
"'jobs',(SELECT count(*) FROM jobs WHERE agent_id='"+agent+"')); COMMIT;")
|
|
denied=json.loads(subprocess.run(cmd,input=denial.encode(),capture_output=True,check=True,timeout=25).stdout)
|
|
require(denied==dict(installations=0,observations=0,jobs=0),'actual foreign-tenant application-role stored-data denial')
|
|
result.update(tenant_denial=denied,tenant_denial_scope='postgres_rmm_app_rls_not_http',central_root_scope_closed=True,read_only=True,rmm_jobs=False,approvals=False)
|
|
return result
|
|
|
|
|
|
def rpc(prefix,function,payload,folder):
|
|
source=inspect.getsource(function);labels=set()
|
|
for node in ast.walk(ast.parse(source)):
|
|
if not isinstance(node,ast.Call) or not isinstance(node.func,ast.Name):continue
|
|
index=1 if node.func.id=='require' else 0 if node.func.id=='RuntimeError' else None
|
|
if index is not None and len(node.args)>index and isinstance(node.args[index],ast.Constant) and isinstance(node.args[index].value,str):labels.add(node.args[index].value)
|
|
code=inspect.getsource(require)+'\n'+source
|
|
code+='\ntry:\n print(__import__("json").dumps('+function.__name__+'('+repr(payload)+')))\nexcept Exception as e:\n import traceback\n label=str(e) if isinstance(e,RuntimeError) and str(e) in '+repr(sorted(labels))+' else None\n print(__import__("json").dumps(dict(bootstrap_error=True,error_class=type(e).__name__,guard_code=label,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(e.__traceback__)],sqlite_errorcode=getattr(e,"sqlite_errorcode",None),sqlite_errorname=getattr(e,"sqlite_errorname",None))))\n'
|
|
return support.transport(prefix,'python3 -',code.encode(),folder,timeout=240)
|
|
|
|
|
|
def server_prefix():
|
|
cfg=support.access.load_config(pre.ACCESS_ROOT/'credentials/server.conf')
|
|
require(cfg['address']=='88.99.80.202' and cfg['username']=='root' and cfg.get('port','22')=='22' and cfg.get('ssh_key_path'),'fixed existing server bootstrap transport')
|
|
return ['ssh','-F','/dev/null','-T','-p','22','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'root@88.99.80.202']
|
|
|
|
|
|
def parse_stored_time(value):
|
|
"""PostgreSQL RFC3339 at microsecond precision; pad, never round/truncate."""
|
|
import datetime as dt
|
|
import re
|
|
require(isinstance(value,str),'stored UTC timestamp string')
|
|
matched=re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})',value)
|
|
require(matched is not None,'exact bounded PostgreSQL RFC3339 timestamp')
|
|
fraction=('.'+matched[2].ljust(6,'0')) if matched[2] else ''
|
|
zone='+00:00' if matched[3]=='Z' else matched[3]
|
|
return dt.datetime.fromisoformat(matched[1]+fraction+zone)
|
|
|
|
|
|
def compare_samples(samples):
|
|
import datetime as dt
|
|
require(len(samples)==3,'exact baseline and two subsequent passive samples')
|
|
baseline=samples[0];last=baseline
|
|
for sample in samples:
|
|
current=sample['vm'];stored=sample['server'];installation=stored['installation'];d=current['decision']
|
|
require(current['read_only'] is True and current['verified_state_preservation'] is True
|
|
and current['product_state_writes'] is False and current['rmm_jobs'] is False and current['approvals'] is False,'every native VM preservation proof')
|
|
require(stored['read_only'] is True and stored['rmm_jobs'] is False and stored['approvals'] is False
|
|
and stored['central_root_scope_closed'] is True and stored['tenant_denial']==dict(installations=0,observations=0,jobs=0)
|
|
and stored['tenant_denial_scope']=='postgres_rmm_app_rls_not_http'
|
|
and len(stored['jobs'])==30 and stored['outbox_count']==30 and stored['permit_count']==26,'actual stored closed server/tenant-denial probes')
|
|
require(installation['connection_generation']==current['generation']==baseline['vm']['generation']
|
|
and installation['connection_boot_id']==current['boot_id']==baseline['vm']['boot_id']
|
|
and current['processes']==baseline['vm']['processes'],'stable actual new process pair/Full helper and connection generation')
|
|
require(installation['sync_ready'] is True and installation['helper_sync_ready'] is True
|
|
and all(installation[k] is False for k in ('restore_blocked','sequence_regressed','sync_recovery_required'))
|
|
and installation['revoked_at'] is None and installation['certificate_sha256']==installation['last_authenticated_certificate_sha256'],'stored authenticated current ready channel')
|
|
found=[r for r in stored['decisions'] if r['decision_id']==d['decision_id']]
|
|
require(len(found)==1 and found[0]['decision']==d and all(found[0][k]==current['signed_sync'][k] for k in ('payload_sha256','signature_sha256')),'same original signed decision retained at server/agent/helper')
|
|
caps=installation['capabilities'];pin=current['full_pin']
|
|
require(all(c in caps for c in ('rmm.sync.v1','script.full_maintenance/1','root:run_script',
|
|
'helper.policy.sha256:'+pin['policy_sha256'],'helper.binary.sha256:'+pin['helper_binary_sha256'])),'normal fresh agent publication of authenticated Full peer/policy/image capability')
|
|
require(stored['jobs']==baseline['server']['jobs'],'all existing server job state/row hashes unchanged')
|
|
inventory=current['inventory_queue_convergence'];require(inventory is not None,'specific persisted inventory and local queue convergence')
|
|
inventories=[i for i in stored['inventories'] if i['message_id']==inventory['message_id']]
|
|
require(len(inventories)==1 and inventories[0]['payload_sha256']==inventory['server_payload_sha256']
|
|
and inventories[0]['boot_id']==current['boot_id'] and inventories[0]['source_id']=='linux.base'
|
|
and parse_stored_time(inventories[0]['received_at'])>=parse_stored_time(current['first_start_not_before']),'concrete fresh passive stored inventory identity')
|
|
contact=parse_stored_time(installation['last_contact_at'])
|
|
require(contact>=parse_stored_time(current['first_start_not_before'])
|
|
and 0<=(parse_stored_time(stored['observed_at'])-contact).total_seconds()<=45,'fresh stored heartbeat after first start')
|
|
if sample is not baseline:
|
|
require(contact>parse_stored_time(last['server']['installation']['last_contact_at'])
|
|
and installation['last_sequence']>last['server']['installation']['last_sequence'],'two distinct subsequent stored heartbeat advances')
|
|
last=sample
|
|
return dict(verified_preservation=True,verified_normal_signed_sync=True,stored_heartbeat_advances=2,
|
|
specific_inventory_queue_convergence=True,wire_inventory_ack_captured=False,tenant_rls_denial=True,
|
|
tenant_http_denial_tested=False,read_only=True,rmm_jobs=False,approvals=False,full_job_accepted=False)
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('mode',choices=('prepare-local','observe','finish'))
|
|
parser.add_argument('--id')
|
|
args=parser.parse_args()
|
|
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);support.private(STATE,True)
|
|
manifest_path=pre.native.PACK/'manifest.json';manifest=support.load(manifest_path)
|
|
require(support.sha(manifest_path)==pre.native.MANIFEST_SHA and support.sha(pre.NATIVE)==pre.NATIVE_SHA
|
|
and all(support.sha(ROOT/name)==digest for name,digest in manifest['source_files'].items()),'unchanged frozen158 inputs and original Linux-first native proof')
|
|
if args.mode=='prepare-local':
|
|
require(args.id is None,'new local evidence intent only')
|
|
install_path=ROOT/'devstate/script-console-install'/CASE/'intent.json';install=support.load(install_path)
|
|
require(install['id']==CASE and install['preflight_source_sha256']==PREFLIGHT_SHA and install['manifest_sha256']==pre.native.MANIFEST_SHA,'exact reviewed installation provenance')
|
|
folder=STATE/str(uuid.uuid4());folder.mkdir(mode=0o700)
|
|
support.save(folder/'intent.json',dict(type='rmm.script-console.read-only-post-attestation-v1',case_id=CASE,
|
|
runner_sha256=support.sha(__file__),install_intent_sha256=support.sha(install_path),install=install,
|
|
dropin_b64=base64.b64encode(DROPIN).decode(),read_only_target_calls=True))
|
|
else:
|
|
require(args.id is not None and str(uuid.UUID(args.id))==args.id,'canonical original local evidence case')
|
|
folder=STATE/args.id;intent=support.load(folder/'intent.json')
|
|
require(intent['runner_sha256']==support.sha(__file__) and intent['case_id']==CASE,'original same-source observation intent')
|
|
require(support.sha(ROOT/'devstate/script-console-install'/CASE/'intent.json')==intent['install_intent_sha256'],'original local installation intent retained')
|
|
if args.mode=='observe':
|
|
number=len(list(folder.glob('sample-*.json')));require(number<3,'three distinct passive samples only')
|
|
sample_id=str(uuid.uuid4());support.save(folder/('read-intent-'+sample_id+'.json'),dict(read_only=True))
|
|
linux=support.access_settings(pre.ACCESS_ROOT,'linux')
|
|
initial=rpc(linux,vm,intent,folder)
|
|
stored=rpc(server_prefix(),server,intent,folder)
|
|
import datetime as dt
|
|
fresh=[i for i in stored['inventories'] if i['boot_id']==initial['boot_id'] and i['source_id']=='linux.base'
|
|
and parse_stored_time(i['received_at'])>=parse_stored_time(initial['first_start_not_before'])]
|
|
require(fresh,'no fresh startup inventory stored yet; read-only evidence incomplete')
|
|
current=rpc(linux,vm,dict(intent,inventory=fresh[0]),folder)
|
|
stored=rpc(server_prefix(),server,intent,folder)
|
|
support.save(folder/('sample-'+str(number)+'.json'),dict(vm=current,server=stored))
|
|
else:
|
|
samples=[support.load(folder/('sample-'+str(i)+'.json')) for i in range(3)]
|
|
result=compare_samples(samples);support.save(folder/'attested.json',dict(result,case_id=CASE,
|
|
runner_sha256=support.sha(__file__),sample_sha256=[support.sha(folder/('sample-'+str(i)+'.json')) for i in range(3)]))
|
|
print(json.dumps(dict(id=folder.name,intent=str(folder/'intent.json'),mode=args.mode,target_called=args.mode=='observe',read_only=True,rmm_jobs=False,approvals=False)))
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|