kernel: add close_fd helper, debloat dmesg log

Signed-off-by: fc5b87cf <rissu.ntk@gmail.com>
This commit is contained in:
fc5b87cf
2025-11-17 21:44:24 +07:00
committed by ShirkNeko
parent 6e44090e57
commit 83db28b262
6 changed files with 21 additions and 28 deletions

View File

@@ -436,7 +436,7 @@ void persistent_allow_list(void)
goto put_task; goto put_task;
} }
cb->func = do_persistent_allow_list; cb->func = do_persistent_allow_list;
ksu_task_work_add(tsk, cb, TWA_RESUME); task_work_add(tsk, cb, TWA_RESUME);
put_task: put_task:
put_task_struct(tsk); put_task_struct(tsk);

View File

@@ -81,6 +81,7 @@ static long ksu_sys_setns(int fd, int flags)
PT_REGS_PARM1(&regs) = fd; PT_REGS_PARM1(&regs) = fd;
PT_REGS_PARM2(&regs) = flags; PT_REGS_PARM2(&regs) = flags;
// TODO: arm support
#if (defined(__aarch64__) || defined(__x86_64__)) #if (defined(__aarch64__) || defined(__x86_64__))
return SYS_SETNS_SYMBOL(&regs); return SYS_SETNS_SYMBOL(&regs);
#else #else
@@ -180,13 +181,7 @@ static void setup_mount_namespace(int32_t ns_mode)
if (ret) { if (ret) {
pr_warn("sys_setns failed: %ld\n", ret); pr_warn("sys_setns failed: %ld\n", ret);
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) do_close_fd(fd);
close_fd(fd);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0)
ksys_close(fd);
#else
sys_close(fd);
#endif
} }
if (ns_mode == 2) { if (ns_mode == 2) {

View File

@@ -4,6 +4,7 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/version.h> #include <linux/version.h>
#include <linux/task_work.h> #include <linux/task_work.h>
#include <linux/fdtable.h>
#include "ss/policydb.h" #include "ss/policydb.h"
#include "linux/key.h" #include "linux/key.h"
@@ -69,16 +70,21 @@ extern struct key *init_session_keyring;
// Linux >= 5.7 // Linux >= 5.7
// task_work_add (struct, struct, enum) // task_work_add (struct, struct, enum)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)
#define ksu_task_work_add(tsk, cb, notify) task_work_add(tsk, cb, notify)
#else
// Linux pre-5.7 // Linux pre-5.7
// task_work_add (struct, struct, bool) // task_work_add (struct, struct, bool)
#define ksu_task_work_add(tsk, cb, notify) task_work_add(tsk, cb, notify) #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
// Decoy, so it wouldn't complain.
#ifndef TWA_RESUME #ifndef TWA_RESUME
#define TWA_RESUME true #define TWA_RESUME true
#endif #endif
#endif #endif
static inline int do_close_fd(unsigned int fd)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
return close_fd(fd);
#else
return __close_fd(current->files, fd);
#endif
}
#endif #endif

View File

@@ -198,7 +198,7 @@ int ksu_handle_umount(uid_t old_uid, uid_t new_uid)
tw->old_cred = get_current_cred(); tw->old_cred = get_current_cred();
tw->cb.func = umount_tw_func; tw->cb.func = umount_tw_func;
int err = ksu_task_work_add(current, &tw->cb, TWA_RESUME); int err = task_work_add(current, &tw->cb, TWA_RESUME);
if (err) { if (err) {
if (tw->old_cred) { if (tw->old_cred) {
put_cred(tw->old_cred); put_cred(tw->old_cred);

View File

@@ -254,7 +254,7 @@ first_app_process:
rcu_read_lock(); rcu_read_lock();
init_task = rcu_dereference(current->real_parent); init_task = rcu_dereference(current->real_parent);
if (init_task) { if (init_task) {
ksu_task_work_add(init_task, &on_post_fs_data_cb, task_work_add(init_task, &on_post_fs_data_cb,
TWA_RESUME); TWA_RESUME);
} }
rcu_read_unlock(); rcu_read_unlock();

View File

@@ -669,11 +669,7 @@ static void ksu_install_fd_tw_func(struct callback_head *cb)
if (copy_to_user(tw->outp, &fd, sizeof(fd))) { if (copy_to_user(tw->outp, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n"); pr_err("install ksu fd reply err\n");
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) do_close_fd(fd);
close_fd(fd);
#else
ksys_close(fd);
#endif
} }
kfree(tw); kfree(tw);
@@ -699,7 +695,7 @@ static int reboot_handler_pre(struct kprobe *p, struct pt_regs *regs)
tw->outp = (int __user *)arg4; tw->outp = (int __user *)arg4;
tw->cb.func = ksu_install_fd_tw_func; tw->cb.func = ksu_install_fd_tw_func;
if (ksu_task_work_add(current, &tw->cb, TWA_RESUME)) { if (task_work_add(current, &tw->cb, TWA_RESUME)) {
kfree(tw); kfree(tw);
pr_warn("install fd add task_work failed\n"); pr_warn("install fd add task_work failed\n");
} }
@@ -730,13 +726,7 @@ int ksu_handle_sys_reboot(int magic1, int magic2, unsigned int cmd,
// downstream: dereference all arg usage! // downstream: dereference all arg usage!
if (copy_to_user((void __user *)*arg, &fd, sizeof(fd))) { if (copy_to_user((void __user *)*arg, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n"); pr_err("install ksu fd reply err\n");
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) do_close_fd(fd);
close_fd(fd);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0)
ksys_close(fd);
#else
sys_close(fd);
#endif
} }
return 0; return 0;
} }
@@ -842,8 +832,10 @@ int ksu_install_fd(void)
// Install fd // Install fd
fd_install(fd, filp); fd_install(fd, filp);
#ifdef CONFIG_KSU_DEBUG
pr_info("ksu fd[%d] installed for %s/%d\n", fd, current->comm, pr_info("ksu fd[%d] installed for %s/%d\n", fd, current->comm,
current->pid); current->pid);
#endif
return fd; return fd;
} }