Formal Security & Quaternion Invariants Statement
Status: Audit-Grade Formal Specification (grounded in live workspace implementations)
Scope:sections/manifold/b8g-kernel-engine.mts,sections/ephemeral-token/index.mts,sections/chromium-build/inmem-linker/b8g_origin_linker.h, andB8G-ABI v2 -> v3C-ABI contracts.
Machine-Checkable Verification: sections/manifold/quaternion-security-invariants.test.mts
Executive Summary & Trust Boundary
This document establishes four formal invariants governing mathematical state-space evolution, capability confinement, executable memory isolation, and binary ABI compatibility across Awesome-OS:
| # | Domain | Core Invariant | Primary Implementation | ||
|---|---|---|---|---|---|
| :- | :--- | :--- | :--- | ||
| 1 | S3 Unit-Quaternion Algebra | $\ | q\ | = 1$, q-1 = q*, exact rollback q*(q v q*)q = v | sections/manifold/b8g-kernel-engine.mts |
| 2 | Ephemeral Capability Lattice | Cchild ≤ Cparent, RAM-only lease, fill(0) + revoke() on dispose() |
sections/ephemeral-token/index.mts |
||
| 3 | Strict W^X & O(1) Origin Arena |
∀ i,p: ¬(Write(p,i) ∧ Exec(p,i)), 0xCC guards, atomic VirtualFree/munmap |
sections/chromium-build/inmem-linker/b8g_origin_linker.h |
||
| 4 | B8G-ABI v2 -> v3 Prefix Contract |
_Static_assert(offsetof(V3, base) == 0) + runtime struct_size bounds check |
packages/@awesome-os/b8g/b8g-abi/b8g_abi.h, b8g_origin_linker.h |
Section 1: Algebraic S3 Unit-Quaternion State & Conjugation Rollback Formalism
1.1 Mathematical Definition
Let ℍ = { w + xi + yj + zk ∣ w, x, y, z ∈ ℝ } be the division ring of Hamilton quaternions with basis relations:
For q = w + xi + yj + zk ∈ ℍ, define:
- Euclidean Norm: ‖q‖ = √(w2 + x2 + y2 + z2).
- Conjugate: q* = w - xi - yj - zk.
- Unit 3-Sphere Group: S3 = { q ∈ ℍ ∣ ‖q‖ = 1 } ≅ SU(2).
For every q ∈ S3:
Embed any 3D vector vecv = (vx, vy, vz) ∈ ℝ3 as a pure imaginary quaternion v = 0 + vx i + vy j + vz k ∈ Im(ℍ). The adjoint (conjugation) action of q ∈ S3 on Im(ℍ) is:
The map π: S3 → SO(3) defined by π(q) = Φq is a surjective Lie group homomorphism with ker(π) = { +1, -1 } (the spinor double cover of SO(3)).
1.2 Exact Conjugation Rollback
Because q-1 = q* on S3 and quaternion multiplication is associative, applying the conjugate action Φq* to v' = q v q* yields exact algebraic rollback:
For a sequence of state-space orientation transitions q1, q2, …, qn ∈ S3 with composite rotor Q = qn ⋯ q2 q1 ∈ S3, the exact inverse transition is Q* = q1* q2* ⋯ qn*, restoring v = Q* (Q v Q*) Q in a single conjugation step.
1.3 Verified Mathematical Properties
In sections/manifold/b8g-kernel-engine.mts (UnitQuaternion, lines 151–263; B8GKernelArena.step, lines 508–536):
- Norm Preservation (Closure on S3):
- Analytically: ‖q1 q2‖ = ‖q1‖‖q2‖ = 1 and ‖q v q*‖ = ‖q‖‖v‖‖q*‖ = ‖v‖.
- Numerically:
UnitQuaternionnormalizes on construction (normalize()), andB8GKernelArena.step()re-projects every integrated rotor onto S3 (qw = nqw / rawNorm, etc.), withauditNormInvariants()tracking maxi |‖qi‖ - 1| and emittingTRAP_QUATERNION_NORM_DRIFTif drift exceeds tolerance. - Exact Invertibility (Conjugation Rollback):
- Given
v' = q.rotateVec3(vx, vy, vz)andqConj = new UnitQuaternion(q.w, -q.x, -q.y, -q.z),v'' = qConj.rotateVec3(v'.x, v'.y, v'.z)satisfies ‖v'' - v‖2 < 10-12 under IEEE-754float64arithmetic. - Gimbal-Lock-Free Geodesic Interpolation (SLERP):
UnitQuaternion.slerp(target, t)canonicalizes the antipodal hemisphere (if (cosOmega < 0) negate target) to guarantee shortest-arc geodesic interpolation on S3, clampscosOmegato [-1, 1] to preventNaN, falls back to normalized linear interpolation (NLERP) for cosΩ > 0.9995, and preserves unit norm across all t ∈ [0, 1] with exact endpoints at t = 0 and t = 1.
1.4 Honest Security Boundary Note
[!IMPORTANT]
Architectural Honesty Boundary:UnitQuaternionon S3 models geometric and state-space orientation and algebraically reversible transitions (specifically the nonholonomic multi-sphere rolling manifold SO(3) × ℝ2 insections/manifold/b8g-kernel-engine.mts).
>
Quaternion multiplication and conjugation are not cryptographic primitives: they are linear over ℝ4, trivially invertible by any observer who knows q, and provide neither confidentiality, integrity authentication, nor memory isolation.
>
Cryptographic, capability, and memory-isolation guarantees are enforced exclusively by the concrete runtime mechanisms formalized in Sections 2–4 below—together with the branchless 64-bit constant-time capability gate (B8GMemoryArena64.ctEq64/UniversalCapabilityGate, lines 98–145 ofb8g-kernel-engine.mts), which verifies((capabilityToken & requiredMask) ^ requiredMask) == 0without data-dependent branching.
Section 2: Monotonic Capability Narrowing & Ephemeral Token Invariants
Implementation: sections/ephemeral-token/index.mts
2.1 Formal Capability Lattice & Monotonic Narrowing
Model the authority space as a bounded meet-semilattice (𝒞, ≤, sqcap, bot) where each capability C = (scope, [tissue, texp], state) carries a scope set, a finite validity interval, and a lifecycle state state ∈ {ACTIVE, REVOKED}, with bot denoting the revoked / zero-authority element.
- Partial Order (≤): Cchild ≤ Cparent iff:
- scope(Cchild) ⊆ scope(Cparent),
- [tissue(Cchild), texp(Cchild)] ⊆ [tissue(Cparent), texp(Cparent)], and
- state(Cparent) = REVOKED ⇒ state(Cchild) = REVOKED (i.e., Cchild = bot).
- Monotonic Attenuation Invariant: Every delegation or sub-lease derivation applies Cchild = Cparent sqcap Creq ≤ Cparent. No operation in the capability API can synthesize a capability C' with C' not≤ Cparent.
2.2 RAM-Only Lease Lifecycle
EphemeralCapabilityIntersection replaces static long-lived secrets with short-lived, scope-bound leases held exclusively in process RAM:
- Acquisition (
acquire()): Mints a scoped lease viaprovider.mint(this.scope, this.ttlMs)(minimumttlMsclamped to50ms). Stores secret bytes in a privateBuffer(this.secretBuf) and schedules an unref'd auto-refresh timer at 60% of TTL (Math.max(25, Math.floor(ttlMs * 0.6))). Rejects immediately withCapabilityRevokedError(ERR_CAPABILITY_REVOKED) ifthis.revoked === true. - Scoped Use (
withSecret(fn)): Checks!this.revoked && this.secretBuf && this.lease. IfDate.now() > this.lease.expiresAtMs, attempts a synchronous-awaittryRefresh(); if refresh fails or the lease remains expired/revoked, it fails closed by throwingCapabilityRevokedError. - Refresh Hygiene (
storeSecret()): Before replacingthis.secretBufon refresh, overwrites the previous buffer in-place viathis.secretBuf.fill(0). - Automatic Log Redaction (
redact(text)): Replaces every occurrence of the active secret (length ≥ 4) in outbound logs/strings with***EPHEMERAL_REDACTED***.
2.3 Deterministic Disposal (fill(0) + provider.revoke())
EphemeralCapabilityIntersection.dispose() (lines 120–136) enforces an irreversible transition to bot:
- Idempotent Lockout:
if (this.revoked) return; this.revoked = true;— immediately blocks concurrentacquire(),tryRefresh(), andwithSecret()calls. - Timer Teardown:
clearInterval(this.refreshTimer); this.refreshTimer = null;— halts any future background refresh. - In-Place Memory Zeroing:
this.secretBuf.fill(0); this.secretBuf = null;— deterministically overwrites every byte of the backingBufferwith0x00before releasing the reference. - Upstream Revocation: Detaches
this.lease = nulland awaitsthis.provider.revoke(oldLease)so the remote/local issuer invalidates the token immediately.
2.4 Proof Sketch of Non-Escalation & Post-Disposal Unreachability
- Theorem (Non-Escalation): Given a parent intersection bound to
scopeand monotonic derivation Cchild ≤ Cparent, no call sequence onEphemeralCapabilityIntersectioncan widenthis.scope(declaredprivate readonly), extend authority pastdispose(), or recover a secret afterdispose(). - Proof Sketch:
scopeis immutable (private readonly) after construction;acquire()andtryRefresh()only requestthis.scope.revokedtransitions monotonically false → true and has no reset path.- On
dispose(),revokedis set totruebefore anyawait(provider.revoke), eliminating TOCTOU re-entry windows during async revocation. Bothacquire()andwithSecret()checkthis.revokedas their first guard and throwCapabilityRevokedError. secretBuf.fill(0)zeroes the mutable backing store prior to dereferencing, ensuring residual heap inspection of theBufferyields only0x00bytes.- Audit Caveat (JS Runtime Boundary): Because JavaScript
stringvalues passed towithSecret(fn)callbacks are immutable and GC-managed by V8,fill(0)deterministically zeroes theBufferbacking store but cannot forcibly zero transient JSstringcopies inside user callbacks. Callers MUST NOT copy or cache thesecretargument outsidewithSecret.
Section 3: Strict W^X (RW -> RX) In-Memory Linking & O(1) Origin Disposal
Implementation: sections/chromium-build/inmem-linker/b8g_origin_linker.h and sections/chromium-build/inmem-linker/b8g_inmem_linker_bench.cc
3.1 Strict W^X Invariant
For every virtual memory page p owned by a B8gOriginArena and every CPU instruction retirement point i:
No page in B8gOriginArena is ever allocated or protected with PAGE_EXECUTE_READWRITE (RWX).
3.2 RW -> RX Trampoline Emission & 0xCC Slot Guards
In B8gOriginArena::InstallWxTrampoline(uint32_t slot, void* target_fn) (lines 164–192):
- Initial Allocation (
RWonly): Allocates one 4096-byte per-originwx_trampoline_pageviaVirtualAlloc(..., PAGE_READWRITE). - Slot Layout (16-byte stride, up to
B8G_DEP_MAX = 32slots): - Transitions page protection to
PAGE_READWRITE. - Writes the 12-byte x86-64 direct jump stub at
wx_trampoline_page + slot * 16: code[0..1] = 0x48, 0xB8(movabs rax, imm64)code[2..9] = <uint64_t target_fn>code[10..11] = 0xFF, 0xE0(jmp rax)- Writes 4 bytes of
0xCC(INT3software breakpoint trap) intocode[12..15]as hardware-enforced slot guards. Any miscalculated jump offset or linear fallthrough past a stub traps immediately instead of sliding into the next dependency slot. - Finalization (
RXonly + I-Cache Coherence): - Transitions page protection to
PAGE_EXECUTE_READ(RX) viaVirtualProtect. - Calls
FlushInstructionCache(GetCurrentProcess(), code, 16)before returning the callable function pointer. - Data Arena Isolation:
B8gOriginArena::base(B8G_ARENA_V1) is allocated strictlyPAGE_READWRITE(PROT_READ | PROT_WRITEon POSIX) and is never transitioned to executable.
[!NOTE]
Platform & Concurrency Audit Notes:
- Single-Writer Patching: On Windows,InstallWxTrampolinetoggles the per-origin trampoline pageRX -> RW -> RXwhile installing a new lazy slot. Lazy loading on an origin MUST be serialized with respect to calls through previously installed slots on that same origin.
- POSIX Path: On non-Windows builds (#elsebranch, line 190),InstallWxTrampolinecurrently returnsreinterpret_cast<RawFn>(target_fn)directly without allocating a separate trampoline page (preservingW^Xby avoiding any writable code page).
3.3 O(1) Atomic Origin Disposal & Zero-Persistence Proof
Every origin binds its dependency allocations to a per-origin bump allocator (AllocCb / FreeCb no-op, lines 114–138) backed by B8gOriginArena::base. Individual frees are O(1) no-ops; cross-origin heap reuse is structurally impossible because no two origins share a backing region.
On tab/context teardown or cross-origin navigation, B8gOriginArena::Dispose() (lines 234–270):
- Invokes each loaded module's
dep_disposers[i](dep_states[i])and unloads module handles (FreeLibrary/dlclose). - Atomically releases
wx_trampoline_pageviaVirtualFree(wx_trampoline_page, 0, MEM_RELEASE)(Windows). - Atomically unmaps the entire data arena
baseviaVirtualFree(base, 0, MEM_RELEASE)(Windows) ormunmap(base, capacity)(POSIX). - Resets
loaded_mask = 0,loaded_count = 0,capacity = 0,offset = 0.
Empirical Stress Verification: sections/chromium-build/inmem-linker/b8g_inmem_linker_bench.cc (lines 115–129) executes 100 consecutive full origin lifecycles (Init(id, 1 MiB) → 64 KiB arena allocation + memset → O(1) Dispose()), verifying complete reclamation with 0 leaked bytes across all 100 tab lifecycles.
Section 4: Step-Wise B8G-ABI v2 -> v3 Binary Prefix Invariant
4.1 ABI Lineage
B8G Component ABI v2:packages/@awesome-os/b8g/b8g-abi/b8g_abi.h(COMPONENT_API_VERSION 2,component_api_t) andsections/b8g-abi-v8/b8g_abi_v8_section.h(B8G_V8_SECTION_ABI_VERSION 2u).B8G-ABI v3(Origin-Scoped Dependency Contract):sections/chromium-build/inmem-linker/b8g_origin_linker.h(B8G_DEP_ABI_VERSION 3u,B8gDepModuleV3).
4.2 Formal Binary Prefix & struct_size Bounds Contract
To guarantee zero-UB, step-wise ABI evolution across independently compiled C, C++, Zig, and Rust modules without recompiling the entire dependency graph, any versioned ABI extension Sv+1 that extends Sv MUST satisfy two invariants:
- Compile-Time Prefix Identity (
_Static_assert):
Embed the previous ABI version as the first member (base) at byte offset 0, and enforce it at compile time:
```c
typedef struct B8gAbiV3Extended {
B8gAbiV2Base base; // MUST remain at offset 0
uint32_t struct_size; // sizeof(B8gAbiV3Extended) set by producer
uint32_t abi_version; // >= 3u
/ ... v3-only trailing fields ... /
} B8gAbiV3Extended;
_Static_assert(offsetof(B8gAbiV3Extended, base) == 0,
"B8G-ABI v3 must preserve v2 binary prefix at offset 0");
_Static_assert(sizeof(B8gAbiV3Extended) >= sizeof(B8gAbiV2Base),
"B8G-ABI v3 struct cannot shrink below v2 prefix");
```
Because offsetof(S_{v+1}, base) == 0, any pointer const S_{v+1}* is strictly layout-compatible with const S_v* under ISO C11 §6.7.2.1p15 ("a pointer to a structure object, suitably converted, points to its initial member").
- Runtime
struct_sizeBounds Check (Fail-Closed):
Before any consumer dereferences an extended field f introduced in ABI version v+1, it MUST verify:
```c
#define B8G_ABI_HAS_FIELD(ptr, Type, field) \
((ptr) != NULL && \
(ptr)->struct_size >= (offsetof(Type, field) + sizeof(((const Type*)0)->field)))
```
If abi_version is unsupported or B8G_ABI_HAS_FIELD evaluates to false, the loader fails closed (returns NULL / error code) and never reads past the caller-allocated descriptor boundary.
4.3 Current Implementation Status Note
In b8g_origin_linker.h (B8gDepModuleV3, lines 55–63), the origin-scoped dependency descriptor is currently a standalone v3 struct validated by exact version equality (mod->abi_version != B8G_DEP_ABI_VERSION, checked in b8g_18_dep_origin_proof.cc). Any future v3 → v4 extension or v2-bridged descriptor MUST adopt the _Static_assert(offsetof == 0) prefix layout and struct_size bounds check above.
Section 5: Verification Matrix
| Invariant | Verification Artifact | Command | ||
|---|---|---|---|---|
| S3 unit norm, conjugation rollback ($\ | q^(qvq^)q - v\ | < 10^{-12}$), SLERP endpoints | quaternion-security-invariants.test.mts |
.\bin\node.ps1 --test sections/manifold/quaternion-security-invariants.test.mts |
Ephemeral token redaction, fill(0) zero-wipe, provider.revoke(), fail-closed post-dispose |
quaternion-security-invariants.test.mts |
.\bin\node.ps1 --test sections/manifold/quaternion-security-invariants.test.mts |
||
W^X RW -> RX trampoline & 100-tab O(1) VirtualFree (0 leaked bytes) |
b8g_inmem_linker_bench.cc |
Native C++ benchmark target | ||
18-target B8G-ABI v3 version-checked origin linking & disposal |
b8g_18_dep_origin_proof.cc |
Native C++ proof target |