Re: [PATCH] execve.2: execve also returns E2BIG if a string is too long
From: Alejandro Colomar
Date: Wed Oct 11 2023 - 10:44:24 EST
Hi Matthew,
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)
> return -E2BIG;
> bytes += strlen(*a) + 1;
> }
> for (char **e = envp; *e != NULL; e++, envc++) {
> if (strlen(*e) >= MAX_ARG_STRLEN)
> return -E2BIG;
> bytes += strlen(*e) + 1;
> }
>
> if (argc > MAX_ARG_STRINGS || envc > MAX_ARG_STRINGS)
> return -E2BIG;
> bytes += (argc + envc) * sizeof(void *);
>
> limit = max(min(_STK_LIM / 4 * 3, rlim_stack.rlim_cur / 4), ARG_MAX);
> if (bytes > limit)
> return -E2BIG;
>
> Thank you,
> Matthew House
Thanks!
This thing would be useful in the commit message. An example program
demonstrating it would be even better.
Cheers,
Alex
--
<https://www.alejandro-colomar.es/>
Attachment:
signature.asc
Description: PGP signature