Linux Media Controller development
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab@s-opensource.com>
To: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Linux Media Mailing List <linux-media@vger.kernel.org>,
	Mauro Carvalho Chehab <mchehab@infradead.org>,
	Hans Verkuil <hans.verkuil@cisco.com>,
	Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>,
	Sylwester Nawrocki <s.nawrocki@samsung.com>,
	Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>,
	Tomasz Figa <tfiga@chromium.org>
Subject: Re: [PATCH 08/30] media: v4l2-ioctl: fix some "too small" warnings
Date: Mon, 26 Mar 2018 07:08:16 -0300	[thread overview]
Message-ID: <20180326070816.26859af6@vento.lan> (raw)
In-Reply-To: <20180323215356.3ib2ho2q7sd5z27v@kekkonen.localdomain>

Em Fri, 23 Mar 2018 23:53:56 +0200
Sakari Ailus <sakari.ailus@linux.intel.com> escreveu:

> Hi Mauro,
> 
> On Fri, Mar 23, 2018 at 07:56:54AM -0400, Mauro Carvalho Chehab wrote:
> > While the code there is right, it produces three false positives:
> > 	drivers/media/v4l2-core/v4l2-ioctl.c:2868 video_usercopy() error: copy_from_user() 'parg' too small (128 vs 16383)
> > 	drivers/media/v4l2-core/v4l2-ioctl.c:2868 video_usercopy() error: copy_from_user() 'parg' too small (128 vs 16383)
> > 	drivers/media/v4l2-core/v4l2-ioctl.c:2876 video_usercopy() error: memset() 'parg' too small (128 vs 16383)
> > 
> > Store the ioctl size on a cache var, in order to suppress those.  
> 
> I have to say I'm not a big fan of changing perfectly fine code in order to
> please static analysers.

Well, there's a side effect of this patch: it allows gcc to better
optimize the text size, with is good:

   text	   data	    bss	    dec	    hex	filename
  34538	   2320	      0	  36858	   8ffa	old/drivers/media/v4l2-core/v4l2-ioctl.o
  34490	   2320	      0	  36810	   8fca	new/drivers/media/v4l2-core/v4l2-ioctl.o

> What's this, smatch? I wonder if it could be fixed
> instead of changing the code. That'd be presumably a lot more work though.

Yes, the warnings came from smatch. No idea how easy/hard would be to
change it. 

> 
> On naming --- "size" is rather more generic, but it's not a long function
> either. I'd be a bit more specific, e.g. ioc_size or arg_size.

Agreed.

I'll add the enclosed patch changing it to ioc_size.


Thanks,
Mauro

[PATCH] media: v4l2-ioctl: rename a temp var that stores _IOC_SIZE(cmd)
    
Instead of just calling it as "size", let's name it as "ioc_size",
as it reflects better its contents.
    
As this is constant along the function, also mark it as const.
    
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>

diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index a5dab16ff2d2..f48c505550e0 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -2833,15 +2833,15 @@ video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
 	size_t  array_size = 0;
 	void __user *user_ptr = NULL;
 	void	**kernel_ptr = NULL;
-	size_t	size = _IOC_SIZE(cmd);
+	const size_t ioc_size = _IOC_SIZE(cmd);
 
 	/*  Copy arguments into temp kernel buffer  */
 	if (_IOC_DIR(cmd) != _IOC_NONE) {
-		if (size <= sizeof(sbuf)) {
+		if (ioc_size <= sizeof(sbuf)) {
 			parg = sbuf;
 		} else {
 			/* too big to allocate from stack */
-			mbuf = kvmalloc(size, GFP_KERNEL);
+			mbuf = kvmalloc(ioc_size, GFP_KERNEL);
 			if (NULL == mbuf)
 				return -ENOMEM;
 			parg = mbuf;
@@ -2849,7 +2849,7 @@ video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
 
 		err = -EFAULT;
 		if (_IOC_DIR(cmd) & _IOC_WRITE) {
-			unsigned int n = size;
+			unsigned int n = ioc_size;
 
 			/*
 			 * In some cases, only a few fields are used as input,
@@ -2870,11 +2870,11 @@ video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
 				goto out;
 
 			/* zero out anything we don't copy from userspace */
-			if (n < size)
-				memset((u8 *)parg + n, 0, size - n);
+			if (n < ioc_size)
+				memset((u8 *)parg + n, 0, ioc_size - n);
 		} else {
 			/* read-only ioctl */
-			memset(parg, 0, size);
+			memset(parg, 0, ioc_size);
 		}
 	}
 
@@ -2932,7 +2932,7 @@ video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
 	switch (_IOC_DIR(cmd)) {
 	case _IOC_READ:
 	case (_IOC_WRITE | _IOC_READ):
-		if (copy_to_user((void __user *)arg, parg, size))
+		if (copy_to_user((void __user *)arg, parg, ioc_size))
 			err = -EFAULT;
 		break;
 	}

  reply	other threads:[~2018-03-26 10:08 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-23 11:56 [PATCH 01/30] media: dvbdev: handle ENOMEM error at dvb_module_probe() Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 02/30] media: imx-media-utils: fix a warning Mauro Carvalho Chehab
2018-03-23 12:03   ` Dan Carpenter
2018-03-23 11:56 ` [PATCH 03/30] media: dvb_frontend: add proper __user annotations Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 04/30] media: vpss: fix annotations for vpss_regs_base2 Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 05/30] media: rca: declare formats var as static Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 06/30] media: ov5670: get rid of a series of __be warnings Mauro Carvalho Chehab
2018-03-23 21:45   ` Sakari Ailus
2018-03-23 11:56 ` [PATCH 07/30] media: v4l2-tpg-core: avoid buffer overflows Mauro Carvalho Chehab
2018-03-23 12:35   ` Hans Verkuil
2018-03-23 11:56 ` [PATCH 08/30] media: v4l2-ioctl: fix some "too small" warnings Mauro Carvalho Chehab
2018-03-23 21:53   ` Sakari Ailus
2018-03-26 10:08     ` Mauro Carvalho Chehab [this message]
2018-03-26 10:28       ` Sakari Ailus
2018-03-26 18:47       ` Laurent Pinchart
2018-03-26 20:28         ` Mauro Carvalho Chehab
2018-03-27 11:31           ` Laurent Pinchart
2018-03-23 11:56 ` [PATCH 09/30] media: sp887x: fix a warning Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 10/30] media: tvaudio: improve error handling Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 11/30] media: bttv-input: better handle errors at I2C transfer Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 12/30] media: solo6x10: simplify the logic at solo_p2m_dma_desc() Mauro Carvalho Chehab
2018-03-23 11:56 ` [PATCH 13/30] media: cx88: fix two warnings Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 14/30] media: cx23885: fix a warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 15/30] soc_camera: fix a weird cast on printk Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 16/30] media: videobuf-dma-sg: Fix a weird cast Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 17/30] media: ivtvfb: Cleanup some warnings Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 18/30] media: s2255drv: fix a casting warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 19/30] media: saa7134-input: improve error handling Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 20/30] media: ir-kbd-i2c: improve error handling code Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 21/30] media: ir-kbd-i2c: change the if logic to avoid a warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 22/30] media: zoran: don't cast pointers to print them Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 23/30] media: solo6x10: get rid of an address space warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 24/30] media: saa7134-alsa: don't use casts to print a buffer address Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 25/30] media: vivid-radio-rx: add a cast to avoid a warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 26/30] media: zr364xx: avoid casting just to print pointer address Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 27/30] media: em28xx-input: improve error handling code Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 28/30] media: tm6000: avoid casting just to print pointer address Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 29/30] media: tda9840: cleanup a warning Mauro Carvalho Chehab
2018-03-23 11:57 ` [PATCH 30/30] media: cec-core: fix a bug at cec_error_inj_write() Mauro Carvalho Chehab
2018-03-23 12:24   ` 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=20180326070816.26859af6@vento.lan \
    --to=mchehab@s-opensource.com \
    --cc=hans.verkuil@cisco.com \
    --cc=laurent.pinchart+renesas@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@infradead.org \
    --cc=ramesh.shanmugasundaram@bp.renesas.com \
    --cc=s.nawrocki@samsung.com \
    --cc=sakari.ailus@linux.intel.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