File: mounted_linux_test.go

package info (click to toggle)
golang-github-moby-sys 0.0~git20201113.5a29239-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental
  • size: 460 kB
  • sloc: makefile: 35
file content (254 lines) | stat: -rw-r--r-- 5,755 bytes parent folder | download
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
package mountinfo

import (
	"errors"
	"io/ioutil"
	"net"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/sys/unix"
)

const (
	notMounted = "not-mounted"
)

func prepareMounts(t *testing.T) (dir string, mounts []string, err error) {
	dir, err = ioutil.TempDir("", t.Name())
	if err != nil {
		return
	}

	// A real (tmpfs) mount.
	mnt := filepath.Join(dir, "tmpfs-mount")
	err = os.Mkdir(mnt, 0750)
	if err != nil {
		return
	}

	err = unix.Mount("tmpfs", mnt, "tmpfs", 0, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	// A directory bind-mounted to itself.
	mnt = filepath.Join(dir, "bind-mount-dir")
	err = os.Mkdir(mnt, 0750)
	if err != nil {
		return
	}

	err = unix.Mount(mnt, mnt, "", unix.MS_BIND, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	// A directory bind-mounted to other directory.
	src := filepath.Join(dir, "some-dir")
	err = os.Mkdir(src, 0750)
	if err != nil {
		return
	}

	mnt = filepath.Join(dir, "bind-mounted-dir2")
	err = os.Mkdir(mnt, 0750)
	if err != nil {
		return
	}
	err = unix.Mount(src, mnt, "", unix.MS_BIND, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	// A regular file bind-mounted to itself.
	mnt = filepath.Join(dir, "bind-mount-file")
	err = ioutil.WriteFile(mnt, []byte(""), 0640)
	if err != nil {
		return
	}

	err = unix.Mount(mnt, mnt, "", unix.MS_BIND, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	// Not mounted socket.
	sock := filepath.Join(dir, notMounted+".sock")
	_, err = net.Listen("unix", sock)
	if err != nil {
		return
	}
	mounts = append(mounts, sock)

	// Bind-mounted socket.
	mnt = filepath.Join(dir, "bind-mounted-socket")
	err = ioutil.WriteFile(mnt, []byte(""), 0640)
	if err != nil {
		return
	}
	err = unix.Mount(sock, mnt, "", unix.MS_BIND, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	// Not mounted directory.
	mnt = filepath.Join(dir, notMounted+"-dir")
	err = os.Mkdir(mnt, 0750)
	if err != nil {
		return
	}
	mounts = append(mounts, mnt)

	// Not mounted file.
	mnt = filepath.Join(dir, notMounted+"-file")
	err = ioutil.WriteFile(mnt, []byte(""), 0640)
	if err != nil {
		return
	}
	mounts = append(mounts, mnt)

	// A broken not-mounted symlink.
	symlink := filepath.Join(dir, notMounted+"-broken-symlink")
	err = unix.Symlink("non-existent-dest", symlink)
	if err != nil {
		err = &os.PathError{Op: "symlink", Path: symlink, Err: err}
		return
	}
	mounts = append(mounts, symlink)

	// A valid not-mounted symlink.
	dst := filepath.Join(dir, "file")
	err = ioutil.WriteFile(dst, []byte(""), 0640)
	if err != nil {
		return
	}
	symlink = filepath.Join(dir, notMounted+"-valid-symlink")
	err = unix.Symlink(dst, symlink)
	if err != nil {
		err = &os.PathError{Op: "symlink", Path: symlink, Err: err}
		return
	}
	mounts = append(mounts, symlink)

	// A valid bind-mounted symlink
	mnt = filepath.Join(dir, "bind-mounted-symlink")
	err = ioutil.WriteFile(mnt, []byte(""), 0640)
	if err != nil {
		return
	}
	err = unix.Mount(symlink, mnt, "", unix.MS_BIND, "")
	if err != nil {
		err = &os.PathError{Op: "mount", Path: mnt, Err: err}
		return
	}
	mounts = append(mounts, mnt)

	return
}

func cleanupMounts(t *testing.T, dir string, mounts []string) {
	for _, m := range mounts {
		if strings.Contains(m, notMounted) {
			continue
		}
		err := unix.Unmount(m, unix.MNT_DETACH)
		if err != nil {
			t.Logf("can't umount %s: %v", m, err)
		}
	}
	if err := os.RemoveAll(dir); err != nil {
		t.Log(err)
	}
}

func TestMountedBy(t *testing.T) {
	if os.Getuid() != 0 {
		t.Skip("requires root")
	}

	dir, mounts, err := prepareMounts(t)
	defer cleanupMounts(t, dir, mounts)
	if err != nil {
		t.Fatalf("setup failed: %v", err)
	}

	checked := false
	for _, m := range mounts {
		exp := !strings.Contains(m, notMounted)

		mounted, err := mountedByMountinfo(m)
		if err != nil {
			t.Errorf("mountedByMountinfo(%q) error: %v", m, err)
		} else if mounted != exp {
			t.Errorf("mountedByMountinfo(%q): expected %v, got %v", m, exp, mounted)
		}

		checked = true
		mounted, err = mountedByOpenat2(m)
		if err != nil {
			t.Errorf("mountedByOpenat2(%q) error: %v", m, err)
		} else if mounted != exp {
			t.Errorf("mountedByOpenat2(%q): expected %v, got %v", m, exp, mounted)
		}

		mounted, err = mountedByStat(m)
		// mountedByStat can't detect bind mounts, returning
		// errNotSure in case it can't reliably detect the mount.
		if strings.Contains(m, "bind") {
			exp = false
		}
		if err != nil {
			t.Errorf("mountedByStat(%q) error: %v", m, err)
		} else {
			if mounted != exp {
				t.Errorf("mountedByStat(%q): expected %v, got %v", m, exp, mounted)
			}
		}
	}
	if !checked {
		t.Skip("no mounts to check can be found")
	}
}

func TestMountedByOpenat2VsMountinfo(t *testing.T) {
	fd, err := unix.Openat2(unix.AT_FDCWD, ".", &unix.OpenHow{Flags: unix.O_RDONLY})
	if err != nil {
		t.Skipf("openat2: %v (old kernel? need Linux 5.6+)", err)
	}
	unix.Close(fd)

	mounts, err := GetMounts(nil)
	if err != nil {
		t.Fatalf("GetMounts error: %v", err)
	}

	for _, mount := range mounts {
		m := mount.Mountpoint
		if m == "/" {
			// mountedBy*() won't work for /, so skip it
			// (this special case is handled by Mounted).
			continue
		}
		mounted, err := mountedByOpenat2(m)
		if err != nil {
			if !errors.Is(err, os.ErrPermission) {
				t.Errorf("mountedByOpenat2(%q) error: %+v", m, err)
			}
		} else if !mounted {
			t.Errorf("mountedByOpenat2(%q): expected true, got false", m)
		}
	}
}