linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] [media] v4l2-core: fix use-after-free error
@ 2019-04-10 14:28 Sumit Gupta
  2019-04-10 14:52 ` Hans Verkuil
  0 siblings, 1 reply; 2+ messages in thread
From: Sumit Gupta @ 2019-04-10 14:28 UTC (permalink / raw)
  To: mchehab, hverkuil-cisco, sakari.ailus, paul.kocialkowski, tfiga,
	keiichiw, sumitg
  Cc: linux-media, linux-kernel

From: sumitg <sumitg@nvidia.com>

Fixing use-after-free within __v4l2_ctrl_handler_setup().
Memory is being freed with kfree(new_ref) for duplicate
control reference entry but cluster is still referring
to the duplicate entry. Change done to point cluster to
original reference instead of duplicate which is freed.

 ==================================================================
 BUG: KASAN: use-after-free in __v4l2_ctrl_handler_setup+0x388/0x428
 Read of size 8 at addr ffffffc324e78618 by task systemd-udevd/312

 Allocated by task 312:

 Freed by task 312:

 The buggy address belongs to the object at ffffffc324e78600
  which belongs to the cache kmalloc-64 of size 64
 The buggy address is located 24 bytes inside of
  64-byte region [ffffffc324e78600, ffffffc324e78640)
 The buggy address belongs to the page:
 page:ffffffbf0c939e00 count:1 mapcount:0 mapping:
					(null) index:0xffffffc324e78f80
 flags: 0x4000000000000100(slab)
 raw: 4000000000000100 0000000000000000 ffffffc324e78f80 000000018020001a
 raw: 0000000000000000 0000000100000001 ffffffc37040fb80 0000000000000000
 page dumped because: kasan: bad access detected

 Memory state around the buggy address:
  ffffffc324e78500: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
  ffffffc324e78580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 >ffffffc324e78600: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
                             ^
  ffffffc324e78680: 00 00 00 00 00 00 00 00 fc fc fc fc fc fc fc fc
  ffffffc324e78700: 00 00 00 00 00 fc fc fc fc fc fc fc fc fc fc fc
 ==================================================================

Signed-off-by: sumitg <sumitg@nvidia.com>
---
 drivers/media/v4l2-core/v4l2-ctrls.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
index 5e3806f..e971bab 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls.c
@@ -2182,6 +2182,7 @@ static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
 			continue;
 		/* Don't add duplicates */
 		if (ref->ctrl->id == id) {
+			ctrl->cluster = &ref->ctrl;
 			kfree(new_ref);
 			goto unlock;
 		}
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] [media] v4l2-core: fix use-after-free error
  2019-04-10 14:28 [PATCH] [media] v4l2-core: fix use-after-free error Sumit Gupta
@ 2019-04-10 14:52 ` Hans Verkuil
  0 siblings, 0 replies; 2+ messages in thread
From: Hans Verkuil @ 2019-04-10 14:52 UTC (permalink / raw)
  To: Sumit Gupta, mchehab, sakari.ailus, paul.kocialkowski, tfiga, keiichiw
  Cc: linux-media, linux-kernel

On 4/10/19 4:28 PM, Sumit Gupta wrote:
> From: sumitg <sumitg@nvidia.com>
> 
> Fixing use-after-free within __v4l2_ctrl_handler_setup().
> Memory is being freed with kfree(new_ref) for duplicate
> control reference entry but cluster is still referring
> to the duplicate entry. Change done to point cluster to
> original reference instead of duplicate which is freed.
> 
>  ==================================================================
>  BUG: KASAN: use-after-free in __v4l2_ctrl_handler_setup+0x388/0x428
>  Read of size 8 at addr ffffffc324e78618 by task systemd-udevd/312
> 
>  Allocated by task 312:
> 
>  Freed by task 312:
> 
>  The buggy address belongs to the object at ffffffc324e78600
>   which belongs to the cache kmalloc-64 of size 64
>  The buggy address is located 24 bytes inside of
>   64-byte region [ffffffc324e78600, ffffffc324e78640)
>  The buggy address belongs to the page:
>  page:ffffffbf0c939e00 count:1 mapcount:0 mapping:
> 					(null) index:0xffffffc324e78f80
>  flags: 0x4000000000000100(slab)
>  raw: 4000000000000100 0000000000000000 ffffffc324e78f80 000000018020001a
>  raw: 0000000000000000 0000000100000001 ffffffc37040fb80 0000000000000000
>  page dumped because: kasan: bad access detected
> 
>  Memory state around the buggy address:
>   ffffffc324e78500: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>   ffffffc324e78580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>  >ffffffc324e78600: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>                              ^
>   ffffffc324e78680: 00 00 00 00 00 00 00 00 fc fc fc fc fc fc fc fc
>   ffffffc324e78700: 00 00 00 00 00 fc fc fc fc fc fc fc fc fc fc fc
>  ==================================================================
> 
> Signed-off-by: sumitg <sumitg@nvidia.com>
> ---
>  drivers/media/v4l2-core/v4l2-ctrls.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
> index 5e3806f..e971bab 100644
> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
> @@ -2182,6 +2182,7 @@ static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
>  			continue;
>  		/* Don't add duplicates */
>  		if (ref->ctrl->id == id) {
> +			ctrl->cluster = &ref->ctrl;
>  			kfree(new_ref);
>  			goto unlock;
>  		}
> 

I don't like this solution. Instead move this code:

        if (ctrl->handler == hdl) {
                /* By default each control starts in a cluster of its own.
                   new_ref->ctrl is basically a cluster array with one
                   element, so that's perfect to use as the cluster pointer.
                   But only do this for the handler that owns the control. */
                ctrl->cluster = &new_ref->ctrl;
                ctrl->ncontrols = 1;
        }

to after the insert_in_hash label.

That way the ctrl->cluster pointer is only updated if everything is OK.

Nice catch, BTW!

Regards,

	Hans

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-10 14:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-10 14:28 [PATCH] [media] v4l2-core: fix use-after-free error Sumit Gupta
2019-04-10 14:52 ` Hans Verkuil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).