Skip to content

Commit 9a776c2

Browse files
authored
Revert "Use rootfs for magisktmp if possible"
1 parent 363566d commit 9a776c2

File tree

5 files changed

+33
-120
lines changed

5 files changed

+33
-120
lines changed

native/src/core/mount.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,7 @@ pub fn revert_unmount(pid: i32) {
221221

222222
// Unmount Magisk tmpfs and mounts from module files
223223
for info in parse_mount_info("self") {
224-
if info.source == "magisk"
225-
|| info.root.starts_with("/adb/modules")
226-
|| (info.fs_type == "rootfs" && info.root.starts_with("/magisk"))
227-
{
224+
if info.source == "magisk" || info.root.starts_with("/adb/modules") {
228225
targets.push(info.target);
229226
}
230227
}

native/src/init/init.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::ffi::{BootConfig, MagiskInit, backup_init, magisk_proxy_main};
22
use crate::logging::setup_klog;
3-
use crate::mount::{is_rootfs, occupy, unoccupy};
3+
use crate::mount::is_rootfs;
44
use crate::twostage::hexpatch_init_for_second_stage;
55
use base::libc::{basename, getpid, mount, umask};
6-
use base::nix::mount::MsFlags;
7-
use base::{LibcReturn, LoggedResult, ResultExt, Utf8CStr, cstr, info, nix, raw_cstr};
6+
use base::{LibcReturn, LoggedResult, ResultExt, cstr, info, raw_cstr};
87
use std::ffi::{CStr, c_char};
98
use std::ptr::null;
109

@@ -32,7 +31,7 @@ impl MagiskInit {
3231

3332
fn first_stage(&self) {
3433
info!("First Stage Init");
35-
let rootfs_magisktmp = self.prepare_data(true);
34+
self.prepare_data();
3635

3736
if !cstr!("/sdcard").exists() && !cstr!("/first_stage_ramdisk/sdcard").exists() {
3837
self.hijack_init_with_switch_root();
@@ -42,28 +41,11 @@ impl MagiskInit {
4241
// Fallback to hexpatch if /sdcard exists
4342
hexpatch_init_for_second_stage(true);
4443
}
45-
46-
if rootfs_magisktmp {
47-
info!("Occupy /data");
48-
occupy(cstr!("/data"));
49-
}
5044
}
5145

5246
fn second_stage(&mut self) {
5347
info!("Second Stage Init");
5448

55-
if unoccupy(cstr!("/data")) {
56-
nix::mount::mount(
57-
None::<&Utf8CStr>,
58-
cstr!("/data"),
59-
None::<&Utf8CStr>,
60-
MsFlags::MS_REMOUNT,
61-
Some(cstr!("size=100%")),
62-
)
63-
.check_os_err("mount", Some("/data"), Some("tmpfs"))
64-
.log_ok();
65-
}
66-
6749
cstr!("/init").unmount().ok();
6850
cstr!("/system/bin/init").unmount().ok(); // just in case
6951
cstr!("/data/init").remove().ok();
@@ -89,7 +71,7 @@ impl MagiskInit {
8971

9072
fn legacy_system_as_root(&mut self) {
9173
info!("Legacy SAR Init");
92-
self.prepare_data(false);
74+
self.prepare_data();
9375
let is_two_stage = self.mount_system_root();
9476
if is_two_stage {
9577
hexpatch_init_for_second_stage(false);
@@ -100,7 +82,7 @@ impl MagiskInit {
10082

10183
fn rootfs(&mut self) {
10284
info!("RootFS Init");
103-
self.prepare_data(false);
85+
self.prepare_data();
10486
self.restore_ramdisk_init();
10587
self.patch_rw_root();
10688
}

native/src/init/mount.rs

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::ffi::MagiskInit;
2-
use base::WalkResult::{Continue, Skip};
3-
use base::nix::mount::{MntFlags, mount, umount2};
42
use base::{
53
Directory, FsPathBuilder, LibcReturn, LoggedResult, ResultExt, Utf8CStr, cstr, debug, libc,
64
nix, parse_mount_info, raw_cstr,
@@ -64,54 +62,6 @@ pub(crate) fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool {
6462
false
6563
}
6664

67-
pub(crate) fn occupy(path: &Utf8CStr) {
68-
Directory::open(path)
69-
.map(|mut dir| {
70-
dir.pre_order_walk(|entry| {
71-
let mut path = cstr::buf::default();
72-
entry.resolve_path(&mut path)?;
73-
let path = path.as_utf8_cstr();
74-
mount(
75-
Some(path),
76-
path,
77-
None::<&Utf8CStr>,
78-
MsFlags::MS_BIND | MsFlags::MS_RDONLY,
79-
None::<&Utf8CStr>,
80-
)
81-
.check_os_err("occupy", Some(path), None)?;
82-
Ok(Continue)
83-
})
84-
.log_ok();
85-
})
86-
.log_ok();
87-
}
88-
89-
pub(crate) fn unoccupy(path: &Utf8CStr) -> bool {
90-
let mut ok = false;
91-
Directory::open(path)
92-
.map(|mut dir| {
93-
ok = dir
94-
.pre_order_walk(|entry| {
95-
let mut path = cstr::buf::default();
96-
entry.resolve_path(&mut path)?;
97-
let path = path.as_utf8_cstr();
98-
umount2(path, MntFlags::MNT_DETACH).check_os_err(
99-
"unoccupy",
100-
Some(path),
101-
None,
102-
)?;
103-
if entry.is_dir() {
104-
Ok(Skip)
105-
} else {
106-
Ok(Continue)
107-
}
108-
})
109-
.is_ok();
110-
})
111-
.log_ok();
112-
ok
113-
}
114-
11565
const RAMFS_MAGIC: u32 = 0x858458f6;
11666

11767
pub(crate) fn is_rootfs() -> bool {
@@ -123,44 +73,22 @@ pub(crate) fn is_rootfs() -> bool {
12373
}
12474

12575
impl MagiskInit {
126-
pub(crate) fn prepare_data(&self, use_rootfs: bool) -> bool {
76+
pub(crate) fn prepare_data(&self) {
12777
debug!("Setup data tmp");
12878
cstr!("/data").mkdir(0o755).log_ok();
79+
nix::mount::mount(
80+
Some(cstr!("magisk")),
81+
cstr!("/data"),
82+
Some(cstr!("tmpfs")),
83+
MsFlags::empty(),
84+
Some(cstr!("mode=755")),
85+
)
86+
.check_os_err("mount", Some("/data"), Some("tmpfs"))
87+
.log_ok();
12988

130-
let mut rootfs_magisktmp = false;
131-
132-
if use_rootfs {
133-
cstr!("/magisk").mkdir(0o755).log_ok();
134-
rootfs_magisktmp = cstr!("/magisk")
135-
.bind_mount_to(cstr!("/data"), false)
136-
.is_ok();
137-
}
138-
139-
if rootfs_magisktmp {
140-
cstr!("/init")
141-
.rename_to(cstr!("/magisk/magiskinit"))
142-
.log_ok();
143-
cstr!("/.backup").copy_to(cstr!("/magisk/.backup")).ok();
144-
cstr!("/overlay.d")
145-
.rename_to(cstr!("/magisk/overlay.d"))
146-
.ok();
147-
} else {
148-
nix::mount::mount(
149-
Some(cstr!("magisk")),
150-
cstr!("/data"),
151-
Some(cstr!("tmpfs")),
152-
MsFlags::empty(),
153-
Some(cstr!("mode=755")),
154-
)
155-
.check_os_err("mount", Some("/data"), Some("tmpfs"))
156-
.log_ok();
157-
158-
cstr!("/init").copy_to(cstr!("/data/magiskinit")).ok();
159-
cstr!("/.backup").copy_to(cstr!("/data/.backup")).ok();
160-
cstr!("/overlay.d").copy_to(cstr!("/data/overlay.d")).ok();
161-
}
162-
163-
rootfs_magisktmp
89+
cstr!("/init").copy_to(cstr!("/data/magiskinit")).ok();
90+
cstr!("/.backup").copy_to(cstr!("/data/.backup")).ok();
91+
cstr!("/overlay.d").copy_to(cstr!("/data/overlay.d")).ok();
16492
}
16593

16694
pub(crate) fn exec_init(&mut self) {

native/src/init/twostage.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ impl MagiskInit {
8484
.log_ok();
8585
debug!("Symlink /storage/self/primary -> /system/system/bin/init");
8686
}
87+
cstr!("/init").rename_to(cstr!("/sdcard")).log_ok();
8788

88-
// Binding mounting from rootfs is not supported before Linux 3.12
89-
cstr!("/sdcard")
90-
.create(OFlag::O_RDONLY | OFlag::O_CLOEXEC, 0)
91-
.log_ok();
92-
cstr!("/data/magiskinit")
89+
// First try to mount magiskinit from rootfs to workaround Samsung RKP
90+
if cstr!("/sdcard")
9391
.bind_mount_to(cstr!("/sdcard"), false)
94-
.log_ok();
95-
debug!("Bind mount /data/magiskinit -> /sdcard");
92+
.is_ok()
93+
{
94+
debug!("Bind mount /sdcard -> /sdcard");
95+
} else {
96+
// Binding mounting from rootfs is not supported before Linux 3.12
97+
cstr!("/data/magiskinit")
98+
.bind_mount_to(cstr!("/sdcard"), false)
99+
.log_ok();
100+
debug!("Bind mount /data/magiskinit -> /sdcard");
101+
}
96102
}
97103
}

native/src/sepolicy/rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl SePolicy {
110110
allow(["kernel"], ["rootfs", "tmpfs"], ["chr_file"], ["write"]);
111111

112112
// Allow magiskinit daemon to handle mock selinuxfs
113-
allow(["kernel"], ["rootfs", "tmpfs"], ["fifo_file"], ["open", "read", "write"]);
113+
allow(["kernel"], ["tmpfs"], ["fifo_file"], ["open", "read", "write"]);
114114

115115
// For relabelling files
116116
allow(["rootfs"], ["labeledfs", "tmpfs"], ["filesystem"], ["associate"]);

0 commit comments

Comments
 (0)