diff --git a/kernel/manager/apk_sign.c b/kernel/manager/apk_sign.c index 68b34d55c336..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) @@ -23,6 +24,54 @@ struct sdesc { char ctx[]; }; +#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 +// 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; + + for (const char *p = path; *p; p++) { + if (*p == '/') { + second_last_slash = last_slash; + last_slash = p; + } + } + + 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 + 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 +292,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 +318,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; } 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];