drm/msm: fix use of copy_from_user() while holding spinlock
Subsystems (3)
PATCH 1/2 drm/msm: fix use of copy_from_user() while holding spinlock
Use instead __copy_from_user_inatomic() and fallback to slow-path where we drop and re-aquire the lock in case of fault.
--- drivers/gpu/drm/msm/msm_gem_submit.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/gpu/drm/msm/msm_gem_submit.c +22 -5
@@ -66,6 +66,14 @@ void msm_gem_submit_free(struct msm_gem_submit *submit)kfree (submit); } +static inline unsigned long __must_check +copy_from_user_inatomic (void *to,const void __user *from,unsigned long n) +{ +if (access_ok (VERIFY_READ , from, n)) +return __copy_from_user_inatomic (to, from, n); +return -EFAULT ; +} +static int submit_lookup_objects (struct msm_gem_submit *submit,struct drm_msm_gem_submit *args,struct drm_file *file) {
@@ -73,6 +81,7 @@ static int submit_lookup_objects(struct msm_gem_submit *submit,int ret =0 ;spin_lock (&file->table_lock); +pagefault_disable ();for (i =0 ; i < args->nr_bos; i++) {struct drm_msm_gem_submit_bo submit_bo;
@@ -86,10 +95,15 @@ static int submit_lookup_objects(struct msm_gem_submit *submit,*/ submit->bos[i].flags =0 ; - ret =copy_from_user (&submit_bo, userptr,sizeof (submit_bo)); -if (ret) { - ret = -EFAULT ; - goto out_unlock; + ret =copy_from_user_inatomic (&submit_bo, userptr,sizeof (submit_bo)); +if (unlikely (ret)) { +pagefault_enable (); +spin_unlock (&file->table_lock); + ret =copy_from_user (&submit_bo, userptr,sizeof (submit_bo)); +if (ret) + goto out; +spin_lock (&file->table_lock); +pagefault_disable (); }if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS ) {
@@ -129,9 +143,12 @@ static int submit_lookup_objects(struct msm_gem_submit *submit, } out_unlock: - submit->nr_bos = i; +pagefault_enable ();spin_unlock (&file->table_lock); +out: + submit->nr_bos = i; +return ret; }
Signature · 6 lines
2.7.4 _______________________________________________ Freedreno mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/freedreno
PATCH 2/2 drm/msm: protect against faults from copy_from_user() in submit ioctl
An evil userspace could try to cause deadlock by passing an unfaulted-in GEM bo as submit->bos (or submit->cmds) table. Which will trigger msm_gem_fault() while we already hold struct_mutex. See:
https://github.com/freedreno/msmtest/blob/master/evilsubmittest.c
--- drivers/gpu/drm/msm/msm_drv.h | 6 ++++++ drivers/gpu/drm/msm/msm_gem.c | 9 +++++++++ drivers/gpu/drm/msm/msm_gem_submit.c | 3 +++ 3 files changed, 18 insertions(+)
drivers/gpu/drm/msm/msm_drv.h +6 -0
@@ -157,6 +157,12 @@ struct msm_drm_private {struct shrinker shrinker;struct msm_vblank_ctrl vblank_ctrl; + +/* task holding struct_mutex.. currently only used in submit path +* to detect and reject faults from copy_from_user() for submit +* ioctl. +*/ +struct task_struct *struct_mutex_task; };struct msm_format {
drivers/gpu/drm/msm/msm_gem.c +9 -0
@@ -196,11 +196,20 @@ int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct drm_gem_object *obj = vma->vm_private_data;struct drm_device *dev = obj->dev; +struct msm_drm_private *priv = dev->dev_private;struct page **pages;unsigned long pfn;pgoff_t pgoff;int ret; +/* This should only happen if userspace tries to pass a mmap'd +* but unfaulted gem bo vaddr into submit ioctl, triggering +* a page fault while struct_mutex is already held. This is +* not a valid use-case so just bail. +*/ +if (priv->struct_mutex_task == current) +return VM_FAULT_SIGBUS ; +/* Make sure we don't parallel update on a fault, nor move or remove * something from beneath our feet */
drivers/gpu/drm/msm/msm_gem_submit.c +3 -0
@@ -426,6 +426,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,if (ret)return ret; + priv->struct_mutex_task = current; +if (args->flags &MSM_SUBMIT_FENCE_FD_OUT ) { out_fence_fd =get_unused_fd_flags (O_CLOEXEC );if (out_fence_fd <0 ) {
@@ -549,6 +551,7 @@ out: out_unlock:if (ret && (out_fence_fd >=0 ))put_unused_fd (out_fence_fd); + priv->struct_mutex_task =NULL ;mutex_unlock (&dev->struct_mutex);return ret; }
Signature · 6 lines
2.7.4 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/2] drm/msm: protect against faults from copy_from_user() in submit ioctl
Quoted 41 lines · Rob Clark
An evil userspace could try to cause deadlock by passing an unfaulted-in GEM bo as
submit->bos(orsubmit->cmds) table. Which will triggermsm_gem_fault()while we already holdstruct_mutex. See:https://github.com/freedreno/msmtest/blob/master/evilsubmittest.c
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/msm/msm_drv.h | 6 ++++++ drivers/gpu/drm/msm/msm_gem.c | 9 +++++++++ drivers/gpu/drm/msm/msm_gem_submit.c | 3 +++ 3 files changed, 18 insertions(+)drivers/gpu/drm/msm/msm_drv.h +6
-0@@ -157,6 +157,12 @@ struct msm_drm_private {struct shrinker shrinker;struct msm_vblank_ctrl vblank_ctrl; + +/* task holding struct_mutex.. currently only used in submit path +* to detect and reject faults from copy_from_user() for submit +* ioctl. +*/ +struct task_struct *struct_mutex_task; };struct msm_format {drivers/gpu/drm/msm/msm_gem.c +1
-0@@ -196,11 +196,20 @@ int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct drm_gem_object *obj = vma->vm_private_data;struct drm_device *dev = obj->dev; +struct msm_drm_private *priv = dev->dev_private;struct page **pages;
drivers/gpu/drm/msm/msm_gem.c int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 210
Quoted 10 lines
drivers/gpu/drm/msm/msm_gem.c +6
-0unsigned long pfn;pgoff_t pgoff;int ret; +/* This should only happen if userspace tries to pass a mmap'd +* but unfaulted gem bo vaddr into submit ioctl, triggering +* a page fault while struct_mutex is already held. This is +* not a valid use-case so just bail. +*/ +if (priv->struct_mutex_task == current)
READ_ONCE here I think. Otherwise should at least work, though I still think it's sloppy ;-)
-Daniel
Quoted 29 lines
drivers/gpu/drm/msm/msm_gem.c +2
-0+return VM_FAULT_SIGBUS ; +/* Make sure we don't parallel update on a fault, nor move or remove * something from beneath our feet */ drivers/gpu/drm/msm/msm_gem_submit.c +3
-0@@ -426,6 +426,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,if (ret)return ret; + priv->struct_mutex_task = current; +if (args->flags &MSM_SUBMIT_FENCE_FD_OUT ) { out_fence_fd =get_unused_fd_flags (O_CLOEXEC );if (out_fence_fd <0 ) {@@ -549,6 +551,7 @@ out: out_unlock:if (ret && (out_fence_fd >=0 ))put_unused_fd (out_fence_fd); + priv->struct_mutex_task =NULL ;mutex_unlock (&dev->struct_mutex);return ret; }Signature · 1 line
2.7.4
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
A message that is not in the archive (sent off-list or not yet received): 20160823060306.GK24290-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org
Re: [PATCH 2/2] drm/msm: protect against faults from copy_from_user() in submit ioctl
Quoted 46 lines · Daniel Vetter
Quoted 45 lines · Rob Clark
An evil userspace could try to cause deadlock by passing an unfaulted-in GEM bo as
submit->bos(orsubmit->cmds) table. Which will triggermsm_gem_fault()while we already holdstruct_mutex. See:https://github.com/freedreno/msmtest/blob/master/evilsubmittest.c
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/msm/msm_drv.h | 6 ++++++ drivers/gpu/drm/msm/msm_gem.c | 9 +++++++++ drivers/gpu/drm/msm/msm_gem_submit.c | 3 +++ 3 files changed, 18 insertions(+)drivers/gpu/drm/msm/msm_drv.h +6
-0@@ -157,6 +157,12 @@ struct msm_drm_private {struct shrinker shrinker;struct msm_vblank_ctrl vblank_ctrl; + +/* task holding struct_mutex.. currently only used in submit path +* to detect and reject faults from copy_from_user() for submit +* ioctl. +*/ +struct task_struct *struct_mutex_task; };struct msm_format {drivers/gpu/drm/msm/msm_gem.c +1
-0@@ -196,11 +196,20 @@ int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct drm_gem_object *obj = vma->vm_private_data;struct drm_device *dev = obj->dev; +struct msm_drm_private *priv = dev->dev_private;struct page **pages;unsigned long pfn;pgoff_t pgoff;int ret;
Quoted 9 lines
drivers/gpu/drm/msm/msm_gem.c int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 210
Quoted 6 lines
drivers/gpu/drm/msm/msm_gem.c +6
-0+/* This should only happen if userspace tries to pass a mmap'd +* but unfaulted gem bo vaddr into submit ioctl, triggering +* a page fault while struct_mutex is already held. This is +* not a valid use-case so just bail. +*/ +if (priv->struct_mutex_task == current)
READ_ONCEhere I think. Otherwise should at least work, though I still think it's sloppy ;-)
hmm, is READ_ONCE() actually needed? In the "== current" case I don't see how there could be a race, and the "!= current" case I don't really care..
and yeah, maybe not sloppy but I wouldn't call it pretty. I couldn't come up with any low overhead way to check if submit->cmds / submit->objs wasn't a GEM object (otherwise I would have bailed out earlier than copy_from_user() with an -EINVAL, and wouldn't have needed this)
BR,
-R
Quoted 28 lines
-Daniel
Quoted 26 lines
drivers/gpu/drm/msm/msm_gem.c +2
-0+return VM_FAULT_SIGBUS ; +/* Make sure we don't parallel update on a fault, nor move or remove * something from beneath our feet */ drivers/gpu/drm/msm/msm_gem_submit.c +3
-0@@ -426,6 +426,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,if (ret)return ret; + priv->struct_mutex_task = current; +if (args->flags &MSM_SUBMIT_FENCE_FD_OUT ) { out_fence_fd =get_unused_fd_flags (O_CLOEXEC );if (out_fence_fd <0 ) {@@ -549,6 +551,7 @@ out: out_unlock:if (ret && (out_fence_fd >=0 ))put_unused_fd (out_fence_fd); + priv->struct_mutex_task =NULL ;mutex_unlock (&dev->struct_mutex);return ret; }
Quoted 8 lines
Quoted 3 lines
--
2.7.4--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Freedreno mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/freedreno
A message that is not in the archive (sent off-list or not yet received): CAF6AEGtbFCDmkGgL7b289CaM++PdPV3Q-SQ8n6H8Vy7V-N-FQw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [PATCH 2/2] drm/msm: protect against faults from copy_from_user() in submit ioctl
Quoted 61 lines · Rob Clark
Quoted 46 lines · Daniel Vetter
Quoted 45 lines · Rob Clark
An evil userspace could try to cause deadlock by passing an unfaulted-in GEM bo as
submit->bos(orsubmit->cmds) table. Which will triggermsm_gem_fault()while we already holdstruct_mutex. See:https://github.com/freedreno/msmtest/blob/master/evilsubmittest.c
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/msm/msm_drv.h | 6 ++++++ drivers/gpu/drm/msm/msm_gem.c | 9 +++++++++ drivers/gpu/drm/msm/msm_gem_submit.c | 3 +++ 3 files changed, 18 insertions(+)drivers/gpu/drm/msm/msm_drv.h +6
-0@@ -157,6 +157,12 @@ struct msm_drm_private {struct shrinker shrinker;struct msm_vblank_ctrl vblank_ctrl; + +/* task holding struct_mutex.. currently only used in submit path +* to detect and reject faults from copy_from_user() for submit +* ioctl. +*/ +struct task_struct *struct_mutex_task; };struct msm_format {drivers/gpu/drm/msm/msm_gem.c +1
-0@@ -196,11 +196,20 @@ int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct drm_gem_object *obj = vma->vm_private_data;struct drm_device *dev = obj->dev; +struct msm_drm_private *priv = dev->dev_private;struct page **pages;unsigned long pfn;pgoff_t pgoff;int ret;Quoted 9 lines
drivers/gpu/drm/msm/msm_gem.c int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 210
Quoted 6 lines
drivers/gpu/drm/msm/msm_gem.c +6
-0+/* This should only happen if userspace tries to pass a mmap'd +* but unfaulted gem bo vaddr into submit ioctl, triggering +* a page fault while struct_mutex is already held. This is +* not a valid use-case so just bail. +*/ +if (priv->struct_mutex_task == current)
READ_ONCEhere I think. Otherwise should at least work, though I still think it's sloppy ;-)hmm, is
READ_ONCE()actually needed? In the "== current" case I don't see how there could be a race, and the "!= current" case I don't really care..
Quoted 5 lines
and yeah, maybe not sloppy but I wouldn't call it pretty. I couldn't come up with any low overhead way to check if
submit->cmds/submit->objswasn't a GEM object (otherwise I would have bailed out earlier thancopy_from_user()with an-EINVAL, and wouldn't have needed this)
Like I said imo the correct fix isn't trying to detect your own objects, but by adding a slowpath into the CS ioctl where you drop locks and copy relocations (or whatever it is you need) into a vmalloced (or similar) staging area. Not fast, but it'll work. And the only signal you need for that is the short read/write from copy_*_user_atomic.
Without this you'll still hit the lockdep snag as soon as arm's copy*user functions gain all the necessary annotations (like x86 already has). And that seems to happen rsn if Al Viro's plans hold up.
-Daniel
Quoted 40 lines
BR,
-RQuoted 36 lines
-Daniel
Quoted 27 lines
drivers/gpu/drm/msm/msm_gem.c +2
-0+return VM_FAULT_SIGBUS ; +/* Make sure we don't parallel update on a fault, nor move or remove * something from beneath our feet */ drivers/gpu/drm/msm/msm_gem_submit.c +3
-1@@ -426,6 +426,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,if (ret)return ret; + priv->struct_mutex_task = current; +if (args->flags &MSM_SUBMIT_FENCE_FD_OUT ) { out_fence_fd =get_unused_fd_flags (O_CLOEXEC );if (out_fence_fd <0 ) {@@ -549,6 +551,7 @@ out: out_unlock:if (ret && (out_fence_fd >=0 ))put_unused_fd (out_fence_fd); + priv->struct_mutex_task =NULL ;mutex_unlock (&dev->struct_mutex);return ret; } --Quoted 2 lines
2.7.4
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ Freedreno mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/freedreno
A message that is not in the archive (sent off-list or not yet received): 20160828165340.GB10980-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org
Re: [PATCH 2/2] drm/msm: protect against faults from copy_from_user() in submit ioctl
Quoted 68 lines · Daniel Vetter
Quoted 61 lines · Rob Clark
Quoted 46 lines · Daniel Vetter
Quoted 45 lines · Rob Clark
An evil userspace could try to cause deadlock by passing an unfaulted-in GEM bo as
submit->bos(orsubmit->cmds) table. Which will triggermsm_gem_fault()while we already holdstruct_mutex. See:https://github.com/freedreno/msmtest/blob/master/evilsubmittest.c
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/msm/msm_drv.h | 6 ++++++ drivers/gpu/drm/msm/msm_gem.c | 9 +++++++++ drivers/gpu/drm/msm/msm_gem_submit.c | 3 +++ 3 files changed, 18 insertions(+)drivers/gpu/drm/msm/msm_drv.h +6
-0@@ -157,6 +157,12 @@ struct msm_drm_private {struct shrinker shrinker;struct msm_vblank_ctrl vblank_ctrl; + +/* task holding struct_mutex.. currently only used in submit path +* to detect and reject faults from copy_from_user() for submit +* ioctl. +*/ +struct task_struct *struct_mutex_task; };struct msm_format {drivers/gpu/drm/msm/msm_gem.c +1
-0@@ -196,11 +196,20 @@ int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct drm_gem_object *obj = vma->vm_private_data;struct drm_device *dev = obj->dev; +struct msm_drm_private *priv = dev->dev_private;struct page **pages;unsigned long pfn;pgoff_t pgoff;int ret;Quoted 9 lines
drivers/gpu/drm/msm/msm_gem.c int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 210
Quoted 6 lines
drivers/gpu/drm/msm/msm_gem.c +6
-0+/* This should only happen if userspace tries to pass a mmap'd +* but unfaulted gem bo vaddr into submit ioctl, triggering +* a page fault while struct_mutex is already held. This is +* not a valid use-case so just bail. +*/ +if (priv->struct_mutex_task == current)
READ_ONCEhere I think. Otherwise should at least work, though I still think it's sloppy ;-)hmm, is
READ_ONCE()actually needed? In the "== current" case I don't see how there could be a race, and the "!= current" case I don't really care..Quoted 5 lines
and yeah, maybe not sloppy but I wouldn't call it pretty. I couldn't come up with any low overhead way to check if
submit->cmds/submit->objswasn't a GEM object (otherwise I would have bailed out earlier thancopy_from_user()with an-EINVAL, and wouldn't have needed this)
Quoted 5 lines
Like I said imo the correct fix isn't trying to detect your own objects, but by adding a slowpath into the CS ioctl where you drop locks and copy relocations (or whatever it is you need) into a vmalloced (or similar) staging area. Not fast, but it'll work. And the only signal you need for that is the short read/write from copy_*_user_atomic.
well, the previous patch adds slowpath (but that is only dropping table_lock spinlock, not struct_mutex..)
I'll consider an even-more-slowpath approach when I re-work locking to be more fine grained, but that isn't something for -fixes (let alone stable). And either way, I'd prefer to just disallow abusing GEM BOs as part of the submit ioctl data structure, since there is no valid reason for that use-case (unlike, perhaps, a pwrite type interface). But I'll settle for just keeping that from being a DoS vector, as this patch does.
But that is a whole different topic.. for now I need a fixes/stable solution, and I do not see why READ_ONCE() is needed, although if someone could explain why it is needed I will add it..
Quoted 3 lines
Without this you'll still hit the lockdep snag as soon as arm's copy*user functions gain all the necessary annotations (like x86 already has). And that seems to happen rsn if Al Viro's plans hold up.
I did locally add the missing might_fault() to arm's copy_from_user() for testing, and did not see issues.
BR,
-R
Quoted 43 lines
-Daniel
Quoted 40 lines
BR,
-RQuoted 36 lines
-Daniel
Quoted 27 lines
drivers/gpu/drm/msm/msm_gem.c +2
-0+return VM_FAULT_SIGBUS ; +/* Make sure we don't parallel update on a fault, nor move or remove * something from beneath our feet */ drivers/gpu/drm/msm/msm_gem_submit.c +3
-1@@ -426,6 +426,8 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,if (ret)return ret; + priv->struct_mutex_task = current; +if (args->flags &MSM_SUBMIT_FENCE_FD_OUT ) { out_fence_fd =get_unused_fd_flags (O_CLOEXEC );if (out_fence_fd <0 ) {@@ -549,6 +551,7 @@ out: out_unlock:if (ret && (out_fence_fd >=0 ))put_unused_fd (out_fence_fd); + priv->struct_mutex_task =NULL ;mutex_unlock (&dev->struct_mutex);return ret; } --Quoted 2 lines
2.7.4
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Quoted 4 lines
--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Freedreno mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/freedreno