2023-06-14 22:24:42 +08:00
|
|
|
#![feature(format_args_nl)]
|
2024-03-28 14:11:03 -07:00
|
|
|
#![feature(try_blocks)]
|
2024-07-16 22:58:11 +08:00
|
|
|
#![feature(let_chains)]
|
2025-01-03 11:38:15 -08:00
|
|
|
#![feature(fn_traits)]
|
2025-01-29 20:16:48 +08:00
|
|
|
#![feature(unix_socket_ancillary_data)]
|
|
|
|
#![feature(unix_socket_peek)]
|
2023-05-30 22:23:11 -07:00
|
|
|
#![allow(clippy::missing_safety_doc)]
|
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
use crate::ffi::SuRequest;
|
|
|
|
use crate::socket::Encodable;
|
|
|
|
use base::{libc, Utf8CStr};
|
|
|
|
use cxx::{type_id, ExternType};
|
2025-02-02 02:42:18 +08:00
|
|
|
use daemon::{daemon_entry, MagiskD};
|
2025-02-02 04:30:16 +08:00
|
|
|
use derive::Decodable;
|
2025-01-07 00:53:21 -08:00
|
|
|
use logging::{android_logging, setup_logfile, zygisk_close_logd, zygisk_get_logd, zygisk_logging};
|
2025-02-14 11:24:46 -08:00
|
|
|
use mount::{find_preinit_device, revert_unmount};
|
2023-09-06 15:52:14 -07:00
|
|
|
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
|
2025-01-30 02:13:54 +08:00
|
|
|
use socket::{recv_fd, recv_fds, send_fd, send_fds};
|
2025-02-02 04:30:16 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::mem::ManuallyDrop;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
use std::os::fd::FromRawFd;
|
2025-01-29 20:16:48 +08:00
|
|
|
use zygisk::zygisk_should_load_module;
|
2022-07-05 21:13:09 -07:00
|
|
|
|
2023-05-23 21:30:30 -07:00
|
|
|
#[path = "../include/consts.rs"]
|
|
|
|
mod consts;
|
2023-05-09 18:54:38 -07:00
|
|
|
mod daemon;
|
2025-01-03 11:38:15 -08:00
|
|
|
mod db;
|
2022-07-05 21:13:09 -07:00
|
|
|
mod logging;
|
2024-03-28 14:11:03 -07:00
|
|
|
mod mount;
|
2025-01-09 00:14:08 +08:00
|
|
|
mod package;
|
2023-06-23 20:37:06 +08:00
|
|
|
mod resetprop;
|
2025-01-30 02:13:54 +08:00
|
|
|
mod socket;
|
2025-01-05 00:44:05 -08:00
|
|
|
mod su;
|
2025-01-29 20:16:48 +08:00
|
|
|
mod zygisk;
|
2022-07-01 04:53:41 -07:00
|
|
|
|
2025-02-02 12:57:09 +08:00
|
|
|
#[allow(clippy::needless_lifetimes)]
|
2022-07-01 04:53:41 -07:00
|
|
|
#[cxx::bridge]
|
|
|
|
pub mod ffi {
|
2023-11-17 13:35:50 -08:00
|
|
|
#[repr(i32)]
|
|
|
|
enum RequestCode {
|
|
|
|
START_DAEMON,
|
|
|
|
CHECK_VERSION,
|
|
|
|
CHECK_VERSION_CODE,
|
|
|
|
STOP_DAEMON,
|
|
|
|
|
|
|
|
_SYNC_BARRIER_,
|
|
|
|
|
|
|
|
SUPERUSER,
|
|
|
|
ZYGOTE_RESTART,
|
|
|
|
DENYLIST,
|
|
|
|
SQLITE_CMD,
|
|
|
|
REMOVE_MODULES,
|
|
|
|
ZYGISK,
|
|
|
|
|
|
|
|
_STAGE_BARRIER_,
|
|
|
|
|
|
|
|
POST_FS_DATA,
|
|
|
|
LATE_START,
|
|
|
|
BOOT_COMPLETE,
|
|
|
|
|
|
|
|
END,
|
|
|
|
}
|
|
|
|
|
2025-01-03 11:38:15 -08:00
|
|
|
enum DbEntryKey {
|
|
|
|
RootAccess,
|
|
|
|
SuMultiuserMode,
|
|
|
|
SuMntNs,
|
|
|
|
DenylistConfig,
|
|
|
|
ZygiskConfig,
|
|
|
|
BootloopCount,
|
|
|
|
SuManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(i32)]
|
|
|
|
enum MntNsMode {
|
|
|
|
Global,
|
|
|
|
Requester,
|
|
|
|
Isolate,
|
|
|
|
}
|
|
|
|
|
2025-01-05 00:44:05 -08:00
|
|
|
#[repr(i32)]
|
|
|
|
enum SuPolicy {
|
|
|
|
Query,
|
|
|
|
Deny,
|
|
|
|
Allow,
|
|
|
|
}
|
|
|
|
|
2025-01-26 19:41:34 +08:00
|
|
|
struct ModuleInfo {
|
|
|
|
name: String,
|
|
|
|
z32: i32,
|
|
|
|
z64: i32,
|
|
|
|
}
|
|
|
|
|
2025-01-29 20:16:48 +08:00
|
|
|
#[repr(i32)]
|
|
|
|
enum ZygiskRequest {
|
|
|
|
GetInfo,
|
|
|
|
ConnectCompanion,
|
|
|
|
GetModDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(u32)]
|
|
|
|
enum ZygiskStateFlags {
|
|
|
|
ProcessGrantedRoot = 0x00000001,
|
|
|
|
ProcessOnDenyList = 0x00000002,
|
|
|
|
DenyListEnforced = 0x40000000,
|
|
|
|
ProcessIsMagiskApp = 0x80000000,
|
|
|
|
}
|
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
#[derive(Decodable)]
|
|
|
|
struct SuRequest {
|
|
|
|
target_uid: i32,
|
|
|
|
target_pid: i32,
|
|
|
|
login: bool,
|
|
|
|
keep_env: bool,
|
|
|
|
shell: String,
|
|
|
|
command: String,
|
|
|
|
context: String,
|
|
|
|
gids: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SuAppRequest<'a> {
|
|
|
|
uid: i32,
|
|
|
|
pid: i32,
|
|
|
|
eval_uid: i32,
|
|
|
|
mgr_pkg: &'a str,
|
|
|
|
mgr_uid: i32,
|
|
|
|
request: &'a SuRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C++" {
|
|
|
|
include!("include/resetprop.hpp");
|
|
|
|
|
|
|
|
#[cxx_name = "prop_cb"]
|
|
|
|
type PropCb;
|
|
|
|
unsafe fn get_prop_rs(name: *const c_char, persist: bool) -> String;
|
|
|
|
unsafe fn prop_cb_exec(
|
|
|
|
cb: Pin<&mut PropCb>,
|
|
|
|
name: *const c_char,
|
|
|
|
value: *const c_char,
|
|
|
|
serial: u32,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-01-03 11:38:15 -08:00
|
|
|
unsafe extern "C++" {
|
2025-02-02 04:30:16 +08:00
|
|
|
#[namespace = "rust"]
|
|
|
|
#[cxx_name = "Utf8CStr"]
|
|
|
|
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
|
|
|
|
#[cxx_name = "ucred"]
|
|
|
|
type UCred = crate::UCred;
|
2025-01-03 11:38:15 -08:00
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
include!("include/core.hpp");
|
2025-01-03 11:38:15 -08:00
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
#[cxx_name = "get_magisk_tmp_rs"]
|
|
|
|
fn get_magisk_tmp() -> Utf8CStrRef<'static>;
|
|
|
|
#[cxx_name = "resolve_preinit_dir_rs"]
|
|
|
|
fn resolve_preinit_dir(base_dir: Utf8CStrRef) -> String;
|
|
|
|
fn setup_magisk_env() -> bool;
|
|
|
|
fn check_key_combo() -> bool;
|
|
|
|
fn disable_modules();
|
|
|
|
fn exec_common_scripts(stage: Utf8CStrRef);
|
|
|
|
fn exec_module_scripts(state: Utf8CStrRef, modules: &Vec<ModuleInfo>);
|
|
|
|
fn install_apk(apk: Utf8CStrRef);
|
|
|
|
fn uninstall_pkg(apk: Utf8CStrRef);
|
|
|
|
fn update_deny_flags(uid: i32, process: &str, flags: &mut u32);
|
|
|
|
fn initialize_denylist();
|
|
|
|
fn restore_zygisk_prop();
|
|
|
|
fn switch_mnt_ns(pid: i32) -> i32;
|
|
|
|
fn app_request(req: &SuAppRequest) -> i32;
|
|
|
|
fn app_notify(req: &SuAppRequest, policy: SuPolicy);
|
|
|
|
fn app_log(req: &SuAppRequest, policy: SuPolicy, notify: bool);
|
|
|
|
fn exec_root_shell(client: i32, pid: i32, req: &mut SuRequest, mode: MntNsMode);
|
2025-01-03 11:38:15 -08:00
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
include!("include/sqlite.hpp");
|
|
|
|
|
|
|
|
type sqlite3;
|
2025-01-03 11:38:15 -08:00
|
|
|
type DbValues;
|
|
|
|
type DbStatement;
|
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
fn sqlite3_errstr(code: i32) -> *const c_char;
|
|
|
|
fn open_and_init_db() -> *mut sqlite3;
|
2025-01-03 11:38:15 -08:00
|
|
|
fn get_int(self: &DbValues, index: i32) -> i32;
|
|
|
|
#[cxx_name = "get_str"]
|
|
|
|
fn get_text(self: &DbValues, index: i32) -> &str;
|
|
|
|
fn bind_text(self: Pin<&mut DbStatement>, index: i32, val: &str) -> i32;
|
|
|
|
fn bind_int64(self: Pin<&mut DbStatement>, index: i32, val: i64) -> i32;
|
|
|
|
}
|
|
|
|
|
2022-07-01 04:53:41 -07:00
|
|
|
extern "Rust" {
|
2022-07-05 21:13:09 -07:00
|
|
|
fn android_logging();
|
|
|
|
fn zygisk_logging();
|
2023-11-04 23:59:11 -07:00
|
|
|
fn zygisk_close_logd();
|
|
|
|
fn zygisk_get_logd() -> i32;
|
2025-01-07 00:53:21 -08:00
|
|
|
fn setup_logfile();
|
2024-03-28 14:11:03 -07:00
|
|
|
fn find_preinit_device() -> String;
|
2022-08-08 22:30:03 +08:00
|
|
|
fn revert_unmount(pid: i32);
|
2025-01-29 20:16:48 +08:00
|
|
|
fn zygisk_should_load_module(flags: u32) -> bool;
|
2023-12-26 23:08:06 +08:00
|
|
|
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);
|
2023-09-06 15:52:14 -07:00
|
|
|
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>);
|
2023-12-26 23:08:06 +08:00
|
|
|
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
|
|
|
|
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
|
2025-01-29 21:02:22 +08:00
|
|
|
fn send_fd(socket: i32, fd: i32) -> bool;
|
|
|
|
fn send_fds(socket: i32, fds: &[i32]) -> bool;
|
|
|
|
fn recv_fd(socket: i32) -> i32;
|
|
|
|
fn recv_fds(socket: i32) -> Vec<i32>;
|
2025-02-02 04:30:16 +08:00
|
|
|
unsafe fn write_to_fd(self: &SuRequest, fd: i32);
|
2022-07-05 21:13:09 -07:00
|
|
|
|
2024-12-03 15:51:17 -08:00
|
|
|
#[namespace = "rust"]
|
2023-05-09 18:54:38 -07:00
|
|
|
fn daemon_entry();
|
2024-12-03 15:51:17 -08:00
|
|
|
}
|
2023-05-09 18:54:38 -07:00
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
// Default constructors
|
|
|
|
extern "Rust" {
|
|
|
|
#[Self = SuRequest]
|
|
|
|
#[cxx_name = "New"]
|
|
|
|
fn default() -> SuRequest;
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:51:17 -08:00
|
|
|
// FFI for MagiskD
|
|
|
|
extern "Rust" {
|
2023-05-09 18:54:38 -07:00
|
|
|
type MagiskD;
|
2025-01-05 00:44:05 -08:00
|
|
|
fn is_recovery(&self) -> bool;
|
2025-01-06 02:48:06 -08:00
|
|
|
fn sdk_int(&self) -> i32;
|
2025-01-30 00:47:01 +08:00
|
|
|
fn zygisk_enabled(&self) -> bool;
|
2025-01-05 00:44:05 -08:00
|
|
|
fn boot_stage_handler(&self, client: i32, code: i32);
|
2025-01-29 20:16:48 +08:00
|
|
|
fn zygisk_handler(&self, client: i32);
|
2025-01-30 00:47:01 +08:00
|
|
|
fn zygisk_reset(&self, restore: bool);
|
2025-01-10 09:04:56 +08:00
|
|
|
fn prune_su_access(&self);
|
2025-02-02 04:30:16 +08:00
|
|
|
fn su_daemon_handler(&self, client: i32, cred: &UCred);
|
2025-01-09 00:14:08 +08:00
|
|
|
#[cxx_name = "get_manager"]
|
|
|
|
unsafe fn get_manager_for_cxx(&self, user: i32, ptr: *mut CxxString, install: bool) -> i32;
|
2024-12-03 15:51:17 -08:00
|
|
|
|
2025-01-03 11:38:15 -08:00
|
|
|
fn get_db_setting(&self, key: DbEntryKey) -> i32;
|
|
|
|
#[cxx_name = "set_db_setting"]
|
|
|
|
fn set_db_setting_for_cxx(&self, key: DbEntryKey, value: i32) -> bool;
|
|
|
|
#[cxx_name = "db_exec"]
|
|
|
|
fn db_exec_for_cxx(&self, client_fd: i32);
|
2025-01-05 00:44:05 -08:00
|
|
|
|
2025-02-02 02:42:18 +08:00
|
|
|
#[Self = MagiskD]
|
|
|
|
#[cxx_name = "Get"]
|
|
|
|
fn get() -> &'static MagiskD;
|
2024-12-03 15:51:17 -08:00
|
|
|
}
|
|
|
|
unsafe extern "C++" {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn reboot(self: &MagiskD);
|
2025-02-02 12:57:09 +08:00
|
|
|
fn handle_modules(self: &MagiskD) -> Vec<ModuleInfo>;
|
2023-05-09 18:54:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 04:30:16 +08:00
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct UCred(pub libc::ucred);
|
|
|
|
|
|
|
|
unsafe impl ExternType for UCred {
|
|
|
|
type Id = type_id!("ucred");
|
|
|
|
type Kind = cxx::kind::Trivial;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SuRequest {
|
|
|
|
unsafe fn write_to_fd(&self, fd: i32) {
|
2025-03-06 23:04:02 -08:00
|
|
|
unsafe {
|
|
|
|
let mut w = ManuallyDrop::new(File::from_raw_fd(fd));
|
|
|
|
self.encode(w.deref_mut()).ok();
|
|
|
|
}
|
2025-02-02 04:30:16 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 23:51:30 -07:00
|
|
|
|
2023-06-12 05:59:50 -07:00
|
|
|
pub fn get_prop(name: &Utf8CStr, persist: bool) -> String {
|
2023-05-21 23:51:30 -07:00
|
|
|
unsafe { ffi::get_prop_rs(name.as_ptr(), persist) }
|
|
|
|
}
|