Skip to content

Commit 349afee

Browse files
backslashxxTrijal08
authored andcommitted
kernel: handle backports
Legacy kernel builders have to touch their kernel source code anyway so we might as well tell them to backport things to achieve some sort of feature parity. As for others, 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. Required: - get_cred_rcu: context: tiann/KernelSU#2320 (comment) apply: torvalds/linux@97d0fb2 if above conflicts, try: xiaomi-sdm678/android_kernel_xiaomi_mojito@3fbad8b Optional: - path_umount: context: tiann/KernelSU#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 dadb9f5 commit 349afee

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

kernel/Makefile

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,47 +104,24 @@ $(info -- KSU_NEXT: modifying 'get_task_cred' function in $(srctree)/kernel/cred
104104
$(shell sed -i "s/!$(ATOMIC_INC_FUNC)(&((struct cred \*)cred)->usage)/!get_cred_rcu(cred)/g" $(srctree)/kernel/cred.c)
105105
endif
106106

107-
ifneq ($(shell grep -Eq "^static int can_umount" $(srctree)/fs/namespace.c; echo $$?),0)
108-
$(info -- KSU_NEXT: adding function 'static int can_umount(const struct path *path, int flags);' to $(srctree)/fs/namespace.c)
109-
CAN_UMOUNT = static int can_umount(const struct path *path, int flags)\n\
110-
{\n\t\
111-
struct mount *mnt = real_mount(path->mnt);\n\t\
112-
if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))\n\t\t\
113-
return -EINVAL;\n\t\
114-
if (!may_mount())\n\t\t\
115-
return -EPERM;\n\t\
116-
if (path->dentry != path->mnt->mnt_root)\n\t\t\
117-
return -EINVAL;\n\t\
118-
if (!check_mnt(mnt))\n\t\t\
119-
return -EINVAL;\n\t\
120-
if (mnt->mnt.mnt_flags & MNT_LOCKED)\n\t\t\
121-
return -EINVAL;\n\t\
122-
if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))\n\t\t\
123-
return -EPERM;\n\t\
124-
return 0;\n\
125-
}\n
126-
$(shell sed -i '/^static bool is_mnt_ns_file/i $(CAN_UMOUNT)' $(srctree)/fs/namespace.c;)
107+
ifeq ($(shell grep -q "int path_umount" $(srctree)/fs/namespace.c; echo $$?),0)
108+
$(info -- KernelSU/compat: path_umount found)
109+
ccflags-y += -DKSU_HAS_PATH_UMOUNT
127110
endif
128111

129-
ifneq ($(shell grep -Eq "^int path_umount" $(srctree)/fs/namespace.c; echo $$?),0)
130-
$(info -- KSU_NEXT: adding function 'int path_umount(struct path *path, int flags);' to $(srctree)/fs/namespace.c)
131-
PATH_UMOUNT = int path_umount(struct path *path, int flags)\n\
132-
{\n\t\
133-
struct mount *mnt = real_mount(path->mnt);\n\t\
134-
int ret;\n\t\
135-
ret = can_umount(path, flags);\n\t\
136-
if (!ret)\n\t\t\
137-
ret = do_umount(mnt, flags);\n\t\
138-
dput(path->dentry);\n\t\
139-
mntput_no_expire(mnt);\n\t\
140-
return ret;\n\
141-
}\n
142-
$(shell sed -i '/^static bool is_mnt_ns_file/i $(PATH_UMOUNT)' $(srctree)/fs/namespace.c;)
112+
ifeq ($(shell grep -q "strncpy_from_unsafe_user" $(srctree)/include/linux/uaccess.h; echo $$?),0)
113+
$(info -- KernelSU/compat: strncpy_from_unsafe_user found)
114+
ccflags-y += -DKSU_STRNCPY_FROM_UNSAFE_USER
143115
endif
144116

145-
ifneq ($(shell grep -Eq "^int path_umount" $(srctree)/fs/internal.h; echo $$?),0)
146-
$(shell sed -i '/^extern void __init mnt_init/a int path_umount(struct path *path, int flags);' $(srctree)/fs/internal.h;)
147-
$(info -- KSU_NEXT: adding 'int path_umount(struct path *path, int flags);' to $(srctree)/fs/internal.h)
117+
ifeq ($(shell grep -q "ssize_t kernel_read" $(srctree)/fs/read_write.c; echo $$?),0)
118+
$(info -- KernelSU/compat: newer kernel_read found)
119+
ccflags-y += -DKSU_NEW_KERNEL_READ
120+
endif
121+
122+
ifeq ($(shell grep "ssize_t kernel_write" $(srctree)/fs/read_write.c | grep -q "const void" ; echo $$?),0)
123+
$(info -- KernelSU/compat: newer kernel_write found)
124+
ccflags-y += -DKSU_NEW_KERNEL_WRITE
148125
endif
149126

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

kernel/kernel_compat.c

Lines changed: 4 additions & 5 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) || defined(KSU_KERNEL_READ)
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) || defined(KSU_KERNEL_WRITE)
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;
@@ -140,14 +140,13 @@ long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
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)