Skip to content

Commit 38fa191

Browse files
committed
fix(podman): initialize rootless workspace ownership
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 39014b8 commit 38fa191

3 files changed

Lines changed: 87 additions & 18 deletions

File tree

‎crates/openshell-driver-podman/src/container.rs‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,8 @@ pub struct IsolationSpecInput<'a> {
14021402
pub supervisor_bin: Option<&'a Path>,
14031403
pub tls_secrets: Option<&'a [String; 3]>,
14041404
pub identity: &'a openshell_isolation_interface::contract::ResolvedWorkloadIdentity,
1405+
/// Whether this workload is created by a rootless Podman service.
1406+
pub rootless: bool,
14051407
}
14061408

14071409
pub struct IsolationSpecs {
@@ -1439,19 +1441,44 @@ pub fn build_isolation_specs(
14391441
.iter()
14401442
.filter_map(|entry| entry.split_once('=').map(|(key, _)| key.to_string()))
14411443
.collect();
1442-
workload.command = vec![
1443-
"--bootstrap".into(),
1444-
crate::isolation::BOOTSTRAP_PATH.into(),
1445-
];
1446-
workload.user.clone_from(&user);
1447-
workload.groups = input
1448-
.identity
1449-
.supplementary_gids
1450-
.iter()
1451-
.map(ToString::to_string)
1452-
.collect();
1453-
workload.cap_drop = vec!["ALL".into()];
1454-
workload.cap_add.clear();
1444+
if input.rootless {
1445+
// Podman's archive endpoint writes named-volume contents with the
1446+
// rootless gateway user's host ownership. In a remapped user namespace,
1447+
// that is container root rather than the resolved workload UID. Start
1448+
// the trusted runtime as namespace root only long enough to chown the
1449+
// empty workspace and irreversibly drop to the resolved identity before
1450+
// it reads bootstrap material or accepts a control connection.
1451+
workload.command = vec![
1452+
"launch-capability-free".into(),
1453+
input.identity.uid.to_string(),
1454+
input.identity.gid.to_string(),
1455+
crate::isolation::BOOTSTRAP_PATH.into(),
1456+
driver_mounts::DEFAULT_WORKSPACE_ROOT.into(),
1457+
];
1458+
workload.user = "0:0".into();
1459+
workload.groups.clear();
1460+
workload.cap_drop = vec!["ALL".into()];
1461+
workload.cap_add = vec![
1462+
"CHOWN".into(),
1463+
"SETGID".into(),
1464+
"SETUID".into(),
1465+
"SETPCAP".into(),
1466+
];
1467+
} else {
1468+
workload.command = vec![
1469+
"--bootstrap".into(),
1470+
crate::isolation::BOOTSTRAP_PATH.into(),
1471+
];
1472+
workload.user.clone_from(&user);
1473+
workload.groups = input
1474+
.identity
1475+
.supplementary_gids
1476+
.iter()
1477+
.map(ToString::to_string)
1478+
.collect();
1479+
workload.cap_drop = vec!["ALL".into()];
1480+
workload.cap_add.clear();
1481+
}
14551482
workload.apparmor_profile = input
14561483
.config
14571484
.app_armor_profile
@@ -1740,16 +1767,33 @@ mod tests {
17401767
supervisor_bin: None,
17411768
tls_secrets: None,
17421769
identity: &identity,
1770+
rootless: true,
17431771
})
17441772
.unwrap();
17451773
for spec in [&specs.workload, &specs.supervisor] {
1746-
assert_eq!(spec.user, "1000:1001");
1747-
assert_eq!(spec.groups, vec!["2000"]);
17481774
assert_eq!(spec.cap_drop, vec!["ALL"]);
1749-
assert!(spec.cap_add.is_empty());
17501775
assert!(spec.seccomp_profile_path.is_empty());
17511776
assert!(spec.no_new_privileges);
17521777
}
1778+
assert_eq!(specs.workload.user, "0:0");
1779+
assert!(specs.workload.groups.is_empty());
1780+
assert_eq!(
1781+
specs.workload.cap_add,
1782+
vec!["CHOWN", "SETGID", "SETUID", "SETPCAP"]
1783+
);
1784+
assert_eq!(
1785+
specs.workload.command,
1786+
vec![
1787+
"launch-capability-free",
1788+
"1000",
1789+
"1001",
1790+
crate::isolation::BOOTSTRAP_PATH,
1791+
driver_mounts::DEFAULT_WORKSPACE_ROOT,
1792+
]
1793+
);
1794+
assert_eq!(specs.supervisor.user, "1000:1001");
1795+
assert_eq!(specs.supervisor.groups, vec!["2000"]);
1796+
assert!(specs.supervisor.cap_add.is_empty());
17531797
assert_eq!(specs.workload.netns.nsmode, "none");
17541798
assert_eq!(
17551799
specs

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ impl PodmanComputeDriver {
938938
supervisor_bin: supervisor_bin_path.as_deref(),
939939
tls_secrets: tls_secret_names.as_ref(),
940940
identity: &identity,
941+
rootless: self.rootless,
941942
});
942943
let specs = match specs {
943944
Ok(spec) => spec,

‎crates/openshell-sandbox/src/main.rs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,13 +1518,37 @@ fn launch_capability_probe(_args: &[String]) -> Result<()> {
15181518
fn launch_capability_free(args: &[String]) -> Result<()> {
15191519
use miette::{Context as _, IntoDiagnostic as _};
15201520

1521-
let [uid, gid, bootstrap] = args else {
1521+
let [uid, gid, bootstrap, workspace @ ..] = args else {
15221522
return Err(miette::miette!(
1523-
"usage: openshell-sandbox {CAPABILITY_FREE_LAUNCH_SUBCOMMAND} <UID> <GID> <BOOTSTRAP>"
1523+
"usage: openshell-sandbox {CAPABILITY_FREE_LAUNCH_SUBCOMMAND} <UID> <GID> <BOOTSTRAP> [WORKSPACE]"
15241524
));
15251525
};
1526+
if workspace.len() > 1 {
1527+
return Err(miette::miette!(
1528+
"usage: openshell-sandbox {CAPABILITY_FREE_LAUNCH_SUBCOMMAND} <UID> <GID> <BOOTSTRAP> [WORKSPACE]"
1529+
));
1530+
}
15261531
let uid = uid.parse::<u32>().into_diagnostic().wrap_err("parse UID")?;
15271532
let gid = gid.parse::<u32>().into_diagnostic().wrap_err("parse GID")?;
1533+
if let Some(workspace) = workspace.first() {
1534+
let workspace = Path::new(workspace);
1535+
let metadata = std::fs::symlink_metadata(workspace)
1536+
.into_diagnostic()
1537+
.wrap_err_with(|| format!("read workspace metadata {}", workspace.display()))?;
1538+
if !metadata.file_type().is_dir() {
1539+
return Err(miette::miette!(
1540+
"workspace {} must be a real directory",
1541+
workspace.display()
1542+
));
1543+
}
1544+
nix::unistd::chown(
1545+
workspace,
1546+
Some(nix::unistd::Uid::from_raw(uid)),
1547+
Some(nix::unistd::Gid::from_raw(gid)),
1548+
)
1549+
.into_diagnostic()
1550+
.wrap_err_with(|| format!("set workspace ownership {}:{gid}", workspace.display()))?;
1551+
}
15281552
enter_capability_free_identity(uid, gid)?;
15291553
let log_level = std::env::var(openshell_core::sandbox_env::LOG_LEVEL)
15301554
.unwrap_or_else(|_| "warn".to_string());

0 commit comments

Comments
 (0)