glibc/sysdeps
Michal Nazarewicz a9880586ee linux: sysconf: limit _SC_MAX_ARG to 6 MiB (BZ #25305)
Since Linux 4.13, kernel limits the maximum command line arguments
length to 6 MiB [1].  Normally the limit is still quarter of the maximum
stack size but if that limit exceeds 6 MiB it's clamped down.

glibc's __sysconf implementation for Linux platform is not aware of
this limitation and for stack sizes of over 24 MiB it returns higher
ARG_MAX than Linux will actually accept.  This can be verified by
executing the following application on Linux 4.13 or newer:

    #include <stdio.h>
    #include <string.h>
    #include <sys/resource.h>
    #include <sys/time.h>
    #include <unistd.h>

    int main(void) {
            const struct rlimit rlim = { 40 * 1024 * 1024,
                                         40 * 1024 * 1024 };
            if (setrlimit(RLIMIT_STACK, &rlim) < 0) {
                    perror("setrlimit: RLIMIT_STACK");
                    return 1;
            }

            printf("ARG_MAX     : %8ld\n", sysconf(_SC_ARG_MAX));
            printf("63 * 100 KiB: %8ld\n", 63L * 100 * 1024);
            printf("6 MiB       : %8ld\n", 6L * 1024 * 1024);

            char str[100 * 1024], *argv[64], *envp[1];
            memset(&str, 'A', sizeof str);
            str[sizeof str - 1] = '\0';
            for (size_t i = 0; i < sizeof argv / sizeof *argv - 1; ++i) {
                    argv[i] = str;
            }
            argv[sizeof argv / sizeof *argv - 1] = envp[0] = 0;

            execve("/bin/true", argv, envp);
            perror("execve");
            return 1;
    }

On affected systems the program will report ARG_MAX as 10 MiB but
despite that executing /bin/true with a bit over 6 MiB of command line
arguments will fail with E2BIG error.  Expected result is that ARG_MAX
is reported as 6 MiB.

Update the __sysconf function to clamp ARG_MAX value to 6 MiB if it
would otherwise exceed it.  This resolves bug #25305 which was market
WONTFIX as suggested solution was to cap ARG_MAX at 128 KiB.

As an aside and point of comparison, bionic (a libc implementation for
Android systems) decided to resolve this issue by always returning 128
KiB ignoring any potential xargs regressions [2].

On older kernels this results in returning overly conservative value
but that's a safer option than being aggressive and returning invalid
value on recent systems.  It's also worth noting that at this point
all supported Linux releases have the 6 MiB barrier so only someone
running an unsupported kernel version would get incorrectly truncated
result.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

[1] See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=da029c11e6b12f321f36dac8771e833b65cec962
[2] See baed51ee3a
2021-04-13 17:10:02 -03:00
..
aarch64 aarch64: update libm test ulps 2021-04-08 08:24:30 +01:00
alpha Implement <unwind-link.h> for dynamically loading the libgcc_s unwinder 2021-03-01 15:58:01 +01:00
arc Reduce the statically linked startup code [BZ #23323] 2021-02-25 12:13:02 +01:00
arm arm: update libm test ulps 2021-04-08 09:55:33 +01:00
csky Reduce the statically linked startup code [BZ #23323] 2021-02-25 12:13:02 +01:00
generic fork.h: replace with register-atfork.h 2021-03-29 21:41:09 +02:00
gnu Move sysdeps/gnu/unwind-resume.c to sysdeps/generic/unwind-resume.c 2021-03-01 15:59:49 +01:00
hppa Update hppa libm-test-ulps 2021-04-06 18:55:58 +00:00
htl fork.h: replace with register-atfork.h 2021-03-29 21:41:09 +02:00
hurd Update copyright dates with scripts/update-copyrights 2021-01-02 12:17:34 -08:00
i386 i386: Update ulps 2021-04-13 16:33:27 -03:00
ia64 ia64: Update ulps 2021-04-05 10:11:09 -03:00
ieee754 Improve the accuracy of tgamma (BZ #26983) 2021-04-07 13:23:39 +02:00
m68k m68: Fix build after 9acda61d94 2021-04-06 15:10:31 -03:00
mach hurd: Export _hurd_libc_proc_init 2021-04-12 00:23:36 +02:00
microblaze Reduce the statically linked startup code [BZ #23323] 2021-02-25 12:13:02 +01:00
mips Implement <unwind-link.h> for dynamically loading the libgcc_s unwinder 2021-03-01 15:58:01 +01:00
nios2 Update Nios II libm-test-ulps. 2021-04-01 19:41:40 +00:00
nptl fork.h: replace with register-atfork.h 2021-03-29 21:41:09 +02:00
posix nss: fix nss_database_lookup2's alternate handling [BZ #27416] 2021-03-09 14:34:50 -05:00
powerpc powerpc: Update libm test ulps 2021-04-09 17:41:22 -03:00
pthread nptl: Remove open from libpthread 2021-03-26 13:37:14 -03:00
riscv Reduce the statically linked startup code [BZ #23323] 2021-02-25 12:13:02 +01:00
s390 s390: Update ulps 2021-04-13 16:33:27 -03:00
sh Implement <unwind-link.h> for dynamically loading the libgcc_s unwinder 2021-03-01 15:58:01 +01:00
sparc Fix the inaccuracy of j0f/j1f/y0f/y1f [BZ #14469, #14470, #14471, #14472] 2021-04-02 06:15:48 +02:00
unix linux: sysconf: limit _SC_MAX_ARG to 6 MiB (BZ #25305) 2021-04-13 17:10:02 -03:00
wordsize-32 Update copyright dates with scripts/update-copyrights 2021-01-02 12:17:34 -08:00
wordsize-64 Update copyright dates with scripts/update-copyrights 2021-01-02 12:17:34 -08:00
x86 Move __isnanf128 to libc.so 2021-03-30 14:58:19 +05:30
x86_64 Improve the accuracy of tgamma (BZ #26983) 2021-04-07 13:23:39 +02:00