Skip to content

Commit 74eacee

Browse files
authored
refactor(sandbox): remove the unused Landlock root allow-list helper (#5221)
The root allow-list was implemented twice: open_root_allowlist and RootEntryHandle in linux/landlock.rs were documented as the audited primitive but had no production caller, while the capability-free baseline enumerates the root itself in sandbox/linux/landlock.rs. Remove the unused helper, its handle type, their tests, and the dead imports, so the audited module describes only what runs. abi_version stays; it backs probe_availability. Reword the baseline's private-root comment to match the code: the descriptor backs only the eager O_DIRECTORY|O_NOFOLLOW validation and is dropped before enumeration, which excludes the private name and opens every entry with O_NOFOLLOW. Closes #5190 Signed-off-by: Emilien Macchi <emacchi@redhat.com>
1 parent 8758cbf commit 74eacee

2 files changed

Lines changed: 5 additions & 174 deletions

File tree

Lines changed: 1 addition & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Race-resistant handles for an explicit Landlock root allow-list.
4+
//! Landlock ABI probing for the sandbox boundary.
55
66
#![allow(unsafe_code)]
7-
use std::collections::BTreeSet;
8-
use std::ffi::{OsStr, OsString};
97
use std::io;
10-
use std::os::fd::OwnedFd;
11-
use std::os::unix::ffi::OsStrExt;
12-
use std::path::Path;
13-
14-
use rustix::fs::{AtFlags, Mode, OFlags, Stat, fstat, open, openat, statat};
158

169
const LANDLOCK_CREATE_RULESET_VERSION: libc::c_uint = 1;
1710

@@ -34,168 +27,3 @@ pub fn abi_version() -> io::Result<u32> {
3427
u32::try_from(result).map_err(|_| io::Error::other("Landlock ABI does not fit u32"))
3528
}
3629
}
37-
/// One verified immediate child of the sandbox root.
38-
pub struct RootEntryHandle {
39-
name: OsString,
40-
fd: OwnedFd,
41-
stat: Stat,
42-
}
43-
44-
impl RootEntryHandle {
45-
/// Immediate-root entry name.
46-
#[must_use]
47-
pub fn name(&self) -> &OsStr {
48-
&self.name
49-
}
50-
51-
/// Open, no-follow handle suitable for a later Landlock `PathBeneath` rule.
52-
#[must_use]
53-
pub fn fd(&self) -> &OwnedFd {
54-
&self.fd
55-
}
56-
57-
/// Device number captured when the entry was opened.
58-
#[must_use]
59-
pub fn device(&self) -> u64 {
60-
self.stat.st_dev
61-
}
62-
63-
/// Inode number captured when the entry was opened.
64-
#[must_use]
65-
pub fn inode(&self) -> u64 {
66-
self.stat.st_ino
67-
}
68-
}
69-
70-
/// Open exactly the named root entries while proving that none is the private
71-
/// sandbox hierarchy, a symlink, or a raced replacement.
72-
///
73-
/// Unnamed root entries are deliberately not returned and therefore cannot be
74-
/// admitted accidentally. The caller obtains the allow-list names from trusted
75-
/// image/driver policy, not by blindly allowing everything present in `/`.
76-
pub fn open_root_allowlist(
77-
root: &Path,
78-
allowed_names: &BTreeSet<OsString>,
79-
private_name: &OsStr,
80-
) -> io::Result<Vec<RootEntryHandle>> {
81-
validate_component(private_name)?;
82-
if allowed_names.contains(private_name) {
83-
return Err(io::Error::new(
84-
io::ErrorKind::InvalidInput,
85-
"private sandbox root cannot appear in the Landlock allow-list",
86-
));
87-
}
88-
89-
let root_fd = open(
90-
root,
91-
OFlags::PATH | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
92-
Mode::empty(),
93-
)?;
94-
let mut result = Vec::with_capacity(allowed_names.len());
95-
for name in allowed_names {
96-
validate_component(name)?;
97-
let before = statat(&root_fd, name, AtFlags::SYMLINK_NOFOLLOW)?;
98-
if before.st_mode & libc::S_IFMT == libc::S_IFLNK {
99-
return Err(io::Error::new(
100-
io::ErrorKind::InvalidData,
101-
format!(
102-
"Landlock root entry {} is a symlink",
103-
Path::new(name).display()
104-
),
105-
));
106-
}
107-
let fd = openat(
108-
&root_fd,
109-
name,
110-
OFlags::PATH | OFlags::NOFOLLOW | OFlags::CLOEXEC,
111-
Mode::empty(),
112-
)?;
113-
let after = fstat(&fd)?;
114-
if before.st_dev != after.st_dev
115-
|| before.st_ino != after.st_ino
116-
|| before.st_mode & libc::S_IFMT != after.st_mode & libc::S_IFMT
117-
{
118-
return Err(io::Error::new(
119-
io::ErrorKind::InvalidData,
120-
format!(
121-
"Landlock root entry {} changed while it was opened",
122-
Path::new(name).display()
123-
),
124-
));
125-
}
126-
result.push(RootEntryHandle {
127-
name: name.clone(),
128-
fd,
129-
stat: after,
130-
});
131-
}
132-
Ok(result)
133-
}
134-
135-
fn validate_component(name: &OsStr) -> io::Result<()> {
136-
let bytes = name.as_bytes();
137-
if bytes.is_empty() || bytes == b"." || bytes == b".." || bytes.contains(&b'/') {
138-
return Err(io::Error::new(
139-
io::ErrorKind::InvalidInput,
140-
format!("invalid immediate-root entry {}", Path::new(name).display()),
141-
));
142-
}
143-
Ok(())
144-
}
145-
146-
#[cfg(test)]
147-
mod tests {
148-
use std::fs;
149-
use std::os::unix::fs::symlink;
150-
use std::sync::atomic::{AtomicU64, Ordering};
151-
152-
use super::*;
153-
154-
static NEXT_TEMP: AtomicU64 = AtomicU64::new(0);
155-
156-
fn temp_root() -> std::path::PathBuf {
157-
let path = std::env::temp_dir().join(format!(
158-
"openshell-landlock-root-{}-{}",
159-
std::process::id(),
160-
NEXT_TEMP.fetch_add(1, Ordering::Relaxed)
161-
));
162-
fs::create_dir(&path).expect("create temp root");
163-
path
164-
}
165-
166-
#[test]
167-
fn opens_only_explicit_entries_and_omits_private_root() {
168-
let root = temp_root();
169-
fs::create_dir(root.join("bin")).expect("create bin");
170-
fs::create_dir(root.join("sandbox")).expect("create workspace");
171-
fs::create_dir(root.join(".openshell")).expect("create private root");
172-
fs::create_dir(root.join("unexpected")).expect("create unexpected root");
173-
174-
let allowed = BTreeSet::from([OsString::from("bin"), OsString::from("sandbox")]);
175-
let entries = open_root_allowlist(&root, &allowed, OsStr::new(".openshell"))
176-
.expect("open allow-list");
177-
assert_eq!(
178-
entries
179-
.iter()
180-
.map(|entry| entry.name().to_owned())
181-
.collect::<Vec<_>>(),
182-
vec![OsString::from("bin"), OsString::from("sandbox")]
183-
);
184-
185-
fs::remove_dir_all(root).expect("remove temp root");
186-
}
187-
188-
#[test]
189-
fn rejects_private_entry_and_symlink() {
190-
let root = temp_root();
191-
fs::create_dir(root.join(".openshell")).expect("create private root");
192-
symlink(".openshell", root.join("runtime")).expect("create symlink");
193-
194-
let private = BTreeSet::from([OsString::from(".openshell")]);
195-
assert!(open_root_allowlist(&root, &private, OsStr::new(".openshell")).is_err());
196-
let symlinked = BTreeSet::from([OsString::from("runtime")]);
197-
assert!(open_root_allowlist(&root, &symlinked, OsStr::new(".openshell")).is_err());
198-
199-
fs::remove_dir_all(root).expect("remove temp root");
200-
}
201-
}

‎crates/openshell-sandbox/src/sandbox/linux/landlock.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ fn capability_free_baseline_entries(
186186
)
187187
.into_diagnostic()?;
188188
// The reserved root itself must not redirect private child mounts into an
189-
// allowed subtree. Pin and validate it independently of the public entries.
189+
// allowed subtree. Validate it eagerly and independently of the public
190+
// entries: when present it must be a real directory. The descriptor only
191+
// backs this check and is dropped before any per-entry descriptor is
192+
// opened; exclusion during enumeration is by name plus O_NOFOLLOW.
190193
// Absence is allowed for qualification before driver bootstrap is staged.
191194
let _private_root = match openat(
192195
&root_fd,

0 commit comments

Comments
 (0)