-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (125 loc) · 3.68 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
/*
#cgo LDFLAGS: -lseccomp
#include <seccomp.h>
#include <signal.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int *shared_mem;
// Function to set the shared memory pointer from Go
void set_shared_mem(void *mem) {
shared_mem = (int *)mem;
}
void *create_shared_memory(size_t size) {
return mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
}
// Signal handler for SIGSYS in the child
void sigsys_handler(int signum, siginfo_t *info, void *context) {
if (signum == SIGSYS) {
// Write the syscall number to shared memory (for example purposes)
printf("Syscall found: %d\n", info->si_syscall);
printf("Syscall Addr: %#018lx\n", (unsigned long)info->si_addr);
printf("Syscall Value at addr: %d\n", *(int *)(info->si_addr));
printf("sigsys_handler() shared_mem: %#018lx\n", (int *)context);
printf("Shared Memory: %#018lx\n", (int *)shared_mem);
printf("Value at addr: %d\n", *(shared_mem));
*shared_mem = info->si_syscall; // Store the intercepted syscall number
printf("Value at addr: %d\n", *(shared_mem));
printf("Writing syscall %d to shared memory...\n", info->si_syscall);
}
}
// Set up the seccomp filter
int setup_seccomp() {
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_ALLOW);
if (ctx == NULL) {
return -1;
}
seccomp_rule_add(ctx, SCMP_ACT_TRAP, SCMP_SYS(getpid), 0);
return seccomp_load(ctx);
}
// Set up the SIGSYS signal handler
void setup_sigsys_handler() {
printf("setup_sigsys_handler() shared_mem Addr: %#018lx\n", (int *)shared_mem);
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_sigaction = sigsys_handler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGSYS, &act, NULL);
}
*/
import "C"
import (
"fmt"
"os"
//"os/signal"
"syscall"
"time"
"unsafe"
)
const sharedMemSize = 4
var sharedMemPtr unsafe.Pointer
func main() {
// Create shared memory
sharedMemPtr = C.create_shared_memory(C.size_t(sharedMemSize))
fmt.Printf("Shared memory pointer in Go: %p\n", sharedMemPtr)
if sharedMemPtr == nil {
fmt.Println("Failed to create shared memory")
os.Exit(1)
}
// Set the shared memory pointer in C
C.set_shared_mem(sharedMemPtr)
// Fork a child process
pid, _, errno := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
if errno != 0 {
fmt.Printf("Fork failed: %v\n", errno)
os.Exit(1)
}
if pid == 0 {
// Child process
childProcess()
} else {
// Parent process
parentProcess(pid)
}
}
func parentProcess(pid uintptr) {
fmt.Println("Parent process started. Monitoring shared memory...")
// Wait for child to complete
//syscall.Wait4(int(pid), nil, 0, nil)
for {
syscallNum := *(*int)(sharedMemPtr)
fmt.Printf("Intercepted syscall number from child: %d\n", syscallNum)
if syscallNum == 39 {
parentPid := syscall.Getpid()
fmt.Printf("Parent performed Getpid(): %d\n", parentPid)
*(*int)(sharedMemPtr) = parentPid
fmt.Println(*(*int)(sharedMemPtr))
break
}
}
}
func childProcess() {
// Set up seccomp in the child process
if C.setup_seccomp() != 0 {
fmt.Println("Failed to set up seccomp")
os.Exit(1)
}
// Set up the SIGSYS handler
C.setup_sigsys_handler()
// Trigger a syscall to be intercepted
fmt.Println("Child process making a syscall to trigger SIGSYS...")
pid := syscall.Getpid()
fmt.Printf("Result of intercepted Getpid(): %d\n", pid)
// Read the result from shared memory (provided by parent)
for {
result := *(*int)(sharedMemPtr)
if result != 39 {
fmt.Printf("Result of intercepted Getpid() from parent: %d\n", result)
break
}
time.Sleep(10 * time.Millisecond)
}
// The program continues or exits after handling
}