byteflow-actors 0.5.0

Embeddable flow runtime: bytecode VM, M:N scheduler, Atomic Hop + FlowCap, supervisor
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Byteflow Security Model


## Status


This document defines the security contract of the Byteflow runtime.

The security model is intentionally capability-oriented. Byteflow does not
consider a numeric FlowId, Message field, opcode, or register value to be an
authority by itself.

The runtime is the security boundary.

---

## 1. Threat Model


Byteflow executes bytecode that may be supplied by an untrusted module.

The following distinction is fundamental:

### Trusted components


The following components are trusted:

- host Rust code;
- the Byteflow runtime;
- the scheduler;
- the mailbox implementation;
- the flow directory;
- native functions explicitly registered by the host;
- the host supervisor responsible for configuring the runtime;
- verified bytecode admission performed by the host.

Trusted components are responsible for enforcing all authority boundaries.

Bytecode must never be trusted to enforce its own security policy.

### Untrusted components


The following are considered untrusted:

- `.bf` bytecode;
- bytecode instructions;
- register contents;
- values constructed by bytecode;
- Message fields supplied by bytecode;
- Pid values supplied by bytecode;
- native arguments supplied by bytecode;
- control flow generated by bytecode.

A malformed or malicious bytecode module must therefore be assumed capable of
deliberately constructing invalid values whenever the VM permits such values.

---

## 2. Security Boundary


The VM is a computation engine, not the authority manager.

The VM may validate structural invariants required by an instruction, but it
must not grant authority merely because a bytecode value claims to possess it.

The scheduler and runtime own:

- flow identity;
- message delivery;
- mailbox ownership;
- sender authentication;
- capability resolution;
- native authority;
- resource quotas.

This distinction is critical.

A register containing:

```text
Value::Pid(42)
```

does not prove that the current flow is authorized to communicate with flow 42.

Likewise:

```text
Message {
    sender: 42,
    ...
}
```

does not prove that flow 42 actually created the message.

---

## 3. Atomic Hop Security


Byteflow's Atomic Hop model establishes that Send and Ask operate on
`Value::Message`.

This provides an important structural invariant:

```text
Send/Ask -> Message -> mailbox
```

However, structural typing is not authentication.

A Message constructed by bytecode may contain arbitrary application data,
including a forged sender field.

Therefore the runtime MUST authenticate the sender at the scheduler boundary.

The authoritative sender is:

```text
current_flow.id
```

and never:

```text
Message.sender
```

provided by bytecode.

---

## 4. Authenticated Sender


### Rule


Before a Message is delivered by Send or Ask, the runtime MUST overwrite the
sender field with the identity of the flow performing the operation.

Conceptually:

```text
message.sender = current_flow.id;
```

The value originally contained in `Message.sender` MUST NOT influence the
authenticated origin of the hop.

This means the following bytecode behavior is intentionally ineffective:

```text
make_msg(
    sender = victim_flow,
    request_id = 1,
    tag = REQUEST,
    payload = 41
)
```

followed by `Send(...)`. The receiver MUST observe `sender = actual_sender_flow`
and never `sender = victim_flow`.

Host-side [`Runtime::send`](../src/scheduler/runtime.rs) remains a **trusted**
injection path: the host may choose `sender` (including `0` for non-flow
origins). Only bytecode-driven `Send` / `Ask` are re-stamped.

---

## 5. Why Sender Authentication Happens in the Worker


Sender stamping MUST happen after the VM has identified the current flow and
before the message enters another flow's mailbox.

The VM does not own mailbox state and must not become responsible for
scheduler authority.

The intended execution boundary is:

```text
bytecode
   |
   v
VM validates Message
   |
   v
VmResult::Send / VmResult::Ask
   |
   v
scheduler / worker
   |
   +--> authenticate sender
   |
   +--> resolve destination
   |
   +--> deliver mailbox message
   |
   v
target flow
```

The scheduler therefore becomes the single authority responsible for the
identity associated with an outgoing hop.

---

## 6. Ask Security


Ask uses request correlation.

The reply MUST NOT be accepted solely because the request_id matches.

The authenticated reply rule is:

```text
reply.request_id == request.request_id
&&
reply.sender == target
```

where `target` is the flow to which the request was delivered.

This prevents an unrelated flow from satisfying an outstanding Ask by
guessing or reusing a request_id.

The request itself is also sender-stamped before delivery.

Therefore:

- forged request sender → overwritten by runtime
- forged reply sender → rejected by Ask correlation

are separate security properties. Both are required.

After FlowCap (0.5.x), Ask must compare `reply.sender` to the **resolved
FlowId** behind the target Cap — never to a CapId.

---

## 7. FlowCap — Current Version (0.5.x)


Bytecode `Send` / `Ask` require [`Value::Cap`]. A CapId is opaque and resolves
through the runtime `CapTable` to `{ FlowId, CapRights }` (`SEND`, `ASK`).

[`Value::Pid`] remains for **identity** inside authenticated messages
(`Message.sender` / `msg_sender`). It is **not** an ambient address.

Outgoing hops also mint `Message.reply_cap` with **SEND**-only rights so a
receiver can answer without knowing or forging a Pid address.

`SelfPid` and bytecode `Spawn` write a Cap (`SEND|ASK`) into the destination
register — not a raw Pid.

Host `Runtime::send(FlowId, …)` remains a trusted host path (no Cap required).

---

## 8. Message Construction


The current `make_msg` native may accept a sender field for compatibility with
the existing Value/Message ABI.

That field MUST be considered untrusted metadata.

The sender field supplied by `make_msg` is never authoritative.

For outgoing Send and Ask operations:

```text
runtime_sender = current_flow.id
```

always wins.

Future versions may remove the sender argument from `make_msg` entirely.
That is an API cleanup, not a prerequisite for sender authentication.

---

## 9. Native Authority


Native functions are trusted host extensions.

A native function executes with authority granted by the host's native table.

Bytecode must not be able to manufacture native authority merely by providing
a numeric native index.

The current native-index ABI is preserved in 0.4.x.

Native capabilities and per-flow native allowlists are planned for a later
security phase.

Until then:

- NativeTable configuration = trusted
- native index supplied by bytecode = untrusted input

---

## 10. Mailbox Security


Mailbox operations are scheduler-owned operations.

The mailbox MUST preserve the existing anti-lost-wakeup invariant:
`park + push/wake` must be synchronized under the same mailbox synchronization
boundary.

Selective receive MUST preserve FIFO-skip semantics.

A message that does not satisfy the active waiter filter must remain in the
mailbox.

Security changes MUST NOT weaken these synchronization guarantees.

---

## 11. Fail-Closed Policy


Security failures must fail closed.

Production code MUST NOT use `unwrap()` / `expect()` for runtime security
decisions.

Poisoned synchronization primitives MUST NOT be recovered through
`PoisonError::into_inner()`.

A poisoned security-relevant lock is treated as a runtime failure.

The runtime must prefer refusing an operation over continuing with potentially
corrupted authority state.

---

## 12. Panic Isolation


Untrusted bytecode must not be able to directly terminate the host process
through ordinary VM execution.

Runtime boundaries should convert invalid bytecode operations into explicit
VM traps or scheduler errors.

Host-native code remains trusted.

A malicious or incorrectly implemented native may still violate host-level
assumptions. Native isolation is therefore outside the VM's trust guarantees.

---

## 13. Denial of Service


Byteflow 0.4.x does not yet provide complete resource isolation.

The following remain known limitations:

- unlimited or insufficiently bounded mailbox growth;
- unrestricted flow creation where permitted by the host;
- potentially unbounded outstanding Ask operations;
- native functions that consume arbitrary host resources.

Resource quotas are planned for the quota phase.

Capability security prevents unauthorized access but does not automatically
prevent an authorized flow from exhausting resources.

---

## 14. Timing Side Channels


Byteflow does not currently claim resistance against timing side channels.

Observable differences may exist through scheduling, mailbox contention,
message latency, native execution, flow starvation, and resource exhaustion.

Applications requiring side-channel resistance must implement additional
isolation at the host/platform level.

---

## 15. FFI Boundary


FFI and native code are trusted boundaries.

Values crossing the FFI boundary must be validated before being converted into
runtime-owned structures.

A host must not construct invalid internal runtime state through unsafe or
unchecked FFI integration.

The Byteflow runtime does not treat an FFI-provided value as trusted merely
because it originated outside bytecode.

---

## 16. Future Capability Model


The target security architecture is object-capability based.

The intended model is `Value::Cap(CapId)` where CapId is opaque and is not a
FlowId.

A capability resolves through the runtime directory to `{ FlowId, Rights }`
(e.g. `SEND`, `ASK`, `LINK`, `ADMIN`).

The bytecode-visible capability MUST NOT expose the underlying FlowId.

Capability creation, delegation and attenuation belong to the trusted runtime.

---

## 17. Security Invariants


The following invariants are normative.

### S1 — Sender authenticity


A receiver observes the sender assigned by the runtime, never the sender
claimed by bytecode.

### S2 — Ask reply authenticity


An Ask completes only when the reply matches both `request_id` and
`sender == requested target`.

### S3 — VM authority separation


The VM does not directly manipulate mailboxes, scheduler state, timers or
threads.

### S4 — Fail closed


Invalid security state results in an error/trap rather than recovery using
possibly corrupted state.

### S5 — FIFO selective receive


Selective waiting does not discard unrelated messages.

### S6 — Cap is the address; Pid is identity


Bytecode addressing for `Send` / `Ask` uses `Value::Cap`. `Value::Pid` is
authenticated identity only and does not grant delivery authority.

### S7 — Native index is not inherently a capability


Native authorization is currently host-configured and is not yet represented
as a first-class bytecode capability.

---

## 18. Security Roadmap


### Phase 1 (done)


Authenticated sender stamping on bytecode `Send` / `Ask`.

### Phase 2 (done — 0.5.x)


Flow capabilities: `Value::Cap` for Send/Ask; `reply_cap` grant; Pid = identity.

### Phase 3


Native capabilities and resource quotas.

### Phase 4


Optional host-side bytecode attestation.

---

## 19. Non-Goals


The following are explicitly outside the current security model:

- cryptographic authentication between flows;
- encrypted mailbox contents;
- OS sandboxing / seccomp / process isolation;
- constant-time scheduling;
- protection against malicious trusted natives;
- complete DoS resistance;
- cryptographic module attestation.

These may be implemented at higher layers where appropriate.