|
4 | 4 | //! Landlock filesystem sandboxing. |
5 | 5 |
|
6 | 6 | use landlock::{ |
7 | | - ABI, Access, AccessFs, CompatLevel, Compatible, PathBeneath, PathFd, PathFdError, Ruleset, |
8 | | - RulesetAttr, RulesetCreatedAttr, |
| 7 | + ABI, Access, AccessFs, BitFlags, CompatLevel, Compatible, PathBeneath, PathFd, PathFdError, |
| 8 | + Ruleset, RulesetAttr, RulesetCreatedAttr, |
9 | 9 | }; |
10 | 10 | use miette::{IntoDiagnostic, Result}; |
11 | 11 | use openshell_core::policy::{LandlockCompatibility, SandboxPolicy}; |
| 12 | +use std::os::fd::AsFd; |
12 | 13 | use std::path::{Path, PathBuf}; |
13 | 14 | use tracing::debug; |
14 | 15 |
|
@@ -216,19 +217,21 @@ fn prepare_with_path_open_mode( |
216 | 217 |
|
217 | 218 | for path in &read_only { |
218 | 219 | if let Some(path_fd) = try_open_path(path, compatibility, path_open_mode)? { |
| 220 | + let allowed_access = access_for_path_fd(&path_fd, access_read, abi)?; |
219 | 221 | debug!(path = %path.display(), "Landlock allow read-only"); |
220 | 222 | ruleset = ruleset |
221 | | - .add_rule(PathBeneath::new(path_fd, access_read)) |
| 223 | + .add_rule(PathBeneath::new(path_fd, allowed_access)) |
222 | 224 | .into_diagnostic()?; |
223 | 225 | rules_applied += 1; |
224 | 226 | } |
225 | 227 | } |
226 | 228 |
|
227 | 229 | for path in &read_write { |
228 | 230 | if let Some(path_fd) = try_open_path(path, compatibility, path_open_mode)? { |
| 231 | + let allowed_access = access_for_path_fd(&path_fd, access_all, abi)?; |
229 | 232 | debug!(path = %path.display(), "Landlock allow read-write"); |
230 | 233 | ruleset = ruleset |
231 | | - .add_rule(PathBeneath::new(path_fd, access_all)) |
| 234 | + .add_rule(PathBeneath::new(path_fd, allowed_access)) |
232 | 235 | .into_diagnostic()?; |
233 | 236 | rules_applied += 1; |
234 | 237 | } |
@@ -344,6 +347,23 @@ pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> { |
344 | 347 | Ok(()) |
345 | 348 | } |
346 | 349 |
|
| 350 | +/// Tailor a rule's access mask to the inode referenced by its already-open FD. |
| 351 | +/// |
| 352 | +/// Landlock directory-only rights such as `ReadDir` are invalid for regular |
| 353 | +/// files and device nodes in hard-requirement mode. Classifying through the |
| 354 | +/// same `PathFd` used by the rule avoids a pathname TOCTOU race. |
| 355 | +fn access_for_path_fd( |
| 356 | + path_fd: &PathFd, |
| 357 | + requested_access: BitFlags<AccessFs>, |
| 358 | + abi: ABI, |
| 359 | +) -> Result<BitFlags<AccessFs>> { |
| 360 | + let stat = rustix::fs::fstat(path_fd.as_fd()).into_diagnostic()?; |
| 361 | + Ok(match rustix::fs::FileType::from_raw_mode(stat.st_mode) { |
| 362 | + rustix::fs::FileType::Directory => requested_access, |
| 363 | + _ => requested_access & AccessFs::from_file(abi), |
| 364 | + }) |
| 365 | +} |
| 366 | + |
347 | 367 | /// Attempt to open a path for Landlock rule creation. |
348 | 368 | /// |
349 | 369 | /// In `BestEffort` mode, inaccessible paths (missing, permission denied, symlink |
@@ -468,6 +488,98 @@ fn compat_level(level: &LandlockCompatibility) -> CompatLevel { |
468 | 488 | #[cfg(test)] |
469 | 489 | mod tests { |
470 | 490 | use super::*; |
| 491 | + use openshell_core::policy::{FilesystemPolicy, LandlockPolicy, NetworkPolicy, ProcessPolicy}; |
| 492 | + |
| 493 | + fn hard_requirement_policy(read_only: Vec<PathBuf>, read_write: Vec<PathBuf>) -> SandboxPolicy { |
| 494 | + SandboxPolicy { |
| 495 | + version: 1, |
| 496 | + filesystem: FilesystemPolicy { |
| 497 | + read_only, |
| 498 | + read_write, |
| 499 | + include_workdir: false, |
| 500 | + }, |
| 501 | + network: NetworkPolicy::default(), |
| 502 | + landlock: LandlockPolicy { |
| 503 | + compatibility: LandlockCompatibility::HardRequirement, |
| 504 | + }, |
| 505 | + process: ProcessPolicy::default(), |
| 506 | + } |
| 507 | + } |
| 508 | + |
| 509 | + #[test] |
| 510 | + fn prepare_hard_requirement_accepts_device_paths() { |
| 511 | + if !matches!(probe_availability(), LandlockAvailability::Available { .. }) { |
| 512 | + return; |
| 513 | + } |
| 514 | + |
| 515 | + let policy = hard_requirement_policy( |
| 516 | + vec![PathBuf::from("/tmp"), PathBuf::from("/dev/urandom")], |
| 517 | + vec![PathBuf::from("/dev/null")], |
| 518 | + ); |
| 519 | + |
| 520 | + let result = prepare(&policy, None); |
| 521 | + if let Err(err) = result { |
| 522 | + panic!("hard_requirement should accept mixed directory and device paths: {err}"); |
| 523 | + } |
| 524 | + } |
| 525 | + fn tailored_access(path: &Path, requested_access: BitFlags<AccessFs>) -> BitFlags<AccessFs> { |
| 526 | + let path_fd = PathFd::new(path).unwrap(); |
| 527 | + access_for_path_fd(&path_fd, requested_access, ABI::V2).unwrap() |
| 528 | + } |
| 529 | + |
| 530 | + #[test] |
| 531 | + fn access_for_path_fd_preserves_directory_access() { |
| 532 | + let dir = tempfile::tempdir().unwrap(); |
| 533 | + let requested_access = AccessFs::from_all(ABI::V2); |
| 534 | + |
| 535 | + assert_eq!( |
| 536 | + tailored_access(dir.path(), requested_access), |
| 537 | + requested_access |
| 538 | + ); |
| 539 | + } |
| 540 | + |
| 541 | + #[test] |
| 542 | + fn access_for_path_fd_limits_regular_file_access() { |
| 543 | + let file = tempfile::NamedTempFile::new().unwrap(); |
| 544 | + let requested_access = AccessFs::from_all(ABI::V2); |
| 545 | + |
| 546 | + assert_eq!( |
| 547 | + tailored_access(file.path(), requested_access), |
| 548 | + requested_access & AccessFs::from_file(ABI::V2) |
| 549 | + ); |
| 550 | + } |
| 551 | + |
| 552 | + #[test] |
| 553 | + fn access_for_path_fd_limits_character_device_access() { |
| 554 | + let requested_read = AccessFs::from_read(ABI::V2); |
| 555 | + let requested_write = AccessFs::from_all(ABI::V2); |
| 556 | + |
| 557 | + assert_eq!( |
| 558 | + tailored_access(Path::new("/dev/urandom"), requested_read), |
| 559 | + requested_read & AccessFs::from_file(ABI::V2) |
| 560 | + ); |
| 561 | + assert_eq!( |
| 562 | + tailored_access(Path::new("/dev/null"), requested_write), |
| 563 | + requested_write & AccessFs::from_file(ABI::V2) |
| 564 | + ); |
| 565 | + } |
| 566 | + |
| 567 | + #[test] |
| 568 | + fn access_for_path_fd_classifies_symlink_target() { |
| 569 | + use std::os::unix::fs::symlink; |
| 570 | + |
| 571 | + let dir = tempfile::tempdir().unwrap(); |
| 572 | + let target = dir.path().join("target"); |
| 573 | + let link = dir.path().join("link"); |
| 574 | + std::fs::File::create(&target).unwrap(); |
| 575 | + symlink(&target, &link).unwrap(); |
| 576 | + |
| 577 | + let requested_access = AccessFs::from_all(ABI::V2); |
| 578 | + assert_eq!( |
| 579 | + tailored_access(&link, requested_access), |
| 580 | + requested_access & AccessFs::from_file(ABI::V2) |
| 581 | + ); |
| 582 | + } |
471 | 583 |
|
472 | 584 | #[test] |
473 | 585 | fn try_open_path_best_effort_returns_none_for_missing_path() { |
|
0 commit comments