-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrap-syscall.scm
313 lines (247 loc) · 8.01 KB
/
wrap-syscall.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;; The wrap-syscall Guile module.
;; Copyright (C) 2014 Free Software Foundation, Inc.
;;
;; This file is part of wrap-syscall.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(define-module (wrap-syscall))
(define *wrap-syscall-dl* #f)
;; This is really a function defined in C.
(define enable! #f)
(define (initialize-dl!)
(setenv "LD_LIBRARY_PATH" (string-append (getenv "HOME") "/gdb/github/wrap-syscall/b-master"))
(set! *wrap-syscall-dl* (dynamic-link "wrap-syscall"))
(dynamic-call (dynamic-func "wrap_syscall_initialize" *wrap-syscall-dl*) #f)
)
(define (initialize!)
;; Perform the initialization in our module so Scheme functions defined
;; in C appear in our module.
(eval '(initialize-dl!) (resolve-module '(wrap-syscall)))
(set! reverse-PTRACE_COMMANDS (reverse-alist PTRACE_COMMANDS))
(set! reverse-SIGNALS (reverse-alist SIGNALS))
(set! reverse-SYSCALLS (reverse-alist SYSCALLS))
)
;; This is actually a function provided by C.
(define enable! #f)
(define (lookup-ptrace-name request)
(assq-ref reverse-PTRACE_COMMANDS request))
(define (lookup-signal-name signal-nr)
(assq-ref reverse-SIGNALS signal-nr))
(define (lookup-syscall-name syscall-nr)
(assq-ref reverse-SYSCALLS syscall-nr))
(define (lookup-wait-flag name)
(assq-ref WAIT_FLAGS name))
(define (lookup-ptrace-nr name)
(assq-ref PTRACE_COMMANDS name))
(define (lookup-signal-nr name)
(assq-ref SIGNALS name))
(define (lookup-syscall-nr name)
(assq-ref SYSCALLS name))
(define (make-stopped signal-nr)
(+ (ash signal-nr 8) #x7f))
(define (format-syscall-call syscall-nr arg1 arg2 arg3 result)
(let ((name (assq-ref reverse-SYSCALLS syscall-nr)))
(case name
((tkill) (if (= arg2 0)
(format #f "syscall (~a (~a), ~a, ~a) -> ~a\n"
syscall-nr name arg1 arg2 result)
(format #f "syscall (~a (~a), ~a, ~a (~a)) -> ~a\n"
syscall-nr name arg1 arg2
(assq-ref reverse-SIGNALS arg2) result)))
(else (format #f "syscall (~a, ~a, ~a, ~a) -> ~a\n"
syscall-nr arg1 arg2 arg3 result)))))
;; Reverse the elements in a key/value alist (key and value are swapped).
(define (reverse-alist alist)
(map (lambda (elm) (cons (cdr elm) (car elm)))
alist))
(define PTRACE_COMMANDS
'(
;; Indicate that the process making this request should be traced.
;; All signals received by this process can be intercepted by its
;; parent, and its parent can use the other `ptrace' requests.
(PTRACE_TRACEME . 0)
;; Return the word in the process's text space at address ADDR.
(PTRACE_PEEKTEXT . 1)
;; Return the word in the process's data space at address ADDR.
(PTRACE_PEEKDATA . 2)
;; Return the word in the process's user area at offset ADDR.
(PTRACE_PEEKUSER . 3)
;; Write the word DATA into the process's text space at address ADDR.
(PTRACE_POKETEXT . 4)
;; Write the word DATA into the process's data space at address ADDR.
(PTRACE_POKEDATA . 5)
;; Write the word DATA into the process's user area at offset ADDR.
(PTRACE_POKEUSER . 6)
;; Continue the process.
(PTRACE_CONT . 7)
;; Kill the process.
(PTRACE_KILL . 8)
;; Single step the process.
;; This is not supported on all machines.
(PTRACE_SINGLESTEP . 9)
;; Get all general purpose registers used by a processes.
;; This is not supported on all machines.
(PTRACE_GETREGS . 12)
;; Set all general purpose registers used by a processes.
;; This is not supported on all machines.
(PTRACE_SETREGS . 13)
;; Get all floating point registers used by a processes.
;; This is not supported on all machines.
(PTRACE_GETFPREGS . 14)
;; Set all floating point registers used by a processes.
;; This is not supported on all machines.
(PTRACE_SETFPREGS . 15)
;; Attach to a process that is already running.
(PTRACE_ATTACH . 16)
;; Detach from a process attached to with PTRACE_ATTACH.
(PTRACE_DETACH . 17)
;; Get all extended floating point registers used by a processes.
;; This is not supported on all machines.
(PTRACE_GETFPXREGS . 18)
;; Set all extended floating point registers used by a processes.
;; This is not supported on all machines.
(PTRACE_SETFPXREGS . 19)
;; Continue and stop at the next (return from) syscall.
(PTRACE_SYSCALL . 24)
;; Set ptrace filter options.
(PTRACE_SETOPTIONS . #x4200)
;; Get last ptrace message.
(PTRACE_GETEVENTMSG . #x4201)
;; Get siginfo for process.
(PTRACE_GETSIGINFO . #x4202)
;; Set new siginfo for process.
(PTRACE_SETSIGINFO . #x4203)
;; Get register content.
(PTRACE_GETREGSET . #x4204)
;; Set register content.
(PTRACE_SETREGSET . #x4205)
;; Like PTRACE_ATTACH, but do not force tracee to trap and do not affect
;; signal or group stop state.
(PTRACE_SEIZE . #x4206)
;; Trap seized tracee.
(PTRACE_INTERRUPT . #x4207)
;; Wait for next group event.
(PTRACE_LISTEN . #x4208)
))
(define reverse-PTRACE_COMMANDS #f)
;; Flag for PTRACE_LISTEN.
(define PTRACE_FLAGS
'(
(PTRACE_SEIZE_DEVEL . #x80000000)
))
;; Options set using PTRACE_SETOPTIONS.
(define PTRACE_SETOPTIONS
'(
(PTRACE_O_TRACESYSGOOD . #x00000001)
(PTRACE_O_TRACEFORK . #x00000002)
(PTRACE_O_TRACEVFORK . #x00000004)
(PTRACE_O_TRACECLONE . #x00000008)
(PTRACE_O_TRACEEXEC . #x00000010)
(PTRACE_O_TRACEVFORKDONE . #x00000020)
(PTRACE_O_TRACEEXIT . #x00000040)
(PTRACE_O_TRACESECCOMP . #x00000080)
(PTRACE_O_MASK . #x000000ff)
))
;; Wait extended result codes for the above trace options.
(define PTRACE_EVENTCODES
'(
(PTRACE_EVENT_FORK . 1)
(PTRACE_EVENT_VFORK . 2)
(PTRACE_EVENT_CLONE . 3)
(PTRACE_EVENT_EXEC . 4)
(PTRACE_EVENT_VFORK_DONE . 5)
(PTRACE_EVENT_EXIT . 6)
(PTRAVE_EVENT_SECCOMP . 7)
))
(define WAIT_FLAGS
'(
(WNOHANG . 1) ;; Don't block waiting.
(WUNTRACED . 2) ;; Report status of stopped children.
(WSTOPPED . 2) ;; Report stopped child (same as WUNTRACED). */
(WEXITED . 4) ;; Report dead child. */
(WCONTINUED . 8) ;; Report continued child. */
(WNOWAIT . #x01000000) ;; Don't reap, just poll status. */
(__WNOTHREAD . #x20000000) ;; Don't wait on children of other threads in this group
(__WALL . #x40000000) ;; Wait for any child.
(__WCLONE . #x80000000) ;; Wait for cloned process.
))
(define SIGNALS
'(
(SIGHUP . 1)
(SIGINT . 2)
(SIGQUIT . 3)
(SIGILL . 4)
(SIGTRAP . 5)
(SIGABRT . 6)
(SIGIOT . 6)
(SIGBUS . 7)
(SIGFPE . 8)
(SIGKILL . 9)
(SIGUSR1 . 10)
(SIGSEGV . 11)
(SIGUSR2 . 12)
(SIGPIPE . 13)
(SIGALRM . 14)
(SIGTERM . 15)
(SIGSTKFLT . 16)
(SIGCHLD . 17)
(SIGCONT . 18)
(SIGSTOP . 19)
(SIGTSTP . 20)
(SIGTTIN . 21)
(SIGTTOU . 22)
(SIGURG . 23)
(SIGXCPU . 24)
(SIGXFSZ . 25)
(SIGVTALRM . 26)
(SIGPROF . 27)
(SIGWINCH . 28)
(SIGIO . 29)
(SIGPOLL . 29)
(SIGPWR . 30)
(SIGSYS . 31)
(SIGUNUSED . 31)
))
(define reverse-SIGNALS #f)
(define SYSCALLS
'(
(tkill . 200)
))
(define reverse-SYSCALLS #f)
(export
initialize!
enable!
set-wait-status!
get-wait-status
lookup-ptrace-name
lookup-ptrace-nr
lookup-signal-name
lookup-signal-nr
lookup-syscall-name
lookup-syscall-nr
lookup-wait-flag
make-stopped
format-syscall-call
real-ptrace
real-waitpid
real-kill
real-fork
real-vfork
real-open
real-open64
real-read
real-pread64
real-close
real-syscall
)