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
|
package main
import (
"fmt"
"runtime"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
const processExecEmitFor = 2 * time.Second
func processExecLifecycle() error {
deadline := time.Now().Add(processExecEmitFor)
for time.Now().Before(deadline) {
if err := callExecveMissing(); err != nil {
return err
}
if err := callExecveatMissing(); err != nil {
return err
}
time.Sleep(10 * time.Millisecond)
}
return nil
}
func callExecveMissing() error {
filename, err := syscall.BytePtrFromString("/tmp/ior-missing-execve-only")
if err != nil {
return fmt.Errorf("execve filename: %w", err)
}
argv := []uintptr{uintptr(unsafe.Pointer(filename)), 0}
envp := []uintptr{0}
_, _, errno := syscall.RawSyscall(
syscall.SYS_EXECVE,
uintptr(unsafe.Pointer(filename)),
uintptr(unsafe.Pointer(&argv[0])),
uintptr(unsafe.Pointer(&envp[0])),
)
runtime.KeepAlive(filename)
runtime.KeepAlive(argv)
runtime.KeepAlive(envp)
if errno != syscall.ENOENT {
return fmt.Errorf("execve errno=%v, want ENOENT", errno)
}
return nil
}
func callExecveatMissing() error {
filename, err := syscall.BytePtrFromString("ior-missing-execveat-only")
if err != nil {
return fmt.Errorf("execveat filename: %w", err)
}
argv := []uintptr{uintptr(unsafe.Pointer(filename)), 0}
envp := []uintptr{0}
dirfdSigned := int64(unix.AT_FDCWD)
dirfd := uintptr(dirfdSigned)
_, _, errno := syscall.RawSyscall6(
unix.SYS_EXECVEAT,
dirfd,
uintptr(unsafe.Pointer(filename)),
uintptr(unsafe.Pointer(&argv[0])),
uintptr(unsafe.Pointer(&envp[0])),
0,
0,
)
runtime.KeepAlive(filename)
runtime.KeepAlive(argv)
runtime.KeepAlive(envp)
if errno != syscall.ENOENT {
return fmt.Errorf("execveat errno=%v, want ENOENT", errno)
}
return nil
}
|