summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/fdpipe/pipe_opener_test.go
blob: e556da48a002f50026dfa767463d56e2769241ce (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright 2018 The gVisor Authors.
//
// 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 fdpipe

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path"
	"syscall"
	"testing"
	"time"

	"github.com/google/uuid"

	"gvisor.dev/gvisor/pkg/context"
	"gvisor.dev/gvisor/pkg/fd"
	"gvisor.dev/gvisor/pkg/sentry/contexttest"
	"gvisor.dev/gvisor/pkg/sentry/fs"
	"gvisor.dev/gvisor/pkg/syserror"
	"gvisor.dev/gvisor/pkg/usermem"
)

type hostOpener struct {
	name string
}

func (h *hostOpener) NonBlockingOpen(_ context.Context, p fs.PermMask) (*fd.FD, error) {
	var flags int
	switch {
	case p.Read && p.Write:
		flags = syscall.O_RDWR
	case p.Write:
		flags = syscall.O_WRONLY
	case p.Read:
		flags = syscall.O_RDONLY
	default:
		return nil, syscall.EINVAL
	}
	f, err := syscall.Open(h.name, flags|syscall.O_NONBLOCK, 0666)
	if err != nil {
		return nil, err
	}
	return fd.New(f), nil
}

func pipename() string {
	return fmt.Sprintf(path.Join(os.TempDir(), "test-named-pipe-%s"), uuid.New())
}

func mkpipe(name string) error {
	return syscall.Mknod(name, syscall.S_IFIFO|0666, 0)
}

func TestTryOpen(t *testing.T) {
	for _, test := range []struct {
		// desc is the test's description.
		desc string

		// makePipe is true if the test case should create the pipe.
		makePipe bool

		// flags are the fs.FileFlags used to open the pipe.
		flags fs.FileFlags

		// expectFile is true if a fs.File is expected.
		expectFile bool

		// err is the expected error
		err error
	}{
		{
			desc:       "FileFlags lacking Read and Write are invalid",
			makePipe:   false,
			flags:      fs.FileFlags{}, /* bogus */
			expectFile: false,
			err:        syscall.EINVAL,
		},
		{
			desc:       "NonBlocking Read only error returns immediately",
			makePipe:   false, /* causes the error */
			flags:      fs.FileFlags{Read: true, NonBlocking: true},
			expectFile: false,
			err:        syscall.ENOENT,
		},
		{
			desc:       "NonBlocking Read only success returns immediately",
			makePipe:   true,
			flags:      fs.FileFlags{Read: true, NonBlocking: true},
			expectFile: true,
			err:        nil,
		},
		{
			desc:       "NonBlocking Write only error returns immediately",
			makePipe:   false, /* causes the error */
			flags:      fs.FileFlags{Write: true, NonBlocking: true},
			expectFile: false,
			err:        syscall.ENOENT,
		},
		{
			desc:       "NonBlocking Write only no reader error returns immediately",
			makePipe:   true,
			flags:      fs.FileFlags{Write: true, NonBlocking: true},
			expectFile: false,
			err:        syscall.ENXIO,
		},
		{
			desc:       "ReadWrite error returns immediately",
			makePipe:   false, /* causes the error */
			flags:      fs.FileFlags{Read: true, Write: true},
			expectFile: false,
			err:        syscall.ENOENT,
		},
		{
			desc:       "ReadWrite returns immediately",
			makePipe:   true,
			flags:      fs.FileFlags{Read: true, Write: true},
			expectFile: true,
			err:        nil,
		},
		{
			desc:       "Blocking Write only returns open error",
			makePipe:   false, /* causes the error */
			flags:      fs.FileFlags{Write: true},
			expectFile: false,
			err:        syscall.ENOENT, /* from bogus perms */
		},
		{
			desc:       "Blocking Read only returns open error",
			makePipe:   false, /* causes the error */
			flags:      fs.FileFlags{Read: true},
			expectFile: false,
			err:        syscall.ENOENT,
		},
		{
			desc:       "Blocking Write only returns with syserror.ErrWouldBlock",
			makePipe:   true,
			flags:      fs.FileFlags{Write: true},
			expectFile: false,
			err:        syserror.ErrWouldBlock,
		},
		{
			desc:       "Blocking Read only returns with syserror.ErrWouldBlock",
			makePipe:   true,
			flags:      fs.FileFlags{Read: true},
			expectFile: false,
			err:        syserror.ErrWouldBlock,
		},
	} {
		name := pipename()
		if test.makePipe {
			// Create the pipe.  We do this per-test case to keep tests independent.
			if err := mkpipe(name); err != nil {
				t.Errorf("%s: failed to make host pipe: %v", test.desc, err)
				continue
			}
			defer syscall.Unlink(name)
		}

		// Use a host opener to keep things simple.
		opener := &hostOpener{name: name}

		pipeOpenState := &pipeOpenState{}
		ctx := contexttest.Context(t)
		pipeOps, err := pipeOpenState.TryOpen(ctx, opener, test.flags)
		if unwrapError(err) != test.err {
			t.Errorf("%s: got error %v, want %v", test.desc, err, test.err)
			if pipeOps != nil {
				// Cleanup the state of the pipe, and remove the fd from the
				// fdnotifier.  Sadly this needed to maintain the correctness
				// of other tests because the fdnotifier is global.
				pipeOps.Release()
			}
			continue
		}
		if (pipeOps != nil) != test.expectFile {
			t.Errorf("%s: got non-nil file %v, want %v", test.desc, pipeOps != nil, test.expectFile)
		}
		if pipeOps != nil {
			// Same as above.
			pipeOps.Release()
		}
	}
}

func TestPipeOpenUnblocksEventually(t *testing.T) {
	for _, test := range []struct {
		// desc is the test's description.
		desc string

		// partnerIsReader is true if the goroutine opening the same pipe as the test case
		// should open the pipe read only.  Otherwise write only.  This also means that the
		// test case will open the pipe in the opposite way.
		partnerIsReader bool

		// partnerIsBlocking is true if the goroutine opening the same pipe as the test case
		// should do so without the O_NONBLOCK flag, otherwise opens the pipe with O_NONBLOCK
		// until ENXIO is not returned.
		partnerIsBlocking bool
	}{
		{
			desc:              "Blocking Read with blocking writer partner opens eventually",
			partnerIsReader:   false,
			partnerIsBlocking: true,
		},
		{
			desc:              "Blocking Write with blocking reader partner opens eventually",
			partnerIsReader:   true,
			partnerIsBlocking: true,
		},
		{
			desc:              "Blocking Read with non-blocking writer partner opens eventually",
			partnerIsReader:   false,
			partnerIsBlocking: false,
		},
		{
			desc:              "Blocking Write with non-blocking reader partner opens eventually",
			partnerIsReader:   true,
			partnerIsBlocking: false,
		},
	} {
		// Create the pipe.  We do this per-test case to keep tests independent.
		name := pipename()
		if err := mkpipe(name); err != nil {
			t.Errorf("%s: failed to make host pipe: %v", test.desc, err)
			continue
		}
		defer syscall.Unlink(name)

		// Spawn the partner.
		type fderr struct {
			fd  int
			err error
		}
		errch := make(chan fderr, 1)
		go func() {
			var flags int
			if test.partnerIsReader {
				flags = syscall.O_RDONLY
			} else {
				flags = syscall.O_WRONLY
			}
			if test.partnerIsBlocking {
				fd, err := syscall.Open(name, flags, 0666)
				errch <- fderr{fd: fd, err: err}
			} else {
				var fd int
				err := error(syscall.ENXIO)
				for err == syscall.ENXIO {
					fd, err = syscall.Open(name, flags|syscall.O_NONBLOCK, 0666)
					time.Sleep(1 * time.Second)
				}
				errch <- fderr{fd: fd, err: err}
			}
		}()

		// Setup file flags for either a read only or write only open.
		flags := fs.FileFlags{
			Read:  !test.partnerIsReader,
			Write: test.partnerIsReader,
		}

		// Open the pipe in a blocking way, which should succeed eventually.
		opener := &hostOpener{name: name}
		ctx := contexttest.Context(t)
		pipeOps, err := Open(ctx, opener, flags)
		if pipeOps != nil {
			// Same as TestTryOpen.
			pipeOps.Release()
		}

		// Check that the partner opened the file successfully.
		e := <-errch
		if e.err != nil {
			t.Errorf("%s: partner got error %v, wanted nil", test.desc, e.err)
			continue
		}
		// If so, then close the partner fd to avoid leaking an fd.
		syscall.Close(e.fd)

		// Check that our blocking open was successful.
		if err != nil {
			t.Errorf("%s: blocking open got error %v, wanted nil", test.desc, err)
			continue
		}
		if pipeOps == nil {
			t.Errorf("%s: blocking open got nil file, wanted non-nil", test.desc)
			continue
		}
	}
}

func TestCopiedReadAheadBuffer(t *testing.T) {
	// Create the pipe.
	name := pipename()
	if err := mkpipe(name); err != nil {
		t.Fatalf("failed to make host pipe: %v", err)
	}
	defer syscall.Unlink(name)

	// We're taking advantage of the fact that pipes opened read only always return
	// success, but internally they are not deemed "opened" until we're sure that
	// another writer comes along.  This means we can open the same pipe write only
	// with no problems + write to it, given that opener.Open already tried to open
	// the pipe RDONLY and succeeded, which we know happened if TryOpen returns
	// syserror.ErrwouldBlock.
	//
	// This simulates the open(RDONLY) <-> open(WRONLY)+write race we care about, but
	// does not cause our test to be racy (which would be terrible).
	opener := &hostOpener{name: name}
	pipeOpenState := &pipeOpenState{}
	ctx := contexttest.Context(t)
	pipeOps, err := pipeOpenState.TryOpen(ctx, opener, fs.FileFlags{Read: true})
	if pipeOps != nil {
		pipeOps.Release()
		t.Fatalf("open(%s, %o) got file, want nil", name, syscall.O_RDONLY)
	}
	if err != syserror.ErrWouldBlock {
		t.Fatalf("open(%s, %o) got error %v, want %v", name, syscall.O_RDONLY, err, syserror.ErrWouldBlock)
	}

	// Then open the same pipe write only and write some bytes to it.  The next
	// time we try to open the pipe read only again via the pipeOpenState, we should
	// succeed and buffer some of the bytes written.
	fd, err := syscall.Open(name, syscall.O_WRONLY, 0666)
	if err != nil {
		t.Fatalf("open(%s, %o) got error %v, want nil", name, syscall.O_WRONLY, err)
	}
	defer syscall.Close(fd)

	data := []byte("hello")
	if n, err := syscall.Write(fd, data); n != len(data) || err != nil {
		t.Fatalf("write(%v) got (%d, %v), want (%d, nil)", data, n, err, len(data))
	}

	// Try the read again, knowing that it should succeed this time.
	pipeOps, err = pipeOpenState.TryOpen(ctx, opener, fs.FileFlags{Read: true})
	if pipeOps == nil {
		t.Fatalf("open(%s, %o) got nil file, want not nil", name, syscall.O_RDONLY)
	}
	defer pipeOps.Release()

	if err != nil {
		t.Fatalf("open(%s, %o) got error %v, want nil", name, syscall.O_RDONLY, err)
	}

	inode := fs.NewMockInode(ctx, fs.NewMockMountSource(nil), fs.StableAttr{
		Type: fs.Pipe,
	})
	file := fs.NewFile(ctx, fs.NewDirent(ctx, inode, "pipe"), fs.FileFlags{Read: true}, pipeOps)

	// Check that the file we opened points to a pipe with a non-empty read ahead buffer.
	bufsize := len(pipeOps.readAheadBuffer)
	if bufsize != 1 {
		t.Fatalf("read ahead buffer got %d bytes, want %d", bufsize, 1)
	}

	// Now for the final test, try to read everything in, expecting to get back all of
	// the bytes that were written at once.  Note that in the wild there is no atomic
	// read size so expecting to get all bytes from a single writer when there are
	// multiple readers is a bad expectation.
	buf := make([]byte, len(data))
	ioseq := usermem.BytesIOSequence(buf)
	n, err := pipeOps.Read(ctx, file, ioseq, 0)
	if err != nil {
		t.Fatalf("read request got error %v, want nil", err)
	}
	if n != int64(len(data)) {
		t.Fatalf("read request got %d bytes, want %d", n, len(data))
	}
	if !bytes.Equal(buf, data) {
		t.Errorf("read request got bytes [%v], want [%v]", buf, data)
	}
}

func TestPipeHangup(t *testing.T) {
	for _, test := range []struct {
		// desc is the test's description.
		desc string

		// flags control how we open our end of the pipe and must be read
		// only or write only.  They also dicate how a coordinating partner
		// fd is opened, which is their inverse (read only -> write only, etc).
		flags fs.FileFlags

		// hangupSelf if true causes the test case to close our end of the pipe
		// and causes hangup errors to be asserted on our coordinating partner's
		// fd.  If hangupSelf is false, then our partner's fd is closed and the
		// hangup errors are expected on our end of the pipe.
		hangupSelf bool
	}{
		{
			desc:  "Read only gets hangup error",
			flags: fs.FileFlags{Read: true},
		},
		{
			desc:  "Write only gets hangup error",
			flags: fs.FileFlags{Write: true},
		},
		{
			desc:       "Read only generates hangup error",
			flags:      fs.FileFlags{Read: true},
			hangupSelf: true,
		},
		{
			desc:       "Write only generates hangup error",
			flags:      fs.FileFlags{Write: true},
			hangupSelf: true,
		},
	} {
		if test.flags.Read == test.flags.Write {
			t.Errorf("%s: test requires a single reader or writer", test.desc)
			continue
		}

		// Create the pipe.  We do this per-test case to keep tests independent.
		name := pipename()
		if err := mkpipe(name); err != nil {
			t.Errorf("%s: failed to make host pipe: %v", test.desc, err)
			continue
		}
		defer syscall.Unlink(name)

		// Fire off a partner routine which tries to open the same pipe blocking,
		// which will synchronize with us.  The channel allows us to get back the
		// fd once we expect this partner routine to succeed, so we can manifest
		// hangup events more directly.
		fdchan := make(chan int, 1)
		go func() {
			// Be explicit about the flags to protect the test from
			// misconfiguration.
			var flags int
			if test.flags.Read {
				flags = syscall.O_WRONLY
			} else {
				flags = syscall.O_RDONLY
			}
			fd, err := syscall.Open(name, flags, 0666)
			if err != nil {
				t.Logf("Open(%q, %o, 0666) partner failed: %v", name, flags, err)
			}
			fdchan <- fd
		}()

		// Open our end in a blocking way to ensure that we coordinate.
		opener := &hostOpener{name: name}
		ctx := contexttest.Context(t)
		pipeOps, err := Open(ctx, opener, test.flags)
		if err != nil {
			t.Errorf("%s: Open got error %v, want nil", test.desc, err)
			continue
		}
		// Don't defer file.DecRef here because that causes the hangup we're
		// trying to test for.

		// Expect the partner routine to have coordinated with us and get back
		// its open fd.
		f := <-fdchan
		if f < 0 {
			t.Errorf("%s: partner routine got fd %d, want > 0", test.desc, f)
			pipeOps.Release()
			continue
		}

		if test.hangupSelf {
			// Hangup self and assert that our partner got the expected hangup
			// error.
			pipeOps.Release()

			if test.flags.Read {
				// Partner is writer.
				assertWriterHungup(t, test.desc, fd.NewReadWriter(f))
			} else {
				// Partner is reader.
				assertReaderHungup(t, test.desc, fd.NewReadWriter(f))
			}
		} else {
			// Hangup our partner and expect us to get the hangup error.
			syscall.Close(f)
			defer pipeOps.Release()

			if test.flags.Read {
				assertReaderHungup(t, test.desc, pipeOps.(*pipeOperations).file)
			} else {
				assertWriterHungup(t, test.desc, pipeOps.(*pipeOperations).file)
			}
		}
	}
}

func assertReaderHungup(t *testing.T, desc string, reader io.Reader) bool {
	// Drain the pipe completely, it might have crap in it, but expect EOF eventually.
	var err error
	for err == nil {
		_, err = reader.Read(make([]byte, 10))
	}
	if err != io.EOF {
		t.Errorf("%s: read from self after hangup got error %v, want %v", desc, err, io.EOF)
		return false
	}
	return true
}

func assertWriterHungup(t *testing.T, desc string, writer io.Writer) bool {
	if _, err := writer.Write([]byte("hello")); unwrapError(err) != syscall.EPIPE {
		t.Errorf("%s: write to self after hangup got error %v, want %v", desc, err, syscall.EPIPE)
		return false
	}
	return true
}