github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/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 newConfig := &specs.LinuxSeccomp{} 86 87 if len(config.Architectures) != 0 && len(config.ArchMap) != 0 { 88 return nil, errors.New("'architectures' and 'archMap' were specified in the seccomp profile, use either 'architectures' or 'archMap'") 89 } 90 91 // if config.Architectures == 0 then libseccomp will figure out the architecture to use 92 if len(config.Architectures) != 0 { 93 newConfig.Architectures = config.Architectures 94 } 95 96 arch := goToNative[runtime.GOARCH] 97 seccompArch, archExists := nativeToSeccomp[arch] 98 99 if len(config.ArchMap) != 0 && archExists { 100 for _, a := range config.ArchMap { 101 if a.Arch == seccompArch { 102 newConfig.Architectures = append(newConfig.Architectures, a.Arch) 103 newConfig.Architectures = append(newConfig.Architectures, a.SubArches...) 104 break 105 } 106 } 107 } 108 109 newConfig.DefaultAction = config.DefaultAction 110 111 Loop: 112 // Loop through all syscall blocks and convert them to libcontainer format after filtering them 113 for _, call := range config.Syscalls { 114 if call.Excludes != nil { 115 if len(call.Excludes.Arches) > 0 { 116 if inSlice(call.Excludes.Arches, arch) { 117 continue Loop 118 } 119 } 120 if len(call.Excludes.Caps) > 0 { 121 for _, c := range call.Excludes.Caps { 122 if inSlice(rs.Process.Capabilities.Bounding, c) { 123 continue Loop 124 } 125 } 126 } 127 if call.Excludes.MinKernel != nil { 128 if ok, err := kernelGreaterEqualThan(*call.Excludes.MinKernel); err != nil { 129 return nil, err 130 } else if ok { 131 continue Loop 132 } 133 } 134 } 135 if call.Includes != nil { 136 if len(call.Includes.Arches) > 0 { 137 if !inSlice(call.Includes.Arches, arch) { 138 continue Loop 139 } 140 } 141 if len(call.Includes.Caps) > 0 { 142 for _, c := range call.Includes.Caps { 143 if !inSlice(rs.Process.Capabilities.Bounding, c) { 144 continue Loop 145 } 146 } 147 } 148 if call.Includes.MinKernel != nil { 149 if ok, err := kernelGreaterEqualThan(*call.Includes.MinKernel); err != nil { 150 return nil, err 151 } else if !ok { 152 continue Loop 153 } 154 } 155 } 156 157 if call.Name != "" { 158 if len(call.Names) != 0 { 159 return nil, errors.New("'name' and 'names' were specified in the seccomp profile, use either 'name' or 'names'") 160 } 161 call.Names = append(call.Names, call.Name) 162 } 163 164 newConfig.Syscalls = append(newConfig.Syscalls, call.LinuxSyscall) 165 } 166 167 return newConfig, nil 168 }