Skip to content

Commit 201766a

Browse files
parport0torvalds
authored andcommitted
ptrace: add PTRACE_GET_SYSCALL_INFO request
PTRACE_GET_SYSCALL_INFO is a generic ptrace API that lets ptracer obtain details of the syscall the tracee is blocked in. There are two reasons for a special syscall-related ptrace request. Firstly, with the current ptrace API there are cases when ptracer cannot retrieve necessary information about syscalls. Some examples include: * The notorious int-0x80-from-64-bit-task issue. See [1] for details. In short, if a 64-bit task performs a syscall through int 0x80, its tracer has no reliable means to find out that the syscall was, in fact, a compat syscall, and misidentifies it. * Syscall-enter-stop and syscall-exit-stop look the same for the tracer. Common practice is to keep track of the sequence of ptrace-stops in order not to mix the two syscall-stops up. But it is not as simple as it looks; for example, strace had a (just recently fixed) long-standing bug where attaching strace to a tracee that is performing the execve system call led to the tracer identifying the following syscall-exit-stop as syscall-enter-stop, which messed up all the state tracking. * Since the introduction of commit 84d77d3 ("ptrace: Don't allow accessing an undumpable mm"), both PTRACE_PEEKDATA and process_vm_readv become unavailable when the process dumpable flag is cleared. On such architectures as ia64 this results in all syscall arguments being unavailable for the tracer. Secondly, ptracers also have to support a lot of arch-specific code for obtaining information about the tracee. For some architectures, this requires a ptrace(PTRACE_PEEKUSER, ...) invocation for every syscall argument and return value. ptrace(2) man page: long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data); ... PTRACE_GET_SYSCALL_INFO Retrieve information about the syscall that caused the stop. The information is placed into the buffer pointed by "data" argument, which should be a pointer to a buffer of type "struct ptrace_syscall_info". The "addr" argument contains the size of the buffer pointed to by "data" argument (i.e., sizeof(struct ptrace_syscall_info)). The return value contains the number of bytes available to be written by the kernel. If the size of data to be written by the kernel exceeds the size specified by "addr" argument, the output is truncated. [[email protected]: selftests/seccomp/seccomp_bpf: update for PTRACE_GET_SYSCALL_INFO] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Elvira Khabirova <[email protected]> Co-developed-by: Dmitry V. Levin <[email protected]> Signed-off-by: Dmitry V. Levin <[email protected]> Reviewed-by: Oleg Nesterov <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Andy Lutomirski <[email protected]> Cc: Eugene Syromyatnikov <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Greentime Hu <[email protected]> Cc: Helge Deller <[email protected]> [parisc] Cc: James E.J. Bottomley <[email protected]> Cc: James Hogan <[email protected]> Cc: kbuild test robot <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Paul Burton <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Ralf Baechle <[email protected]> Cc: Richard Kuo <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Vincent Chen <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f296f1d commit 201766a

File tree

4 files changed

+150
-8
lines changed

4 files changed

+150
-8
lines changed

include/linux/tracehook.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ struct linux_binprm;
5454
/*
5555
* ptrace report for syscall entry and exit looks identical.
5656
*/
57-
static inline int ptrace_report_syscall(struct pt_regs *regs)
57+
static inline int ptrace_report_syscall(struct pt_regs *regs,
58+
unsigned long message)
5859
{
5960
int ptrace = current->ptrace;
6061

6162
if (!(ptrace & PT_PTRACED))
6263
return 0;
6364

65+
current->ptrace_message = message;
6466
ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
6567

6668
/*
@@ -73,6 +75,7 @@ static inline int ptrace_report_syscall(struct pt_regs *regs)
7375
current->exit_code = 0;
7476
}
7577

78+
current->ptrace_message = 0;
7679
return fatal_signal_pending(current);
7780
}
7881

@@ -98,7 +101,7 @@ static inline int ptrace_report_syscall(struct pt_regs *regs)
98101
static inline __must_check int tracehook_report_syscall_entry(
99102
struct pt_regs *regs)
100103
{
101-
return ptrace_report_syscall(regs);
104+
return ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_ENTRY);
102105
}
103106

104107
/**
@@ -123,7 +126,7 @@ static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
123126
if (step)
124127
user_single_step_report(regs);
125128
else
126-
ptrace_report_syscall(regs);
129+
ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_EXIT);
127130
}
128131

129132
/**

include/uapi/linux/ptrace.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,41 @@ struct seccomp_metadata {
7373
__u64 flags; /* Output: filter's flags */
7474
};
7575

76+
#define PTRACE_GET_SYSCALL_INFO 0x420e
77+
#define PTRACE_SYSCALL_INFO_NONE 0
78+
#define PTRACE_SYSCALL_INFO_ENTRY 1
79+
#define PTRACE_SYSCALL_INFO_EXIT 2
80+
#define PTRACE_SYSCALL_INFO_SECCOMP 3
81+
82+
struct ptrace_syscall_info {
83+
__u8 op; /* PTRACE_SYSCALL_INFO_* */
84+
__u32 arch __attribute__((__aligned__(sizeof(__u32))));
85+
__u64 instruction_pointer;
86+
__u64 stack_pointer;
87+
union {
88+
struct {
89+
__u64 nr;
90+
__u64 args[6];
91+
} entry;
92+
struct {
93+
__s64 rval;
94+
__u8 is_error;
95+
} exit;
96+
struct {
97+
__u64 nr;
98+
__u64 args[6];
99+
__u32 ret_data;
100+
} seccomp;
101+
};
102+
};
103+
104+
/*
105+
* These values are stored in task->ptrace_message
106+
* by tracehook_report_syscall_* to describe the current syscall-stop.
107+
*/
108+
#define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
109+
#define PTRACE_EVENTMSG_SYSCALL_EXIT 2
110+
76111
/* Read signals from a shared (process wide) queue */
77112
#define PTRACE_PEEKSIGINFO_SHARED (1 << 0)
78113

kernel/ptrace.c

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <linux/compat.h>
3333
#include <linux/sched/signal.h>
3434

35+
#include <asm/syscall.h> /* for syscall_get_* */
36+
3537
/*
3638
* Access another process' address space via ptrace.
3739
* Source/target buffer must be kernel space,
@@ -897,7 +899,100 @@ static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
897899
* to ensure no machine forgets it.
898900
*/
899901
EXPORT_SYMBOL_GPL(task_user_regset_view);
900-
#endif
902+
903+
static unsigned long
904+
ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs,
905+
struct ptrace_syscall_info *info)
906+
{
907+
unsigned long args[ARRAY_SIZE(info->entry.args)];
908+
int i;
909+
910+
info->op = PTRACE_SYSCALL_INFO_ENTRY;
911+
info->entry.nr = syscall_get_nr(child, regs);
912+
syscall_get_arguments(child, regs, args);
913+
for (i = 0; i < ARRAY_SIZE(args); i++)
914+
info->entry.args[i] = args[i];
915+
916+
/* args is the last field in struct ptrace_syscall_info.entry */
917+
return offsetofend(struct ptrace_syscall_info, entry.args);
918+
}
919+
920+
static unsigned long
921+
ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs,
922+
struct ptrace_syscall_info *info)
923+
{
924+
/*
925+
* As struct ptrace_syscall_info.entry is currently a subset
926+
* of struct ptrace_syscall_info.seccomp, it makes sense to
927+
* initialize that subset using ptrace_get_syscall_info_entry().
928+
* This can be reconsidered in the future if these structures
929+
* diverge significantly enough.
930+
*/
931+
ptrace_get_syscall_info_entry(child, regs, info);
932+
info->op = PTRACE_SYSCALL_INFO_SECCOMP;
933+
info->seccomp.ret_data = child->ptrace_message;
934+
935+
/* ret_data is the last field in struct ptrace_syscall_info.seccomp */
936+
return offsetofend(struct ptrace_syscall_info, seccomp.ret_data);
937+
}
938+
939+
static unsigned long
940+
ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,
941+
struct ptrace_syscall_info *info)
942+
{
943+
info->op = PTRACE_SYSCALL_INFO_EXIT;
944+
info->exit.rval = syscall_get_error(child, regs);
945+
info->exit.is_error = !!info->exit.rval;
946+
if (!info->exit.is_error)
947+
info->exit.rval = syscall_get_return_value(child, regs);
948+
949+
/* is_error is the last field in struct ptrace_syscall_info.exit */
950+
return offsetofend(struct ptrace_syscall_info, exit.is_error);
951+
}
952+
953+
static int
954+
ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size,
955+
void __user *datavp)
956+
{
957+
struct pt_regs *regs = task_pt_regs(child);
958+
struct ptrace_syscall_info info = {
959+
.op = PTRACE_SYSCALL_INFO_NONE,
960+
.arch = syscall_get_arch(child),
961+
.instruction_pointer = instruction_pointer(regs),
962+
.stack_pointer = user_stack_pointer(regs),
963+
};
964+
unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry);
965+
unsigned long write_size;
966+
967+
/*
968+
* This does not need lock_task_sighand() to access
969+
* child->last_siginfo because ptrace_freeze_traced()
970+
* called earlier by ptrace_check_attach() ensures that
971+
* the tracee cannot go away and clear its last_siginfo.
972+
*/
973+
switch (child->last_siginfo ? child->last_siginfo->si_code : 0) {
974+
case SIGTRAP | 0x80:
975+
switch (child->ptrace_message) {
976+
case PTRACE_EVENTMSG_SYSCALL_ENTRY:
977+
actual_size = ptrace_get_syscall_info_entry(child, regs,
978+
&info);
979+
break;
980+
case PTRACE_EVENTMSG_SYSCALL_EXIT:
981+
actual_size = ptrace_get_syscall_info_exit(child, regs,
982+
&info);
983+
break;
984+
}
985+
break;
986+
case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8):
987+
actual_size = ptrace_get_syscall_info_seccomp(child, regs,
988+
&info);
989+
break;
990+
}
991+
992+
write_size = min(actual_size, user_size);
993+
return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size;
994+
}
995+
#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
901996

902997
int ptrace_request(struct task_struct *child, long request,
903998
unsigned long addr, unsigned long data)
@@ -1114,6 +1209,10 @@ int ptrace_request(struct task_struct *child, long request,
11141209
ret = __put_user(kiov.iov_len, &uiov->iov_len);
11151210
break;
11161211
}
1212+
1213+
case PTRACE_GET_SYSCALL_INFO:
1214+
ret = ptrace_get_syscall_info(child, addr, datavp);
1215+
break;
11171216
#endif
11181217

11191218
case PTRACE_SECCOMP_GET_FILTER:

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,13 +1775,18 @@ void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
17751775
unsigned long msg;
17761776
static bool entry;
17771777

1778-
/* Make sure we got an empty message. */
1778+
/*
1779+
* The traditional way to tell PTRACE_SYSCALL entry/exit
1780+
* is by counting.
1781+
*/
1782+
entry = !entry;
1783+
1784+
/* Make sure we got an appropriate message. */
17791785
ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg);
17801786
EXPECT_EQ(0, ret);
1781-
EXPECT_EQ(0, msg);
1787+
EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
1788+
: PTRACE_EVENTMSG_SYSCALL_EXIT, msg);
17821789

1783-
/* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */
1784-
entry = !entry;
17851790
if (!entry)
17861791
return;
17871792

0 commit comments

Comments
 (0)