-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
167 lines (142 loc) · 3.69 KB
/
main.go
File metadata and controls
167 lines (142 loc) · 3.69 KB
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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"syscall"
libseccomp "github.com/seccomp/libseccomp-golang"
"golang.org/x/sys/unix"
)
// SyscallHandler defines the handler function for a syscall notification.
type SyscallHandler func(fd libseccomp.ScmpFd, req *libseccomp.ScmpNotifReq) (val uint64, errno int32, flags uint32)
func initSeccomp() chan<- struct{} {
api, err := libseccomp.GetAPI()
if err != nil {
fmt.Println("Failed to get seccomp API level")
os.Exit(1)
} else if api < 5 {
fmt.Printf("Need seccomp API level >= 5; it's currently %d\n", api)
os.Exit(1)
}
fd, err := LoadFilter()
if err != nil {
fmt.Printf("Failed to load seccomp filter: %v\n", err)
os.Exit(1)
}
fmt.Printf("Seccomp filter loaded with notification FD: %v\n", fd)
handlers := map[string]SyscallHandler{"connect": HandleConnect}
stop, errChan := Handle(fd, handlers)
go func() {
for err := range errChan {
fmt.Printf("Error in syscall monitoring: %v\n", err)
os.Exit(1)
}
}()
return stop
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: sockstrace <program> [args...]")
os.Exit(1)
}
stop := initSeccomp()
defer close(stop)
runProgram(args[0], args[1:])
}
func runProgram(program string, args []string) {
fmt.Printf("Executing program: %s\n", program)
cmd := exec.Command(program, args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("Error executing program: %v\n", err)
os.Exit(1)
}
}
func LoadFilter() (libseccomp.ScmpFd, error) {
filter, err := libseccomp.NewFilter(libseccomp.ActAllow)
if err != nil {
return 0, err
}
syscallID, err := libseccomp.GetSyscallFromName("connect")
if err != nil {
return 0, err
}
if err := filter.AddRule(syscallID, libseccomp.ActNotify); err != nil {
return 0, err
}
if err := filter.Load(); err != nil {
return 0, err
}
fd, err := filter.GetNotifFd()
if err != nil {
return 0, err
}
return fd, nil
}
func Handle(fd libseccomp.ScmpFd, handlers map[string]SyscallHandler) (chan<- struct{}, <-chan error) {
stop := make(chan struct{})
errChan := make(chan error)
go func() {
for {
req, err := libseccomp.NotifReceive(fd)
if err != nil {
if err == syscall.ENOENT {
fmt.Printf("Notification no longer valid: %v\n", err)
continue
}
fmt.Printf("Failed to receive notification: %v\n", err)
errChan <- err
if err == unix.ECANCELED {
return
}
continue
}
select {
case <-stop:
_ = libseccomp.NotifRespond(fd, &libseccomp.ScmpNotifResp{
ID: req.ID,
Error: int32(unix.EPERM),
Val: 0,
Flags: 0,
})
return
default:
}
err = libseccomp.NotifIDValid(fd, req.ID)
if err != nil {
fmt.Printf("Failed to validate notification ID: %v\n", err)
}
go func(req *libseccomp.ScmpNotifReq) {
syscallName, _ := req.Data.Syscall.GetName()
handler, ok := handlers[syscallName]
if !ok {
fmt.Printf("Unknown syscall: %s (PID: %d)\n", syscallName, req.Pid)
_ = libseccomp.NotifRespond(fd, &libseccomp.ScmpNotifResp{
ID: req.ID,
Error: int32(unix.ENOSYS),
Val: 0,
Flags: 0,
})
return
}
val, errno, flags := handler(fd, req)
if err := libseccomp.NotifRespond(fd, &libseccomp.ScmpNotifResp{
ID: req.ID,
Error: errno,
Val: val,
Flags: flags,
}); err != nil {
errChan <- err
}
}(req)
}
}()
return stop, errChan
}
func HandleConnect(seccompNotifFd libseccomp.ScmpFd, req *libseccomp.ScmpNotifReq) (uint64, int32, uint32) {
fmt.Printf("Intercepted 'connect' syscall from PID %d\n", req.Pid)
return 0, 0, unix.SECCOMP_USER_NOTIF_FLAG_CONTINUE
}