linux: Add HWCAP2_HBC from Linux 6.6 to AArch64 bits/hwcap.h
[glibc.git] / elf / tst-env-setuid-tunables.c
blobf0b92c97e7670a93ff7300a8d97d0329fd184452
1 /* Copyright (C) 2017-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Verify that tunables correctly filter out unsafe tunables like
19 glibc.malloc.check and glibc.malloc.mmap_threshold but also retain
20 glibc.malloc.mmap_threshold in an unprivileged child. */
22 #define _LIBC 1
23 #include "config.h"
24 #undef _LIBC
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <intprops.h>
36 #include <array_length.h>
38 #include <support/check.h>
39 #include <support/support.h>
40 #include <support/test-driver.h>
41 #include <support/capture_subprocess.h>
43 const char *teststrings[] =
45 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
46 "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
47 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
48 "glibc.malloc.perturb=0x800",
49 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
50 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
51 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
52 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
53 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
54 "glibc.malloc.check=2",
55 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
56 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
57 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
58 "glibc.malloc.check=1:glibc.malloc.check=2",
59 "not_valid.malloc.check=2",
60 "glibc.not_valid.check=2",
63 const char *resultstrings[] =
65 "glibc.malloc.mmap_threshold=4096",
66 "glibc.malloc.mmap_threshold=4096",
67 "glibc.malloc.mmap_threshold=4096",
68 "glibc.malloc.perturb=0x800",
69 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
70 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
71 "glibc.malloc.mmap_threshold=4096",
72 "glibc.malloc.mmap_threshold=4096",
73 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
74 "",
75 "",
76 "",
77 "",
78 "",
79 "",
80 "",
83 static int
84 test_child (int off)
86 const char *val = getenv ("GLIBC_TUNABLES");
88 printf (" [%d] GLIBC_TUNABLES is %s\n", off, val);
89 fflush (stdout);
90 if (val != NULL && strcmp (val, resultstrings[off]) == 0)
91 return 0;
93 if (val != NULL)
94 printf (" [%d] Unexpected GLIBC_TUNABLES VALUE %s, expected %s\n",
95 off, val, resultstrings[off]);
96 else
97 printf (" [%d] GLIBC_TUNABLES environment variable absent\n", off);
99 fflush (stdout);
101 return 1;
104 static int
105 do_test (int argc, char **argv)
107 /* Setgid child process. */
108 if (argc == 2)
110 if (getgid () == getegid ())
111 /* This can happen if the file system is mounted nosuid. */
112 FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
113 (intmax_t) getgid ());
115 int ret = test_child (atoi (argv[1]));
117 if (ret != 0)
118 exit (1);
120 /* Special return code to make sure that the child executed all the way
121 through. */
122 exit (42);
124 else
126 /* Spawn tests. */
127 for (int i = 0; i < array_length (teststrings); i++)
129 char buf[INT_BUFSIZE_BOUND (int)];
131 printf ("[%d] Spawned test for %s\n", i, teststrings[i]);
132 snprintf (buf, sizeof (buf), "%d\n", i);
133 fflush (stdout);
134 if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
136 printf (" [%d] Failed to set GLIBC_TUNABLES: %m", i);
137 support_record_failure ();
138 continue;
141 int status = support_capture_subprogram_self_sgid (buf);
143 /* Bail out early if unsupported. */
144 if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
145 return EXIT_UNSUPPORTED;
147 if (WEXITSTATUS (status) != 42)
149 printf (" [%d] child failed with status %d\n", i,
150 WEXITSTATUS (status));
151 support_record_failure ();
154 return 0;
158 #define TEST_FUNCTION_ARGV do_test
159 #include <support/test-driver.c>