Commit e9f562a8 authored by LoveSy's avatar LoveSy Committed by John Wu

Fix abuse of `fdopendir`

After `fdopendir`, the fd is no longer usable. Should dup and
make use of RAII
Co-authored-by: 's avatar残页 <31466456+canyie@users.noreply.github.com>
parent 084e0a73
...@@ -15,7 +15,6 @@ using namespace std; ...@@ -15,7 +15,6 @@ using namespace std;
static void restore_syscon(int dirfd) { static void restore_syscon(int dirfd) {
struct dirent *entry; struct dirent *entry;
DIR *dir;
char *con; char *con;
if (fgetfilecon(dirfd, &con) >= 0) { if (fgetfilecon(dirfd, &con) >= 0) {
...@@ -24,11 +23,12 @@ static void restore_syscon(int dirfd) { ...@@ -24,11 +23,12 @@ static void restore_syscon(int dirfd) {
freecon(con); freecon(con);
} }
dir = xfdopendir(dirfd); auto dir = xopen_dir(dirfd);
while ((entry = xreaddir(dir))) { while ((entry = xreaddir(dir.get()))) {
int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC); int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
restore_syscon(fd); restore_syscon(fd);
continue;
} else if (entry->d_type == DT_REG) { } else if (entry->d_type == DT_REG) {
if (fgetfilecon(fd, &con) >= 0) { if (fgetfilecon(fd, &con) >= 0) {
if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0 || strcmp(con, ADB_CON) == 0) if (con[0] == '\0' || strcmp(con, UNLABEL_CON) == 0 || strcmp(con, ADB_CON) == 0)
...@@ -47,16 +47,16 @@ static void restore_syscon(int dirfd) { ...@@ -47,16 +47,16 @@ static void restore_syscon(int dirfd) {
static void restore_magiskcon(int dirfd) { static void restore_magiskcon(int dirfd) {
struct dirent *entry; struct dirent *entry;
DIR *dir;
fsetfilecon(dirfd, MAGISK_CON); fsetfilecon(dirfd, MAGISK_CON);
fchown(dirfd, 0, 0); fchown(dirfd, 0, 0);
dir = xfdopendir(dirfd); auto dir = xopen_dir(dirfd);
while ((entry = xreaddir(dir))) { while ((entry = xreaddir(dir.get()))) {
int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC); int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
restore_magiskcon(fd); restore_magiskcon(fd);
continue;
} else if (entry->d_type) { } else if (entry->d_type) {
fsetfilecon(fd, MAGISK_CON); fsetfilecon(fd, MAGISK_CON);
fchown(fd, 0, 0); fchown(fd, 0, 0);
...@@ -71,12 +71,8 @@ void restorecon() { ...@@ -71,12 +71,8 @@ void restorecon() {
lsetfilecon(SECURE_DIR, ADB_CON); lsetfilecon(SECURE_DIR, ADB_CON);
close(fd); close(fd);
lsetfilecon(MODULEROOT, SYSTEM_CON); lsetfilecon(MODULEROOT, SYSTEM_CON);
fd = xopen(MODULEROOT, O_RDONLY | O_CLOEXEC); restore_syscon(xopen(MODULEROOT, O_RDONLY | O_CLOEXEC));
restore_syscon(fd); restore_magiskcon(xopen(DATABIN, O_RDONLY | O_CLOEXEC));
close(fd);
fd = xopen(DATABIN, O_RDONLY | O_CLOEXEC);
restore_magiskcon(fd);
close(fd);
} }
void restore_tmpcon() { void restore_tmpcon() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment