Skip to content

Commit 796c19e

Browse files
akramelezar
authored andcommitted
feat(driver): default to numeric non-root identity for USER-less images
With the default sandbox image now Alpine, images that declare no OCI USER must start instead of being rejected. When the image declares no USER and the policy requests none, the Podman and Docker drivers now supply a numeric non-root identity (DEFAULT_SANDBOX_UID/GID = 1000) instead of rejecting, matching the numeric-identity behavior of the Kubernetes and VM drivers. The supervisor's resolved-identity path runs the sandbox as a synthesized non-root account without the account existing in the image. Images that declare a USER keep the OCI resolution path unchanged. Part of NVIDIA#3116. Signed-off-by: Akram <[email protected]> Signed-off-by: Evan Lezar <[email protected]>
1 parent 487cdae commit 796c19e

4 files changed

Lines changed: 89 additions & 16 deletions

File tree

‎crates/openshell-core/src/sandbox_env.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ pub const SANDBOX_UID: &str = "OPENSHELL_SANDBOX_UID";
243243
/// supervisor drops privileges to a group other than the UID's primary group.
244244
pub const SANDBOX_GID: &str = "OPENSHELL_SANDBOX_GID";
245245

246+
/// Default numeric UID assigned to a sandbox when the image declares no OCI
247+
/// `USER` (e.g. a plain Alpine base).
248+
///
249+
/// Local container drivers (Docker, Podman) supply this in place of an empty
250+
/// OCI declaration so the supervisor runs the sandbox as a synthesized non-root
251+
/// account instead of rejecting the image, matching the numeric-identity
252+
/// behavior of the Kubernetes and VM drivers.
253+
pub const DEFAULT_SANDBOX_UID: u32 = 1000;
254+
255+
/// Default numeric GID paired with [`DEFAULT_SANDBOX_UID`].
256+
pub const DEFAULT_SANDBOX_GID: u32 = 1000;
257+
246258
/// Raw OCI `Config.User` declaration from the immutable image selected by a
247259
/// local container driver.
248260
///

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,18 @@ fn resolve_docker_identity_from_accounts(
630630
requested_user
631631
};
632632
if user_selector.is_empty() {
633-
return Err(Status::failed_precondition(
634-
"the pinned image defaults to root; configure a non-root process.run_as_user",
635-
));
633+
// The image declares no USER (e.g. a plain Alpine base) and the policy
634+
// requested none. Synthesize a numeric non-root identity instead of
635+
// rejecting, matching the Podman driver's USER-less default and the
636+
// numeric-identity behavior of the Kubernetes and VM drivers.
637+
return ResolvedWorkloadIdentity::new(
638+
openshell_core::sandbox_env::DEFAULT_SANDBOX_UID,
639+
openshell_core::sandbox_env::DEFAULT_SANDBOX_GID,
640+
Vec::new(),
641+
"default".to_string(),
642+
image.id.clone(),
643+
)
644+
.map_err(|error| Status::failed_precondition(error.to_string()));
636645
}
637646
let (uid, passwd_entry) = resolve_numeric_or_named_user(user_selector, &passwd)?;
638647
let username = passwd_entry.map(|entry| entry.name.as_str());

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -593,18 +593,37 @@ fn build_env(
593593
// hostname could otherwise present a certificate for a name they control
594594
// and intercept the sandbox JWT.
595595
env.remove(openshell_core::sandbox_env::GATEWAY_TLS_SERVER_NAME);
596-
env.insert(
597-
openshell_core::sandbox_env::OCI_IMAGE_USER.into(),
598-
oci_user.to_string(),
599-
);
600-
env.insert(
601-
openshell_core::sandbox_env::SANDBOX_UID.into(),
602-
String::new(),
603-
);
604-
env.insert(
605-
openshell_core::sandbox_env::SANDBOX_GID.into(),
606-
String::new(),
607-
);
596+
if oci_user.is_empty() {
597+
// The image declares no OCI USER (e.g. a plain Alpine base). Assign a
598+
// numeric non-root identity like the Kubernetes and VM drivers so the
599+
// supervisor synthesizes the account instead of rejecting the image.
600+
env.insert(
601+
openshell_core::sandbox_env::OCI_IMAGE_USER.into(),
602+
String::new(),
603+
);
604+
env.insert(
605+
openshell_core::sandbox_env::SANDBOX_UID.into(),
606+
openshell_core::sandbox_env::DEFAULT_SANDBOX_UID.to_string(),
607+
);
608+
env.insert(
609+
openshell_core::sandbox_env::SANDBOX_GID.into(),
610+
openshell_core::sandbox_env::DEFAULT_SANDBOX_GID.to_string(),
611+
);
612+
} else {
613+
// The image declares a USER; preserve the OCI resolution path.
614+
env.insert(
615+
openshell_core::sandbox_env::OCI_IMAGE_USER.into(),
616+
oci_user.to_string(),
617+
);
618+
env.insert(
619+
openshell_core::sandbox_env::SANDBOX_UID.into(),
620+
String::new(),
621+
);
622+
env.insert(
623+
openshell_core::sandbox_env::SANDBOX_GID.into(),
624+
String::new(),
625+
);
626+
}
608627

609628
// 4. Gateway-minted sandbox JWT. Keep the raw bearer out of container
610629
// metadata; the supervisor reads it from a driver-owned bind mount.

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ pub fn resolve_identity(
8787
} else {
8888
requested_group
8989
};
90+
if user.is_empty() && group.is_empty() {
91+
// The image declares no OCI USER (e.g. a plain Alpine base) and the
92+
// policy requested no identity. Synthesize a numeric non-root identity
93+
// instead of rejecting the image, matching Docker, Kubernetes, and VM.
94+
return ResolvedWorkloadIdentity::new(
95+
openshell_core::sandbox_env::DEFAULT_SANDBOX_UID,
96+
openshell_core::sandbox_env::DEFAULT_SANDBOX_GID,
97+
Vec::new(),
98+
"default".into(),
99+
image_id.into(),
100+
)
101+
.map_err(invalid);
102+
}
90103
let account = accounts
91104
.iter()
92105
.find(|(name, uid, _)| *name == user || user.parse::<u32>().ok() == Some(*uid));
@@ -361,10 +374,30 @@ mod tests {
361374
assert_eq!(identity.supplementary_gids, vec![2000]);
362375
assert_eq!(identity.resource_digest, "sha256:pinned");
363376
assert!(resolve_identity(&sandbox, "sha256:pinned", "root", passwd, groups).is_err());
364-
assert!(resolve_identity(&sandbox, "sha256:pinned", "", passwd, groups).is_err());
365377
assert!(resolve_identity(&sandbox, "sha256:pinned", "2000", passwd, groups).is_err());
366378
}
367379

380+
#[test]
381+
fn identity_uses_numeric_default_for_userless_image() {
382+
let identity = resolve_identity(
383+
&DriverSandbox::default(),
384+
"sha256:pinned",
385+
"",
386+
b"root:x:0:0:root:/root:/bin/sh\n",
387+
b"root:x:0:\n",
388+
)
389+
.unwrap();
390+
391+
assert_eq!(
392+
(identity.uid, identity.gid),
393+
(
394+
openshell_core::sandbox_env::DEFAULT_SANDBOX_UID,
395+
openshell_core::sandbox_env::DEFAULT_SANDBOX_GID,
396+
)
397+
);
398+
assert_eq!(identity.source, "default");
399+
}
400+
368401
fn files(bytes: &[u8]) -> BTreeMap<PathBuf, Vec<u8>> {
369402
tar::Archive::new(bytes)
370403
.entries()

0 commit comments

Comments
 (0)