summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fsimpl/host/default_file.go
blob: 172cdb161281a53862979c654f9cdda86bbab521 (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
// Copyright 2020 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 host

import (
	"math"
	"syscall"

	"golang.org/x/sys/unix"
	"gvisor.dev/gvisor/pkg/abi/linux"
	"gvisor.dev/gvisor/pkg/context"
	"gvisor.dev/gvisor/pkg/safemem"
	"gvisor.dev/gvisor/pkg/sentry/memmap"
	"gvisor.dev/gvisor/pkg/sentry/vfs"
	"gvisor.dev/gvisor/pkg/sync"
	"gvisor.dev/gvisor/pkg/syserror"
	"gvisor.dev/gvisor/pkg/usermem"
)

// defaultFileFD implements FileDescriptionImpl for non-socket, non-TTY files.
type defaultFileFD struct {
	fileDescription

	// canMap specifies whether we allow the file to be memory mapped.
	canMap bool

	// mu protects the fields below.
	mu sync.Mutex

	// offset specifies the current file offset.
	offset int64
}

// TODO(gvisor.dev/issue/1672): Implement Waitable interface.

// PRead implements FileDescriptionImpl.
func (f *defaultFileFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
	// TODO(b/34716638): Some char devices do support offsets, e.g. /dev/null.
	if f.inode.isStream {
		return 0, syserror.ESPIPE
	}

	return readFromHostFD(ctx, f.inode.hostFD, dst, offset, int(opts.Flags))
}

// Read implements FileDescriptionImpl.
func (f *defaultFileFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
	// TODO(b/34716638): Some char devices do support offsets, e.g. /dev/null.
	if f.inode.isStream {
		// These files can't be memory mapped, assert this.
		if f.canMap {
			panic("files that can return EWOULDBLOCK (sockets, pipes, etc.) cannot be memory mapped")
		}

		f.mu.Lock()
		n, err := readFromHostFD(ctx, f.inode.hostFD, dst, -1, int(opts.Flags))
		f.mu.Unlock()
		if isBlockError(err) {
			// If we got any data at all, return it as a "completed" partial read
			// rather than retrying until complete.
			if n != 0 {
				err = nil
			} else {
				err = syserror.ErrWouldBlock
			}
		}
		return n, err
	}
	// TODO(gvisor.dev/issue/1672): Cache pages, when forced to do so.
	f.mu.Lock()
	n, err := readFromHostFD(ctx, f.inode.hostFD, dst, f.offset, int(opts.Flags))
	f.offset += n
	f.mu.Unlock()
	return n, err
}

func readFromHostFD(ctx context.Context, fd int, dst usermem.IOSequence, offset int64, flags int) (int64, error) {
	if flags&^(linux.RWF_VALID) != 0 {
		return 0, syserror.EOPNOTSUPP
	}

	reader := safemem.FromVecReaderFunc{
		func(srcs [][]byte) (int64, error) {
			n, err := unix.Preadv2(fd, srcs, offset, flags)
			return int64(n), err
		},
	}
	n, err := dst.CopyOutFrom(ctx, reader)
	return int64(n), err
}

// PWrite implements FileDescriptionImpl.
func (f *defaultFileFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) {
	// TODO(b/34716638): Some char devices do support offsets, e.g. /dev/null.
	if f.inode.isStream {
		return 0, syserror.ESPIPE
	}

	return writeToHostFD(ctx, f.inode.hostFD, src, offset, int(opts.Flags))
}

// Write implements FileDescriptionImpl.
func (f *defaultFileFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
	// TODO(b/34716638): Some char devices do support offsets, e.g. /dev/null.
	if f.inode.isStream {
		// These files can't be memory mapped, assert this.
		if f.canMap {
			panic("files that can return EWOULDBLOCK (sockets, pipes, etc.) cannot be memory mapped")
		}

		f.mu.Lock()
		n, err := writeToHostFD(ctx, f.inode.hostFD, src, -1, int(opts.Flags))
		f.mu.Unlock()
		if isBlockError(err) {
			err = syserror.ErrWouldBlock
		}
		return n, err
	}
	// TODO(gvisor.dev/issue/1672): Cache pages, when forced to do so.
	// TODO(gvisor.dev/issue/1672): Write to end of file and update offset if O_APPEND is set on this file.
	f.mu.Lock()
	n, err := writeToHostFD(ctx, f.inode.hostFD, src, f.offset, int(opts.Flags))
	f.offset += n
	f.mu.Unlock()
	return n, err
}

func writeToHostFD(ctx context.Context, fd int, src usermem.IOSequence, offset int64, flags int) (int64, error) {
	if flags&^(linux.RWF_VALID) != 0 {
		return 0, syserror.EOPNOTSUPP
	}

	writer := safemem.FromVecWriterFunc{
		func(srcs [][]byte) (int64, error) {
			n, err := unix.Pwritev2(fd, srcs, offset, flags)
			return int64(n), err
		},
	}
	n, err := src.CopyInTo(ctx, writer)
	return int64(n), err
}

// Seek implements FileDescriptionImpl.
//
// Note that we do not support seeking on directories, since we do not even
// allow directory fds to be imported at all.
func (f *defaultFileFD) Seek(_ context.Context, offset int64, whence int32) (int64, error) {
	// TODO(b/34716638): Some char devices do support seeking, e.g. /dev/null.
	if f.inode.isStream {
		return 0, syserror.ESPIPE
	}

	f.mu.Lock()
	defer f.mu.Unlock()

	switch whence {
	case linux.SEEK_SET:
		if offset < 0 {
			return f.offset, syserror.EINVAL
		}
		f.offset = offset

	case linux.SEEK_CUR:
		// Check for overflow. Note that underflow cannot occur, since f.offset >= 0.
		if offset > math.MaxInt64-f.offset {
			return f.offset, syserror.EOVERFLOW
		}
		if f.offset+offset < 0 {
			return f.offset, syserror.EINVAL
		}
		f.offset += offset

	case linux.SEEK_END:
		var s syscall.Stat_t
		if err := syscall.Fstat(f.inode.hostFD, &s); err != nil {
			return f.offset, err
		}
		size := s.Size

		// Check for overflow. Note that underflow cannot occur, since size >= 0.
		if offset > math.MaxInt64-size {
			return f.offset, syserror.EOVERFLOW
		}
		if size+offset < 0 {
			return f.offset, syserror.EINVAL
		}
		f.offset = size + offset

	case linux.SEEK_DATA, linux.SEEK_HOLE:
		// Modifying the offset in the host file table should not matter, since
		// this is the only place where we use it.
		//
		// For reading and writing, we always rely on our internal offset.
		n, err := unix.Seek(f.inode.hostFD, offset, int(whence))
		if err != nil {
			return f.offset, err
		}
		f.offset = n

	default:
		// Invalid whence.
		return f.offset, syserror.EINVAL
	}

	return f.offset, nil
}

// Sync implements FileDescriptionImpl.
func (f *defaultFileFD) Sync(context.Context) error {
	// TODO(gvisor.dev/issue/1672): Currently we do not support the SyncData optimization, so we always sync everything.
	return unix.Fsync(f.inode.hostFD)
}

// ConfigureMMap implements FileDescriptionImpl.
func (f *defaultFileFD) ConfigureMMap(_ context.Context, opts *memmap.MMapOpts) error {
	if !f.canMap {
		return syserror.ENODEV
	}
	// TODO(gvisor.dev/issue/1672): Implement ConfigureMMap and Mappable interface.
	return syserror.ENODEV
}