kernel: increase reliability, add ksu_access_ok to simplify

Co-authored-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
Signed-off-by: rsuntk <rsuntk@yukiprjkt.my.id>
This commit is contained in:
rsuntk
2025-07-22 22:31:58 +07:00
committed by ShirkNeko
parent 39811e311f
commit 0c87765958
4 changed files with 18 additions and 9 deletions

View File

@@ -178,3 +178,12 @@ long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
return ret; return ret;
} }
#endif #endif
int ksu_access_ok(const void *addr, unsigned long size)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
return access_ok(addr, size);
#else
return access_ok(VERIFY_READ, addr, size);
#endif
}

View File

@@ -45,5 +45,5 @@ extern ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
loff_t *pos); loff_t *pos);
extern ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, extern ssize_t ksu_kernel_write_compat(struct file *p, const void *buf,
size_t count, loff_t *pos); size_t count, loff_t *pos);
extern int ksu_access_ok(const void *addr, unsigned long size);
#endif #endif

View File

@@ -577,6 +577,7 @@ static int ksu_execve_ksud_common(const char __user *filename_user,
{ {
struct filename filename_in, *filename_p; struct filename filename_in, *filename_p;
char path[32]; char path[32];
long len;
// return early if disabled. // return early if disabled.
if (!ksu_execveat_hook) { if (!ksu_execveat_hook) {
@@ -586,8 +587,11 @@ static int ksu_execve_ksud_common(const char __user *filename_user,
if (!filename_user) if (!filename_user)
return 0; return 0;
memset(path, 0, sizeof(path)); len = ksu_strncpy_from_user_nofault(path, filename_user, 32);
ksu_strncpy_from_user_nofault(path, filename_user, 32); if (len <= 0)
return 0;
path[sizeof(path) - 1] = '\0';
// this is because ksu_handle_execveat_ksud calls it filename->name // this is because ksu_handle_execveat_ksud calls it filename->name
filename_in.name = path; filename_in.name = path;

View File

@@ -155,13 +155,9 @@ int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
* some cpus dont really have that good speculative execution * some cpus dont really have that good speculative execution
* access_ok to substitute set_fs, we check if pointer is accessible * access_ok to substitute set_fs, we check if pointer is accessible
*/ */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) if (!ksu_access_ok((const void *)filename_user, sizeof(path)))
if (!access_ok(VERIFY_READ, *filename_user, sizeof(path)))
return 0; return 0;
#else
if (!access_ok(*filename_user, sizeof(path)))
return 0;
#endif
// success = returns number of bytes and should be less than path // success = returns number of bytes and should be less than path
long len = strncpy_from_user(path, *filename_user, sizeof(path)); long len = strncpy_from_user(path, *filename_user, sizeof(path));
if (len <= 0) if (len <= 0)