From 6f6dd293063a0513b94e66f4ba51b7ea0aa30093 Mon Sep 17 00:00:00 2001 From: Rifat Azad Date: Thu, 16 Jan 2025 15:41:48 +0600 Subject: [PATCH] kernel: auto backport get_cred_rcu --- kernel/Makefile | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index c4e25fd4..fa854af8 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -56,15 +56,27 @@ $(info -- KernelSU-Next Manager signature hash: $(KSU_NEXT_EXPECTED_HASH)) ccflags-y += -DEXPECTED_NEXT_SIZE=$(KSU_NEXT_EXPECTED_SIZE) ccflags-y += -DEXPECTED_NEXT_HASH=\"$(KSU_NEXT_EXPECTED_HASH)\" +ccflags-y += -DKSU_COMPAT_GET_CRED_RCU + ccflags-y += -DKSU_UMOUNT # This feature is introduced in linux 5.0-rc1 -ifeq ($(shell grep -q "get_cred_rcu" $(srctree)/include/linux/cred.h; echo $$?),0) -ccflags-y += -DKSU_COMPAT_GET_CRED_RCU -else -$(info -- Your kernel does not support get_cred_rcu, please backport it.) -$(info -- How to backport: https://github.com/tiann/KernelSU/pull/2320#issuecomment-2564232958) -$(info -- Fallback to old changes.) +ifneq ($(shell grep -Eq "^static inline const struct cred \*get_cred_rcu" $(srctree)/include/linux/cred.h; echo $$?),0) +$(info -- KSU_NEXT: adding function 'static inline const struct cred *get_cred_rcu(const struct cred *cred);' to $(srctree)/include/linux/cred.h) +GET_CRED_RCU = static inline const struct cred *get_cred_rcu(const struct cred *cred)\n\ +{\n\t\ + struct cred *nonconst_cred = (struct cred *) cred;\n\t\ + if (!cred)\n\t\t\ + return NULL;\n\t\ + if (!atomic_inc_not_zero(&nonconst_cred->usage))\n\t\t\ + return NULL;\n\t\ + validate_creds(cred);\n\t\ + return cred;\n\ +}\n +$(shell grep -qF "$(GET_CRED_RCU)" $(srctree)/include/linux/cred.h || sed -i '/^static inline void put_cred/i $(GET_CRED_RCU)' $(srctree)/include/linux/cred.h) + +$(info -- KSU_NEXT: modifying 'get_task_cred' function in $(srctree)/kernel/cred.c) +$(shell sed -i 's/!atomic_inc_not_zero(&((struct cred \*)cred)->usage)/!get_cred_rcu(cred)/g' $(srctree)/kernel/cred.c) endif ifneq ($(shell grep -Eq "^static int can_umount" $(srctree)/fs/namespace.c; echo $$?),0)