1 /* Copyright (C) 2017-2024 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 GLIBC_TUNABLES is kept unchanged but no tunable is actually
19 enabled for AT_SECURE processes. */
21 #include <dl-tunables.h>
32 #include <array_length.h>
34 #include <support/check.h>
35 #include <support/support.h>
36 #include <support/test-driver.h>
37 #include <support/capture_subprocess.h>
39 static const char *teststrings
[] =
41 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
42 "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
43 "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
44 "glibc.malloc.perturb=0x800",
45 "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
46 "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
47 "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
48 "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
49 "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
50 "glibc.malloc.check=2",
51 "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
52 "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
53 ":glibc.malloc.garbage=2:glibc.malloc.check=1",
54 "glibc.malloc.check=1:glibc.malloc.check=2",
55 "not_valid.malloc.check=2",
56 "glibc.not_valid.check=2",
62 const char *val
= getenv ("GLIBC_TUNABLES");
65 printf (" [%d] GLIBC_TUNABLES is %s\n", off
, val
);
68 printf (" [%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off
, val
);
73 /* Also check if the set tunables are effectively unchanged. */
74 int32_t check
= TUNABLE_GET_FULL (glibc
, malloc
, check
, int32_t, NULL
);
75 size_t mmap_threshold
= TUNABLE_GET_FULL (glibc
, malloc
, mmap_threshold
,
77 int32_t perturb
= TUNABLE_GET_FULL (glibc
, malloc
, perturb
, int32_t, NULL
);
79 printf (" [%d] glibc.malloc.check=%d\n", off
, check
);
81 printf (" [%d] glibc.malloc.mmap_threshold=%zu\n", off
, mmap_threshold
);
83 printf (" [%d] glibc.malloc.perturb=%d\n", off
, perturb
);
87 ret
|= mmap_threshold
!= 0;
94 do_test (int argc
, char **argv
)
96 /* Setgid child process. */
99 if (getgid () == getegid ())
100 /* This can happen if the file system is mounted nosuid. */
101 FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
102 (intmax_t) getgid ());
104 int ret
= test_child (atoi (argv
[1]));
109 /* Special return code to make sure that the child executed all the way
116 for (int i
= 0; i
< array_length (teststrings
); i
++)
118 char buf
[INT_BUFSIZE_BOUND (int)];
120 printf ("[%d] Spawned test for %s\n", i
, teststrings
[i
]);
121 snprintf (buf
, sizeof (buf
), "%d\n", i
);
123 if (setenv ("GLIBC_TUNABLES", teststrings
[i
], 1) != 0)
125 printf (" [%d] Failed to set GLIBC_TUNABLES: %m", i
);
126 support_record_failure ();
130 int status
= support_capture_subprogram_self_sgid (buf
);
132 /* Bail out early if unsupported. */
133 if (WEXITSTATUS (status
) == EXIT_UNSUPPORTED
)
134 return EXIT_UNSUPPORTED
;
136 if (WEXITSTATUS (status
) != 42)
138 printf (" [%d] child failed with status %d\n", i
,
139 WEXITSTATUS (status
));
140 support_record_failure ();
147 #define TEST_FUNCTION_ARGV do_test
148 #include <support/test-driver.c>