github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/platform/mmap_min_addr.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 platform
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/ttpreport/gvisor-ligolo/pkg/hostarch"
    24  )
    25  
    26  // systemMMapMinAddrSource is the source file.
    27  const systemMMapMinAddrSource = "/proc/sys/vm/mmap_min_addr"
    28  
    29  // systemMMapMinAddr is the system's minimum map address.
    30  var systemMMapMinAddr uint64
    31  
    32  // SystemMMapMinAddr returns the minimum system address.
    33  func SystemMMapMinAddr() hostarch.Addr {
    34  	return hostarch.Addr(systemMMapMinAddr)
    35  }
    36  
    37  // MMapMinAddr is a size zero struct that implements MinUserAddress based on
    38  // the system minimum address. It is suitable for embedding in platforms that
    39  // rely on the system mmap, and thus require the system minimum.
    40  type MMapMinAddr struct {
    41  }
    42  
    43  // MinUserAddress implements platform.MinUserAddresss.
    44  func (*MMapMinAddr) MinUserAddress() hostarch.Addr {
    45  	return SystemMMapMinAddr()
    46  }
    47  
    48  func init() {
    49  	// Open the source file.
    50  	b, err := ioutil.ReadFile(systemMMapMinAddrSource)
    51  	if err != nil {
    52  		panic(fmt.Sprintf("couldn't open %s: %v", systemMMapMinAddrSource, err))
    53  	}
    54  
    55  	// Parse the result.
    56  	systemMMapMinAddr, err = strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64)
    57  	if err != nil {
    58  		panic(fmt.Sprintf("couldn't parse %s from %s: %v", string(b), systemMMapMinAddrSource, err))
    59  	}
    60  }