Use nix for libc functions

This commit is contained in:
topjohnwu
2025-09-08 23:59:29 -07:00
parent 17082af438
commit ef4e230258
31 changed files with 457 additions and 475 deletions
+2 -2
View File
@@ -130,7 +130,7 @@ impl MagiskInit {
null(),
)
}
.check_io_err()?;
.check_err()?;
self.mount_list.push("/proc".to_string());
}
if !cstr!("/sys/block").exists() {
@@ -144,7 +144,7 @@ impl MagiskInit {
null(),
)
}
.check_io_err()?;
.check_err()?;
self.mount_list.push("/sys".to_string());
}
+18 -7
View File
@@ -1,8 +1,9 @@
use base::nix::fcntl::OFlag;
use base::{
LOGGER, LogLevel, Logger, SilentLogExt, Utf8CStr, cstr,
libc::{
O_CLOEXEC, O_RDWR, O_WRONLY, S_IFCHR, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO, SYS_dup3,
makedev, mknod, syscall,
O_CLOEXEC, S_IFCHR, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO, SYS_dup3, makedev, mknod,
syscall,
},
raw_cstr,
};
@@ -19,10 +20,14 @@ static mut KMSG: RawFd = -1;
pub fn setup_klog() {
unsafe {
// Shut down first 3 fds
let mut fd = cstr!("/dev/null").open(O_RDWR | O_CLOEXEC).silent();
let mut fd = cstr!("/dev/null")
.open(OFlag::O_RDWR | OFlag::O_CLOEXEC)
.silent();
if fd.is_err() {
mknod(raw_cstr!("/null"), S_IFCHR | 0o666, makedev(1, 3));
fd = cstr!("/null").open(O_RDWR | O_CLOEXEC).silent();
fd = cstr!("/null")
.open(OFlag::O_RDWR | OFlag::O_CLOEXEC)
.silent();
cstr!("/null").remove().ok();
}
if let Ok(ref fd) = fd {
@@ -32,17 +37,23 @@ pub fn setup_klog() {
}
// Then open kmsg fd
let mut fd = cstr!("/dev/kmsg").open(O_WRONLY | O_CLOEXEC).silent();
let mut fd = cstr!("/dev/kmsg")
.open(OFlag::O_WRONLY | OFlag::O_CLOEXEC)
.silent();
if fd.is_err() {
mknod(raw_cstr!("/kmsg"), S_IFCHR | 0o666, makedev(1, 11));
fd = cstr!("/kmsg").open(O_WRONLY | O_CLOEXEC).silent();
fd = cstr!("/kmsg")
.open(OFlag::O_WRONLY | OFlag::O_CLOEXEC)
.silent();
cstr!("/kmsg").remove().ok();
}
KMSG = fd.map(|fd| fd.into_raw_fd()).unwrap_or(-1);
}
// Disable kmsg rate limiting
if let Ok(mut rate) = cstr!("/proc/sys/kernel/printk_devkmsg").open(O_WRONLY | O_CLOEXEC) {
if let Ok(mut rate) =
cstr!("/proc/sys/kernel/printk_devkmsg").open(OFlag::O_WRONLY | OFlag::O_CLOEXEC)
{
writeln!(rate, "on").ok();
}
+3 -3
View File
@@ -44,7 +44,7 @@ pub(crate) fn switch_root(path: &Utf8CStr) {
mounts.insert(info.target);
}
unsafe {
chdir(path.as_ptr()).check_io_err()?;
chdir(path.as_ptr()).check_err()?;
path.move_mount_to(cstr!("/"))?;
chroot(raw_cstr!("."));
}
@@ -88,7 +88,7 @@ impl MagiskInit {
raw_cstr!("mode=755").cast(),
)
}
.check_io_err()
.check_err()
.log_ok();
cstr!("/init").copy_to(cstr!("/data/magiskinit")).log_ok();
@@ -107,7 +107,7 @@ impl MagiskInit {
}
unsafe {
execve(raw_cstr!("/init"), self.argv.cast(), environ.cast())
.check_io_err()
.check_err()
.log_ok();
exit(1);
}
+3 -3
View File
@@ -1,6 +1,6 @@
use crate::consts::{ROOTMNT, ROOTOVL};
use crate::ffi::MagiskInit;
use base::libc::{O_CREAT, O_RDONLY, O_WRONLY};
use base::nix::fcntl::OFlag;
use base::{
BufReadExt, Directory, FsPathBuilder, LoggedResult, ResultExt, Utf8CStr, Utf8CString,
clone_attr, cstr, debug,
@@ -44,7 +44,7 @@ pub struct OverlayAttr(Utf8CString, Utf8CString);
impl MagiskInit {
pub(crate) fn parse_config_file(&mut self) {
if let Ok(fd) = cstr!("/data/.backup/.magisk").open(O_RDONLY) {
if let Ok(fd) = cstr!("/data/.backup/.magisk").open(OFlag::O_RDONLY) {
let mut reader = BufReader::new(fd);
reader.for_each_prop(|key, val| {
if key == "PREINITDEVICE" {
@@ -95,7 +95,7 @@ impl MagiskInit {
let mut mount_list = String::new();
self.mount_impl(cstr!(ROOTOVL), dest, &mut mount_list)
.log_ok();
if let Ok(mut fd) = cstr!(ROOTMNT).create(O_CREAT | O_WRONLY, 0) {
if let Ok(mut fd) = cstr!(ROOTMNT).create(OFlag::O_CREAT | OFlag::O_WRONLY, 0) {
fd.write(mount_list.as_bytes()).log_ok();
}
}
+13 -10
View File
@@ -1,6 +1,7 @@
use crate::consts::{PREINITMIRR, SELINUXMOCK};
use crate::ffi::{MagiskInit, preload_ack, preload_lib, preload_policy, split_plat_cil};
use base::const_format::concatcp;
use base::nix::fcntl::OFlag;
use base::{
BytesExt, LibcReturn, LoggedResult, MappedFile, ResultExt, Utf8CStr, cstr, debug, error, info,
libc, raw_cstr,
@@ -54,7 +55,7 @@ fn mock_fifo(target: &Utf8CStr, mock: &Utf8CStr) -> LoggedResult<()> {
fn mock_file(target: &Utf8CStr, mock: &Utf8CStr) -> LoggedResult<()> {
debug!("Hijack [{}]", target);
drop(mock.create(libc::O_RDONLY, 0o666)?);
drop(mock.create(OFlag::O_RDONLY, 0o666)?);
mock.bind_mount_to(target, false).log()
}
@@ -93,7 +94,9 @@ impl MagiskInit {
let rule_file = cstr!(concatcp!("/data/", PREINITMIRR, "/sepolicy.rule"));
if rule_file.exists() {
debug!("Loading custom sepolicy patch: [{}]", rule_file);
rule_file.open(libc::O_RDONLY)?.read_to_string(&mut rules)?;
rule_file
.open(OFlag::O_RDONLY)?
.read_to_string(&mut rules)?;
}
// Step 0: determine strategy
@@ -153,7 +156,7 @@ impl MagiskInit {
0,
ptr::null(),
)
.check_io_err()?;
.check_err()?;
}
}
@@ -165,7 +168,7 @@ impl MagiskInit {
if !policy_ver.exists() {
// The file does not exist, create one
drop(policy_ver.create(libc::O_RDONLY, 0o666)?);
drop(policy_ver.create(OFlag::O_RDONLY, 0o666)?);
}
// The only purpose of this is to block init's control flow after it mounts
@@ -204,7 +207,7 @@ impl MagiskInit {
mock_fifo(SELINUX_REQPROT, MOCK_REQPROT)?;
// This will unblock init at selinux_android_load_policy() -> set_policy_index().
drop(MOCK_VERSION.open(libc::O_WRONLY)?);
drop(MOCK_VERSION.open(OFlag::O_WRONLY)?);
policy_ver.unmount()?;
@@ -218,7 +221,7 @@ impl MagiskInit {
match strat {
SePatchStrategy::LdPreload => {
// This open will block until preload.so finish writing the sepolicy
let mut ack_fd = preload_ack().open(libc::O_WRONLY)?;
let mut ack_fd = preload_ack().open(OFlag::O_WRONLY)?;
let mut sepol = SePolicy::from_file(preload_policy());
@@ -237,14 +240,14 @@ impl MagiskInit {
}
SePatchStrategy::SelinuxFs => {
// This open will block until init calls security_getenforce().
let mut mock_enforce = MOCK_ENFORCE.open(libc::O_WRONLY)?;
let mut mock_enforce = MOCK_ENFORCE.open(OFlag::O_WRONLY)?;
self.cleanup_and_load(&rules);
// security_getenforce was called, read from real and redirect to mock
let mut data = vec![];
SELINUX_ENFORCE
.open(libc::O_RDONLY)?
.open(OFlag::O_RDONLY)?
.read_to_end(&mut data)?;
mock_enforce.write_all(&data)?;
}
@@ -266,10 +269,10 @@ impl MagiskInit {
// init is blocked on checkreqprot, write to the real node first, then
// unblock init by opening the mock FIFO.
SELINUX_REQPROT
.open(libc::O_WRONLY)?
.open(OFlag::O_WRONLY)?
.write_all("0".as_bytes())?;
let mut v = vec![];
MOCK_REQPROT.open(libc::O_RDONLY)?.read_to_end(&mut v)?;
MOCK_REQPROT.open(OFlag::O_RDONLY)?.read_to_end(&mut v)?;
}
}
+4 -6
View File
@@ -1,8 +1,6 @@
use crate::ffi::MagiskInit;
use base::{
LoggedResult, MappedFile, MutBytesExt, ResultExt, cstr, debug, error,
libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_WRONLY},
};
use base::nix::fcntl::OFlag;
use base::{LoggedResult, MappedFile, MutBytesExt, ResultExt, cstr, debug, error};
use std::io::Write;
pub(crate) fn hexpatch_init_for_second_stage(writable: bool) {
@@ -32,7 +30,7 @@ pub(crate) fn hexpatch_init_for_second_stage(writable: bool) {
let dest = cstr!("/data/init");
let _: LoggedResult<()> = try {
{
let mut fd = dest.create(O_CREAT | O_WRONLY, 0)?;
let mut fd = dest.create(OFlag::O_CREAT | OFlag::O_WRONLY, 0)?;
fd.write_all(init.as_ref())?;
}
let attr = src.follow_link().get_attr()?;
@@ -77,7 +75,7 @@ impl MagiskInit {
.log_ok();
debug!("Symlink /first_stage_ramdisk/storage/self/primary -> /system/system/bin/init");
cstr!("/first_stage_ramdisk/sdcard")
.create(O_RDONLY | O_CREAT | O_CLOEXEC, 0)
.create(OFlag::O_RDONLY | OFlag::O_CREAT | OFlag::O_CLOEXEC, 0)
.log_ok();
} else {
cstr!("/storage/self").mkdirs(0o755).log_ok();