DriverDev-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v3] media: allegro: add missed checks in allegro_open()
@ 2020-01-13  5:59 Chuhong Yuan
  2020-01-21  8:23 ` Michael Tretter
  0 siblings, 1 reply; 4+ messages in thread
From: Chuhong Yuan @ 2020-01-13  5:59 UTC (permalink / raw)
  Cc: devel, Greg Kroah-Hartman, Chuhong Yuan, Michael Tretter,
	linux-kernel, Pengutronix Kernel Team, Mauro Carvalho Chehab,
	linux-media

allegro_open() misses checks for v4l2_m2m_ctx_init() and results of
v4l2_ctrl_new* calls.
Add checks and error handlers to fix the problems.

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
Changes in v3:
  - Make code cleaner.
  - Add a check for handler->error.

 .../staging/media/allegro-dvt/allegro-core.c  | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/media/allegro-dvt/allegro-core.c b/drivers/staging/media/allegro-dvt/allegro-core.c
index 6f0cd0784786..e86001e42963 100644
--- a/drivers/staging/media/allegro-dvt/allegro-core.c
+++ b/drivers/staging/media/allegro-dvt/allegro-core.c
@@ -2270,15 +2270,12 @@ static int allegro_open(struct file *file)
 	struct allegro_channel *channel = NULL;
 	struct v4l2_ctrl_handler *handler;
 	u64 mask;
+	int ret;
 
 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
 	if (!channel)
 		return -ENOMEM;
 
-	v4l2_fh_init(&channel->fh, vdev);
-	file->private_data = &channel->fh;
-	v4l2_fh_add(&channel->fh);
-
 	init_completion(&channel->completion);
 
 	channel->dev = dev;
@@ -2328,6 +2325,11 @@ static int allegro_open(struct file *file)
 			V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
 			1, 32,
 			1, 1);
+	if (handler->error != 0) {
+		ret = handler->error;
+		goto error;
+	}
+
 	channel->fh.ctrl_handler = handler;
 
 	channel->mcu_channel_id = -1;
@@ -2341,7 +2343,21 @@ static int allegro_open(struct file *file)
 	channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
 						allegro_queue_init);
 
+	if (IS_ERR(channel->fh.m2m_ctx)) {
+		ret = PTR_ERR(channel->fh.m2m_ctx);
+		goto error;
+	}
+
+	v4l2_fh_init(&channel->fh, vdev);
+	file->private_data = &channel->fh;
+	v4l2_fh_add(&channel->fh);
+
 	return 0;
+
+error:
+	v4l2_ctrl_handler_free(handler);
+	kfree(channel);
+	return ret;
 }
 
 static int allegro_release(struct file *file)
-- 
2.24.1

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

* Re: [PATCH v3] media: allegro: add missed checks in allegro_open()
  2020-01-13  5:59 [PATCH v3] media: allegro: add missed checks in allegro_open() Chuhong Yuan
@ 2020-01-21  8:23 ` Michael Tretter
  2020-01-21 11:59   ` Chuhong Yuan
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Tretter @ 2020-01-21  8:23 UTC (permalink / raw)
  To: Chuhong Yuan
  Cc: devel, Greg Kroah-Hartman, linux-kernel, Pengutronix Kernel Team,
	Mauro Carvalho Chehab, linux-media

On Mon, 13 Jan 2020 13:59:51 +0800, Chuhong Yuan wrote:
> allegro_open() misses checks for v4l2_m2m_ctx_init() and results of
> v4l2_ctrl_new* calls.
> Add checks and error handlers to fix the problems.
> 
> Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> ---
> Changes in v3:
>   - Make code cleaner.
>   - Add a check for handler->error.
> 
>  .../staging/media/allegro-dvt/allegro-core.c  | 24 +++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/staging/media/allegro-dvt/allegro-core.c b/drivers/staging/media/allegro-dvt/allegro-core.c
> index 6f0cd0784786..e86001e42963 100644
> --- a/drivers/staging/media/allegro-dvt/allegro-core.c
> +++ b/drivers/staging/media/allegro-dvt/allegro-core.c
> @@ -2270,15 +2270,12 @@ static int allegro_open(struct file *file)
>  	struct allegro_channel *channel = NULL;
>  	struct v4l2_ctrl_handler *handler;
>  	u64 mask;
> +	int ret;
>  
>  	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
>  	if (!channel)
>  		return -ENOMEM;
>  
> -	v4l2_fh_init(&channel->fh, vdev);
> -	file->private_data = &channel->fh;
> -	v4l2_fh_add(&channel->fh);
> -
>  	init_completion(&channel->completion);
>  
>  	channel->dev = dev;
> @@ -2328,6 +2325,11 @@ static int allegro_open(struct file *file)
>  			V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
>  			1, 32,
>  			1, 1);
> +	if (handler->error != 0) {
> +		ret = handler->error;
> +		goto error;
> +	}
> +
>  	channel->fh.ctrl_handler = handler;
>  
>  	channel->mcu_channel_id = -1;
> @@ -2341,7 +2343,21 @@ static int allegro_open(struct file *file)
>  	channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
>  						allegro_queue_init);
>  
> +	if (IS_ERR(channel->fh.m2m_ctx)) {
> +		ret = PTR_ERR(channel->fh.m2m_ctx);
> +		goto error;
> +	}
> +
> +	v4l2_fh_init(&channel->fh, vdev);

This call sets channel->fh.ctrl_handler to vdev->ctrl_handler, which
has previously been overriden by the driver to handler. Therefore, this
patch breaks all controls. I think we should initialize channel->fh
before setting any fields of this struct.

Michael

> +	file->private_data = &channel->fh;
> +	v4l2_fh_add(&channel->fh);
> +
>  	return 0;
> +
> +error:
> +	v4l2_ctrl_handler_free(handler);
> +	kfree(channel);
> +	return ret;
>  }
>  
>  static int allegro_release(struct file *file)
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

* Re: [PATCH v3] media: allegro: add missed checks in allegro_open()
  2020-01-21  8:23 ` Michael Tretter
@ 2020-01-21 11:59   ` Chuhong Yuan
  2020-01-21 13:53     ` Michael Tretter
  0 siblings, 1 reply; 4+ messages in thread
From: Chuhong Yuan @ 2020-01-21 11:59 UTC (permalink / raw)
  To: Michael Tretter
  Cc: devel, Greg Kroah-Hartman, LKML, Pengutronix Kernel Team,
	Hans Verkuil, Mauro Carvalho Chehab, linux-media

On Tue, Jan 21, 2020 at 4:23 PM Michael Tretter
<m.tretter@pengutronix.de> wrote:
>
> On Mon, 13 Jan 2020 13:59:51 +0800, Chuhong Yuan wrote:
> > allegro_open() misses checks for v4l2_m2m_ctx_init() and results of
> > v4l2_ctrl_new* calls.
> > Add checks and error handlers to fix the problems.
> >
> > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> > ---
> > Changes in v3:
> >   - Make code cleaner.
> >   - Add a check for handler->error.
> >
> >  .../staging/media/allegro-dvt/allegro-core.c  | 24 +++++++++++++++----
> >  1 file changed, 20 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/staging/media/allegro-dvt/allegro-core.c b/drivers/staging/media/allegro-dvt/allegro-core.c
> > index 6f0cd0784786..e86001e42963 100644
> > --- a/drivers/staging/media/allegro-dvt/allegro-core.c
> > +++ b/drivers/staging/media/allegro-dvt/allegro-core.c
> > @@ -2270,15 +2270,12 @@ static int allegro_open(struct file *file)
> >       struct allegro_channel *channel = NULL;
> >       struct v4l2_ctrl_handler *handler;
> >       u64 mask;
> > +     int ret;
> >
> >       channel = kzalloc(sizeof(*channel), GFP_KERNEL);
> >       if (!channel)
> >               return -ENOMEM;
> >
> > -     v4l2_fh_init(&channel->fh, vdev);
> > -     file->private_data = &channel->fh;
> > -     v4l2_fh_add(&channel->fh);
> > -
> >       init_completion(&channel->completion);
> >
> >       channel->dev = dev;
> > @@ -2328,6 +2325,11 @@ static int allegro_open(struct file *file)
> >                       V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
> >                       1, 32,
> >                       1, 1);
> > +     if (handler->error != 0) {
> > +             ret = handler->error;
> > +             goto error;
> > +     }
> > +
> >       channel->fh.ctrl_handler = handler;
> >
> >       channel->mcu_channel_id = -1;
> > @@ -2341,7 +2343,21 @@ static int allegro_open(struct file *file)
> >       channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
> >                                               allegro_queue_init);
> >
> > +     if (IS_ERR(channel->fh.m2m_ctx)) {
> > +             ret = PTR_ERR(channel->fh.m2m_ctx);
> > +             goto error;
> > +     }
> > +
> > +     v4l2_fh_init(&channel->fh, vdev);
>
> This call sets channel->fh.ctrl_handler to vdev->ctrl_handler, which
> has previously been overriden by the driver to handler. Therefore, this
> patch breaks all controls. I think we should initialize channel->fh
> before setting any fields of this struct.
>

I'm not very clear about this issue.
In my second version, Hans replied that init could be moved before return 0.
I have sent this mail to him.

> Michael
>
> > +     file->private_data = &channel->fh;
> > +     v4l2_fh_add(&channel->fh);
> > +
> >       return 0;
> > +
> > +error:
> > +     v4l2_ctrl_handler_free(handler);
> > +     kfree(channel);
> > +     return ret;
> >  }
> >
> >  static int allegro_release(struct file *file)
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

* Re: [PATCH v3] media: allegro: add missed checks in allegro_open()
  2020-01-21 11:59   ` Chuhong Yuan
@ 2020-01-21 13:53     ` Michael Tretter
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Tretter @ 2020-01-21 13:53 UTC (permalink / raw)
  To: Chuhong Yuan
  Cc: devel, Greg Kroah-Hartman, LKML, Pengutronix Kernel Team,
	Hans Verkuil, Mauro Carvalho Chehab, linux-media

On Tue, 21 Jan 2020 19:59:46 +0800, Chuhong Yuan wrote:
> On Tue, Jan 21, 2020 at 4:23 PM Michael Tretter
> <m.tretter@pengutronix.de> wrote:
> >
> > On Mon, 13 Jan 2020 13:59:51 +0800, Chuhong Yuan wrote:  
> > > allegro_open() misses checks for v4l2_m2m_ctx_init() and results of
> > > v4l2_ctrl_new* calls.
> > > Add checks and error handlers to fix the problems.
> > >
> > > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> > > ---
> > > Changes in v3:
> > >   - Make code cleaner.
> > >   - Add a check for handler->error.
> > >
> > >  .../staging/media/allegro-dvt/allegro-core.c  | 24 +++++++++++++++----
> > >  1 file changed, 20 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/staging/media/allegro-dvt/allegro-core.c b/drivers/staging/media/allegro-dvt/allegro-core.c
> > > index 6f0cd0784786..e86001e42963 100644
> > > --- a/drivers/staging/media/allegro-dvt/allegro-core.c
> > > +++ b/drivers/staging/media/allegro-dvt/allegro-core.c
> > > @@ -2270,15 +2270,12 @@ static int allegro_open(struct file *file)
> > >       struct allegro_channel *channel = NULL;
> > >       struct v4l2_ctrl_handler *handler;
> > >       u64 mask;
> > > +     int ret;
> > >
> > >       channel = kzalloc(sizeof(*channel), GFP_KERNEL);
> > >       if (!channel)
> > >               return -ENOMEM;
> > >
> > > -     v4l2_fh_init(&channel->fh, vdev);
> > > -     file->private_data = &channel->fh;
> > > -     v4l2_fh_add(&channel->fh);
> > > -
> > >       init_completion(&channel->completion);
> > >
> > >       channel->dev = dev;
> > > @@ -2328,6 +2325,11 @@ static int allegro_open(struct file *file)
> > >                       V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
> > >                       1, 32,
> > >                       1, 1);
> > > +     if (handler->error != 0) {
> > > +             ret = handler->error;
> > > +             goto error;
> > > +     }
> > > +
> > >       channel->fh.ctrl_handler = handler;
> > >
> > >       channel->mcu_channel_id = -1;
> > > @@ -2341,7 +2343,21 @@ static int allegro_open(struct file *file)
> > >       channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
> > >                                               allegro_queue_init);
> > >
> > > +     if (IS_ERR(channel->fh.m2m_ctx)) {
> > > +             ret = PTR_ERR(channel->fh.m2m_ctx);
> > > +             goto error;
> > > +     }
> > > +
> > > +     v4l2_fh_init(&channel->fh, vdev);  
> >
> > This call sets channel->fh.ctrl_handler to vdev->ctrl_handler, which
> > has previously been overriden by the driver to handler. Therefore, this
> > patch breaks all controls. I think we should initialize channel->fh
> > before setting any fields of this struct.
> >  
> 
> I'm not very clear about this issue.
> In my second version, Hans replied that init could be moved before return 0.
> I have sent this mail to him.

This driver uses its own v4l2_ctrl_handler, which is initialized in this
function (see the "handler" variable). After the initialization, the
handler is added to the fh as

	channel->fh.ctrl_handler = handler;

to override the ctrl_handler of the fh.

However, the v4l2_fh_init() function re-initializes the ctrl_handler of
the fh

	/* Inherit from video_device. May be overridden by the driver. */
	fh->ctrl_handler = vdev->ctrl_handler;

which reverts the ctrl_handler override by the driver.

Therefore, the ctrl_handler must be overridden after the call to
v4l2_fh_init().

Michael

> 
> > Michael
> >  
> > > +     file->private_data = &channel->fh;
> > > +     v4l2_fh_add(&channel->fh);
> > > +
> > >       return 0;
> > > +
> > > +error:
> > > +     v4l2_ctrl_handler_free(handler);
> > > +     kfree(channel);
> > > +     return ret;
> > >  }
> > >
> > >  static int allegro_release(struct file *file)  
> 
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-13  5:59 [PATCH v3] media: allegro: add missed checks in allegro_open() Chuhong Yuan
2020-01-21  8:23 ` Michael Tretter
2020-01-21 11:59   ` Chuhong Yuan
2020-01-21 13:53     ` Michael Tretter

DriverDev-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/driverdev-devel/0 driverdev-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 driverdev-devel driverdev-devel/ https://lore.kernel.org/driverdev-devel \
		driverdev-devel@linuxdriverproject.org devel@driverdev.osuosl.org
	public-inbox-index driverdev-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.linuxdriverproject.driverdev-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git