From c5fda9be5146d1e3b815f50b60b85cc535aa0215 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Wed, 22 Apr 2020 09:14:09 -0400 Subject: Move selinux labeling support from pkg/util to pkg/selinux The goal here is to make the package less heavy and not overload the pkg/util. Signed-off-by: Daniel J Walsh --- pkg/selinux/selinux.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 pkg/selinux/selinux.go diff --git a/pkg/selinux/selinux.go b/pkg/selinux/selinux.go new file mode 100644 index 000000000..6eb3b5076 --- /dev/null +++ b/pkg/selinux/selinux.go @@ -0,0 +1,40 @@ +package util + +import ( + "github.com/opencontainers/selinux/go-selinux" +) + +// SELinuxKVMLabel returns labels for running kvm isolated containers +func SELinuxKVMLabel(cLabel string) (string, error) { + if cLabel == "" { + // selinux is disabled + return "", nil + } + processLabel, _ := selinux.KVMContainerLabels() + selinux.ReleaseLabel(processLabel) + return swapSELinuxLabel(cLabel, processLabel) +} + +// SELinuxInitLabel returns labels for running systemd based containers +func SELinuxInitLabel(cLabel string) (string, error) { + if cLabel == "" { + // selinux is disabled + return "", nil + } + processLabel, _ := selinux.InitContainerLabels() + selinux.ReleaseLabel(processLabel) + return swapSELinuxLabel(cLabel, processLabel) +} + +func swapSELinuxLabel(cLabel, processLabel string) (string, error) { + dcon, err := selinux.NewContext(cLabel) + if err != nil { + return "", err + } + scon, err := selinux.NewContext(processLabel) + if err != nil { + return "", err + } + dcon["type"] = scon["type"] + return dcon.Get(), nil +} -- cgit v1.2.3-54-g00ecf