github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/tmpfs/tmpfs_test.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 tmpfs
    16  
    17  import (
    18  	"fmt"
    19  	"sync/atomic"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    22  	"github.com/SagerNet/gvisor/pkg/context"
    23  	"github.com/SagerNet/gvisor/pkg/fspath"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/auth"
    25  	"github.com/SagerNet/gvisor/pkg/sentry/vfs"
    26  )
    27  
    28  // nextFileID is used to generate unique file names.
    29  var nextFileID int64
    30  
    31  // newTmpfsRoot creates a new tmpfs mount, and returns the root. If the error
    32  // is not nil, then cleanup should be called when the root is no longer needed.
    33  func newTmpfsRoot(ctx context.Context) (*vfs.VirtualFilesystem, vfs.VirtualDentry, func(), error) {
    34  	creds := auth.CredentialsFromContext(ctx)
    35  
    36  	vfsObj := &vfs.VirtualFilesystem{}
    37  	if err := vfsObj.Init(ctx); err != nil {
    38  		return nil, vfs.VirtualDentry{}, nil, fmt.Errorf("VFS init: %v", err)
    39  	}
    40  
    41  	vfsObj.MustRegisterFilesystemType("tmpfs", FilesystemType{}, &vfs.RegisterFilesystemTypeOptions{
    42  		AllowUserMount: true,
    43  	})
    44  	mntns, err := vfsObj.NewMountNamespace(ctx, creds, "", "tmpfs", &vfs.MountOptions{})
    45  	if err != nil {
    46  		return nil, vfs.VirtualDentry{}, nil, fmt.Errorf("failed to create tmpfs root mount: %v", err)
    47  	}
    48  	root := mntns.Root()
    49  	root.IncRef()
    50  	return vfsObj, root, func() {
    51  		root.DecRef(ctx)
    52  		mntns.DecRef(ctx)
    53  	}, nil
    54  }
    55  
    56  // newFileFD creates a new file in a new tmpfs mount, and returns the FD. If
    57  // the returned err is not nil, then cleanup should be called when the FD is no
    58  // longer needed.
    59  func newFileFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) {
    60  	creds := auth.CredentialsFromContext(ctx)
    61  	vfsObj, root, cleanup, err := newTmpfsRoot(ctx)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	filename := fmt.Sprintf("tmpfs-test-file-%d", atomic.AddInt64(&nextFileID, 1))
    67  
    68  	// Create the file that will be write/read.
    69  	fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{
    70  		Root:  root,
    71  		Start: root,
    72  		Path:  fspath.Parse(filename),
    73  	}, &vfs.OpenOptions{
    74  		Flags: linux.O_RDWR | linux.O_CREAT | linux.O_EXCL,
    75  		Mode:  linux.ModeRegular | mode,
    76  	})
    77  	if err != nil {
    78  		cleanup()
    79  		return nil, nil, fmt.Errorf("failed to create file %q: %v", filename, err)
    80  	}
    81  
    82  	return fd, cleanup, nil
    83  }
    84  
    85  // newDirFD is like newFileFD, but for directories.
    86  func newDirFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) {
    87  	creds := auth.CredentialsFromContext(ctx)
    88  	vfsObj, root, cleanup, err := newTmpfsRoot(ctx)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  
    93  	dirname := fmt.Sprintf("tmpfs-test-dir-%d", atomic.AddInt64(&nextFileID, 1))
    94  
    95  	// Create the dir.
    96  	if err := vfsObj.MkdirAt(ctx, creds, &vfs.PathOperation{
    97  		Root:  root,
    98  		Start: root,
    99  		Path:  fspath.Parse(dirname),
   100  	}, &vfs.MkdirOptions{
   101  		Mode: linux.ModeDirectory | mode,
   102  	}); err != nil {
   103  		cleanup()
   104  		return nil, nil, fmt.Errorf("failed to create directory %q: %v", dirname, err)
   105  	}
   106  
   107  	// Open the dir and return it.
   108  	fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{
   109  		Root:  root,
   110  		Start: root,
   111  		Path:  fspath.Parse(dirname),
   112  	}, &vfs.OpenOptions{
   113  		Flags: linux.O_RDONLY | linux.O_DIRECTORY,
   114  	})
   115  	if err != nil {
   116  		cleanup()
   117  		return nil, nil, fmt.Errorf("failed to open directory %q: %v", dirname, err)
   118  	}
   119  
   120  	return fd, cleanup, nil
   121  }
   122  
   123  // newPipeFD is like newFileFD, but for pipes.
   124  func newPipeFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) {
   125  	creds := auth.CredentialsFromContext(ctx)
   126  	vfsObj, root, cleanup, err := newTmpfsRoot(ctx)
   127  	if err != nil {
   128  		return nil, nil, err
   129  	}
   130  
   131  	name := fmt.Sprintf("tmpfs-test-%d", atomic.AddInt64(&nextFileID, 1))
   132  
   133  	if err := vfsObj.MknodAt(ctx, creds, &vfs.PathOperation{
   134  		Root:  root,
   135  		Start: root,
   136  		Path:  fspath.Parse(name),
   137  	}, &vfs.MknodOptions{
   138  		Mode: linux.ModeNamedPipe | mode,
   139  	}); err != nil {
   140  		cleanup()
   141  		return nil, nil, fmt.Errorf("failed to create pipe %q: %v", name, err)
   142  	}
   143  
   144  	fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{
   145  		Root:  root,
   146  		Start: root,
   147  		Path:  fspath.Parse(name),
   148  	}, &vfs.OpenOptions{
   149  		Flags: linux.O_RDWR,
   150  	})
   151  	if err != nil {
   152  		cleanup()
   153  		return nil, nil, fmt.Errorf("failed to open pipe %q: %v", name, err)
   154  	}
   155  
   156  	return fd, cleanup, nil
   157  }