Skip to content

Commit 898e9d4

Browse files
Ylarodtiann
andauthored
[1.0] Drop Non-GKI Support (#1483)
Co-authored-by: weishu <twsxtd@gmail.com>
1 parent b766b98 commit 898e9d4

File tree

14 files changed

+11
-581
lines changed

14 files changed

+11
-581
lines changed

kernel/Makefile

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ $(warning "KSU_GIT_VERSION not defined! It is better to make KernelSU a git subm
2929
ccflags-y += -DKSU_VERSION=16
3030
endif
3131

32-
ifeq ($(shell grep -q " current_sid(void)" $(srctree)/security/selinux/include/objsec.h; echo $$?),0)
33-
ccflags-y += -DKSU_COMPAT_HAS_CURRENT_SID
34-
endif
35-
36-
ifeq ($(shell grep -q "struct selinux_state " $(srctree)/security/selinux/include/security.h; echo $$?),0)
37-
ccflags-y += -DKSU_COMPAT_HAS_SELINUX_STATE
38-
endif
39-
4032
ifndef KSU_EXPECTED_SIZE
4133
KSU_EXPECTED_SIZE := 0x033b
4234
endif
@@ -56,13 +48,6 @@ $(info -- KernelSU Manager signature hash: $(KSU_EXPECTED_HASH))
5648
ccflags-y += -DEXPECTED_SIZE=$(KSU_EXPECTED_SIZE)
5749
ccflags-y += -DEXPECTED_HASH=\"$(KSU_EXPECTED_HASH)\"
5850

59-
ifeq ($(shell grep -q "int path_umount" $(srctree)/fs/namespace.c; echo $$?),0)
60-
ccflags-y += -DKSU_UMOUNT
61-
else
62-
$(info -- Did you know you can backport path_umount to fs/namespace.c from 5.9?)
63-
$(info -- Read: https://kernelsu.org/guide/how-to-integrate-for-non-gki.html#how-to-backport-path-umount)
64-
endif
65-
6651
ccflags-y += -Wno-implicit-function-declaration -Wno-strict-prototypes -Wno-int-conversion -Wno-gcc-compat
6752
ccflags-y += -Wno-declaration-after-statement -Wno-unused-function
6853

kernel/allowlist.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#include <linux/slab.h>
88
#include <linux/types.h>
99
#include <linux/version.h>
10-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
1110
#include <linux/compiler_types.h>
12-
#endif
1311

1412
#include "ksu.h"
1513
#include "klog.h" // IWYU pragma: keep

kernel/arch.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@
1818
#define __PT_SP_REG sp
1919
#define __PT_IP_REG pc
2020

21-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
2221
#define PRCTL_SYMBOL "__arm64_sys_prctl"
2322
#define SYS_READ_SYMBOL "__arm64_sys_read"
2423
#define SYS_NEWFSTATAT_SYMBOL "__arm64_sys_newfstatat"
2524
#define SYS_FACCESSAT_SYMBOL "__arm64_sys_faccessat"
2625
#define SYS_EXECVE_SYMBOL "__arm64_sys_execve"
27-
#else
28-
#define PRCTL_SYMBOL "sys_prctl"
29-
#define SYS_READ_SYMBOL "sys_read"
30-
#define SYS_NEWFSTATAT_SYMBOL "sys_newfstatat"
31-
#define SYS_FACCESSAT_SYMBOL "sys_faccessat"
32-
#define SYS_EXECVE_SYMBOL "sys_execve"
33-
#endif
3426

3527
#elif defined(__x86_64__)
3628

@@ -47,19 +39,11 @@
4739
#define __PT_RC_REG ax
4840
#define __PT_SP_REG sp
4941
#define __PT_IP_REG ip
50-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
5142
#define PRCTL_SYMBOL "__x64_sys_prctl"
5243
#define SYS_READ_SYMBOL "__x64_sys_read"
5344
#define SYS_NEWFSTATAT_SYMBOL "__x64_sys_newfstatat"
5445
#define SYS_FACCESSAT_SYMBOL "__x64_sys_faccessat"
5546
#define SYS_EXECVE_SYMBOL "__x64_sys_execve"
56-
#else
57-
#define PRCTL_SYMBOL "sys_prctl"
58-
#define SYS_READ_SYMBOL "sys_read"
59-
#define SYS_NEWFSTATAT_SYMBOL "sys_newfstatat"
60-
#define SYS_FACCESSAT_SYMBOL "sys_faccessat"
61-
#define SYS_EXECVE_SYMBOL "sys_execve"
62-
#endif
6347

6448
#else
6549
#error "Unsupported arch"
@@ -83,10 +67,6 @@
8367
#define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG)
8468
#define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG)
8569

86-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
8770
#define PT_REAL_REGS(regs) ((struct pt_regs *)PT_REGS_PARM1(regs))
88-
#else
89-
#define PT_REAL_REGS(regs) ((regs))
90-
#endif
9171

9272
#endif

kernel/core_hook.c

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ static void setup_groups(struct root_profile *profile, struct cred *cred)
9898
put_group_info(group_info);
9999
return;
100100
}
101-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
102101
group_info->gid[i] = kgid;
103-
#else
104-
GROUP_AT(group_info, i) = kgid;
105-
#endif
106102
}
107103

108104
groups_sort(group_info);
@@ -449,14 +445,12 @@ static bool should_umount(struct path *path)
449445
return false;
450446
}
451447

452-
static int ksu_umount_mnt(struct path *path, int flags)
448+
static void ksu_umount_mnt(struct path *path, int flags)
453449
{
454-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 9, 0) || defined(KSU_UMOUNT)
455-
return path_umount(path, flags);
456-
#else
457-
// TODO: umount for non GKI kernel
458-
return -ENOSYS;
459-
#endif
450+
int err = path_umount(path, flags);
451+
if (err) {
452+
pr_info("umount %s failed: %d\n", path->dentry->d_iname, err);
453+
}
460454
}
461455

462456
static void try_umount(const char *mnt, bool check_mnt, int flags)
@@ -477,10 +471,7 @@ static void try_umount(const char *mnt, bool check_mnt, int flags)
477471
return;
478472
}
479473

480-
err = ksu_umount_mnt(&path, flags);
481-
if (err) {
482-
pr_warn("umount %s failed: %d\n", mnt, err);
483-
}
474+
ksu_umount_mnt(&path, flags);
484475
}
485476

486477
int ksu_handle_setuid(struct cred *new, const struct cred *old)
@@ -557,14 +548,8 @@ static int handler_pre(struct kprobe *p, struct pt_regs *regs)
557548
int option = (int)PT_REGS_PARM1(real_regs);
558549
unsigned long arg2 = (unsigned long)PT_REGS_PARM2(real_regs);
559550
unsigned long arg3 = (unsigned long)PT_REGS_PARM3(real_regs);
560-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
561551
// PRCTL_SYMBOL is the arch-specificed one, which receive raw pt_regs from syscall
562552
unsigned long arg4 = (unsigned long)PT_REGS_SYSCALL_PARM4(real_regs);
563-
#else
564-
// PRCTL_SYMBOL is the common one, called by C convention in do_syscall_64
565-
// https://elixir.bootlin.com/linux/v4.15.18/source/arch/x86/entry/common.c#L287
566-
unsigned long arg4 = (unsigned long)PT_REGS_CCALL_PARM4(real_regs);
567-
#endif
568553
unsigned long arg5 = (unsigned long)PT_REGS_PARM5(real_regs);
569554

570555
return ksu_handle_prctl(option, arg2, arg3, arg4, arg5);
@@ -624,23 +609,7 @@ static int ksu_task_prctl(int option, unsigned long arg2, unsigned long arg3,
624609
ksu_handle_prctl(option, arg2, arg3, arg4, arg5);
625610
return -ENOSYS;
626611
}
627-
// kernel 4.4 and 4.9
628-
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI)
629-
static int ksu_key_permission(key_ref_t key_ref, const struct cred *cred,
630-
unsigned perm)
631-
{
632-
if (init_session_keyring != NULL) {
633-
return 0;
634-
}
635-
if (strcmp(current->comm, "init")) {
636-
// we are only interested in `init` process
637-
return 0;
638-
}
639-
init_session_keyring = cred->session_keyring;
640-
pr_info("kernel_compat: got init_session_keyring\n");
641-
return 0;
642-
}
643-
#endif
612+
644613
static int ksu_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
645614
struct inode *new_inode, struct dentry *new_dentry)
646615
{
@@ -658,19 +627,11 @@ static struct security_hook_list ksu_hooks[] = {
658627
LSM_HOOK_INIT(task_prctl, ksu_task_prctl),
659628
LSM_HOOK_INIT(inode_rename, ksu_inode_rename),
660629
LSM_HOOK_INIT(task_fix_setuid, ksu_task_fix_setuid),
661-
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI)
662-
LSM_HOOK_INIT(key_permission, ksu_key_permission)
663-
#endif
664630
};
665631

666632
void __init ksu_lsm_hook_init(void)
667633
{
668-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
669634
security_add_hooks(ksu_hooks, ARRAY_SIZE(ksu_hooks), "ksu");
670-
#else
671-
// https://elixir.bootlin.com/linux/v4.10.17/source/include/linux/lsm_hooks.h#L1892
672-
security_add_hooks(ksu_hooks, ARRAY_SIZE(ksu_hooks));
673-
#endif
674635
}
675636

676637
#else

kernel/kernel_compat.c

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
#include <linux/version.h>
22
#include <linux/fs.h>
33
#include <linux/nsproxy.h>
4-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
54
#include <linux/sched/task.h>
6-
#else
7-
#include <linux/sched.h>
8-
#endif
95
#include <linux/uaccess.h>
106
#include "klog.h" // IWYU pragma: keep
11-
#include "kernel_compat.h" // Add check Huawei Device
12-
13-
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI)
14-
#include <linux/key.h>
15-
#include <linux/errno.h>
16-
#include <linux/cred.h>
17-
struct key *init_session_keyring = NULL;
18-
19-
static inline int install_session_keyring(struct key *keyring)
20-
{
21-
struct cred *new;
22-
int ret;
23-
24-
new = prepare_creds();
25-
if (!new)
26-
return -ENOMEM;
27-
28-
ret = install_session_keyring_to_cred(new, keyring);
29-
if (ret < 0) {
30-
abort_creds(new);
31-
return ret;
32-
}
33-
34-
return commit_creds(new);
35-
}
36-
#endif
7+
#include "kernel_compat.h"
378

389
extern struct task_struct init_task;
3910

@@ -79,13 +50,6 @@ void ksu_android_ns_fs_check()
7950

8051
struct file *ksu_filp_open_compat(const char *filename, int flags, umode_t mode)
8152
{
82-
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI)
83-
if (init_session_keyring != NULL && !current_cred()->session_keyring &&
84-
(current->flags & PF_WQ_WORKER)) {
85-
pr_info("installing init session keyring for older kernel\n");
86-
install_session_keyring(init_session_keyring);
87-
}
88-
#endif
8953
// switch mnt_ns even if current is not wq_worker, to ensure what we open is the correct file in android mnt_ns, rather than user created mnt_ns
9054
struct ksu_ns_fs_saved saved;
9155
if (android_context_saved_enabled) {
@@ -108,69 +72,17 @@ struct file *ksu_filp_open_compat(const char *filename, int flags, umode_t mode)
10872
ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
10973
loff_t *pos)
11074
{
111-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
11275
return kernel_read(p, buf, count, pos);
113-
#else
114-
loff_t offset = pos ? *pos : 0;
115-
ssize_t result = kernel_read(p, offset, (char *)buf, count);
116-
if (pos && result > 0) {
117-
*pos = offset + result;
118-
}
119-
return result;
120-
#endif
12176
}
12277

12378
ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
12479
loff_t *pos)
12580
{
126-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
12781
return kernel_write(p, buf, count, pos);
128-
#else
129-
loff_t offset = pos ? *pos : 0;
130-
ssize_t result = kernel_write(p, buf, count, offset);
131-
if (pos && result > 0) {
132-
*pos = offset + result;
133-
}
134-
return result;
135-
#endif
13682
}
13783

138-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
13984
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
14085
long count)
14186
{
14287
return strncpy_from_user_nofault(dst, unsafe_addr, count);
14388
}
144-
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
145-
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
146-
long count)
147-
{
148-
return strncpy_from_unsafe_user(dst, unsafe_addr, count);
149-
}
150-
#else
151-
// Copied from: https://elixir.bootlin.com/linux/v4.9.337/source/mm/maccess.c#L201
152-
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
153-
long count)
154-
{
155-
mm_segment_t old_fs = get_fs();
156-
long ret;
157-
158-
if (unlikely(count <= 0))
159-
return 0;
160-
161-
set_fs(USER_DS);
162-
pagefault_disable();
163-
ret = strncpy_from_user(dst, unsafe_addr, count);
164-
pagefault_enable();
165-
set_fs(old_fs);
166-
167-
if (ret >= count) {
168-
ret = count;
169-
dst[ret - 1] = '\0';
170-
} else if (ret > 0) {
171-
ret++;
172-
}
173-
174-
return ret;
175-
}
176-
#endif

kernel/kernel_compat.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ extern long ksu_strncpy_from_user_nofault(char *dst,
2424
const void __user *unsafe_addr,
2525
long count);
2626

27-
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI)
28-
extern struct key *init_session_keyring;
29-
#endif
30-
3127
extern void ksu_android_ns_fs_check();
3228
extern struct file *ksu_filp_open_compat(const char *filename, int flags,
3329
umode_t mode);

kernel/ksu.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,4 @@ module_exit(kernelsu_exit);
9494
MODULE_LICENSE("GPL");
9595
MODULE_AUTHOR("weishu");
9696
MODULE_DESCRIPTION("Android KernelSU");
97-
98-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
9997
MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
100-
#endif

0 commit comments

Comments
 (0)