From: Pierre Morel <pmorel@linux.vnet.ibm.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Oleg Nesterov <oleg@tv-sign.ru>,
Roland McGrath <roland@redhat.com>,
Heiko Carstens <heicars2@linux.vnet.ibm.com>,
sameske@linux.vnet.ibm.com,
Martin Schwidefsky <schwidefsky@de.ibm.com>,
Ingo Molnar <mingo@elte.hu>,
gregkh@suse.de,
uml-devel <user-mode-linux-devel@lists.sourceforge.net>,
Dave Hansen <dave@linux.vnet.ibm.com>,
Cedric Le Goater <clg@fr.ibm.com>,
Daniel Lezcano <dlezcano@fr.ibm.com>
Subject: [PATCH 1/1] system call notification with self_ptrace
Date: Mon, 08 Sep 2008 14:02:01 +0200 [thread overview]
Message-ID: <48C51439.7000706@linux.vnet.ibm.com> (raw)
Subject: [PATCH] system call notification with self_ptrace
From: Pierre Morel <pmorel@fr.ibm.com>
PTRACE SELF
This patch adds a new functionality to ptrace: system call notification to
the current process.
When a process requests self ptrace, with the new request PTRACE_SELF_ON:
1. the next system call performed by the process will not be executed
2. self ptrace will be disabled for the process
3. a SIGSYS signal will be sent to the process.
With an appropriate SIGSYS signal handler, the process can access its own
data structures to
1. get the system call number from the siginfo structure
2. get the system call arguments from the stack
3. instrument the system call with other system calls
4. emulate the system call with other system calls
5. change the arguments of the system call
6. perform the system call for good
7. change the return value of the system call
8. request self ptrace again before returning.
The new request PTRACE_SELF_OFF disables self ptrace.
Signed-off-by: Pierre Morel <pmorel@fr.ibm.com>
Signed-off-by: Volker Sameske <sameske@de.ibm.com>
---
arch/s390/kernel/ptrace.c | 16 ++++++++++++++++
arch/s390/kernel/signal.c | 5 +++++
arch/x86/kernel/ptrace.c | 29 +++++++++++++++++++++++++++++
arch/x86/kernel/signal_32.c | 5 +++++
arch/x86/kernel/signal_64.c | 5 +++++
include/asm-generic/siginfo.h | 6 ++++++
include/linux/ptrace.h | 18 ++++++++++++++++++
include/linux/sched.h | 1 +
kernel/ptrace.c | 32 ++++++++++++++++++++++++++++++++
9 files changed, 117 insertions(+)
Index: linux-2.6.26/arch/s390/kernel/ptrace.c
===================================================================
--- linux-2.6.26.orig/arch/s390/kernel/ptrace.c
+++ linux-2.6.26/arch/s390/kernel/ptrace.c
@@ -583,6 +583,22 @@ syscall_trace(struct pt_regs *regs, int
if (!test_thread_flag(TIF_SYSCALL_TRACE))
goto out;
+
+ if (is_self_ptracing(regs->gprs[2])) {
+ if (!entryexit) {
+ struct siginfo info;
+
+ memset(&info, 0, sizeof(struct siginfo));
+ info.si_signo = SIGSYS;
+ info.si_code = SYS_SYSCALL;
+ info.si_errno = regs->gprs[2];
+ info.si_addr = (void *)regs->orig_gpr2;
+ send_sig_info(SIGSYS, &info, current);
+ regs->gprs[2] = -1;
+ }
+ return;
+ }
+
if (!(current->ptrace & PT_PTRACED))
goto out;
ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
Index: linux-2.6.26/arch/s390/kernel/signal.c
===================================================================
--- linux-2.6.26.orig/arch/s390/kernel/signal.c
+++ linux-2.6.26/arch/s390/kernel/signal.c
@@ -409,6 +409,11 @@ handle_signal(unsigned long sig, struct
spin_unlock_irq(¤t->sighand->siglock);
}
+ if (current->instrumentation) {
+ clear_thread_flag(TIF_SYSCALL_TRACE);
+ current->instrumentation &= ~PTS_SELF;
+ }
+
return ret;
}
Index: linux-2.6.26/arch/x86/kernel/ptrace.c
===================================================================
--- linux-2.6.26.orig/arch/x86/kernel/ptrace.c
+++ linux-2.6.26/arch/x86/kernel/ptrace.c
@@ -1394,6 +1394,19 @@ int do_syscall_trace(struct pt_regs *reg
if (!entryexit)
secure_computing(regs->orig_ax);
+ if (is_self_ptracing(regs->orig_ax)) {
+ if (!entryexit) {
+ struct siginfo info;
+
+ memset(&info, 0, sizeof(struct siginfo));
+ info.si_signo = SIGSYS;
+ info.si_code = SYS_SYSCALL;
+ info.si_addr = (void *) regs->orig_ax;
+ send_sig_info(SIGSYS, &info, current);
+ }
+ return 1; /* Skip system call, deliver signal. */
+ }
+
if (unlikely(current->audit_context)) {
if (entryexit)
audit_syscall_exit(AUDITSC_RESULT(regs->ax),
@@ -1486,6 +1499,18 @@ asmlinkage void syscall_trace_enter(stru
/* do the secure computing check first */
secure_computing(regs->orig_ax);
+ if (is_self_ptracing(regs->orig_ax)) {
+ struct siginfo info;
+
+ memset(&info, 0, sizeof(struct siginfo));
+ info.si_signo = SIGSYS;
+ info.si_code = SYS_SYSCALL;
+ info.si_addr = (void *) regs->orig_ax;
+ send_sig_info(SIGSYS, &info, current);
+ regs->ax = -1 ;
+ return; /* Skip system call, deliver signal. */
+ }
+
if (test_thread_flag(TIF_SYSCALL_TRACE)
&& (current->ptrace & PT_PTRACED))
syscall_trace(regs);
@@ -1507,6 +1532,10 @@ asmlinkage void syscall_trace_enter(stru
asmlinkage void syscall_trace_leave(struct pt_regs *regs)
{
+ if (is_self_ptracing(regs->orig_ax)) {
+ regs->ax = -1 ;
+ return; /* Skip system call. */
+ }
if (unlikely(current->audit_context))
audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
Index: linux-2.6.26/arch/x86/kernel/signal_32.c
===================================================================
--- linux-2.6.26.orig/arch/x86/kernel/signal_32.c
+++ linux-2.6.26/arch/x86/kernel/signal_32.c
@@ -568,6 +568,11 @@ handle_signal(unsigned long sig, siginfo
recalc_sigpending();
spin_unlock_irq(¤t->sighand->siglock);
+ if (current->instrumentation & PTS_SELF) {
+ clear_thread_flag(TIF_SYSCALL_TRACE);
+ current->instrumentation &= ~PTS_SELF;
+ }
+
return 0;
}
Index: linux-2.6.26/arch/x86/kernel/signal_64.c
===================================================================
--- linux-2.6.26.orig/arch/x86/kernel/signal_64.c
+++ linux-2.6.26/arch/x86/kernel/signal_64.c
@@ -402,6 +402,11 @@ handle_signal(unsigned long sig, siginfo
spin_unlock_irq(¤t->sighand->siglock);
}
+ if (current->instrumentation & PTS_SELF) {
+ clear_thread_flag(TIF_SYSCALL_TRACE);
+ current->instrumentation &= ~PTS_SELF;
+ }
+
return ret;
}
Index: linux-2.6.26/include/asm-generic/siginfo.h
===================================================================
--- linux-2.6.26.orig/include/asm-generic/siginfo.h
+++ linux-2.6.26/include/asm-generic/siginfo.h
@@ -224,6 +224,12 @@ typedef struct siginfo {
#define NSIGPOLL 6
/*
+ * SIGSYS si_codes
+ */
+#define SYS_SYSCALL (__SI_FAULT|1) /* system call notification */
+#define NSIGSYS 1
+
+/*
* sigevent definitions
*
* It seems likely that SIGEV_THREAD will have to be handled from
Index: linux-2.6.26/include/linux/ptrace.h
===================================================================
--- linux-2.6.26.orig/include/linux/ptrace.h
+++ linux-2.6.26/include/linux/ptrace.h
@@ -27,6 +27,10 @@
#define PTRACE_GETSIGINFO 0x4202
#define PTRACE_SETSIGINFO 0x4203
+/* PTRACE SELF requests */
+#define PTRACE_SELF_ON 0x4281
+#define PTRACE_SELF_OFF 0x4282
+
/* options set using PTRACE_SETOPTIONS */
#define PTRACE_O_TRACESYSGOOD 0x00000001
#define PTRACE_O_TRACEFORK 0x00000002
@@ -78,7 +82,21 @@
#include <linux/compiler.h> /* For unlikely. */
#include <linux/sched.h> /* For struct task_struct. */
+#include <linux/unistd.h> /* For syscall definitions */
+
+#define PTS_INSTRUMENTED 0x00000001
+#define PTS_SELF 0x00000002
+static inline int is_self_ptracing(unsigned long syscall)
+{
+ if (!(current->instrumentation & PTS_SELF))
+ return 0;
+ if (syscall == __NR_rt_sigreturn)
+ return 0;
+ if (syscall == __NR_ptrace)
+ return 0;
+ return 1;
+}
extern long arch_ptrace(struct task_struct *child, long request, long addr, long data);
extern struct task_struct *ptrace_get_task_struct(pid_t pid);
Index: linux-2.6.26/kernel/ptrace.c
===================================================================
--- linux-2.6.26.orig/kernel/ptrace.c
+++ linux-2.6.26/kernel/ptrace.c
@@ -543,6 +543,38 @@ asmlinkage long sys_ptrace(long request,
* This lock_kernel fixes a subtle race with suid exec
*/
lock_kernel();
+ if (request == PTRACE_SELF_ON) {
+ task_lock(current);
+ if (current->ptrace) {
+ task_unlock(current);
+ ret = -EPERM;
+ goto out;
+ }
+ set_thread_flag(TIF_SYSCALL_TRACE);
+ current->instrumentation |= PTS_INSTRUMENTED|PTS_SELF;
+ task_unlock(current);
+ ret = 0;
+ goto out;
+ }
+ if (request == PTRACE_SELF_OFF) {
+ task_lock(current);
+ if (current->ptrace) {
+ task_unlock(current);
+ ret = -EPERM;
+ goto out;
+ }
+ clear_thread_flag(TIF_SYSCALL_TRACE);
+ current->instrumentation &= ~PTS_SELF;
+ task_unlock(current);
+ ret = 0;
+ goto out;
+ }
+
+ if (current->instrumentation) {
+ ret = -EPERM;
+ goto out;
+ }
+
if (request == PTRACE_TRACEME) {
ret = ptrace_traceme();
if (!ret)
Index: linux-2.6.26/include/linux/sched.h
===================================================================
--- linux-2.6.26.orig/include/linux/sched.h
+++ linux-2.6.26/include/linux/sched.h
@@ -1303,6 +1303,7 @@ struct task_struct {
int latency_record_count;
struct latency_record latency_record[LT_SAVECOUNT];
#endif
+ u64 instrumentation;
};
/*
next reply other threads:[~2008-09-08 12:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-08 12:02 Pierre Morel [this message]
2008-09-09 0:04 ` [PATCH 1/1] system call notification with self_ptrace Andrew Morton
2008-09-10 14:17 ` Pierre Morel
2008-09-09 12:43 ` Oleg Nesterov
2008-09-10 15:11 ` Pierre Morel
2008-09-10 16:20 ` Oleg Nesterov
2008-09-10 16:23 ` Dave Hansen
2008-09-12 12:22 ` Pierre Morel
2008-09-12 12:19 ` Pierre Morel
2008-09-12 14:32 ` Oleg Nesterov
2008-09-10 16:19 ` Dave Hansen
2008-09-12 12:30 ` Pierre Morel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=48C51439.7000706@linux.vnet.ibm.com \
--to=pmorel@linux.vnet.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=clg@fr.ibm.com \
--cc=dave@linux.vnet.ibm.com \
--cc=dlezcano@fr.ibm.com \
--cc=gregkh@suse.de \
--cc=heicars2@linux.vnet.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=oleg@tv-sign.ru \
--cc=roland@redhat.com \
--cc=sameske@linux.vnet.ibm.com \
--cc=schwidefsky@de.ibm.com \
--cc=user-mode-linux-devel@lists.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox