public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Hans Verkuil <hverkuil@xs4all.nl>
Cc: Dan Carpenter <dan.carpenter@oracle.com>,
	syzbot <syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com>,
	Hillf Danton <hdanton@sina.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Allison Randal <allison@lohutok.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Souptick Joarder <jrdr.linux@gmail.com>,
	andreyknvl@google.com, bnvandana@gmail.com,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
	linux-usb@vger.kernel.org, mchehab@kernel.org,
	syzkaller-bugs@googlegroups.com
Subject: Re: [PATCH] media: usbvision: Fix a use after free in v4l2_release()
Date: Fri, 14 Feb 2020 13:22:39 +0200	[thread overview]
Message-ID: <20200214112239.GC4831@pendragon.ideasonboard.com> (raw)
In-Reply-To: <d8663b81-e920-3e1d-11d0-f636ea52c6ef@xs4all.nl>

Hi Hans,

On Fri, Feb 14, 2020 at 11:06:36AM +0100, Hans Verkuil wrote:
> On 1/24/20 3:13 PM, Dan Carpenter wrote:
> > Syzbot triggered a use after free in v5.5-rc6:
> > 
> > BUG: KASAN: use-after-free in v4l2_release+0x2f1/0x390 drivers/media/v4l2-core/v4l2-dev.c:459
> > 
> > Allocated by task 94:
> >  usbvision_alloc drivers/media/usb/usbvision/usbvision-video.c:1315 [inline]
> >  usbvision_probe.cold+0x5c5/0x1f21 drivers/media/usb/usbvision/usbvision-video.c:1469
> > 
> > Freed by task 1913:
> >  kfree+0xd5/0x300 mm/slub.c:3957
> >  usbvision_release+0x181/0x1c0 drivers/media/usb/usbvision/usbvision-video.c:1364
> >  usbvision_radio_close.cold+0x2b/0x74 drivers/media/usb/usbvision/usbvision-video.c:1130
> >  v4l2_release+0x2e7/0x390 drivers/media/v4l2-core/v4l2-dev.c:455
> > 
> > The problem is that the v4l2_release() calls usbvision_release() which
> > frees "usbvision" but v4l2_release() still wants to use
> > "usbvision->vdev".  One solution is to make this devm_ allocated memory
> > so the memory isn't freed until later.
> 
> devm_ allocated memory is freed after disconnect, so I doubt this will help, or at
> best it will just move the problem elsewhere.

Yes, devm_*alloc is evil :-( It has spread to many drivers and is used
incorrectly in most cases.

> The right approach would be to use the release() callback from struct v4l2_device:
> that's called when the very last open filehandle is closed.

Hillf Danton has sent a patch to do so in the "Re: KASAN: use-after-free
Read in v4l2_release (3)" thread. Have you seen it ?

> But I'm not sure if it is worth the effort. The usbvision driver is a mess and
> personally I think it should be deprecated.

I agree.

> > Reported-by: syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > I copied this idea from a different driver, but I haven't tested it.
> > I wanted to try the #syz fix command to see if it works.
> > 
> >  drivers/media/usb/usbvision/usbvision-video.c | 4 +---
> >  1 file changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
> > index 93d36aab824f..07b4763062c4 100644
> > --- a/drivers/media/usb/usbvision/usbvision-video.c
> > +++ b/drivers/media/usb/usbvision/usbvision-video.c
> > @@ -1312,7 +1312,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
> >  {
> >  	struct usb_usbvision *usbvision;
> >  
> > -	usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
> > +	usbvision = devm_kzalloc(&dev->dev, sizeof(*usbvision), GFP_KERNEL);
> >  	if (!usbvision)
> >  		return NULL;
> >  
> > @@ -1336,7 +1336,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
> >  	v4l2_ctrl_handler_free(&usbvision->hdl);
> >  	v4l2_device_unregister(&usbvision->v4l2_dev);
> >  err_free:
> > -	kfree(usbvision);
> >  	return NULL;
> >  }
> >  
> > @@ -1361,7 +1360,6 @@ static void usbvision_release(struct usb_usbvision *usbvision)
> >  
> >  	v4l2_ctrl_handler_free(&usbvision->hdl);
> >  	v4l2_device_unregister(&usbvision->v4l2_dev);
> > -	kfree(usbvision);
> >  
> >  	PDEBUG(DBG_PROBE, "success");
> >  }

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2020-02-14 11:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-08 20:24 KASAN: use-after-free Read in v4l2_release (3) syzbot
2020-01-22 22:58 ` syzbot
     [not found] ` <20200123102707.2596-1-hdanton@sina.com>
2020-01-23 12:19   ` Laurent Pinchart
     [not found]   ` <20200124022847.11244-1-hdanton@sina.com>
2020-01-24 12:41     ` Andrey Konovalov
2020-01-24 12:54       ` syzbot
2020-01-24 14:10   ` Dan Carpenter
2020-01-24 14:13   ` [PATCH] media: usbvision: Fix a use after free in v4l2_release() Dan Carpenter
2020-02-14 10:06     ` Hans Verkuil
2020-02-14 11:22       ` Laurent Pinchart [this message]
2020-02-14 11:30         ` Hans Verkuil
     [not found]         ` <20200214121447.13612-1-hdanton@sina.com>
2020-02-14 12:21           ` Hans Verkuil
     [not found]           ` <20200214124825.12568-1-hdanton@sina.com>
2020-02-14 13:34             ` Hans Verkuil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200214112239.GC4831@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=allison@lohutok.net \
    --cc=andreyknvl@google.com \
    --cc=bnvandana@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hdanton@sina.com \
    --cc=hverkuil@xs4all.nl \
    --cc=jrdr.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=syzbot+75287f75e2fedd69d680@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox