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
|
// 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 fsutil
import (
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/memmap"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserror"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
// FileNoopRelease implements fs.FileOperations.Release for files that have no
// resources to release.
type FileNoopRelease struct{}
// Release is a no-op.
func (FileNoopRelease) Release() {}
// SeekWithDirCursor is used to implement fs.FileOperations.Seek. If dirCursor
// is not nil and the seek was on a directory, the cursor will be updated.
//
// Currently only seeking to 0 on a directory is supported.
//
// FIXME(b/33075855): Lift directory seeking limitations.
func SeekWithDirCursor(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64, dirCursor *string) (int64, error) {
inode := file.Dirent.Inode
current := file.Offset()
// Does the Inode represents a non-seekable type?
if fs.IsPipe(inode.StableAttr) || fs.IsSocket(inode.StableAttr) {
return current, syserror.ESPIPE
}
// Does the Inode represent a character device?
if fs.IsCharDevice(inode.StableAttr) {
// Ignore seek requests.
//
// FIXME(b/34716638): This preserves existing
// behavior but is not universally correct.
return 0, nil
}
// Otherwise compute the new offset.
switch whence {
case fs.SeekSet:
switch inode.StableAttr.Type {
case fs.RegularFile, fs.SpecialFile, fs.BlockDevice:
if offset < 0 {
return current, syserror.EINVAL
}
return offset, nil
case fs.Directory, fs.SpecialDirectory:
if offset != 0 {
return current, syserror.EINVAL
}
// SEEK_SET to 0 moves the directory "cursor" to the beginning.
if dirCursor != nil {
*dirCursor = ""
}
return 0, nil
default:
return current, syserror.EINVAL
}
case fs.SeekCurrent:
switch inode.StableAttr.Type {
case fs.RegularFile, fs.SpecialFile, fs.BlockDevice:
if current+offset < 0 {
return current, syserror.EINVAL
}
return current + offset, nil
case fs.Directory, fs.SpecialDirectory:
if offset != 0 {
return current, syserror.EINVAL
}
return current, nil
default:
return current, syserror.EINVAL
}
case fs.SeekEnd:
switch inode.StableAttr.Type {
case fs.RegularFile, fs.BlockDevice:
// Allow the file to determine the end.
uattr, err := inode.UnstableAttr(ctx)
if err != nil {
return current, err
}
sz := uattr.Size
if sz+offset < 0 {
return current, syserror.EINVAL
}
return sz + offset, nil
// FIXME(b/34778850): This is not universally correct.
// Remove SpecialDirectory.
case fs.SpecialDirectory:
if offset != 0 {
return current, syserror.EINVAL
}
// SEEK_END to 0 moves the directory "cursor" to the end.
//
// FIXME(b/35442290): The ensures that after the seek,
// reading on the directory will get EOF. But it is not
// correct in general because the directory can grow in
// size; attempting to read those new entries will be
// futile (EOF will always be the result).
return fs.FileMaxOffset, nil
default:
return current, syserror.EINVAL
}
}
// Not a valid seek request.
return current, syserror.EINVAL
}
// FileGenericSeek implements fs.FileOperations.Seek for files that use a
// generic seek implementation.
type FileGenericSeek struct{}
// Seek implements fs.FileOperations.Seek.
func (FileGenericSeek) Seek(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64) (int64, error) {
return SeekWithDirCursor(ctx, file, whence, offset, nil)
}
// FileZeroSeek implements fs.FileOperations.Seek for files that maintain a
// constant zero-value offset and require a no-op Seek.
type FileZeroSeek struct{}
// Seek implements fs.FileOperations.Seek.
func (FileZeroSeek) Seek(context.Context, *fs.File, fs.SeekWhence, int64) (int64, error) {
return 0, nil
}
// FileNoSeek implements fs.FileOperations.Seek to return EINVAL.
type FileNoSeek struct{}
// Seek implements fs.FileOperations.Seek.
func (FileNoSeek) Seek(context.Context, *fs.File, fs.SeekWhence, int64) (int64, error) {
return 0, syserror.EINVAL
}
// FilePipeSeek implements fs.FileOperations.Seek and can be used for files
// that behave like pipes (seeking is not supported).
type FilePipeSeek struct{}
// Seek implements fs.FileOperations.Seek.
func (FilePipeSeek) Seek(context.Context, *fs.File, fs.SeekWhence, int64) (int64, error) {
return 0, syserror.ESPIPE
}
// FileNotDirReaddir implements fs.FileOperations.Readdir for non-directories.
type FileNotDirReaddir struct{}
// Readdir implements fs.FileOperations.FileNotDirReaddir.
func (FileNotDirReaddir) Readdir(context.Context, *fs.File, fs.DentrySerializer) (int64, error) {
return 0, syserror.ENOTDIR
}
// FileNoFsync implements fs.FileOperations.Fsync for files that don't support
// syncing.
type FileNoFsync struct{}
// Fsync implements fs.FileOperations.Fsync.
func (FileNoFsync) Fsync(context.Context, *fs.File, int64, int64, fs.SyncType) error {
return syserror.EINVAL
}
// FileNoopFsync implements fs.FileOperations.Fsync for files that don't need
// to synced.
type FileNoopFsync struct{}
// Fsync implements fs.FileOperations.Fsync.
func (FileNoopFsync) Fsync(context.Context, *fs.File, int64, int64, fs.SyncType) error {
return nil
}
// FileNoopFlush implements fs.FileOperations.Flush as a no-op.
type FileNoopFlush struct{}
// Flush implements fs.FileOperations.Flush.
func (FileNoopFlush) Flush(context.Context, *fs.File) error {
return nil
}
// FileNoMMap implements fs.FileOperations.Mappable for files that cannot
// be memory mapped.
type FileNoMMap struct{}
// ConfigureMMap implements fs.FileOperations.ConfigureMMap.
func (FileNoMMap) ConfigureMMap(context.Context, *fs.File, *memmap.MMapOpts) error {
return syserror.ENODEV
}
// GenericConfigureMMap implements fs.FileOperations.ConfigureMMap for most
// filesystems that support memory mapping.
func GenericConfigureMMap(file *fs.File, m memmap.Mappable, opts *memmap.MMapOpts) error {
opts.Mappable = m
opts.MappingIdentity = file
file.IncRef()
return nil
}
// FileNoIoctl implements fs.FileOperations.Ioctl for files that don't
// implement the ioctl syscall.
type FileNoIoctl struct{}
// Ioctl implements fs.FileOperations.Ioctl.
func (FileNoIoctl) Ioctl(ctx context.Context, io usermem.IO, args arch.SyscallArguments) (uintptr, error) {
return 0, syserror.ENOTTY
}
// FileNoSplice implements fs.FileOperations.ReadFrom and
// fs.FileOperations.WriteTo for files that don't support splice.
type FileNoSplice struct{}
// WriteTo implements fs.FileOperations.WriteTo.
func (FileNoSplice) WriteTo(context.Context, *fs.File, *fs.File, fs.SpliceOpts) (int64, error) {
return 0, syserror.ENOSYS
}
// ReadFrom implements fs.FileOperations.ReadFrom.
func (FileNoSplice) ReadFrom(context.Context, *fs.File, *fs.File, fs.SpliceOpts) (int64, error) {
return 0, syserror.ENOSYS
}
// DirFileOperations implements most of fs.FileOperations for directories,
// except for Readdir and UnstableAttr which the embedding type must implement.
type DirFileOperations struct {
waiter.AlwaysReady
FileGenericSeek
FileNoIoctl
FileNoMMap
FileNoopFlush
FileNoopFsync
FileNoopRelease
FileNoSplice
}
// Read implements fs.FileOperations.Read
func (*DirFileOperations) Read(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
return 0, syserror.EISDIR
}
// Write implements fs.FileOperations.Write.
func (*DirFileOperations) Write(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
return 0, syserror.EISDIR
}
// StaticDirFileOperations implements fs.FileOperations for directories with
// static children.
//
// +stateify savable
type StaticDirFileOperations struct {
DirFileOperations `state:"nosave"`
FileUseInodeUnstableAttr `state:"nosave"`
// dentryMap is a SortedDentryMap used to implement Readdir.
dentryMap *fs.SortedDentryMap
// dirCursor contains the name of the last directory entry that was
// serialized.
dirCursor string
}
// NewStaticDirFileOperations returns a new StaticDirFileOperations that will
// iterate the given denty map.
func NewStaticDirFileOperations(dentries *fs.SortedDentryMap) *StaticDirFileOperations {
return &StaticDirFileOperations{
dentryMap: dentries,
}
}
// IterateDir implements DirIterator.IterateDir.
func (sdfo *StaticDirFileOperations) IterateDir(ctx context.Context, dirCtx *fs.DirCtx, offset int) (int, error) {
n, err := fs.GenericReaddir(dirCtx, sdfo.dentryMap)
return offset + n, err
}
// Readdir implements fs.FileOperations.Readdir.
func (sdfo *StaticDirFileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &sdfo.dirCursor,
}
return fs.DirentReaddir(ctx, file.Dirent, sdfo, root, dirCtx, file.Offset())
}
// NoReadWriteFile is a file that does not support reading or writing.
//
// +stateify savable
type NoReadWriteFile struct {
waiter.AlwaysReady `state:"nosave"`
FileGenericSeek `state:"nosave"`
FileNoIoctl `state:"nosave"`
FileNoMMap `state:"nosave"`
FileNoopFsync `state:"nosave"`
FileNoopFlush `state:"nosave"`
FileNoopRelease `state:"nosave"`
FileNoRead `state:"nosave"`
FileNoWrite `state:"nosave"`
FileNotDirReaddir `state:"nosave"`
FileUseInodeUnstableAttr `state:"nosave"`
FileNoSplice `state:"nosave"`
}
var _ fs.FileOperations = (*NoReadWriteFile)(nil)
// FileStaticContentReader is a helper to implement fs.FileOperations.Read with
// static content.
//
// +stateify savable
type FileStaticContentReader struct {
// content is immutable.
content []byte
}
// NewFileStaticContentReader initializes a FileStaticContentReader with the
// given content.
func NewFileStaticContentReader(b []byte) FileStaticContentReader {
return FileStaticContentReader{
content: b,
}
}
// Read implements fs.FileOperations.Read.
func (scr *FileStaticContentReader) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, offset int64) (int64, error) {
if offset < 0 {
return 0, syserror.EINVAL
}
if offset >= int64(len(scr.content)) {
return 0, nil
}
n, err := dst.CopyOut(ctx, scr.content[offset:])
return int64(n), err
}
// FileNoopWrite implements fs.FileOperations.Write as a noop.
type FileNoopWrite struct{}
// Write implements fs.FileOperations.Write.
func (FileNoopWrite) Write(_ context.Context, _ *fs.File, src usermem.IOSequence, _ int64) (int64, error) {
return src.NumBytes(), nil
}
// FileNoRead implements fs.FileOperations.Read to return EINVAL.
type FileNoRead struct{}
// Read implements fs.FileOperations.Read.
func (FileNoRead) Read(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
return 0, syserror.EINVAL
}
// FileNoWrite implements fs.FileOperations.Write to return EINVAL.
type FileNoWrite struct{}
// Write implements fs.FileOperations.Write.
func (FileNoWrite) Write(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
return 0, syserror.EINVAL
}
// FileNoopRead implement fs.FileOperations.Read as a noop.
type FileNoopRead struct{}
// Read implements fs.FileOperations.Read.
func (FileNoopRead) Read(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
return 0, nil
}
// FileUseInodeUnstableAttr implements fs.FileOperations.UnstableAttr by calling
// InodeOperations.UnstableAttr.
type FileUseInodeUnstableAttr struct{}
// UnstableAttr implements fs.FileOperations.UnstableAttr.
func (FileUseInodeUnstableAttr) UnstableAttr(ctx context.Context, file *fs.File) (fs.UnstableAttr, error) {
return file.Dirent.Inode.UnstableAttr(ctx)
}
|