fic/tast/infoflow.rs
1//! Information flow over `tast`, on the two-level lattice `Low ≤ High`: a
2//! mapping's label must be at least its memory region's, so a `High` region
3//! is never read through a `Low` mapping.
4//!
5//! ```text
6//! // .fis
7//! High MemoryRegion secret(0x1000);
8//! Low MemoryRegion public(0x1000);
9//!
10//! // .fi
11//! High Mapping a(secret, "r"); // OK
12//! High Mapping b(public, "rw"); // OK: upward
13//! Low Mapping c(secret, "r"); // ERROR: High region through a Low mapping
14//! ```
15
16use crate::error::{Diagnostic, Diagnostics};
17use crate::tast::TyEnv;
18
19/// Check that no `High` memory region is mapped at `Low`.
20///
21/// # Errors
22///
23/// One diagnostic per such mapping, at the mapping.
24///
25/// # Examples
26///
27/// ```
28/// use fic::ast::RawAst;
29/// use fic::foreign::ForeignModules;
30/// use fic::parser::{parse_program, parse_system};
31/// use fic::tast::infoflow::infoflow;
32///
33/// // The check reads the typed tree; the declarations alone type it.
34/// let flows = |label: &str| {
35/// let system = parse_system("High MemoryRegion secret(0x1000); ProtectionDomain pd1(1);").unwrap();
36/// let program = parse_program(&format!(r#"{label} Mapping m(secret, "rw");"#)).unwrap();
37/// // One program per protection domain, by position.
38/// let rawast = RawAst { system, programs: vec![program] };
39/// let (env, tenv) = fic::frontend::data_layer(&rawast).unwrap();
40/// let (rast, _) = fic::rast::build(&rawast, env, &mut ForeignModules::new()).unwrap();
41/// let (tast, _) = fic::tast::build(rast, tenv).unwrap();
42/// infoflow(&tast.tenv)
43/// };
44/// assert!(flows("High").is_ok());
45/// assert!(flows("Low").is_err());
46/// ```
47pub fn infoflow(tenv: &TyEnv) -> Result<(), Diagnostics> {
48 let mut errors = Vec::new();
49 // A channel buffer's mapping takes its region's label, so only a
50 // program's own mapping can fail, reported in its `.fi`.
51 for mapping in tenv.mappings.iter() {
52 let mr = &tenv.mrs[mapping.region];
53 if !mr.security_label.flows_to(&mapping.label) {
54 errors.push(Diagnostic::at(
55 format!(
56 "information flow violation: {} memory region '{}' is accessed through {} mapping '{}'",
57 mr.security_label, mr.name, mapping.label, mapping.name
58 ),
59 mapping.span.clone(),
60 &tenv.pds[mapping.pd].fi,
61 ));
62 }
63 }
64 Diagnostics::finish(errors, ())
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use crate::tast::env::built;
71
72 /// The flows of one program `pd1` over the regions in `fis`.
73 fn run(fis: &str, fi: &str) -> Result<(), Diagnostics> {
74 let fis = format!("{fis}\nProtectionDomain pd1(1);");
75 let (tast, _) = built(&fis, &[("pd1", fi)]).expect("builds");
76 infoflow(&tast.tenv)
77 }
78
79 #[test]
80 fn high_mr_low_mapping_is_error() {
81 let errs = run(
82 "High MemoryRegion secret(0x1000);",
83 r#"Low Mapping m(secret, "rw");"#,
84 )
85 .unwrap_err();
86 assert_eq!(errs.0.len(), 1);
87 assert!(errs.0[0].to_string().contains("information flow violation"));
88 }
89
90 #[test]
91 fn upward_matching_and_default_labels_are_ok() {
92 for (fis, fi) in [
93 (
94 "Low MemoryRegion buf(0x1000);",
95 r#"High Mapping m(buf, "rw");"#,
96 ),
97 (
98 "High MemoryRegion secret(0x1000);\nLow MemoryRegion public(0x1000);",
99 "High Mapping m1(secret, \"rw\");\nLow Mapping m2(public, \"rw\");",
100 ),
101 ("MemoryRegion buf(0x1000);", r#"Mapping m(buf, "rw");"#),
102 ] {
103 assert!(run(fis, fi).is_ok(), "{fi}");
104 }
105 }
106
107 #[test]
108 fn no_programs_no_errors() {
109 let (tast, _) = built("High MemoryRegion secret(0x1000);", &[]).expect("builds");
110 assert!(infoflow(&tast.tenv).is_ok());
111 }
112}