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