Skip to content

Commit 89d54d5

Browse files
committed
cleanup get_file_rcu() which is changed from 6.7
define our own rather than using kernel's rcu-protected implementation. it's guaranteed that our usecases are in atomic context or task_lock. Signed-off-by: shenping.matt <shenping.matt@bytedance.com>
1 parent 5598890 commit 89d54d5

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

driver/LKM/include/smith_hook.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
#include <linux/kmod.h>
4747
#include <linux/dcache.h>
4848

49-
#ifndef get_file_rcu
50-
#define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count)
51-
#endif
52-
5349
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
5450
#define __ARG_PLACEHOLDER_1 0,
5551
#define config_enabled(cfg) _config_enabled(cfg)

driver/LKM/src/smith_hook.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ static char *smith_d_path(const struct path *path, char *buf, int buflen)
457457
return name;
458458
}
459459

460+
/* only inc f_count when it's not 0 to avoid races upon exe_file */
461+
#define smith_get_file(x) atomic_long_inc_not_zero(&(x)->f_count)
462+
460463
/*
461464
* query task's executable image file, with mmap lock avoided, just because
462465
* mmput() could lead resched() (since it's calling might_sleep() interally)
@@ -478,14 +481,8 @@ static struct file *smith_get_task_exe_file(struct task_struct *task)
478481
task_lock(task);
479482
if (task->mm && task->mm->exe_file) {
480483
exe = task->mm->exe_file;
481-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
482-
if (!get_file_rcu(exe))
483-
exe = NULL;
484-
#else
485-
/* only inc f_count when it's not 0 to avoid races upon exe_file */
486-
if (!atomic_long_inc_not_zero(&exe->f_count))
484+
if (!smith_get_file(exe))
487485
exe = NULL;
488-
#endif
489486
}
490487
task_unlock(task);
491488

@@ -2955,7 +2952,7 @@ static int mprotect_pre_handler(struct kprobe *p, struct pt_regs *regs)
29552952
rcu_read_lock();
29562953
if (!IS_ERR_OR_NULL(vma->vm_mm)) {
29572954
if (!IS_ERR_OR_NULL(&vma->vm_mm->exe_file)) {
2958-
if (get_file_rcu(vma->vm_mm->exe_file)) {
2955+
if (smith_get_file(vma->vm_mm->exe_file)) {
29592956
file_buf = smith_kzalloc(PATH_MAX, GFP_ATOMIC);
29602957
file_path = smith_d_path(&vma->vm_mm->exe_file->f_path, file_buf, PATH_MAX);
29612958
smith_fput(vma->vm_mm->exe_file);
@@ -2967,9 +2964,8 @@ static int mprotect_pre_handler(struct kprobe *p, struct pt_regs *regs)
29672964
}
29682965

29692966
if (!IS_ERR_OR_NULL(vma->vm_file)) {
2970-
if (get_file_rcu(vma->vm_file)) {
2971-
vm_file_buff =
2972-
smith_kzalloc(PATH_MAX, GFP_ATOMIC);
2967+
if (smith_get_file(vma->vm_file)) {
2968+
vm_file_buff = smith_kzalloc(PATH_MAX, GFP_ATOMIC);
29732969
vm_file_path = smith_d_path(&vma->vm_file->f_path, vm_file_buff, PATH_MAX);
29742970
smith_fput(vma->vm_file);
29752971
}

0 commit comments

Comments
 (0)