From b7505c3c9ccda5f0b4e366f74c1d1866dce81609 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Feb 2024 00:45:07 -0800 Subject: [PATCH] Remove fopen usage in magiskinit --- native/src/base/files.cpp | 17 +++++++++++++---- native/src/sepolicy/policydb.cpp | 10 +++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/native/src/base/files.cpp b/native/src/base/files.cpp index e91404470..d3a85aa08 100644 --- a/native/src/base/files.cpp +++ b/native/src/base/files.cpp @@ -75,9 +75,14 @@ void file_readline(bool trim, FILE *fp, const function &fn) { } void file_readline(bool trim, const char *file, const function &fn) { - if (auto fp = open_file(file, "re")) - file_readline(trim, fp.get(), fn); + int fd = xopen(file, O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + auto fp = fdopen(fd, "re"); + file_readline(trim, fp, fn); + fclose(fp); + } } + void file_readline(const char *file, const function &fn) { file_readline(false, file, fn); } @@ -96,8 +101,12 @@ void parse_prop_file(FILE *fp, const function &f } void parse_prop_file(const char *file, const function &fn) { - if (auto fp = open_file(file, "re")) - parse_prop_file(fp.get(), fn); + int fd = xopen(file, O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + auto fp = fdopen(fd, "re"); + parse_prop_file(fp, fn); + fclose(fp); + } } std::vector parse_mount_info(const char *pid) { diff --git a/native/src/sepolicy/policydb.cpp b/native/src/sepolicy/policydb.cpp index a3d81c41e..30d7f89be 100644 --- a/native/src/sepolicy/policydb.cpp +++ b/native/src/sepolicy/policydb.cpp @@ -103,7 +103,8 @@ sepolicy *sepolicy::from_file(const char *file) { policy_file_t pf; policy_file_init(&pf); - auto fp = xopen_file(file, "re"); + int fd = xopen(file, O_RDONLY | O_CLOEXEC); + auto fp = make_file(fdopen(fd, "re")); pf.fp = fp.get(); pf.type = PF_USE_STDIO; @@ -123,6 +124,7 @@ sepolicy *sepolicy::compile_split() { cil_db_t *db = nullptr; sepol_policydb_t *pdb = nullptr; FILE *f; + int fd; int policy_ver; const char *cil_file; #if MAGISK_DEBUG @@ -148,13 +150,15 @@ sepolicy *sepolicy::compile_split() { cil_set_target_platform(db, SEPOL_TARGET_SELINUX); cil_set_attrs_expand_generated(db, 1); - f = xfopen(SELINUX_VERSION, "re"); + fd = xopen(SELINUX_VERSION, O_RDONLY | O_CLOEXEC); + f = fdopen(fd, "re"); fscanf(f, "%d", &policy_ver); fclose(f); cil_set_policy_version(db, policy_ver); // Get mapping version - f = xfopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", "re"); + fd = xopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", O_RDONLY | O_CLOEXEC); + f = fdopen(fd, "re"); fscanf(f, "%s", plat_ver); fclose(f);