github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/gofer/session_state.go (about)

     1  // Copyright 2018 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 gofer
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/context"
    21  	"github.com/SagerNet/gvisor/pkg/p9"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    23  	"github.com/SagerNet/gvisor/pkg/unet"
    24  )
    25  
    26  // beforeSave is invoked by stateify.
    27  func (s *session) beforeSave() {
    28  	if s.overrides != nil {
    29  		ctx := &dummyClockContext{context.Background()}
    30  		if err := s.fillPathMap(ctx); err != nil {
    31  			panic("failed to save paths to override map before saving" + err.Error())
    32  		}
    33  	}
    34  }
    35  
    36  // afterLoad is invoked by stateify.
    37  func (s *session) afterLoad() {
    38  	// The restore environment contains the 9p connection of this mount.
    39  	fsys := filesystem{}
    40  	env, ok := fs.CurrentRestoreEnvironment()
    41  	if !ok {
    42  		panic("failed to find restore environment")
    43  	}
    44  	mounts, ok := env.MountSources[fsys.Name()]
    45  	if !ok {
    46  		panic("failed to find mounts for filesystem type " + fsys.Name())
    47  	}
    48  	var args fs.MountArgs
    49  	var found bool
    50  	for _, mount := range mounts {
    51  		if mount.Dev == s.connID {
    52  			args = mount
    53  			found = true
    54  		}
    55  	}
    56  	if !found {
    57  		panic(fmt.Sprintf("no connection for connection id %q", s.connID))
    58  	}
    59  
    60  	// Validate the mount flags and options.
    61  	opts, err := options(args.DataString)
    62  	if err != nil {
    63  		panic("failed to parse mount options: " + err.Error())
    64  	}
    65  	if opts.msize != s.msize {
    66  		panic(fmt.Sprintf("new message size %v, want %v", opts.msize, s.msize))
    67  	}
    68  	if opts.version != s.version {
    69  		panic(fmt.Sprintf("new version %v, want %v", opts.version, s.version))
    70  	}
    71  	if opts.policy != s.cachePolicy {
    72  		panic(fmt.Sprintf("new cache policy %v, want %v", opts.policy, s.cachePolicy))
    73  	}
    74  	if opts.aname != s.aname {
    75  		panic(fmt.Sprintf("new attach name %v, want %v", opts.aname, s.aname))
    76  	}
    77  
    78  	// Check if overrideMaps exist when uds sockets are enabled (only pathmaps
    79  	// will actually have been saved).
    80  	if opts.privateunixsocket != (s.overrides != nil) {
    81  		panic(fmt.Sprintf("new privateunixsocket option %v, want %v", opts.privateunixsocket, s.overrides != nil))
    82  	}
    83  	if args.Flags != s.superBlockFlags {
    84  		panic(fmt.Sprintf("new mount flags %v, want %v", args.Flags, s.superBlockFlags))
    85  	}
    86  
    87  	// Manually restore the connection.
    88  	conn, err := unet.NewSocket(opts.fd)
    89  	if err != nil {
    90  		panic(fmt.Sprintf("failed to create Socket for FD %d: %v", opts.fd, err))
    91  	}
    92  
    93  	// Manually restore the client.
    94  	s.client, err = p9.NewClient(conn, s.msize, s.version)
    95  	if err != nil {
    96  		panic(fmt.Sprintf("failed to connect client to server: %v", err))
    97  	}
    98  
    99  	// Manually restore the attach point.
   100  	s.attach.file, err = s.client.Attach(s.aname)
   101  	if err != nil {
   102  		panic(fmt.Sprintf("failed to attach to aname: %v", err))
   103  	}
   104  
   105  	// If private unix sockets are enabled, create and fill the session's endpoint
   106  	// maps.
   107  	if opts.privateunixsocket {
   108  		ctx := &dummyClockContext{context.Background()}
   109  
   110  		if err = s.restoreEndpointMaps(ctx); err != nil {
   111  			panic("failed to restore endpoint maps: " + err.Error())
   112  		}
   113  	}
   114  }