linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hid: uhid.c:  Cleaning up missing null-terminate in conjunction with strncpy
@ 2014-07-26 16:46 Rickard Strandqvist
  2014-07-28  8:31 ` David Herrmann
  0 siblings, 1 reply; 2+ messages in thread
From: Rickard Strandqvist @ 2014-07-26 16:46 UTC (permalink / raw)
  To: David Herrmann, Jiri Kosina
  Cc: Rickard Strandqvist, linux-input, linux-kernel

Replacing strncpy with strlcpy to avoid strings that lacks null terminate.
And removed unnecessary magic numbers.

Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
 drivers/hid/uhid.c |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c
index 0cb92e3..b72ab0c 100644
--- a/drivers/hid/uhid.c
+++ b/drivers/hid/uhid.c
@@ -392,12 +392,9 @@ static int uhid_dev_create(struct uhid_device *uhid,
 		goto err_free;
 	}
 
-	strncpy(hid->name, ev->u.create.name, 127);
-	hid->name[127] = 0;
-	strncpy(hid->phys, ev->u.create.phys, 63);
-	hid->phys[63] = 0;
-	strncpy(hid->uniq, ev->u.create.uniq, 63);
-	hid->uniq[63] = 0;
+	strlcpy(hid->name, ev->u.create.name, sizeof(hid->name));
+	strlcpy(hid->phys, ev->u.create.phys, sizeof(hid->phys));
+	strlcpy(hid->uniq, ev->u.create.uniq, sizeof(hid->uniq));
 
 	hid->ll_driver = &uhid_hid_driver;
 	hid->bus = ev->u.create.bus;
@@ -452,12 +449,9 @@ static int uhid_dev_create2(struct uhid_device *uhid,
 		goto err_free;
 	}
 
-	strncpy(hid->name, ev->u.create2.name, 127);
-	hid->name[127] = 0;
-	strncpy(hid->phys, ev->u.create2.phys, 63);
-	hid->phys[63] = 0;
-	strncpy(hid->uniq, ev->u.create2.uniq, 63);
-	hid->uniq[63] = 0;
+	strlcpy(hid->name, ev->u.create2.name, sizeof(hid->name));
+	strlcpy(hid->phys, ev->u.create2.phys, sizeof(hid->phys));
+	strlcpy(hid->uniq, ev->u.create2.uniq, sizeof(hid->uniq));
 
 	hid->ll_driver = &uhid_hid_driver;
 	hid->bus = ev->u.create2.bus;
-- 
1.7.10.4

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

* Re: [PATCH] hid: uhid.c: Cleaning up missing null-terminate in conjunction with strncpy
  2014-07-26 16:46 [PATCH] hid: uhid.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
@ 2014-07-28  8:31 ` David Herrmann
  0 siblings, 0 replies; 2+ messages in thread
From: David Herrmann @ 2014-07-28  8:31 UTC (permalink / raw)
  To: Rickard Strandqvist
  Cc: David Herrmann, Jiri Kosina, open list:HID CORE LAYER,
	linux-kernel

Hi

On Sat, Jul 26, 2014 at 6:46 PM, Rickard Strandqvist
<rickard_strandqvist@spectrumdigital.se> wrote:
> Replacing strncpy with strlcpy to avoid strings that lacks null terminate.
> And removed unnecessary magic numbers.
>
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> ---
>  drivers/hid/uhid.c |   18 ++++++------------
>  1 file changed, 6 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c
> index 0cb92e3..b72ab0c 100644
> --- a/drivers/hid/uhid.c
> +++ b/drivers/hid/uhid.c
> @@ -392,12 +392,9 @@ static int uhid_dev_create(struct uhid_device *uhid,
>                 goto err_free;
>         }
>
> -       strncpy(hid->name, ev->u.create.name, 127);
> -       hid->name[127] = 0;
> -       strncpy(hid->phys, ev->u.create.phys, 63);
> -       hid->phys[63] = 0;
> -       strncpy(hid->uniq, ev->u.create.uniq, 63);
> -       hid->uniq[63] = 0;
> +       strlcpy(hid->name, ev->u.create.name, sizeof(hid->name));
> +       strlcpy(hid->phys, ev->u.create.phys, sizeof(hid->phys));
> +       strlcpy(hid->uniq, ev->u.create.uniq, sizeof(hid->uniq));

NAK, this patch is wrong. strlcpy() is *not* a replacement for
strncpy(). Please see lib/string.c or the man-page of strlcpy(3). It
calls strlen() on the source, which strncpy() does not. "ev" was
copied unmodified from user-space and might not be NULL-terminated,
therefore, strncpy() is the right choice.

Btw., please avoid commit-messages that sound like bug-fixes, which
this definitely is not. hid_allocate_device() cleans the object to
ZERO, therefore, you're guaranteed that there's a proper
NULL-terminator, independent of the contents of ev->u.xyz.

Thanks
David

>         hid->ll_driver = &uhid_hid_driver;
>         hid->bus = ev->u.create.bus;
> @@ -452,12 +449,9 @@ static int uhid_dev_create2(struct uhid_device *uhid,
>                 goto err_free;
>         }
>
> -       strncpy(hid->name, ev->u.create2.name, 127);
> -       hid->name[127] = 0;
> -       strncpy(hid->phys, ev->u.create2.phys, 63);
> -       hid->phys[63] = 0;
> -       strncpy(hid->uniq, ev->u.create2.uniq, 63);
> -       hid->uniq[63] = 0;
> +       strlcpy(hid->name, ev->u.create2.name, sizeof(hid->name));
> +       strlcpy(hid->phys, ev->u.create2.phys, sizeof(hid->phys));
> +       strlcpy(hid->uniq, ev->u.create2.uniq, sizeof(hid->uniq));
>
>         hid->ll_driver = &uhid_hid_driver;
>         hid->bus = ev->u.create2.bus;
> --
> 1.7.10.4
>

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

end of thread, other threads:[~2014-07-28  8:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-26 16:46 [PATCH] hid: uhid.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
2014-07-28  8:31 ` David Herrmann

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).