1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* Basic tests for pldd program.
Copyright (C) 2019-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <array_length.h>
#include <gnu/lib-names.h>
#include <support/subprocess.h>
#include <support/capture_subprocess.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xptrace.h>
#include <support/xunistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <signal.h>
static void
target_process (void *arg)
{
pause ();
}
static void
pldd_process (void *arg)
{
pid_t *target_pid_ptr = (pid_t *) arg;
/* Create a copy of current test to check with pldd. As the
target_process is a child of this pldd_process, pldd is also able
to attach to target_process if YAMA is configured to 1 =
"restricted ptrace". */
struct support_subprocess target = support_subprocess (target_process, NULL);
/* Store the pid of target-process as do_test needs it in order to
e.g. terminate it at end of the test. */
*target_pid_ptr = target.pid;
/* Three digits per byte plus null terminator. */
char pid[3 * sizeof (uint32_t) + 1];
snprintf (pid, array_length (pid), "%d", target.pid);
char *prog = xasprintf ("%s/pldd", support_bindir_prefix);
/* Run pldd and use the pid of target_process as argument. */
execve (prog, (char *const []) { (char *) prog, pid, NULL },
(char *const []) { NULL });
FAIL_EXIT1 ("Returned from execve: errno=%d=%m\n", errno);
}
/* The test runs in a container because pldd does not support tracing
a binary started by the loader iself (as with testrun.sh). */
static bool
in_str_list (const char *libname, const char *const strlist[])
{
for (const char *const *str = strlist; *str != NULL; str++)
if (strcmp (libname, *str) == 0)
return true;
return false;
}
static int
do_test (void)
{
/* Check if our subprocess can be debugged with ptrace. */
{
int ptrace_scope = support_ptrace_scope ();
if (ptrace_scope >= 2)
FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2");
}
pid_t *target_pid_ptr = (pid_t *) xmmap (NULL, sizeof (pid_t),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1);
/* Run 'pldd' on test subprocess which will be created in pldd_process.
The pid of the subprocess will be written to target_pid_ptr. */
struct support_capture_subprocess pldd;
pldd = support_capture_subprocess (pldd_process, target_pid_ptr);
support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
/* Check 'pldd' output. The test is expected to be linked against only
loader and libc. */
{
pid_t pid;
char buffer[512];
#define STRINPUT(size) "%" # size "s"
FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
TEST_VERIFY (out != NULL);
/* First line is in the form of <pid>: <full path of executable> */
TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
TEST_COMPARE (pid, *target_pid_ptr);
TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
/* It expects only one loader and libc loaded by the program. */
bool interpreter_found = false, libc_found = false;
while (fgets (buffer, array_length (buffer), out) != NULL)
{
/* Ignore vDSO. */
if (buffer[0] != '/')
continue;
/* Remove newline so baseline (buffer) can compare against the
LD_SO and LIBC_SO macros unmodified. */
if (buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
const char *libname = basename (buffer);
/* It checks for default names in case of build configure with
--enable-hardcoded-path-in-tests (BZ #24506). */
if (in_str_list (libname,
(const char *const []) { "ld.so", LD_SO, NULL }))
{
TEST_COMPARE (interpreter_found, false);
interpreter_found = true;
continue;
}
if (in_str_list (libname,
(const char *const []) { "libc.so", LIBC_SO, NULL }))
{
TEST_COMPARE (libc_found, false);
libc_found = true;
continue;
}
}
TEST_COMPARE (interpreter_found, true);
TEST_COMPARE (libc_found, true);
fclose (out);
}
support_capture_subprocess_free (&pldd);
if (kill (*target_pid_ptr, SIGKILL) != 0)
FAIL_EXIT1 ("Unable to kill target_process: errno=%d=%m\n", errno);
xmunmap (target_pid_ptr, sizeof (pid_t));
return 0;
}
#include <support/test-driver.c>
|