github.com/rish1988/moby@v25.0.2+incompatible/profiles/seccomp/seccomp_linux.go (about)

     1  //go:generate go run -tags 'seccomp' generate.go
     2  
     3  package seccomp // import "github.com/docker/docker/profiles/seccomp"
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"runtime"
    10  
    11  	specs "github.com/opencontainers/runtime-spec/specs-go"
    12  )
    13  
    14  // GetDefaultProfile returns the default seccomp profile.
    15  func GetDefaultProfile(rs *specs.Spec) (*specs.LinuxSeccomp, error) {
    16  	return setupSeccomp(DefaultProfile(), rs)
    17  }
    18  
    19  // LoadProfile takes a json string and decodes the seccomp profile.
    20  func LoadProfile(body string, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
    21  	var config Seccomp
    22  	if err := json.Unmarshal([]byte(body), &config); err != nil {
    23  		return nil, fmt.Errorf("Decoding seccomp profile failed: %v", err)
    24  	}
    25  	return setupSeccomp(&config, rs)
    26  }
    27  
    28  // libseccomp string => seccomp arch
    29  var nativeToSeccomp = map[string]specs.Arch{
    30  	"x86":         specs.ArchX86,
    31  	"amd64":       specs.ArchX86_64,
    32  	"arm":         specs.ArchARM,
    33  	"arm64":       specs.ArchAARCH64,
    34  	"mips64":      specs.ArchMIPS64,
    35  	"mips64n32":   specs.ArchMIPS64N32,
    36  	"mipsel64":    specs.ArchMIPSEL64,
    37  	"mips3l64n32": specs.ArchMIPSEL64N32,
    38  	"mipsle":      specs.ArchMIPSEL,
    39  	"ppc":         specs.ArchPPC,
    40  	"ppc64":       specs.ArchPPC64,
    41  	"ppc64le":     specs.ArchPPC64LE,
    42  	"s390":        specs.ArchS390,
    43  	"s390x":       specs.ArchS390X,
    44  }
    45  
    46  // GOARCH => libseccomp string
    47  var goToNative = map[string]string{
    48  	"386":         "x86",
    49  	"amd64":       "amd64",
    50  	"arm":         "arm",
    51  	"arm64":       "arm64",
    52  	"mips64":      "mips64",
    53  	"mips64p32":   "mips64n32",
    54  	"mips64le":    "mipsel64",
    55  	"mips64p32le": "mips3l64n32",
    56  	"mipsle":      "mipsel",
    57  	"ppc":         "ppc",
    58  	"ppc64":       "ppc64",
    59  	"ppc64le":     "ppc64le",
    60  	"s390":        "s390",
    61  	"s390x":       "s390x",
    62  }
    63  
    64  // inSlice tests whether a string is contained in a slice of strings or not.
    65  // Comparison is case sensitive
    66  func inSlice(slice []string, s string) bool {
    67  	for _, ss := range slice {
    68  		if s == ss {
    69  			return true
    70  		}
    71  	}
    72  	return false
    73  }
    74  
    75  func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
    76  	if config == nil {
    77  		return nil, nil
    78  	}
    79  
    80  	// No default action specified, no syscalls listed, assume seccomp disabled
    81  	if config.DefaultAction == "" && len(config.Syscalls) == 0 {
    82  		return nil, nil
    83  	}
    84  
    85  	if len(config.Architectures) != 0 && len(config.ArchMap) != 0 {
    86  		return nil, errors.New("both 'architectures' and 'archMap' are specified in the seccomp profile, use either 'architectures' or 'archMap'")
    87  	}
    88  
    89  	if len(config.LinuxSeccomp.Syscalls) != 0 {
    90  		// The Seccomp type overrides the LinuxSeccomp.Syscalls field,
    91  		// so 'this should never happen' when loaded from JSON, but could
    92  		// happen if someone constructs the Config from source.
    93  		return nil, errors.New("the LinuxSeccomp.Syscalls field should be empty")
    94  	}
    95  
    96  	var (
    97  		// Copy all common / standard properties to the output profile
    98  		newConfig = &config.LinuxSeccomp
    99  		arch      = goToNative[runtime.GOARCH]
   100  	)
   101  	if seccompArch, ok := nativeToSeccomp[arch]; ok {
   102  		for _, a := range config.ArchMap {
   103  			if a.Arch == seccompArch {
   104  				newConfig.Architectures = append(newConfig.Architectures, a.Arch)
   105  				newConfig.Architectures = append(newConfig.Architectures, a.SubArches...)
   106  				break
   107  			}
   108  		}
   109  	}
   110  
   111  Loop:
   112  	// Convert Syscall to OCI runtimes-spec specs.LinuxSyscall after filtering them.
   113  	for _, call := range config.Syscalls {
   114  		if call.Name != "" {
   115  			if len(call.Names) != 0 {
   116  				return nil, errors.New("both 'name' and 'names' are specified in the seccomp profile, use either 'name' or 'names'")
   117  			}
   118  			call.Names = []string{call.Name}
   119  		}
   120  		if call.Excludes != nil {
   121  			if len(call.Excludes.Arches) > 0 {
   122  				if inSlice(call.Excludes.Arches, arch) {
   123  					continue Loop
   124  				}
   125  			}
   126  			if len(call.Excludes.Caps) > 0 {
   127  				for _, c := range call.Excludes.Caps {
   128  					if inSlice(rs.Process.Capabilities.Bounding, c) {
   129  						continue Loop
   130  					}
   131  				}
   132  			}
   133  			if call.Excludes.MinKernel != nil {
   134  				if ok, err := kernelGreaterEqualThan(*call.Excludes.MinKernel); err != nil {
   135  					return nil, err
   136  				} else if ok {
   137  					continue Loop
   138  				}
   139  			}
   140  		}
   141  		if call.Includes != nil {
   142  			if len(call.Includes.Arches) > 0 {
   143  				if !inSlice(call.Includes.Arches, arch) {
   144  					continue Loop
   145  				}
   146  			}
   147  			if len(call.Includes.Caps) > 0 {
   148  				for _, c := range call.Includes.Caps {
   149  					if !inSlice(rs.Process.Capabilities.Bounding, c) {
   150  						continue Loop
   151  					}
   152  				}
   153  			}
   154  			if call.Includes.MinKernel != nil {
   155  				if ok, err := kernelGreaterEqualThan(*call.Includes.MinKernel); err != nil {
   156  					return nil, err
   157  				} else if !ok {
   158  					continue Loop
   159  				}
   160  			}
   161  		}
   162  		newConfig.Syscalls = append(newConfig.Syscalls, call.LinuxSyscall)
   163  	}
   164  
   165  	return newConfig, nil
   166  }