Debian Codegraph @

link/link_windows_test.go

raw symbols SCIP
1 package link
2
3 import (
4 "os/exec"
5 "testing"
6
7 "github.com/go-quicktest/qt"
8 "golang.org/x/sys/windows"
9
10 "github.com/cilium/ebpf"
11 "github.com/cilium/ebpf/asm"
12 )
13
14 // ntosebpfext has not yet assigned a stable enum value so we can't refer to
15 // it via that (https://github.com/microsoft/ntosebpfext/issues/152).
16 //
17 // See https://github.com/microsoft/ntosebpfext/blob/75ceaac38a0254e44f3219852d79a336d10ad9f3/include/ebpf_ntos_program_attach_type_guids.h
18 var (
19 programTypeProcessGUID = makeGUID(0x22ea7b37, 0x1043, 0x4d0d, [8]byte{0xb6, 0x0d, 0xca, 0xfa, 0x1c, 0x7b, 0x63, 0x8e})
20 attachTypeProcessGUID = makeGUID(0x66e20687, 0x9805, 0x4458, [8]byte{0xa0, 0xdb, 0x38, 0xe2, 0x20, 0xd3, 0x16, 0x85})
21 )
22
23 func testLinkArch(t *testing.T, link Link) {}
24
25 func newRawLink(t *testing.T) (*RawLink, *ebpf.Program) {
26 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
27 Type: ebpf.WindowsBind,
28 Instructions: asm.Instructions{
29 asm.Mov.Imm(asm.R0, 0),
30 asm.Return(),
31 },
32 License: "MIT",
33 })
34 qt.Assert(t, qt.IsNil(err))
35 t.Cleanup(func() { prog.Close() })
36
37 link, err := AttachRawLink(RawLinkOptions{
38 Program: prog,
39 Attach: ebpf.AttachWindowsBind,
40 })
41 qt.Assert(t, qt.IsNil(err))
42 t.Cleanup(func() { link.Close() })
43
44 return link, prog
45 }
46
47 func TestProcessLink(t *testing.T) {
48 array, err := ebpf.NewMap(&ebpf.MapSpec{
49 Type: ebpf.WindowsArray,
50 Name: "process_state",
51 KeySize: 4,
52 ValueSize: 4,
53 MaxEntries: 1,
54 })
55 qt.Assert(t, qt.IsNil(err))
56 defer array.Close()
57
58 prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
59 Type: windowsProgramTypeForGUID(t, programTypeProcessGUID),
60 Name: "process_test",
61 Instructions: asm.Instructions{
62 // R1 = map
63 asm.LoadMapPtr(asm.R1, array.FD()),
64 // R2 = key
65 asm.Mov.Reg(asm.R2, asm.R10),
66 asm.Add.Imm(asm.R2, -4),
67 asm.StoreImm(asm.R2, 0, 0, asm.Word),
68 // R3 = value
69 asm.Mov.Reg(asm.R3, asm.R2),
70 asm.Add.Imm(asm.R3, -4),
71 asm.StoreImm(asm.R3, 0, 1, asm.Word),
72 // R4 = flags
73 asm.Mov.Imm(asm.R4, 0),
74 // bpf_map_update_elem(map, key, value, flags)
75 asm.WindowsFnMapUpdateElem.Call(),
76 asm.Mov.Imm(asm.R0, 0),
77 asm.Return(),
78 },
79 License: "MIT",
80 })
81 qt.Assert(t, qt.IsNil(err))
82 defer prog.Close()
83
84 link, err := AttachRawLink(RawLinkOptions{
85 Program: prog,
86 Attach: windowsAttachTypeForGUID(t, attachTypeProcessGUID),
87 })
88 qt.Assert(t, qt.IsNil(err))
89 defer link.Close()
90
91 qt.Assert(t, qt.IsNil(exec.Command("cmd.exe", "/c", "exit 0").Run()))
92
93 var value uint32
94 qt.Assert(t, qt.IsNil(array.Lookup(uint32(0), &value)))
95 qt.Assert(t, qt.Equals(value, 1), qt.Commentf("Executing a binary should trigger the program"))
96
97 qt.Assert(t, qt.IsNil(link.Close()))
98 }
99
100 func makeGUID(data1 uint32, data2 uint16, data3 uint16, data4 [8]byte) windows.GUID {
101 return windows.GUID{Data1: data1, Data2: data2, Data3: data3, Data4: data4}
102 }