elf: Ignore loader debug env vars for setuid
[glibc.git] / elf / tst-pldd.c
bloba6ecaabf578b4bdd4110d7f94181531f0a642f40
1 /* Basic tests for pldd program.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdbool.h>
26 #include <array_length.h>
27 #include <gnu/lib-names.h>
29 #include <support/subprocess.h>
30 #include <support/capture_subprocess.h>
31 #include <support/check.h>
32 #include <support/support.h>
33 #include <support/xptrace.h>
34 #include <support/xunistd.h>
35 #include <sys/mman.h>
36 #include <errno.h>
37 #include <signal.h>
39 static void
40 target_process (void *arg)
42 pause ();
45 static void
46 pldd_process (void *arg)
48 pid_t *target_pid_ptr = (pid_t *) arg;
50 /* Create a copy of current test to check with pldd. As the
51 target_process is a child of this pldd_process, pldd is also able
52 to attach to target_process if YAMA is configured to 1 =
53 "restricted ptrace". */
54 struct support_subprocess target = support_subprocess (target_process, NULL);
56 /* Store the pid of target-process as do_test needs it in order to
57 e.g. terminate it at end of the test. */
58 *target_pid_ptr = target.pid;
60 /* Three digits per byte plus null terminator. */
61 char pid[3 * sizeof (uint32_t) + 1];
62 snprintf (pid, array_length (pid), "%d", target.pid);
64 char *prog = xasprintf ("%s/pldd", support_bindir_prefix);
66 /* Run pldd and use the pid of target_process as argument. */
67 execve (prog, (char *const []) { (char *) prog, pid, NULL },
68 (char *const []) { NULL });
70 FAIL_EXIT1 ("Returned from execve: errno=%d=%m\n", errno);
73 /* The test runs in a container because pldd does not support tracing
74 a binary started by the loader itself (as with testrun.sh). */
76 static bool
77 in_str_list (const char *libname, const char *const strlist[])
79 for (const char *const *str = strlist; *str != NULL; str++)
80 if (strcmp (libname, *str) == 0)
81 return true;
82 return false;
85 static int
86 do_test (void)
88 support_need_proc ("needs /proc/sys/kernel/yama/ptrace_scope and /proc/$child");
90 /* Check if our subprocess can be debugged with ptrace. */
92 int ptrace_scope = support_ptrace_scope ();
93 if (ptrace_scope >= 2)
94 FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2");
97 pid_t *target_pid_ptr = (pid_t *) xmmap (NULL, sizeof (pid_t),
98 PROT_READ | PROT_WRITE,
99 MAP_SHARED | MAP_ANONYMOUS, -1);
101 /* Run 'pldd' on test subprocess which will be created in pldd_process.
102 The pid of the subprocess will be written to target_pid_ptr. */
103 struct support_capture_subprocess pldd;
104 pldd = support_capture_subprocess (pldd_process, target_pid_ptr);
105 support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
107 /* Check 'pldd' output. The test is expected to be linked against only
108 loader and libc. */
110 pid_t pid;
111 #define BUFFERLEN 511
112 char buffer[BUFFERLEN + 1];
113 #define STRINPUT(size) XSTRINPUT(size)
114 #define XSTRINPUT(size) "%" # size "s"
116 FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
117 TEST_VERIFY (out != NULL);
119 /* First line is in the form of <pid>: <full path of executable> */
120 TEST_COMPARE (fscanf (out, "%u: " STRINPUT (BUFFERLEN), &pid, buffer), 2);
122 TEST_COMPARE (pid, *target_pid_ptr);
123 TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
125 /* It expects only one loader and libc loaded by the program. */
126 bool interpreter_found = false, libc_found = false;
127 while (fgets (buffer, array_length (buffer), out) != NULL)
129 /* Ignore vDSO. */
130 if (buffer[0] != '/')
131 continue;
133 /* Remove newline so baseline (buffer) can compare against the
134 LD_SO and LIBC_SO macros unmodified. */
135 if (buffer[strlen(buffer)-1] == '\n')
136 buffer[strlen(buffer)-1] = '\0';
138 const char *libname = basename (buffer);
140 /* It checks for default names in case of build configure with
141 --enable-hardcoded-path-in-tests (BZ #24506). */
142 if (in_str_list (libname,
143 (const char *const []) { "ld.so", LD_SO, NULL }))
145 TEST_COMPARE (interpreter_found, false);
146 interpreter_found = true;
147 continue;
150 if (in_str_list (libname,
151 (const char *const []) { "libc.so", LIBC_SO, NULL }))
153 TEST_COMPARE (libc_found, false);
154 libc_found = true;
155 continue;
158 TEST_COMPARE (interpreter_found, true);
159 TEST_COMPARE (libc_found, true);
161 fclose (out);
164 support_capture_subprocess_free (&pldd);
165 if (kill (*target_pid_ptr, SIGKILL) != 0)
166 FAIL_EXIT1 ("Unable to kill target_process: errno=%d=%m\n", errno);
167 xmunmap (target_pid_ptr, sizeof (pid_t));
169 return 0;
172 #include <support/test-driver.c>