Skip to content

Commit

Permalink
Check device tree fstab entries are compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
canyie authored and topjohnwu committed Apr 2, 2022
1 parent d8718d8 commit 3f840f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion native/jni/init/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MagiskInit : public BaseInit {
#endif

void patch_sepolicy(const char *file);
void hijack_sepolicy();
bool hijack_sepolicy();
void setup_tmp(const char *path);
void patch_rw_root();
public:
Expand Down
8 changes: 2 additions & 6 deletions native/jni/init/rootdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ void SARBase::patch_ro_root() {
}
}

if (access(SPLIT_PLAT_CIL, F_OK) != 0 && access("/sepolicy", F_OK) == 0) {
if ((access(SPLIT_PLAT_CIL, F_OK) != 0 && access("/sepolicy", F_OK) == 0) || !hijack_sepolicy()) {
patch_sepolicy(ROOTOVL "/sepolicy");
} else {
hijack_sepolicy();
}

// Mount rootdir
Expand Down Expand Up @@ -309,10 +307,8 @@ void MagiskInit::patch_rw_root() {
}
}

if (!treble && access("/sepolicy", F_OK) == 0) {
if ((!treble && access("/sepolicy", F_OK) == 0) || !hijack_sepolicy()) {
patch_sepolicy("/sepolicy");
} else {
hijack_sepolicy();
}

chdir("/");
Expand Down
22 changes: 14 additions & 8 deletions native/jni/init/selinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void MagiskInit::patch_sepolicy(const char *file) {
#define MOCK_BLOCKING SELINUXMOCK "/blocking"
#define REAL_SELINUXFS SELINUXMOCK "/fs"

void MagiskInit::hijack_sepolicy() {
bool MagiskInit::hijack_sepolicy() {
const char *blocking_target;
string actual_content;

Expand All @@ -60,7 +60,7 @@ void MagiskInit::hijack_sepolicy() {
} else {
// Error, should never happen
LOGE("! Cannot find plat_file_contexts\n");
return;
return false;
}
actual_content = full_read(blocking_target);

Expand Down Expand Up @@ -91,19 +91,25 @@ void MagiskInit::hijack_sepolicy() {
// and let the original init mount selinuxfs for us
// This only happens on Android 8.0 - 9.0

// Preserve sysfs and procfs for hijacking
mount_list.erase(std::remove_if(
mount_list.begin(), mount_list.end(),
[](const string &s) { return s == "/proc" || s == "/sys"; }), mount_list.end());

// Remount procfs with proper options
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");

char buf[4096];
snprintf(buf, sizeof(buf), "%s/fstab/compatible", config->dt_dir);
dt_compat = full_read(buf);

if (dt_compat.empty()) {
// Device does not do early mount and apparently use monolithic policy
return false;
}

LOGD("Hijack [%s]\n", buf);

// Preserve sysfs and procfs for hijacking
mount_list.erase(std::remove_if(
mount_list.begin(), mount_list.end(),
[](const string &s) { return s == "/proc" || s == "/sys"; }), mount_list.end());

mkfifo(MOCK_COMPAT, 0444);
xmount(MOCK_COMPAT, buf, nullptr, MS_BIND, nullptr);
} else {
Expand All @@ -128,7 +134,7 @@ void MagiskInit::hijack_sepolicy() {
// Create a new process waiting for init operations
if (xfork()) {
// In parent, return and continue boot process
return;
return true;
}

if (!dt_compat.empty()) {
Expand Down

0 comments on commit 3f840f5

Please sign in to comment.