ksud: don't follow link when restore file context

This commit is contained in:
weishu
2023-08-03 11:34:11 +08:00
parent a83390b0ec
commit 278cbef3ec
2 changed files with 7 additions and 9 deletions

View File

@@ -245,7 +245,7 @@ pub fn on_boot_completed() -> Result<()> {
pub fn install() -> Result<()> { pub fn install() -> Result<()> {
ensure_dir_exists(defs::ADB_DIR)?; ensure_dir_exists(defs::ADB_DIR)?;
std::fs::copy("/proc/self/exe", defs::DAEMON_PATH)?; std::fs::copy("/proc/self/exe", defs::DAEMON_PATH)?;
restorecon::setcon(defs::DAEMON_PATH, restorecon::ADB_CON)?; restorecon::lsetfilecon(defs::DAEMON_PATH, restorecon::ADB_CON)?;
// install binary assets // install binary assets
assets::ensure_binaries().with_context(|| "Failed to extract assets")?; assets::ensure_binaries().with_context(|| "Failed to extract assets")?;

View File

@@ -1,3 +1,4 @@
use crate::defs;
use anyhow::Result; use anyhow::Result;
use jwalk::{Parallelism::Serial, WalkDir}; use jwalk::{Parallelism::Serial, WalkDir};
use std::path::Path; use std::path::Path;
@@ -5,15 +6,15 @@ use std::path::Path;
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
use anyhow::{Context, Ok}; use anyhow::{Context, Ok};
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
use extattr::{setxattr, Flags as XattrFlags}; use extattr::{lsetxattr, Flags as XattrFlags};
pub const SYSTEM_CON: &str = "u:object_r:system_file:s0"; pub const SYSTEM_CON: &str = "u:object_r:system_file:s0";
pub const ADB_CON: &str = "u:object_r:adb_data_file:s0"; pub const ADB_CON: &str = "u:object_r:adb_data_file:s0";
const SELINUX_XATTR: &str = "security.selinux"; const SELINUX_XATTR: &str = "security.selinux";
pub fn setcon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> { pub fn lsetfilecon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> {
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
setxattr(&path, SELINUX_XATTR, con, XattrFlags::empty()).with_context(|| { lsetxattr(&path, SELINUX_XATTR, con, XattrFlags::empty()).with_context(|| {
format!( format!(
"Failed to change SELinux context for {}", "Failed to change SELinux context for {}",
path.as_ref().display() path.as_ref().display()
@@ -24,7 +25,7 @@ pub fn setcon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> {
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> { pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
setcon(path, SYSTEM_CON) lsetfilecon(path, SYSTEM_CON)
} }
#[cfg(not(any(target_os = "linux", target_os = "android")))] #[cfg(not(any(target_os = "linux", target_os = "android")))]
@@ -35,10 +36,7 @@ pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
pub fn restore_syscon<P: AsRef<Path>>(dir: P) -> Result<()> { pub fn restore_syscon<P: AsRef<Path>>(dir: P) -> Result<()> {
for dir_entry in WalkDir::new(dir).parallelism(Serial) { for dir_entry in WalkDir::new(dir).parallelism(Serial) {
if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) { if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) {
#[cfg(any(target_os = "linux", target_os = "android"))] setsyscon(&path)?;
setxattr(&path, SELINUX_XATTR, SYSTEM_CON, XattrFlags::empty()).with_context(|| {
format!("Failed to change SELinux context for {}", path.display())
})?;
} }
} }
Ok(()) Ok(())