Skip to content

Commit 52a7568

Browse files
committed
refactor: Expose get_pkg_from_apk_path and integrate it into apk_sign.c's package extraction with system app fallback.
1 parent 31271ac commit 52a7568

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

kernel/apk_sign.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/version.h>
77
#ifdef CONFIG_KSU_DEBUG
88
#include <linux/moduleparam.h>
9+
#include "throne_tracker.h"
910
#endif
1011
#include <crypto/hash.h>
1112
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
@@ -24,26 +25,29 @@ struct sdesc {
2425
};
2526

2627
#ifdef CONFIG_KSU_DEBUG
28+
extern int get_pkg_from_apk_path(char *pkg, const char *path);
29+
2730
// Extract package name from APK path for debug logging
28-
// Examples:
29-
// "/data/app/~~xxx==/me.weishu.kernelsu-yyy==/base.apk" -> "me.weishu.kernelsu"
30-
// "/data/app/MIUICalculator/base.apk" -> "MIUICalculator"
31-
// "/system/app/Calculator/Calculator.apk" -> "Calculator"
31+
// Reuses get_pkg_from_apk_path() for third-party apps, with fallback for system apps
3232
static const char *extract_package_name(const char *path)
3333
{
3434
static char pkg_name[256];
35+
36+
// Try official function first (handles third-party apps)
37+
if (get_pkg_from_apk_path(pkg_name, path) == 0)
38+
return pkg_name;
39+
40+
// Fallback for system apps: extract directory name
3541
const char *last_slash = NULL;
3642
const char *second_last_slash = NULL;
3743

38-
// Find last two '/' in path
3944
for (const char *p = path; *p; p++) {
4045
if (*p == '/') {
4146
second_last_slash = last_slash;
4247
last_slash = p;
4348
}
4449
}
4550

46-
// Extract directory name between the two slashes
4751
if (!second_last_slash || !last_slash)
4852
return path;
4953

@@ -54,7 +58,7 @@ static const char *extract_package_name(const char *path)
5458
memcpy(pkg_name, second_last_slash + 1, len);
5559
pkg_name[len] = '\0';
5660

57-
// Remove ".apk" suffix if present (e.g., /system/app/Calc/Calc.apk)
61+
// Remove .apk suffix if present
5862
len = strlen(pkg_name);
5963
if (len > 4 && !strcmp(pkg_name + len - 4, ".apk"))
6064
pkg_name[len - 4] = '\0';

kernel/throne_tracker.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,45 @@ struct uid_data {
2222
char package[KSU_MAX_PACKAGE_NAME];
2323
};
2424

25+
int get_pkg_from_apk_path(char *pkg, const char *path)
26+
{
27+
int len = strlen(path);
28+
if (len >= KSU_MAX_PACKAGE_NAME || len < 1)
29+
return -1;
30+
31+
const char *last_slash = NULL;
32+
const char *second_last_slash = NULL;
33+
34+
int i;
35+
for (i = len - 1; i >= 0; i--) {
36+
if (path[i] == '/') {
37+
if (!last_slash) {
38+
last_slash = &path[i];
39+
} else {
40+
second_last_slash = &path[i];
41+
break;
42+
}
43+
}
44+
}
45+
46+
if (!last_slash || !second_last_slash)
47+
return -1;
48+
49+
const char *last_hyphen = strchr(second_last_slash, '-');
50+
if (!last_hyphen || last_hyphen > last_slash)
51+
return -1;
52+
53+
int pkg_len = last_hyphen - second_last_slash - 1;
54+
if (pkg_len >= KSU_MAX_PACKAGE_NAME || pkg_len <= 0)
55+
return -1;
56+
57+
// Copying the package name
58+
strncpy(pkg, second_last_slash + 1, pkg_len);
59+
pkg[pkg_len] = '\0';
60+
61+
return 0;
62+
}
63+
2564
static void crown_manager(const char *apk, struct list_head *uid_data)
2665
{
2766
char pkg[KSU_MAX_PACKAGE_NAME];

0 commit comments

Comments
 (0)