fic/ast/infoflow.rs
1//! Information flow security labels.
2//!
3//! Fiducia uses a two-level lattice (`Low` / `High`) to track information flow
4//! between memory regions and mappings. Data may flow upward (Low -> High) but
5//! never downward (High -> Low), preventing information leaks.
6//!
7//! ## Security Labels
8//!
9//! Both memory regions (`.fis`) and mappings (`.fi`) carry an optional security label:
10//!
11//! | Label | Meaning | Default |
12//! |--------|---------------------|---------|
13//! | `Low` | Public / unclassified | Yes |
14//! | `High` | Confidential | No |
15//!
16//! If the label is omitted, `Low` is assumed.
17//!
18//! ```text
19//! High MemoryRegion secret(0x1000); // confidential
20//! MemoryRegion public(0x1000); // Low by default
21//! ```
22//!
23//! Information-flow checking is at an initial stage, still under research:
24//! today `fic` checks only that a mapping's label is at least its region's.
25//! Language reference: [Information Flow](https://fiducia-lang.github.io/docs/lang-spec/checks/information-flow.html).
26
27use serde::{Deserialize, Serialize};
28use std::fmt;
29
30/// A two-level security label for information flow control.
31///
32/// `Low` data can flow anywhere; `High` data must stay at `High`.
33///
34/// ```
35/// use fic::ast::SecurityLabel;
36///
37/// assert!(SecurityLabel::Low.flows_to(&SecurityLabel::High));
38/// assert!(!SecurityLabel::High.flows_to(&SecurityLabel::Low));
39/// ```
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
41pub enum SecurityLabel {
42 /// Low-security (public) data.
43 #[default]
44 Low,
45 /// High-security (confidential) data.
46 High,
47}
48
49impl SecurityLabel {
50 /// Check if information can flow from `self` to `target`.
51 ///
52 /// - Low -> Low: OK
53 /// - Low -> High: OK (upward flow is safe)
54 /// - High -> High: OK
55 /// - High -> Low: **rejected** (information leak)
56 pub fn flows_to(&self, target: &SecurityLabel) -> bool {
57 match (self, target) {
58 (SecurityLabel::Low, _) | (SecurityLabel::High, SecurityLabel::High) => true,
59 (SecurityLabel::High, SecurityLabel::Low) => false,
60 }
61 }
62}
63
64impl fmt::Display for SecurityLabel {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 SecurityLabel::Low => write!(f, "Low"),
68 SecurityLabel::High => write!(f, "High"),
69 }
70 }
71}