introduce get_task_exe_file and use it to fix audit_exe_compare
Subsystems (5)
v2 cover introduce get_task_exe_file and use it to fix audit_exe_compare
audit_exe_compare directly accesses mm->exe_file without making sure the object is stable. Fixing it using current primitives results in partially duplicating what proc_exe_link is doing.
As such, introduce a trivial helper which can be used in both places and fix the func.
Changes since v1:
* removed an unused 'out' label which crept in
Mateusz Guzik (2):
mm: introduce get_task_exe_file
audit: fix exe_file access in audit_exe_compare
fs/proc/base.c | 7 +------ include/linux/mm.h | 1 + kernel/audit_watch.c | 8 +++++--- kernel/fork.c | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/2 mm: introduce get_task_exe_file
For more convenient access if one has a pointer to the task.
As a minor nit take advantage of the fact that only task lock + rcu are needed to safely grab ->exe_file. This saves mm refcount dance.
Use the helper in proc_exe_link.
--- fs/proc/base.c | 7 +------ include/linux/mm.h | 1 + kernel/fork.c | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-)
fs/proc/base.c +1 -6
@@ -1556,18 +1556,13 @@ static const struct file_operations proc_pid_set_comm_operations = {static int proc_exe_link (struct dentry *dentry,struct path *exe_path) {struct task_struct *task; -struct mm_struct *mm;struct file *exe_file; task =get_proc_task (d_inode (dentry));if (!task)return -ENOENT ; - mm =get_task_mm (task); + exe_file =get_task_exe_file (task);put_task_struct (task); -if (!mm) -return -ENOENT ; - exe_file =get_mm_exe_file (mm); -mmput (mm);if (exe_file) { *exe_path = exe_file->f_path;path_get (&exe_file->f_path);
include/linux/mm.h +1 -0
@@ -2014,6 +2014,7 @@ extern void mm_drop_all_locks(struct mm_struct *mm);extern void set_mm_exe_file (struct mm_struct *mm,struct file *new_exe_file);extern struct file *get_mm_exe_file (struct mm_struct *mm); +extern struct file *get_task_exe_file (struct task_struct *task);extern bool may_expand_vm (struct mm_struct *,vm_flags_t ,unsigned long npages);extern void vm_stat_account (struct mm_struct *,vm_flags_t ,long npages);
kernel/fork.c +23 -0
@@ -800,6 +800,29 @@ struct file *get_mm_exe_file(struct mm_struct *mm)EXPORT_SYMBOL (get_mm_exe_file);/** +* get_task_exe_file - acquire a reference to the task's executable file +* +* Returns %NULL if task's mm (if any) has no associated executable file or +* this is a kernel thread with borrowed mm (see the comment above get_task_mm). +* User must release file via fput(). +*/ +struct file *get_task_exe_file (struct task_struct *task) +{ +struct file *exe_file =NULL ; +struct mm_struct *mm; + +task_lock (task); + mm = task->mm; +if (mm) { +if (!(task->flags &PF_KTHREAD )) + exe_file =get_mm_exe_file (mm); + } +task_unlock (task); +return exe_file; +} +EXPORT_SYMBOL (get_task_exe_file); + +/** * get_task_mm - acquire a reference to the task's mm * * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
Signature · 1 line
1.8.3.1
Re: [PATCHv2 1/2] mm: introduce get_task_exe_file
Quoted 5 lines · Mateusz Guzik
kernel/fork.c +4
-0+struct file *get_task_exe_file (struct task_struct *task) +{ +struct file *exe_file =NULL ; +struct mm_struct *mm;
kernel/fork.c struct file *get_mm_exe_file(struct mm_struct *mm) · line 822
Quoted 10 lines
kernel/fork.c +10
-0+ +task_lock (task); + mm = task->mm; +if (mm) { +if (!(task->flags &PF_KTHREAD )) + exe_file =get_mm_exe_file (mm); + } +task_unlock (task); +return exe_file; +}
I can't believe I am going to comment the coding style but I can't resist ;)
if (mm && !(task->flags & PF_KTHREAD))) exe_file = get_mm_exe_file(mm);
looks a bit simpler to me. But this is purely cosmetic and subjective, both patches look good to me.
Re: [PATCHv2 1/2] mm: introduce get_task_exe_file
Quoted 19 lines · Oleg Nesterov
Quoted 5 lines · Mateusz Guzik
kernel/fork.c +4
-0+struct file *get_task_exe_file (struct task_struct *task) +{ +struct file *exe_file =NULL ; +struct mm_struct *mm;kernel/fork.c struct file *get_mm_exe_file(struct mm_struct *mm) · line 822
Quoted 10 lines
kernel/fork.c +10
-0+ +task_lock (task); + mm = task->mm; +if (mm) { +if (!(task->flags &PF_KTHREAD )) + exe_file =get_mm_exe_file (mm); + } +task_unlock (task); +return exe_file; +}I can't believe I am going to comment the coding style but I can't resist ;)
Quoted 6 lines
if (mm && !(task->flags & PF_KTHREAD))) exe_file = get_mm_exe_file(mm);looks a bit simpler to me. But this is purely cosmetic and subjective, both patches look good to me.
Actually I did it for some consistency with get_task_mm.
The check can likely be done prior to taking the lock in both functions and that would clean them up a little bit, but I wanted to avoid nit picking... :>
Quoted 2 lines
Acked-by Oleg Nesterov <[email protected]>
Thanks
Signature · 1 line
Mateusz Guzik
PATCH v2 2/2 audit: fix exe_file access in audit_exe_compare
In reply to Mateusz GuzikPrior to the change the function would blindly deference mm, exe_file and exe_file->f_inode, each of which could have been NULL or freed.
Use get_task_exe_file to safely obtain stable exe_file.
--- kernel/audit_watch.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/audit_watch.c +5 -3
@@ -19,6 +19,7 @@* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <linux/file.h> #include <linux/kernel.h> #include <linux/audit.h> #include <linux/kthread.h>
@@ -544,10 +545,11 @@ int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)unsigned long ino;dev_t dev; -rcu_read_lock (); - exe_file =rcu_dereference (tsk->mm->exe_file); + exe_file =get_task_exe_file (tsk); +if (!exe_file) +return 0 ; ino = exe_file->f_inode->i_ino; dev = exe_file->f_inode->i_sb->s_dev; -rcu_read_unlock (); +fput (exe_file);return audit_mark_compare (mark, ino, dev); }
Signature · 1 line
1.8.3.1
Paul Moore
maintainerQuoted 14 lines · Mateusz Guzik
audit_exe_comparedirectly accessesmm->exe_filewithout making sure the object is stable. Fixing it using current primitives results in partially duplicating whatproc_exe_linkis doing.As such, introduce a trivial helper which can be used in both places and fix the func.
Changes since v1:
* removed an unused 'out' label which crept inMateusz Guzik (2):
mm: introduceget_task_exe_file
audit: fixexe_fileaccess inaudit_exe_compare
Quoted 5 lines
fs/proc/base.c | 7 +------ include/linux/mm.h | 1 + kernel/audit_watch.c | 8 +++++--- kernel/fork.c | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-)
Thanks for doing this.
Both patches look fine to me, does anyone in the mm area have any objections? If not, I'll merge these into the audit tree and mark them for stable.
Signature · 2 lines
paul moore www.paul-moore.com
Paul Moore
maintainerQuoted 21 lines · Paul Moore
Quoted 14 lines · Mateusz Guzik
audit_exe_comparedirectly accessesmm->exe_filewithout making sure the object is stable. Fixing it using current primitives results in partially duplicating whatproc_exe_linkis doing.As such, introduce a trivial helper which can be used in both places and fix the func.
Changes since v1:
* removed an unused 'out' label which crept inMateusz Guzik (2):
mm: introduceget_task_exe_file
audit: fixexe_fileaccess inaudit_exe_compareQuoted 5 lines
fs/proc/base.c | 7 +------ include/linux/mm.h | 1 + kernel/audit_watch.c | 8 +++++--- kernel/fork.c | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-)
Quoted 5 lines
Thanks for doing this.
Both patches look fine to me, does anyone in the mm area have any objections? If not, I'll merge these into the audit tree and mark them for stable.
I just merged these patches into audit#stable-4.8 and have a kernel building now, as soon as it finishes I'll do some quick sanity tests and send them off to Linus.
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 7 lines · Mateusz Guzik
audit_exe_comparedirectly accessesmm->exe_filewithout making sure the object is stable. Fixing it using current primitives results in partially duplicating whatproc_exe_linkis doing.As such, introduce a trivial helper which can be used in both places and fix the func.
Quoted 6 lines
Changes since v1:
* removed an unused 'out' label which crept inMateusz Guzik (2):
mm: introduceget_task_exe_file
audit: fixexe_fileaccess inaudit_exe_compare
The task_lock affects a much bigger struct than the mm ref count. Is this really necessary? Is a spin-lock significantly lower cost than a refcount? Other than that, this refactorization looks sensible.
Quoted 9 lines
fs/proc/base.c | 7 +------ include/linux/mm.h | 1 + kernel/audit_watch.c | 8 +++++--- kernel/fork.c | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-)Signature · 1 line
1.8.3.1
- RGB
--
Richard Guy Briggs <[email protected]>
Kernel Security Engineering, Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635
Quoted 11 lines · Richard Guy Briggs
Quoted 10 lines · Mateusz Guzik
audit_exe_comparedirectly accessesmm->exe_filewithout making sure the object is stable. Fixing it using current primitives results in partially duplicating whatproc_exe_linkis doing.As such, introduce a trivial helper which can be used in both places and fix the func.
Changes since v1:
* removed an unused 'out' label which crept in
Quoted 8 lines
Quoted 3 lines
Mateusz Guzik (2):
mm: introduceget_task_exe_file
audit: fixexe_fileaccess inaudit_exe_compareThe
task_lockaffects a much bigger struct than the mm ref count. Is this really necessary? Is a spin-lock significantly lower cost than a refcount? Other than that, this refactorization looks sensible.
proc_exe_link was taking the lock anyway to guarantee a stable mm. I think the helper cleans the code up a little bit and there is microoptimisation to not play with the refcount.
If audit_exe_compare has guarantees the task wont reach exit_mm, it can use get_mm_exe_file which means the atomic op would be only on the file object.
I was under the impression this is the expected behaviour, but your patch used the task lock to grab mm, so I mimicked it here.
Signature · 1 line
Mateusz Guzik