linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil@xs4all.nl>
To: Kangjie Lu <kjlu@umn.edu>
Cc: pakki001@umn.edu, Jacob chen <jacob2.chen@rock-chips.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Heiko Stuebner <heiko@sntech.de>,
	linux-media@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] media: rga: fix NULL pointer dereferences, use-after-free, memory leak
Date: Thu, 28 Mar 2019 15:47:19 +0100	[thread overview]
Message-ID: <47c6d19c-96bd-49e1-7a3e-702fc50df670@xs4all.nl> (raw)
In-Reply-To: <20190314050344.29790-1-kjlu@umn.edu>

Hi Kangjie,

Unfortunately there are more issues with the cleanup sequence in
this probe function:

On 3/14/19 6:03 AM, Kangjie Lu wrote:
> 1. dma_alloc_attrs, __get_free_pages can fail and return NULL.
> Further uses of their return values lead to NULL pointer
> dereferences
> 
> 2. In the error-handling path, video_unregister_device uses
> "rga->vfd" which has been freed by video_device_release
> 
> 3. The error handling for v4l2_m2m_init and video_register_device
> has memory-leak issues
> 
> The patch fixes the above issues.
> 
> Signed-off-by: Kangjie Lu <kjlu@umn.edu>
> ---
>  drivers/media/platform/rockchip/rga/rga.c | 26 ++++++++++++++++++++---
>  1 file changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/media/platform/rockchip/rga/rga.c b/drivers/media/platform/rockchip/rga/rga.c
> index 5c653287185f..468365ceb99d 100644
> --- a/drivers/media/platform/rockchip/rga/rga.c
> +++ b/drivers/media/platform/rockchip/rga/rga.c
> @@ -889,11 +889,24 @@ static int rga_probe(struct platform_device *pdev)
>  	rga->cmdbuf_virt = dma_alloc_attrs(rga->dev, RGA_CMDBUF_SIZE,
>  					   &rga->cmdbuf_phy, GFP_KERNEL,
>  					   DMA_ATTR_WRITE_COMBINE);
> +	if (!rga->cmdbuf_virt) {
> +		ret = -ENOMEM;
> +		goto unreg_video_dev;

Actually, the unreg_video_dev label makes no sense: if the video device
is successfully registered, then probe() returns 0. So you never need to
unregister the video device again. You *do* need to release it with
video_device_release().

So this should be a goto rel_vdev.

And the cleanup sequence at the end also fails to call v4l2_m2m_release().

Can you make v4?

Thanks!

	Hans

> +	}
>  
>  	rga->src_mmu_pages =
>  		(unsigned int *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 3);
> +	if (!rga->src_mmu_pages) {
> +		ret = -ENOMEM;
> +		goto free_dma_attrs;
> +	}
> +
>  	rga->dst_mmu_pages =
>  		(unsigned int *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 3);
> +	if (!rga->dst_mmu_pages) {
> +		ret = -ENOMEM;
> +		goto free_dst_pages;
> +	}
>  
>  	def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
>  	def_frame.size = def_frame.stride * def_frame.height;
> @@ -901,7 +914,7 @@ static int rga_probe(struct platform_device *pdev)
>  	ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
>  	if (ret) {
>  		v4l2_err(&rga->v4l2_dev, "Failed to register video device\n");
> -		goto rel_vdev;
> +		goto free_pages;
>  	}
>  
>  	v4l2_info(&rga->v4l2_dev, "Registered %s as /dev/%s\n",
> @@ -909,10 +922,17 @@ static int rga_probe(struct platform_device *pdev)
>  
>  	return 0;
>  
> -rel_vdev:
> -	video_device_release(vfd);
> +free_pages:
> +	free_pages((unsigned long)rga->src_mmu_pages, 3);
> +free_dst_pages:
> +	free_pages((unsigned long)rga->dst_mmu_pages, 3);
> +free_dma_attrs:
> +	dma_free_attrs(rga->dev, RGA_CMDBUF_SIZE, rga->cmdbuf_virt,
> +		       rga->cmdbuf_phy,
> +		       DMA_ATTR_WRITE_COMBINE);
>  unreg_video_dev:
>  	video_unregister_device(rga->vfd);
> +	video_device_release(vfd);
>  unreg_v4l2_dev:
>  	v4l2_device_unregister(&rga->v4l2_dev);
>  err_put_clk:
> 


  parent reply	other threads:[~2019-03-28 14:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-09  6:35 [PATCH] media: rga: fix NULL pointer dereferences Kangjie Lu
2019-03-11 13:15 ` Steven Price
2019-03-12  6:58   ` [PATCH v2] media: rga: fix NULL pointer dereferences and a memory leak Kangjie Lu
2019-03-13 13:35     ` Steven Price
2019-03-14  5:03       ` [PATCH v3] media: rga: fix NULL pointer dereferences, use-after-free, " Kangjie Lu
2019-03-14 10:30         ` Steven Price
2019-03-28 14:47         ` Hans Verkuil [this message]
2019-03-28 15:00           ` Kangjie Lu

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=47c6d19c-96bd-49e1-7a3e-702fc50df670@xs4all.nl \
    --to=hverkuil@xs4all.nl \
    --cc=heiko@sntech.de \
    --cc=jacob2.chen@rock-chips.com \
    --cc=kjlu@umn.edu \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=mchehab@kernel.org \
    --cc=pakki001@umn.edu \
    /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).