Verify signed OPNsense kernel probes on the service account and retain exactly-once evidence
Rust development slice / linux (push) Failing after 13s
Rust development slice / linux (push) Failing after 13s
This commit is contained in:
@@ -17,6 +17,6 @@ Der OPNsense-eigene Python-Interpreter führt zwei Zipapps aus. Der lokale Colle
|
||||
|
||||
**Firewall:** aktiver PF-Hauptregelsatz in Kernelreihenfolge, einschließlich automatisch erzeugter Regeln. Kein Editor und kein rekursives Auslesen der Anchor-Regelsätze.
|
||||
|
||||
**Grenzen:** Das passive Profil meldet keine Skript-, Root- oder Release-Capability. Der bestehende Linux-Jobpfad verwendet Linux-spezifische Uhren, Helper und Interpreterprüfung. Für Parität benötigt OPNsense eigene native Adapter für den signierten Abgleich, den Startgrant und das Ausführungsjournal sowie einen eigenen signierten Installer-/Updatekanal. Der OPNsense-Gast darf nicht als Linuxziel für APT- oder Bash-Vorlagen angeboten werden.
|
||||
**Profile:** Ohne Worker und Signaturbindung bleibt das Gerät passiv. Mit dem FreeBSD-Worker unterstützt `0.1.0-opnsense.5` genau die feste Kernelabfrage (`uname -s\n`, `posix_sh`, erwartete Ausgabe `FreeBSD\n`, keine Argumente, Dienstkonto, 10 s, 1024 Byte). Unter **Geräte → OPNsense-Kernelabfrage** lässt sie sich für diese Installation einreichen. Die persönliche WebUI-Genehmigung und der signierte Startgrant sind erforderlich. Root, beliebige Shell-/Bibliotheksskripte und signierte OPNsense-Releases bleiben offen. Das Gerät ist kein Linuxziel.
|
||||
|
||||
**Stand des Rust-Ports:** Im Gast steht die gepinnte native Toolchain unter `/opt/discovery-rust`. Der gemeinsame Abgleich, die Signatur-/Journalverträge, die native Abfrage von `ntp_gettime` mit Zeitfehlergrenze und die Prozessgruppenbeobachtung ohne Linux-`/proc` sind nativ getestet. `cargo check -p rmm-agent --bin rmm-agent` erreicht nach der `waitid`-Portierung den Linux-Helper, scheitert dort aber an `SO_PEERCRED`, `adjtimex` und den nur für Linux exportierten Updater-Modulen. Der laufende Telemetriedienst verwendet diese unvollständige Jobstrecke nicht. Ein Shellprozess-Test auf FreeBSD ist kein genehmigter Discovery-Auftrag. Zum nächsten Nachweis gehören ein über die WebUI genehmigter fester POSIX-Auftrag, sein genau einmal verbrauchter Startgrant und ein natives Ergebnis unter derselben Geräteidentität.
|
||||
**Probe bereitstellen:** Im Gast steht die gepinnte Toolchain unter `/opt/discovery-rust`. `cargo build --locked -p rmm-opnsense-jobs` nativ bauen und `rmm-opnsense-jobs` root-eigen (0555) neben den Zipapps installieren. Auf der bereits aufgenommenen Testinstallation `python3 -I pin-probe.py TENANT ASSET AGENT ENROLLMENT_GENERATION ASSIGNMENT_VERSION` mit den bestätigten Kennungen aufrufen. Das Skript bezieht den öffentlichen Jobschlüssel über das bestehende Gerätezertifikat und überschreibt keine andere Bindung. Den Agentendienst anschließend neu starten. Der Worker nutzt bestehende Nachrichtenfolgen, den signierten Abgleich und getrennte dauerhafte Start-/Ergebnisjournale. Er läuft ohne Netzwerklistener als `discovery`. Der [native Integrationstest](../../crates/agent-opnsense-jobs/src/native/tests.rs) prüft mit eigener Testidentität und eigenem Schlüssel die Ablehnung einer defekten Signatur, erwartete Ausgabe, einen Verbrauch und keine zweite Ausführung bei Wiederzustellung. Das ist ein separater Beleg vom persönlich genehmigten Geräteauftrag.
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""Unprivileged OPNsense telemetry channel. Input never selects a local command."""
|
||||
import hashlib
|
||||
import contextlib
|
||||
import fcntl
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
@@ -18,7 +20,9 @@ STATE = Path('/var/db/discovery-agent')
|
||||
CA = '/usr/local/etc/discovery-agent/ca.pem'
|
||||
HOST = 'discovery.ease-technologies.de'
|
||||
MEDIA = 'application/x-protobuf'
|
||||
VERSION = '0.1.0-opnsense.4'
|
||||
VERSION = '0.1.0-opnsense.5'
|
||||
WORKER = Path('/usr/local/libexec/discovery-agent/rmm-opnsense-jobs')
|
||||
BINDING = Path('/usr/local/etc/discovery-agent/job-binding.json')
|
||||
|
||||
|
||||
def post(ctx, path, data, media):
|
||||
@@ -111,10 +115,73 @@ def usage(previous):
|
||||
return sample,ticks
|
||||
|
||||
|
||||
class Jobs:
|
||||
"""Private pipe to the non-root worker. The mTLS agent owns the transport."""
|
||||
def __init__(self, process):
|
||||
self.process = process
|
||||
|
||||
def read(self):
|
||||
line = self.process.stdout.readline(2_100_000)
|
||||
if not line.endswith(b'\n'):
|
||||
raise ValueError('job worker disconnected')
|
||||
return line[:-1]
|
||||
|
||||
def call(self, line):
|
||||
self.process.stdin.write(line+b'\n')
|
||||
self.process.stdin.flush()
|
||||
return self.read()
|
||||
|
||||
def received(self, envelope):
|
||||
if self.call(envelope.SerializeToString().hex().encode()) != b'ok':
|
||||
raise ValueError('job response not committed')
|
||||
|
||||
def pump(self, ctx, agent, boot, generation):
|
||||
for _ in range(12):
|
||||
frame = self.call(b'next')
|
||||
if not frame:
|
||||
return
|
||||
e = pb.Envelope.FromString(bytes.fromhex(frame.decode('ascii')))
|
||||
kind = e.WhichOneof('payload')
|
||||
if (e.protocol_major!=1 or e.agent_id!=agent or e.enrollment_generation!=1 or
|
||||
e.connection_generation!=generation or kind not in (
|
||||
'job_accepted','job_state_changed','job_result','sync_request','execution_permit_request')):
|
||||
raise ValueError('invalid outbound job event')
|
||||
batch = pb.MessageBatch(protocol_major=1,messages=[e])
|
||||
reply = pb.MessageBatch.FromString(post(ctx,'messages',batch.SerializeToString(),MEDIA))
|
||||
expected = {'sync_request':'sync_response','execution_permit_request':'execution_permit'}.get(kind,'ack')
|
||||
check_reply(reply,e,expected)
|
||||
for r in reply.messages:
|
||||
self.received(r)
|
||||
|
||||
|
||||
def check_reply(batch,e,expected):
|
||||
if batch.protocol_major!=1 or not 1<=len(batch.messages)<=16:
|
||||
raise ValueError('unexpected reply')
|
||||
first = batch.messages[0]
|
||||
generation = first.server_hello.connection_generation if expected=='server_hello' else e.connection_generation
|
||||
for index,r in enumerate(batch.messages):
|
||||
if (r.protocol_major,r.protocol_minor,r.agent_id,r.enrollment_generation,r.connection_generation,
|
||||
r.boot_id,r.sequence)!=(1,0,e.agent_id,e.enrollment_generation,generation,e.boot_id,e.sequence):
|
||||
raise ValueError('reply identity')
|
||||
kind = r.WhichOneof('payload')
|
||||
if (not index and kind!=expected) or (index and kind not in ('job_offer','cancel_request')):
|
||||
raise ValueError('unexpected job reply')
|
||||
if expected=='ack' and first.ack.message_id!=e.message_id:
|
||||
raise ValueError('unconfirmed message')
|
||||
return first
|
||||
|
||||
|
||||
def session(db, identity, boot, instance):
|
||||
with contextlib.ExitStack() as stack:
|
||||
return run_channel(db, identity, boot, instance, stack)
|
||||
|
||||
|
||||
def run_channel(db, identity, boot, instance, stack):
|
||||
ctx = context()
|
||||
ctx.load_cert_chain(STATE/'device.pem', STATE/'device.key')
|
||||
generation = 0
|
||||
jobs = None
|
||||
enabled = BINDING.is_file() and WORKER.is_file()
|
||||
|
||||
def send(kind, payload, captured=None):
|
||||
# A lost inventory ACK replays the exact durable envelope, including its
|
||||
@@ -136,12 +203,11 @@ def session(db, identity, boot, instance):
|
||||
db.execute('INSERT INTO pending VALUES(1,?,?)',(e.SerializeToString(),e.observed_at))
|
||||
batch = pb.MessageBatch(protocol_major=1, messages=[e])
|
||||
reply = pb.MessageBatch.FromString(post(ctx, 'messages', batch.SerializeToString(), MEDIA))
|
||||
if reply.protocol_major != 1 or len(reply.messages) != 1:
|
||||
raise ValueError('unexpected reply')
|
||||
r = reply.messages[0]
|
||||
if (r.protocol_major,r.agent_id,r.enrollment_generation,r.boot_id,r.sequence) != (
|
||||
1,e.agent_id,1,e.boot_id,e.sequence):
|
||||
raise ValueError('reply identity')
|
||||
r = check_reply(reply,e,'server_hello' if kind=='client_hello' else 'ack')
|
||||
if len(reply.messages)>1 and jobs is None:
|
||||
raise ValueError('work outside established job session')
|
||||
for extra in reply.messages[1:]:
|
||||
jobs.received(extra)
|
||||
if kind == 'client_hello':
|
||||
if (r.WhichOneof('payload') != 'server_hello' or r.connection_generation == 0 or
|
||||
r.connection_generation != r.server_hello.connection_generation or
|
||||
@@ -158,7 +224,25 @@ def session(db, identity, boot, instance):
|
||||
if renew(ctx, identity):
|
||||
ctx = context()
|
||||
ctx.load_cert_chain(STATE/'device.pem', STATE/'device.key')
|
||||
generation = send('client_hello',pb.ClientHello(agent_version=VERSION, capabilities=['heartbeat/1','opnsense.base/1']))
|
||||
caps = ['heartbeat/1','opnsense.base/1']
|
||||
if enabled:
|
||||
caps.extend(['rmm.sync.v1','run_script','opnsense.uname/1'])
|
||||
generation = send('client_hello',pb.ClientHello(agent_version=VERSION, capabilities=caps))
|
||||
if enabled:
|
||||
p = subprocess.Popen([WORKER,identity['agent_id'],identity['asset_id'],boot,str(generation)],
|
||||
stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True)
|
||||
def close():
|
||||
p.stdin.close()
|
||||
try:
|
||||
p.wait(timeout=15)
|
||||
except subprocess.TimeoutExpired:
|
||||
p.kill(); p.wait()
|
||||
finally:
|
||||
p.stdout.close()
|
||||
stack.callback(close)
|
||||
jobs = Jobs(p)
|
||||
if jobs.read()!=b'ready':
|
||||
raise ValueError('job worker unavailable')
|
||||
last = None
|
||||
previous = None
|
||||
check_cert = time.monotonic()+12*3600
|
||||
@@ -187,6 +271,8 @@ def session(db, identity, boot, instance):
|
||||
scope='opnsense.base',schema_version=1,chunk_count=1,data=raw,
|
||||
sha256=hashlib.sha256(raw).digest(),completeness='partial'),stamp//1_000_000)
|
||||
last = stamp
|
||||
if jobs:
|
||||
jobs.pump(ctx,identity['agent_id'],boot,generation)
|
||||
time.sleep(20)
|
||||
|
||||
|
||||
@@ -194,6 +280,9 @@ def main():
|
||||
os.umask(0o077)
|
||||
if sys.argv[1:] == ['enroll']:
|
||||
return enroll()
|
||||
# Never allow a second network actor to fence or race an in-flight worker.
|
||||
lock = (STATE/'channel.lock').open('ab')
|
||||
fcntl.flock(lock,fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
identity = json.loads((STATE/'identity.json').read_bytes())
|
||||
db = sqlite3.connect(STATE/'channel.sqlite')
|
||||
with db:
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Provision the first service-account probe on an existing development installation.
|
||||
Run as root in FreeBSD with the operator-confirmed tenant/asset/agent UUIDs and
|
||||
enrollment/assignment generations. Fetch only the public signing pin as discovery.
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
|
||||
def main():
|
||||
if os.geteuid() or len(sys.argv)!=6:
|
||||
raise SystemExit('pin-probe.py TENANT ASSET AGENT ENROLLMENT_GENERATION ASSIGNMENT_VERSION')
|
||||
tenant,asset,agent = (str(uuid.UUID(s)) for s in sys.argv[1:4])
|
||||
enroll,assignment = map(int,sys.argv[4:])
|
||||
if min(enroll,assignment)<1:
|
||||
raise ValueError('invalid generation')
|
||||
# Device-owned inputs are opened only after dropping privileges.
|
||||
source = """
|
||||
import hashlib,http.client,json,ssl
|
||||
from pathlib import Path
|
||||
p=Path('/var/db/discovery-agent')
|
||||
i=json.loads((p/'identity.json').read_bytes())
|
||||
ctx=ssl.create_default_context(cafile='/usr/local/etc/discovery-agent/ca.pem')
|
||||
ctx.load_cert_chain(p/'device.pem',p/'device.key')
|
||||
c=http.client.HTTPSConnection('discovery.ease-technologies.de',context=ctx,timeout=10)
|
||||
c.request('GET','/api/v1/job-signing-key')
|
||||
r=c.getresponse()
|
||||
if r.status!=200:raise ValueError('device trust endpoint rejected')
|
||||
print(json.dumps([i['agent_id'],i['asset_id'],json.loads(r.read(4097))]))
|
||||
"""
|
||||
current,bound_asset,k=json.loads(subprocess.check_output(
|
||||
['su','-m','discovery','-c','/usr/local/bin/python3 -I -'],input=source.encode(),timeout=15))
|
||||
key = k['public_key_hex']
|
||||
if (current,bound_asset)!=(agent,asset) or not re.fullmatch('[a-f0-9]{64}',key) or (
|
||||
k['algorithm'],k['schema_version'],k['key_id'])!=('Ed25519',1,hashlib.sha256(bytes.fromhex(key)).hexdigest()):
|
||||
raise ValueError('conflicting device or signing key')
|
||||
payload=dict(agent_id=agent,asset_id=asset,tenant_id=tenant,enrollment_generation=enroll,
|
||||
assignment_version=assignment,public_key_hex=key)
|
||||
p=Path('/usr/local/etc/discovery-agent/job-binding.json')
|
||||
try:
|
||||
with p.open('x') as f:
|
||||
os.fchmod(f.fileno(),0o444); json.dump(payload,f); f.flush(); os.fsync(f.fileno())
|
||||
except FileExistsError:
|
||||
if p.is_symlink() or json.loads(p.read_bytes())!=payload: raise ValueError('existing binding differs')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
@@ -16,6 +16,19 @@ spec.loader.exec_module(a)
|
||||
|
||||
|
||||
class Reconnect(unittest.TestCase):
|
||||
def test_piggybacked_work_requires_same_authenticated_channel_and_exact_receipt(self):
|
||||
e=a.pb.Envelope(protocol_major=1,enrollment_generation=1,connection_generation=3,
|
||||
agent_id=str(uuid.uuid4()),boot_id=str(uuid.uuid4()),sequence=42,message_id=str(uuid.uuid4()))
|
||||
ack=a.pb.Envelope();ack.CopyFrom(e);ack.ack.message_id=e.message_id
|
||||
offer=a.pb.Envelope();offer.CopyFrom(e);offer.job_offer.job_id=str(uuid.uuid4())
|
||||
batch=a.pb.MessageBatch(protocol_major=1,messages=[ack,offer])
|
||||
self.assertEqual(a.check_reply(batch,e,'ack'),ack)
|
||||
batch.messages[1].connection_generation=2
|
||||
with self.assertRaisesRegex(ValueError,'identity'): a.check_reply(batch,e,'ack')
|
||||
del batch.messages[1:]
|
||||
batch.messages[0].ack.message_id=str(uuid.uuid4())
|
||||
with self.assertRaisesRegex(ValueError,'unconfirmed'): a.check_reply(batch,e,'ack')
|
||||
|
||||
def test_lost_ack_keeps_original_identity_after_restart(self):
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
db = sqlite3.connect(':memory:')
|
||||
|
||||
Reference in New Issue
Block a user