github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/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 len(call.Excludes.Arches) > 0 { 115 if inSlice(call.Excludes.Arches, arch) { 116 continue Loop 117 } 118 } 119 if len(call.Excludes.Caps) > 0 { 120 for _, c := range call.Excludes.Caps { 121 if inSlice(rs.Process.Capabilities.Bounding, c) { 122 continue Loop 123 } 124 } 125 } 126 if call.Excludes.MinKernel != nil { 127 if ok, err := kernelGreaterEqualThan(*call.Excludes.MinKernel); err != nil { 128 return nil, err 129 } else if ok { 130 continue Loop 131 } 132 } 133 if len(call.Includes.Arches) > 0 { 134 if !inSlice(call.Includes.Arches, arch) { 135 continue Loop 136 } 137 } 138 if len(call.Includes.Caps) > 0 { 139 for _, c := range call.Includes.Caps { 140 if !inSlice(rs.Process.Capabilities.Bounding, c) { 141 continue Loop 142 } 143 } 144 } 145 if call.Includes.MinKernel != nil { 146 if ok, err := kernelGreaterEqualThan(*call.Includes.MinKernel); err != nil { 147 return nil, err 148 } else if !ok { 149 continue Loop 150 } 151 } 152 153 newCall := specs.LinuxSyscall{ 154 Action: call.Action, 155 ErrnoRet: call.ErrnoRet, 156 } 157 if call.Name != "" && len(call.Names) != 0 { 158 return nil, errors.New("'name' and 'names' were specified in the seccomp profile, use either 'name' or 'names'") 159 } 160 if call.Name != "" { 161 newCall.Names = []string{call.Name} 162 } else { 163 newCall.Names = call.Names 164 } 165 // Loop through all the arguments of the syscall and convert them 166 for _, arg := range call.Args { 167 newCall.Args = append(newCall.Args, *arg) 168 } 169 170 newConfig.Syscalls = append(newConfig.Syscalls, newCall) 171 } 172 173 return newConfig, nil 174 }