Magisk/native/src/core/daemon.rs

434 lines
14 KiB
Rust
Raw Normal View History

2025-01-06 02:48:06 -08:00
use crate::consts::{MAGISK_FULL_VER, MAIN_CONFIG};
use crate::db::Sqlite3;
use crate::ffi::{get_magisk_tmp, initialize_denylist, DbEntryKey, ModuleInfo, RequestCode};
2025-01-06 02:48:06 -08:00
use crate::get_prop;
2025-01-07 00:53:21 -08:00
use crate::logging::{magisk_logging, start_log_daemon};
use crate::mount::setup_mounts;
2025-01-09 00:14:08 +08:00
use crate::package::ManagerInfo;
2023-11-15 15:44:43 -08:00
use base::libc::{O_CLOEXEC, O_RDONLY};
2025-01-10 09:04:56 +08:00
use base::{
2025-01-29 20:16:48 +08:00
cstr, info, libc, open_fd, warn, BufReadExt, Directory, FsPath, FsPathBuf, LoggedResult,
2025-01-29 21:02:22 +08:00
ReadExt, ResultExt, Utf8CStr, Utf8CStrBufArr, WriteExt,
2025-01-10 09:04:56 +08:00
};
use bit_set::BitSet;
2025-01-29 20:16:48 +08:00
use bytemuck::{bytes_of, bytes_of_mut, Pod, Zeroable};
2025-01-03 11:38:15 -08:00
use std::fs::File;
use std::io;
2025-01-29 20:16:48 +08:00
use std::io::{BufReader, ErrorKind, IoSlice, IoSliceMut, Read, Write};
2025-01-29 21:02:22 +08:00
use std::mem::ManuallyDrop;
use std::os::fd::{FromRawFd, IntoRawFd, OwnedFd, RawFd};
2025-01-29 20:16:48 +08:00
use std::os::unix::net::{AncillaryData, SocketAncillary, UnixStream};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
2025-01-03 11:38:15 -08:00
use std::sync::{Mutex, OnceLock};
2023-06-10 01:40:45 -07:00
2023-05-09 18:54:38 -07:00
// Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
2023-11-17 13:35:50 -08:00
#[repr(u32)]
enum BootState {
PostFsDataDone = (1 << 0),
LateStartDone = (1 << 1),
BootComplete = (1 << 2),
SafeMode = (1 << 3),
}
#[derive(Default)]
#[repr(transparent)]
2023-11-27 17:40:58 +08:00
struct BootStateFlags(u32);
2023-11-17 13:35:50 -08:00
impl BootStateFlags {
fn contains(&self, stage: BootState) -> bool {
2023-11-27 17:40:58 +08:00
(self.0 & stage as u32) != 0
2023-11-17 13:35:50 -08:00
}
2023-11-27 17:40:58 +08:00
fn set(&mut self, stage: BootState) {
self.0 |= stage as u32;
2023-11-17 13:35:50 -08:00
}
}
2025-01-10 09:38:06 +08:00
pub const AID_ROOT: i32 = 0;
pub const AID_SHELL: i32 = 2000;
2025-01-10 09:04:56 +08:00
pub const AID_APP_START: i32 = 10000;
pub const AID_APP_END: i32 = 19999;
2025-01-09 00:14:08 +08:00
pub const AID_USER_OFFSET: i32 = 100000;
pub const fn to_app_id(uid: i32) -> i32 {
uid % AID_USER_OFFSET
}
2025-01-10 09:38:06 +08:00
pub const fn to_user_id(uid: i32) -> i32 {
uid / AID_USER_OFFSET
}
2023-05-09 18:54:38 -07:00
#[derive(Default)]
pub struct MagiskD {
2025-01-03 11:38:15 -08:00
pub sql_connection: Mutex<Option<Sqlite3>>,
2025-01-09 00:14:08 +08:00
pub manager_info: Mutex<ManagerInfo>,
2023-11-27 17:40:58 +08:00
boot_stage_lock: Mutex<BootStateFlags>,
2025-01-29 20:16:48 +08:00
pub module_list: OnceLock<Vec<ModuleInfo>>,
pub zygiskd_sockets: Mutex<(Option<UnixStream>, Option<UnixStream>)>,
pub zygisk_enabled: AtomicBool,
pub zygote_start_count: AtomicU32,
2025-01-06 02:48:06 -08:00
sdk_int: i32,
pub is_emulator: bool,
2023-11-15 15:44:43 -08:00
is_recovery: bool,
2023-05-09 18:54:38 -07:00
}
impl MagiskD {
2023-11-15 15:44:43 -08:00
pub fn is_recovery(&self) -> bool {
self.is_recovery
}
2023-11-17 13:35:50 -08:00
pub fn zygisk_enabled(&self) -> bool {
self.zygisk_enabled.load(Ordering::Acquire)
}
2025-01-06 02:48:06 -08:00
pub fn sdk_int(&self) -> i32 {
self.sdk_int
}
2025-01-26 19:41:34 +08:00
pub fn set_module_list(&self, module_list: Vec<ModuleInfo>) {
self.module_list.set(module_list).ok();
}
pub fn module_list(&self) -> &Vec<ModuleInfo> {
self.module_list.get().unwrap()
}
2025-01-10 09:04:56 +08:00
pub fn app_data_dir(&self) -> &'static Utf8CStr {
2025-01-09 00:14:08 +08:00
if self.sdk_int >= 24 {
2025-01-10 09:04:56 +08:00
cstr!("/data/user_de")
2025-01-09 00:14:08 +08:00
} else {
2025-01-10 09:04:56 +08:00
cstr!("/data/user")
2025-01-09 00:14:08 +08:00
}
}
2025-01-10 09:04:56 +08:00
// app_id = app_no + AID_APP_START
// app_no range: [0, 9999]
pub fn get_app_no_list(&self) -> BitSet {
let mut list = BitSet::new();
let _: LoggedResult<()> = try {
2025-01-10 20:33:33 +08:00
let mut app_data_dir = Directory::open(self.app_data_dir())?;
2025-01-10 09:04:56 +08:00
// For each user
loop {
2025-01-10 20:33:33 +08:00
let entry = match app_data_dir.read()? {
2025-01-10 09:04:56 +08:00
None => break,
2025-01-10 20:33:33 +08:00
Some(e) => e,
};
let mut user_dir = match entry.open_as_dir() {
Err(_) => continue,
Ok(dir) => dir,
};
// For each package
loop {
match user_dir.read()? {
None => break,
Some(e) => {
let attr = e.get_attr()?;
let app_id = to_app_id(attr.st.st_uid as i32);
if (AID_APP_START..=AID_APP_END).contains(&app_id) {
let app_no = app_id - AID_APP_START;
list.insert(app_no as usize);
2025-01-10 09:04:56 +08:00
}
}
}
}
}
};
list
}
2023-11-17 13:35:50 -08:00
pub fn boot_stage_handler(&self, client: i32, code: i32) {
// Make sure boot stage execution is always serialized
2023-11-27 17:40:58 +08:00
let mut state = self.boot_stage_lock.lock().unwrap();
2023-11-17 13:35:50 -08:00
let code = RequestCode { repr: code };
match code {
RequestCode::POST_FS_DATA => {
2023-11-27 17:40:58 +08:00
if check_data() && !state.contains(BootState::PostFsDataDone) {
2024-12-03 15:51:17 -08:00
if self.post_fs_data() {
2023-11-27 17:40:58 +08:00
state.set(BootState::SafeMode);
} else {
self.zygisk_enabled.store(
self.get_db_setting(DbEntryKey::ZygiskConfig) != 0,
Ordering::Release,
);
initialize_denylist();
setup_mounts();
self.handle_modules();
2023-11-27 17:40:58 +08:00
}
state.set(BootState::PostFsDataDone);
2023-11-17 13:35:50 -08:00
}
unsafe { libc::close(client) };
}
RequestCode::LATE_START => {
unsafe { libc::close(client) };
2023-11-27 17:40:58 +08:00
if state.contains(BootState::PostFsDataDone) && !state.contains(BootState::SafeMode)
2023-11-17 13:35:50 -08:00
{
2024-12-03 15:51:17 -08:00
self.late_start();
2023-11-27 17:40:58 +08:00
state.set(BootState::LateStartDone);
2023-11-17 13:35:50 -08:00
}
}
RequestCode::BOOT_COMPLETE => {
unsafe { libc::close(client) };
2024-01-06 04:00:37 +08:00
if state.contains(BootState::PostFsDataDone) {
2023-11-27 17:40:58 +08:00
state.set(BootState::BootComplete);
2024-12-03 15:51:17 -08:00
self.boot_complete()
2023-11-17 13:35:50 -08:00
}
}
_ => {
unsafe { libc::close(client) };
}
}
}
}
2023-05-09 18:54:38 -07:00
pub fn daemon_entry() {
2025-01-07 00:53:21 -08:00
start_log_daemon();
magisk_logging();
info!("Magisk {} daemon started", MAGISK_FULL_VER);
2024-07-19 23:22:08 -07:00
let is_emulator = get_prop(cstr!("ro.kernel.qemu"), false) == "1"
|| get_prop(cstr!("ro.boot.qemu"), false) == "1"
|| get_prop(cstr!("ro.product.device"), false).contains("vsoc");
2023-11-15 15:44:43 -08:00
// Load config status
let mut buf = Utf8CStrBufArr::<64>::new();
let path = FsPathBuf::new(&mut buf)
.join(get_magisk_tmp())
2024-03-26 18:03:40 -07:00
.join(MAIN_CONFIG);
2023-11-15 15:44:43 -08:00
let mut is_recovery = false;
if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file);
file.foreach_props(|key, val| {
if key == "RECOVERYMODE" {
is_recovery = val == "true";
return false;
}
true
});
}
2025-01-06 02:48:06 -08:00
let mut sdk_int = -1;
if let Ok(file) = FsPath::from(cstr!("/system/build.prop")).open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file);
file.foreach_props(|key, val| {
if key == "ro.build.version.sdk" {
sdk_int = val.parse::<i32>().unwrap_or(-1);
return false;
}
true
});
}
if sdk_int < 0 {
// In case some devices do not store this info in build.prop, fallback to getprop
sdk_int = get_prop(cstr!("ro.build.version.sdk"), false)
.parse::<i32>()
.unwrap_or(-1);
}
2025-01-07 00:53:21 -08:00
info!("* Device API level: {}", sdk_int);
2025-01-06 02:48:06 -08:00
let magiskd = MagiskD {
2025-01-06 02:48:06 -08:00
sdk_int,
is_emulator,
2023-11-15 15:44:43 -08:00
is_recovery,
zygote_start_count: AtomicU32::new(1),
2023-11-17 13:35:50 -08:00
..Default::default()
};
2023-05-09 18:54:38 -07:00
MAGISKD.set(magiskd).ok();
}
2023-11-17 13:35:50 -08:00
fn check_data() -> bool {
if let Ok(fd) = open_fd!(cstr!("/proc/mounts"), O_RDONLY | O_CLOEXEC) {
let file = File::from(fd);
let mut mnt = false;
BufReader::new(file).foreach_lines(|line| {
if line.contains(" /data ") && !line.contains("tmpfs") {
mnt = true;
return false;
}
true
});
if !mnt {
return false;
}
let crypto = get_prop(cstr!("ro.crypto.state"), false);
return if !crypto.is_empty() {
if crypto != "encrypted" {
// Unencrypted, we can directly access data
true
} else {
// Encrypted, check whether vold is started
!get_prop(cstr!("init.svc.vold"), false).is_empty()
}
} else {
// ro.crypto.state is not set, assume it's unencrypted
true
};
}
false
}
2023-05-09 18:54:38 -07:00
pub fn get_magiskd() -> &'static MagiskD {
unsafe { MAGISKD.get().unwrap_unchecked() }
2023-05-09 18:54:38 -07:00
}
2025-01-03 11:38:15 -08:00
pub trait IpcRead {
fn ipc_read_int(&mut self) -> io::Result<i32>;
fn ipc_read_string(&mut self) -> io::Result<String>;
2025-01-29 20:16:48 +08:00
fn ipc_read_vec<E: Pod>(&mut self) -> io::Result<Vec<E>>;
2025-01-03 11:38:15 -08:00
}
impl<T: Read> IpcRead for T {
fn ipc_read_int(&mut self) -> io::Result<i32> {
let mut val: i32 = 0;
self.read_pod(&mut val)?;
Ok(val)
}
fn ipc_read_string(&mut self) -> io::Result<String> {
let len = self.ipc_read_int()?;
let mut val = "".to_string();
self.take(len as u64).read_to_string(&mut val)?;
Ok(val)
}
2025-01-29 20:16:48 +08:00
fn ipc_read_vec<E: Pod>(&mut self) -> io::Result<Vec<E>> {
let len = self.ipc_read_int()? as usize;
let mut vec = Vec::new();
let mut val: E = Zeroable::zeroed();
for _ in 0..len {
self.read_pod(&mut val)?;
vec.push(val.clone());
}
Ok(vec)
}
2025-01-03 11:38:15 -08:00
}
pub trait IpcWrite {
fn ipc_write_int(&mut self, val: i32) -> io::Result<()>;
fn ipc_write_string(&mut self, val: &str) -> io::Result<()>;
}
impl<T: Write> IpcWrite for T {
fn ipc_write_int(&mut self, val: i32) -> io::Result<()> {
2025-01-13 12:29:42 +08:00
self.write_pod(&val)
2025-01-03 11:38:15 -08:00
}
fn ipc_write_string(&mut self, val: &str) -> io::Result<()> {
self.ipc_write_int(val.len() as i32)?;
self.write_all(val.as_bytes())
}
}
2025-01-29 20:16:48 +08:00
pub trait UnixSocketExt {
fn send_fds(&mut self, fd: &[RawFd]) -> io::Result<()>;
fn recv_fd(&mut self) -> io::Result<Option<OwnedFd>>;
fn recv_fds(&mut self) -> io::Result<Vec<OwnedFd>>;
}
impl UnixSocketExt for UnixStream {
fn send_fds(&mut self, fds: &[RawFd]) -> io::Result<()> {
match fds.len() {
0 => self.ipc_write_int(-1)?,
len => {
// 4k buffer is reasonable enough
let mut buf = [0u8; 4096];
let mut ancillary = SocketAncillary::new(&mut buf);
if !ancillary.add_fds(fds) {
return Err(ErrorKind::OutOfMemory.into());
}
let fd_count = len as i32;
let iov = IoSlice::new(bytes_of(&fd_count));
self.send_vectored_with_ancillary(&[iov], &mut ancillary)?;
}
};
Ok(())
}
fn recv_fd(&mut self) -> io::Result<Option<OwnedFd>> {
let mut fd_count = 0;
self.peek(bytes_of_mut(&mut fd_count))?;
if fd_count < 1 {
return Ok(None);
}
// 4k buffer is reasonable enough
let mut buf = [0u8; 4096];
let mut ancillary = SocketAncillary::new(&mut buf);
let iov = IoSliceMut::new(bytes_of_mut(&mut fd_count));
self.recv_vectored_with_ancillary(&mut [iov], &mut ancillary)?;
for msg in ancillary.messages() {
if let Ok(msg) = msg {
if let AncillaryData::ScmRights(mut scm_rights) = msg {
// We only want the first one
let fd = if let Some(fd) = scm_rights.next() {
unsafe { OwnedFd::from_raw_fd(fd) }
} else {
return Ok(None);
};
// Close all others
for fd in scm_rights {
unsafe { libc::close(fd) };
}
return Ok(Some(fd));
}
}
}
Ok(None)
}
fn recv_fds(&mut self) -> io::Result<Vec<OwnedFd>> {
let mut fd_count = 0;
// 4k buffer is reasonable enough
let mut buf = [0u8; 4096];
let mut ancillary = SocketAncillary::new(&mut buf);
let iov = IoSliceMut::new(bytes_of_mut(&mut fd_count));
self.recv_vectored_with_ancillary(&mut [iov], &mut ancillary)?;
let mut fds: Vec<OwnedFd> = Vec::new();
for msg in ancillary.messages() {
if let Ok(msg) = msg {
if let AncillaryData::ScmRights(scm_rights) = msg {
fds = scm_rights
.map(|fd| unsafe { OwnedFd::from_raw_fd(fd) })
.collect();
}
}
}
if fd_count as usize != fds.len() {
warn!(
"Received unexpected number of fds: expected={} actual={}",
fd_count,
fds.len()
);
}
Ok(fds)
}
}
2025-01-29 21:02:22 +08:00
pub fn send_fd(socket: RawFd, fd: RawFd) -> bool {
let mut socket = ManuallyDrop::new(unsafe { UnixStream::from_raw_fd(socket) });
socket.send_fds(&[fd]).log().is_ok()
}
pub fn send_fds(socket: RawFd, fds: &[RawFd]) -> bool {
let mut socket = ManuallyDrop::new(unsafe { UnixStream::from_raw_fd(socket) });
socket.send_fds(fds).log().is_ok()
}
pub fn recv_fd(socket: RawFd) -> RawFd {
let mut socket = ManuallyDrop::new(unsafe { UnixStream::from_raw_fd(socket) });
socket
.recv_fd()
.log()
.unwrap_or(None)
.map_or(-1, IntoRawFd::into_raw_fd)
}
pub fn recv_fds(socket: RawFd) -> Vec<RawFd> {
let mut socket = ManuallyDrop::new(unsafe { UnixStream::from_raw_fd(socket) });
let fds = socket.recv_fds().log().unwrap_or(Vec::new());
// SAFETY: OwnedFd and RawFd has the same layout
unsafe { std::mem::transmute(fds) }
}