Skip to content

Commit ecb6cf2

Browse files
committed
fix(vmm): restrict swtpm socket to QEMU user
1 parent 00c5103 commit ecb6cf2

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

dstack/vmm/src/app/qemu.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use anyhow::{bail, Context, Result};
3030
use bon::Builder;
3131
use dstack_types::{shared_filenames::HOST_SHARED_DISK_LABEL, KeyProviderKind};
3232
use fs_err as fs;
33+
use nix::unistd::User;
3334
use serde::Serialize;
3435
use supervisor_client::supervisor::ProcessConfig;
3536

@@ -345,14 +346,25 @@ impl VmConfig {
345346
.swtpm_path
346347
.as_ref()
347348
.context("missing swtpm executable for configured socket")?;
349+
let (socket_uid, socket_gid) = if cfg.user.is_empty() {
350+
(unsafe { libc::geteuid() }, unsafe { libc::getegid() })
351+
} else {
352+
let user = User::from_name(&cfg.user)
353+
.context("failed to resolve QEMU user")?
354+
.with_context(|| format!("QEMU user {} does not exist", cfg.user))?;
355+
(user.uid.as_raw(), user.gid.as_raw())
356+
};
348357

349358
let swtpm_args = vec![
350359
"socket".into(),
351360
"--tpm2".into(),
352361
"--tpmstate".into(),
353362
format!("dir={}", prepared.workdir.swtpm_state_dir().display()),
354363
"--ctrl".into(),
355-
format!("type=unixio,path={},mode=0600", socket.display()),
364+
format!(
365+
"type=unixio,path={},mode=0600,uid={socket_uid},gid={socket_gid}",
366+
socket.display()
367+
),
356368
"--flags".into(),
357369
"not-need-init,startup-clear".into(),
358370
];

0 commit comments

Comments
 (0)