execsurface-model 0.1.0-alpha.5

Raw observation model for AETHER X ExecSurface
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
480
481
482
483
484
485
486
//! Research-only Semantics v3 prototype.
//!
//! This module is intentionally disconnected from public alpha.4 learn/check,
//! baseline and verdict paths. It prototypes proposition-scoped proof semantics
//! before any public schema integration is considered.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::canonical::{CanonicalExecutable, CanonicalNetworkEndpoint, CanonicalPath, OpenIntent};
use crate::{FileOperation, SpawnMechanism};

pub const SEMANTICS_V3_PROTOTYPE_SCHEMA_VERSION: u32 = 3;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FdTableRelationState {
    Shared,
    IndependentCopy,
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "proposition_kind", rename_all = "snake_case")]
pub enum Proposition {
    ProcessChildCreated {
        actor: Option<CanonicalExecutable>,
        mechanism: SpawnMechanism,
    },
    ProcessExecSucceeded {
        from: Option<CanonicalExecutable>,
        executable: CanonicalExecutable,
    },
    CausalExecLineageObserved {
        execution_chain: Vec<CanonicalExecutable>,
    },
    FilePathnameAttemptObserved {
        actor: Option<CanonicalExecutable>,
        execution_chain: Vec<CanonicalExecutable>,
        operation: FileOperation,
        target: CanonicalPath,
        open_intent: Option<OpenIntent>,
    },
    FileOpenObjectObserved {
        actor: Option<CanonicalExecutable>,
        execution_chain: Vec<CanonicalExecutable>,
        target: CanonicalPath,
    },
    FileFdEffectObserved {
        actor: Option<CanonicalExecutable>,
        execution_chain: Vec<CanonicalExecutable>,
        operation: FileOperation,
        target: CanonicalPath,
    },
    FileRenameAttemptObserved {
        actor: Option<CanonicalExecutable>,
        execution_chain: Vec<CanonicalExecutable>,
        from: CanonicalPath,
        to: CanonicalPath,
    },
    NetworkConnectDestinationAttemptObserved {
        actor: Option<CanonicalExecutable>,
        execution_chain: Vec<CanonicalExecutable>,
        endpoint: CanonicalNetworkEndpoint,
    },
    ObserverHealthObserved {
        complete: bool,
        warning_codes: BTreeSet<String>,
    },
    FdTableRelationObserved {
        actor: Option<CanonicalExecutable>,
        mechanism: SpawnMechanism,
        relation: FdTableRelationState,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObservationPoint {
    UserspaceArgumentPreKernel,
    PtraceLifecycleEvent,
    SyscallResultPostOperation,
    DerivedRuntimeFdState,
    KernelSecurityHook,
    KernelTracepoint,
    ImportedAttestedTrace,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IdentityBasis {
    None,
    LexicalArgument,
    TraceTimeDirfdResolvedArgument,
    RuntimeFdPathCorrelated,
    KernelObjectGrounded,
    SocketAddressArgument,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TemporalBinding {
    PreOperationIntent,
    SuccessfulOperationResult,
    PostOperationDerivedState,
    KernelDecisionPoint,
    LifecycleTransition,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CausalBinding {
    DirectEvent,
    StateMachineCorrelated,
    LineageDerived,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct EvidenceGuarantees {
    pub observation_points: BTreeSet<ObservationPoint>,
    pub identity_bases: BTreeSet<IdentityBasis>,
    pub temporal_bindings: BTreeSet<TemporalBinding>,
    pub causal_bindings: BTreeSet<CausalBinding>,
}

impl EvidenceGuarantees {
    pub fn entails(&self, required: &Self) -> bool {
        required
            .observation_points
            .is_subset(&self.observation_points)
            && required.identity_bases.is_subset(&self.identity_bases)
            && required
                .temporal_bindings
                .is_subset(&self.temporal_bindings)
            && required.causal_bindings.is_subset(&self.causal_bindings)
    }

    fn is_empty(&self) -> bool {
        self.observation_points.is_empty()
            && self.identity_bases.is_empty()
            && self.temporal_bindings.is_empty()
            && self.causal_bindings.is_empty()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CompletenessDimension {
    SessionScope,
    Lifecycle,
    Transport,
    ResourceBudget,
    Capability,
    ObjectIdentity,
    FdTableRelation,
    CausalLineage,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum CompletenessState {
    NotRequired,
    Complete,
    Incomplete { reason_code: String },
    Ambiguous { reason_code: String },
    Unsupported { reason_code: String },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ProofRequirement {
    #[serde(default)]
    pub expected_proposition: Option<Proposition>,
    pub guarantees: EvidenceGuarantees,
    pub required_complete: BTreeSet<CompletenessDimension>,
}

impl ProofRequirement {
    pub fn for_proposition(
        proposition: Proposition,
        guarantees: EvidenceGuarantees,
        required_complete: BTreeSet<CompletenessDimension>,
    ) -> Self {
        Self {
            expected_proposition: Some(proposition),
            guarantees,
            required_complete,
        }
    }

    fn is_non_vacuous(&self) -> bool {
        self.expected_proposition.is_some()
            && (!self.guarantees.is_empty() || !self.required_complete.is_empty())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BackendSemanticProfile {
    pub name: String,
    pub semantic_profile_version: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProofCarryingObservation {
    pub schema_version: u32,
    pub proposition: Proposition,
    pub guarantees: EvidenceGuarantees,
    pub completeness: BTreeMap<CompletenessDimension, CompletenessState>,
    pub backend_profile: BackendSemanticProfile,
    pub ambiguity_codes: BTreeSet<String>,
}

impl ProofCarryingObservation {
    pub fn new(
        proposition: Proposition,
        guarantees: EvidenceGuarantees,
        backend_profile: BackendSemanticProfile,
    ) -> Self {
        Self {
            schema_version: SEMANTICS_V3_PROTOTYPE_SCHEMA_VERSION,
            proposition,
            guarantees,
            completeness: BTreeMap::new(),
            backend_profile,
            ambiguity_codes: BTreeSet::new(),
        }
    }

    fn ambiguity_invalidates(&self, requirement: &ProofRequirement) -> bool {
        for code in &self.ambiguity_codes {
            match code.as_str() {
                "object_identity_conflict" => {
                    if requirement
                        .required_complete
                        .contains(&CompletenessDimension::ObjectIdentity)
                    {
                        return true;
                    }
                }
                _ => {
                    // Unknown ambiguity semantics must never silently increase authority.
                    return true;
                }
            }
        }
        false
    }

    pub fn satisfies(&self, requirement: &ProofRequirement) -> bool {
        if self.schema_version != SEMANTICS_V3_PROTOTYPE_SCHEMA_VERSION {
            return false;
        }
        if !requirement.is_non_vacuous() {
            return false;
        }
        if requirement.expected_proposition.as_ref() != Some(&self.proposition) {
            return false;
        }
        if !self.guarantees.entails(&requirement.guarantees) {
            return false;
        }
        if self.ambiguity_invalidates(requirement) {
            return false;
        }
        requirement
            .required_complete
            .iter()
            .all(|dimension| self.completeness.get(dimension) == Some(&CompletenessState::Complete))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canonical::{PathClass, PathResolution};

    fn target_path() -> CanonicalPath {
        CanonicalPath {
            value: "$WORKSPACE/input.txt".to_owned(),
            class: PathClass::Workspace,
            resolution: PathResolution::Lexical,
        }
    }

    fn pathname_attempt() -> Proposition {
        Proposition::FilePathnameAttemptObserved {
            actor: None,
            execution_chain: vec![],
            operation: FileOperation::Open,
            target: target_path(),
            open_intent: None,
        }
    }

    fn ptrace_argument_guarantees() -> EvidenceGuarantees {
        EvidenceGuarantees {
            observation_points: BTreeSet::from([ObservationPoint::UserspaceArgumentPreKernel]),
            identity_bases: BTreeSet::from([IdentityBasis::LexicalArgument]),
            temporal_bindings: BTreeSet::from([TemporalBinding::PreOperationIntent]),
            causal_bindings: BTreeSet::from([CausalBinding::DirectEvent]),
        }
    }

    fn requirement_for(
        guarantees: EvidenceGuarantees,
        required_complete: BTreeSet<CompletenessDimension>,
    ) -> ProofRequirement {
        ProofRequirement::for_proposition(pathname_attempt(), guarantees, required_complete)
    }

    fn base_record() -> ProofCarryingObservation {
        let mut record = ProofCarryingObservation::new(
            pathname_attempt(),
            ptrace_argument_guarantees(),
            BackendSemanticProfile {
                name: "linux-ptrace-semantics-v3-prototype".to_owned(),
                semantic_profile_version: 1,
            },
        );
        record.completeness.insert(
            CompletenessDimension::SessionScope,
            CompletenessState::Complete,
        );
        record.completeness.insert(
            CompletenessDimension::Lifecycle,
            CompletenessState::Complete,
        );
        record
    }

    #[test]
    fn weak_path_argument_does_not_entail_kernel_object_grounding() {
        let record = base_record();
        let requirement = requirement_for(
            EvidenceGuarantees {
                identity_bases: BTreeSet::from([IdentityBasis::KernelObjectGrounded]),
                ..EvidenceGuarantees::default()
            },
            BTreeSet::new(),
        );

        assert!(!record.satisfies(&requirement));
    }

    #[test]
    fn ambiguity_blocks_required_completeness() {
        let mut record = base_record();
        record.completeness.insert(
            CompletenessDimension::ObjectIdentity,
            CompletenessState::Ambiguous {
                reason_code: "shared_fd_table_ambiguity".to_owned(),
            },
        );
        let requirement = requirement_for(
            ptrace_argument_guarantees(),
            BTreeSet::from([
                CompletenessDimension::SessionScope,
                CompletenessDimension::ObjectIdentity,
            ]),
        );

        assert!(!record.satisfies(&requirement));
    }

    #[test]
    fn exact_required_guarantees_and_completeness_are_admissible() {
        let record = base_record();
        let requirement = requirement_for(
            ptrace_argument_guarantees(),
            BTreeSet::from([
                CompletenessDimension::SessionScope,
                CompletenessDimension::Lifecycle,
            ]),
        );

        assert!(record.satisfies(&requirement));
    }

    #[test]
    fn default_requirement_fails_closed() {
        let record = base_record();
        assert!(!record.satisfies(&ProofRequirement::default()));
    }

    #[test]
    fn proposition_mismatch_fails_closed() {
        let record = base_record();
        let mut wrong = pathname_attempt();
        if let Proposition::FilePathnameAttemptObserved { target, .. } = &mut wrong {
            target.value = "$WORKSPACE/other.txt".to_owned();
        }
        let requirement = ProofRequirement::for_proposition(
            wrong,
            ptrace_argument_guarantees(),
            BTreeSet::from([CompletenessDimension::SessionScope]),
        );
        assert!(!record.satisfies(&requirement));
    }

    #[test]
    fn explicit_object_identity_conflict_blocks_complete_claim() {
        let mut record = base_record();
        record.completeness.insert(
            CompletenessDimension::ObjectIdentity,
            CompletenessState::Complete,
        );
        record
            .ambiguity_codes
            .insert("object_identity_conflict".to_owned());
        let requirement = requirement_for(
            ptrace_argument_guarantees(),
            BTreeSet::from([CompletenessDimension::ObjectIdentity]),
        );
        assert!(!record.satisfies(&requirement));
    }

    #[test]
    fn unknown_ambiguity_code_fails_closed_for_admission() {
        let mut record = base_record();
        record
            .ambiguity_codes
            .insert("unrecognized_future_ambiguity".to_owned());
        let requirement = requirement_for(
            ptrace_argument_guarantees(),
            BTreeSet::from([CompletenessDimension::SessionScope]),
        );
        assert!(!record.satisfies(&requirement));
    }

    #[test]
    fn deterministic_serialization_is_independent_of_set_insertion_order() {
        let mut first = base_record();
        first.ambiguity_codes.insert("zeta".to_owned());
        first.ambiguity_codes.insert("alpha".to_owned());

        let mut second = base_record();
        second.ambiguity_codes.insert("alpha".to_owned());
        second.ambiguity_codes.insert("zeta".to_owned());

        assert_eq!(
            serde_json::to_vec(&first).expect("serialize first"),
            serde_json::to_vec(&second).expect("serialize second")
        );
    }

    #[test]
    fn same_canonical_value_with_different_authority_is_not_equal_evidence() {
        let first = base_record();
        let mut second = base_record();
        second
            .guarantees
            .identity_bases
            .insert(IdentityBasis::KernelObjectGrounded);

        assert_ne!(first, second);
    }

    #[test]
    fn fd_table_relation_unknown_is_first_class_proposition_state() {
        let proposition = Proposition::FdTableRelationObserved {
            actor: None,
            mechanism: SpawnMechanism::Clone,
            relation: FdTableRelationState::Unknown,
        };
        let encoded = serde_json::to_vec(&proposition).expect("serialize fd-table relation");
        let decoded: Proposition =
            serde_json::from_slice(&encoded).expect("deserialize fd-table relation");
        assert_eq!(decoded, proposition);
    }

    #[test]
    fn observer_health_warning_codes_serialize_deterministically() {
        let first = Proposition::ObserverHealthObserved {
            complete: false,
            warning_codes: BTreeSet::from(["zeta".to_owned(), "alpha".to_owned()]),
        };
        let second = Proposition::ObserverHealthObserved {
            complete: false,
            warning_codes: BTreeSet::from(["alpha".to_owned(), "zeta".to_owned()]),
        };
        assert_eq!(
            serde_json::to_vec(&first).expect("serialize first health proposition"),
            serde_json::to_vec(&second).expect("serialize second health proposition")
        );
    }
}