public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Jann Horn <jannh@google.com>
Cc: Danilo Krummrich <dakr@kernel.org>,
	 Luis Chamberlain <mcgrof@kernel.org>,
	 Russ Weight <russ.weight@linux.dev>,
	 Danilo Krummrich <dakr@redhat.com>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	linux-kernel@vger.kernel.org,  stable@vger.kernel.org
Subject: Re: [PATCH v2] firmware_loader: Block path traversal
Date: Mon, 26 Aug 2024 09:59:23 +0200	[thread overview]
Message-ID: <87seurd89w.fsf@prevas.dk> (raw)
In-Reply-To: <CAG48ez3A=NZ9GqkQv9U6871ciNc+Yy=AvPfm3UgeXfMyh=0+oQ@mail.gmail.com> (Jann Horn's message of "Sat, 24 Aug 2024 03:34:20 +0200")

Jann Horn <jannh@google.com> writes:

> On Sat, Aug 24, 2024 at 2:31 AM Danilo Krummrich <dakr@kernel.org> wrote:
>> On Fri, Aug 23, 2024 at 08:38:55PM +0200, Jann Horn wrote:
>> > Fix it by rejecting any firmware names containing ".." path components.
> [...]
>> > +/*
>> > + * Reject firmware file names with ".." path components.
>> > + * There are drivers that construct firmware file names from device-supplied
>> > + * strings, and we don't want some device to be able to tell us "I would like to
>> > + * be sent my firmware from ../../../etc/shadow, please".
>> > + *
>> > + * Search for ".." surrounded by either '/' or start/end of string.
>> > + *
>> > + * This intentionally only looks at the firmware name, not at the firmware base
>> > + * directory or at symlink contents.
>> > + */
>> > +static bool name_contains_dotdot(const char *name)
>> > +{
>> > +     size_t name_len = strlen(name);
>> > +     size_t i;
>> > +
>> > +     if (name_len < 2)
>> > +             return false;
>> > +     for (i = 0; i < name_len - 1; i++) {
>> > +             /* do we see a ".." sequence? */
>> > +             if (name[i] != '.' || name[i+1] != '.')
>> > +                     continue;
>> > +
>> > +             /* is it a path component? */
>> > +             if ((i == 0 || name[i-1] == '/') &&
>> > +                 (i == name_len - 2 || name[i+2] == '/'))
>> > +                     return true;
>> > +     }
>> > +     return false;
>> > +}
>>
>> Why do you open code it, instead of using strstr() and strncmp() like you did
>> in v1? I think your approach from v1 read way better.
>
> The code in v1 was kinda sloppy - it was probably good enough for this
> check, but not good enough to put in a function called
> name_contains_dotdot() that is documented to exactly search for any
> ".." components.
>
> Basically, the precise regex we have to search for is something like
> /(^|/)\.\.($|/)/
>
> To implement that by searching for substrings like in v1, we'd have to
> search for each possible combination of the capture groups in the
> regex, which gives the following four (pow(2,2)) patterns:
>
> <start>..<end>
> <start>../
> /..<end>
> /../
>
> So written like in v1, that'd look something like:
>
> if (strcmp(name, "..") == 0 || strncmp(name, "../", 3) == 0 ||
> strstr(name, "/../") != NULL || (name_len >= 3 &&
> strcmp(name+name_len-3, "/..") == 0)))
>   return true;
>
> Compared to that, I prefer the code I wrote in v2, since it is less
> repetitive. But if you want, I can change it to the expression I wrote
> just now.

Maybe

	for (p = s; (q = strstr(p, "..")) != NULL; p = q+2) {
		if ((q == s || q[-1] == '/') &&
		    (q[2] == '\0' || q[2] == '/'))
			return true;
	}
        return false;

?

Rasmus

  reply	other threads:[~2024-08-26  7:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-23 18:38 [PATCH v2] firmware_loader: Block path traversal Jann Horn
2024-08-23 21:13 ` Luis Chamberlain
2024-08-24  0:14   ` Danilo Krummrich
2024-08-24  1:14   ` Linus Torvalds
2024-08-24  1:48     ` Jann Horn
2024-08-24  2:02       ` Linus Torvalds
2024-08-26 12:54     ` Christian Brauner
2024-08-24  0:31 ` Danilo Krummrich
2024-08-24  1:34   ` Jann Horn
2024-08-26  7:59     ` Rasmus Villemoes [this message]
2024-08-26  9:13     ` Danilo Krummrich
2024-08-26  9:10 ` Danilo Krummrich

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=87seurd89w.fsf@prevas.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=dakr@kernel.org \
    --cc=dakr@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=rafael@kernel.org \
    --cc=russ.weight@linux.dev \
    --cc=stable@vger.kernel.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