-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathopen_tree_test.go
More file actions
303 lines (258 loc) · 8.33 KB
/
Copy pathopen_tree_test.go
File metadata and controls
303 lines (258 loc) · 8.33 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux && functionaltests
// Package tests holds tests related files
package tests
import (
"errors"
"fmt"
"os"
"os/exec"
"syscall"
"testing"
"time"
"github.com/moby/sys/mountinfo"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/rules"
"github.com/DataDog/datadog-agent/pkg/util/testutil/flake"
)
func openTreeIsSupported() bool {
_, _, errno := unix.Syscall6(unix.SYS_OPEN_TREE, 0, 0, 0, 0, 0, 0)
return !errors.Is(errno, syscall.ENOSYS)
}
func TmpMountAt(dir string) (int, error) {
openfd, err := unix.Fsopen("tmpfs", unix.FSOPEN_CLOEXEC)
if err != nil {
return 0, err
}
defer unix.Close(openfd)
_ = fsconfigStr(openfd, unix.FSCONFIG_SET_STRING, "source", "tmpfs", 0)
_ = fsconfigStr(openfd, unix.FSCONFIG_SET_STRING, "size", "1M", 0)
_ = fsconfig(openfd, unix.FSCONFIG_CMD_CREATE, nil, nil, 0)
mountfd, err := unix.Fsmount(openfd, unix.FSMOUNT_CLOEXEC, 0)
if err != nil {
return 0, err
}
if dir != "" {
err = unix.MoveMount(mountfd, "", unix.AT_FDCWD, dir, unix.MOVE_MOUNT_F_EMPTY_PATH)
if err != nil {
unix.Close(mountfd)
return 0, err
}
}
return mountfd, nil
}
func TmpMountFdAt(fd int, at string) (int, error) {
openfd, err := unix.Fsopen("tmpfs", unix.FSOPEN_CLOEXEC)
if err != nil {
return 0, err
}
_ = fsconfigStr(openfd, unix.FSCONFIG_SET_STRING, "source", "tmpfs", 0)
_ = fsconfigStr(openfd, unix.FSCONFIG_SET_STRING, "size", "1M", 0)
_ = fsconfig(openfd, unix.FSCONFIG_CMD_CREATE, nil, nil, 0)
mountfd, err := unix.Fsmount(openfd, unix.FSMOUNT_CLOEXEC, 0)
if err != nil {
return 0, err
}
err = unix.MoveMount(mountfd, "", fd, at, unix.MOVE_MOUNT_F_EMPTY_PATH)
if err != nil {
return 0, err
}
return mountfd, nil
}
func getMountID(path string) (uint32, error) {
var stat unix.Stat_t
if err := unix.Stat(path, &stat); err != nil {
return 0, fmt.Errorf("failed to stat %s: %w", path, err)
}
mounts, err := mountinfo.GetMounts(nil)
if err != nil {
return 0, fmt.Errorf("failed to get mounts: %w", err)
}
for _, mnt := range mounts {
mountDevice := unix.Mkdev(uint32(mnt.Major), uint32(mnt.Minor))
if mountDevice == stat.Dev {
return uint32(mnt.ID), nil
}
}
return 0, fmt.Errorf("mount not found for path %s", path)
}
func TestOpenTree(t *testing.T) {
SkipIfNotAvailable(t)
if !openTreeIsSupported() {
t.Skip("open_tree syscall is not supported on this platform")
}
execRules := []*rules.RuleDefinition{
{
ID: "test_rule1",
Expression: `exec.file.name == "true" && exec.file.mount_visible == false && exec.file.mount_detached == true`,
},
{
ID: "test_rule2",
Expression: `exec.file.name == "false" && exec.file.mount_visible == true && exec.file.mount_detached == false`,
},
}
mountRules := []*rules.RuleDefinition{
{
ID: "test_rule3",
Expression: `mount.detached == true && mount.visible == false`,
},
}
test, err := newTestModule(t, nil, mountRules)
if err != nil {
t.Fatal(err)
}
// Mount the following directory struct in /tmp:
// + /tmp/<tmpdir> (tmpfs, 1MB)
// |-- /tmp/<tmpdir>/tmp1 (tmpfs, 1MB)
// |-- /tmp/<tmpdir>/tmp2 (tmpfs, 1MB)
// In which `tmp1` and `tmp2` are have 001 as the parent mount
// This is using the new mount api, but could have been accomplished with the mount() syscall too
// because this isn't the part that we're testing
var tounmount []string
mountIDsToPath := make(map[uint32]string)
dir := t.TempDir()
tounmount = append(tounmount, dir)
fdRoot, err := TmpMountAt(dir)
if err != nil {
// Syscall not available in this kernel
t.Skip(err)
}
defer unix.Close(fdRoot)
if id, err := getMountID(dir); err != nil {
t.Fatal(err)
} else {
mountIDsToPath[id] = "/"
}
mountSubDir := func(subdir string) {
fullpath := dir + "/" + subdir
err = os.Mkdir(fullpath, 0755)
tounmount = append(tounmount, fullpath)
if err != nil {
t.Fatal(err)
}
fd, err := TmpMountAt(fullpath)
if err != nil {
t.Fatal(err)
}
defer unix.Close(fd)
if id, err := getMountID(fullpath); err != nil {
t.Fatal(err)
} else {
mountIDsToPath[id] = "/" + subdir
}
}
mountSubDir("tmp1")
mountSubDir("tmp2")
defer func() {
for i := len(tounmount) - 1; i >= 0; i-- {
err = unix.Unmount(tounmount[i], syscall.MNT_DETACH)
if err != nil {
t.Fatal(err)
}
}
}()
t.Run("copy-tree-test-detached-recursive", func(t *testing.T) {
flake.MarkOnJobName(t, "ubuntu_25.10")
seen := 0
err = test.GetProbeEvent(func() error {
fd, err := unix.OpenTree(0, dir, unix.OPEN_TREE_CLONE|unix.AT_RECURSIVE)
if err != nil {
t.Fatal(err)
}
defer unix.Close(fd)
return nil
}, func(event *model.Event) bool {
if event.GetType() != "mount" || event.Mount.Origin != model.MountOriginOpenTree {
return false
}
assert.NotEqual(t, uint32(0), event.Mount.BindSrcMountID, "mount id is zero")
assert.NotEmpty(t, event.GetMountMountpointPath(), "path is empty")
assert.Equal(t, mountIDsToPath[event.Mount.BindSrcMountID], event.GetMountMountpointPath(), "Wrong Path")
seen++
if seen == 1 {
assert.Equal(t, true, event.Mount.Detached, "First mount should be detached")
assert.Equal(t, false, event.Mount.Visible, "First mount shouldn't be visible")
} else {
assert.Equal(t, false, event.Mount.Detached, "Second and third mounts shouldn't be detached")
assert.Equal(t, false, event.Mount.Visible, "Second and third mounts shouldn't be visible")
}
return seen == 3
}, 10*time.Second, model.FileMountEventType)
assert.Equal(t, 3, seen)
})
t.Run("copy-tree-test-detached-non-recursive", func(t *testing.T) {
seen := 0
err = test.GetProbeEvent(func() error {
fd, err := unix.OpenTree(0, dir, unix.OPEN_TREE_CLONE)
if err != nil {
t.Fatal(err)
}
defer unix.Close(fd)
return nil
}, func(event *model.Event) bool {
if event.GetType() != "mount" && event.Mount.Origin != model.MountOriginOpenTree {
return false
}
seen++
assert.NotEqual(t, uint32(0), event.Mount.BindSrcMountID, "mount id is zero")
assert.NotEmpty(t, event.GetMountMountpointPath(), "path is empty")
assert.Equal(t, "/", event.GetMountMountpointPath(), "Wrong Path")
assert.Equal(t, true, event.Mount.Detached, "Mount should be detached")
assert.Equal(t, false, event.Mount.Visible, "Mount shouldn't be visible")
return seen == 1
}, 10*time.Second, model.FileMountEventType)
assert.Equal(t, 1, seen)
})
t.Run("detached-event-captured", func(t *testing.T) {
test.WaitSignalFromRule(t, func() error {
fd, err := unix.OpenTree(0, dir, unix.OPEN_TREE_CLONE)
if err != nil {
t.Fatal(err)
}
defer unix.Close(fd)
return nil
}, func(event *model.Event, _ *rules.Rule) {
assert.Equal(t, true, event.Mount.Detached, "Mount should be detached")
assert.Equal(t, false, event.Mount.Visible, "Mount shouldn't be visible")
}, "test_rule3")
})
test.Close()
test, err = newTestModule(t, nil, execRules)
if err != nil {
t.Fatal(err)
}
defer test.Close()
t.Run("execution-from-detached-mount", func(t *testing.T) {
srcPath := which(t, "true")
pid := os.Getpid()
fd, err := unix.OpenTree(0, dir, unix.OPEN_TREE_CLONE)
if err != nil {
t.Fatal(err)
}
destPath := fmt.Sprintf("/proc/%d/fd/%d/true", pid, fd)
_ = exec.Command("cp", srcPath, destPath).Run()
defer unix.Close(fd)
test.WaitSignalFromRule(t, func() error {
err = exec.Command(destPath).Run()
return nil
}, func(event *model.Event, _ *rules.Rule) {
assert.Equal(t, true, event.Exec.FileEvent.MountDetached, "Mount should be detached")
assert.Equal(t, false, event.Exec.FileEvent.MountVisible, "Mount shouldn't be visible")
}, "test_rule1")
})
t.Run("execution-from-visible-mount", func(t *testing.T) {
exePath, _ := exec.LookPath("false")
test.WaitSignalFromRule(t, func() error {
_ = exec.Command(exePath).Run()
return nil
}, func(event *model.Event, _ *rules.Rule) {
assert.Equal(t, false, event.Exec.FileEvent.MountDetached, "Mount should be detached")
assert.Equal(t, true, event.Exec.FileEvent.MountVisible, "Mount shouldn't be visible")
}, "test_rule2")
})
}