Skip to content

Commit

Permalink
Lazy initialization for labels
Browse files Browse the repository at this point in the history
Perform lazy (on demand) init of labels.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Sep 8, 2021
1 parent 7213bfa commit 397b931
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
2 changes: 2 additions & 0 deletions go-selinux/selinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,7 @@ func GetDefaultContextWithLevel(user, level, scon string) (string, error) {

// PrivContainerMountLabel returns mount label for privileged containers
func PrivContainerMountLabel() string {
// Make sure label is loaded.
_ = label("")
return privContainerMountLabel
}
42 changes: 26 additions & 16 deletions go-selinux/selinux_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ var (
// for policyRoot()
policyRootOnce sync.Once
policyRootVal string

// for label()
loadLabelsOnce sync.Once
labels map[string]string
privContainerMountLabel string
)

func policyRoot() string {
Expand Down Expand Up @@ -904,13 +909,11 @@ func openContextFile() (*os.File, error) {
return os.Open(filepath.Join(policyRoot(), "/contexts/lxc_contexts"))
}

var labels, privContainerMountLabel = loadLabels()

func loadLabels() (map[string]string, string) {
labels := make(map[string]string)
func loadLabels() {
labels = make(map[string]string)
in, err := openContextFile()
if err != nil {
return labels, ""
return
}
defer in.Close()

Expand All @@ -934,30 +937,37 @@ func loadLabels() (map[string]string, string) {

con, _ := NewContext(labels["file"])
con["level"] = fmt.Sprintf("s0:c%d,c%d", maxCategory-2, maxCategory-1)
reserveLabel(con.get())
return labels, con.get()
privContainerMountLabel = con.get()
reserveLabel(privContainerMountLabel)
}

func label(key string) string {
loadLabelsOnce.Do(func() {
loadLabels()
})
return labels[key]
}

// kvmContainerLabels returns the default processLabel and mountLabel to be used
// for kvm containers by the calling process.
func kvmContainerLabels() (string, string) {
processLabel := labels["kvm_process"]
processLabel := label("kvm_process")
if processLabel == "" {
processLabel = labels["process"]
processLabel = label("process")
}

return addMcs(processLabel, labels["file"])
return addMcs(processLabel, label("file"))
}

// initContainerLabels returns the default processLabel and file labels to be
// used for containers running an init system like systemd by the calling process.
func initContainerLabels() (string, string) {
processLabel := labels["init_process"]
processLabel := label("init_process")
if processLabel == "" {
processLabel = labels["process"]
processLabel = label("process")
}

return addMcs(processLabel, labels["file"])
return addMcs(processLabel, label("file"))
}

// containerLabels returns an allocated processLabel and fileLabel to be used for
Expand All @@ -967,9 +977,9 @@ func containerLabels() (processLabel string, fileLabel string) {
return "", ""
}

processLabel = labels["process"]
fileLabel = labels["file"]
readOnlyFileLabel = labels["ro_file"]
processLabel = label("process")
fileLabel = label("file")
readOnlyFileLabel = label("ro_file")

if processLabel == "" || fileLabel == "" {
return "", fileLabel
Expand Down
2 changes: 0 additions & 2 deletions go-selinux/selinux_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func TestKVMLabels(t *testing.T) {
t.Skip("SELinux not enabled, skipping.")
}

t.Log(labels)

plabel, flabel := KVMContainerLabels()
if plabel == "" {
t.Log("Failed to read kvm label")
Expand Down
2 changes: 2 additions & 0 deletions go-selinux/selinux_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,5 @@ func disableSecOpt() []string {
func getDefaultContextWithLevel(user, level, scon string) (string, error) {
return "", nil
}

func loadLabels() {}

0 comments on commit 397b931

Please sign in to comment.