Skip to main content

nucleus/security/
landlock.rs

1use crate::error::{NucleusError, Result};
2use landlock::{
3    Access, AccessFs, PathBeneath, PathFd, Ruleset, RulesetAttr, RulesetCreatedAttr, RulesetError,
4    RulesetStatus, ABI,
5};
6use tracing::{debug, info, warn};
7
8/// Target ABI – covers up to Linux 6.12 features (Truncate, IoctlDev, Refer, etc.).
9/// The landlock crate gracefully degrades for older kernels.
10const TARGET_ABI: ABI = ABI::V5;
11
12/// Minimum Landlock ABI version required for production mode.
13///
14/// V3 adds LANDLOCK_ACCESS_FS_TRUNCATE which prevents silent data truncation
15/// that V1/V2 cannot control. This is the minimum we consider safe for
16/// production workloads.
17const MINIMUM_PRODUCTION_ABI: ABI = ABI::V3;
18
19/// Landlock filesystem access-control manager
20///
21/// Implements fine-grained, path-based filesystem restrictions as an additional
22/// defense layer on top of namespaces, capabilities, and seccomp.
23///
24/// Properties (matching Nucleus security invariants):
25/// - Irreversible: once restrict_self() is called, restrictions cannot be lifted
26/// - Stackable: layered with seccomp and capability dropping
27/// - Unprivileged: works in rootless mode
28pub struct LandlockManager {
29    applied: bool,
30}
31
32impl LandlockManager {
33    pub fn new() -> Self {
34        Self { applied: false }
35    }
36
37    /// Apply the container Landlock policy.
38    ///
39    /// Rules:
40    /// - `/` (root):         read-only traversal (ReadDir) so path resolution works
41    /// - `/bin`, `/usr`:     read + execute (for running agent binaries)
42    /// - `/lib`, `/lib64`:   read (shared libraries)
43    /// - `/etc`:             read (config / resolv.conf / nsswitch)
44    /// - `/dev`:             read (already minimal device nodes)
45    /// - `/proc`:            read (already mounted read-only)
46    /// - `/tmp`:             read + write + create + remove (agent scratch space)
47    /// - `/context`:         read-only (pre-populated agent data)
48    ///
49    /// Everything else is denied by the ruleset.
50    pub fn apply_container_policy(&mut self) -> Result<bool> {
51        self.apply_container_policy_with_mode(false)
52    }
53
54    /// Assert that the kernel supports at least the minimum Landlock ABI version
55    /// required for production workloads.
56    ///
57    /// Returns Ok(()) if the ABI is sufficient, or Err if the kernel is too old.
58    /// In best-effort mode, a too-old kernel is logged but not fatal.
59    pub fn assert_minimum_abi(&self, production_mode: bool) -> Result<()> {
60        // Probe the kernel's Landlock ABI version by attempting to create a ruleset
61        // with the minimum ABI's access rights. If the kernel doesn't support the
62        // minimum ABI, the ruleset will be NotEnforced or PartiallyEnforced.
63        let min_access = AccessFs::from_all(MINIMUM_PRODUCTION_ABI);
64        let target_access = AccessFs::from_all(TARGET_ABI);
65
66        // If the minimum access set equals the target, the kernel supports everything
67        // If the minimum is a subset, check that at least the minimum rights are present
68        if min_access != target_access {
69            info!(
70                "Landlock ABI: target={:?}, minimum_production={:?}",
71                TARGET_ABI, MINIMUM_PRODUCTION_ABI
72            );
73        }
74
75        // The actual enforcement check happens in build_and_restrict().
76        // Here we do a lightweight check: if the kernel supports the target ABI,
77        // it certainly supports the minimum. The landlock crate handles this
78        // gracefully, but we want an explicit assertion for production.
79        match Ruleset::default().handle_access(AccessFs::from_all(MINIMUM_PRODUCTION_ABI)) {
80            Ok(_) => {
81                info!("Landlock ABI >= V3 confirmed");
82                Ok(())
83            }
84            Err(e) => {
85                let msg = format!(
86                    "Kernel Landlock ABI is below minimum required version (V3): {}",
87                    e
88                );
89                if production_mode {
90                    Err(ll_err(e))
91                } else {
92                    warn!("{}", msg);
93                    Ok(())
94                }
95            }
96        }
97    }
98
99    /// Apply with configurable failure behavior.
100    ///
101    /// When `best_effort` is true, failures (e.g. kernel without Landlock) are
102    /// logged and execution continues.
103    pub fn apply_container_policy_with_mode(&mut self, best_effort: bool) -> Result<bool> {
104        if self.applied {
105            debug!("Landlock policy already applied, skipping");
106            return Ok(true);
107        }
108
109        info!("Applying Landlock filesystem policy");
110
111        match self.build_and_restrict() {
112            Ok(status) => match status {
113                RulesetStatus::FullyEnforced => {
114                    self.applied = true;
115                    info!("Landlock policy fully enforced");
116                    Ok(true)
117                }
118                RulesetStatus::PartiallyEnforced => {
119                    self.applied = true;
120                    info!("Landlock policy partially enforced (kernel lacks some access rights)");
121                    Ok(true)
122                }
123                RulesetStatus::NotEnforced => {
124                    if best_effort {
125                        warn!("Landlock not enforced (kernel does not support Landlock)");
126                        Ok(false)
127                    } else {
128                        Err(NucleusError::LandlockError(
129                            "Landlock not enforced (kernel does not support Landlock)".to_string(),
130                        ))
131                    }
132                }
133            },
134            Err(e) => {
135                if best_effort {
136                    warn!(
137                        "Failed to apply Landlock policy: {} (continuing without Landlock)",
138                        e
139                    );
140                    Ok(false)
141                } else {
142                    Err(e)
143                }
144            }
145        }
146    }
147
148    /// Build the ruleset and call restrict_self().
149    fn build_and_restrict(&self) -> Result<RulesetStatus> {
150        let access_all = AccessFs::from_all(TARGET_ABI);
151        let access_read = AccessFs::from_read(TARGET_ABI);
152
153        // Read + execute for binary paths
154        let access_read_exec = access_read | AccessFs::Execute;
155
156        // Write access set for /tmp — full read+write but no execute.
157        // Executing from /tmp is a common attack pattern (drop-and-exec).
158        let mut access_tmp = access_all;
159        access_tmp.remove(AccessFs::Execute);
160
161        let mut ruleset = Ruleset::default()
162            .handle_access(access_all)
163            .map_err(ll_err)?
164            .create()
165            .map_err(ll_err)?;
166
167        // Root directory: minimal traversal only
168        // We add ReadDir so that path resolution through / works
169        if let Ok(fd) = PathFd::new("/") {
170            ruleset = ruleset
171                .add_rule(PathBeneath::new(fd, AccessFs::ReadDir))
172                .map_err(ll_err)?;
173        }
174
175        // M13: Mandatory paths that must exist for a functional container.
176        // Warn (or error in strict mode) when these are missing.
177        const MANDATORY_PATHS: &[&str] = &["/bin", "/usr", "/lib", "/etc"];
178        for path in MANDATORY_PATHS {
179            if !std::path::Path::new(path).exists() {
180                warn!(
181                    "Landlock: mandatory path {} does not exist; container may not function correctly",
182                    path
183                );
184            }
185        }
186
187        // Binary paths: read + execute
188        for path in &["/bin", "/usr", "/sbin"] {
189            if let Ok(fd) = PathFd::new(path) {
190                ruleset = ruleset
191                    .add_rule(PathBeneath::new(fd, access_read_exec))
192                    .map_err(ll_err)?;
193            }
194        }
195
196        // Shared libraries: read
197        for path in &["/lib", "/lib64", "/lib32"] {
198            if let Ok(fd) = PathFd::new(path) {
199                ruleset = ruleset
200                    .add_rule(PathBeneath::new(fd, access_read))
201                    .map_err(ll_err)?;
202            }
203        }
204
205        // Config/device/proc: read
206        for path in &["/etc", "/dev", "/proc"] {
207            if let Ok(fd) = PathFd::new(path) {
208                ruleset = ruleset
209                    .add_rule(PathBeneath::new(fd, access_read))
210                    .map_err(ll_err)?;
211            }
212        }
213
214        // /tmp: full read+write+create+remove
215        if let Ok(fd) = PathFd::new("/tmp") {
216            ruleset = ruleset
217                .add_rule(PathBeneath::new(fd, access_tmp))
218                .map_err(ll_err)?;
219        }
220
221        // /nix/store: read + execute (NixOS binaries and libraries)
222        if let Ok(fd) = PathFd::new("/nix/store") {
223            ruleset = ruleset
224                .add_rule(PathBeneath::new(fd, access_read_exec))
225                .map_err(ll_err)?;
226        }
227
228        // /run/secrets: read-only (container secrets mounted on tmpfs)
229        if let Ok(fd) = PathFd::new("/run/secrets") {
230            ruleset = ruleset
231                .add_rule(PathBeneath::new(fd, access_read))
232                .map_err(ll_err)?;
233        }
234
235        // /context: read-only (agent data)
236        if let Ok(fd) = PathFd::new("/context") {
237            ruleset = ruleset
238                .add_rule(PathBeneath::new(fd, access_read))
239                .map_err(ll_err)?;
240        }
241
242        let status = ruleset.restrict_self().map_err(ll_err)?;
243        Ok(status.ruleset)
244    }
245
246    /// Check if Landlock policy has been applied
247    pub fn is_applied(&self) -> bool {
248        self.applied
249    }
250}
251
252impl Default for LandlockManager {
253    fn default() -> Self {
254        Self::new()
255    }
256}
257
258/// Convert a landlock RulesetError into NucleusError::LandlockError
259fn ll_err(e: RulesetError) -> NucleusError {
260    NucleusError::LandlockError(e.to_string())
261}
262
263#[cfg(test)]
264mod tests {
265    use super::*;
266
267    #[test]
268    fn test_landlock_manager_initial_state() {
269        let mgr = LandlockManager::new();
270        assert!(!mgr.is_applied());
271    }
272
273    #[test]
274    fn test_apply_idempotent() {
275        let mut mgr = LandlockManager::new();
276        // Best-effort so it succeeds even without Landlock support
277        let _ = mgr.apply_container_policy_with_mode(true);
278        // Second call should be a no-op
279        let result = mgr.apply_container_policy_with_mode(true);
280        assert!(result.is_ok());
281    }
282
283    #[test]
284    fn test_best_effort_on_unsupported_kernel() {
285        let mut mgr = LandlockManager::new();
286        // Should not error even if kernel has no Landlock
287        let result = mgr.apply_container_policy_with_mode(true);
288        assert!(result.is_ok());
289    }
290
291    /// Extract the body of a function from source text by brace-matching,
292    /// avoiding fragile hardcoded character-window offsets (SEC-MED-03).
293    fn extract_fn_body<'a>(source: &'a str, fn_signature: &str) -> &'a str {
294        let fn_start = source
295            .find(fn_signature)
296            .unwrap_or_else(|| panic!("function '{}' not found in source", fn_signature));
297        let after = &source[fn_start..];
298        let open = after
299            .find('{')
300            .unwrap_or_else(|| panic!("no opening brace found for '{}'", fn_signature));
301        let mut depth = 0u32;
302        let mut end = open;
303        for (i, ch) in after[open..].char_indices() {
304            match ch {
305                '{' => depth += 1,
306                '}' => {
307                    depth -= 1;
308                    if depth == 0 {
309                        end = open + i + 1;
310                        break;
311                    }
312                }
313                _ => {}
314            }
315        }
316        &after[..end]
317    }
318
319    #[test]
320    fn test_policy_covers_nix_store_and_secrets() {
321        // Landlock policy must include rules for /nix/store (read+exec) and
322        // /run/secrets (read) so NixOS binaries can execute and secrets are readable.
323        // NOTE: The Landlock API does not expose the ruleset for inspection, so
324        // this remains a source-text check — but uses brace-matched function
325        // body extraction instead of hardcoded char offsets.
326        let source = include_str!("landlock.rs");
327        let fn_body = extract_fn_body(source, "fn build_and_restrict");
328        assert!(
329            fn_body.contains("\"/nix/store\"") || fn_body.contains("\"/nix\""),
330            "Landlock build_and_restrict must include a rule for /nix/store or /nix"
331        );
332        assert!(
333            fn_body.contains("\"/run/secrets\"") || fn_body.contains("\"/run\""),
334            "Landlock build_and_restrict must include a rule for /run/secrets"
335        );
336    }
337
338    #[test]
339    fn test_tmp_access_excludes_execute() {
340        // L-5: /tmp should have read+write but NOT execute permission.
341        // Verify at the type level that our access_tmp definition
342        // does not include Execute.
343        let access_all = AccessFs::from_all(TARGET_ABI);
344        let mut access_tmp = access_all;
345        access_tmp.remove(AccessFs::Execute);
346        assert!(!access_tmp.contains(AccessFs::Execute));
347        // But it should still have write capabilities
348        assert!(access_tmp.contains(AccessFs::WriteFile));
349        assert!(access_tmp.contains(AccessFs::RemoveFile));
350    }
351
352    #[test]
353    fn test_not_enforced_returns_error_in_strict_mode() {
354        // SEC-11: When best_effort=false, NotEnforced must return Err, not Ok(false)
355        let source = include_str!("landlock.rs");
356        let fn_body = extract_fn_body(source, "fn apply_container_policy_with_mode");
357        // Find the NotEnforced match arm within the function body
358        let not_enforced_start = fn_body
359            .find("NotEnforced")
360            .expect("function must handle NotEnforced status");
361        // Search from NotEnforced to the next match arm ('=>' after a '}')
362        let rest = &fn_body[not_enforced_start..];
363        let arm_end = rest
364            .find("RestrictionStatus::")
365            .unwrap_or(rest.len().min(500));
366        let not_enforced_block = &rest[..arm_end];
367        assert!(
368            not_enforced_block.contains("best_effort") && not_enforced_block.contains("Err"),
369            "NotEnforced must return Err when best_effort=false. Block: {}",
370            not_enforced_block
371        );
372    }
373}