1 /* Basic tests for pldd program.
2    Copyright (C) 2019-2022 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 <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25 
26 #include <array_length.h>
27 #include <gnu/lib-names.h>
28 
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>
38 
39 static void
target_process(void * arg)40 target_process (void *arg)
41 {
42   pause ();
43 }
44 
45 static void
pldd_process(void * arg)46 pldd_process (void *arg)
47 {
48   pid_t *target_pid_ptr = (pid_t *) arg;
49 
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);
55 
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;
59 
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);
63 
64   char *prog = xasprintf ("%s/pldd", support_bindir_prefix);
65 
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 });
69 
70   FAIL_EXIT1 ("Returned from execve: errno=%d=%m\n", errno);
71 }
72 
73 /* The test runs in a container because pldd does not support tracing
74    a binary started by the loader iself (as with testrun.sh).  */
75 
76 static bool
in_str_list(const char * libname,const char * const strlist[])77 in_str_list (const char *libname, const char *const strlist[])
78 {
79   for (const char *const *str = strlist; *str != NULL; str++)
80     if (strcmp (libname, *str) == 0)
81       return true;
82   return false;
83 }
84 
85 static int
do_test(void)86 do_test (void)
87 {
88   support_need_proc ("needs /proc/sys/kernel/yama/ptrace_scope and /proc/$child");
89 
90   /* Check if our subprocess can be debugged with ptrace.  */
91   {
92     int ptrace_scope = support_ptrace_scope ();
93     if (ptrace_scope >= 2)
94       FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2");
95   }
96 
97   pid_t *target_pid_ptr = (pid_t *) xmmap (NULL, sizeof (pid_t),
98 					   PROT_READ | PROT_WRITE,
99 					   MAP_SHARED | MAP_ANONYMOUS, -1);
100 
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);
106 
107   /* Check 'pldd' output.  The test is expected to be linked against only
108      loader and libc.  */
109   {
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"
115 
116     FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
117     TEST_VERIFY (out != NULL);
118 
119     /* First line is in the form of <pid>: <full path of executable>  */
120     TEST_COMPARE (fscanf (out, "%u: " STRINPUT (BUFFERLEN), &pid, buffer), 2);
121 
122     TEST_COMPARE (pid, *target_pid_ptr);
123     TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
124 
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)
128       {
129 	/* Ignore vDSO.  */
130 	if (buffer[0] != '/')
131 	  continue;
132 
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';
137 
138 	const char *libname = basename (buffer);
139 
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 }))
144 	  {
145 	    TEST_COMPARE (interpreter_found, false);
146 	    interpreter_found = true;
147 	    continue;
148 	  }
149 
150 	if (in_str_list (libname,
151 			 (const char *const []) { "libc.so", LIBC_SO, NULL }))
152 	  {
153 	    TEST_COMPARE (libc_found, false);
154 	    libc_found = true;
155 	    continue;
156 	  }
157       }
158     TEST_COMPARE (interpreter_found, true);
159     TEST_COMPARE (libc_found, true);
160 
161     fclose (out);
162   }
163 
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));
168 
169   return 0;
170 }
171 
172 #include <support/test-driver.c>
173