linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pavel Machek <pavel@ucw.cz>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Jacek Anaszewski" <jacek.anaszewski@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-leds@vger.kernel.org
Subject: Re: [PATCH 1/4] leds: netdev trigger: use memcpy in device_name_store
Date: Thu, 14 Mar 2019 13:16:39 +0100	[thread overview]
Message-ID: <20190314121639.GB19072@amd> (raw)
In-Reply-To: <9324dc3c-a9dd-e179-6fc5-17b2fdaab74b@rasmusvillemoes.dk>

[-- Attachment #1: Type: text/plain, Size: 4133 bytes --]

Hi!

> >> If userspace doesn't end the input with a newline (which can easily
> >> happen if the write happens from a C program that does write(fd,
> >> iface, strlen(iface))), we may end up including garbage from a
> >> previous, longer value in the device_name. For example
> >>
> >> # cat device_name
> >>
> >> # printf 'eth12' > device_name
> >> # cat device_name
> >> eth12
> >> # printf 'eth3' > device_name
> >> # cat device_name
> >> eth32
> >>
> >> I highly doubt anybody is relying on this behaviour, so switch to
> >> simply copying the bytes (we've already checked that size is <
> >> IFNAMSIZ) and unconditionally zero-terminate it; of course, we also
> >> still have to strip a trailing newline.
> > 
> >   char device_name[IFNAMSIZ];
> > 
> > Ok, good catch reporting the bug, but are you sure the fix is right?
> > 
> > AFAICT the design is that device_name does _not_ have to be zero
> > terminated, and your fix incorrectly limits the size of device_name.
> 
> Yes, I'm sure.
> 
> /**
>  *      dev_valid_name - check if name is okay for network device
>  *      @name: name string
>  *
>  *      Network device names need to be valid file names to
>  *      to allow sysfs to work.  We also disallow any kind of
>  *      whitespace.
>  */
> bool dev_valid_name(const char *name)
> {
>         if (*name == '\0')
>                 return false;
>         if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
>                 return false;
> 
> so no netdevice name has a string length > 15 (IOW, there must be a nul
> byte within the first 16 bytes of name). Also note all the places a
> net_device->name is printed with a simple %s, so they are definitely
> always 0-terminated.
> 
> Moreover, I'm actually not limiting anything more than was already done;
> we already reject any input from userspace >= 16 bytes. I'm simply
> ensuring that we're never confused by leftover garbage.

Ok. Sorry for the noise.

> >> --- a/drivers/leds/trigger/ledtrig-netdev.c
> >> +++ b/drivers/leds/trigger/ledtrig-netdev.c
> >> @@ -122,7 +122,8 @@ static ssize_t device_name_store(struct device *dev,
> >>  		trigger_data->net_dev = NULL;
> >>  	}
> >>  
> >> -	strncpy(trigger_data->device_name, buf, size);
> >> +	memcpy(trigger_data->device_name, buf, size);
> >> +	trigger_data->device_name[size] = '\0';
> > 
> > I'd do = 0 for consistency with code below.
> 
> I'd rather the other way around :) but yeah, let's just be consistent.
> I'll fix in next version.

Or just use your version and fix both places :-). Your option, as long
as it is consistent. 

> > I believe the strncpy() is right to use here, but code should be
> > modified so that zero-termination is not required.
> 
> So, I believe the above should convince you that strncpy is wrong. Or
> rather, strncpy is really just a convoluted memcpy: the userspace input
> doesn't contain a nul among the size bytes [1], so the "copy all the
> bytes, but don't nul-terminate" semantics of strncpy kick in - which is
> often a security bug, but the code is such that the zero at
> device_name[15] (because it was originally kzalloc'ed) is never
> overwritten. So in theory, we could keep strncpy and just add the
> nul-termination, but the str* prefix is very misleading (which is
> probably why the bug happened in the first place).
> 
> [1] And if it did, the "zero the rest of the output buffer" semantics
> kick in. That's functionally equivalent to my memcpy() + write one nul
> byte, since nothing after that first nul byte in device_name would ever
> be inspected.

Actually "zero the rest" semantics kind of makes sense here.

No, I'm sorry, I don't understand. How are you getting stale data
there?

> >> # printf 'eth12' > device_name
> >> # cat device_name
> >> eth12
> >> # printf 'eth3' > device_name
> >> # cat device_name
> >> eth32

strncpy() should zero the rest, and you should get "eth3"...?!

									Pavel

-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

  reply	other threads:[~2019-03-14 12:16 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190311144227.GA4404@amd>
     [not found] ` <20190313202615.22883-1-linux@rasmusvillemoes.dk>
2019-03-13 20:26   ` [PATCH 1/4] leds: netdev trigger: use memcpy in device_name_store Rasmus Villemoes
2019-03-14  9:29     ` Uwe Kleine-König
2019-03-14  9:57       ` Rasmus Villemoes
2019-03-14 10:04         ` Uwe Kleine-König
2019-03-14 10:14     ` Pavel Machek
2019-03-14 10:54       ` Rasmus Villemoes
2019-03-14 12:16         ` Pavel Machek [this message]
2019-03-13 20:26   ` [PATCH 2/4] leds: netdev trigger: factor out middle part of device_name_store Rasmus Villemoes
2019-03-14  9:31     ` Uwe Kleine-König
2019-03-14  9:57       ` Rasmus Villemoes
2019-03-14 10:15       ` Pavel Machek
2019-03-14 10:20         ` Uwe Kleine-König
2019-03-13 20:26   ` [PATCH 3/4] leds: netdev trigger: add documentation to leds/common.txt Rasmus Villemoes
2019-03-13 20:26   ` [PATCH 4/4] leds: netdev trigger: allow setting initial values in device tree Rasmus Villemoes
2019-03-14  9:36     ` Uwe Kleine-König
2019-03-14 10:28       ` Rasmus Villemoes
2019-03-14 10:29     ` Pavel Machek
2019-03-14 11:26       ` Rasmus Villemoes
2019-03-14 12:00         ` Pavel Machek
2019-03-14 13:19           ` Rasmus Villemoes
2019-03-17 19:11             ` Pavel Machek
2019-03-24 20:39               ` Rasmus Villemoes
2019-03-14 10:32     ` Jacek Anaszewski
     [not found]   ` <20190314140619.3309-1-linux@rasmusvillemoes.dk>
2019-03-14 14:06     ` [PATCH v2 1/6] leds: netdev trigger: use memcpy in device_name_store Rasmus Villemoes
2019-03-18 11:20       ` Pavel Machek
2019-03-26 19:53       ` Jacek Anaszewski
2019-03-27 15:26         ` Rasmus Villemoes
2019-03-27 21:20           ` Jacek Anaszewski
2019-03-27 21:31             ` Rasmus Villemoes
2019-03-27 21:45               ` Jacek Anaszewski
2019-03-14 14:06     ` [PATCH v2 2/6] leds: netdev trigger: factor out middle part of device_name_store Rasmus Villemoes
2019-03-18 11:24       ` Pavel Machek
2019-03-14 14:06     ` [PATCH v2 3/6] leds: netdev trigger: move newline handling back to device_name_store Rasmus Villemoes
2019-03-18 11:25       ` Pavel Machek
2019-03-14 14:06     ` [PATCH v2 4/6] leds: netdev trigger: move name length checking to netdev_trig_set_device Rasmus Villemoes
2019-03-18 11:26       ` Pavel Machek
2019-03-14 14:06     ` [PATCH v2 5/6] leds: netdev trigger: add documentation to leds/common.txt Rasmus Villemoes
2019-03-14 14:06     ` [PATCH v2 6/6] leds: netdev trigger: allow setting initial values in device tree Rasmus Villemoes
2019-03-14 14:24       ` Jacek Anaszewski
2019-03-14 15:05         ` Rasmus Villemoes
2019-03-14 15:36           ` Jacek Anaszewski
2019-03-18 11:54       ` Pavel Machek
2019-03-28 16:28       ` Rob Herring

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=20190314121639.GB19072@amd \
    --to=pavel@ucw.cz \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=u.kleine-koenig@pengutronix.de \
    /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).