@@ -457,6 +457,9 @@ static char *smith_d_path(const struct path *path, char *buf, int buflen)
457
457
return name ;
458
458
}
459
459
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
+
460
463
/*
461
464
* query task's executable image file, with mmap lock avoided, just because
462
465
* 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)
478
481
task_lock (task );
479
482
if (task -> mm && task -> mm -> exe_file ) {
480
483
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 ))
487
485
exe = NULL ;
488
- #endif
489
486
}
490
487
task_unlock (task );
491
488
@@ -2955,7 +2952,7 @@ static int mprotect_pre_handler(struct kprobe *p, struct pt_regs *regs)
2955
2952
rcu_read_lock ();
2956
2953
if (!IS_ERR_OR_NULL (vma -> vm_mm )) {
2957
2954
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 )) {
2959
2956
file_buf = smith_kzalloc (PATH_MAX , GFP_ATOMIC );
2960
2957
file_path = smith_d_path (& vma -> vm_mm -> exe_file -> f_path , file_buf , PATH_MAX );
2961
2958
smith_fput (vma -> vm_mm -> exe_file );
@@ -2967,9 +2964,8 @@ static int mprotect_pre_handler(struct kprobe *p, struct pt_regs *regs)
2967
2964
}
2968
2965
2969
2966
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 );
2973
2969
vm_file_path = smith_d_path (& vma -> vm_file -> f_path , vm_file_buff , PATH_MAX );
2974
2970
smith_fput (vma -> vm_file );
2975
2971
}
0 commit comments