gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/specutils/safemount_test/safemount_runner.go (about)

     1  // Copyright 2021 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  // safemount_runner is used to test the SafeMount function. Because use of
    16  // unix.Mount requires privilege, tests must launch this process with
    17  // CLONE_NEWNS and CLONE_NEWUSER.
    18  package main
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"log"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"golang.org/x/sys/unix"
    28  	"gvisor.dev/gvisor/runsc/specutils"
    29  )
    30  
    31  func main() {
    32  	// The test temporary directory is the first argument.
    33  	tempdir := os.Args[1]
    34  
    35  	tcs := []struct {
    36  		name     string
    37  		testfunc func() error
    38  	}{{
    39  		name: "unix.Mount to folder succeeds",
    40  		testfunc: func() error {
    41  			dir2Path := filepath.Join(tempdir, "subdir2")
    42  			if err := unix.Mount(filepath.Join(tempdir, "subdir"), dir2Path, "bind", unix.MS_BIND, ""); err != nil {
    43  				return fmt.Errorf("mount: %v", err)
    44  			}
    45  			return unix.Unmount(dir2Path, unix.MNT_DETACH)
    46  		},
    47  	}, {
    48  		// unix.Mount doesn't care whether the target is a symlink.
    49  		name: "unix.Mount to symlink succeeds",
    50  		testfunc: func() error {
    51  			symlinkPath := filepath.Join(tempdir, "symlink")
    52  			if err := unix.Mount(filepath.Join(tempdir, "subdir"), symlinkPath, "bind", unix.MS_BIND, ""); err != nil {
    53  				return fmt.Errorf("mount: %v", err)
    54  			}
    55  			return unix.Unmount(symlinkPath, unix.MNT_DETACH)
    56  		},
    57  	}, {
    58  		name: "SafeMount to folder succeeds",
    59  		testfunc: func() error {
    60  			dir2Path := filepath.Join(tempdir, "subdir2")
    61  			if err := specutils.SafeMount(filepath.Join(tempdir, "subdir"), dir2Path, "bind", unix.MS_BIND, "", "/proc"); err != nil {
    62  				return fmt.Errorf("SafeMount: %v", err)
    63  			}
    64  			return unix.Unmount(dir2Path, unix.MNT_DETACH)
    65  		},
    66  	}, {
    67  		name: "SafeMount to symlink fails",
    68  		testfunc: func() error {
    69  			err := specutils.SafeMount(filepath.Join(tempdir, "subdir"), filepath.Join(tempdir, "symlink"), "bind", unix.MS_BIND, "", "/proc")
    70  			if err == nil {
    71  				return fmt.Errorf("SafeMount didn't fail, but should have")
    72  			}
    73  			var symErr *specutils.ErrSymlinkMount
    74  			if !errors.As(err, &symErr) {
    75  				return fmt.Errorf("expected SafeMount to fail with ErrSymlinkMount, but got: %v", err)
    76  			}
    77  			return nil
    78  		},
    79  	}}
    80  
    81  	for _, tc := range tcs {
    82  		if err := runTest(tempdir, tc.testfunc); err != nil {
    83  			log.Fatalf("failed test %q: %v", tc.name, err)
    84  		}
    85  	}
    86  }
    87  
    88  // runTest runs testfunc with the following directory structure:
    89  //
    90  //	 tempdir/
    91  //		subdir/
    92  //		subdir2/
    93  //		symlink --> ./subdir2
    94  func runTest(tempdir string, testfunc func() error) error {
    95  	// Create tempdir/subdir/.
    96  	dirPath := filepath.Join(tempdir, "subdir")
    97  	if err := os.Mkdir(dirPath, 0777); err != nil {
    98  		return fmt.Errorf("os.Mkdir(%s, 0777)", dirPath)
    99  	}
   100  	defer os.Remove(dirPath)
   101  
   102  	// Create tempdir/subdir2/.
   103  	dir2Path := filepath.Join(tempdir, "subdir2")
   104  	if err := os.Mkdir(dir2Path, 0777); err != nil {
   105  		return fmt.Errorf("os.Mkdir(%s, 0777)", dir2Path)
   106  	}
   107  	defer os.Remove(dir2Path)
   108  
   109  	// Create tempdir/symlink, which points to ./subdir2.
   110  	symlinkPath := filepath.Join(tempdir, "symlink")
   111  	if err := os.Symlink("./subdir2", symlinkPath); err != nil {
   112  		return fmt.Errorf("failed to create symlink %s: %v", symlinkPath, err)
   113  	}
   114  	defer os.Remove(symlinkPath)
   115  
   116  	// Run the actual test.
   117  	return testfunc()
   118  }