summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/host/fs_test.go
blob: 44db61ecd9e590fb7c79b1a55f7b68d4c02ffa3a (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package host

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"reflect"
	"sort"
	"testing"

	"gvisor.googlesource.com/gvisor/pkg/sentry/context"
	"gvisor.googlesource.com/gvisor/pkg/sentry/context/contexttest"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
)

// newTestMountNamespace creates a MountNamespace with a ramfs root.
// It returns the host folder created, which should be removed when done.
func newTestMountNamespace(t *testing.T) (*fs.MountNamespace, string, error) {
	p, err := ioutil.TempDir("", "root")
	if err != nil {
		return nil, "", err
	}

	fd, err := open(nil, p)
	if err != nil {
		os.RemoveAll(p)
		return nil, "", err
	}
	ctx := contexttest.Context(t)
	root, err := newInode(ctx, newMountSource(ctx, p, fs.RootOwner, &Filesystem{}, fs.MountSourceFlags{}, false), fd, false, false)
	if err != nil {
		os.RemoveAll(p)
		return nil, "", err
	}
	mm, err := fs.NewMountNamespace(ctx, root)
	if err != nil {
		os.RemoveAll(p)
		return nil, "", err
	}
	return mm, p, nil
}

// createTestDirs populates the root with some test files and directories.
// /a/a1.txt
// /a/a2.txt
// /b/b1.txt
// /b/c/c1.txt
// /symlinks/normal.txt
// /symlinks/to_normal.txt -> /symlinks/normal.txt
// /symlinks/recursive -> /symlinks
func createTestDirs(ctx context.Context, t *testing.T, m *fs.MountNamespace) error {
	r := m.Root()
	defer r.DecRef()

	if err := r.CreateDirectory(ctx, r, "a", fs.FilePermsFromMode(0777)); err != nil {
		return err
	}

	a, err := r.Walk(ctx, r, "a")
	if err != nil {
		return err
	}
	defer a.DecRef()

	a1, err := a.Create(ctx, r, "a1.txt", fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
	if err != nil {
		return err
	}
	a1.DecRef()

	a2, err := a.Create(ctx, r, "a2.txt", fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
	if err != nil {
		return err
	}
	a2.DecRef()

	if err := r.CreateDirectory(ctx, r, "b", fs.FilePermsFromMode(0777)); err != nil {
		return err
	}

	b, err := r.Walk(ctx, r, "b")
	if err != nil {
		return err
	}
	defer b.DecRef()

	b1, err := b.Create(ctx, r, "b1.txt", fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
	if err != nil {
		return err
	}
	b1.DecRef()

	if err := b.CreateDirectory(ctx, r, "c", fs.FilePermsFromMode(0777)); err != nil {
		return err
	}

	c, err := b.Walk(ctx, r, "c")
	if err != nil {
		return err
	}
	defer c.DecRef()

	c1, err := c.Create(ctx, r, "c1.txt", fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
	if err != nil {
		return err
	}
	c1.DecRef()

	if err := r.CreateDirectory(ctx, r, "symlinks", fs.FilePermsFromMode(0777)); err != nil {
		return err
	}

	symlinks, err := r.Walk(ctx, r, "symlinks")
	if err != nil {
		return err
	}
	defer symlinks.DecRef()

	normal, err := symlinks.Create(ctx, r, "normal.txt", fs.FileFlags{Read: true, Write: true}, fs.FilePermsFromMode(0666))
	if err != nil {
		return err
	}
	normal.DecRef()

	if err := symlinks.CreateLink(ctx, r, "/symlinks/normal.txt", "to_normal.txt"); err != nil {
		return err
	}

	return symlinks.CreateLink(ctx, r, "/symlinks", "recursive")
}

// allPaths returns a slice of all paths of entries visible in the rootfs.
func allPaths(ctx context.Context, t *testing.T, m *fs.MountNamespace, base string) ([]string, error) {
	var paths []string
	root := m.Root()
	defer root.DecRef()

	maxTraversals := uint(1)
	d, err := m.FindLink(ctx, root, nil, base, &maxTraversals)
	if err != nil {
		t.Logf("FindLink failed for %q", base)
		return paths, err
	}
	defer d.DecRef()

	if fs.IsDir(d.Inode.StableAttr) {
		dir, err := d.Inode.GetFile(ctx, d, fs.FileFlags{Read: true})
		if err != nil {
			return nil, fmt.Errorf("failed to open directory %q: %v", base, err)
		}
		iter, ok := dir.FileOperations.(fs.DirIterator)
		if !ok {
			return nil, fmt.Errorf("cannot directly iterate on host directory %q", base)
		}
		dirCtx := &fs.DirCtx{
			Serializer: noopDentrySerializer{},
		}
		if _, err := fs.DirentReaddir(ctx, d, iter, root, dirCtx, 0); err != nil {
			return nil, err
		}
		for name := range dirCtx.DentAttrs() {
			if name == "." || name == ".." {
				continue
			}

			fullName := path.Join(base, name)
			paths = append(paths, fullName)

			// Recurse.
			subpaths, err := allPaths(ctx, t, m, fullName)
			if err != nil {
				return paths, err
			}
			paths = append(paths, subpaths...)
		}
	}

	return paths, nil
}

type noopDentrySerializer struct{}

func (noopDentrySerializer) CopyOut(string, fs.DentAttr) error {
	return nil
}
func (noopDentrySerializer) Written() int {
	return 4096
}

// pathsEqual returns true if the two string slices contain the same entries.
func pathsEqual(got, want []string) bool {
	sort.Strings(got)
	sort.Strings(want)

	if len(got) != len(want) {
		return false
	}

	for i := range got {
		if got[i] != want[i] {
			return false
		}
	}

	return true
}

func TestWhitelist(t *testing.T) {
	for _, test := range []struct {
		// description of the test.
		desc string
		// paths are the paths to whitelist
		paths []string
		// want are all of the directory entries that should be
		// visible (nothing beyond this set should be visible).
		want []string
	}{
		{
			desc:  "root",
			paths: []string{"/"},
			want:  []string{"/a", "/a/a1.txt", "/a/a2.txt", "/b", "/b/b1.txt", "/b/c", "/b/c/c1.txt", "/symlinks", "/symlinks/normal.txt", "/symlinks/to_normal.txt", "/symlinks/recursive"},
		},
		{
			desc:  "top-level directories",
			paths: []string{"/a", "/b"},
			want:  []string{"/a", "/a/a1.txt", "/a/a2.txt", "/b", "/b/b1.txt", "/b/c", "/b/c/c1.txt"},
		},
		{
			desc:  "nested directories (1/2)",
			paths: []string{"/b", "/b/c"},
			want:  []string{"/b", "/b/b1.txt", "/b/c", "/b/c/c1.txt"},
		},
		{
			desc:  "nested directories (2/2)",
			paths: []string{"/b/c", "/b"},
			want:  []string{"/b", "/b/b1.txt", "/b/c", "/b/c/c1.txt"},
		},
		{
			desc:  "single file",
			paths: []string{"/b/c/c1.txt"},
			want:  []string{"/b", "/b/c", "/b/c/c1.txt"},
		},
		{
			desc:  "single file and directory",
			paths: []string{"/a/a1.txt", "/b/c"},
			want:  []string{"/a", "/a/a1.txt", "/b", "/b/c", "/b/c/c1.txt"},
		},
		{
			desc:  "symlink",
			paths: []string{"/symlinks/to_normal.txt"},
			want:  []string{"/symlinks", "/symlinks/normal.txt", "/symlinks/to_normal.txt"},
		},
		{
			desc:  "recursive symlink",
			paths: []string{"/symlinks/recursive/normal.txt"},
			want:  []string{"/symlinks", "/symlinks/normal.txt", "/symlinks/recursive"},
		},
	} {
		t.Run(test.desc, func(t *testing.T) {
			m, p, err := newTestMountNamespace(t)
			if err != nil {
				t.Errorf("Failed to create MountNamespace: %v", err)
			}
			defer os.RemoveAll(p)

			ctx := withRoot(contexttest.RootContext(t), m.Root())
			if err := createTestDirs(ctx, t, m); err != nil {
				t.Errorf("Failed to create test dirs: %v", err)
			}

			if err := installWhitelist(ctx, m, test.paths); err != nil {
				t.Errorf("installWhitelist(%v) err got %v want nil", test.paths, err)
			}

			got, err := allPaths(ctx, t, m, "/")
			if err != nil {
				t.Fatalf("Failed to lookup paths (whitelisted: %v): %v", test.paths, err)
			}

			if !pathsEqual(got, test.want) {
				t.Errorf("For paths %v got %v want %v", test.paths, got, test.want)
			}
		})
	}
}

func TestRootPath(t *testing.T) {
	// Create a temp dir, which will be the root of our mounted fs.
	rootPath, err := ioutil.TempDir(os.TempDir(), "root")
	if err != nil {
		t.Fatalf("TempDir failed: %v", err)
	}
	defer os.RemoveAll(rootPath)

	// Create two files inside the new root, one which will be whitelisted
	// and one not.
	whitelisted, err := ioutil.TempFile(rootPath, "white")
	if err != nil {
		t.Fatalf("TempFile failed: %v", err)
	}
	if _, err := ioutil.TempFile(rootPath, "black"); err != nil {
		t.Fatalf("TempFile failed: %v", err)
	}

	// Create a mount with a root path and single whitelisted file.
	hostFS := &Filesystem{}
	ctx := contexttest.Context(t)
	data := fmt.Sprintf("%s=%s,%s=%s", rootPathKey, rootPath, whitelistKey, whitelisted.Name())
	inode, err := hostFS.Mount(ctx, "", fs.MountSourceFlags{}, data)
	if err != nil {
		t.Fatalf("Mount failed: %v", err)
	}
	mm, err := fs.NewMountNamespace(ctx, inode)
	if err != nil {
		t.Fatalf("NewMountNamespace failed: %v", err)
	}
	if err := hostFS.InstallWhitelist(ctx, mm); err != nil {
		t.Fatalf("InstallWhitelist failed: %v", err)
	}

	// Get the contents of the root directory.
	rootDir := mm.Root()
	rctx := withRoot(ctx, rootDir)
	f, err := rootDir.Inode.GetFile(rctx, rootDir, fs.FileFlags{})
	if err != nil {
		t.Fatalf("GetFile failed: %v", err)
	}
	c := &fs.CollectEntriesSerializer{}
	if err := f.Readdir(rctx, c); err != nil {
		t.Fatalf("Readdir failed: %v", err)
	}

	// We should have only our whitelisted file, plus the dots.
	want := []string{path.Base(whitelisted.Name()), ".", ".."}
	got := c.Order
	sort.Strings(want)
	sort.Strings(got)
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Readdir got %v, wanted %v", got, want)
	}
}

type rootContext struct {
	context.Context
	root *fs.Dirent
}

// withRoot returns a copy of ctx with the given root.
func withRoot(ctx context.Context, root *fs.Dirent) context.Context {
	return &rootContext{
		Context: ctx,
		root:    root,
	}
}

// Value implements Context.Value.
func (rc rootContext) Value(key interface{}) interface{} {
	switch key {
	case fs.CtxRoot:
		rc.root.IncRef()
		return rc.root
	default:
		return rc.Context.Value(key)
	}
}