github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/fdimport/fdimport.go (about)

     1  // Copyright 2020 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 fdimport provides the Import function.
    16  package fdimport
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"golang.org/x/sys/unix"
    22  	"github.com/MerlinKodo/gvisor/pkg/context"
    23  	"github.com/MerlinKodo/gvisor/pkg/fd"
    24  	"github.com/MerlinKodo/gvisor/pkg/sentry/fsimpl/host"
    25  	"github.com/MerlinKodo/gvisor/pkg/sentry/kernel"
    26  	"github.com/MerlinKodo/gvisor/pkg/sentry/kernel/auth"
    27  	"github.com/MerlinKodo/gvisor/pkg/sentry/vfs"
    28  )
    29  
    30  // Import imports a map of FDs into the given FDTable. If console is true,
    31  // sets up TTY for sentry stdin, stdout, and stderr FDs. Used FDs are either
    32  // closed or released. It's safe for the caller to close any remaining files
    33  // upon return.
    34  func Import(ctx context.Context, fdTable *kernel.FDTable, console bool, uid auth.KUID, gid auth.KGID, fds map[int]*fd.FD) (*host.TTYFileDescription, error) {
    35  	k := kernel.KernelFromContext(ctx)
    36  	if k == nil {
    37  		return nil, fmt.Errorf("cannot find kernel from context")
    38  	}
    39  	mnt := k.HostMount()
    40  
    41  	// Collect host fds and flags. Do this before importing any fds because
    42  	// multiple fds may refer to the same file, and importing fds may
    43  	// change flags.
    44  	fdFlags := make(map[*fd.FD]kernel.FDFlags)
    45  	for _, hostFD := range fds {
    46  		var err error
    47  		fdFlags[hostFD], err = getFDFlags(hostFD)
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  	}
    52  
    53  	var ttyFile *vfs.FileDescription
    54  	for appFD, hostFD := range fds {
    55  		fdOpts := host.NewFDOptions{
    56  			Savable: true,
    57  		}
    58  		if uid != auth.NoID || gid != auth.NoID {
    59  			fdOpts.VirtualOwner = true
    60  			fdOpts.UID = uid
    61  			fdOpts.GID = gid
    62  		}
    63  		var appFile *vfs.FileDescription
    64  
    65  		if console && appFD < 3 {
    66  			// Import the file as a host TTY file.
    67  			if ttyFile == nil {
    68  				fdOpts.IsTTY = true
    69  				var err error
    70  				appFile, err = host.NewFD(ctx, mnt, hostFD.FD(), &fdOpts)
    71  				if err != nil {
    72  					return nil, err
    73  				}
    74  				defer appFile.DecRef(ctx)
    75  				hostFD.Release() // FD is transfered to host FD.
    76  
    77  				// Remember this in the TTY file, as we will use it for the other stdio
    78  				// FDs.
    79  				ttyFile = appFile
    80  			} else {
    81  				// Re-use the existing TTY file, as all three stdio FDs must point to
    82  				// the same fs.File in order to share TTY state, specifically the
    83  				// foreground process group id.
    84  				appFile = ttyFile
    85  			}
    86  		} else {
    87  			var err error
    88  			appFile, err = host.NewFD(ctx, mnt, hostFD.FD(), &fdOpts)
    89  			if err != nil {
    90  				return nil, err
    91  			}
    92  			defer appFile.DecRef(ctx)
    93  			hostFD.Release() // FD is transfered to host FD.
    94  		}
    95  
    96  		if err := fdTable.NewFDAt(ctx, int32(appFD), appFile, fdFlags[hostFD]); err != nil {
    97  			return nil, err
    98  		}
    99  	}
   100  	if ttyFile == nil {
   101  		return nil, nil
   102  	}
   103  	return ttyFile.Impl().(*host.TTYFileDescription), nil
   104  }
   105  
   106  func getFDFlags(f *fd.FD) (kernel.FDFlags, error) {
   107  	fdflags, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(f.FD()), unix.F_GETFD, 0)
   108  	if errno != 0 {
   109  		return kernel.FDFlags{}, fmt.Errorf("failed to get fd flags for fd %d (errno=%d)", f, errno)
   110  	}
   111  	return kernel.FDFlags{
   112  		CloseOnExec: fdflags&unix.O_CLOEXEC != 0,
   113  	}, nil
   114  }