From eb87c1355b3783f22fa50f242b36d1e5bd00158c Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 13 Sep 2025 20:25:04 +0800 Subject: [PATCH] Fixed some minor issues that may have existed --- kernel/core_hook.c | 27 ++++++++----- kernel/throne_tracker.c | 84 ++++++++++++++++++++--------------------- 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/kernel/core_hook.c b/kernel/core_hook.c index a80f2855..698534b9 100644 --- a/kernel/core_hook.c +++ b/kernel/core_hook.c @@ -298,19 +298,26 @@ int ksu_handle_rename(struct dentry *old_dentry, struct dentry *new_dentry) return 0; } - // It still monitors changes to certain system files to trigger scans, but does not rely on packages.list. - if (strstr(new_dentry->d_iname, "packages") && - strstr(new_dentry->d_iname, "list")) { - char path[128]; - char *buf = dentry_path_raw(new_dentry, path, sizeof(path)); - if (!IS_ERR(buf) && strstr(buf, "/system/")) { - pr_info("System package change detected: %s -> %s, triggering user scan\n", - old_dentry->d_iname, new_dentry->d_iname); - track_throne(); - } + // /data/system/packages.list.tmp -> /data/system/packages.list + if (strcmp(new_dentry->d_iname, "packages.list")) { return 0; } + char path[128]; + char *buf = dentry_path_raw(new_dentry, path, sizeof(path)); + if (IS_ERR(buf)) { + pr_err("dentry_path_raw failed.\n"); + return 0; + } + + if (!strstr(buf, "/system/packages.list")) { + return 0; + } + pr_info("renameat: %s -> %s, new path: %s\n", old_dentry->d_iname, + new_dentry->d_iname, buf); + + track_throne(); + return 0; } diff --git a/kernel/throne_tracker.c b/kernel/throne_tracker.c index 55297fb8..03125137 100644 --- a/kernel/throne_tracker.c +++ b/kernel/throne_tracker.c @@ -19,8 +19,8 @@ uid_t ksu_manager_uid = KSU_INVALID_UID; #define USER_DATA_BASE_PATH "/data/user_de" -#define USER_DATA_LEGACY_BASE_PATH "/data/user" -#define USER_DATA_PATH_LEN 256 // 256 is enough for /data/user_de/{userid}/ +#define MAX_SUPPORTED_USERS 32 // Supports up to 32 users +#define USER_DATA_PATH_LEN 384 // 384 is enough for /data/user_de/{userid}/ #define MAX_ANDROID_USER_ID 999 #define MIN_ANDROID_USER_ID 0 @@ -165,45 +165,43 @@ struct user_data_context { // Retrieve a list of all active Android user IDs in the system static int get_active_user_ids(uid_t *user_ids, size_t max_users, size_t *found_users) { - struct file *dir_file; - struct dir_context ctx __maybe_unused = {0}; - int ret = 0; - size_t count = 0; - - *found_users = 0; - - dir_file = ksu_filp_open_compat(USER_DATA_BASE_PATH, O_RDONLY, 0); - if (IS_ERR(dir_file)) { - pr_warn("Failed to open %s: %ld, trying legacy path\n", - USER_DATA_BASE_PATH, PTR_ERR(dir_file)); - - // Try the legacy path - dir_file = ksu_filp_open_compat(USER_DATA_LEGACY_BASE_PATH, O_RDONLY, 0); - if (IS_ERR(dir_file)) { - pr_err("Failed to open both user data paths: %ld\n", PTR_ERR(dir_file)); - return PTR_ERR(dir_file); - } - } + struct file *dir_file; + int ret = 0; + size_t count = 0; - struct path path; - for (uid_t user_id = MIN_ANDROID_USER_ID; user_id <= MAX_ANDROID_USER_ID && count < max_users; user_id++) { - char user_path[USER_DATA_PATH_LEN]; - - snprintf(user_path, sizeof(user_path), "%s/%u", USER_DATA_BASE_PATH, user_id); - - if (!kern_path(user_path, 0, &path)) { - user_ids[count++] = user_id; - path_put(&path); - } - } - - filp_close(dir_file, NULL); - *found_users = count; - if (count > 0) { - pr_info("UserDE UID: Found %zu active users\n", count); - } - - return ret; + const char *opened_base = NULL; + + *found_users = 0; + + dir_file = ksu_filp_open_compat(USER_DATA_BASE_PATH, O_RDONLY, 0); + if (IS_ERR(dir_file)) { + pr_err("Failed to open user data path %s: %ld\n", + USER_DATA_BASE_PATH, PTR_ERR(dir_file)); + return PTR_ERR(dir_file); + } + opened_base = USER_DATA_BASE_PATH; + + for (uid_t user_id = MIN_ANDROID_USER_ID; + user_id <= MAX_ANDROID_USER_ID && count < max_users; + user_id++) { + + char user_path[USER_DATA_PATH_LEN]; + struct path path; + + snprintf(user_path, sizeof(user_path), "%s/%u", opened_base, user_id); + + if (!kern_path(user_path, 0, &path)) { + user_ids[count++] = user_id; + path_put(&path); + } + } + + filp_close(dir_file, NULL); + *found_users = count; + if (count > 0) + pr_info("UserDE UID: Found %zu active users\n", count); + + return ret; } FILLDIR_RETURN_TYPE user_data_actor(struct dir_context *ctx, const char *name, @@ -264,7 +262,7 @@ FILLDIR_RETURN_TYPE user_data_actor(struct dir_context *ctx, const char *name, return FILLDIR_ACTOR_CONTINUE; } - struct uid_data *data = kzalloc(sizeof(struct uid_data), GFP_ATOMIC); + struct uid_data *data = kzalloc(sizeof(struct uid_data), GFP_KERNEL); if (!data) { pr_err("Failed to allocate memory for package: %.*s (user %u)\n", namelen, name, scan_ctx->user_id); scan_ctx->errors_count++; @@ -272,7 +270,7 @@ FILLDIR_RETURN_TYPE user_data_actor(struct dir_context *ctx, const char *name, } data->uid = uid; - size_t copy_len = min(namelen, KSU_MAX_PACKAGE_NAME - 1); + size_t copy_len = min_t(size_t, namelen, KSU_MAX_PACKAGE_NAME - 1); strncpy(data->package, name, copy_len); data->package[copy_len] = '\0'; @@ -331,7 +329,7 @@ static int scan_user_data_for_user(uid_t user_id, struct list_head *uid_list, int scan_user_data_for_uids(struct list_head *uid_list) { - uid_t user_ids[32]; // Supports up to 32 users + uid_t user_ids[MAX_SUPPORTED_USERS]; size_t active_users = 0; size_t total_packages = 0; size_t total_errors = 0;