diff --git a/kernel/kernel_compat.h b/kernel/kernel_compat.h index b5cc6c9a..3216f75d 100644 --- a/kernel/kernel_compat.h +++ b/kernel/kernel_compat.h @@ -4,6 +4,15 @@ #include "linux/fs.h" #include "linux/key.h" #include "linux/version.h" +#include "linux/uaccess.h" + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) +#define ksu_strncpy_from_user_nofault strncpy_from_user_nofault +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0) +#define ksu_strncpy_from_user_nofault strncpy_from_unsafe_user +#else +#define ksu_strncpy_from_user_nofault strncpy_from_user +#endif extern struct key *init_session_keyring; diff --git a/kernel/ksud.c b/kernel/ksud.c index 8ef4f445..42d0a1cd 100644 --- a/kernel/ksud.c +++ b/kernel/ksud.c @@ -1,7 +1,5 @@ #include "asm/current.h" -#include "linux/string.h" #include "linux/compat.h" -#include "linux/cred.h" #include "linux/dcache.h" #include "linux/err.h" #include "linux/fs.h" @@ -12,12 +10,12 @@ #include "linux/uaccess.h" #include "linux/version.h" #include "linux/workqueue.h" -#include "linux/input.h" #include "allowlist.h" #include "arch.h" #include "klog.h" // IWYU pragma: keep #include "ksud.h" +#include "kernel_compat.h" #include "selinux/selinux.h" static const char KERNEL_SU_RC[] = @@ -174,13 +172,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, const char __user *p = get_user_arg_ptr(*ptr, 1); if (p && !IS_ERR(p)) { char first_arg[16]; - #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) - strncpy_from_user_nofault(first_arg, p, sizeof(first_arg)); - #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0) - strncpy_from_unsafe_user(first_arg, p, sizeof(first_arg)); - #else - strncpy_from_user(first_arg, p, sizeof(first_arg)); - #endif + ksu_strncpy_from_user_nofault(first_arg, p, sizeof(first_arg)); pr_info("first arg: %s\n", first_arg); if (!strcmp(first_arg, "second_stage")) { pr_info("/system/bin/init second_stage executed\n"); diff --git a/kernel/sucompat.c b/kernel/sucompat.c index f09a0af6..3683cd55 100644 --- a/kernel/sucompat.c +++ b/kernel/sucompat.c @@ -16,16 +16,11 @@ #include "arch.h" #include "klog.h" // IWYU pragma: keep #include "ksud.h" +#include "kernel_compat.h" #define SU_PATH "/system/bin/su" #define SH_PATH "/system/bin/sh" -#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) -#define ksu_strncpy_from_user_nofault strncpy_from_user_nofault -#else -#define ksu_strncpy_from_user_nofault strncpy_from_unsafe_user -#endif - extern void escape_to_root(); static void __user *userspace_stack_buffer(const void *d, size_t len)