byteflow-actors 0.9.28

Embeddable flow runtime: M:N scheduler, Atomic Hop (Message+Cap), supervisor — use byteflow::
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Sandbox policy surface — fingerprint, counters, and capability attestation.
//!
//! The sandbox is **opt-in** via [`crate::RuntimeConfig::sandbox`]. Default
//! [`crate::RuntimeConfig`] stays permissive (unlimited process-wide caps,
//! generous per-flow quotas). This module does **not** replace
//! [`super::capability::CapTable`], [`super::quota::FlowQuota`], or
//! [`crate::MemoryBudget`]; it exposes host-facing policy identity and
//! observability on top of those enforcement points.
//!
//! Isolation is **in-process**. Native code and the host remain inside the
//! same trust boundary — see `docs/sandbox/limitations.md`.

// Docs ratchet: public items here predate `missing_docs`. Delete this
// allow once they are documented; new files get the lint by default.
#![allow(missing_docs)]

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{sync_channel, RecvTimeoutError, SyncSender};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::Duration;

use crate::bytecode::{CapId, CapRights, CapTarget, TrustLevel};

use super::capability::{CapTable, Capability};
use super::error::RuntimeError;
use super::process::FlowId;
use super::runtime::RuntimeConfig;

/// Canonical encoding version mixed into [`policy_fingerprint`].
const POLICY_VERSION: u8 = 1;

/// Stable identity of the security-relevant knobs on a [`RuntimeConfig`].
///
/// The 16 bytes are the leading half of SHA-256 over a fixed-width encoding
/// (see [`policy_fingerprint`]). The same config yields the same bytes on
/// every rustc, OS, and pointer width. It is **not** a cryptographic
/// attestation of process integrity. A value stored before this encoding
/// (std `DefaultHasher`) will not match and must be recomputed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PolicyFingerprint([u8; 16]);

impl PolicyFingerprint {
    pub fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }

    pub fn to_hex(&self) -> String {
        let mut s = String::with_capacity(32);
        for b in &self.0 {
            use std::fmt::Write as _;
            let _ = write!(s, "{b:02x}");
        }
        s
    }
}

impl std::fmt::Display for PolicyFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_hex())
    }
}

/// Compute a fingerprint over the security-relevant fields of `config`.
///
/// Encoding (`POLICY_VERSION` = 1), little-endian, fixed widths so a 32-bit
/// host matches a 64-bit host:
///
/// `b"BFQP" | version | trust | root_authority | max_flows u32 | max_ask_waits u32 |
/// max_host_awaits u32 | max_links u32 | max_monitors u32 | max_registry_names u32 |
/// max_runtime_bytes u64 | mailbox capacity u32 | mailbox bytes u64 | overflow u8 |
/// cpu_budget i64 | mem_limit u64 | spawn_burst i64 | spawn_per_sec i64 |
/// send_burst i64 | send_per_sec i64`
///
/// Workers, quantum, JIT, sinks, and bridges are not part of the policy
/// identity. The returned value is SHA-256 of that buffer, truncated to 16
/// bytes.
pub fn policy_fingerprint(config: &RuntimeConfig) -> PolicyFingerprint {
    let digest = crate::digest::sha256(&canonical_policy_bytes(config));
    let mut out = [0u8; 16];
    out.copy_from_slice(&digest[..16]);
    PolicyFingerprint(out)
}

fn canonical_policy_bytes(config: &RuntimeConfig) -> Vec<u8> {
    let mut buf = Vec::with_capacity(100);
    buf.extend_from_slice(b"BFQP");
    buf.push(POLICY_VERSION);
    buf.push(trust_tag(config.trust));
    buf.push(root_authority_tag(config.root_authority));
    push_u32(&mut buf, config.max_flows);
    push_u32(&mut buf, config.max_ask_waits);
    push_u32(&mut buf, config.max_host_awaits);
    push_u32(&mut buf, config.max_links);
    push_u32(&mut buf, config.max_monitors);
    push_u32(&mut buf, config.max_registry_names);
    push_u64(&mut buf, as_u64(config.max_runtime_bytes));
    push_u32(&mut buf, config.mailbox.capacity().get_u32());
    push_u64(&mut buf, as_u64(config.mailbox.bytes().get()));
    buf.push(config.mailbox.overflow() as u8);
    let q = &config.quota;
    push_i64(&mut buf, q.cpu_budget);
    push_u64(&mut buf, as_u64(q.mem_limit));
    push_i64(&mut buf, q.spawn_burst);
    push_i64(&mut buf, q.spawn_per_sec);
    push_i64(&mut buf, q.send_burst);
    push_i64(&mut buf, q.send_per_sec);
    buf
}

fn as_u64(n: usize) -> u64 {
    match u64::try_from(n) {
        Ok(v) => v,
        Err(_) => u64::MAX,
    }
}

fn push_u32(buf: &mut Vec<u8>, value: u32) {
    buf.extend_from_slice(&value.to_le_bytes());
}

fn push_u64(buf: &mut Vec<u8>, value: u64) {
    buf.extend_from_slice(&value.to_le_bytes());
}

fn push_i64(buf: &mut Vec<u8>, value: i64) {
    buf.extend_from_slice(&value.to_le_bytes());
}

fn trust_tag(trust: TrustLevel) -> u8 {
    match trust {
        TrustLevel::Trusted => 0,
        TrustLevel::Restricted => 1,
        TrustLevel::Untrusted => 2,
    }
}

fn root_authority_tag(policy: crate::scheduler::RootAuthorityPolicy) -> u8 {
    match policy {
        crate::scheduler::RootAuthorityPolicy::TrustedHost => 0,
        crate::scheduler::RootAuthorityPolicy::Restricted => 1,
        crate::scheduler::RootAuthorityPolicy::Untrusted => 2,
    }
}

/// Host-visible security counters. Untrusted bytecode cannot write these.
#[derive(Default, Debug)]
pub struct SecurityCounters {
    pub denied_operations: AtomicU64,
    pub quota_exceeded: AtomicU64,
    pub capabilities_created: AtomicU64,
    pub capabilities_revoked: AtomicU64,
    pub invalid_modules: AtomicU64,
    pub native_denied: AtomicU64,
    pub active_untrusted_flows: AtomicU64,
}

impl SecurityCounters {
    #[inline]
    pub fn inc(counter: &AtomicU64) {
        counter.fetch_add(1, Ordering::Relaxed);
    }

    /// Saturating decrement for [`Self::active_untrusted_flows`].
    pub fn dec_active_untrusted(&self) {
        let mut cur = self.active_untrusted_flows.load(Ordering::Relaxed);
        while cur > 0 {
            match self.active_untrusted_flows.compare_exchange_weak(
                cur,
                cur - 1,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => return,
                Err(actual) => cur = actual,
            }
        }
    }

    pub fn snapshot(&self) -> SecurityCountersSnapshot {
        SecurityCountersSnapshot {
            denied_operations: self.denied_operations.load(Ordering::Relaxed),
            quota_exceeded: self.quota_exceeded.load(Ordering::Relaxed),
            capabilities_created: self.capabilities_created.load(Ordering::Relaxed),
            capabilities_revoked: self.capabilities_revoked.load(Ordering::Relaxed),
            invalid_modules: self.invalid_modules.load(Ordering::Relaxed),
            native_denied: self.native_denied.load(Ordering::Relaxed),
            active_untrusted_flows: self.active_untrusted_flows.load(Ordering::Relaxed),
        }
    }
}

/// Point-in-time copy of [`SecurityCounters`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SecurityCountersSnapshot {
    pub denied_operations: u64,
    pub quota_exceeded: u64,
    pub capabilities_created: u64,
    pub capabilities_revoked: u64,
    pub invalid_modules: u64,
    pub native_denied: u64,
    pub active_untrusted_flows: u64,
}

impl std::fmt::Display for SecurityCountersSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "denied={} quota={} caps_created={} caps_revoked={} invalid_modules={} native_denied={} untrusted_flows={}",
            self.denied_operations,
            self.quota_exceeded,
            self.capabilities_created,
            self.capabilities_revoked,
            self.invalid_modules,
            self.native_denied,
            self.active_untrusted_flows,
        )
    }
}

/// One live capability as seen by host attestation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapabilityGrantView {
    pub id: CapId,
    pub holder: FlowId,
    pub target: CapTarget,
    pub rights: CapRights,
}

impl CapabilityGrantView {
    pub fn from_entry(id: CapId, cap: &Capability) -> Self {
        Self {
            id,
            holder: cap.holder,
            target: cap.cap.target(),
            rights: cap.cap.rights(),
        }
    }
}

/// Host inspection of what a flow currently holds.
///
/// Not a cryptographic proof of process integrity — only a table snapshot.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapabilityAttestation {
    pub flow: FlowId,
    pub trust: TrustLevel,
    pub policy: PolicyFingerprint,
    pub grants: Vec<CapabilityGrantView>,
}

impl CapabilityAttestation {
    pub fn from_table(
        table: &CapTable,
        flow: FlowId,
        trust: TrustLevel,
        policy: PolicyFingerprint,
    ) -> Result<Self, RuntimeError> {
        Ok(Self {
            flow,
            trust,
            policy,
            grants: table.list_held_by(flow)?,
        })
    }
}

/// Compact security event for an optional host audit sink.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SecurityEvent {
    CapabilityCreated { holder: u64 },
    CapabilityDelegated { from: u64, to: u64 },
    CapabilityRevoked { flow: u64, removed: usize },
    AccessDenied { reason: &'static str },
    QuotaExceeded { kind: &'static str },
    ModuleRejected { reason: &'static str },
    NativeDenied { index: u32 },
    FlowFinalized { flow: u64 },
}

impl std::fmt::Display for SecurityEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SecurityEvent::CapabilityCreated { holder } => {
                write!(f, "cap_created holder={holder}")
            }
            SecurityEvent::CapabilityDelegated { from, to } => {
                write!(f, "cap_delegated from={from} to={to}")
            }
            SecurityEvent::CapabilityRevoked { flow, removed } => {
                write!(f, "cap_revoked flow={flow} removed={removed}")
            }
            SecurityEvent::AccessDenied { reason } => write!(f, "access_denied {reason}"),
            SecurityEvent::QuotaExceeded { kind } => write!(f, "quota_exceeded {kind}"),
            SecurityEvent::ModuleRejected { reason } => write!(f, "module_rejected {reason}"),
            SecurityEvent::NativeDenied { index } => write!(f, "native_denied idx={index}"),
            SecurityEvent::FlowFinalized { flow } => write!(f, "flow_finalized id={flow}"),
        }
    }
}

/// Optional host callback for security events (never receives full CapId tokens).
///
/// The runtime delivers events on a **dedicated audit thread** with a bounded
/// queue. `record` on the host sink may block that
/// thread but must not block scheduler workers. Prefer fast, non-panicking
/// sinks; a full queue drops the newest event (fail-open for observability).
pub trait SecurityAuditSink: Send + Sync + std::fmt::Debug + 'static {
    fn record(&self, event: &SecurityEvent);
}

/// Default capacity for the async audit queue between workers and the sink.
pub(super) const DEFAULT_AUDIT_QUEUE: usize = 1024;

/// Join handle for the audit dispatcher started by [`wrap_audit_sink`].
pub(crate) struct AuditWorkerHandle {
    stop: Arc<AtomicBool>,
    join: Option<JoinHandle<()>>,
}

impl AuditWorkerHandle {
    pub(crate) fn stop(mut self) {
        self.shutdown();
    }

    fn shutdown(&mut self) {
        self.stop.store(true, Ordering::Release);
        if let Some(join) = self.join.take() {
            let _ = join.join();
        }
    }
}

impl Drop for AuditWorkerHandle {
    fn drop(&mut self) {
        self.shutdown();
    }
}

#[derive(Debug)]
struct QueuedAuditSink {
    tx: SyncSender<SecurityEvent>,
}

impl SecurityAuditSink for QueuedAuditSink {
    fn record(&self, event: &SecurityEvent) {
        let _ = self.tx.try_send(event.clone());
    }
}

/// Wrap a host [`SecurityAuditSink`] so worker `record` never blocks on it.
///
/// Returns a sink suitable for [`crate::RuntimeConfig::security_audit`] /
/// `Shared`, plus a handle the runtime joins on shutdown (drains remaining
/// events after `stop`).
pub(crate) fn wrap_audit_sink(
    sink: Arc<dyn SecurityAuditSink>,
    capacity: usize,
) -> (Arc<dyn SecurityAuditSink>, AuditWorkerHandle) {
    let capacity = capacity.max(1);
    let (tx, rx) = sync_channel::<SecurityEvent>(capacity);
    let stop = Arc::new(AtomicBool::new(false));
    let stop_worker = Arc::clone(&stop);
    let join = thread::Builder::new()
        .name("byteflow-audit".into())
        .spawn(move || {
            while !stop_worker.load(Ordering::Acquire) {
                match rx.recv_timeout(Duration::from_millis(50)) {
                    Ok(event) => sink.record(&event),
                    Err(RecvTimeoutError::Timeout) => {}
                    Err(RecvTimeoutError::Disconnected) => break,
                }
            }
            while let Ok(event) = rx.try_recv() {
                sink.record(&event);
            }
        })
        .ok();
    (
        Arc::new(QueuedAuditSink { tx }),
        AuditWorkerHandle { stop, join },
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bytecode::TrustLevel;
    use crate::scheduler::mailbox::{MailboxCapacity, MailboxConfig, OverflowPolicy};

    #[test]
    fn sandbox_fingerprint_differs_from_default() {
        let permissive = RuntimeConfig::default();
        let sandboxed = RuntimeConfig::sandbox();
        assert_ne!(
            policy_fingerprint(&permissive),
            policy_fingerprint(&sandboxed)
        );
    }

    #[test]
    fn fingerprint_is_stable_for_identical_config() {
        let a = RuntimeConfig::sandbox();
        let b = RuntimeConfig::sandbox();
        assert_eq!(policy_fingerprint(&a), policy_fingerprint(&b));
    }

    /// Pinned SHA-256 prefixes. A rustc or pointer-width change must not move these.
    #[test]
    fn fingerprint_matches_canonical_sha256_prefix() {
        assert_eq!(
            policy_fingerprint(&RuntimeConfig::default()).to_hex(),
            "2e1be905f47892334e759d3327a05d46"
        );
        assert_eq!(
            policy_fingerprint(&RuntimeConfig::sandbox()).to_hex(),
            "d59bbad5eefb7b525a0cdeaf6708be36"
        );
    }

    #[test]
    fn fingerprint_changes_when_trust_changes() {
        let mut a = RuntimeConfig::sandbox();
        let mut b = RuntimeConfig::sandbox();
        a.trust = TrustLevel::Untrusted;
        b.trust = TrustLevel::Restricted;
        assert_ne!(policy_fingerprint(&a), policy_fingerprint(&b));
    }

    #[test]
    fn fingerprint_changes_when_mailbox_changes() {
        let a = RuntimeConfig::sandbox();
        let mut b = RuntimeConfig::sandbox();
        b.mailbox = MailboxConfig::new(MailboxCapacity::DEFAULT, OverflowPolicy::DropNewest);
        assert_ne!(policy_fingerprint(&a), policy_fingerprint(&b));
    }
}