gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/vfs/options.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vfs
    16  
    17  import (
    18  	"gvisor.dev/gvisor/pkg/abi/linux"
    19  	"gvisor.dev/gvisor/pkg/sentry/socket/unix/transport"
    20  )
    21  
    22  // GetDentryOptions contains options to VirtualFilesystem.GetDentryAt() and
    23  // FilesystemImpl.GetDentryAt().
    24  //
    25  // +stateify savable
    26  type GetDentryOptions struct {
    27  	// If CheckSearchable is true, FilesystemImpl.GetDentryAt() must check that
    28  	// the returned Dentry is a directory for which creds has search
    29  	// permission.
    30  	CheckSearchable bool
    31  }
    32  
    33  // MkdirOptions contains options to VirtualFilesystem.MkdirAt() and
    34  // FilesystemImpl.MkdirAt().
    35  //
    36  // +stateify savable
    37  type MkdirOptions struct {
    38  	// Mode is the file mode bits for the created directory.
    39  	Mode linux.FileMode
    40  
    41  	// If ForSyntheticMountpoint is true, FilesystemImpl.MkdirAt() may create
    42  	// the given directory in memory only (as opposed to persistent storage).
    43  	// The created directory should be able to support the creation of
    44  	// subdirectories with ForSyntheticMountpoint == true. It does not need to
    45  	// support the creation of subdirectories with ForSyntheticMountpoint ==
    46  	// false, or files of other types.
    47  	//
    48  	// FilesystemImpls are permitted to ignore the ForSyntheticMountpoint
    49  	// option.
    50  	//
    51  	// The ForSyntheticMountpoint option exists because, unlike mount(2), the
    52  	// OCI Runtime Specification permits the specification of mount points that
    53  	// do not exist, under the expectation that container runtimes will create
    54  	// them. (More accurately, the OCI Runtime Specification completely fails
    55  	// to document this feature, but it's implemented by runc.)
    56  	// ForSyntheticMountpoint allows such mount points to be created even when
    57  	// the underlying persistent filesystem is immutable.
    58  	ForSyntheticMountpoint bool
    59  }
    60  
    61  // MknodOptions contains options to VirtualFilesystem.MknodAt() and
    62  // FilesystemImpl.MknodAt().
    63  //
    64  // +stateify savable
    65  type MknodOptions struct {
    66  	// Mode is the file type and mode bits for the created file.
    67  	Mode linux.FileMode
    68  
    69  	// If Mode specifies a character or block device special file, DevMajor and
    70  	// DevMinor are the major and minor device numbers for the created device.
    71  	DevMajor uint32
    72  	DevMinor uint32
    73  
    74  	// Endpoint is the endpoint to bind to the created file, if a socket file is
    75  	// being created for bind(2) on a Unix domain socket.
    76  	Endpoint transport.BoundEndpoint
    77  }
    78  
    79  // MountFlags contains flags as specified for mount(2), e.g. MS_NOEXEC.
    80  // MS_RDONLY is not part of MountFlags because it's tracked in Mount.writers.
    81  //
    82  // +stateify savable
    83  type MountFlags struct {
    84  	// NoExec is equivalent to MS_NOEXEC.
    85  	NoExec bool
    86  
    87  	// NoATime is equivalent to MS_NOATIME and indicates that the
    88  	// filesystem should not update access time in-place.
    89  	NoATime bool
    90  
    91  	// NoDev is equivalent to MS_NODEV and indicates that the
    92  	// filesystem should not allow access to devices (special files).
    93  	// TODO(gVisor.dev/issue/3186): respect this flag in non FUSE
    94  	// filesystems.
    95  	NoDev bool
    96  
    97  	// NoSUID is equivalent to MS_NOSUID and indicates that the
    98  	// filesystem should not honor set-user-ID and set-group-ID bits or
    99  	// file capabilities when executing programs.
   100  	NoSUID bool
   101  }
   102  
   103  // MountOptions contains options to VirtualFilesystem.MountAt(), and VirtualFilesystem.RemountAt()
   104  //
   105  // +stateify savable
   106  type MountOptions struct {
   107  	// Flags contains flags as specified for mount(2), e.g. MS_NOEXEC.
   108  	Flags MountFlags
   109  
   110  	// ReadOnly is equivalent to MS_RDONLY.
   111  	ReadOnly bool
   112  
   113  	// GetFilesystemOptions contains options to FilesystemType.GetFilesystem().
   114  	GetFilesystemOptions GetFilesystemOptions
   115  
   116  	// Locked determines whether to lock this mount so it cannot be unmounted by
   117  	// normal user processes.
   118  	Locked bool
   119  }
   120  
   121  // OpenOptions contains options to VirtualFilesystem.OpenAt() and
   122  // FilesystemImpl.OpenAt().
   123  //
   124  // +stateify savable
   125  type OpenOptions struct {
   126  	// Flags contains access mode and flags as specified for open(2).
   127  	//
   128  	// FilesystemImpls are responsible for implementing the following flags:
   129  	// O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_DIRECT, O_DSYNC,
   130  	// O_EXCL, O_NOATIME, O_NOCTTY, O_NONBLOCK, O_SYNC, O_TMPFILE, and
   131  	// O_TRUNC. VFS is responsible for handling O_DIRECTORY, O_LARGEFILE, and
   132  	// O_NOFOLLOW. VFS users are responsible for handling O_CLOEXEC, since file
   133  	// descriptors are mostly outside the scope of VFS.
   134  	Flags uint32
   135  
   136  	// If FilesystemImpl.OpenAt() creates a file, Mode is the file mode for the
   137  	// created file.
   138  	Mode linux.FileMode
   139  
   140  	// FileExec is set when the file is being opened to be executed.
   141  	// VirtualFilesystem.OpenAt() checks that the caller has execute permissions
   142  	// on the file, that the file is a regular file, and that the mount doesn't
   143  	// have MS_NOEXEC set.
   144  	FileExec bool
   145  }
   146  
   147  // ReadOptions contains options to FileDescription.PRead(),
   148  // FileDescriptionImpl.PRead(), FileDescription.Read(), and
   149  // FileDescriptionImpl.Read().
   150  //
   151  // +stateify savable
   152  type ReadOptions struct {
   153  	// Flags contains flags as specified for preadv2(2).
   154  	Flags uint32
   155  }
   156  
   157  // RenameOptions contains options to VirtualFilesystem.RenameAt() and
   158  // FilesystemImpl.RenameAt().
   159  //
   160  // +stateify savable
   161  type RenameOptions struct {
   162  	// Flags contains flags as specified for renameat2(2).
   163  	Flags uint32
   164  
   165  	// If MustBeDir is true, the renamed file must be a directory.
   166  	MustBeDir bool
   167  }
   168  
   169  // SetStatOptions contains options to VirtualFilesystem.SetStatAt(),
   170  // FilesystemImpl.SetStatAt(), FileDescription.SetStat(), and
   171  // FileDescriptionImpl.SetStat().
   172  //
   173  // +stateify savable
   174  type SetStatOptions struct {
   175  	// Stat is the metadata that should be set. Only fields indicated by
   176  	// Stat.Mask should be set.
   177  	//
   178  	// If Stat specifies that a timestamp should be set,
   179  	// FilesystemImpl.SetStatAt() and FileDescriptionImpl.SetStat() must
   180  	// special-case StatxTimestamp.Nsec == UTIME_NOW as described by
   181  	// utimensat(2); however, they do not need to check for StatxTimestamp.Nsec
   182  	// == UTIME_OMIT (VFS users must unset the corresponding bit in Stat.Mask
   183  	// instead).
   184  	Stat linux.Statx
   185  
   186  	// NeedWritePerm indicates that write permission on the file is needed for
   187  	// this operation. This is needed for truncate(2) (note that ftruncate(2)
   188  	// does not require the same check--instead, it checks that the fd is
   189  	// writable).
   190  	NeedWritePerm bool
   191  }
   192  
   193  // BoundEndpointOptions contains options to VirtualFilesystem.BoundEndpointAt()
   194  // and FilesystemImpl.BoundEndpointAt().
   195  //
   196  // +stateify savable
   197  type BoundEndpointOptions struct {
   198  	// Addr is the path of the file whose socket endpoint is being retrieved.
   199  	// It is generally irrelevant: most endpoints are stored at a dentry that
   200  	// was created through a bind syscall, so the path can be stored on creation.
   201  	// However, if the endpoint was created in FilesystemImpl.BoundEndpointAt(),
   202  	// then we may not know what the original bind address was.
   203  	//
   204  	// For example, if connect(2) is called with address "foo" which corresponds
   205  	// a remote named socket in goferfs, we need to generate an endpoint wrapping
   206  	// that file. In this case, we can use Addr to set the endpoint address to
   207  	// "foo". Note that Addr is only a best-effort attempt--we still do not know
   208  	// the exact address that was used on the remote fs to bind the socket (it
   209  	// may have been "foo", "./foo", etc.).
   210  	Addr string
   211  }
   212  
   213  // GetXattrOptions contains options to VirtualFilesystem.GetXattrAt(),
   214  // FilesystemImpl.GetXattrAt(), FileDescription.GetXattr(), and
   215  // FileDescriptionImpl.GetXattr().
   216  //
   217  // +stateify savable
   218  type GetXattrOptions struct {
   219  	// Name is the name of the extended attribute to retrieve.
   220  	Name string
   221  
   222  	// Size is the maximum value size that the caller will tolerate. If the value
   223  	// is larger than size, getxattr methods may return ERANGE, but they are also
   224  	// free to ignore the hint entirely (i.e. the value returned may be larger
   225  	// than size). All size checking is done independently at the syscall layer.
   226  	Size uint64
   227  }
   228  
   229  // SetXattrOptions contains options to VirtualFilesystem.SetXattrAt(),
   230  // FilesystemImpl.SetXattrAt(), FileDescription.SetXattr(), and
   231  // FileDescriptionImpl.SetXattr().
   232  //
   233  // +stateify savable
   234  type SetXattrOptions struct {
   235  	// Name is the name of the extended attribute being mutated.
   236  	Name string
   237  
   238  	// Value is the extended attribute's new value.
   239  	Value string
   240  
   241  	// Flags contains flags as specified for setxattr/lsetxattr/fsetxattr(2).
   242  	Flags uint32
   243  }
   244  
   245  // StatOptions contains options to VirtualFilesystem.StatAt(),
   246  // FilesystemImpl.StatAt(), FileDescription.Stat(), and
   247  // FileDescriptionImpl.Stat().
   248  //
   249  // +stateify savable
   250  type StatOptions struct {
   251  	// Mask is the set of fields in the returned Statx that the FilesystemImpl
   252  	// or FileDescriptionImpl should provide. Bits are as in linux.Statx.Mask.
   253  	//
   254  	// The FilesystemImpl or FileDescriptionImpl may return fields not
   255  	// requested in Mask, and may fail to return fields requested in Mask that
   256  	// are not supported by the underlying filesystem implementation, without
   257  	// returning an error.
   258  	Mask uint32
   259  
   260  	// Sync specifies the synchronization required, and is one of
   261  	// linux.AT_STATX_SYNC_AS_STAT (which is 0, and therefore the default),
   262  	// linux.AT_STATX_SYNC_FORCE_SYNC, or linux.AT_STATX_SYNC_DONT_SYNC.
   263  	Sync uint32
   264  }
   265  
   266  // UmountOptions contains options to VirtualFilesystem.UmountAt().
   267  //
   268  // +stateify savable
   269  type UmountOptions struct {
   270  	// Flags contains flags as specified for umount2(2).
   271  	Flags uint32
   272  }
   273  
   274  // WriteOptions contains options to FileDescription.PWrite(),
   275  // FileDescriptionImpl.PWrite(), FileDescription.Write(), and
   276  // FileDescriptionImpl.Write().
   277  //
   278  // +stateify savable
   279  type WriteOptions struct {
   280  	// Flags contains flags as specified for pwritev2(2).
   281  	Flags uint32
   282  }