|
| 1 | +/* |
| 2 | +Copyright The containerd Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | + Copyright The runc Authors. |
| 19 | +
|
| 20 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + you may not use this file except in compliance with the License. |
| 22 | + You may obtain a copy of the License at |
| 23 | +
|
| 24 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | +
|
| 26 | + Unless required by applicable law or agreed to in writing, software |
| 27 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + See the License for the specific language governing permissions and |
| 30 | + limitations under the License. |
| 31 | +*/ |
| 32 | + |
| 33 | +package seccomp |
| 34 | + |
| 35 | +import ( |
| 36 | + "bufio" |
| 37 | + "os" |
| 38 | + "strings" |
| 39 | + |
| 40 | + "golang.org/x/sys/unix" |
| 41 | +) |
| 42 | + |
| 43 | +// IsEnabled returns if the kernel has been configured to support seccomp. |
| 44 | +// From https://github.com/opencontainers/runc/blob/v1.0.0-rc91/libcontainer/seccomp/seccomp_linux.go#L86-L102 |
| 45 | +func IsEnabled() bool { |
| 46 | + // Try to read from /proc/self/status for kernels > 3.8 |
| 47 | + s, err := parseStatusFile("/proc/self/status") |
| 48 | + if err != nil { |
| 49 | + // Check if Seccomp is supported, via CONFIG_SECCOMP. |
| 50 | + if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL { |
| 51 | + // Make sure the kernel has CONFIG_SECCOMP_FILTER. |
| 52 | + if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL { |
| 53 | + return true |
| 54 | + } |
| 55 | + } |
| 56 | + return false |
| 57 | + } |
| 58 | + _, ok := s["Seccomp"] |
| 59 | + return ok |
| 60 | +} |
| 61 | + |
| 62 | +// parseStatusFile is from https://github.com/opencontainers/runc/blob/v1.0.0-rc91/libcontainer/seccomp/seccomp_linux.go#L243-L268 |
| 63 | +func parseStatusFile(path string) (map[string]string, error) { |
| 64 | + f, err := os.Open(path) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + defer f.Close() |
| 69 | + |
| 70 | + s := bufio.NewScanner(f) |
| 71 | + status := make(map[string]string) |
| 72 | + |
| 73 | + for s.Scan() { |
| 74 | + text := s.Text() |
| 75 | + parts := strings.Split(text, ":") |
| 76 | + |
| 77 | + if len(parts) <= 1 { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + status[parts[0]] = parts[1] |
| 82 | + } |
| 83 | + if err := s.Err(); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + return status, nil |
| 88 | +} |
0 commit comments