|
| 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 | +package seutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/opencontainers/selinux/go-selinux" |
| 24 | +) |
| 25 | + |
| 26 | +var seTypes map[string]struct{} |
| 27 | + |
| 28 | +const typePath = "/etc/selinux/targeted/contexts/customizable_types" |
| 29 | + |
| 30 | +func init() { |
| 31 | + seTypes = make(map[string]struct{}) |
| 32 | + if !selinux.GetEnabled() { |
| 33 | + return |
| 34 | + } |
| 35 | + f, err := os.Open(typePath) |
| 36 | + if err != nil { |
| 37 | + return |
| 38 | + } |
| 39 | + defer f.Close() |
| 40 | + s := bufio.NewScanner(f) |
| 41 | + for s.Scan() { |
| 42 | + seTypes[s.Text()] = struct{}{} |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// HasType returns true if the underlying system has the |
| 47 | +// provided selinux type enabled. |
| 48 | +func HasType(name string) bool { |
| 49 | + _, ok := seTypes[name] |
| 50 | + return ok |
| 51 | +} |
| 52 | + |
| 53 | +// ChangeToKVM process label |
| 54 | +func ChangeToKVM(l string) (string, error) { |
| 55 | + if l == "" || !selinux.GetEnabled() { |
| 56 | + return "", nil |
| 57 | + } |
| 58 | + proc, _ := selinux.KVMContainerLabels() |
| 59 | + selinux.ReleaseLabel(proc) |
| 60 | + |
| 61 | + current, err := selinux.NewContext(l) |
| 62 | + if err != nil { |
| 63 | + return "", err |
| 64 | + } |
| 65 | + next, err := selinux.NewContext(proc) |
| 66 | + if err != nil { |
| 67 | + return "", err |
| 68 | + } |
| 69 | + current["type"] = next["type"] |
| 70 | + return current.Get(), nil |
| 71 | +} |
0 commit comments