-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathreplace_unix.go
34 lines (28 loc) · 946 Bytes
/
replace_unix.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
//go:build !windows
// +build !windows
package monkey
import (
"syscall"
)
func mprotectCrossPage(addr uintptr, length int, prot int) {
pageSize := syscall.Getpagesize()
for p := pageStart(addr); p < addr+uintptr(length); p += uintptr(pageSize) {
page := rawMemoryAccess(p, pageSize)
err := syscall.Mprotect(page, prot)
if err != nil {
panic(err)
}
}
}
// this function is super unsafe
// aww yeah
// It copies a slice to a raw memory location, disabling all memory protection before doing so.
func copyToLocation(location uintptr, data []byte) {
f := rawMemoryAccess(location, len(data))
mprotectCrossPage(location, len(data), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
copy(f, data[:])
mprotectCrossPage(location, len(data), syscall.PROT_READ|syscall.PROT_EXEC)
}
func allowExec(location uintptr, length int) {
mprotectCrossPage(location, length, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
}