diff --git a/native/jni/init/getinfo.cpp b/native/jni/init/getinfo.cpp index cb4be3278..5d6734eac 100644 --- a/native/jni/init/getinfo.cpp +++ b/native/jni/init/getinfo.cpp @@ -178,3 +178,10 @@ void load_kernel_info(cmdline *cmd) { LOGD("hardware=[%s]\n", cmd->hardware); LOGD("hardware.platform=[%s]\n", cmd->hardware_plat); } + +bool check_two_stage() { + if (access("/apex", F_OK) == 0) + return true; + auto init = raw_data::mmap_ro("/init"); + return init.find("selinux_setup"); +} diff --git a/native/jni/init/init.cpp b/native/jni/init/init.cpp index a3685a507..82b28ca6b 100644 --- a/native/jni/init/init.cpp +++ b/native/jni/init/init.cpp @@ -40,6 +40,14 @@ int data_holder::patch(str_pairs list) { return count; } +bool data_holder::find(string_view pattern) { + for (uint8_t *p = buf, *eof = buf + sz; p < eof; ++p) { + if (memcmp(p, pattern.data(), pattern.length() + 1) == 0) + return true; + } + return false; +} + void data_holder::consume(data_holder &other) { buf = other.buf; sz = other.sz; @@ -47,16 +55,6 @@ void data_holder::consume(data_holder &other) { other.sz = 0; } -template <> -auto_data::~auto_data() { - if (buf) munmap(buf, sz); -} - -template <> -auto_data::~auto_data() { - free(buf); -} - auto_data raw_data::read(int fd) { auto_data data; fd_full_read(fd, data.buf, data.sz); @@ -75,9 +73,11 @@ auto_data raw_data::mmap_rw(const char *name) { return data; } -// Explicit instantiation -template struct auto_data; -template struct auto_data; +auto_data raw_data::mmap_ro(const char *name) { + auto_data data; + ::mmap_ro(name, data.buf, data.sz); + return data; +} static bool unxz(int fd, const uint8_t *buf, size_t size) { uint8_t out[8192]; @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) { // This will also mount /sys and /proc load_kernel_info(&cmd); - bool two_stage = access("/apex", F_OK) == 0; + bool two_stage = check_two_stage(); if (cmd.skip_initramfs) { if (two_stage) init = new SARFirstStageInit(argv, &cmd); diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index a5ba15f0c..efdfb931d 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -21,12 +21,12 @@ struct data_holder { size_t sz = 0; using str_pairs = std::initializer_list>; int patch(str_pairs list); + bool find(std::string_view pattern); protected: void consume(data_holder &other); }; enum data_type { HEAP, MMAP }; - template struct auto_data : public data_holder { auto_data() = default; @@ -35,11 +35,14 @@ struct auto_data : public data_holder { ~auto_data() {} auto_data& operator=(auto_data &&other) { consume(other); return *this; } }; +template <> inline auto_data::~auto_data() { if (buf) munmap(buf, sz); } +template <> inline auto_data::~auto_data() { free(buf); } namespace raw_data { auto_data read(const char *name); auto_data read(int fd); auto_data mmap_rw(const char *name); + auto_data mmap_ro(const char *name); } struct fstab_entry { @@ -59,6 +62,7 @@ struct fstab_entry { #define DEFAULT_DT_DIR "/proc/device-tree/firmware/android" void load_kernel_info(cmdline *cmd); +bool check_two_stage(); int dump_magisk(const char *path, mode_t mode); int magisk_proxy_main(int argc, char *argv[]); void setup_klog();