Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions crates/muvm/src/guest/user.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use std::env;
use std::fs::{self, Permissions};
use std::os::unix::fs::{chown, PermissionsExt as _};
use std::fs;
use std::os::unix::fs::chown;
use std::path::{Path, PathBuf};

use crate::guest::hidpipe::UINPUT_PATH;
use crate::utils::fs::mkdir_mode;
use anyhow::{anyhow, Context, Result};
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{fork, setresgid, setresuid, ForkResult, Gid, Uid, User};

pub fn setup_user(uid: Uid, gid: Gid) -> Result<PathBuf> {
setup_directories(uid, gid)?;

let path = PathBuf::from(format!("/run/user/{uid}"));

mkdir_mode(path.parent().unwrap(), 0o755)?;
mkdir_mode(&path, 0o700)?;

chown(&path, Some(uid.into()), Some(gid.into()))
.with_context(|| format!("Failed to chown {path:?}"))?;

setresgid(gid, gid, Gid::from(0)).context("Failed to setgid")?;
setresuid(uid, uid, Uid::from(0)).context("Failed to setuid")?;

let path = tempfile::Builder::new()
.prefix(&format!("muvm-run-{uid}-"))
.permissions(Permissions::from_mode(0o700))
.tempdir()
.context("Failed to create temp dir for `XDG_RUNTIME_DIR`")?
.into_path();
// SAFETY: Safe if and only if `muvm-guest` program is not multithreaded.
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
env::set_var("XDG_RUNTIME_DIR", &path);
Expand Down
9 changes: 8 additions & 1 deletion crates/muvm/src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;
use std::os::unix::fs::PermissionsExt as _;
use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
Expand All @@ -25,3 +25,10 @@ where

Ok(None)
}

pub fn mkdir_mode<P: AsRef<Path>>(path: P, mode: u32) -> Result<()> {
fs::DirBuilder::new()
.mode(mode)
.create(&path)
.with_context(|| format!("Failed to create {:?}", path.as_ref()))
}
Loading