From df7f2a41cebb36540be407c38598d5f67c69e8e6 Mon Sep 17 00:00:00 2001 From: sakana164 <76257039+sakana164@users.noreply.github.com> Date: Tue, 25 Nov 2025 16:13:12 +0800 Subject: [PATCH 1/2] kernel: apk_sign: Add package name extraction for debug logging --- kernel/manager/apk_sign.c | 49 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/kernel/manager/apk_sign.c b/kernel/manager/apk_sign.c index 68b34d55c336..4cf0bca911e3 100644 --- a/kernel/manager/apk_sign.c +++ b/kernel/manager/apk_sign.c @@ -23,6 +23,51 @@ struct sdesc { char ctx[]; }; +#ifdef CONFIG_KSU_DEBUG +// Extract package name from APK path for debug logging +// Examples: +// "/data/app/~~xxx==/me.weishu.kernelsu-yyy==/base.apk" -> "me.weishu.kernelsu" +// "/data/app/MIUICalculator/base.apk" -> "MIUICalculator" +// "/system/app/Calculator/Calculator.apk" -> "Calculator" +static const char *extract_package_name(const char *path) +{ + static char pkg_name[256]; + const char *last_slash = NULL; + const char *second_last_slash = NULL; + + // Find last two '/' in path + for (const char *p = path; *p; p++) { + if (*p == '/') { + second_last_slash = last_slash; + last_slash = p; + } + } + + // Extract directory name between the two slashes + if (!second_last_slash || !last_slash) + return path; + + int len = last_slash - second_last_slash - 1; + if (len <= 0 || len >= sizeof(pkg_name)) + return path; + + memcpy(pkg_name, second_last_slash + 1, len); + pkg_name[len] = '\0'; + + // Remove ".apk" suffix if present (e.g., /system/app/Calc/Calc.apk) + len = strlen(pkg_name); + if (len > 4 && !strcmp(pkg_name + len - 4, ".apk")) + pkg_name[len - 4] = '\0'; + + // Find first '-' and check total length from there to end + char *dash = strchr(pkg_name, '-'); + if (dash && dash > pkg_name && strlen(dash) > 10) + *dash = '\0'; + + return pkg_name; +} +#endif + static struct sdesc *init_sdesc(struct crypto_shash *alg) { struct sdesc *sdesc; @@ -243,7 +288,7 @@ static __always_inline bool check_v2_signature(char *path, unsigned expected_siz v3_1_signing_exist = true; } else { #ifdef CONFIG_KSU_DEBUG - pr_info("Unknown id: 0x%08x\n", id); + pr_info("[%s] Unknown id: 0x%08x\n", extract_package_name(path), id); #endif } pos += (size8 - offset); @@ -269,7 +314,7 @@ static __always_inline bool check_v2_signature(char *path, unsigned expected_siz if (v3_signing_exist || v3_1_signing_exist) { #ifdef CONFIG_KSU_DEBUG - pr_err("Unexpected v3 signature scheme found!\n"); + pr_err("[%s] Unexpected v3 signature scheme found!\n", extract_package_name(path)); #endif return false; } From 4e0c9acd41dc116d3c6be6422c11b813675b45ec Mon Sep 17 00:00:00 2001 From: sakana164 <76257039+sakana164@users.noreply.github.com> Date: Tue, 25 Nov 2025 19:55:57 +0800 Subject: [PATCH 2/2] refactor: Expose `get_pkg_from_apk_path` and integrate it into `apk_sign.c`'s package extraction with system app fallback. --- kernel/manager/apk_sign.c | 18 +++++++++------ kernel/manager/throne_tracker.c | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/kernel/manager/apk_sign.c b/kernel/manager/apk_sign.c index 4cf0bca911e3..f3e3f3731c82 100644 --- a/kernel/manager/apk_sign.c +++ b/kernel/manager/apk_sign.c @@ -6,6 +6,7 @@ #include #ifdef CONFIG_KSU_DEBUG #include +#include "throne_tracker.h" #endif #include #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) @@ -24,18 +25,22 @@ struct sdesc { }; #ifdef CONFIG_KSU_DEBUG +extern int get_pkg_from_apk_path(char *pkg, const char *path); + // Extract package name from APK path for debug logging -// Examples: -// "/data/app/~~xxx==/me.weishu.kernelsu-yyy==/base.apk" -> "me.weishu.kernelsu" -// "/data/app/MIUICalculator/base.apk" -> "MIUICalculator" -// "/system/app/Calculator/Calculator.apk" -> "Calculator" +// Reuses get_pkg_from_apk_path() for third-party apps, with fallback for system apps static const char *extract_package_name(const char *path) { static char pkg_name[256]; + + // Try official function first (handles third-party apps) + if (get_pkg_from_apk_path(pkg_name, path) == 0) + return pkg_name; + + // Fallback for system apps: extract directory name const char *last_slash = NULL; const char *second_last_slash = NULL; - // Find last two '/' in path for (const char *p = path; *p; p++) { if (*p == '/') { second_last_slash = last_slash; @@ -43,7 +48,6 @@ static const char *extract_package_name(const char *path) } } - // Extract directory name between the two slashes if (!second_last_slash || !last_slash) return path; @@ -54,7 +58,7 @@ static const char *extract_package_name(const char *path) memcpy(pkg_name, second_last_slash + 1, len); pkg_name[len] = '\0'; - // Remove ".apk" suffix if present (e.g., /system/app/Calc/Calc.apk) + // Remove .apk suffix if present len = strlen(pkg_name); if (len > 4 && !strcmp(pkg_name + len - 4, ".apk")) pkg_name[len - 4] = '\0'; diff --git a/kernel/manager/throne_tracker.c b/kernel/manager/throne_tracker.c index 818b07faa21b..8421eb7723da 100644 --- a/kernel/manager/throne_tracker.c +++ b/kernel/manager/throne_tracker.c @@ -22,6 +22,45 @@ struct uid_data { char package[KSU_MAX_PACKAGE_NAME]; }; +int get_pkg_from_apk_path(char *pkg, const char *path) +{ + int len = strlen(path); + if (len >= KSU_MAX_PACKAGE_NAME || len < 1) + return -1; + + const char *last_slash = NULL; + const char *second_last_slash = NULL; + + int i; + for (i = len - 1; i >= 0; i--) { + if (path[i] == '/') { + if (!last_slash) { + last_slash = &path[i]; + } else { + second_last_slash = &path[i]; + break; + } + } + } + + if (!last_slash || !second_last_slash) + return -1; + + const char *last_hyphen = strchr(second_last_slash, '-'); + if (!last_hyphen || last_hyphen > last_slash) + return -1; + + int pkg_len = last_hyphen - second_last_slash - 1; + if (pkg_len >= KSU_MAX_PACKAGE_NAME || pkg_len <= 0) + return -1; + + // Copying the package name + strncpy(pkg, second_last_slash + 1, pkg_len); + pkg[pkg_len] = '\0'; + + return 0; +} + static void crown_manager(const char *apk, struct list_head *uid_data) { char pkg[KSU_MAX_PACKAGE_NAME];