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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user