Skip to content

Commit

Permalink
kernel: auto backport get_cred_rcu
Browse files Browse the repository at this point in the history
  • Loading branch information
rifsxd committed Jan 16, 2025
1 parent 7314c8e commit 2710048
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,32 @@ $(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_RCPU = 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_RCPU)" $(srctree)/include/linux/cred.h || sed -i '/^static inline void put_cred/i $(GET_CRED_RCPU)' $(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 "^const struct cred \*get_task_cred" $(srctree)/kernel/cred.c; echo $$?),0)
$(info -- KSU_NEXT: modifying 'get_task_cred' function in $(srctree)/kernel/cred.c)
$(shell sed -i 's/} while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));/} while (!get_cred_rcu(cred));/' $(srctree)/kernel/cred.c)
endif

ifneq ($(shell grep -Eq "^static int can_umount" $(srctree)/fs/namespace.c; echo $$?),0)
Expand Down

0 comments on commit 2710048

Please sign in to comment.