public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: Cosmin Tanislav <demonsingur@gmail.com>
Cc: "Mauro Carvalho Chehab" <mchehab@kernel.org>,
	"Laurent Pinchart" <laurent.pinchart+renesas@ideasonboard.com>,
	"Hans Verkuil" <hverkuil@xs4all.nl>,
	"Tomi Valkeinen" <tomi.valkeinen@ideasonboard.com>,
	"Paweł Anikiel" <panikiel@google.com>,
	linux-media@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] media: v4l: subdev: Prevent NULL routes access
Date: Mon, 25 Nov 2024 08:39:22 +0000	[thread overview]
Message-ID: <Z0Q3ukermwmPax2b@kekkonen.localdomain> (raw)
In-Reply-To: <20241122143717.173344-1-demonsingur@gmail.com>

Hi Cosmin,

Thanks for the patch.

On Fri, Nov 22, 2024 at 04:37:12PM +0200, Cosmin Tanislav wrote:
> When using v4l2_subdev_set_routing to set a subdev's routing, and the
> passed routing.num_routes is 0, kmemdup is not called to populate the
> routes of the new routing (which is fine, since we wouldn't want to pass
> a possible NULL value to kmemdup).
> 
> This results in subdev's routing.routes to be NULL.
> 
> routing.routes is further used in some places without being guarded by
> the same num_routes non-zero condition.
> 
> Fix it.

While I think moving the code to copy the routing table seems reasonable,
is there a need to make num_routes == 0 a special case? No memcpy()
implementation should access destination or source if the size is 0.

> 
> Signed-off-by: Cosmin Tanislav <demonsingur@gmail.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 46 +++++++++++++--------------
>  1 file changed, 23 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index cde1774c9098d..4f0eecd7fd66f 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -605,6 +605,23 @@ subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
>  			     v4l2_subdev_get_unlocked_active_state(sd);
>  }
>  
> +static void subdev_copy_krouting(struct v4l2_subdev_routing *routing,
> +				 struct v4l2_subdev_krouting *krouting)
> +{
> +	memset(routing->reserved, 0, sizeof(routing->reserved));
> +
> +	if (!krouting->routes) {
> +		routing->num_routes = 0;
> +		return;
> +	}
> +
> +	memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
> +	       krouting->routes,
> +	       min(krouting->num_routes, routing->len_routes) *
> +	       sizeof(*krouting->routes));
> +	routing->num_routes = krouting->num_routes;
> +}
> +
>  static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  			    struct v4l2_subdev_state *state)
>  {
> @@ -976,7 +993,6 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  
>  	case VIDIOC_SUBDEV_G_ROUTING: {
>  		struct v4l2_subdev_routing *routing = arg;
> -		struct v4l2_subdev_krouting *krouting;
>  
>  		if (!v4l2_subdev_enable_streams_api)
>  			return -ENOIOCTLCMD;
> @@ -984,15 +1000,7 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  		if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
>  			return -ENOIOCTLCMD;
>  
> -		memset(routing->reserved, 0, sizeof(routing->reserved));
> -
> -		krouting = &state->routing;
> -
> -		memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
> -		       krouting->routes,
> -		       min(krouting->num_routes, routing->len_routes) *
> -		       sizeof(*krouting->routes));
> -		routing->num_routes = krouting->num_routes;
> +		subdev_copy_krouting(routing, &state->routing);
>  
>  		return 0;
>  	}
> @@ -1016,8 +1024,6 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  		if (routing->num_routes > routing->len_routes)
>  			return -EINVAL;
>  
> -		memset(routing->reserved, 0, sizeof(routing->reserved));
> -
>  		for (i = 0; i < routing->num_routes; ++i) {
>  			const struct v4l2_subdev_route *route = &routes[i];
>  			const struct media_pad *pads = sd->entity.pads;
> @@ -1046,12 +1052,7 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  		 * the routing table.
>  		 */
>  		if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
> -			memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
> -			       state->routing.routes,
> -			       min(state->routing.num_routes, routing->len_routes) *
> -			       sizeof(*state->routing.routes));
> -			routing->num_routes = state->routing.num_routes;
> -
> +			subdev_copy_krouting(routing, &state->routing);
>  			return 0;
>  		}
>  
> @@ -1064,11 +1065,7 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
>  		if (rval < 0)
>  			return rval;
>  
> -		memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
> -		       state->routing.routes,
> -		       min(state->routing.num_routes, routing->len_routes) *
> -		       sizeof(*state->routing.routes));
> -		routing->num_routes = state->routing.num_routes;
> +		subdev_copy_krouting(routing, &state->routing);
>  
>  		return 0;
>  	}
> @@ -1956,6 +1953,9 @@ struct v4l2_subdev_route *
>  __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
>  				struct v4l2_subdev_route *route)
>  {
> +	if (!routing->routes)
> +		return NULL;

Same here.

> +
>  	if (route)
>  		++route;
>  	else

-- 
Kind regards,

Sakari Ailus

  parent reply	other threads:[~2024-11-25  8:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-22 14:37 [PATCH] media: v4l: subdev: Prevent NULL routes access Cosmin Tanislav
2024-11-24  6:49 ` Laurent Pinchart
2024-11-25 18:28   ` Cosmin Tanislav
2024-11-25  8:39 ` Sakari Ailus [this message]
2024-11-25 11:33   ` Tomi Valkeinen
2024-11-25 12:07     ` Laurent Pinchart
2024-11-25 18:42       ` Cosmin Tanislav
2024-12-13  8:49         ` Sakari Ailus

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=Z0Q3ukermwmPax2b@kekkonen.localdomain \
    --to=sakari.ailus@linux.intel.com \
    --cc=demonsingur@gmail.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart+renesas@ideasonboard.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=panikiel@google.com \
    --cc=tomi.valkeinen@ideasonboard.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