8000 Fix /etc/passwd root home to match sandbox HOME by bryan-minimal · Pull Request #113 · gominimal/minimal · GitHub
[go: up one dir, main page]

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
26 changes: 23 additions & 3 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ pub fn synth_dns_config(p: &Path) -> Result<(), io::Error> {
std::fs::write(p.join("etc").join("resolv.conf"), format!("{}", conf))
}

pub fn synth_user_group_config(p: &Path, username: &str) -> Result<(), io::Error> {
pub fn synth_user_group_config(p: &Path, username: &str, home: &str) -> Result<(), io::Error> {
std::fs::create_dir_all(p.join("etc"))?;
std::fs::write(
p.join("etc").join("passwd"),
format!(
"root:x:0:0:root:/root:/usr/bin/bash\n{}:x:1000:1000:User,,,:/home/{}:/bin/bash",
username, username
"root:x:0:0:root:/root:/usr/bin/bash\n{}:x:1000:1000:User,,,:{}:/bin/bash",
username, home
),
)?;
std::fs::write(
Expand Down Expand Up @@ -619,3 +619,23 @@ pub fn default_parallelism() -> usize {
.map(|m| (m as usize).min(para_by_cpu))
.unwrap_or(para_by_cpu)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn synth_user_group_config_sets_home_on_uid_1000() {
let tmp = tempfile::tempdir().unwrap();
synth_user_group_config(tmp.path(), "build", "/host/Users/bryan").unwrap();

let passwd = std::fs::read_to_string(tmp.path().join("etc/passwd")).unwrap();
// uid 1000 (the runtime uid) gets the passed-in home so user.Current()
// returns a path that matches the HOME env var.
assert!(
passwd.contains("build:x:1000:1000:User,,,:/host/Users/bryan:"),
"build line missing or wrong: {}",
passwd
);
}
}
10 changes: 8 additions & 2 deletions crates/sandbox2/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,15 @@ impl Config {
common::synth_dns_config(&sd)
.map_err(|e| Error::IO("synthesizing DNS configuration", sd.clone(), e))?;
}
let home = match &self.wd {
WdSetup::Isolated { .. } => "/state/home".to_string(),
WdSetup::BoundDir { .. } => std::env::home_dir()
.and_then(|p| p.to_str().map(String::from))
.unwrap_or_else(|| "/state/home".to_string()),
};
match &self.username {
Some(n) => common::synth_user_group_config(&sd, n),
None => common::synth_user_group_config(&sd, "build"),
Some(n) => common::synth_user_group_config(&sd, n, &home),
None => common::synth_user_group_config(&sd, "build", &home),
}
.map_err(|e| Error::IO("synthesizing user/group configuration", sd, e))?;

Expand Down
0