chroot: start removing unsafe (#12053)

This commit is contained in:
oech3
2026-04-28 12:51:22 +02:00
committed by GitHub
parent ec72d08bf1
commit 90af16891f
3 changed files with 8 additions and 20 deletions
Generated
+1
View File
@@ -3370,6 +3370,7 @@ version = "0.8.0"
dependencies = [
"clap",
"fluent",
"rustix",
"thiserror 2.0.18",
"uucore",
]
+1
View File
@@ -21,6 +21,7 @@ doctest = false
[dependencies]
clap = { workspace = true }
rustix = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["entries", "fs"] }
fluent = { workspace = true }
+6 -20
View File
@@ -8,16 +8,15 @@ mod error;
use crate::error::ChrootError;
use clap::{Arg, ArgAction, Command};
use std::ffi::{CString, OsStr};
use std::ffi::OsStr;
use std::io::{Error, ErrorKind};
use std::os::unix::prelude::OsStrExt;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process;
use uucore::entries::{Locate, Passwd, grp2gid, usr2gid, usr2uid};
use uucore::error::{UResult, UUsageError};
use uucore::fs::{MissingHandling, ResolveMode, canonicalize};
use uucore::libc::{self, chroot, setgid, setgroups, setuid};
use uucore::libc::{self, setgid, setgroups, setuid};
use uucore::{format_usage, show};
use uucore::translate;
@@ -424,22 +423,9 @@ fn set_context(options: &Options) -> UResult<()> {
}
fn enter_chroot(root: &Path, skip_chdir: bool) -> UResult<()> {
let err = unsafe {
chroot(
CString::new(root.as_os_str().as_bytes().to_vec())
.map_err(|e| ChrootError::CannotEnter("root".into(), e.into()))?
.as_bytes_with_nul()
.as_ptr()
.cast(),
)
};
if err == 0 {
if !skip_chdir {
std::env::set_current_dir("/")?;
}
Ok(())
} else {
Err(ChrootError::CannotEnter(root.into(), Error::last_os_error()).into())
rustix::process::chroot(root).map_err(|e| ChrootError::CannotEnter(root.into(), e.into()))?;
if !skip_chdir {
std::env::set_current_dir("/")?;
}
Ok(())
}