Skip to content

Commit 6a347f4

Browse files
fix(podman): create managed workspace volumes owned by the workload identity
Podman now creates the managed /sandbox volume with uid/gid options for the resolved workload identity, so the workload starts directly as that identity. This removes the root-then-drop workspace chown and fixes rootful sandboxes whose image USER or policy run_as_user could not write to a root-owned /sandbox. Signed-off-by: Matthew Grossman <[email protected]>
1 parent 0f3bdfb commit 6a347f4

4 files changed

Lines changed: 118 additions & 109 deletions

File tree

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

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,30 @@ impl PodmanClient {
705705
// ── Volume operations ────────────────────────────────────────────────
706706

707707
/// Never adopt an unrelated existing volume on a private provisioning path.
708+
///
709+
/// With `owner`, Podman creates the volume root owned by that UID and GID,
710+
/// so a non-root workload can use it without a privileged chown.
708711
pub(crate) async fn create_owned_volume(
709712
&self,
710713
name: &str,
711714
sandbox_id: &str,
712715
workspace: &str,
716+
owner: Option<(u32, u32)>,
713717
) -> Result<(), PodmanApiError> {
718+
let mount_options = owner.map(|(uid, gid)| format!("uid={uid},gid={gid}"));
719+
// Podman records the parsed `UID`/`GID` next to the raw `o` option.
720+
let owned_as_requested = |options: &HashMap<String, String>| {
721+
let Some((uid, gid)) = owner else {
722+
return options.is_empty();
723+
};
724+
options.get("o") == mount_options.as_ref()
725+
&& options.iter().all(|(key, value)| match key.as_str() {
726+
"o" => true,
727+
"UID" => *value == uid.to_string(),
728+
"GID" => *value == gid.to_string(),
729+
_ => false,
730+
})
731+
};
714732
let labels = HashMap::from([
715733
(
716734
openshell_core::driver_utils::LABEL_SANDBOX_ID.to_string(),
@@ -724,7 +742,7 @@ impl PodmanClient {
724742
match self.inspect_volume(name).await {
725743
Ok(existing) => {
726744
if existing.driver != "local"
727-
|| !existing.options.is_empty()
745+
|| !owned_as_requested(&existing.options)
728746
|| existing.labels.as_ref() != Some(&labels)
729747
{
730748
return Err(PodmanApiError::InvalidInput(
@@ -736,14 +754,15 @@ impl PodmanClient {
736754
Err(PodmanApiError::NotFound(_)) => {}
737755
Err(error) => return Err(error),
738756
}
739-
self.create_ignore_conflict(
740-
"/libpod/volumes/create",
741-
&serde_json::json!({"Name":name,"Driver":"local","Labels":labels}),
742-
)
743-
.await?;
757+
let mut body = serde_json::json!({"Name":name,"Driver":"local","Labels":labels});
758+
if let Some(mount_options) = &mount_options {
759+
body["Options"] = serde_json::json!({ "o": mount_options });
760+
}
761+
self.create_ignore_conflict("/libpod/volumes/create", &body)
762+
.await?;
744763
let created = self.inspect_volume(name).await?;
745764
if created.driver != "local"
746-
|| !created.options.is_empty()
765+
|| !owned_as_requested(&created.options)
747766
|| created.labels.as_ref() != Some(&labels)
748767
{
749768
return Err(PodmanApiError::InvalidInput(
@@ -1161,6 +1180,42 @@ mod tests {
11611180
let _ = std::fs::remove_file(socket_path);
11621181
}
11631182

1183+
#[tokio::test]
1184+
async fn create_owned_volume_verifies_requested_owner() {
1185+
let labels =
1186+
r#"{"openshell.ai/sandbox-id":"sandbox-1","openshell.ai/sandbox-workspace":"team-a"}"#;
1187+
for (options, accepted) in [
1188+
(
1189+
r#"{"o":"uid=1234,gid=1235","UID":"1234","GID":"1235"}"#,
1190+
true,
1191+
),
1192+
(r#"{"o":"uid=1234,gid=1235"}"#, true),
1193+
(r#"{"o":"uid=1234,gid=1235","UID":"0","GID":"1235"}"#, false),
1194+
(r#"{"o":"uid=1234,gid=1235","device":"/srv/work"}"#, false),
1195+
("{}", false),
1196+
] {
1197+
let (socket_path, _, handle) = spawn_podman_stub(
1198+
"owned-volume",
1199+
vec![
1200+
StubResponse::new(StatusCode::NOT_FOUND, ""),
1201+
StubResponse::new(StatusCode::CREATED, "{}"),
1202+
StubResponse::new(
1203+
StatusCode::OK,
1204+
format!(
1205+
r#"{{"Name":"work","Driver":"local","Options":{options},"Labels":{labels}}}"#
1206+
),
1207+
),
1208+
],
1209+
);
1210+
let result = PodmanClient::new(socket_path.clone())
1211+
.create_owned_volume("work", "sandbox-1", "team-a", Some((1234, 1235)))
1212+
.await;
1213+
assert_eq!(result.is_ok(), accepted, "options {options}: {result:?}");
1214+
handle.await.expect("stub task should finish");
1215+
let _ = std::fs::remove_file(socket_path);
1216+
}
1217+
}
1218+
11641219
#[tokio::test]
11651220
async fn inspect_image_reads_immutable_id_and_oci_config() {
11661221
let (socket_path, request_log, handle) = spawn_podman_stub(

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

Lines changed: 23 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,6 @@ pub struct IsolationSpecInput<'a> {
15321532
pub supervisor_bin: Option<&'a Path>,
15331533
pub tls_secrets: Option<&'a [String; 3]>,
15341534
pub identity: &'a openshell_isolation_interface::contract::ResolvedWorkloadIdentity,
1535-
/// Whether this workload is created by a rootless Podman service.
1536-
pub rootless: bool,
15371535
}
15381536

15391537
pub struct IsolationSpecs {
@@ -1585,46 +1583,19 @@ pub fn build_isolation_specs(
15851583
.iter()
15861584
.filter_map(|entry| entry.split_once('=').map(|(key, _)| key.to_string()))
15871585
.collect();
1588-
if input.image.uses_managed_workspace()
1589-
&& (input.rootless || input.identity.source == "default")
1590-
{
1591-
// Podman's archive endpoint leaves named-volume contents owned by
1592-
// container root for rootless services and for a rootful USER-less
1593-
// image's newly-created workspace. Start the trusted runtime as root
1594-
// only long enough to chown the workspace, then irreversibly drop to
1595-
// the resolved workload identity before reading bootstrap material or
1596-
// accepting a control connection.
1597-
workload.command = vec![
1598-
"launch-capability-free".into(),
1599-
input.identity.uid.to_string(),
1600-
input.identity.gid.to_string(),
1601-
crate::isolation::BOOTSTRAP_PATH.into(),
1602-
input.image.workspace_root.clone(),
1603-
];
1604-
workload.user = "0:0".into();
1605-
workload.groups.clear();
1606-
workload.cap_drop = vec!["ALL".into()];
1607-
workload.cap_add = vec![
1608-
"CHOWN".into(),
1609-
"SETGID".into(),
1610-
"SETUID".into(),
1611-
"SETPCAP".into(),
1612-
];
1613-
} else {
1614-
workload.command = vec![
1615-
"--bootstrap".into(),
1616-
crate::isolation::BOOTSTRAP_PATH.into(),
1617-
];
1618-
workload.user.clone_from(&user);
1619-
workload.groups = input
1620-
.identity
1621-
.supplementary_gids
1622-
.iter()
1623-
.map(ToString::to_string)
1624-
.collect();
1625-
workload.cap_drop = vec!["ALL".into()];
1626-
workload.cap_add.clear();
1627-
}
1586+
workload.command = vec![
1587+
"--bootstrap".into(),
1588+
crate::isolation::BOOTSTRAP_PATH.into(),
1589+
];
1590+
workload.user.clone_from(&user);
1591+
workload.groups = input
1592+
.identity
1593+
.supplementary_gids
1594+
.iter()
1595+
.map(ToString::to_string)
1596+
.collect();
1597+
workload.cap_drop = vec!["ALL".into()];
1598+
workload.cap_add.clear();
16281599
workload.apparmor_profile = input
16291600
.config
16301601
.app_armor_profile
@@ -1934,29 +1905,21 @@ mod tests {
19341905
supervisor_bin: None,
19351906
tls_secrets: None,
19361907
identity: &identity,
1937-
rootless: true,
19381908
})
19391909
.unwrap();
19401910
for spec in [&specs.workload, &specs.supervisor] {
19411911
assert_eq!(spec.cap_drop, vec!["ALL"]);
19421912
assert!(spec.seccomp_profile_path.is_empty());
19431913
assert!(spec.no_new_privileges);
19441914
}
1945-
assert_eq!(specs.workload.user, "0:0");
1946-
assert!(specs.workload.groups.is_empty());
1947-
assert_eq!(
1948-
specs.workload.cap_add,
1949-
vec!["CHOWN", "SETGID", "SETUID", "SETPCAP"]
1950-
);
1915+
// The driver creates the managed workspace volume owned by the
1916+
// workload identity, so the workload never starts as root.
1917+
assert_eq!(specs.workload.user, "1000:1001");
1918+
assert_eq!(specs.workload.groups, vec!["2000"]);
1919+
assert!(specs.workload.cap_add.is_empty());
19511920
assert_eq!(
19521921
specs.workload.command,
1953-
vec![
1954-
"launch-capability-free",
1955-
"1000",
1956-
"1001",
1957-
crate::isolation::BOOTSTRAP_PATH,
1958-
driver_mounts::DEFAULT_WORKSPACE_ROOT,
1959-
]
1922+
vec!["--bootstrap", crate::isolation::BOOTSTRAP_PATH]
19601923
);
19611924
assert_eq!(specs.supervisor.user, "1000:1001");
19621925
assert_eq!(specs.supervisor.groups, vec!["2000"]);
@@ -1985,7 +1948,7 @@ mod tests {
19851948
"sha256:image".into(),
19861949
)
19871950
.unwrap();
1988-
let rootful_specs = build_isolation_specs(IsolationSpecInput {
1951+
let default_specs = build_isolation_specs(IsolationSpecInput {
19891952
sandbox: &sandbox,
19901953
config: &config,
19911954
token_secret: Some("jwt"),
@@ -1996,39 +1959,12 @@ mod tests {
19961959
supervisor_bin: None,
19971960
tls_secrets: None,
19981961
identity: &default_identity,
1999-
rootless: false,
2000-
})
2001-
.unwrap();
2002-
assert_eq!(rootful_specs.workload.user, "0:0");
2003-
assert_eq!(
2004-
rootful_specs.workload.command,
2005-
vec![
2006-
"launch-capability-free",
2007-
"1000",
2008-
"1000",
2009-
crate::isolation::BOOTSTRAP_PATH,
2010-
driver_mounts::DEFAULT_WORKSPACE_ROOT,
2011-
]
2012-
);
2013-
let custom_image = resolved_image("sha256:image", "1000:1001", "/workspace/project");
2014-
let custom_specs = build_isolation_specs(IsolationSpecInput {
2015-
sandbox: &sandbox,
2016-
config: &config,
2017-
token_secret: Some("jwt"),
2018-
resolver_secret: "resolver",
2019-
gpu_devices: None,
2020-
requested_image: "image:latest",
2021-
image: &custom_image,
2022-
supervisor_bin: None,
2023-
tls_secrets: None,
2024-
identity: &identity,
2025-
rootless: true,
20261962
})
20271963
.unwrap();
2028-
assert_eq!(custom_specs.workload.user, "1000:1001");
2029-
assert!(custom_specs.workload.cap_add.is_empty());
1964+
assert_eq!(default_specs.workload.user, "1000:1000");
1965+
assert!(default_specs.workload.cap_add.is_empty());
20301966
assert_eq!(
2031-
custom_specs.workload.command,
1967+
default_specs.workload.command,
20321968
vec!["--bootstrap", crate::isolation::BOOTSTRAP_PATH]
20331969
);
20341970
let workload_json = serde_json::to_string(&specs.workload).unwrap();

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,12 @@ impl PodmanComputeDriver {
10351035
let result = async {
10361036
if managed_workspace {
10371037
self.client
1038-
.create_owned_volume(&vol_name, &sandbox.id, &sandbox.workspace)
1038+
.create_owned_volume(
1039+
&vol_name,
1040+
&sandbox.id,
1041+
&sandbox.workspace,
1042+
Some((identity.uid, identity.gid)),
1043+
)
10391044
.await
10401045
.map_err(ComputeDriverError::from)?;
10411046
}
@@ -1162,7 +1167,6 @@ impl PodmanComputeDriver {
11621167
supervisor_bin: supervisor_bin_path.as_deref(),
11631168
tls_secrets: tls_secret_names.as_ref(),
11641169
identity: &identity,
1165-
rootless: self.rootless,
11661170
});
11671171
let mut specs = match specs {
11681172
Ok(spec) => spec,
@@ -1177,7 +1181,7 @@ impl PodmanComputeDriver {
11771181
let identities = self.validate_user_volume_mounts_available(sandbox).await?;
11781182
specs.record_resource_identities(&identities)?;
11791183
self.client
1180-
.create_owned_volume(&channel_volume, &sandbox.id, &sandbox.workspace)
1184+
.create_owned_volume(&channel_volume, &sandbox.id, &sandbox.workspace, None)
11811185
.await?;
11821186
channel_owned.store(true, std::sync::atomic::Ordering::Relaxed);
11831187
let workload_id = self.client.create_typed_container(&specs.workload).await?;
@@ -3040,7 +3044,7 @@ mod tests {
30403044
assert!(
30413045
driver
30423046
.client
3043-
.create_owned_volume("private-collision", "sandbox-123", "team-a")
3047+
.create_owned_volume("private-collision", "sandbox-123", "team-a", None)
30443048
.await
30453049
.is_err()
30463050
);
@@ -3556,7 +3560,11 @@ mod tests {
35563560
image_response("sha256:supervisor"),
35573561
StubResponse::new(StatusCode::NOT_FOUND, ""), // no existing private workspace
35583562
StubResponse::new(StatusCode::CREATED, "{}"), // workspace volume
3559-
owned_volume_response(&container::volume_name(sandbox_id), sandbox_id),
3563+
owned_volume_response(
3564+
&container::volume_name(sandbox_id),
3565+
sandbox_id,
3566+
Some((1234, 1235)), // the stub image's OCI user
3567+
),
35603568
StubResponse::new(StatusCode::CREATED, "{}"), // resolver secret
35613569
];
35623570
if proxy_secret {
@@ -3573,15 +3581,30 @@ mod tests {
35733581
responses.push(owned_volume_response(
35743582
&crate::isolation::channel_volume_name(sandbox_id),
35753583
sandbox_id,
3584+
None,
35763585
));
35773586
responses
35783587
}
35793588

3580-
fn owned_volume_response(name: &str, sandbox_id: &str) -> StubResponse {
3589+
fn owned_volume_response(
3590+
name: &str,
3591+
sandbox_id: &str,
3592+
owner: Option<(u32, u32)>,
3593+
) -> StubResponse {
3594+
let options = owner.map_or_else(
3595+
|| serde_json::json!({}),
3596+
|(uid, gid)| {
3597+
serde_json::json!({
3598+
"o": format!("uid={uid},gid={gid}"),
3599+
"UID": uid.to_string(),
3600+
"GID": gid.to_string(),
3601+
})
3602+
},
3603+
);
35813604
StubResponse::new(
35823605
StatusCode::OK,
35833606
serde_json::json!({
3584-
"Name": name, "Driver": "local", "Options": {},
3607+
"Name": name, "Driver": "local", "Options": options,
35853608
"Labels": {LABEL_SANDBOX_ID: sandbox_id, container::LABEL_SANDBOX_WORKSPACE: ""}
35863609
})
35873610
.to_string(),

‎tests/suites/features/oci-image/tests/oci_image.rs‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,14 @@ USER 2345:2346
7474
.await;
7575
}
7676

77-
/// An image without a `WORKDIR` uses the managed `/sandbox` workspace. An
78-
/// image that declares `USER` provides a `/sandbox` owned by that user.
77+
/// An image without a `WORKDIR` uses the managed `/sandbox` workspace, owned
78+
/// by the image user, even when the image does not contain `/sandbox`.
7979
#[tokio::test]
8080
async fn default_workdir_uses_managed_workspace() {
8181
run("oci-image/default-workdir", async |runner| {
8282
let image = TestImage::build(
8383
"default-workdir",
84-
&format!(
85-
"FROM {BASE_IMAGE}
86-
RUN mkdir /sandbox && chown 2345:2346 /sandbox && chmod 0700 /sandbox
87-
USER 2345:2346
88-
"
89-
),
84+
&format!("FROM {BASE_IMAGE}\nUSER 2345:2346\n"),
9085
)?;
9186
let checks = workspace_checks("2345:2346", "/sandbox", false);
9287
create_sandbox(runner, "default", "dw", &image, &checks)

0 commit comments

Comments
 (0)