mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
The existing sepolicy patching strategy looks like this: 1. 2SI: use LD_PRELOAD to hijack `security_load_policy` 2. Split policy: devices using split policy implies it also needs to do early mount, which means fstab is stored in device tree. So we do the following: - Hijack the fstab node in the device tree in sysfs - Wait for init to mount selinuxfs for us - Hijack selinuxfs to intercept sepolicy loading 3. Monolithic policy: directly patch `/sepolicy` Method #1 and #2 both has the magiskinit pre-init daemon handling the sepolicy patching and loading process, while method #3 gives us zero control over sepolicy loading process. Downsides: a. Pre-init daemon bypasses the need to guess which sepolicy init will load, because the original init will literally send the stock sepolicy file directly to us with this approach. b. If we want to add more features/functionalities during the sepolicy patching process, we will leave out devices using method #3 In order to solve these issues, we completely redesign the sepolicy patching strategy for non-2SI devices. Instead of limiting usage of pre-init daemon to early mount devices, we always intercept the sepolicy loading process regardless of the Android version and device setup. This will give us a unified implementation for sepolicy patching, and will make it easier to develop further new features down the line.
101 lines
2.7 KiB
Rust
101 lines
2.7 KiB
Rust
#![feature(format_args_nl)]
|
|
#![feature(once_cell_try)]
|
|
#![feature(try_blocks)]
|
|
#![allow(clippy::missing_safety_doc)]
|
|
|
|
use logging::setup_klog;
|
|
// Has to be pub so all symbols in that crate is included
|
|
pub use magiskpolicy;
|
|
use mount::{is_device_mounted, switch_root};
|
|
use rootdir::{inject_magisk_rc, OverlayAttr};
|
|
|
|
#[path = "../include/consts.rs"]
|
|
mod consts;
|
|
mod getinfo;
|
|
mod init;
|
|
mod logging;
|
|
mod mount;
|
|
mod rootdir;
|
|
mod twostage;
|
|
|
|
#[cxx::bridge]
|
|
pub mod ffi {
|
|
#[derive(Debug)]
|
|
struct KeyValue {
|
|
key: String,
|
|
value: String,
|
|
}
|
|
|
|
struct BootConfig {
|
|
skip_initramfs: bool,
|
|
force_normal_boot: bool,
|
|
rootwait: bool,
|
|
emulator: bool,
|
|
slot: [c_char; 3],
|
|
dt_dir: [c_char; 64],
|
|
fstab_suffix: [c_char; 32],
|
|
hardware: [c_char; 32],
|
|
hardware_plat: [c_char; 32],
|
|
partition_map: Vec<KeyValue>,
|
|
}
|
|
|
|
struct MagiskInit {
|
|
preinit_dev: String,
|
|
mount_list: Vec<String>,
|
|
argv: *mut *mut c_char,
|
|
config: BootConfig,
|
|
overlay_con: Vec<OverlayAttr>,
|
|
}
|
|
|
|
unsafe extern "C++" {
|
|
include!("init.hpp");
|
|
|
|
#[namespace = "rust"]
|
|
#[cxx_name = "Utf8CStr"]
|
|
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
|
|
|
|
unsafe fn magisk_proxy_main(argc: i32, argv: *mut *mut c_char) -> i32;
|
|
fn backup_init() -> Utf8CStrRef<'static>;
|
|
}
|
|
|
|
#[namespace = "rust"]
|
|
extern "Rust" {
|
|
fn setup_klog();
|
|
fn inject_magisk_rc(fd: i32, tmp_dir: Utf8CStrRef);
|
|
fn switch_root(path: Utf8CStrRef);
|
|
fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool;
|
|
}
|
|
|
|
// BootConfig
|
|
extern "Rust" {
|
|
fn print(self: &BootConfig);
|
|
}
|
|
unsafe extern "C++" {
|
|
fn init(self: &mut BootConfig);
|
|
type kv_pairs;
|
|
fn set(self: &mut BootConfig, config: &kv_pairs);
|
|
}
|
|
|
|
// MagiskInit
|
|
extern "Rust" {
|
|
type OverlayAttr;
|
|
fn parse_config_file(self: &mut MagiskInit);
|
|
fn mount_overlay(self: &mut MagiskInit, dest: Utf8CStrRef);
|
|
fn restore_overlay_contexts(self: &MagiskInit);
|
|
}
|
|
unsafe extern "C++" {
|
|
// Used in Rust
|
|
fn mount_system_root(self: &mut MagiskInit) -> bool;
|
|
fn patch_rw_root(self: &mut MagiskInit);
|
|
fn patch_ro_root(self: &mut MagiskInit);
|
|
|
|
// Used in C++
|
|
unsafe fn setup_tmp(self: &MagiskInit, path: *const c_char);
|
|
fn collect_devices(self: &MagiskInit);
|
|
fn mount_preinit_dir(self: &MagiskInit);
|
|
unsafe fn find_block(self: &MagiskInit, partname: *const c_char) -> u64;
|
|
fn handle_sepolicy(self: &mut MagiskInit);
|
|
unsafe fn patch_fissiond(self: &mut MagiskInit, tmp_path: *const c_char);
|
|
}
|
|
}
|