github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/boot/filter/filter.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 filter defines all syscalls the sandbox is allowed to make
    16  // to the host, and installs seccomp filters to prevent prohibited
    17  // syscalls in case it's compromised.
    18  package filter
    19  
    20  import (
    21  	"github.com/SagerNet/gvisor/pkg/log"
    22  	"github.com/SagerNet/gvisor/pkg/seccomp"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/platform"
    24  )
    25  
    26  // Options are seccomp filter related options.
    27  type Options struct {
    28  	Platform      platform.Platform
    29  	HostNetwork   bool
    30  	ProfileEnable bool
    31  	ControllerFD  int
    32  }
    33  
    34  // Install installs seccomp filters for based on the given platform.
    35  func Install(opt Options) error {
    36  	s := allowedSyscalls
    37  	s.Merge(controlServerFilters(opt.ControllerFD))
    38  
    39  	// Set of additional filters used by -race and -msan. Returns empty
    40  	// when not enabled.
    41  	s.Merge(instrumentationFilters())
    42  
    43  	if opt.HostNetwork {
    44  		Report("host networking enabled: syscall filters less restrictive!")
    45  		s.Merge(hostInetFilters())
    46  	}
    47  	if opt.ProfileEnable {
    48  		Report("profile enabled: syscall filters less restrictive!")
    49  		s.Merge(profileFilters())
    50  	}
    51  
    52  	s.Merge(opt.Platform.SyscallFilters())
    53  
    54  	return seccomp.Install(s)
    55  }
    56  
    57  // Report writes a warning message to the log.
    58  func Report(msg string) {
    59  	log.Warningf("*** SECCOMP WARNING: %s", msg)
    60  }