ptrace interface does not permit modification of syscall return
i have a ptrace program that watches for specific syscalls and when matched, will:
- on entry change the syscall nr to -1 (so the kernel will skip it)
- on exit change the return to -EPERM so the userspace sees a denial
i have this working on most arches (x86, x86_64, arm, alpha, ia64, etc...). on parisc, the kernel (using 3.18.7 currently) appears to be wrong. in my tests, if i don't mess with the syscall nr, i can change the return value fine (to EPERM or whatever). but the syscall executed which i do not want. if i change the syscall to -1, then i can't change the return value (so the child sees ENOSYS), but the kernel still executes the original syscall.
i have a simple test case attached to show the issue. the code does:
- spawn a child with the parent tracing it
- child will do:
- dupe stderr to another fd
- unlink a file named ".test.flag"
- write a message through the new fd
- close a magic # so the parent knows to start denying
- should see EPERM but it sees ENOSYS
- close the new fd
- should see EPERM but it is closed!
- write to the new fd
- should work, but the fd is closed
- call create on ".test.flag"
- should see EPERM, but the file is created!
- parent will do:
- log the syscalls until child runs close(-12345)
- the parent will then try to deny all close/creat calls
- uses PTRACE_POKEUSER w/PT_GR20 to set syscall to -1
- uses PTRACE_POKEUSER w/PT_GR28 to set return to -EPERM
you can run the test case by doing:
$ gcc test.c && ./a.out -mike
#define _GNU_SOURCE
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <asm/offset.h>
#include <asm/ptrace.h>
static pid_t trace_pid;
static long _do_ptrace(enum __ptrace_request request, const char *srequest, void *addr, void *data)
{
long ret;
try_again:
errno = 0;
ret = ptrace(request, trace_pid, addr, data);
if (ret == -1) {
/* Child hasn't gotten to the next marker yet ? */
if (errno == ESRCH) {
int status;
if (waitpid(trace_pid, &status, 0) == -1) {
/* nah, it's dead ... should we whine though ? */
_exit(0);
}
sched_yield();
goto try_again;
} else if (errno == EIO || errno == EFAULT) {
/* This comes up when the child itself tries to use a bad pointer.
* That's not something the sandbox should abort on. #560396
*/
return ret;
} else if (!errno)
if (request == PTRACE_PEEKDATA ||
request == PTRACE_PEEKTEXT ||
request == PTRACE_PEEKUSER)
return ret;
err(1, "do_ptrace: ptrace(%s, ..., %p, %p)", srequest, addr, data);
}
return ret;
}#define do_ptrace(request, addr, data) _do_ptrace(request, #request, addr, data)
static long do_peekuser(long offset)
{
return do_ptrace(PTRACE_PEEKUSER, (void *)offset, NULL);
}static long do_pokeuser(long offset, long val)
{
return do_ptrace(PTRACE_POKEUSER, (void *)offset, (void *)val);
}static void trace_child_signal(int signo, siginfo_t *info, void *context) {
#if 0
warnx("got sig %s(%i): code:%s(%i) status:%s(%i)",
strsignal(signo), signo,
"---", info->si_code,
strsignal(info->si_status), info->si_status);#endif
switch (info->si_code) {
case CLD_DUMPED:
case CLD_KILLED:
_exit(128 + info->si_status);
case CLD_EXITED:
_exit(info->si_status);
case CLD_TRAPPED:
switch (info->si_status) {
case SIGSTOP:
kill(trace_pid, SIGCONT);
case SIGTRAP:
case SIGCONT:
return;
}
/* For whatever signal the child caught, let's ignore it and
* continue on. If it aborted, segfaulted, whatever, that's
* its problem, not ours, so don't whine about it. We just
* have to be sure to bubble it back up. #265072
*/
do_ptrace(PTRACE_CONT, NULL, (void *)(long)info->si_status);
return;
}
errx(1, "unhandled signal case");
}static const char *lookup_syscall(long nr)
{
switch (nr) {
#define X(n) case SYS_##n: return #n;
X(access)
X(brk)
X(close)
X(creat)
X(dup)
X(fstat64)
X(mmap)
X(mprotect)
X(munmap)
X(open)
X(read)
X(uname)
X(write)
#undef X
}
return "";
}void child_main(void)
{
char test_file[] = ".test.flag";
char msg[] = "child: you should see two of these\n";
int fd = dup(2);
unlink(test_file);
write(fd, msg, sizeof(msg));
/* Marker for the parent to watch. */
errno = 0;
close(-12345);
fprintf(stderr, "child: close marker (should be EPERM): %m\n");
errno = 0;
close(fd);
fprintf(stderr, "child: real close (should be EPERM): %m\n");
errno = 0;
write(fd, msg, sizeof(msg));
fprintf(stderr, "child: write (should be success): %m\n");
errno = 0;
creat(test_file, 0660);
fprintf(stderr, "child: creat (should be EPERM): %m\n");
errno = 0;
access(test_file, F_OK);
fprintf(stderr, "child: access (should be ENOENT): %m\n");
unlink(test_file);
exit(0);
}static void parent_main(void)
{
int status;
long nr, arg1;
/* Wait for the child to exec. */
while (1) {
do_ptrace(PTRACE_SYSCALL, NULL, NULL);
waitpid(trace_pid, &status, 0);
unsigned event = ((unsigned)status >> 16);
if (event == PTRACE_EVENT_EXEC) {
warnx("parent: hit exec!");
break;
} else
warnx("parent: waiting for exec; status: %#x", status);
}
/* Main loop. */
bool saw_close = false;
bool before_syscall = false;
bool fake_syscall_ret = false;
while (1) {
do_ptrace(PTRACE_SYSCALL, NULL, NULL);
waitpid(trace_pid, &status, 0);
nr = do_peekuser(PT_GR20);
arg1 = do_peekuser(PT_GR26);
if (before_syscall) {
warnx("parent: NR:%3li %s", nr, lookup_syscall(nr));
/* Once the child hits the marker, deny all close & creat calls */
if (nr == __NR_close || nr == __NR_creat) {
if (saw_close || arg1 == -12345) {
saw_close = true;
warnx("parent: setting NR to -1");
do_pokeuser(PT_GR20, -1);
fake_syscall_ret = true;
}
}
} else if (fake_syscall_ret) {
warnx("parent: forcing EPERM");
do_pokeuser(PT_GR28, -EPERM);
fake_syscall_ret = false;
}
before_syscall = !before_syscall;
}
}int main(int argc, char *argv[])
{
struct sigaction sa, old_sa;
/* Child will re-exec us so the ptrace is clean for the parent. */
if (argc > 1)
child_main();
/* Set up signal handler to watch for child events. */
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sa.sa_sigaction = trace_child_signal;
sigaction(SIGCHLD, &sa, &old_sa);
/* Fork a child and have the parent do some early ptrace init. */
trace_pid = fork();
if (trace_pid == -1) {
err(1, "fork() failed");
} else if (trace_pid) {
warn("parent waiting for child (pid=%i) to signal", trace_pid);
waitpid(trace_pid, NULL, 0);
do_ptrace(PTRACE_SETOPTIONS, NULL,
(void *)(PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC));
parent_main();
errx(1, "child should have quit, as should we");
}
/* Have the child set itself up for tracing before execing again. */
warnx("child setting up ...");
sigaction(SIGCHLD, &old_sa, NULL);
do_ptrace(PTRACE_TRACEME, NULL, NULL);
kill(getpid(), SIGSTOP);
execl(argv[0], argv[0], "--child", NULL);
return 0;
}Helge Deller
maintainerHi Mike,
Quoted 32 lines · Mike Frysinger
i have a ptrace program that watches for specific syscalls and when matched, will:
- on entry change the syscall nr to -1 (so the kernel will skip it)
- on exit change the return to-EPERMso the userspace sees a deniali have this working on most arches (x86,
x86_64, arm, alpha, ia64, etc...). on parisc, the kernel (using 3.18.7 currently) appears to be wrong. in my tests, if i don't mess with the syscall nr, i can change the return value fine (to EPERM or whatever). but the syscall executed which i do not want. if i change the syscall to -1, then i can't change the return value (so the child sees ENOSYS), but the kernel still executes the original syscall.i have a simple test case attached to show the issue. the code does:
- spawn a child with the parent tracing it
- child will do:
- dupe stderr to another fd
- unlink a file named ".test.flag"
- write a message through the new fd
- close a magic # so the parent knows to start denying
- should see EPERM but it sees ENOSYS
- close the new fd
- should see EPERM but it is closed!
- write to the new fd
- should work, but the fd is closed
- call create on ".test.flag"
- should see EPERM, but the file is created!
- parent will do:
- log the syscalls until child runs close(-12345)
- the parent will then try to deny all close/creat calls
- usesPTRACE_POKEUSERw/PT_GR20 to set syscall to -1
- usesPTRACE_POKEUSERw/PT_GR28 to set return to-EPERM
Quoted 2 lines
you can run the test case by doing:
$ gcc test.c && ./a.out
I agree, something is fishy :-)
I did some tests with your testcase.
First problem I had was, that compiling failed since it didn't found the asm/offset.h header file. Which one did you used? I know it usually should come with the kernel headers, but there it is asm-offsets.h. If you used debian, which package did you installed?
Instead I used asm-offsets.h.
First problem: I had to install the 64bit header file. PT_GR20 in this one was much higher than it should be for 32bit userspace.
So, I used those defines (taken from the strace source package):
#define PT_GR20 (20*4)
#define PT_GR26 (26*4)
#define PT_GR28 (28*4)
#define PT_IAOQ0 (106*4)
#define PT_IAOQ1 (107*4)
With that I got those output:
root@c3000:~# ./a.out a.out: parent waiting for child (pid=1344) to signal: Success a.out: child setting up ... a.out: parent: waiting for exec; status: 0x1a7f a.out: parent: waiting for exec; status: 0x857f a.out: parent: hit exec! a.out: parent: NR: 45 brk a.out: parent: NR: 59 uname a.out: parent: NR: 33 access a.out: parent: NR: 90 mmap a.out: parent: NR: 33 access a.out: parent: NR: 5 open a.out: parent: NR:112 fstat64 a.out: parent: NR: 90 mmap a.out: parent: NR: 6 close a.out: parent: NR: 33 access a.out: parent: NR: 5 open a.out: parent: NR: 3 read a.out: parent: NR:112 fstat64 a.out: parent: NR: 90 mmap a.out: parent: NR: 90 mmap a.out: parent: NR: 90 mmap a.out: parent: NR: 6 close a.out: parent: NR: 90 mmap a.out: parent: NR:125 mprotect a.out: parent: NR: 90 mmap a.out: parent: NR:125 mprotect a.out: parent: NR: 91 munmap a.out: parent: NR: 41 dup a.out: parent: NR: 10 a.out: parent: NR: 4 write child: you should see two of these a.out: parent: NR: 6 close a.out: parent: setting NR to -1 a.out: parent: forcing EPERM child: close marker (should be EPERM): Function not implemented a.out: parent: NR: 4 write a.out: parent: NR: 6 close a.out: parent: setting NR to -1 a.out: parent: forcing EPERM child: real close (should be EPERM): Success a.out: parent: NR: 4 write a.out: parent: NR: 4 write child: write (should be success): Bad file descriptor a.out: parent: NR: 4 write a.out: parent: NR: 8 creat a.out: parent: setting NR to -1 a.out: parent: forcing EPERM child: creat (should be EPERM): Success a.out: parent: NR: 4 write a.out: parent: NR: 33 access child: access (should be ENOENT): Success a.out: parent: NR: 4 write a.out: parent: NR: 10
Seems not like what it should be.
I need to look closer at it during the next few days.
Regarding on how to get correct 32bit PT_REG #defines/values even on 64bit kernel, maybe the attached patch is a way to go.
Helge
arch/parisc/include/uapi/asm/ptrace.h +20 -0
@@ -33,6 +33,26 @@ struct pt_regs {unsigned long ipsw;/* CR22 */ }; +#if defined(__LP64__) +struct compat_pt_regs { +unsigned int gr[32 ];/* PSW is in gr[0] */ +__u64 fr[32 ]; +unsigned int sr[8 ]; +unsigned int iasq[2 ]; +unsigned int iaoq[2 ]; +unsigned int cr27; +unsigned int pad0;/* available for other uses */ +unsigned int orig_r28; +unsigned int ksp; +unsigned int kpc; +unsigned int sar;/* CR11 */ +unsigned int iir;/* CR19 */ +unsigned int isr;/* CR20 */ +unsigned int ior;/* CR21 */ +unsigned int ipsw;/* CR22 */ +}; +#endif +/* * The numbers chosen here are somewhat arbitrary but absolutely MUST * not overlap with any of the number assigned in <linux/ptrace.h>.
arch/parisc/kernel/asm-offsets.c +90 -0
96 lines not on this page · raw message
Quoted 33 lines · Helge Deller
Quoted 32 lines · Mike Frysinger
i have a ptrace program that watches for specific syscalls and when matched, will:
- on entry change the syscall nr to -1 (so the kernel will skip it)
- on exit change the return to-EPERMso the userspace sees a deniali have this working on most arches (x86,
x86_64, arm, alpha, ia64, etc...). on parisc, the kernel (using 3.18.7 currently) appears to be wrong. in my tests, if i don't mess with the syscall nr, i can change the return value fine (to EPERM or whatever). but the syscall executed which i do not want. if i change the syscall to -1, then i can't change the return value (so the child sees ENOSYS), but the kernel still executes the original syscall.i have a simple test case attached to show the issue. the code does:
- spawn a child with the parent tracing it
- child will do:
- dupe stderr to another fd
- unlink a file named ".test.flag"
- write a message through the new fd
- close a magic # so the parent knows to start denying
- should see EPERM but it sees ENOSYS
- close the new fd
- should see EPERM but it is closed!
- write to the new fd
- should work, but the fd is closed
- call create on ".test.flag"
- should see EPERM, but the file is created!
- parent will do:
- log the syscalls until child runs close(-12345)
- the parent will then try to deny all close/creat calls
- usesPTRACE_POKEUSERw/PT_GR20 to set syscall to -1
- usesPTRACE_POKEUSERw/PT_GR28 to set return to-EPERM
Quoted 8 lines
Quoted 2 lines
you can run the test case by doing:
$ gcc test.c && ./a.outI agree, something is fishy :-)
I did some tests with your testcase.
First problem I had was, that compiling failed since it didn't found theasm/offset.hheader file. Which one did you used? I know it usually should come with the kernel headers, but there it isasm-offsets.h.
hmm, looks like it got installed by hand at some point (Jul 2004 datestamp!) and never cleaned up.
Quoted 8 lines
First problem: I had to install the 64bit header file.
PT_GR20in this one was much higher than it should be for 32bit userspace.So, I used those defines (taken from the strace source package):
#definePT_GR20(20*4)
#definePT_GR26(26*4)
#definePT_GR28(28*4)
#definePT_IAOQ0(106*4)
#definePT_IAOQ1(107*4)
these are the values in my local asm/offset.h, and what i was using in my original code -- the register # multiplied by 4.
Quoted 1 line
With that I got those output:
looks like you're seeing the same as me. i'm only testing 32bit user and 32bit kernel currently as we don't have a 64bit userspace :). -mike
Helge Deller
maintainer-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Quoted 55 lines · Mike Frysinger
Quoted 33 lines · Helge Deller
Quoted 32 lines · Mike Frysinger
i have a ptrace program that watches for specific syscalls and when matched, will:
- on entry change the syscall nr to -1 (so the kernel will skip it)
- on exit change the return to-EPERMso the userspace sees a deniali have this working on most arches (x86,
x86_64, arm, alpha, ia64, etc...). on parisc, the kernel (using 3.18.7 currently) appears to be wrong. in my tests, if i don't mess with the syscall nr, i can change the return value fine (to EPERM or whatever). but the syscall executed which i do not want. if i change the syscall to -1, then i can't change the return value (so the child sees ENOSYS), but the kernel still executes the original syscall.i have a simple test case attached to show the issue. the code does:
- spawn a child with the parent tracing it
- child will do:
- dupe stderr to another fd
- unlink a file named ".test.flag"
- write a message through the new fd
- close a magic # so the parent knows to start denying
- should see EPERM but it sees ENOSYS
- close the new fd
- should see EPERM but it is closed!
- write to the new fd
- should work, but the fd is closed
- call create on ".test.flag"
- should see EPERM, but the file is created!
- parent will do:
- log the syscalls until child runs close(-12345)
- the parent will then try to deny all close/creat calls
- usesPTRACE_POKEUSERw/PT_GR20 to set syscall to -1
- usesPTRACE_POKEUSERw/PT_GR28 to set return to-EPERMQuoted 8 lines
Quoted 2 lines
you can run the test case by doing:
$ gcc test.c && ./a.outI agree, something is fishy :-)
I did some tests with your testcase.
First problem I had was, that compiling failed since it didn't found theasm/offset.hheader file. Which one did you used? I know it usually should come with the kernel headers, but there it isasm-offsets.h.hmm, looks like it got installed by hand at some point (Jul 2004 datestamp!) and never cleaned up.
Quoted 8 lines
First problem: I had to install the 64bit header file.
PT_GR20in this one was much higher than it should be for 32bit userspace.So, I used those defines (taken from the strace source package):
#definePT_GR20(20*4)
#definePT_GR26(26*4)
#definePT_GR28(28*4)
#definePT_IAOQ0(106*4)
#definePT_IAOQ1(107*4)
Quoted 7 lines
these are the values in my local
asm/offset.h, and what i was using in my original code -- the register # multiplied by 4.Quoted 1 line
With that I got those output:
looks like you're seeing the same as me. i'm only testing 32bit user and 32bit kernel currently as we don't have a 64bit userspace :).
Mike, can you please test the patch I just posted:
https://patchwork.kernel.org/patch/8063301/
Helge
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
iQIcBAEBCAAGBQJWnlIGAAoJEKGDlV8wpRJB01AP/1e5cO5DfF2UtDGAC8SNqB1x 4uCIEyCrKyAkNbrfy3HSQ2OVdenuZnxq/z5LiQeY9k8j4IfZgpU8v7fADZz76L1z CsVyIAwi7BQqUUeV6otWo5o1mAZ1F9dvS4jPFpxROotgmBCkhDcAYP6xGpoKbAET VDhgwjgsZLXOuONoxfONaveZGRMjZGbm5nrvra30kfQhzilew5EmWGchWvv5QFAE scukm0Prfi9roefagIllvZrLjGLenQ7Wa/opWrH/KxbkN3t8cQprXUv8ejEEUG8v mCrUQRfHdbgVpzMFyCbdtoRVBRRtRl3Z9Ht8DqgYiuaW34iDA6Una/ntBC/Z0SdD WySBINwuG6VL8gLrS7zMuKyBrhY37PV+eeo+GC0C6bNr37oPgJ1HLqOj8B2tlHU+ /EJYaLSDMXHsrukF+XSOzq9pbiCpPvU3ApzpZnPcypNMid3/6LKf6fIvu/4P/8xK 09gCZrHRXI5J65Qya6oQHP2N1YJYRJzHUct1N6hEJ01DEDfUZxkHun8EDYHC879f RZ7UE2jNJlBAMXTel0HGthgXrqPyM18ePlA4yv5YVK1Z7Ai+wT7j+CL5hnI46x9X 71PugI5vMzZZQ+N9alW9KG2E/RAzcx1Mu/SjntoXBD7jdv1u+DiQtbhD/ahP61Pt +4I+Icqo62/Ql3Y2r8Ia
=2LNU
-----END PGP SIGNATURE-----