github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/mm/metadata.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 mm
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/context"
    19  	"github.com/SagerNet/gvisor/pkg/hostarch"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fsbridge"
    22  )
    23  
    24  // Dumpability describes if and how core dumps should be created.
    25  type Dumpability int
    26  
    27  const (
    28  	// NotDumpable indicates that core dumps should never be created.
    29  	NotDumpable Dumpability = iota
    30  
    31  	// UserDumpable indicates that core dumps should be created, owned by
    32  	// the current user.
    33  	UserDumpable
    34  
    35  	// RootDumpable indicates that core dumps should be created, owned by
    36  	// root.
    37  	RootDumpable
    38  )
    39  
    40  // Dumpability returns the dumpability.
    41  func (mm *MemoryManager) Dumpability() Dumpability {
    42  	mm.metadataMu.Lock()
    43  	defer mm.metadataMu.Unlock()
    44  	return mm.dumpability
    45  }
    46  
    47  // SetDumpability sets the dumpability.
    48  func (mm *MemoryManager) SetDumpability(d Dumpability) {
    49  	mm.metadataMu.Lock()
    50  	defer mm.metadataMu.Unlock()
    51  	mm.dumpability = d
    52  }
    53  
    54  // ArgvStart returns the start of the application argument vector.
    55  //
    56  // There is no guarantee that this value is sensible w.r.t. ArgvEnd.
    57  func (mm *MemoryManager) ArgvStart() hostarch.Addr {
    58  	mm.metadataMu.Lock()
    59  	defer mm.metadataMu.Unlock()
    60  	return mm.argv.Start
    61  }
    62  
    63  // SetArgvStart sets the start of the application argument vector.
    64  func (mm *MemoryManager) SetArgvStart(a hostarch.Addr) {
    65  	mm.metadataMu.Lock()
    66  	defer mm.metadataMu.Unlock()
    67  	mm.argv.Start = a
    68  }
    69  
    70  // ArgvEnd returns the end of the application argument vector.
    71  //
    72  // There is no guarantee that this value is sensible w.r.t. ArgvStart.
    73  func (mm *MemoryManager) ArgvEnd() hostarch.Addr {
    74  	mm.metadataMu.Lock()
    75  	defer mm.metadataMu.Unlock()
    76  	return mm.argv.End
    77  }
    78  
    79  // SetArgvEnd sets the end of the application argument vector.
    80  func (mm *MemoryManager) SetArgvEnd(a hostarch.Addr) {
    81  	mm.metadataMu.Lock()
    82  	defer mm.metadataMu.Unlock()
    83  	mm.argv.End = a
    84  }
    85  
    86  // EnvvStart returns the start of the application environment vector.
    87  //
    88  // There is no guarantee that this value is sensible w.r.t. EnvvEnd.
    89  func (mm *MemoryManager) EnvvStart() hostarch.Addr {
    90  	mm.metadataMu.Lock()
    91  	defer mm.metadataMu.Unlock()
    92  	return mm.envv.Start
    93  }
    94  
    95  // SetEnvvStart sets the start of the application environment vector.
    96  func (mm *MemoryManager) SetEnvvStart(a hostarch.Addr) {
    97  	mm.metadataMu.Lock()
    98  	defer mm.metadataMu.Unlock()
    99  	mm.envv.Start = a
   100  }
   101  
   102  // EnvvEnd returns the end of the application environment vector.
   103  //
   104  // There is no guarantee that this value is sensible w.r.t. EnvvStart.
   105  func (mm *MemoryManager) EnvvEnd() hostarch.Addr {
   106  	mm.metadataMu.Lock()
   107  	defer mm.metadataMu.Unlock()
   108  	return mm.envv.End
   109  }
   110  
   111  // SetEnvvEnd sets the end of the application environment vector.
   112  func (mm *MemoryManager) SetEnvvEnd(a hostarch.Addr) {
   113  	mm.metadataMu.Lock()
   114  	defer mm.metadataMu.Unlock()
   115  	mm.envv.End = a
   116  }
   117  
   118  // Auxv returns the current map of auxiliary vectors.
   119  func (mm *MemoryManager) Auxv() arch.Auxv {
   120  	mm.metadataMu.Lock()
   121  	defer mm.metadataMu.Unlock()
   122  	return append(arch.Auxv(nil), mm.auxv...)
   123  }
   124  
   125  // SetAuxv sets the entire map of auxiliary vectors.
   126  func (mm *MemoryManager) SetAuxv(auxv arch.Auxv) {
   127  	mm.metadataMu.Lock()
   128  	defer mm.metadataMu.Unlock()
   129  	mm.auxv = append(arch.Auxv(nil), auxv...)
   130  }
   131  
   132  // Executable returns the executable, if available.
   133  //
   134  // An additional reference will be taken in the case of a non-nil executable,
   135  // which must be released by the caller.
   136  func (mm *MemoryManager) Executable() fsbridge.File {
   137  	mm.metadataMu.Lock()
   138  	defer mm.metadataMu.Unlock()
   139  
   140  	if mm.executable == nil {
   141  		return nil
   142  	}
   143  
   144  	mm.executable.IncRef()
   145  	return mm.executable
   146  }
   147  
   148  // SetExecutable sets the executable.
   149  //
   150  // This takes a reference on d.
   151  func (mm *MemoryManager) SetExecutable(ctx context.Context, file fsbridge.File) {
   152  	mm.metadataMu.Lock()
   153  
   154  	// Grab a new reference.
   155  	file.IncRef()
   156  
   157  	// Set the executable.
   158  	orig := mm.executable
   159  	mm.executable = file
   160  
   161  	mm.metadataMu.Unlock()
   162  
   163  	// Release the old reference.
   164  	//
   165  	// Do this without holding the lock, since it may wind up doing some
   166  	// I/O to sync the dirent, etc.
   167  	if orig != nil {
   168  		orig.DecRef(ctx)
   169  	}
   170  }
   171  
   172  // VDSOSigReturn returns the address of vdso_sigreturn.
   173  func (mm *MemoryManager) VDSOSigReturn() uint64 {
   174  	mm.metadataMu.Lock()
   175  	defer mm.metadataMu.Unlock()
   176  	return mm.vdsoSigReturnAddr
   177  }
   178  
   179  // SetVDSOSigReturn sets the address of vdso_sigreturn.
   180  func (mm *MemoryManager) SetVDSOSigReturn(addr uint64) {
   181  	mm.metadataMu.Lock()
   182  	defer mm.metadataMu.Unlock()
   183  	mm.vdsoSigReturnAddr = addr
   184  }