aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / crumbs / src / audit.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Leak audits: the vocabulary that must never reach a learner, and scanners |
| 17 | //! for byte streams and for a live learner process's memory. |
| 18 | |
| 19 | use crate::digest::Digest; |
| 20 | use crate::gen::{registry, Mechanism}; |
| 21 | use crate::sealed::{AdversarialClass, Population, Rung, SealedCrumb, SourceFamily}; |
| 22 | |
| 23 | /// Every human-readable label the sealed side knows (>= 5 chars, so short |
| 24 | /// coincidences in binary data are not mistaken for leaks). |
| 25 | pub fn label_vocabulary() -> Vec<String> { |
| 26 | let mut v: Vec<String> = Vec::new(); |
| 27 | for f in ®istry().families { |
| 28 | v.push(f.key.clone()); |
| 29 | v.push(f.topic.to_string()); |
| 30 | } |
| 31 | for m in Mechanism::ALL { |
| 32 | v.push(m.key().to_string()); |
| 33 | } |
| 34 | for r in Rung::ALL { |
| 35 | v.push(format!("{r:?}")); |
| 36 | } |
| 37 | for p in Population::ALL { |
| 38 | v.push(format!("{p:?}")); |
| 39 | } |
| 40 | for s in [ |
| 41 | SourceFamily::Capability, |
| 42 | SourceFamily::Composition, |
| 43 | SourceFamily::Ambiguous, |
| 44 | SourceFamily::RabbitHole, |
| 45 | SourceFamily::Frontier, |
| 46 | SourceFamily::CleanRoomPhysics, |
| 47 | ] { |
| 48 | v.push(format!("{s:?}")); |
| 49 | } |
| 50 | for c in [ |
| 51 | AdversarialClass::SimpleWrongRule, |
| 52 | AdversarialClass::MultiFit, |
| 53 | AdversarialClass::SpuriousDimension, |
| 54 | AdversarialClass::PrefixDiverges, |
| 55 | AdversarialClass::ElegantArbitrary, |
| 56 | AdversarialClass::InsufficientEvidence, |
| 57 | AdversarialClass::Noisy, |
| 58 | ] { |
| 59 | v.push(format!("{c:?}")); |
| 60 | } |
| 61 | for w in [ |
| 62 | "SyntheticNovel", |
| 63 | "AdversarialSynthetic", |
| 64 | "HumanTheoryDerived", |
| 65 | "FrontierDerived", |
| 66 | "RealWorldMeasured", |
| 67 | "decoy", |
| 68 | "rabbit", |
| 69 | "heldout", |
| 70 | "held-out", |
| 71 | "difficulty", |
| 72 | "prime", |
| 73 | "gravity", |
| 74 | "conjecture", |
| 75 | "algebra", |
| 76 | "velocity", |
| 77 | "energy", |
| 78 | "force", |
| 79 | "charge", |
| 80 | "crumbs-registry", |
| 81 | ] { |
| 82 | v.push(w.to_string()); |
| 83 | } |
| 84 | v.retain(|s| s.len() >= 5); |
| 85 | v.sort(); |
| 86 | v.dedup(); |
| 87 | v |
| 88 | } |
| 89 | |
| 90 | /// Labels that exist only in the sealed registry (family keys, mechanism keys, |
| 91 | /// topic phrases). Generic English words are excluded, so this list can be |
| 92 | /// used to scan a learner binary's own code and read-only data. |
| 93 | pub fn sealed_specific_labels() -> Vec<String> { |
| 94 | let mut v: Vec<String> = Vec::new(); |
| 95 | for f in ®istry().families { |
| 96 | v.push(f.key.clone()); |
| 97 | v.push(f.topic.to_string()); |
| 98 | } |
| 99 | for m in Mechanism::ALL { |
| 100 | v.push(m.key().to_string()); |
| 101 | } |
| 102 | v.push("crumbs-registry".into()); |
| 103 | v.retain(|s| s.len() >= 6); |
| 104 | v.sort(); |
| 105 | v.dedup(); |
| 106 | v |
| 107 | } |
| 108 | |
| 109 | pub fn contains(hay: &[u8], needle: &[u8]) -> bool { |
| 110 | !needle.is_empty() && hay.windows(needle.len()).any(|w| w == needle) |
| 111 | } |
| 112 | |
| 113 | /// Labels found in `bytes` (case-sensitive and lowercase variants). |
| 114 | pub fn find_labels(bytes: &[u8]) -> Vec<String> { |
| 115 | let mut hits = Vec::new(); |
| 116 | for l in label_vocabulary() { |
| 117 | if contains(bytes, l.as_bytes()) || contains(bytes, l.to_lowercase().as_bytes()) { |
| 118 | hits.push(l); |
| 119 | } |
| 120 | } |
| 121 | hits |
| 122 | } |
| 123 | |
| 124 | /// Sealed secrets of one crumb that could identify a leak: its digests and |
| 125 | /// every held-out output (and input) value that does not also occur visibly. |
| 126 | pub fn sealed_needles( |
| 127 | sealed: &SealedCrumb, |
| 128 | visible_values: &[(Vec<u64>, Vec<u64>)], |
| 129 | min_value: u64, |
| 130 | ) -> Vec<Vec<u8>> { |
| 131 | let mut vis: Vec<u64> = Vec::new(); |
| 132 | for (i, o) in visible_values { |
| 133 | vis.extend(i); |
| 134 | vis.extend(o); |
| 135 | } |
| 136 | let mut out: Vec<Vec<u8>> = Vec::new(); |
| 137 | for d in [ |
| 138 | sealed.digest(), |
| 139 | sealed.generator_instance_digest, |
| 140 | sealed.heldouts.digest(), |
| 141 | sealed.generator_code_digest, |
| 142 | ] { |
| 143 | out.push(d.0.to_vec()); |
| 144 | out.push(d.hex().into_bytes()); |
| 145 | } |
| 146 | for t in crate::sealed::HeldoutTier::ALL { |
| 147 | for ex in sealed.heldouts.tier(t) { |
| 148 | for v in ex.output.iter().chain(ex.input.iter()) { |
| 149 | if *v >= min_value && !vis.contains(v) { |
| 150 | // Only distinctive encodings count as evidence of a leak: a |
| 151 | // mostly-zero u64 matches ordinary length fields by chance. |
| 152 | let le = v.to_le_bytes(); |
| 153 | if le.iter().filter(|b| **b != 0).count() >= 5 { |
| 154 | out.push(le.to_vec()); |
| 155 | } |
| 156 | let dec = v.to_string(); |
| 157 | if dec.len() >= 6 { |
| 158 | out.push(dec.into_bytes()); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | out |
| 165 | } |
| 166 | |
| 167 | pub fn find_needles(bytes: &[u8], needles: &[Vec<u8>]) -> usize { |
| 168 | needles.iter().filter(|n| contains(bytes, n)).count() |
| 169 | } |
| 170 | |
| 171 | /// Read every writable mapping of a live process (heap, stack, anonymous and |
| 172 | /// data segments). Requires permission to trace the process (a child of ours). |
| 173 | pub fn read_process_writable_memory(pid: u32) -> std::io::Result<Vec<u8>> { |
| 174 | use std::io::{Read, Seek, SeekFrom}; |
| 175 | let maps = std::fs::read_to_string(format!("/proc/{pid}/maps"))?; |
| 176 | let mut mem = std::fs::File::open(format!("/proc/{pid}/mem"))?; |
| 177 | let mut out = Vec::new(); |
| 178 | for line in maps.lines() { |
| 179 | let mut parts = line.split_whitespace(); |
| 180 | let (Some(range), Some(perms)) = (parts.next(), parts.next()) else { |
| 181 | continue; |
| 182 | }; |
| 183 | if !perms.starts_with("rw") { |
| 184 | continue; |
| 185 | } |
| 186 | let name = line.split_whitespace().nth(5).unwrap_or(""); |
| 187 | if name.starts_with("[vvar") || name.starts_with("[vsyscall") { |
| 188 | continue; |
| 189 | } |
| 190 | let Some((a, b)) = range.split_once('-') else { |
| 191 | continue; |
| 192 | }; |
| 193 | let (a, b) = ( |
| 194 | u64::from_str_radix(a, 16).unwrap_or(0), |
| 195 | u64::from_str_radix(b, 16).unwrap_or(0), |
| 196 | ); |
| 197 | if b <= a || b - a > (512 << 20) { |
| 198 | continue; |
| 199 | } |
| 200 | let mut buf = vec![0u8; (b - a) as usize]; |
| 201 | if mem.seek(SeekFrom::Start(a)).is_ok() && mem.read_exact(&mut buf).is_ok() { |
| 202 | out.extend_from_slice(&buf); |
| 203 | } |
| 204 | } |
| 205 | Ok(out) |
| 206 | } |
| 207 | |
| 208 | pub fn digest_needle(d: &Digest) -> Vec<Vec<u8>> { |
| 209 | vec![d.0.to_vec(), d.hex().into_bytes()] |
| 210 | } |