linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil-cisco@xs4all.nl>
To: Sumit Gupta <sumitg@nvidia.com>,
	"mchehab@kernel.org" <mchehab@kernel.org>,
	"sakari.ailus@linux.intel.com" <sakari.ailus@linux.intel.com>,
	"paul.kocialkowski@bootlin.com" <paul.kocialkowski@bootlin.com>,
	"tfiga@chromium.org" <tfiga@chromium.org>,
	"keiichiw@chromium.org" <keiichiw@chromium.org>,
	Bibek Basu <bbasu@nvidia.com>
Cc: "linux-media@vger.kernel.org" <linux-media@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [Patch V2] v4l2-core: fix use-after-free error
Date: Fri, 12 Apr 2019 15:55:44 +0200	[thread overview]
Message-ID: <30ea73dc-7be7-1fe4-5270-fb94df5184e9@xs4all.nl> (raw)
In-Reply-To: <SN6PR12MB2832D36A4D06668F30DABD12B9280@SN6PR12MB2832.namprd12.prod.outlook.com>

On 4/12/19 3:47 PM, Sumit Gupta wrote:
> 
> On 4/11/19 6:01 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 update ctrl->cluster only when new control reference is 
>> added. Also, checking if
>> ctrl->ncontrols is non-zero before accessing controls from
>> cluster pointer to avoid illegal access when cluster has zero 
>> controls.
>>
>>  ==================================================================
>>  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  
>> ==================================================================
>>
>> Suggested-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
>> Signed-off-by: Sumit Gupta <sumitg@nvidia.com>
>> ---
>> v2:
>> * update ctrl->cluster only when new control reference is added.
>> * check ctrl->ncontrols to avoid illegal access when cluster has zero controls.
>>
>>  drivers/media/v4l2-core/v4l2-ctrls.c | 53 
>> +++++++++++++++++++-----------------
>>  1 file changed, 28 insertions(+), 25 deletions(-)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c 
>> b/drivers/media/v4l2-core/v4l2-ctrls.c
>> index 5e3806f..3d0a952 100644
>> --- a/drivers/media/v4l2-core/v4l2-ctrls.c
>> +++ b/drivers/media/v4l2-core/v4l2-ctrls.c
>> @@ -2154,15 +2154,6 @@ static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
>>  	if (size_extra_req)
>>  		new_ref->p_req.p = &new_ref[1];
>>  
>> -	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;
>> -	}
>> -
>>  	INIT_LIST_HEAD(&new_ref->node);
>>  
>>  	mutex_lock(hdl->lock);
>> @@ -2195,6 +2186,15 @@ static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
>>  	hdl->buckets[bucket] = new_ref;
>>  	if (ctrl_ref)
>>  		*ctrl_ref = new_ref;
>> +	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;
>> +	}
>>  
>>  unlock:
>>  	mutex_unlock(hdl->lock);
> 
> This looks good.
> 
>> @@ -2725,25 +2725,28 @@ int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
>>  		ctrl->done = false;
>>  
>>  	list_for_each_entry(ctrl, &hdl->ctrls, node) {
>> -		struct v4l2_ctrl *master = ctrl->cluster[0];
>> -		int i;
>> -
>> -		/* Skip if this control was already handled by a cluster. */
>> -		/* Skip button controls and read-only controls. */
>> -		if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
>> -		    (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
>> -			continue;
>> +		if (ctrl->ncontrols) {
>> +			struct v4l2_ctrl *master = ctrl->cluster[0];
>> +			int i;
>> +
>> +			/* Skip if this control was already handled by a */
>> +			/* cluster. Skip button controls and read-only */
>> +			/* controls. */
>> +			if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
>> +				(ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
>> +				continue;
>>  
>> -		for (i = 0; i < master->ncontrols; i++) {
>> -			if (master->cluster[i]) {
>> -				cur_to_new(master->cluster[i]);
>> -				master->cluster[i]->is_new = 1;
>> -				master->cluster[i]->done = true;
>> +			for (i = 0; i < master->ncontrols; i++) {
>> +				if (master->cluster[i]) {
>> +					cur_to_new(master->cluster[i]);
>> +					master->cluster[i]->is_new = 1;
>> +					master->cluster[i]->done = true;
>> +				}
>>  			}
>> +			ret = call_op(master, s_ctrl);
>> +			if (ret)
>> +				break;
>>  		}
>> -		ret = call_op(master, s_ctrl);
>> -		if (ret)
>> -			break;
>>  	}
>>  
>>  	return ret;
>>
> 
> These changes shouldn't be needed: when you get here all controls are guaranteed to have a non-0 ncontrols value.
> 
> If it is possible to encounter a control where ncontrols == 0, then that's a bug elsewhere. But as far as I can tell that isn't the case.
> 
> Regards,
> 
> 	Hans
> 
> Yes, I am getting a control where ncontrols is zero as printed below.
> It can be another bug as you said. I think, adding check here will be
> helpful to not crash in such cases. Please share if you think otherwise,
> then will remove the check.
> 
>   int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
>                   ctrl->done = false;
>  
>           list_for_each_entry(ctrl, &hdl->ctrls, node) {
>   +               if (ctrl->ncontrols == 0)
>   +                       pr_crit("<func:%s><ctrl->name:%s><ctrl->ncontrols:%d>\n",
>   +                                       __func__, ctrl->name, ctrl->ncontrols);
> 
> Log:
>   [    8.354464] <func:__v4l2_ctrl_handler_setup><ctrl->name:Group Hold><ctrl->ncontrols:0>

Something went wrong if you get this.

This should never happen. Dig a bit deeper to find out how you get into this situation.

You did check hdl->error before calling v4l2_ctrl_handler_setup? Although even if an error
occurred when adding controls, this should still not happen.

Regards,

	Hans

> 
> 
> Regards,
> Sumit Gupta
> 


      reply	other threads:[~2019-04-12 13:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-11 16:01 [Patch V2] v4l2-core: fix use-after-free error Sumit Gupta
2019-04-12 11:23 ` Hans Verkuil
2019-04-12 13:47   ` Sumit Gupta
2019-04-12 13:55     ` Hans Verkuil [this message]

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=30ea73dc-7be7-1fe4-5270-fb94df5184e9@xs4all.nl \
    --to=hverkuil-cisco@xs4all.nl \
    --cc=bbasu@nvidia.com \
    --cc=keiichiw@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=paul.kocialkowski@bootlin.com \
    --cc=sakari.ailus@linux.intel.com \
    --cc=sumitg@nvidia.com \
    --cc=tfiga@chromium.org \
    /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;
as well as URLs for NNTP newsgroup(s).