Skip to content

Commit c933f81

Browse files
committed
kernel: handle backports
It is a common thing on the scene to backport things, so this breaks kernel versioning assumptions. As for those, we scan and check kernel source. The following are backportable: - get_cred_rcu: context: tiann#2320 (comment) apply: torvalds/linux@97d0fb2 if above conflicts, try: xiaomi-sdm678/android_kernel_xiaomi_mojito@3fbad8b - path_umount: context: tiann#1464 (comment) apply: xiaomi-sdm678/android_kernel_xiaomi_mojito@2d51422 - strncpy_from_user_nofault for 5.4, apply: torvalds/linux@bd88bb5 for 4.x, apply: xiaomi-sdm678/android_kernel_xiaomi_mojito@424e21f for any failures, check dependency chain of gregkh/linux@f43434e - kernel_read / kernel_write < 4.14, backport chain, tested on 4.9 torvalds/linux@e13ec93 torvalds/linux@bdd1d2d torvalds/linux@c41fbad torvalds/linux@ac452ac - hint, `curl $url.patch | git am` Signed-off-by: backslashxx <[email protected]>
1 parent b0a87ba commit c933f81

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

kernel/Makefile

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,34 @@ $(info -- KernelSU Manager signature hash: $(KSU_EXPECTED_HASH))
5656
ccflags-y += -DEXPECTED_SIZE=$(KSU_EXPECTED_SIZE)
5757
ccflags-y += -DEXPECTED_HASH=\"$(KSU_EXPECTED_HASH)\"
5858

59+
ifeq ($(shell grep -q "get_cred_rcu" $(srctree)/include/linux/cred.h; echo $$?),0)
60+
$(info -- KernelSU/compat: get_cred_rcu found)
61+
ccflags-y += -DKSU_HAS_GET_CRED_RCU
62+
endif
63+
5964
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)
65+
$(info -- KernelSU/compat: path_umount found)
66+
ccflags-y += -DKSU_HAS_PATH_UMOUNT
67+
endif
68+
69+
ifeq ($(shell grep -q "strncpy_from_user_nofault" $(srctree)/include/linux/uaccess.h; echo $$?),0)
70+
$(info -- KernelSU/compat: strncpy_from_user_nofault found)
71+
ccflags-y += -DKSU_STRNCPY_FROM_USER_NOFAULT
72+
endif
73+
74+
ifeq ($(shell grep -q "strncpy_from_unsafe_user" $(srctree)/include/linux/uaccess.h; echo $$?),0)
75+
$(info -- KernelSU/compat: strncpy_from_unsafe_user found)
76+
ccflags-y += -DKSU_STRNCPY_FROM_UNSAFE_USER
77+
endif
78+
79+
ifeq ($(shell grep -q "ssize_t kernel_read" $(srctree)/fs/read_write.c; echo $$?),0)
80+
$(info -- KernelSU/compat: newer kernel_read found)
81+
ccflags-y += -DKSU_NEW_KERNEL_READ
82+
endif
83+
84+
ifeq ($(shell grep "ssize_t kernel_write" $(srctree)/fs/read_write.c | grep -q "const void" ; echo $$?),0)
85+
$(info -- KernelSU/compat: newer kernel_write found)
86+
ccflags-y += -DKSU_NEW_KERNEL_WRITE
6487
endif
6588

6689
ccflags-y += -Wno-implicit-function-declaration -Wno-strict-prototypes -Wno-int-conversion -Wno-gcc-compat

kernel/kernel_compat.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct file *ksu_filp_open_compat(const char *filename, int flags, umode_t mode)
108108
ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
109109
loff_t *pos)
110110
{
111-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
111+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || defined(KSU_NEW_KERNEL_READ)
112112
return kernel_read(p, buf, count, pos);
113113
#else
114114
loff_t offset = pos ? *pos : 0;
@@ -123,7 +123,7 @@ ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
123123
ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
124124
loff_t *pos)
125125
{
126-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
126+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || defined(KSU_NEW_KERNEL_WRITE)
127127
return kernel_write(p, buf, count, pos);
128128
#else
129129
loff_t offset = pos ? *pos : 0;
@@ -135,20 +135,19 @@ ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
135135
#endif
136136
}
137137

138-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
138+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) || defined(KSU_STRNCPY_FROM_USER_NOFAULT)
139139
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
140140
long count)
141141
{
142142
return strncpy_from_user_nofault(dst, unsafe_addr, count);
143143
}
144-
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
144+
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0) || defined(KSU_STRNCPY_FROM_UNSAFE_USER)
145145
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
146146
long count)
147147
{
148148
return strncpy_from_unsafe_user(dst, unsafe_addr, count);
149149
}
150-
#else
151-
// Copied from: https://elixir.bootlin.com/linux/v4.9.337/source/mm/maccess.c#L201
150+
#else // Copied from: https://elixir.bootlin.com/linux/v4.9.337/source/mm/maccess.c#L201
152151
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
153152
long count)
154153
{

0 commit comments

Comments
 (0)