Skip to content

Commit c46778a

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: - 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 c1c8612 commit c46778a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

kernel/Makefile

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,28 @@ ccflags-y += -DEXPECTED_SIZE=$(KSU_EXPECTED_SIZE)
5757
ccflags-y += -DEXPECTED_HASH=\"$(KSU_EXPECTED_HASH)\"
5858

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

6684
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
@@ -107,7 +107,7 @@ struct file *ksu_filp_open_compat(const char *filename, int flags, umode_t mode)
107107
ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
108108
loff_t *pos)
109109
{
110-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
110+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || defined(KSU_NEW_KERNEL_READ)
111111
return kernel_read(p, buf, count, pos);
112112
#else
113113
loff_t offset = pos ? *pos : 0;
@@ -122,7 +122,7 @@ ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
122122
ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
123123
loff_t *pos)
124124
{
125-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
125+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || defined(KSU_NEW_KERNEL_WRITE)
126126
return kernel_write(p, buf, count, pos);
127127
#else
128128
loff_t offset = pos ? *pos : 0;
@@ -134,20 +134,19 @@ ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
134134
#endif
135135
}
136136

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

0 commit comments

Comments
 (0)