-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrace.go
128 lines (116 loc) · 2.78 KB
/
trace.go
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
package main
import (
"bytes"
"encoding/binary"
"errors"
"os"
"os/exec"
"syscall"
"github.com/orivej/e"
"golang.org/x/sys/unix"
)
var errTraceeExited = errors.New("tracee failed to start")
func getRegs(pid int) (syscall.PtraceRegs, bool) {
var regs syscall.PtraceRegs
err := syscall.PtraceGetRegs(pid, ®s)
if err != nil && err.(syscall.Errno) == syscall.ESRCH {
return regs, false
}
e.Exit(err)
return regs, true
}
func readString(pid int, addr uint64) string {
var chunk [64]byte
var buf []byte
for {
n, err := syscall.PtracePeekData(pid, uintptr(addr), chunk[:])
if err != syscall.EIO {
e.Print(err)
}
end := bytes.IndexByte(chunk[:n], 0)
if end != -1 {
buf = append(buf, chunk[:end]...)
return string(buf)
}
buf = append(buf, chunk[:n]...)
addr += uint64(n)
}
}
func readStrings(pid int, addr uint64) []string {
var buf [8]byte
var res []string
for {
n, err := syscall.PtracePeekData(pid, uintptr(addr), buf[:])
e.Exit(err)
saddr := binary.LittleEndian.Uint64(buf[:])
if saddr == 0 {
return res
}
res = append(res, readString(pid, saddr))
addr += uint64(n)
}
}
func resume(pid, signal int, duringSyscall bool) {
if duringSyscall || !withSeccomp {
err := syscall.PtraceSyscall(pid, signal)
e.Print(err)
} else {
err := syscall.PtraceCont(pid, signal)
e.Print(err)
}
}
func waitForSyscall() (pid, trapcause int, alive bool) {
var wstatus syscall.WaitStatus
for {
pid, err := syscall.Wait4(-1, &wstatus, syscall.WALL, nil)
e.Exit(err)
switch {
case wstatus.Exited(): // Normal exit.
return pid, wstatus.ExitStatus(), false
case wstatus.Signaled(): // Signal exit.
return pid, -int(wstatus.Signal()), false
case wstatus.StopSignal()&0x80 != 0: // Ptrace stop.
return pid, 0, true
case wstatus.TrapCause() > 0: // SIGTRAP stop.
return pid, wstatus.TrapCause(), true
default: // Another signal stop (e.g. SIGSTOP).
resume(pid, int(wstatus.StopSignal()), false)
}
}
}
func trace(tracee string, argv []string) (int, error) {
argv = append([]string{"--"}, argv...)
if withSeccomp {
argv = append([]string{"-seccomp"}, argv...)
}
cmd := exec.Command(tracee, argv...) //#nosec
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return 0, err
}
pid := cmd.Process.Pid
var wstatus syscall.WaitStatus
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
if err != nil {
return 0, err
}
if wstatus.Exited() {
return 0, errTraceeExited
}
err = syscall.PtraceSetOptions(pid, 0|
unix.PTRACE_O_EXITKILL|
unix.PTRACE_O_TRACESECCOMP|
syscall.PTRACE_O_TRACESYSGOOD|
syscall.PTRACE_O_TRACEEXEC|
syscall.PTRACE_O_TRACECLONE|
syscall.PTRACE_O_TRACEFORK|
syscall.PTRACE_O_TRACEVFORK)
if err != nil {
return 0, err
}
resume(pid, 0, false)
return pid, nil
}