aboutsummaryrefslogtreecommitdiff
path: root/pkg/selinux/selinux.go
blob: 6b6d065f7fc40a328a1db155bc4505b955a4f902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package selinux

import (
	"github.com/opencontainers/selinux/go-selinux"
)

// KVMLabel returns labels for running kvm isolated containers
func KVMLabel(cLabel string) (string, error) {
	if cLabel == "" {
		// selinux is disabled
		return "", nil
	}
	processLabel, _ := selinux.KVMContainerLabels()
	selinux.ReleaseLabel(processLabel)
	return swapSELinuxLabel(cLabel, processLabel)
}

// InitLabel returns labels for running systemd based containers
func InitLabel(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
}