Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go-selinux/label/label_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
if !selinux.GetEnabled() {
return "", "", nil
}
if selinux.GetContainerLabelsSize() == int(selinux.CategoryRange*(selinux.CategoryRange-1)/2) {
return "", "", fmt.Errorf("SELinux label exhaustion: %d labels used", selinux.GetContainerLabelsSize())
}
processLabel, mountLabel := selinux.ContainerLabels()
if processLabel != "" {
defer func() {
Expand Down
17 changes: 17 additions & 0 deletions go-selinux/label/label_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package label

import (
"errors"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -53,6 +54,22 @@ func TestInit(t *testing.T) {
}
}

func TestSELinuxLabelExhaustion(t *testing.T) {
needSELinux(t)
selinux.CategoryRange = 5
var testNull []string
for i := 0; i < 20; i++ {
_, _, err := InitLabels(testNull)
if i == 19 {
if err == nil {
t.Fatal("err should not be nil")
} else if err.Error() != fmt.Sprintf("SELinux label exhaustion: %d labels used", selinux.GetContainerLabelsSize()) {
t.Fatalf("unexpected error %s", err.Error())
}
}
}
}

func TestRelabel(t *testing.T) {
needSELinux(t)

Expand Down
6 changes: 6 additions & 0 deletions go-selinux/selinux_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ var (
labels map[string]string
)

func GetContainerLabelsSize() int {
state.Lock()
defer state.Unlock()
return len(state.mcsList)
}

func policyRoot() string {
policyRootOnce.Do(func() {
policyRootVal = filepath.Join(selinuxDir, readConfig(selinuxTypeTag))
Expand Down
Loading