From 0783f3d5b6d46d0a0ed8897de4c8027838af1688 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 13 Mar 2021 22:13:39 +0800 Subject: [PATCH] Fix mount rules dir close #4006 --- native/jni/init/mount.cpp | 12 ++++++++---- native/jni/init/rootdir.cpp | 4 ++-- native/jni/utils/xwrap.cpp | 8 ++++++++ native/jni/utils/xwrap.hpp | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index 1776252e2..d5d235228 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -234,16 +234,20 @@ void MagiskInit::mount_rules_dir(const char *dev_base, const char *mnt_base) { goto cache; strcpy(p, "/data/unencrypted"); - if (access(path, F_OK) == 0) { + if (xaccess(path, F_OK) == 0) { // FBE, need to use an unencrypted path custom_rules_dir = path + "/magisk"s; } else { // Skip if /data/adb does not exist - strcpy(p, "/data/adb"); - if (access(path, F_OK) != 0) + strcpy(p, SECURE_DIR); + if (xaccess(path, F_OK) != 0) return; + strcpy(p, MODULEROOT); + if (xaccess(path, F_OK) != 0) { + goto cache; + } // Unencrypted, directly use module paths - custom_rules_dir = string(mnt_base) + MODULEROOT; + custom_rules_dir = string(path); } goto success; diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index 0ee1f47cf..daab2e164 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -100,10 +100,10 @@ bool MagiskInit::patch_sepolicy(const char *file) { // Custom rules if (!custom_rules_dir.empty()) { - if (auto dir = open_dir(custom_rules_dir.data())) { + if (auto dir = xopen_dir(custom_rules_dir.data())) { for (dirent *entry; (entry = xreaddir(dir.get()));) { auto rule = custom_rules_dir + "/" + entry->d_name + "/sepolicy.rule"; - if (access(rule.data(), R_OK) == 0) { + if (xaccess(rule.data(), R_OK) == 0) { LOGD("Loading custom sepolicy patch: [%s]\n", rule.data()); sepol->load_rule_file(rule.data()); } diff --git a/native/jni/utils/xwrap.cpp b/native/jni/utils/xwrap.cpp index 51588a2dc..5afe65e33 100644 --- a/native/jni/utils/xwrap.cpp +++ b/native/jni/utils/xwrap.cpp @@ -272,6 +272,14 @@ int xpthread_create(pthread_t *thread, const pthread_attr_t *attr, return errno; } +int xaccess(const char *path, int mode) { + int ret = access(path, mode); + if (ret < 0) { + PLOGE("access %s", path); + } + return ret; +} + int xstat(const char *pathname, struct stat *buf) { int ret = stat(pathname, buf); if (ret < 0) { diff --git a/native/jni/utils/xwrap.hpp b/native/jni/utils/xwrap.hpp index af6ec4c19..a396362dd 100644 --- a/native/jni/utils/xwrap.hpp +++ b/native/jni/utils/xwrap.hpp @@ -33,6 +33,7 @@ ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags); ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags); int xpthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); +int xaccess(const char *path, int mode); int xstat(const char *pathname, struct stat *buf); int xlstat(const char *pathname, struct stat *buf); int xfstat(int fd, struct stat *buf);