]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/tst-tunables-enable_secure.c
rtld: Add glibc.rtld.enable_secure tunable.
[thirdparty/glibc.git] / elf / tst-tunables-enable_secure.c
CommitLineData
71648e80
JST
1/* Check GLIBC_TUNABLES parsing for enable_secure.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <array_length.h>
20#include <dl-tunables.h>
21#include <getopt.h>
22#include <intprops.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <support/capture_subprocess.h>
26#include <support/check.h>
27#include <sys/auxv.h>
28#include <unistd.h>
29
30static int restart;
31#define CMDLINE_OPTIONS \
32 { "restart", no_argument, &restart, 1 },
33
34static const struct test_t
35{
36 const char *env;
37 int32_t expected_malloc_check;
38 int32_t expected_enable_secure;
39} tests[] =
40{
41 /* Expected tunable format. */
42 /* Tunables should be ignored if enable_secure is set. */
43 {
44 "glibc.malloc.check=2:glibc.rtld.enable_secure=1",
45 0,
46 1,
47 },
48 /* Tunables should be ignored if enable_secure is set. */
49 {
50 "glibc.rtld.enable_secure=1:glibc.malloc.check=2",
51 0,
52 1,
53 },
54 /* Tunables should be set if enable_secure is unset. */
55 {
56 "glibc.rtld.enable_secure=0:glibc.malloc.check=2",
57 2,
58 0,
59 },
60};
61
62static int
63handle_restart (int i)
64{
65 if (tests[i].expected_enable_secure == 1)
66 {
67 TEST_COMPARE (1, __libc_enable_secure);
68 }
69 else
70 {
71 TEST_COMPARE (tests[i].expected_malloc_check,
72 TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL));
73 TEST_COMPARE (tests[i].expected_enable_secure,
74 TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t,
75 NULL));
76 }
77 return 0;
78}
79
80static int
81do_test (int argc, char *argv[])
82{
83 /* We must have either:
84 - One or four parameters left if called initially:
85 + path to ld.so optional
86 + "--library-path" optional
87 + the library path optional
88 + the application name
89 + the test to check */
90
91 TEST_VERIFY_EXIT (argc == 2 || argc == 5);
92
93 if (restart)
94 return handle_restart (atoi (argv[1]));
95
96 char nteststr[INT_BUFSIZE_BOUND (int)];
97
98 char *spargv[10];
99 {
100 int i = 0;
101 for (; i < argc - 1; i++)
102 spargv[i] = argv[i + 1];
103 spargv[i++] = (char *) "--direct";
104 spargv[i++] = (char *) "--restart";
105 spargv[i++] = nteststr;
106 spargv[i] = NULL;
107 }
108
109 for (int i = 0; i < array_length (tests); i++)
110 {
111 snprintf (nteststr, sizeof nteststr, "%d", i);
112
113 printf ("[%d] Spawned test for %s\n", i, tests[i].env);
114 setenv ("GLIBC_TUNABLES", tests[i].env, 1);
115 struct support_capture_subprocess result
116 = support_capture_subprogram (spargv[0], spargv);
117 support_capture_subprocess_check (&result, "tst-tunables-enable_secure",
118 0, sc_allow_stderr);
119 support_capture_subprocess_free (&result);
120 }
121
122 return 0;
123}
124
125#define TEST_FUNCTION_ARGV do_test
126#include <support/test-driver.c>