Fix race in process_vm_rw_core
Subsystems (6)
PATCH 1/1 Fix race in process_vm_rw_core
Hi Linus,
Below is a patch which fixes the race in process_vm_core found by Oleg (http://article.gmane.org/gmane.linux.kernel/1235667/).
It consolidates some code with mm_for_maps since what they do is almost identical.
Oleg - I've kept the breakout of ptrace_may_attach and get_task_mm to preserve only having to take the task lock once. I see some performance difference with a microbenchmark but haven't had a chance to test with some HPC benchmarks yet so for the moment I'd like to leave it in. At this stage I think its more important to get the race fixed and I'm at Linux.conf.au all next week. I'll send a patch out for the rw_copy_check_uvector cleanup after I get back from LCA.
Regards,
Chris
Signature · 3 lines
[email protected] Signed-off-by: Chris Yeoh <[email protected]> Cc: [email protected]
fs/proc/base.c +1 -16
@@ -254,22 +254,7 @@ static struct mm_struct *check_mem_permission(struct task_struct *task)struct mm_struct *mm_for_maps (struct task_struct *task) { -struct mm_struct *mm; -int err; - - err =mutex_lock_killable (&task->signal->cred_guard_mutex); -if (err) -return ERR_PTR (err); - - mm =get_task_mm (task); -if (mm && mm != current->mm && - !ptrace_may_access (task,PTRACE_MODE_READ )) { -mmput (mm); - mm =ERR_PTR (-EACCES ); - } -mutex_unlock (&task->signal->cred_guard_mutex); - -return mm; +return get_check_task_mm (task,PTRACE_MODE_READ ); }static int proc_pid_cmdline (struct task_struct *task,char * buffer)
include/linux/sched.h +4 -0
@@ -2235,6 +2235,10 @@ static inline void mmdrop(struct mm_struct * mm)extern void mmput (struct mm_struct *);/* Grab a reference to a task's mm, if it is not already going away */ extern struct mm_struct *get_task_mm (struct task_struct *task); +/* Grab a reference to a task's mm, if it is not already going away +and ptrace_may_access with the mode parameter passed to it succeeds */ +extern struct mm_struct *get_check_task_mm (struct task_struct *task, +unsigned int mode);/* Remove the current tasks stale references to the old mm_struct */ extern void mm_release (struct task_struct *,struct mm_struct *);/* Allocate a new mm structure and copy contents from tsk->mm */
kernel/fork.c +31 -0
@@ -644,6 +644,37 @@ struct mm_struct *get_task_mm(struct task_struct *task) }EXPORT_SYMBOL_GPL (get_task_mm); +struct mm_struct *get_check_task_mm (struct task_struct *task,unsigned int mode) +{ +struct mm_struct *mm; +int err; + + err =mutex_lock_killable (&task->signal->cred_guard_mutex); +if (err) +return ERR_PTR (err); + +task_lock (task); +if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + } + + mm = task->mm; +if (mm) { +if (task->flags &PF_KTHREAD ) + mm =NULL ; +else +atomic_inc (&mm->mm_users); + } + +out: +task_unlock (task); +mutex_unlock (&task->signal->cred_guard_mutex); + +return mm; +} +EXPORT_SYMBOL_GPL (get_check_task_mm); +/* Please note the differences between mmput and mm_release. * mmput is called whenever we stop holding onto a mm_struct, * error success whatever.
mm/process_vm_access.c +6 -14
@@ -298,23 +298,15 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto free_proc_pages; } -task_lock (task); -if (__ptrace_may_access (task,PTRACE_MODE_ATTACH )) { -task_unlock (task); - rc = -EPERM ; - goto put_task_struct; - } - mm = task->mm; - -if (!mm || (task->flags &PF_KTHREAD )) { -task_unlock (task); - rc = -EINVAL ; + mm =get_check_task_mm (task,PTRACE_MODE_ATTACH ); +if (!mm ||IS_ERR (mm)) { +if (!mm) + rc = -EINVAL ; +else + rc = -EPERM ; goto put_task_struct; } -atomic_inc (&mm->mm_users); -task_unlock (task); -for (i =0 ; i < riovcnt && iov_l_curr_idx < liovcnt; i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
Quoted 8 lines · Christopher Yeoh
Hi Linus,
Below is a patch which fixes the race in
process_vm_corefound by Oleg (http://article.gmane.org/gmane.linux.kernel/1235667/).
It consolidates some code withmm_for_mapssince what they do is almost identical.
Quoted 4 lines
Oleg - I've kept the breakout of
ptrace_may_attachandget_task_mmto preserve only having to take the task lock once. I see some performance difference with a microbenchmark but haven't had a chance to test with some HPC benchmarks yet so for the moment I'd like to leave it in.
I still think we should avoid the copy-and-paste code, we can do this without the extra unlock+lock if it hurts.
However,
Quoted 4 lines
At
this stage I think its more important to get the race fixed and I'm at Linux.conf.au all next week. I'll send a patch out for therw_copy_check_uvectorcleanup after I get back from LCA.
OK, lets fix the bug first.
Quoted 25 lines
struct
mm_struct*mm_for_maps(structtask_struct*task) {fs/proc/base.c +5
-16-struct mm_struct *mm; -int err; - - err =mutex_lock_killable (&task->signal->cred_guard_mutex); -if (err) -return ERR_PTR (err); - - mm =get_task_mm (task); -if (mm && mm != current->mm && - !ptrace_may_access (task,PTRACE_MODE_READ )) { -mmput (mm); - mm =ERR_PTR (-EACCES ); - } -mutex_unlock (&task->signal->cred_guard_mutex); - -return mm; +return get_check_task_mm (task,PTRACE_MODE_READ ); } +struct mm_struct *get_check_task_mm (struct task_struct *task,unsigned int mode) +{ +struct mm_struct *mm; +int err;
fs/proc/base.c static struct mm_struct *check_mem_permission(struct task_struct *task)
Quoted 10 lines
fs/proc/base.c +10
-0+ + err =mutex_lock_killable (&task->signal->cred_guard_mutex); +if (err) +return ERR_PTR (err); + +task_lock (task); +if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + }
Probably you should check "mm != current->mm" before __ptrace_may_access(), otherwise this changes the rules for, say, /proc/pid/maps.
Quoted 11 lines
mm/process_vm_access.c +0
-7@@ -298,23 +298,15 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto free_proc_pages; } -task_lock (task); -if (__ptrace_may_access (task,PTRACE_MODE_ATTACH )) { -task_unlock (task); - rc = -EPERM ; - goto put_task_struct; - } - mm = task->mm;
mm/process_vm_access.c static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, · line 306
Quoted 10 lines
mm/process_vm_access.c +6
-4- -if (!mm || (task->flags &PF_KTHREAD )) { -task_unlock (task); - rc = -EINVAL ; + mm =get_check_task_mm (task,PTRACE_MODE_ATTACH ); +if (!mm ||IS_ERR (mm)) { +if (!mm) + rc = -EINVAL ; +else + rc = -EPERM ;
Cosmetic nit. I won't insist, but why -EPERM is better than -EACCES returned by get_check_task_mm()? IOW, why not rc = PTR_ERR() ?
Note that get_check_task_mm() can return -EINTR, in this case -EPERM looks confusing even if this doesn't really materr (the killed task can't return to usermode).
Oleg.
Quoted 13 lines · On Fri, 13 Jan 2012 17:04:42 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 12 lines · Christopher Yeoh
...
+structmm_struct*get_check_task_mm(structtask_struct*task, unsigned int mode) +{kernel/fork.c +4
-0+struct mm_struct *mm; +int err; + + err =
mutex_lock_killable(&task->signal->cred_guard_mutex);kernel/fork.c +4
-0+if (err) +return ERR_PTR (err); + +task_lock (task);
Quoted 8 lines
kernel/fork.c struct mm_struct *get_task_mm(struct task_struct *task) · line 660
Quoted 4 lines
kernel/fork.c +4
-0+if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + }Probably you should check "mm !=
current->mm" before__ptrace_may_access(), otherwise this changes the rules for, say,/proc/pid/maps.
__ptrace_may_access has a check for task == current already - Is that sufficient?
/* Don't let security modules deny introspection */ if (task == current) return 0;
Quoted 16 lines
Quoted 16 lines
mm/process_vm_access.c +1
-11@@ -298,23 +298,15 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto free_proc_pages; } -task_lock (task); -if (__ptrace_may_access (task,PTRACE_MODE_ATTACH )) { -task_unlock (task); - rc = -EPERM ; - goto put_task_struct; - } - mm = task->mm; - -if (!mm || (task->flags &PF_KTHREAD )) { -task_unlock (task); - rc = -EINVAL ; + mm =get_check_task_mm (task,PTRACE_MODE_ATTACH );
Quoted 8 lines
mm/process_vm_access.c static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, · line 306
Quoted 5 lines
mm/process_vm_access.c +5
-0+if (!mm ||IS_ERR (mm)) { +if (!mm) + rc = -EINVAL ; +else + rc = -EPERM ;Cosmetic nit. I won't insist, but why
-EPERMis better than-EACCESreturned byget_check_task_mm()? IOW, why not rc =PTR_ERR()?
Maybe I should just convert EACCES to EPERM for process_vm_rw_core. I left get_check_task_mm with EACCESS to preserve existing behaviour for mm_for_maps.
SUSv3 defines EACCES and EPERM as
[EACCES]
Permission denied. An attempt was made to access a file in a way forbidden by its file access permissions.
[EPERM]
Operation not permitted. An attempt was made to perform an operation limited to processes with appropriate privileges or to the owner of a file or other resource.
So EPERM is more appropriate for process_vm_readv/writev
Chris
Signature · 1 line
[email protected]
Quoted 25 lines · Christopher Yeoh
Quoted 13 lines · On Fri, 13 Jan 2012 17:04:42 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 12 lines · Christopher Yeoh
...
+structmm_struct*get_check_task_mm(structtask_struct*task, unsigned int mode) +{kernel/fork.c +4
-0+struct mm_struct *mm; +int err; + + err =
mutex_lock_killable(&task->signal->cred_guard_mutex);kernel/fork.c +4
-0+if (err) +return ERR_PTR (err); + +task_lock (task);Quoted 8 lines
kernel/fork.c struct mm_struct *get_task_mm(struct task_struct *task) · line 660
Quoted 4 lines
kernel/fork.c +4
-0+if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + }Probably you should check "mm !=
current->mm" before__ptrace_may_access(), otherwise this changes the rules for, say,/proc/pid/maps.
Quoted 6 lines
__ptrace_may_accesshas a check for task == current already - Is that sufficient?/* Don't let security modules deny introspection */ if (task == current) return 0;
I don't think this is sufficient in the multithreaded or CLONE_VM case, task_cred/etc is per-thread.
It is not that I think that this "current->mm != mm" check is important, in fact personally I think it shouldn't exist.
But we shouldn't add the subtle and not documented behavioural change, and obviously process_vm_rw() has no security problems if mm == current->mm.
Quoted 20 lines
Quoted 9 lines
mm/process_vm_access.c static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, · line 306
Quoted 6 lines
mm/process_vm_access.c +6
-0+ mm =get_check_task_mm (task,PTRACE_MODE_ATTACH ); +if (!mm ||IS_ERR (mm)) { +if (!mm) + rc = -EINVAL ; +else + rc = -EPERM ;Cosmetic nit. I won't insist, but why
-EPERMis better than-EACCESreturned byget_check_task_mm()? IOW, why not rc =PTR_ERR()?Maybe I should just convert EACCES to EPERM for
process_vm_rw_core. I leftget_check_task_mmwith EACCESS to preserve existing behaviour formm_for_maps.SUSv3 defines EACCES and EPERM as
[EACCES]
Permission denied. An attempt was made to access a file in a way forbidden by its file access permissions.
Quoted 6 lines
[EPERM]
Operation not permitted. An attempt was made to perform an operation limited to processes with appropriate privileges or to the owner of a file or other resource.So EPERM is more appropriate for process_vm_readv/writev
Well, imho EACCES would be fine too and my point was s/EINTR/EPERM/ looks a bit confusing.
But OK, this is subjective and minor, I won't argue.
Oleg.
Quoted 36 lines · On Sat, 14 Jan 2012 18:58:29 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 25 lines · Christopher Yeoh
Quoted 13 lines · On Fri, 13 Jan 2012 17:04:42 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 12 lines · Christopher Yeoh
...
+structmm_struct*get_check_task_mm(structtask_struct*task, unsigned int mode) +{(quoted diff) +4
-0+ struct mm_struct *mm; + int err; + + err =
mutex_lock_killable(&task->signal->cred_guard_mutex);(quoted diff) +4
-0+ if (err) + return ERR_PTR(err); + + task_lock(task);Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ if (__ptrace_may_access(task, mode)) { + mm = ERR_PTR(-EACCES); + goto out; + }Probably you should check "mm !=
current->mm" before__ptrace_may_access(), otherwise this changes the rules for, say,/proc/pid/maps.Quoted 6 lines
__ptrace_may_accesshas a check for task == current already - Is that sufficient?/* Don't let security modules deny introspection */ if (task == current) return 0;I don't think this is sufficient in the multithreaded or
CLONE_VMcase, task_cred/etc is per-thread.
Quoted 7 lines
It is not that I think that this "
current->mm!= mm" check is important, in fact personally I think it shouldn't exist.But we shouldn't add the subtle and not documented behavioural change, and obviously
process_vm_rw()has no security problems if mm ==current->mm.
Ok, updated patch below:
- adds the "current->mm != mm" check
- removes EXPORT_SYMBOL_GPL for get_check_task_mm
fs/proc/base.c | 17 +---------------- include/linux/sched.h | 4 ++++ kernel/fork.c | 30 ++++++++++++++++++++++++++++++ mm/process_vm_access.c | 20 ++++++-------------- 4 files changed, 41 insertions(+), 30 deletions(-)
---
fs/proc/base.c +1 -16
@@ -254,22 +254,7 @@ static struct mm_struct *check_mem_permission(struct task_struct *task)struct mm_struct *mm_for_maps (struct task_struct *task) { -struct mm_struct *mm; -int err; - - err =mutex_lock_killable (&task->signal->cred_guard_mutex); -if (err) -return ERR_PTR (err); - - mm =get_task_mm (task); -if (mm && mm != current->mm && - !ptrace_may_access (task,PTRACE_MODE_READ )) { -mmput (mm); - mm =ERR_PTR (-EACCES ); - } -mutex_unlock (&task->signal->cred_guard_mutex); - -return mm; +return get_check_task_mm (task,PTRACE_MODE_READ ); }static int proc_pid_cmdline (struct task_struct *task,char * buffer)
include/linux/sched.h +4 -0
@@ -2235,6 +2235,10 @@ static inline void mmdrop(struct mm_struct * mm)extern void mmput (struct mm_struct *);/* Grab a reference to a task's mm, if it is not already going away */ extern struct mm_struct *get_task_mm (struct task_struct *task); +/* Grab a reference to a task's mm, if it is not already going away +and ptrace_may_access with the mode parameter passed to it succeeds */ +extern struct mm_struct *get_check_task_mm (struct task_struct *task, +unsigned int mode);/* Remove the current tasks stale references to the old mm_struct */ extern void mm_release (struct task_struct *,struct mm_struct *);/* Allocate a new mm structure and copy contents from tsk->mm */
kernel/fork.c +30 -0
@@ -644,6 +644,36 @@ struct mm_struct *get_task_mm(struct task_struct *task) }EXPORT_SYMBOL_GPL (get_task_mm); +struct mm_struct *get_check_task_mm (struct task_struct *task,unsigned int mode) +{ +struct mm_struct *mm; +int err; + + err =mutex_lock_killable (&task->signal->cred_guard_mutex); +if (err) +return ERR_PTR (err); + +task_lock (task); + mm = task->mm; +if (mm != current->mm &&__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + } + +if (mm) { +if (task->flags &PF_KTHREAD ) + mm =NULL ; +else +atomic_inc (&mm->mm_users); + } + +out: +task_unlock (task); +mutex_unlock (&task->signal->cred_guard_mutex); + +return mm; +} +/* Please note the differences between mmput and mm_release. * mmput is called whenever we stop holding onto a mm_struct, * error success whatever.
mm/process_vm_access.c +6 -14
@@ -298,23 +298,15 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto free_proc_pages; } -task_lock (task); -if (__ptrace_may_access (task,PTRACE_MODE_ATTACH )) { -task_unlock (task); - rc = -EPERM ; - goto put_task_struct; - } - mm = task->mm; - -if (!mm || (task->flags &PF_KTHREAD )) { -task_unlock (task); - rc = -EINVAL ; + mm =get_check_task_mm (task,PTRACE_MODE_ATTACH ); +if (!mm ||IS_ERR (mm)) { +if (!mm) + rc = -EINVAL ; +else + rc = -EPERM ; goto put_task_struct; } -atomic_inc (&mm->mm_users); -task_unlock (task); -for (i =0 ; i < riovcnt && iov_l_curr_idx < liovcnt; i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
Quoted 13 lines · Christopher Yeoh
kernel/fork.c +12
-0+struct mm_struct *get_check_task_mm (struct task_struct *task,unsigned int mode) +{ +struct mm_struct *mm; +int err; + + err =mutex_lock_killable (&task->signal->cred_guard_mutex); +if (err) +return ERR_PTR (err); + +task_lock (task); + mm = task->mm; +if (mm != current->mm &&__ptrace_may_access (task, mode)) {
kernel/fork.c struct mm_struct *get_task_mm(struct task_struct *task) · line 668
Quoted 10 lines
kernel/fork.c +10
-0+ mm =ERR_PTR (-EACCES ); + goto out; + } + +if (mm) { +if (task->flags &PF_KTHREAD ) + mm =NULL ; +else +atomic_inc (&mm->mm_users); + }
This still looks a bit strange, we call __ptrace_may_access() before we check ->mm != NULL even if this is safe... Really, we would simply fix the bug then try to microoptimize this code.
But OK, I promised I won't argue ;)
I believe the patch is correct and fixes the problem.
Oleg.
Andrew Morton
maintainerQuoted 37 lines · On Fri, 13 Jan 2012 22:00:28 +1030 Christopher Yeoh <[email protected]> wrote:
Hi Linus,
Below is a patch which fixes the race in
process_vm_corefound by Oleg (http://article.gmane.org/gmane.linux.kernel/1235667/).
It consolidates some code withmm_for_mapssince what they do is almost identical.Oleg - I've kept the breakout of
ptrace_may_attachandget_task_mmto preserve only having to take the task lock once. I see some performance difference with a microbenchmark but haven't had a chance to test with some HPC benchmarks yet so for the moment I'd like to leave it in. At this stage I think its more important to get the race fixed and I'm at Linux.conf.au all next week. I'll send a patch out for therw_copy_check_uvectorcleanup after I get back from LCA....
kernel/fork.c +20
-0+struct mm_struct *get_check_task_mm (struct task_struct *task,unsigned int mode) +{ +struct mm_struct *mm; +int err; + + err =mutex_lock_killable (&task->signal->cred_guard_mutex); +if (err) +return ERR_PTR (err); + +task_lock (task); +if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + } + + mm = task->mm; +if (mm) { +if (task->flags &PF_KTHREAD ) + mm =NULL ; +else
kernel/fork.c struct mm_struct *get_task_mm(struct task_struct *task) · line 676
Quoted 10 lines
kernel/fork.c +10
-0+atomic_inc (&mm->mm_users); + } + +out: +task_unlock (task); +mutex_unlock (&task->signal->cred_guard_mutex); + +return mm; +} +EXPORT_SYMBOL_GPL (get_check_task_mm);
I don't think the export is needed - CONFIG_PROC_FS=m isn't supported.
btw, I'm trying to work out why we didn't make the whole process_vm_access.o feature Kconfigurable, so people who don't want it do not get burdened with it?
Quoted 25 lines · On Fri, 13 Jan 2012 14:30:50 -0800 Andrew Morton <[email protected]>…
Quoted 25 lines
+struct
mm_struct*get_check_task_mm(structtask_struct*task, unsigned int mode) +{kernel/fork.c +4
-0+struct mm_struct *mm; +int err; + + err =
mutex_lock_killable(&task->signal->cred_guard_mutex);kernel/fork.c +18
-0+if (err) +return ERR_PTR (err); + +task_lock (task); +if (__ptrace_may_access (task, mode)) { + mm =ERR_PTR (-EACCES ); + goto out; + } + + mm = task->mm; +if (mm) { +if (task->flags &PF_KTHREAD ) + mm =NULL ; +else +atomic_inc (&mm->mm_users); + } + +out:
Quoted 8 lines
kernel/fork.c struct mm_struct *get_task_mm(struct task_struct *task) · line 676
Quoted 6 lines
kernel/fork.c +6
-0+task_unlock (task); +mutex_unlock (&task->signal->cred_guard_mutex); + +return mm; +} +EXPORT_SYMBOL_GPL (get_check_task_mm);I don't think the export is needed -
CONFIG_PROC_FS=misn't supported.
ok.
Quoted 3 lines
btw, I'm trying to work out why we didn't make the whole process_vm_access.o feature Kconfigurable, so people who don't want it do not get burdened with it?
I don't know. I'll make this change if you'd like but I won't have time to do it before leaving for LCA tomorrow. Have a toddler about to arrive at my house.
Chris
Signature · 1 line
[email protected]