Skip to content

Commit

Permalink
KernelSU: update to v0.6.1 and fix "the kernel version is too low"
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalfa159 committed Jun 26, 2023
1 parent 6a21707 commit 8ff9873
Show file tree
Hide file tree
Showing 54 changed files with 1,033 additions and 318 deletions.
2 changes: 1 addition & 1 deletion KernelSU/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**English** | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md)
**English** | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md) | [Türkçe](README_TR.md)

# KernelSU

Expand Down
2 changes: 1 addition & 1 deletion KernelSU/README_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English](README.md) | **简体中文** | [繁體中文](README_TW.md) | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md)
[English](README.md) | **简体中文** | [繁體中文](README_TW.md) | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md) | [Türkçe](README_TR.md)

# KernelSU

Expand Down
2 changes: 1 addition & 1 deletion KernelSU/README_JP.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English](README.md) | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | **日本語** | [Portuguese-Brazil](README_PT-BR.md)
[English](README.md) | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | **日本語** | [Portuguese-Brazil](README_PT-BR.md) | [Türkçe](README_TR.md)

# KernelSU

Expand Down
2 changes: 1 addition & 1 deletion KernelSU/README_PT-BR.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English](README.md) | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | [日本語](README_JP.md) | **Portuguese-Brazil**
[English](README.md) | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | [日本語](README_JP.md) | **Portuguese-Brazil** | [Türkçe](README_TR.md)

# KernelSU

Expand Down
42 changes: 42 additions & 0 deletions KernelSU/README_TR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[English](README.md) | [简体中文](README_CN.md) | [繁體中文](README_TW.md) | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md) | **Türkçe**

# KernelSU

Android cihazlar için kernel tabanlı bir root çözümü.

## Özellikler

1. Kernel-tabanlı `su` ve root erişimi yönetimi.
2. Overlayfs'ye dayalı modül sistemi.

## Uyumluluk Durumu

KernelSU resmi olarak Android GKI 2.0 cihazlarını ( 5.10+ kernelli) destekler, eski kernellerle de (4.14+) uyumludur, ancak kerneli kendinizin inşaa etmesi gerekir.

WSA ve konteyner tabanlı Android'in de, KernelSU ile entegre olarak da çalışması gerekmektedir.

Ve desteklenen mevcut ABI'ler : `arm64-v8a` ve `x86_64`

## Kullanım

[Yükleme](https://kernelsu.org/guide/installation.html)

## İnşaa

[Nasıl inşa edilir?](https://kernelsu.org/guide/how-to-build.html)

### Tartışma

- Telegram: [@KernelSU](https://t.me/KernelSU)

## Lisans

- `kernel` klasöründeki dosyalar [GPL-2](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) lisansı altındadır.
- `kernel` klasörü dışındaki bütün diğer bölümler [GPL-3](https://www.gnu.org/licenses/gpl-3.0.html) lisansı altındadır.

## Krediler

- [kernel-assisted-superuser](https://git.zx2c4.com/kernel-assisted-superuser/about/): KernelSU fikri.
- [genuine](https://github.com/brevent/genuine/): apk v2 imza doğrulama.
- [Diamorphine](https://github.com/m0nad/Diamorphine): bazı rootkit becerileri.
- [Magisk](https://github.com/topjohnwu/Magisk): sepolicy uygulaması.
2 changes: 1 addition & 1 deletion KernelSU/README_TW.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English](README.md) | [简体中文](README_CN.md) | **繁體中文** | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md)
[English](README.md) | [简体中文](README_CN.md) | **繁體中文** | [日本語](README_JP.md) | [Portuguese-Brazil](README_PT-BR.md) | [Türkçe](README_TR.md)

# KernelSU

Expand Down
108 changes: 96 additions & 12 deletions KernelSU/kernel/allowlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ static DEFINE_MUTEX(allowlist_mutex);
static struct root_profile default_root_profile;
static struct non_root_profile default_non_root_profile;

static int allow_list_arr[PAGE_SIZE / sizeof(int)] __read_mostly __aligned(PAGE_SIZE);
static int allow_list_pointer __read_mostly = 0;

static void remove_uid_from_arr(uid_t uid)
{
int *temp_arr;
int i, j;

if (allow_list_pointer == 0)
return;

temp_arr = kmalloc(sizeof(allow_list_arr), GFP_KERNEL);
if (temp_arr == NULL) {
pr_err("%s: unable to allocate memory\n", __func__);
return;
}

for (i = j = 0; i < allow_list_pointer; i++) {
if (allow_list_arr[i] == uid)
continue;
temp_arr[j++] = allow_list_arr[i];
}

allow_list_pointer = j;

for (; j < ARRAY_SIZE(allow_list_arr); j++)
temp_arr[j] = -1;

memcpy(&allow_list_arr, temp_arr, PAGE_SIZE);
kfree(temp_arr);
}

static void init_default_profiles()
{
default_root_profile.uid = 0;
Expand All @@ -51,6 +83,9 @@ struct perm_data {

static struct list_head allow_list;

static uint8_t allow_list_bitmap[PAGE_SIZE] __read_mostly __aligned(PAGE_SIZE);
#define BITMAP_UID_MAX ((sizeof(allow_list_bitmap) * BITS_PER_BYTE) - 1)

#define KERNEL_SU_ALLOWLIST "/data/adb/ksu/.allowlist"

static struct work_struct ksu_save_work;
Expand Down Expand Up @@ -104,12 +139,23 @@ bool ksu_get_app_profile(struct app_profile *profile)
return found;
}

static inline bool forbid_system_uid(uid_t uid) {
#define SHELL_UID 2000
#define SYSTEM_UID 1000
return uid < SHELL_UID && uid != SYSTEM_UID;
}

static bool profile_valid(struct app_profile *profile)
{
if (!profile) {
return false;
}

if (forbid_system_uid(profile->current_uid)) {
pr_err("uid lower than 2000 is unsupported: %d\n", profile->current_uid);
return false;
}

if (profile->version < KSU_APP_PROFILE_VER) {
pr_info("Unsupported profile version: %d\n", profile->version);
return false;
Expand Down Expand Up @@ -147,7 +193,7 @@ bool ksu_set_app_profile(struct app_profile *profile, bool persist)
// found it, just override it all!
memcpy(&p->profile, profile, sizeof(*profile));
result = true;
goto exit;
goto out;
}
}

Expand All @@ -170,9 +216,31 @@ bool ksu_set_app_profile(struct app_profile *profile, bool persist)
profile->nrp_config.profile.umount_modules);
}
list_add_tail(&p->list, &allow_list);

out:
if (profile->current_uid <= BITMAP_UID_MAX) {
if (profile->allow_su)
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] |= 1 << (profile->current_uid % BITS_PER_BYTE);
else
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] &= ~(1 << (profile->current_uid % BITS_PER_BYTE));
} else {
if (profile->allow_su) {
/*
* 1024 apps with uid higher than BITMAP_UID_MAX
* registered to request superuser?
*/
if (allow_list_pointer >= ARRAY_SIZE(allow_list_arr)) {
pr_err("too many apps registered\n");
WARN_ON(1);
return false;
}
allow_list_arr[allow_list_pointer++] = profile->current_uid;
} else {
remove_uid_from_arr(profile->current_uid);
}
}
result = true;

exit:
// check if the default profiles is changed, cache it to a single struct to accelerate access.
if (unlikely(!strcmp(profile->key, "$"))) {
// set default non root profile
Expand All @@ -192,21 +260,26 @@ bool ksu_set_app_profile(struct app_profile *profile, bool persist)
return result;
}

bool ksu_is_allow_uid(uid_t uid)
bool __ksu_is_allow_uid(uid_t uid)
{
struct perm_data *p = NULL;
struct list_head *pos = NULL;
int i;

if (uid == 0) {
if (unlikely(uid == 0)) {
// already root, but only allow our domain.
return is_ksu_domain();
}

list_for_each (pos, &allow_list) {
p = list_entry(pos, struct perm_data, list);
// pr_info("is_allow_uid uid :%d, allow: %d\n", p->uid, p->allow);
if (uid == p->profile.current_uid) {
return p->profile.allow_su;
if (forbid_system_uid(uid)) {
// do not bother going through the list if it's system
return false;
}

if (likely(uid <= BITMAP_UID_MAX)) {
return !!(allow_list_bitmap[uid / BITS_PER_BYTE] & (1 << (uid % BITS_PER_BYTE)));
} else {
for (i = 0; i < allow_list_pointer; i++) {
if (allow_list_arr[i] == uid)
return true;
}
}

Expand Down Expand Up @@ -281,7 +354,7 @@ void do_save_allow_list(struct work_struct *work)
filp_open(KERNEL_SU_ALLOWLIST, O_WRONLY | O_CREAT, 0644);

if (IS_ERR(fp)) {
pr_err("save_allow_list creat file failed: %ld\n", PTR_ERR(fp));
pr_err("save_allow_list create file failed: %ld\n", PTR_ERR(fp));
return;
}

Expand Down Expand Up @@ -386,6 +459,9 @@ void ksu_prune_allowlist(bool (*is_uid_exist)(uid_t, void *), void *data)
modified = true;
pr_info("prune uid: %d\n", uid);
list_del(&np->list);
allow_list_bitmap[uid / BITS_PER_BYTE] &= ~(1 << (uid % BITS_PER_BYTE));
remove_uid_from_arr(uid);
smp_mb();
kfree(np);
}
}
Expand All @@ -409,6 +485,14 @@ bool ksu_load_allow_list(void)

void ksu_allowlist_init(void)
{
int i;

BUILD_BUG_ON(sizeof(allow_list_bitmap) != PAGE_SIZE);
BUILD_BUG_ON(sizeof(allow_list_arr) != PAGE_SIZE);

for (i = 0; i < ARRAY_SIZE(allow_list_arr); i++)
allow_list_arr[i] = -1;

INIT_LIST_HEAD(&allow_list);

INIT_WORK(&ksu_save_work, do_save_allow_list);
Expand Down
5 changes: 3 additions & 2 deletions KernelSU/kernel/allowlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ bool ksu_load_allow_list(void);

void ksu_show_allow_list(void);

bool ksu_is_allow_uid(uid_t uid);
bool __ksu_is_allow_uid(uid_t uid);
#define ksu_is_allow_uid(uid) unlikely(__ksu_is_allow_uid(uid))

bool ksu_get_allow_list(int *array, int *length, bool allow);

Expand All @@ -23,4 +24,4 @@ bool ksu_set_app_profile(struct app_profile *, bool persist);

bool ksu_uid_should_umount(uid_t uid);
struct root_profile *ksu_get_root_profile(uid_t uid);
#endif
#endif
2 changes: 1 addition & 1 deletion KernelSU/kernel/apk_sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
// return 0 if signature match
int is_manager_apk(char *path);

#endif
#endif
28 changes: 21 additions & 7 deletions KernelSU/kernel/core_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ static inline bool is_isolated_uid(uid_t uid)
appid <= LAST_APP_ZYGOTE_ISOLATED_UID);
}

static struct group_info root_groups = { .usage = ATOMIC_INIT(2) };

static void setup_groups(struct root_profile *profile, struct cred *cred)
{
if (profile->groups_count > KSU_MAX_GROUPS) {
Expand All @@ -60,14 +62,23 @@ static void setup_groups(struct root_profile *profile, struct cred *cred)
return;
}

if (profile->groups_count == 1 && profile->groups[0] == 0) {
// setgroup to root and return early.
if (cred->group_info)
put_group_info(cred->group_info);
cred->group_info = get_group_info(&root_groups);
return;
}

u32 ngroups = profile->groups_count;
struct group_info *group_info = groups_alloc(ngroups);
if (!group_info) {
pr_warn("Failed to setgroups, ENOMEM for: %d\n", profile->uid);
return;
}

for (int i = 0; i < ngroups; i++) {
int i;
for (i = 0; i < ngroups; i++) {
gid_t gid = profile->groups[i];
kgid_t kgid = make_kgid(current_user_ns(), gid);
if (!gid_valid(kgid)) {
Expand Down Expand Up @@ -464,12 +475,7 @@ static bool should_umount(struct path *path)

if (path->mnt && path->mnt->mnt_sb && path->mnt->mnt_sb->s_type) {
const char *fstype = path->mnt->mnt_sb->s_type->name;
if (strcmp(fstype, "overlay") == 0) {
return ksu_uid_should_umount(current_uid().val);
}
#ifdef CONFIG_KSU_DEBUG
pr_info("uid: %d should not umount!\n", current_uid().val);
#endif
return strcmp(fstype, "overlay") == 0;
}
return false;
}
Expand Down Expand Up @@ -521,6 +527,14 @@ int ksu_handle_setuid(struct cred *new, const struct cred *old)
return 0;
}

if (!ksu_uid_should_umount(new_uid.val)) {
return 0;
} else {
#ifdef CONFIG_KSU_DEBUG
pr_info("uid: %d should not umount!\n", current_uid().val);
#endif
}

// umount the target mnt
pr_info("handle umount for uid: %d\n", new_uid.val);

Expand Down
2 changes: 1 addition & 1 deletion KernelSU/kernel/embed_ksud.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// This file will be regenerated by CI

unsigned int ksud_size = 0;
const char ksud[0] = {};
const char ksud[0] = {};
2 changes: 1 addition & 1 deletion KernelSU/kernel/export_symbol.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
register_kprobe
unregister_kprobe
unregister_kprobe
2 changes: 1 addition & 1 deletion KernelSU/kernel/kernel_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count, l
}
return result;
#endif
}
}
2 changes: 1 addition & 1 deletion KernelSU/kernel/kernel_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ static inline int install_session_keyring(struct key *keyring)
#define KWORKER_INSTALL_KEYRING()
#endif

#endif
#endif
2 changes: 1 addition & 1 deletion KernelSU/kernel/klog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
#define pr_fmt(fmt) "KernelSU: " fmt
#endif

#endif
#endif
Loading

0 comments on commit 8ff9873

Please sign in to comment.