Fixed some minor issues that may have existed

This commit is contained in:
ShirkNeko
2025-09-13 20:25:04 +08:00
parent 316cb79f32
commit eb87c1355b
2 changed files with 58 additions and 53 deletions

View File

@@ -298,19 +298,26 @@ int ksu_handle_rename(struct dentry *old_dentry, struct dentry *new_dentry)
return 0; return 0;
} }
// It still monitors changes to certain system files to trigger scans, but does not rely on packages.list. // /data/system/packages.list.tmp -> /data/system/packages.list
if (strstr(new_dentry->d_iname, "packages") && if (strcmp(new_dentry->d_iname, "packages.list")) {
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();
}
return 0; 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; return 0;
} }

View File

@@ -19,8 +19,8 @@
uid_t ksu_manager_uid = KSU_INVALID_UID; uid_t ksu_manager_uid = KSU_INVALID_UID;
#define USER_DATA_BASE_PATH "/data/user_de" #define USER_DATA_BASE_PATH "/data/user_de"
#define USER_DATA_LEGACY_BASE_PATH "/data/user" #define MAX_SUPPORTED_USERS 32 // Supports up to 32 users
#define USER_DATA_PATH_LEN 256 // 256 is enough for /data/user_de/{userid}/<package> #define USER_DATA_PATH_LEN 384 // 384 is enough for /data/user_de/{userid}/<package>
#define MAX_ANDROID_USER_ID 999 #define MAX_ANDROID_USER_ID 999
#define MIN_ANDROID_USER_ID 0 #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 // 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) static int get_active_user_ids(uid_t *user_ids, size_t max_users, size_t *found_users)
{ {
struct file *dir_file; struct file *dir_file;
struct dir_context ctx __maybe_unused = {0}; int ret = 0;
int ret = 0; size_t count = 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 path path; const char *opened_base = NULL;
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]; *found_users = 0;
snprintf(user_path, sizeof(user_path), "%s/%u", USER_DATA_BASE_PATH, user_id); dir_file = ksu_filp_open_compat(USER_DATA_BASE_PATH, O_RDONLY, 0);
if (IS_ERR(dir_file)) {
if (!kern_path(user_path, 0, &path)) { pr_err("Failed to open user data path %s: %ld\n",
user_ids[count++] = user_id; USER_DATA_BASE_PATH, PTR_ERR(dir_file));
path_put(&path); return PTR_ERR(dir_file);
} }
} opened_base = USER_DATA_BASE_PATH;
filp_close(dir_file, NULL); for (uid_t user_id = MIN_ANDROID_USER_ID;
*found_users = count; user_id <= MAX_ANDROID_USER_ID && count < max_users;
if (count > 0) { user_id++) {
pr_info("UserDE UID: Found %zu active users\n", count);
} char user_path[USER_DATA_PATH_LEN];
struct path path;
return ret;
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, 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; 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) { if (!data) {
pr_err("Failed to allocate memory for package: %.*s (user %u)\n", namelen, name, scan_ctx->user_id); pr_err("Failed to allocate memory for package: %.*s (user %u)\n", namelen, name, scan_ctx->user_id);
scan_ctx->errors_count++; scan_ctx->errors_count++;
@@ -272,7 +270,7 @@ FILLDIR_RETURN_TYPE user_data_actor(struct dir_context *ctx, const char *name,
} }
data->uid = uid; 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); strncpy(data->package, name, copy_len);
data->package[copy_len] = '\0'; 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) 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 active_users = 0;
size_t total_packages = 0; size_t total_packages = 0;
size_t total_errors = 0; size_t total_errors = 0;