Skip to content

Commit a5a9c4b

Browse files
committed
virtiofs: Set SELinux context on readonly mounts
Apply system_u:object_r:usr_t:s0 context to readonly virtiofs mounts to avoid SELinux denials when accessing them as container storage. This allows readonly bind mounts to work correctly with podman. The function was renamed from generate_mount_unit to generate_virtiofs_mount_unit for clarity. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent bfc26db commit a5a9c4b

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

crates/kit/src/credentials.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,19 @@ pub fn guest_path_to_unit_name(guest_path: &str) -> String {
4545
/// Note: systemd automatically creates mount point directories, so DirectoryMode is not needed
4646
///
4747
/// Returns the complete unit file content as a string
48-
pub fn generate_mount_unit(virtiofs_tag: &str, guest_path: &str, readonly: bool) -> String {
49-
let options = if readonly { "Options=ro" } else { "Options=rw" };
48+
pub fn generate_virtiofs_mount_unit(
49+
virtiofs_tag: &str,
50+
guest_path: &str,
51+
readonly: bool,
52+
) -> String {
53+
let options = if readonly {
54+
// Default readonly mounts to usr_t - this helps avoid SELinux
55+
// issues when accessing them as container storage for example.
56+
// TODO don't hardcode this, detect from the environment
57+
"ro,context=system_u:object_r:usr_t:s0"
58+
} else {
59+
"rw"
60+
};
5061

5162
format!(
5263
"[Unit]\n\
@@ -61,7 +72,7 @@ pub fn generate_mount_unit(virtiofs_tag: &str, guest_path: &str, readonly: bool)
6172
What={tag}\n\
6273
Where={path}\n\
6374
Type=virtiofs\n\
64-
{options}\n",
75+
Options={options}\n",
6576
tag = virtiofs_tag,
6677
path = guest_path,
6778
options = options
@@ -82,7 +93,7 @@ pub fn smbios_creds_for_mount_unit(
8293
readonly: bool,
8394
) -> Result<Vec<String>> {
8495
let unit_name = guest_path_to_unit_name(guest_path);
85-
let mount_unit_content = generate_mount_unit(virtiofs_tag, guest_path, readonly);
96+
let mount_unit_content = generate_virtiofs_mount_unit(virtiofs_tag, guest_path, readonly);
8697
let encoded_mount = data_encoding::BASE64.encode(mount_unit_content.as_bytes());
8798

8899
let mount_cred =

crates/kit/src/libvirt/run.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,11 @@ fn process_bind_mounts(
882882

883883
// Generate SMBIOS credential for mount unit (without dropin)
884884
let unit_name = crate::credentials::guest_path_to_unit_name(&bind_mount.guest_path);
885-
let mount_unit_content =
886-
crate::credentials::generate_mount_unit(&tag, &bind_mount.guest_path, readonly);
885+
let mount_unit_content = crate::credentials::generate_virtiofs_mount_unit(
886+
&tag,
887+
&bind_mount.guest_path,
888+
readonly,
889+
);
887890
let encoded_mount = data_encoding::BASE64.encode(mount_unit_content.as_bytes());
888891
let mount_cred =
889892
format!("io.systemd.credential.binary:systemd.extra-unit.{unit_name}={encoded_mount}");
@@ -1211,7 +1214,7 @@ fn create_libvirt_domain_from_disk(
12111214
let guest_mount_path = "/run/host-container-storage";
12121215
let unit_name = crate::credentials::guest_path_to_unit_name(guest_mount_path);
12131216
let mount_unit_content =
1214-
crate::credentials::generate_mount_unit("hoststorage", guest_mount_path, true);
1217+
crate::credentials::generate_virtiofs_mount_unit("hoststorage", guest_mount_path, true);
12151218
let encoded_mount = data_encoding::BASE64.encode(mount_unit_content.as_bytes());
12161219
let mount_cred =
12171220
format!("io.systemd.credential.binary:systemd.extra-unit.{unit_name}={encoded_mount}");

crates/kit/src/run_ephemeral.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ pub(crate) async fn run_impl(opts: RunEphemeralOpts) -> Result<()> {
915915
let mount_point = format!("/run/virtiofs-mnt-{}", mount_name_str);
916916
let unit_name = crate::credentials::guest_path_to_unit_name(&mount_point);
917917
let mount_unit_content =
918-
crate::credentials::generate_mount_unit(&tag, &mount_point, is_readonly);
918+
crate::credentials::generate_virtiofs_mount_unit(&tag, &mount_point, is_readonly);
919919
let encoded_mount = data_encoding::BASE64.encode(mount_unit_content.as_bytes());
920920

921921
// Create SMBIOS credential for the mount unit

0 commit comments

Comments
 (0)