Clear sepolicy rules when disable/remove modules

Co-authored-by: topjohnwu <topjohnwu@gmail.com>
This commit is contained in:
LoveSy 2023-01-19 04:25:44 +08:00 committed by GitHub
parent f562710438
commit 67845f9c21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -779,17 +779,40 @@ void handle_modules() {
collect_modules(true);
}
static int check_rules_dir(char *buf, size_t sz) {
int off = ssprintf(buf, sz, "%s/%s", MAGISKTMP.data(), RULESDIR);
struct stat st1{};
struct stat st2{};
if (xstat(buf, &st1) < 0 || xstat(MODULEROOT, &st2) < 0)
return 0;
if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
return 0;
return off;
}
void disable_modules() {
foreach_module([](int, auto, int modfd) {
char buf[4096];
int off = check_rules_dir(buf, sizeof(buf));
foreach_module([&](int, dirent *entry, int modfd) {
close(xopenat(modfd, "disable", O_RDONLY | O_CREAT | O_CLOEXEC, 0));
if (off) {
ssprintf(buf + off, sizeof(buf) - off, "/%s/%s", entry->d_name, "sepolicy.rule");
unlink(buf);
}
});
}
void remove_modules() {
foreach_module([](int, dirent *entry, int) {
char buf[4096];
int off = check_rules_dir(buf, sizeof(buf));
foreach_module([&](int, dirent *entry, int) {
auto uninstaller = MODULEROOT + "/"s + entry->d_name + "/uninstall.sh";
if (access(uninstaller.data(), F_OK) == 0)
exec_script(uninstaller.data());
if (off) {
ssprintf(buf + off, sizeof(buf) - off, "/%s/%s", entry->d_name, "sepolicy.rule");
unlink(buf);
}
});
rm_rf(MODULEROOT);
}