Skip to content

Commit 60ddd00

Browse files
committed
fix(docker): reject missing bind source paths on legacy binds
Moving user bind mounts from the structured Mount API to the legacy Binds field changed Docker's behavior for missing source directories: the legacy path silently creates them as empty root-owned dirs instead of erroring. Add an explicit Path::exists() check to preserve the fail-fast behavior operators expect. Signed-off-by: Florian Bergmann <[email protected]>
1 parent 087148e commit 60ddd00

2 files changed

Lines changed: 72 additions & 21 deletions

File tree

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,15 @@ fn docker_bind_string(
17991799
) -> Result<String, Status> {
18001800
driver_mounts::validate_absolute_mount_source(source, "bind source")
18011801
.map_err(Status::failed_precondition)?;
1802+
// Legacy `-v` binds silently create missing source directories as empty,
1803+
// root-owned paths. The structured `--mount` API that was used before this
1804+
// change rejected missing sources at container-create time. Preserve that
1805+
// fail-fast behaviour with an explicit existence check.
1806+
if !Path::new(source).exists() {
1807+
return Err(Status::failed_precondition(format!(
1808+
"bind source path does not exist: {source}"
1809+
)));
1810+
}
18021811
driver_mounts::validate_container_mount_target(target).map_err(Status::failed_precondition)?;
18031812
let normalized_target = driver_mounts::normalize_mount_target(target);
18041813

@@ -1824,23 +1833,22 @@ fn docker_driver_mounts(config: &DockerSandboxDriverConfig) -> Result<Vec<Mount>
18241833
config
18251834
.mounts
18261835
.iter()
1827-
.filter(|m| !matches!(m, DockerDriverMountConfig::Bind { .. }))
1828-
.map(docker_mount_from_config)
1836+
.filter_map(|m| docker_mount_from_config(m).transpose())
18291837
.collect()
18301838
}
18311839

1832-
fn docker_mount_from_config(config: &DockerDriverMountConfig) -> Result<Mount, Status> {
1840+
fn docker_mount_from_config(config: &DockerDriverMountConfig) -> Result<Option<Mount>, Status> {
18331841
match config {
18341842
DockerDriverMountConfig::Bind { .. } => {
18351843
// Bind mounts are handled via docker_driver_bind_strings.
1836-
unreachable!("bind mounts are filtered out before reaching docker_mount_from_config")
1844+
Ok(None)
18371845
}
18381846
DockerDriverMountConfig::Volume {
18391847
source,
18401848
target,
18411849
read_only,
18421850
subpath,
1843-
} => Ok(Mount {
1851+
} => Ok(Some(Mount {
18441852
typ: Some(MountTypeEnum::VOLUME),
18451853
source: Some(source.clone()),
18461854
target: Some(target.clone()),
@@ -1850,13 +1858,13 @@ fn docker_mount_from_config(config: &DockerDriverMountConfig) -> Result<Mount, S
18501858
..Default::default()
18511859
}),
18521860
..Default::default()
1853-
}),
1861+
})),
18541862
DockerDriverMountConfig::Tmpfs {
18551863
target,
18561864
options,
18571865
size_bytes,
18581866
mode,
1859-
} => Ok(Mount {
1867+
} => Ok(Some(Mount {
18601868
typ: Some(MountTypeEnum::TMPFS),
18611869
target: Some(target.clone()),
18621870
tmpfs_options: Some(MountTmpfsOptions {
@@ -1875,7 +1883,7 @@ fn docker_mount_from_config(config: &DockerDriverMountConfig) -> Result<Mount, S
18751883
.transpose()?,
18761884
}),
18771885
..Default::default()
1878-
}),
1886+
})),
18791887
DockerDriverMountConfig::Image { .. } => Err(Status::failed_precondition(
18801888
"invalid docker driver_config: docker image mounts are not supported",
18811889
)),

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

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ fn driver_config_rejects_bind_mounts_unless_enabled() {
781781

782782
#[test]
783783
fn build_container_create_body_includes_bind_mounts_when_enabled() {
784+
let bind_src = TempDir::new().unwrap();
785+
let src_path = bind_src.path().to_str().unwrap();
784786
let mut sandbox = test_sandbox();
785787
sandbox
786788
.spec
@@ -792,7 +794,7 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
792794
.driver_config = Some(json_struct(serde_json::json!({
793795
"mounts": [{
794796
"type": "bind",
795-
"source": "/host/path",
797+
"source": src_path,
796798
"target": "/sandbox/host",
797799
"read_only": true
798800
}]
@@ -810,9 +812,10 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
810812
.expect("binds should be set");
811813

812814
// User bind mount appears after the system binds.
815+
let expected = format!("{src_path}:/sandbox/host:ro");
813816
assert!(
814-
binds.iter().any(|b| b == "/host/path:/sandbox/host:ro"),
815-
"expected bind entry '/host/path:/sandbox/host:ro', got {binds:?}"
817+
binds.iter().any(|b| b == &expected),
818+
"expected bind entry '{expected}', got {binds:?}"
816819
);
817820
// Bind mounts must not appear in the structured mounts vec.
818821
let mounts = body.host_config.unwrap().mounts.unwrap_or_default();
@@ -824,6 +827,8 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
824827

825828
#[test]
826829
fn driver_config_defaults_enabled_bind_mounts_to_read_only() {
830+
let bind_src = TempDir::new().unwrap();
831+
let src_path = bind_src.path().to_str().unwrap();
827832
let mut sandbox = test_sandbox();
828833
sandbox
829834
.spec
@@ -835,7 +840,7 @@ fn driver_config_defaults_enabled_bind_mounts_to_read_only() {
835840
.driver_config = Some(json_struct(serde_json::json!({
836841
"mounts": [{
837842
"type": "bind",
838-
"source": "/host/path",
843+
"source": src_path,
839844
"target": "/sandbox/host"
840845
}]
841846
})));
@@ -849,16 +854,17 @@ fn driver_config_defaults_enabled_bind_mounts_to_read_only() {
849854
.binds
850855
.expect("binds should be set");
851856

857+
let expected = format!("{src_path}:/sandbox/host:ro");
852858
assert!(
853-
binds
854-
.iter()
855-
.any(|b| b.contains("/host/path:/sandbox/host:ro")),
859+
binds.iter().any(|b| b == &expected),
856860
"default bind mount should be read-only, got {binds:?}"
857861
);
858862
}
859863

860864
#[test]
861865
fn bind_mount_selinux_shared_label() {
866+
let bind_src = TempDir::new().unwrap();
867+
let src_path = bind_src.path().to_str().unwrap();
862868
let mut sandbox = test_sandbox();
863869
sandbox
864870
.spec
@@ -870,7 +876,7 @@ fn bind_mount_selinux_shared_label() {
870876
.driver_config = Some(json_struct(serde_json::json!({
871877
"mounts": [{
872878
"type": "bind",
873-
"source": "/data/shared",
879+
"source": src_path,
874880
"target": "/sandbox/data",
875881
"read_only": true,
876882
"selinux_label": "shared"
@@ -886,14 +892,17 @@ fn bind_mount_selinux_shared_label() {
886892
.binds
887893
.expect("binds should be set");
888894

895+
let expected = format!("{src_path}:/sandbox/data:ro,z");
889896
assert!(
890-
binds.iter().any(|b| b == "/data/shared:/sandbox/data:ro,z"),
897+
binds.iter().any(|b| b == &expected),
891898
"expected ':ro,z' label, got {binds:?}"
892899
);
893900
}
894901

895902
#[test]
896903
fn bind_mount_selinux_private_label() {
904+
let bind_src = TempDir::new().unwrap();
905+
let src_path = bind_src.path().to_str().unwrap();
897906
let mut sandbox = test_sandbox();
898907
sandbox
899908
.spec
@@ -905,7 +914,7 @@ fn bind_mount_selinux_private_label() {
905914
.driver_config = Some(json_struct(serde_json::json!({
906915
"mounts": [{
907916
"type": "bind",
908-
"source": "/data/exclusive",
917+
"source": src_path,
909918
"target": "/sandbox/data",
910919
"read_only": false,
911920
"selinux_label": "private"
@@ -921,14 +930,17 @@ fn bind_mount_selinux_private_label() {
921930
.binds
922931
.expect("binds should be set");
923932

933+
let expected = format!("{src_path}:/sandbox/data:Z");
924934
assert!(
925-
binds.iter().any(|b| b == "/data/exclusive:/sandbox/data:Z"),
935+
binds.iter().any(|b| b == &expected),
926936
"expected ':Z' label, got {binds:?}"
927937
);
928938
}
929939

930940
#[test]
931941
fn bind_mount_without_selinux_label() {
942+
let bind_src = TempDir::new().unwrap();
943+
let src_path = bind_src.path().to_str().unwrap();
932944
let mut sandbox = test_sandbox();
933945
sandbox
934946
.spec
@@ -940,7 +952,7 @@ fn bind_mount_without_selinux_label() {
940952
.driver_config = Some(json_struct(serde_json::json!({
941953
"mounts": [{
942954
"type": "bind",
943-
"source": "/host/path",
955+
"source": src_path,
944956
"target": "/sandbox/host",
945957
"read_only": false
946958
}]
@@ -955,12 +967,43 @@ fn bind_mount_without_selinux_label() {
955967
.binds
956968
.expect("binds should be set");
957969

970+
let expected = format!("{src_path}:/sandbox/host");
958971
assert!(
959-
binds.iter().any(|b| b == "/host/path:/sandbox/host"),
972+
binds.iter().any(|b| b == &expected),
960973
"expected no options suffix, got {binds:?}"
961974
);
962975
}
963976

977+
#[test]
978+
fn driver_config_rejects_missing_bind_source() {
979+
let mut sandbox = test_sandbox();
980+
sandbox
981+
.spec
982+
.as_mut()
983+
.unwrap()
984+
.template
985+
.as_mut()
986+
.unwrap()
987+
.driver_config = Some(json_struct(serde_json::json!({
988+
"mounts": [{
989+
"type": "bind",
990+
"source": "/no/such/path",
991+
"target": "/sandbox/data"
992+
}]
993+
})));
994+
let mut config = runtime_config();
995+
config.enable_bind_mounts = true;
996+
997+
let err = build_container_create_body(&sandbox, &config).unwrap_err();
998+
999+
assert_eq!(err.code(), tonic::Code::FailedPrecondition);
1000+
assert!(
1001+
err.message().contains("bind source path does not exist"),
1002+
"expected missing-source error, got: {}",
1003+
err.message()
1004+
);
1005+
}
1006+
9641007
#[test]
9651008
fn driver_config_rejects_relative_bind_sources_when_enabled() {
9661009
let mut sandbox = test_sandbox();

0 commit comments

Comments
 (0)