1 /* Copyright (C) 2017-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
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. */
21
22 /* This is compiled as part of the testsuite but needs to see
23 HAVE_TUNABLES. */
24 #define _LIBC 1
25 #include "config.h"
26 #undef _LIBC
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <intprops.h>
38 #include <array_length.h>
39
40 #include <support/check.h>
41 #include <support/support.h>
42 #include <support/test-driver.h>
43 #include <support/capture_subprocess.h>
44
45 const char *teststrings[] =
46 {
47 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
48 "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
49 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
50 "glibc.malloc.perturb=0x800",
51 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
52 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
53 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
54 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
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",
61 };
62
63 const char *resultstrings[] =
64 {
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 "",
74 "",
75 "",
76 "",
77 "",
78 "",
79 };
80
81 static int
test_child(int off)82 test_child (int off)
83 {
84 const char *val = getenv ("GLIBC_TUNABLES");
85
86 #if HAVE_TUNABLES
87 if (val != NULL && strcmp (val, resultstrings[off]) == 0)
88 return 0;
89
90 if (val != NULL)
91 printf ("[%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
92
93 return 1;
94 #else
95 if (val != NULL)
96 {
97 printf ("[%d] GLIBC_TUNABLES not cleared\n", off);
98 return 1;
99 }
100 return 0;
101 #endif
102 }
103
104 static int
do_test(int argc,char ** argv)105 do_test (int argc, char **argv)
106 {
107 /* Setgid child process. */
108 if (argc == 2)
109 {
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 ());
114
115 int ret = test_child (atoi (argv[1]));
116
117 if (ret != 0)
118 exit (1);
119
120 exit (EXIT_SUCCESS);
121 }
122 else
123 {
124 int ret = 0;
125
126 /* Spawn tests. */
127 for (int i = 0; i < array_length (teststrings); i++)
128 {
129 char buf[INT_BUFSIZE_BOUND (int)];
130
131 printf ("Spawned test for %s (%d)\n", teststrings[i], i);
132 snprintf (buf, sizeof (buf), "%d\n", i);
133 if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
134 exit (1);
135
136 int status = support_capture_subprogram_self_sgid (buf);
137
138 /* Bail out early if unsupported. */
139 if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
140 return EXIT_UNSUPPORTED;
141
142 ret |= status;
143 }
144 return ret;
145 }
146 }
147
148 #define TEST_FUNCTION_ARGV do_test
149 #include <support/test-driver.c>
150