Skip to content

Commit 4782868

Browse files
authored
fix(vm): keep the guest root readable under a private umask (#5099)
* fix(vm): keep the guest root readable under a private umask Create guest dirs as 0755 and normalize the overlay root at boot. Signed-off-by: Eric Curtin <eric.curtin@docker.com> * test(vm): pin host state dirs to 0700 Signed-off-by: Eric Curtin <eric.curtin@docker.com> --------- Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent 7b2c28b commit 4782868

3 files changed

Lines changed: 86 additions & 6 deletions

File tree

‎crates/openshell-driver-vm/scripts/openshell-vm-sandbox-init.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ setup_overlay_root() {
258258
fi
259259
fi
260260

261+
# The host makes upper under the gateway's umask and uid, and it becomes /.
262+
chown 0:0 /overlay/upper
263+
chmod 0755 /overlay/upper
264+
261265
mount -t overlay overlay \
262266
-o lowerdir="$lower_root",upperdir=/overlay/upper,workdir=/overlay/work \
263267
/newroot

‎crates/openshell-driver-vm/src/driver.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10698,6 +10698,20 @@ mod tests {
1069810698
);
1069910699
}
1070010700

10701+
#[cfg(unix)]
10702+
#[tokio::test]
10703+
async fn host_state_dirs_stay_private() {
10704+
use std::os::unix::fs::PermissionsExt as _;
10705+
10706+
let base = unique_temp_dir();
10707+
let dir = base.join("sandboxes").join("sandbox-123");
10708+
create_private_dir_all(&dir).await.unwrap();
10709+
10710+
let mode = std::fs::metadata(&dir).unwrap().permissions().mode();
10711+
assert_eq!(mode & 0o777, 0o700);
10712+
let _ = std::fs::remove_dir_all(base);
10713+
}
10714+
1070110715
#[tokio::test]
1070210716
async fn sandbox_request_metadata_round_trips_for_start() {
1070310717
let base = unique_temp_dir();

‎crates/openshell-driver-vm/src/rootfs.rs‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ fn prepare_sandbox_rootfs(
552552

553553
let init_path = rootfs.join("srv/openshell-vm-sandbox-init.sh");
554554
if let Some(parent) = init_path.parent() {
555-
fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
555+
create_guest_dir_all(parent)?;
556556
}
557557
fs::write(
558558
&init_path,
@@ -572,7 +572,7 @@ fn prepare_sandbox_rootfs(
572572
ensure_umoci_binary(rootfs)?;
573573

574574
let opt_dir = rootfs.join("opt/openshell");
575-
fs::create_dir_all(&opt_dir).map_err(|e| format!("create {}: {e}", opt_dir.display()))?;
575+
create_guest_dir_all(&opt_dir)?;
576576
fs::write(opt_dir.join(".rootfs-type"), "sandbox\n")
577577
.map_err(|e| format!("write sandbox rootfs marker: {e}"))?;
578578
ensure_sandbox_guest_user(rootfs, sandbox_uid, sandbox_gid)?;
@@ -606,14 +606,33 @@ pub fn validate_sandbox_rootfs(rootfs: &Path) -> Result<(), String> {
606606
}
607607

608608
fn create_sandbox_mountpoint(path: &Path) -> Result<(), String> {
609-
fs::create_dir_all(path).map_err(|e| format!("create {}: {e}", path.display()))?;
609+
create_guest_dir_all(path)?;
610+
chmod_guest_dir(path)
611+
}
612+
613+
/// Like `create_dir_all`, but each directory it makes is 0755 whatever the
614+
/// umask. The sandbox user must be able to traverse the guest rootfs.
615+
fn create_guest_dir_all(path: &Path) -> Result<(), String> {
616+
if path.is_dir() {
617+
return Ok(());
618+
}
619+
if let Some(parent) = path.parent() {
620+
create_guest_dir_all(parent)?;
621+
}
622+
fs::create_dir(path).map_err(|e| format!("create {}: {e}", path.display()))?;
623+
chmod_guest_dir(path)
624+
}
625+
626+
fn chmod_guest_dir(path: &Path) -> Result<(), String> {
610627
#[cfg(unix)]
611628
{
612629
use std::os::unix::fs::PermissionsExt as _;
613630

614631
fs::set_permissions(path, fs::Permissions::from_mode(0o755))
615632
.map_err(|e| format!("chmod {}: {e}", path.display()))?;
616633
}
634+
#[cfg(not(unix))]
635+
let _ = path;
617636
Ok(())
618637
}
619638

@@ -1104,7 +1123,7 @@ fn ensure_supervisor_binary(rootfs: &Path) -> Result<(), String> {
11041123
}
11051124
} else {
11061125
if let Some(parent) = path.parent() {
1107-
fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
1126+
create_guest_dir_all(parent)?;
11081127
}
11091128

11101129
let sandbox = zstd::decode_all(Cursor::new(SANDBOX))
@@ -1134,7 +1153,7 @@ fn ensure_vm_init_binary(rootfs: &Path) -> Result<(), String> {
11341153
}
11351154
} else {
11361155
if let Some(parent) = path.parent() {
1137-
fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
1156+
create_guest_dir_all(parent)?;
11381157
}
11391158
let helper = zstd::decode_all(Cursor::new(VM_INIT))
11401159
.map_err(|e| format!("decompress VM guest init helper: {e}"))?;
@@ -1163,7 +1182,7 @@ fn ensure_umoci_binary(rootfs: &Path) -> Result<(), String> {
11631182
}
11641183
} else {
11651184
if let Some(parent) = path.parent() {
1166-
fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
1185+
create_guest_dir_all(parent)?;
11671186
}
11681187

11691188
let umoci =
@@ -1435,6 +1454,49 @@ mod tests {
14351454
);
14361455
}
14371456

1457+
#[cfg(unix)]
1458+
#[test]
1459+
fn guest_dirs_are_traversable_under_a_private_umask() {
1460+
use std::os::unix::fs::PermissionsExt as _;
1461+
1462+
// The umask is process-wide, so the check runs in a child.
1463+
const DIR: &str = "OPENSHELL_TEST_GUEST_DIR";
1464+
if let Ok(dir) = std::env::var(DIR) {
1465+
create_guest_dir_all(&Path::new(&dir).join("opt/openshell/bin")).expect("create dirs");
1466+
return;
1467+
}
1468+
1469+
let dir = unique_temp_dir();
1470+
let test = "rootfs::tests::guest_dirs_are_traversable_under_a_private_umask";
1471+
let status = Command::new("sh")
1472+
.args(["-c", "umask 077; exec \"$0\" --exact \"$1\""])
1473+
.arg(std::env::current_exe().expect("test binary"))
1474+
.arg(test)
1475+
.env(DIR, &dir)
1476+
.status()
1477+
.expect("run child");
1478+
assert!(status.success());
1479+
1480+
for path in ["opt", "opt/openshell", "opt/openshell/bin"] {
1481+
let mode = fs::metadata(dir.join(path)).expect("stat").permissions();
1482+
assert_eq!(mode.mode() & 0o777, 0o755, "{path}");
1483+
}
1484+
let _ = fs::remove_dir_all(&dir);
1485+
}
1486+
1487+
#[test]
1488+
fn guest_init_normalizes_the_overlay_root_before_mounting_it() {
1489+
// Overlayfs takes the root's owner and mode from upper, which the host
1490+
// creates under the gateway's umask. A private umask would make / 0700
1491+
// and unreadable to the sandbox user.
1492+
let script = include_str!("../scripts/openshell-vm-sandbox-init.sh");
1493+
let position = |needle: &str| script.find(needle).unwrap_or_else(|| panic!("{needle}"));
1494+
let chown = position("chown 0:0 /overlay/upper");
1495+
let chmod = position("chmod 0755 /overlay/upper");
1496+
let mount = position("mount -t overlay overlay");
1497+
assert!(chown < mount && chmod < mount);
1498+
}
1499+
14381500
#[test]
14391501
fn prepare_sandbox_rootfs_preserves_image_workdir_contents_in_rootfs() {
14401502
let dir = unique_temp_dir();

0 commit comments

Comments
 (0)