From: Alejandro Colomar <alx@kernel.org>
To: Matthew House <mattlloydhouse@gmail.com>
Cc: Rik van Riel <riel@surriel.com>,
linux-man@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
kernel-team@meta.com, Eric Biederman <ebiederm@xmission.com>
Subject: Re: [PATCH] execve.2: execve also returns E2BIG if a string is too long
Date: Wed, 11 Oct 2023 17:50:44 +0200 [thread overview]
Message-ID: <ZSbEVHeUSwdRwUoR@debian> (raw)
In-Reply-To: <20231011151126.754612-1-mattlloydhouse@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2670 bytes --]
Hi Matthew,
On Wed, Oct 11, 2023 at 11:11:24AM -0400, Matthew House wrote:
> On Wed, Oct 11, 2023 at 10:47 AM Alejandro Colomar <alx@kernel.org> wrote:
> > On Wed, Oct 11, 2023 at 09:44:29AM -0400, Matthew House wrote:
> > > To expand on this, there are basically two separate byte limits in
> > > fs/exec.c, one for each individual argv/envp string, and another for all
> > > strings and all pointers to them as a whole. To put the whole thing in
> > > pseudocode, the checks work effectively like this, assuming I haven't made
> > > any errors:
> > >
> > > int argc, envc;
> > > unsigned long bytes, limit;
> > >
> > > /* assume that argv has already been adjusted to add an empty argv[0] */
> > > argc = 0, envc = 0, bytes = 0;
> > > for (char **a = argv; *a != NULL; a++, argc++) {
> > > if (strlen(*a) >= MAX_ARG_STRLEN)
> >
> > Are you sure this is >= and not > ?
>
> Yes. In general, the kernel's string limits tend to include the trailing
> null byte. There are two places where this limit is enforced, one for the
> pathname (or full pathname for execveat) and the other for the argv/envp
> strings. The pathname is handled by copy_string_kernel():
>
> int len = strnlen(arg, MAX_ARG_STRLEN) + 1 /* terminating NUL */;
>
> if (len == 0)
> return -EFAULT;
> if (!valid_arg_len(bprm, len))
> return -E2BIG;
>
> where valid_arg_len(bprm, len) is just (len <= MAX_ARG_STRLEN). Here,
> strnlen() has the same behavior as the ordinary libc strnlen(3),
> effectively returning min(strlen(arg), MAX_ARG_STRLEN). Thus, the check
> succeeds iff strlen(arg) + 1 <= MAX_ARG_STRLEN, or equivalently, iff
> strlen(arg) < MAX_ARG_STRLEN.
>
> Next, each of the environment and argument strings is handled by
> copy_strings():
>
> len = strnlen_user(str, MAX_ARG_STRLEN);
> if (!len)
> goto out;
>
> ret = -E2BIG;
> if (!valid_arg_len(bprm, len))
> goto out;
>
> The strnlen_user() function, per its documentation, is explicitly inclusive
> of the trailing null byte:
>
> * Returns the size of the string INCLUDING the terminating NUL.
> * If the string is too long, returns a number larger than @count. User
> * has to check the return value against "> count".
> * On exception (or invalid count), returns 0.
>
> Thus, the check succeeds iff the size including the null byte is
> <= MAX_ARG_STRLEN, i.e., iff strlen(arg) + 1 <= MAX_ARG_STRLEN, or
> strlen(arg) < MAX_ARG_STRLEN.
Thanks! It's a bit confusing to see the terms 'len' and '_STRLEN'
meaning length+1, but it makes sense now.
Cheers,
Alex
>
> Matthew House
--
<https://www.alejandro-colomar.es/>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-10-11 15:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-11 3:41 [PATCH] execve.2: execve also returns E2BIG if a string is too long Rik van Riel
2023-10-11 10:41 ` Alejandro Colomar
2023-10-11 13:21 ` Rik van Riel
2023-10-11 13:44 ` Matthew House
2023-10-11 14:44 ` Alejandro Colomar
2023-10-11 14:47 ` Alejandro Colomar
2023-10-11 15:11 ` Matthew House
2023-10-11 15:50 ` Alejandro Colomar [this message]
2023-10-11 14:42 ` Alejandro Colomar
2023-10-11 13:52 ` Matthew House
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=ZSbEVHeUSwdRwUoR@debian \
--to=alx@kernel.org \
--cc=ebiederm@xmission.com \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-man@vger.kernel.org \
--cc=mattlloydhouse@gmail.com \
--cc=riel@surriel.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