]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - lib/libproc/proc_bkpt.c
Fix multiple OpenSSL vulnerabilitites. [SA-16:17]
[FreeBSD/releng/9.3.git] / lib / libproc / proc_bkpt.c
1 /*
2  * Copyright (c) 2010 The FreeBSD Foundation 
3  * All rights reserved. 
4  * 
5  * This software was developed by Rui Paulo under sponsorship from the
6  * FreeBSD Foundation. 
7  *  
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions 
10  * are met: 
11  * 1. Redistributions of source code must retain the above copyright 
12  *    notice, this list of conditions and the following disclaimer. 
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
27  * SUCH DAMAGE. 
28  */ 
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
35 #include <sys/wait.h>
36 #include <machine/_inttypes.h>
37
38 #include <assert.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include "_libproc.h"
44
45 #if defined(__i386__) || defined(__amd64__)
46 #define BREAKPOINT_INSTR        0xcc    /* int 0x3 */
47 #define BREAKPOINT_INSTR_SZ     1
48 #else
49 #error "Add support for your architecture"
50 #endif
51
52 static void
53 proc_cont(struct proc_handle *phdl)
54 {
55
56         ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)1, 0);
57 }
58
59 static int
60 proc_stop(struct proc_handle *phdl)
61 {
62         int status;
63
64         if (kill(proc_getpid(phdl), SIGSTOP) == -1) {
65                 DPRINTF("kill %d", proc_getpid(phdl));
66                 return (-1);
67         } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) {
68                 DPRINTF("waitpid %d", proc_getpid(phdl));
69                 return (-1);
70         } else if (!WIFSTOPPED(status)) {
71                 DPRINTFX("waitpid: unexpected status 0x%x", status);
72                 return (-1);
73         }
74
75         return (0);
76 }
77
78 int
79 proc_bkptset(struct proc_handle *phdl, uintptr_t address,
80     unsigned long *saved)
81 {
82         struct ptrace_io_desc piod;
83         unsigned long paddr, caddr;
84         int ret = 0;
85
86         *saved = 0;
87         if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
88             phdl->status == PS_IDLE) {
89                 errno = ENOENT;
90                 return (-1);
91         }
92
93         DPRINTFX("adding breakpoint at 0x%lx", address);
94
95         if (phdl->status != PS_STOP)
96                 if (proc_stop(phdl) != 0)
97                         return (-1);
98
99         /*
100          * Read the original instruction.
101          */
102         caddr = address;
103         paddr = 0;
104         piod.piod_op = PIOD_READ_I;
105         piod.piod_offs = (void *)caddr;
106         piod.piod_addr = &paddr;
107         piod.piod_len  = BREAKPOINT_INSTR_SZ;
108         if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
109                 DPRINTF("ERROR: couldn't read instruction at address 0x%"
110                     PRIuPTR, address);
111                 ret = -1;
112                 goto done;
113         }
114         *saved = paddr;
115         /*
116          * Write a breakpoint instruction to that address.
117          */
118         caddr = address;
119         paddr = BREAKPOINT_INSTR;
120         piod.piod_op = PIOD_WRITE_I;
121         piod.piod_offs = (void *)caddr;
122         piod.piod_addr = &paddr;
123         piod.piod_len  = BREAKPOINT_INSTR_SZ;
124         if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
125                 DPRINTF("ERROR: couldn't write instruction at address 0x%"
126                     PRIuPTR, address);
127                 ret = -1;
128                 goto done;
129         }
130
131 done:
132         if (phdl->status != PS_STOP)
133                 /* Restart the process if we had to stop it. */
134                 proc_cont(phdl);
135
136         return (ret);
137 }
138
139 int
140 proc_bkptdel(struct proc_handle *phdl, uintptr_t address,
141     unsigned long saved)
142 {
143         struct ptrace_io_desc piod;
144         unsigned long paddr, caddr;
145         int ret = 0;
146
147         if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD ||
148             phdl->status == PS_IDLE) {
149                 errno = ENOENT;
150                 return (-1);
151         }
152
153         DPRINTFX("removing breakpoint at 0x%lx", address);
154
155         if (phdl->status != PS_STOP)
156                 if (proc_stop(phdl) != 0)
157                         return (-1);
158
159         /*
160          * Overwrite the breakpoint instruction that we setup previously.
161          */
162         caddr = address;
163         paddr = saved;
164         piod.piod_op = PIOD_WRITE_I;
165         piod.piod_offs = (void *)caddr;
166         piod.piod_addr = &paddr;
167         piod.piod_len  = BREAKPOINT_INSTR_SZ;
168         if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) {
169                 DPRINTF("ERROR: couldn't write instruction at address 0x%"
170                     PRIuPTR, address);
171                 ret = -1;
172         }
173
174         if (phdl->status != PS_STOP)
175                 /* Restart the process if we had to stop it. */
176                 proc_cont(phdl);
177  
178         return (ret);
179 }
180
181 /*
182  * Decrement pc so that we delete the breakpoint at the correct
183  * address, i.e. at the BREAKPOINT_INSTR address.
184  */
185 void
186 proc_bkptregadj(unsigned long *pc)
187 {
188         *pc = *pc - BREAKPOINT_INSTR_SZ;
189 }
190
191 /*
192  * Step over the breakpoint.
193  */
194 int
195 proc_bkptexec(struct proc_handle *phdl, unsigned long saved)
196 {
197         unsigned long pc;
198         unsigned long samesaved;
199         int status;
200
201         if (proc_regget(phdl, REG_PC, &pc) < 0) {
202                 DPRINTFX("ERROR: couldn't get PC register");
203                 return (-1);
204         }
205         proc_bkptregadj(&pc);
206         if (proc_bkptdel(phdl, pc, saved) < 0) {
207                 DPRINTFX("ERROR: couldn't delete breakpoint");
208                 return (-1);
209         }
210         /*
211          * Go back in time and step over the new instruction just
212          * set up by proc_bkptdel().
213          */
214         proc_regset(phdl, REG_PC, pc);
215         if (ptrace(PT_STEP, proc_getpid(phdl), (caddr_t)1, 0) < 0) {
216                 DPRINTFX("ERROR: ptrace step failed");
217                 return (-1);
218         }
219         proc_wstatus(phdl);
220         status = proc_getwstat(phdl);
221         if (!WIFSTOPPED(status)) {
222                 DPRINTFX("ERROR: don't know why process stopped");
223                 return (-1);
224         }
225         /*
226          * Restore the breakpoint. The saved instruction should be
227          * the same as the one that we were passed in.
228          */
229         if (proc_bkptset(phdl, pc, &samesaved) < 0) {
230                 DPRINTFX("ERROR: couldn't restore breakpoint");
231                 return (-1);
232         }
233         assert(samesaved == saved);
234
235         return (0);
236 }