blob: af2b39c7c31a3bc619ed5847249030dfc53f95d5 [file] [log] [blame]
Daniel Erat67829612019-03-15 15:29:29 -07001/*
2 * Copyright (c) 2012 The Chromium OS Authors.
3 *
4 * Based on:
5 * http://bazaar.launchpad.net/~ubuntu-bugcontrol/qa-regression-testing/master/view/head:/scripts/kernel-security/ptrace/thread-prctl.c
6 * Copyright 2011 Canonical, Ltd
7 * License: GPLv3
8 * Author: Kees Cook <kees.cook@canonical.com>
9 *
10 * Based on reproducer written by Philippe Waroquiers in:
11 * https://launchpad.net/bugs/729839
12 */
Daniel Erat67829612019-03-15 15:29:29 -070013#include <unistd.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18#include <signal.h>
19#include <string.h>
20#include <pthread.h>
21#include <sys/ptrace.h>
22#include <sys/prctl.h>
23#ifndef PR_SET_PTRACER
24#define PR_SET_PTRACER 0x59616d61
25#endif
26
27int tracee_method = 0;
28#define TRACEE_FORKS_FROM_TRACER 0
29#define TRACEE_CALLS_PRCTL_FROM_MAIN 1
30#define TRACEE_CALLS_PRCTL_FROM_THREAD 2
31
32/* Define some distinct exit values to aid failure debugging. */
33#define EXIT_FORK_TRACEE 1
34#define EXIT_FORK_TRACER 2
35#define EXIT_PIPE_COMMUNICATION 3
36#define EXIT_PIPE_NOTIFICATION 4
37#define EXIT_TRACEE_PIPE_READ 5
38#define EXIT_TRACEE_UNREACHABLE 6
39#define EXIT_TRACER_PIPE_READ 7
40#define EXIT_TRACER_PTRACE_ATTACH 8
41#define EXIT_TRACER_PTRACE_CONTINUE 9
42#define EXIT_TRACER_UNREACHABLE 10
Daniel Erat29f62db2019-03-24 09:49:32 -070043#define EXIT_TRACEE_PIPE_WRITE 11
44#define EXIT_TRACER_PIPE_WRITE 12
Daniel Erat67829612019-03-15 15:29:29 -070045
46int main_does_ptrace = 0;
47
48int ret;
49int pipes[2];
50int notification[2];
51pid_t tracer, tracee;
52
53static void* thr_fn(void* v) {
54 printf("tracee thread started\n");
55 if (tracee_method == TRACEE_CALLS_PRCTL_FROM_THREAD) {
56 ret = prctl(PR_SET_PTRACER, tracer, 0, 0, 0);
57 printf("tracee thread prtctl result: %d\n", ret);
58 }
59 printf("tracee thread finishing\n");
60 return NULL;
61}
62
63void start_tracee(void);
64
65void* tracer_main(void* data) {
66 long ptrace_result;
67 char buf[8];
68 int saw;
69
70 tracer = getpid();
71 printf("tracer %d waiting\n", tracer);
72
73 if (tracee_method == TRACEE_FORKS_FROM_TRACER) {
74 printf("forking tracee from tracer\n");
75 start_tracee();
76 }
77
78 close(pipes[1]);
79 close(notification[0]);
80 close(notification[1]);
81
82 saw = read(pipes[0], buf, 3);
83 if (saw < 3) {
84 perror("tracer pipe read");
85 exit(EXIT_TRACER_PIPE_READ);
86 }
87
88 printf("tracer to PTRACE_ATTACH my tracee %d\n", tracee);
89 ptrace_result = ptrace(PTRACE_ATTACH, tracee, NULL, NULL);
90 if (ptrace_result != 0) {
91 fflush(NULL);
92 perror("tracer ptrace attach has failed");
93 exit(EXIT_TRACER_PTRACE_ATTACH);
94 }
95 printf("tracer ptrace attach successful\n");
96
97 /* Wait for signal. */
98 printf("tracer waiting for tracee to SIGSTOP\n");
99 waitpid(tracee, NULL, 0);
100
101 printf("tracer to PTRACE_CONT tracee\n");
102 ptrace_result = ptrace(PTRACE_CONT, tracee, NULL, NULL);
103 if (ptrace_result != 0) {
104 fflush(NULL);
105 perror("tracer ptrace continue has failed");
106 exit(EXIT_TRACER_PTRACE_CONTINUE);
107 }
108 printf("tracer ptrace continue successful\n");
109
110 printf("tracer returning 0\n");
111 fflush(NULL);
112 exit(EXIT_SUCCESS);
113
114 return NULL;
115}
116
117/* Tracee knows nothing, needs tracee and tracer pid. */
118void tracee_main(void) {
119 char buf[1024];
120 int saw;
121 pthread_t thr;
Daniel Erat29f62db2019-03-24 09:49:32 -0700122 int num_written;
Daniel Erat67829612019-03-15 15:29:29 -0700123
124 tracee = getpid();
125 close(pipes[0]);
126
127 printf("tracee %d reading tracer pid\n", tracee);
128 close(notification[1]);
129 saw = read(notification[0], buf, 1024);
130 if (saw < 1) {
131 perror("pipe read");
132 exit(EXIT_TRACEE_PIPE_READ);
133 }
134 buf[saw] = '\0';
135 tracer = atoi(buf);
136
137 printf("tracee %d started (expecting %d as tracer)\n", tracee, tracer);
138
139 /* Handle setting PR_SET_PTRACER. */
140 switch (tracee_method) {
141 case TRACEE_CALLS_PRCTL_FROM_MAIN:
142 ret = prctl(PR_SET_PTRACER, tracer, 0, 0, 0);
143 printf("tracee main prtctl result: %d \n", ret);
144 break;
145 case TRACEE_CALLS_PRCTL_FROM_THREAD:
146 printf("tracee thread starting\n");
147 pthread_create(&thr, NULL, thr_fn, NULL);
148 pthread_join(thr, NULL);
149 printf("tracee thread finished\n");
150 break;
151 default:
152 break;
153 }
154
155 /* Wait for Oedipal action. */
156 printf("tracee triggering tracer\n");
157 fflush(NULL);
Daniel Erat29f62db2019-03-24 09:49:32 -0700158 num_written = write(pipes[1], "ok\n", 3);
159 if (num_written != 3) {
160 perror("tracee write to trigger tracer failed");
161 exit(EXIT_TRACEE_PIPE_WRITE);
162 }
Daniel Erat67829612019-03-15 15:29:29 -0700163
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900164 printf("tracee waiting for primary\n");
Daniel Erat67829612019-03-15 15:29:29 -0700165 saw = read(notification[0], buf, 1024);
166 buf[saw] = '\0';
167
168 printf("tracee finished (%s)\n", buf);
169 exit(EXIT_SUCCESS);
170}
171
172void start_tracee(void) {
173 fflush(NULL);
174 tracee = fork();
175 if (tracee < 0) {
176 perror("fork tracee");
177 exit(EXIT_FORK_TRACEE);
178 }
179 if (tracee == 0) {
180 tracee_main();
181 exit(EXIT_TRACEE_UNREACHABLE);
182 }
183}
184
185/* Tracer knows tracee, needs tracer pid. */
186int main(int argc, char* argv[]) {
187 int status;
188 char buf[1024];
Daniel Erat29f62db2019-03-24 09:49:32 -0700189 int num_written;
Daniel Erat67829612019-03-15 15:29:29 -0700190
191 if (argc > 1) {
192 /* Operational states:
193 * 0: tracer forks tracee.
194 * 1: tracee calls prctl from main process.
195 * 2: tracee calls prctl from non-leader thread.
196 */
197 tracee_method = atoi(argv[1]);
198 }
199 if (argc > 2) {
200 /* Operational states:
201 * 0: ptrace happens from non-leader thread.
202 * 1: ptrace happens from main process.
203 */
204 main_does_ptrace = atoi(argv[2]) != 0;
205 }
206
207 if (tracee_method != TRACEE_FORKS_FROM_TRACER) {
208 printf("will issue prctl from %s\n",
209 tracee_method == TRACEE_CALLS_PRCTL_FROM_MAIN ? "main" : "thread");
210 } else {
211 printf("will fork tracee from tracer\n");
212 }
213 printf("will issue ptrace from tracer %s\n",
214 main_does_ptrace ? "main" : "thread");
215
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900216 printf("primary is %d\n", getpid());
Daniel Erat67829612019-03-15 15:29:29 -0700217
218 if (pipe(notification) < 0) {
219 perror("pipe");
220 exit(EXIT_PIPE_NOTIFICATION);
221 }
222 if (pipe(pipes) < 0) {
223 perror("pipe");
224 exit(EXIT_PIPE_COMMUNICATION);
225 }
226
227 if (tracee_method != TRACEE_FORKS_FROM_TRACER) {
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900228 printf("forking tracee from primary\n");
Daniel Erat67829612019-03-15 15:29:29 -0700229 start_tracee();
230 }
231
232 fflush(NULL);
233 tracer = fork();
234 if (tracer < 0) {
235 perror("fork tracer");
236 exit(EXIT_FORK_TRACER);
237 }
238 if (tracer == 0) {
239 printf("tracer is %d\n", getpid());
240 if (main_does_ptrace) {
241 tracer_main(NULL);
242 } else {
243 pthread_t thread;
244 pthread_create(&thread, NULL, tracer_main, NULL);
245 pthread_join(thread, NULL);
246 }
247 exit(EXIT_TRACER_UNREACHABLE);
248 }
249
250 /* Leave the pipes for the tracee and tracer. */
251 close(pipes[0]);
252 close(pipes[1]);
253
254 /* Close our end of pid notification. */
255 close(notification[0]);
256 sprintf(buf, "%d", tracer);
Daniel Erat29f62db2019-03-24 09:49:32 -0700257 num_written = write(notification[1], buf, strlen(buf));
258 if (num_written != strlen(buf)) {
259 perror("tracer write to close pid notification failed");
260 exit(EXIT_TRACER_PIPE_WRITE);
261 }
Daniel Erat67829612019-03-15 15:29:29 -0700262
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900263 printf("primary waiting for tracer to finish\n");
Daniel Erat67829612019-03-15 15:29:29 -0700264 fflush(NULL);
265 waitpid(tracer, &status, 0);
266
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900267 printf("primary waiting for tracee to finish\n");
Daniel Erat67829612019-03-15 15:29:29 -0700268 fflush(NULL);
Daniel Erat29f62db2019-03-24 09:49:32 -0700269 num_written = write(notification[1], "stop", 4);
270 if (num_written != 4) {
271 perror("tracer write to stop tracee failed");
272 exit(EXIT_TRACER_PIPE_WRITE);
273 }
Daniel Erat67829612019-03-15 15:29:29 -0700274 kill(tracee, SIGCONT); // Just in case.
275 waitpid(tracee, NULL, 0);
276
277 status = WEXITSTATUS(status);
Laurent Chavey5ef5e862020-09-09 12:37:16 +0900278 printf("primary saw rc %d from tracer\n", status);
Daniel Erat67829612019-03-15 15:29:29 -0700279 return status;
280}
281