Debian Codegraph @

mountinfo/mounted_linux_test.go

raw symbols SCIP
1 package mountinfo
2
3 import (
4 "errors"
5 "net"
6 "os"
7 "path/filepath"
8 "reflect"
9 "runtime"
10 "strings"
11 "testing"
12
13 "golang.org/x/sys/unix"
14 )
15
16 // tMount is a wrapper for unix.Mount which is used to prepare test cases.
17 // It skips the test case if mounting is not possible (i.e. user is not root),
18 // fails the test case if there is a mount error, and installs a cleanup
19 // handler to undo the mount.
20 func tMount(t *testing.T, src, dst, fstype string, flags uintptr) {
21 t.Helper()
22 if os.Getuid() != 0 {
23 t.Skip("root required for mounting")
24 }
25
26 err := unix.Mount(src, dst, fstype, flags, "")
27 if err != nil {
28 t.Fatal(&os.PathError{Path: dst, Op: "mount", Err: err})
29 }
30 t.Cleanup(func() {
31 if err := unix.Unmount(dst, unix.MNT_DETACH); err != nil {
32 t.Errorf("cleanup: unmount %q failed: %v", dst, err)
33 }
34 })
35 }
36
37 func tCreateFile(t *testing.T, dir string) string {
38 t.Helper()
39 file, err := os.CreateTemp(dir, "file")
40 if err != nil {
41 t.Fatal(err)
42 }
43 file.Close()
44 return file.Name()
45 }
46
47 type testMount struct {
48 desc string
49 isNotExist bool
50 isMount bool
51 isBind bool
52 // prepare returns a path that needs to be checked.
53 // If there is an error preparing the path, the test is either skipped or failed.
54 //
55 // It is responsible for cleanup (by using t.Cleanup).
56 prepare func(t *testing.T) string
57 }
58
59 var testMounts = []testMount{
60 {
61 desc: "non-existent path",
62 isNotExist: true,
63 prepare: func(t *testing.T) string {
64 return "/non/existent/path"
65 },
66 },
67 {
68 desc: "not mounted directory",
69 prepare: func(t *testing.T) string {
70 return t.TempDir()
71 },
72 },
73 {
74 desc: "tmpfs mount",
75 isMount: true,
76 prepare: func(t *testing.T) string {
77 mnt := t.TempDir()
78 tMount(t, "tmpfs", mnt, "tmpfs", 0)
79 return mnt
80 },
81 },
82 {
83 desc: "tmpfs mount ending with a slash",
84 isMount: true,
85 prepare: func(t *testing.T) string {
86 mnt := t.TempDir() + "/"
87 tMount(t, "tmpfs", mnt, "tmpfs", 0)
88 return mnt
89 },
90 },
91 {
92 desc: "broken symlink",
93 isNotExist: true,
94 prepare: func(t *testing.T) string {
95 link := filepath.Join(t.TempDir(), "broken-symlink")
96 err := os.Symlink("/some/non/existent/dest", link)
97 if err != nil {
98 t.Fatal(err)
99 }
100 return link
101 },
102 },
103 {
104 desc: "symlink to not mounted directory",
105 prepare: func(t *testing.T) string {
106 tmp := t.TempDir()
107
108 t.Helper()
109 dir := filepath.Join(tmp, "dir")
110 err := os.Mkdir(dir, 0o700)
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 link := filepath.Join(tmp, "symlink")
116 err = os.Symlink(dir, link)
117 if err != nil {
118 t.Fatal(err)
119 }
120
121 return link
122 },
123 },
124 {
125 desc: "symlink to mounted directory",
126 isMount: true,
127 prepare: func(t *testing.T) string {
128 tmp := t.TempDir()
129
130 t.Helper()
131 dir := filepath.Join(tmp, "dir")
132 err := os.Mkdir(dir, 0o700)
133 if err != nil {
134 t.Fatal(err)
135 }
136
137 tMount(t, "tmpfs", dir, "tmpfs", 0)
138
139 link := filepath.Join(tmp, "symlink")
140 err = os.Symlink(dir, link)
141 if err != nil {
142 t.Fatal(err)
143 }
144
145 return link
146 },
147 },
148 {
149 desc: "symlink to a file on a different filesystem",
150 isMount: false,
151 prepare: func(t *testing.T) string {
152 tmp := t.TempDir()
153
154 t.Helper()
155 mnt := filepath.Join(tmp, "dir")
156 err := os.Mkdir(mnt, 0o700)
157 if err != nil {
158 t.Fatal(err)
159 }
160
161 tMount(t, "tmpfs", mnt, "tmpfs", 0)
162 file := tCreateFile(t, mnt)
163 link := filepath.Join(tmp, "link")
164 err = os.Symlink(file, link)
165 if err != nil {
166 t.Fatal(err)
167 }
168
169 return link
170 },
171 },
172 {
173 desc: "path whose parent is a symlink to directory on another device",
174 isMount: false,
175 prepare: func(t *testing.T) string {
176 tmp := t.TempDir()
177
178 t.Helper()
179 mnt := filepath.Join(tmp, "dir")
180 err := os.Mkdir(mnt, 0o700)
181 if err != nil {
182 t.Fatal(err)
183 }
184
185 tMount(t, "tmpfs", mnt, "tmpfs", 0)
186 file := tCreateFile(t, mnt)
187 // Create link -> mnt under tmp dir.
188 link := filepath.Join(tmp, "link")
189 err = os.Symlink(filepath.Base(mnt), link)
190 if err != nil {
191 t.Fatal(err)
192 }
193 // Path to check is /<tmp>/link/file.
194 return filepath.Join(link, filepath.Base(file))
195 },
196 },
197 {
198 desc: "directory bind mounted to itself",
199 isMount: true,
200 isBind: true,
201 prepare: func(t *testing.T) string {
202 mnt := t.TempDir()
203 tMount(t, mnt, mnt, "", unix.MS_BIND)
204 return mnt
205 },
206 },
207 {
208 desc: "directory bind-mounted to other directory",
209 isMount: true,
210 isBind: true,
211 prepare: func(t *testing.T) string {
212 mnt := t.TempDir()
213 tMount(t, t.TempDir(), mnt, "", unix.MS_BIND)
214 return mnt
215 },
216 },
217 {
218 desc: "not mounted file",
219 prepare: func(t *testing.T) string {
220 return tCreateFile(t, t.TempDir())
221 },
222 },
223 {
224 desc: "regular file bind-mounted to itself",
225 isMount: true,
226 isBind: true,
227 prepare: func(t *testing.T) string {
228 file := tCreateFile(t, t.TempDir())
229 tMount(t, file, file, "", unix.MS_BIND)
230 return file
231 },
232 },
233 {
234 desc: "not mounted socket",
235 prepare: func(t *testing.T) string {
236 path := filepath.Join(t.TempDir(), "sock")
237 _, err := net.Listen("unix", path)
238 if err != nil {
239 t.Helper()
240 t.Fatal(err)
241 }
242 return path
243 },
244 },
245 {
246 desc: "socket bind-mounted to itself",
247 isMount: true,
248 isBind: true,
249 prepare: func(t *testing.T) string {
250 path := filepath.Join(t.TempDir(), "sock")
251 _, err := net.Listen("unix", path)
252 if err != nil {
253 t.Helper()
254 t.Fatal(err)
255 }
256 tMount(t, path, path, "", unix.MS_BIND)
257
258 return path
259 },
260 },
261 }
262
263 func requireOpenat2(t *testing.T) {
264 t.Helper()
265 if err := tryOpenat2(); err != nil {
266 t.Skipf("openat2: %v (old kernel? need Linux 5.6+)", err)
267 }
268 }
269
270 func tryOpenat2() error {
271 fd, err := unix.Openat2(unix.AT_FDCWD, ".", &unix.OpenHow{Flags: unix.O_RDONLY})
272 if err == nil {
273 _ = unix.Close(fd)
274 }
275 return err
276 }
277
278 func testMountedFast(t *testing.T, path string, tc *testMount, openat2Supported bool) {
279 mounted, sure, err := MountedFast(path)
280 if err != nil {
281 // Got an error; is it expected?
282 if !tc.isNotExist || !errors.Is(err, os.ErrNotExist) {
283 t.Errorf("MountedFast: unexpected error: %v", err)
284 }
285
286 // In case of an error, sure and mounted must be false.
287 if sure {
288 t.Error("MountedFast: expected sure to be false on error")
289 }
290 if mounted {
291 t.Error("MountedFast: expected mounted to be false on error")
292 }
293
294 // No more checks.
295 return
296 }
297
298 if openat2Supported {
299 if mounted != tc.isMount {
300 t.Errorf("MountedFast: expected mounted to be %v, got %v", tc.isMount, mounted)
301 }
302
303 // No more checks.
304 return
305 }
306
307 if tc.isBind {
308 // For bind mounts, in case openat2 is not supported,
309 // sure and mounted must be false.
310 if sure {
311 t.Error("MountedFast: expected sure to be false for a bind mount")
312 }
313 if mounted {
314 t.Error("MountedFast: expected mounted to be false for a bind mount")
315 }
316 } else {
317 if mounted != tc.isMount {
318 t.Errorf("MountFast: expected mounted to be %v, got %v", tc.isMount, mounted)
319 }
320 if tc.isMount && !sure {
321 t.Error("MountFast: expected sure to be true for normal mount")
322 }
323 if !tc.isMount && sure {
324 t.Error("MountFast: expected sure to be false for non-mount")
325 }
326 }
327 }
328
329 func TestMountedBy(t *testing.T) {
330 checked := false
331 openat2Supported := false
332
333 // List of individual implementations to check.
334 toCheck := []func(string) (bool, error){mountedByMountinfo, mountedByStat}
335 if tryOpenat2() == nil {
336 openat2Supported = true
337 toCheck = append(toCheck, mountedByOpenat2)
338 }
339
340 for _, tc := range testMounts {
341 tc := tc
342 t.Run(tc.desc, func(t *testing.T) {
343 m := tc.prepare(t)
344
345 // Check the public Mounted() function as a whole.
346 mounted, err := Mounted(m)
347 if err == nil {
348 if mounted != tc.isMount {
349 t.Errorf("Mounted: expected %v, got %v", tc.isMount, mounted)
350 }
351 } else {
352 // Got an error; is it expected?
353 if !tc.isNotExist || !errors.Is(err, os.ErrNotExist) {
354 t.Errorf("Mounted: unexpected error: %v", err)
355 }
356 // Check false is returned in error case.
357 if mounted {
358 t.Error("Mounted: expected false on error")
359 }
360 }
361
362 // Check the public MountedFast() function as a whole.
363 testMountedFast(t, m, &tc, openat2Supported)
364
365 // Check individual mountedBy* implementations.
366
367 // All mountedBy* functions should be called with normalized paths.
368 m, err = normalizePath(m)
369 if err != nil {
370 if tc.isNotExist && errors.Is(err, os.ErrNotExist) {
371 return
372 }
373 t.Fatalf("normalizePath: %v", err)
374 }
375
376 for _, fn := range toCheck {
377 // Figure out function name.
378 name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
379
380 mounted, err = fn(m)
381 if err != nil {
382 t.Errorf("%s: %v", name, err)
383 // Check false is returned in error case.
384 if mounted {
385 t.Errorf("%s: expected false on error", name)
386 }
387 } else if mounted != tc.isMount {
388 if tc.isBind && strings.HasSuffix(name, "mountedByStat") {
389 // mountedByStat can not detect bind mounts.
390 } else {
391 t.Errorf("%s: expected %v, got %v", name, tc.isMount, mounted)
392 }
393 }
394 checked = true
395 }
396 })
397 }
398
399 if !checked {
400 t.Skip("no mounts to check")
401 }
402 }
403
404 func TestMountedByOpenat2VsMountinfo(t *testing.T) {
405 requireOpenat2(t)
406
407 mounts, err := GetMounts(nil)
408 if err != nil {
409 t.Fatalf("GetMounts error: %v", err)
410 }
411
412 for _, mount := range mounts {
413 m := mount.Mountpoint
414 if m == "/" {
415 // mountedBy*() won't work for /, so skip it
416 // (this special case is handled by Mounted).
417 continue
418 }
419 mounted, err := mountedByOpenat2(m)
420 if err != nil {
421 if !errors.Is(err, os.ErrPermission) {
422 t.Errorf("mountedByOpenat2(%q) error: %+v", m, err)
423 }
424 } else if !mounted {
425 t.Errorf("mountedByOpenat2(%q): expected true, got false", m)
426 }
427 }
428 }
429
430 // TestMountedRoot checks that Mounted* functions always return true for root
431 // directory (since / is always mounted).
432 func TestMountedRoot(t *testing.T) {
433 for _, path := range []string{
434 "/",
435 "/../../",
436 "/tmp/..",
437 strings.Repeat("../", unix.PathMax/3), // Hope $CWD is not too deep down.
438 } {
439 mounted, err := Mounted(path)
440 if err != nil || !mounted {
441 t.Errorf("Mounted(%q): expected true, <nil>; got %v, %v", path, mounted, err)
442 }
443
444 mounted, sure, err := MountedFast(path)
445 if err != nil || !mounted || !sure {
446 t.Errorf("MountedFast(%q): expected true, true, <nil>; got %v, %v, %v", path, mounted, sure, err)
447 }
448 }
449 }