Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions kernel/manager/apk_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <linux/version.h>
#ifdef CONFIG_KSU_DEBUG
#include <linux/moduleparam.h>
#include "throne_tracker.h"
#endif
#include <crypto/hash.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
39 changes: 39 additions & 0 deletions kernel/manager/throne_tracker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down