Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 84d77d3f06e7 ("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.

[ldv@altlinux.org: selftests/seccomp/seccomp_bpf: update for PTRACE_GET_SYSCALL_INFO]
Link: http://lkml.kernel.org/r/20190708182904.GA12332@altlinux.org
Link: http://lkml.kernel.org/r/20190510152842.GF28558@altlinux.org
Signed-off-by: Elvira Khabirova <lineprinter@altlinux.org>
Co-developed-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Andy Lutomirski <luto@kernel.org>
Cc: Eugene Syromyatnikov <esyr@redhat.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Greentime Hu <greentime@andestech.com>
Cc: Helge Deller <deller@gmx.de> [parisc]
Cc: James E.J. Bottomley <jejb@parisc-linux.org>
Cc: James Hogan <jhogan@kernel.org>
Cc: kbuild test robot <lkp@intel.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Paul Burton <paul.burton@mips.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Richard Kuo <rkuo@codeaurora.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Vincent Chen <deanbo422@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Elvira Khabirova and committed by
Linus Torvalds
201766a2 f296f1df

+150 -8
+6 -3
include/linux/tracehook.h
··· 54 54 /* 55 55 * ptrace report for syscall entry and exit looks identical. 56 56 */ 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) 58 59 { 59 60 int ptrace = current->ptrace; 60 61 61 62 if (!(ptrace & PT_PTRACED)) 62 63 return 0; 63 64 65 + current->ptrace_message = message; 64 66 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0)); 65 67 66 68 /* ··· 75 73 current->exit_code = 0; 76 74 } 77 75 76 + current->ptrace_message = 0; 78 77 return fatal_signal_pending(current); 79 78 } 80 79 ··· 101 98 static inline __must_check int tracehook_report_syscall_entry( 102 99 struct pt_regs *regs) 103 100 { 104 - return ptrace_report_syscall(regs); 101 + return ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_ENTRY); 105 102 } 106 103 107 104 /** ··· 126 123 if (step) 127 124 user_single_step_report(regs); 128 125 else 129 - ptrace_report_syscall(regs); 126 + ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_EXIT); 130 127 } 131 128 132 129 /**
+35
include/uapi/linux/ptrace.h
··· 73 73 __u64 flags; /* Output: filter's flags */ 74 74 }; 75 75 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 + 76 111 /* Read signals from a shared (process wide) queue */ 77 112 #define PTRACE_PEEKSIGINFO_SHARED (1 << 0) 78 113
+100 -1
kernel/ptrace.c
··· 32 32 #include <linux/compat.h> 33 33 #include <linux/sched/signal.h> 34 34 35 + #include <asm/syscall.h> /* for syscall_get_* */ 36 + 35 37 /* 36 38 * Access another process' address space via ptrace. 37 39 * Source/target buffer must be kernel space, ··· 899 897 * to ensure no machine forgets it. 900 898 */ 901 899 EXPORT_SYMBOL_GPL(task_user_regset_view); 902 - #endif 900 + 901 + static unsigned long 902 + ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs, 903 + struct ptrace_syscall_info *info) 904 + { 905 + unsigned long args[ARRAY_SIZE(info->entry.args)]; 906 + int i; 907 + 908 + info->op = PTRACE_SYSCALL_INFO_ENTRY; 909 + info->entry.nr = syscall_get_nr(child, regs); 910 + syscall_get_arguments(child, regs, args); 911 + for (i = 0; i < ARRAY_SIZE(args); i++) 912 + info->entry.args[i] = args[i]; 913 + 914 + /* args is the last field in struct ptrace_syscall_info.entry */ 915 + return offsetofend(struct ptrace_syscall_info, entry.args); 916 + } 917 + 918 + static unsigned long 919 + ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs, 920 + struct ptrace_syscall_info *info) 921 + { 922 + /* 923 + * As struct ptrace_syscall_info.entry is currently a subset 924 + * of struct ptrace_syscall_info.seccomp, it makes sense to 925 + * initialize that subset using ptrace_get_syscall_info_entry(). 926 + * This can be reconsidered in the future if these structures 927 + * diverge significantly enough. 928 + */ 929 + ptrace_get_syscall_info_entry(child, regs, info); 930 + info->op = PTRACE_SYSCALL_INFO_SECCOMP; 931 + info->seccomp.ret_data = child->ptrace_message; 932 + 933 + /* ret_data is the last field in struct ptrace_syscall_info.seccomp */ 934 + return offsetofend(struct ptrace_syscall_info, seccomp.ret_data); 935 + } 936 + 937 + static unsigned long 938 + ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs, 939 + struct ptrace_syscall_info *info) 940 + { 941 + info->op = PTRACE_SYSCALL_INFO_EXIT; 942 + info->exit.rval = syscall_get_error(child, regs); 943 + info->exit.is_error = !!info->exit.rval; 944 + if (!info->exit.is_error) 945 + info->exit.rval = syscall_get_return_value(child, regs); 946 + 947 + /* is_error is the last field in struct ptrace_syscall_info.exit */ 948 + return offsetofend(struct ptrace_syscall_info, exit.is_error); 949 + } 950 + 951 + static int 952 + ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size, 953 + void __user *datavp) 954 + { 955 + struct pt_regs *regs = task_pt_regs(child); 956 + struct ptrace_syscall_info info = { 957 + .op = PTRACE_SYSCALL_INFO_NONE, 958 + .arch = syscall_get_arch(child), 959 + .instruction_pointer = instruction_pointer(regs), 960 + .stack_pointer = user_stack_pointer(regs), 961 + }; 962 + unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry); 963 + unsigned long write_size; 964 + 965 + /* 966 + * This does not need lock_task_sighand() to access 967 + * child->last_siginfo because ptrace_freeze_traced() 968 + * called earlier by ptrace_check_attach() ensures that 969 + * the tracee cannot go away and clear its last_siginfo. 970 + */ 971 + switch (child->last_siginfo ? child->last_siginfo->si_code : 0) { 972 + case SIGTRAP | 0x80: 973 + switch (child->ptrace_message) { 974 + case PTRACE_EVENTMSG_SYSCALL_ENTRY: 975 + actual_size = ptrace_get_syscall_info_entry(child, regs, 976 + &info); 977 + break; 978 + case PTRACE_EVENTMSG_SYSCALL_EXIT: 979 + actual_size = ptrace_get_syscall_info_exit(child, regs, 980 + &info); 981 + break; 982 + } 983 + break; 984 + case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8): 985 + actual_size = ptrace_get_syscall_info_seccomp(child, regs, 986 + &info); 987 + break; 988 + } 989 + 990 + write_size = min(actual_size, user_size); 991 + return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size; 992 + } 993 + #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 903 994 904 995 int ptrace_request(struct task_struct *child, long request, 905 996 unsigned long addr, unsigned long data) ··· 1209 1114 ret = __put_user(kiov.iov_len, &uiov->iov_len); 1210 1115 break; 1211 1116 } 1117 + 1118 + case PTRACE_GET_SYSCALL_INFO: 1119 + ret = ptrace_get_syscall_info(child, addr, datavp); 1120 + break; 1212 1121 #endif 1213 1122 1214 1123 case PTRACE_SECCOMP_GET_FILTER:
+9 -4
tools/testing/selftests/seccomp/seccomp_bpf.c
··· 1775 1775 unsigned long msg; 1776 1776 static bool entry; 1777 1777 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. */ 1779 1785 ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); 1780 1786 EXPECT_EQ(0, ret); 1781 - EXPECT_EQ(0, msg); 1787 + EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY 1788 + : PTRACE_EVENTMSG_SYSCALL_EXIT, msg); 1782 1789 1783 - /* The only way to tell PTRACE_SYSCALL entry/exit is by counting. */ 1784 - entry = !entry; 1785 1790 if (!entry) 1786 1791 return; 1787 1792