-
Notifications
You must be signed in to change notification settings - Fork 1
/
psendfd.c
394 lines (355 loc) · 9.8 KB
/
psendfd.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
#define SPECIALSOURCES \
SPECIALSOURCE(close) \
#define SPECIALTARGETS \
SPECIALTARGET(any) \
SPECIALTARGET(cwd) \
enum {
SPECIALSOURCES_zero,
#define SPECIALSOURCE(x) SPECIALSOURCE_##x,
SPECIALSOURCES
#undef SPECIALSOURCE
SPECIALSOURCES_end,
};
enum {
SPECIALTARGETS_zero,
#define SPECIALTARGET(x) SPECIALTARGET_##x,
SPECIALTARGETS
#undef SPECIALTARGET
SPECIALTARGETS_end,
};
static void
usage(void)
{
static char const msg[] =
"Usage: psendfd [-ef] [-m mintargetfd] [-P sourcepid] pid fd "
"targetfd [cmd]...\n";
if (fputs(msg, stderr) == EOF)
perror("fputs");
}
static int
str2int(char const *const str)
{
char *endptr;
errno = 0;
long const num = strtol(str, &endptr, 10);
if (errno) {
perror("strtol");
return INT_MIN;
}
if (endptr == str || num < INT_MIN || num > INT_MAX || *endptr)
return INT_MIN;
return (int)num;
}
static void
tracee_perror(char const *const msg, int const err)
{
if (fprintf(stderr, "tracee: %s: %s\n", msg, strerror(err)) == EOF)
perror("fprintf");
}
static bool
nextstop(pid_t const pid, int *const status)
{
while (waitpid(pid, status, 0) == -1) {
if (errno != EINTR) {
perror("waitpid");
return false;
}
}
if (!WIFSTOPPED(*status)) {
if (fputs("Unexpected wait status.\n", stderr) == EOF)
perror("fputs");
return false;
}
return true;
}
static bool
do_syscall(pid_t const pid, struct user_regs_struct *const regs)
{
if (ptrace(PTRACE_SETREGS, pid, 0, regs) == -1) {
perror("ptrace(PTRACE_SETREGS)");
return false;
}
do {
int status;
do {
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) == -1) {
perror("ptrace(PTRACE_SYSCALL)");
return false;
}
if (!nextstop(pid, &status))
return false;
} while (WSTOPSIG(status) != SIGTRAP);
errno = 0;
regs->rax = ptrace(PTRACE_PEEKUSER, pid, 8 * RAX, 0);
if (errno) {
perror("ptrace(PTRACE_PEEKUSER)");
return false;
}
} while ((long)regs->rax == -ENOSYS);
return true;
}
static int
do_close(pid_t const pid, int const fd, bool const fflag,
struct user_regs_struct const *const savedregs)
{
struct user_regs_struct regs;
do {
regs = *savedregs;
regs.rax = SYS_close;
regs.rdi = fd;
if (!do_syscall(pid, ®s))
return 2;
} while ((long)regs.rax == -EINTR);
if (regs.rax == 0 || (fflag && (long)regs.rax == -EBADF))
return 0;
tracee_perror("close", -regs.rax);
return 2;
}
static int
do_send(pid_t const pid, int const fd, int *const targetfdp,
pid_t const sourcepid,
struct user_regs_struct const *const savedregs, int const fdmin)
{
int ret = 0;
int const targetfd = *targetfdp;
struct user_regs_struct regs = *savedregs;
regs.rax = SYS_pidfd_open;
regs.rdi = sourcepid >= 0 ? sourcepid : getpid();
regs.rsi = 0;
if (!do_syscall(pid, ®s))
return 2;
if ((long)regs.rax < 0) {
tracee_perror("pidfd_open", -regs.rax);
return 2;
}
int const pidfd = regs.rax;
regs = *savedregs;
regs.rax = SYS_pidfd_getfd;
regs.rdi = pidfd;
regs.rsi = fd;
regs.rdx = 0;
if (!do_syscall(pid, ®s))
return 2;
if ((long)regs.rax < 0) {
tracee_perror("pidfd_getfd", -regs.rax);
return 2;
}
int thefd = regs.rax;
if (targetfd < 0) {
if (fdmin > thefd) {
int const theoldfd = thefd;
regs = *savedregs;
regs.rax = SYS_fcntl;
regs.rdi = thefd;
regs.rsi = F_DUPFD;
regs.rdx = fdmin;
if (!do_syscall(pid, ®s))
return 2;
if ((long)regs.rax < 0) {
ret = 2;
tracee_perror("fcntl(F_DUPFD)", -regs.rax);
} else {
thefd = regs.rax;
}
if (do_close(pid, theoldfd, false, savedregs))
ret = 2;
}
} else if (thefd != targetfd) {
do {
regs = *savedregs;
regs.rax = SYS_dup2;
regs.rdi = thefd;
regs.rsi = targetfd;
if (!do_syscall(pid, ®s))
return 2;
} while ((long)regs.rax == -EINTR);
if ((long)regs.rax < 0) {
tracee_perror("dup2", -regs.rax);
ret = 2;
}
if (do_close(pid, thefd, false, savedregs))
ret = 2;
}
if (ret || pidfd != targetfd) {
if (do_close(pid, pidfd, false, savedregs))
ret = 2;
}
if (!ret)
*targetfdp = thefd;
return ret;
}
static int
do_fchdir(pid_t const pid, int const fd,
struct user_regs_struct const *const savedregs)
{
int ret = 0;
int targetfd = -SPECIALTARGET_any;
if (do_send(pid, fd, &targetfd, -1, savedregs, -1))
return 2;
struct user_regs_struct regs;
do {
regs = *savedregs;
regs.rax = SYS_fchdir;
regs.rdi = targetfd;
if (!do_syscall(pid, ®s))
return 2;
} while ((long)regs.rax == -EINTR);
if ((long)regs.rax < 0) {
tracee_perror("fchdir", -regs.rax);
ret = 2;
}
if (do_close(pid, targetfd, false, savedregs))
ret = 2;
return ret;
}
int
main(int const argc, char *const *const argv)
{
pid_t sourcepid = -1;
bool eflag = false;
bool fflag = false;
int fdmin = -1;
for (int opt; opt = getopt(argc, argv, "+efm:P:"), opt != -1;) {
switch (opt) {
case 'e':
eflag = true;
break;
case 'f':
fflag = true;
break;
case 'm':
fdmin = str2int(optarg);
if (fdmin <= -2) {
static char const emsg[] =
"Invalid targetfd lower limit.\n";
if (fputs(emsg, stderr) == EOF)
perror("fputs");
return 2;
}
break;
case 'P':
sourcepid = str2int(optarg);
if (sourcepid <= -2) {
if (fputs("Invalid source pid.\n", stderr) == EOF)
perror("fputs");
return 2;
}
break;
default:
usage();
return 2;
}
}
if (argc - optind < 3) {
usage();
return 2;
}
int const intpid = str2int(argv[optind]);
if (intpid < 0) {
if (fputs("Invalid pid.\n", stderr) == EOF)
perror("fputs");
return 2;
}
pid_t const pid = (pid_t)intpid;
char const *const fdstr = argv[optind + 1];
int const fd =
#define SPECIALSOURCE(x) !strcmp(fdstr, #x) ? -SPECIALSOURCE_##x :
SPECIALSOURCES
#undef SPECIALSOURCE
str2int(fdstr);
if (fd <= -SPECIALSOURCES_end) {
if (fputs("Invalid fd.\n", stderr) == EOF)
perror("fputs");
return 2;
}
char const *const tfdstr = argv[optind + 2];
int targetfd =
#define SPECIALTARGET(x) !strcmp(tfdstr, #x) ? -SPECIALTARGET_##x :
SPECIALTARGETS
#undef SPECIALTARGET
str2int(tfdstr);
if (targetfd <= -SPECIALTARGETS_end || (fd < 0 && targetfd < 0)) {
if (fputs("Invalid targetfd.\n", stderr) == EOF)
perror("fputs");
return 2;
}
if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
perror("ptrace(PTRACE_ATTACH)");
return 2;
}
for (int status; !nextstop(pid, &status);)
return 2;
struct user_regs_struct savedregs;
if (ptrace(PTRACE_GETREGS, pid, 0, &savedregs) == -1) {
perror("ptrace(PTRACE_GETREGS)");
return 2;
}
long const word = ptrace(PTRACE_PEEKTEXT, pid, savedregs.rip, 0);
if (word == -1) {
perror("ptrace(PTRACE_PEEKTEXT)");
return 2;
}
long const pokeret = ptrace(PTRACE_POKETEXT, pid, savedregs.rip,
/* syscall */ 0x050f);
if (pokeret == -1) {
perror("ptrace(PTRACE_POKETEXT)");
return 2;
}
int const ret =
fd == -SPECIALSOURCE_close ?
do_close(pid, targetfd, fflag, &savedregs) :
targetfd == -SPECIALTARGET_cwd ?
do_fchdir(pid, fd, &savedregs) :
do_send(pid, fd, &targetfd, sourcepid, &savedregs, fdmin);
if (ptrace(PTRACE_POKETEXT, pid, savedregs.rip, word) == -1) {
perror("ptrace(PTRACE_POKETEXT)");
return 2;
}
if (ptrace(PTRACE_SETREGS, pid, 0, &savedregs) == -1) {
perror("ptrace(PTRACE_SETREGS)");
return 2;
}
if (ret || argc - 3 <= optind)
return ret;
if (fd > 0 && eflag) {
char buf[10 + 1];
int const sz = snprintf(buf, sizeof buf, "%d", targetfd);
if (sz < 0) {
perror("snprintf");
return 2;
}
if ((size_t)sz >= sizeof buf) {
static char const efmt[] =
"snprintf: the buffer is %zd byte%s too small.\n";
ptrdiff_t const diff = ret - sizeof buf;
if (fprintf(stderr, efmt, diff, &"s"[diff == 1]) == EOF)
perror("fprintf");
return 2;
}
if (setenv("PSENDFD_FD", buf, 1) == -1) {
perror("setenv");
return 2;
}
}
if (ptrace(PTRACE_DETACH, pid, 0, 0) == -1) {
perror("ptrace(PTRACE_DETACH)");
return 2;
}
(void)execvp(argv[optind + 3], &argv[optind + 3]);
perror("execvp");
return 2;
}