blob: 5f47422401e1e98d634d12882bd098a9c77a6207 [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Al Viro1bfa2312012-05-23 00:18:33 -04006#include <linux/audit.h>
7#include <linux/ptrace.h>
8#include <linux/sched.h>
9#include <linux/tracehook.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080010#include <linux/uaccess.h>
Richard Weinbergerda028d52015-06-25 22:44:11 +020011#include <asm/ptrace-abi.h>
Christoph Hellwig1bd09502010-03-10 15:22:56 -080012
13void user_enable_single_step(struct task_struct *child)
Bodo Stroesser82c1c112005-05-06 21:30:46 -070014{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080015 child->ptrace |= PT_DTRACE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070016 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070017
18#ifdef SUBARCH_SET_SINGLESTEPPING
Christoph Hellwig1bd09502010-03-10 15:22:56 -080019 SUBARCH_SET_SINGLESTEPPING(child, 1);
20#endif
21}
22
23void user_disable_single_step(struct task_struct *child)
24{
25 child->ptrace &= ~PT_DTRACE;
26 child->thread.singlestep_syscall = 0;
27
28#ifdef SUBARCH_SET_SINGLESTEPPING
29 SUBARCH_SET_SINGLESTEPPING(child, 0);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070030#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070031}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Called by kernel/ptrace.c when detaching..
35 */
36void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070037{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080038 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Bodo Stroesser82c1c112005-05-06 21:30:46 -070041extern int peek_user(struct task_struct * child, long addr, long data);
42extern int poke_user(struct task_struct * child, long addr, long data);
43
Namhyung Kim9b05a692010-10-27 15:33:47 -070044long arch_ptrace(struct task_struct *child, long request,
45 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i, ret;
Namhyung Kim9b05a692010-10-27 15:33:47 -070048 unsigned long __user *p = (void __user *)data;
Namhyung Kim0a3d7632010-10-27 15:34:04 -070049 void __user *vp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070053 case PTRACE_PEEKUSR:
54 ret = peek_user(child, addr, data);
55 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Dikeba180fd2007-10-16 01:27:00 -070057 /* write the word at location addr in the USER area */
58 case PTRACE_POKEUSR:
59 ret = poke_user(child, addr, data);
60 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070062 case PTRACE_SYSEMU:
63 case PTRACE_SYSEMU_SINGLESTEP:
64 ret = -EIO;
65 break;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#ifdef PTRACE_GETREGS
68 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Linus Torvalds96d4f262019-01-03 18:57:57 -080069 if (!access_ok(p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ret = -EIO;
71 break;
72 }
73 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080074 __put_user(getreg(child, i), p);
75 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 ret = 0;
78 break;
79 }
80#endif
81#ifdef PTRACE_SETREGS
82 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
83 unsigned long tmp = 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -080084 if (!access_ok(p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ret = -EIO;
86 break;
87 }
88 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080089 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -080091 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93 ret = 0;
94 break;
95 }
96#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -080097 case PTRACE_GET_THREAD_AREA:
Namhyung Kim0a3d7632010-10-27 15:34:04 -070098 ret = ptrace_get_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -080099 break;
100
101 case PTRACE_SET_THREAD_AREA:
Richard Weinberger8818b672010-11-11 14:05:04 -0800102 ret = ptrace_set_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800103 break;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 default:
106 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700107 if (ret == -EIO)
108 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ret;
113}
114
WANG Cong99764fa2008-07-23 21:28:49 -0700115static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int error_code)
117{
Simon Arlottb60745b2007-10-20 01:23:03 +0200118 /* Send us the fake SIGTRAP */
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500119 force_sig_fault(SIGTRAP, TRAP_BRKPT,
120 /* User-mode eip? */
121 UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Jeff Dikeba180fd2007-10-16 01:27:00 -0700124/*
125 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
127 */
Richard Weinberger5334cda2015-05-31 22:59:03 +0200128int syscall_trace_enter(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Eric Paris91397402014-03-11 13:29:28 -0400130 audit_syscall_entry(UPT_SYSCALL_NR(&regs->regs),
Al Viro1bfa2312012-05-23 00:18:33 -0400131 UPT_SYSCALL_ARG1(&regs->regs),
132 UPT_SYSCALL_ARG2(&regs->regs),
133 UPT_SYSCALL_ARG3(&regs->regs),
134 UPT_SYSCALL_ARG4(&regs->regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (!test_thread_flag(TIF_SYSCALL_TRACE))
Richard Weinberger5334cda2015-05-31 22:59:03 +0200137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Richard Weinberger5334cda2015-05-31 22:59:03 +0200139 return tracehook_report_syscall_entry(regs);
Al Viro1bfa2312012-05-23 00:18:33 -0400140}
141
142void syscall_trace_leave(struct pt_regs *regs)
143{
144 int ptraced = current->ptrace;
145
146 audit_syscall_exit(regs);
147
148 /* Fake a debug trap */
149 if (ptraced & PT_DTRACE)
150 send_sigtrap(current, &regs->regs, 0);
151
152 if (!test_thread_flag(TIF_SYSCALL_TRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return;
154
Al Viro1bfa2312012-05-23 00:18:33 -0400155 tracehook_report_syscall_exit(regs, 0);
156 /* force do_signal() --> is_syscall() */
157 if (ptraced & PT_PTRACED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}