Skip to content

Commit 76f8d4d

Browse files
committed
fix: uniqMcs use all cpu
Signed-off-by: ningmingxiao <[email protected]>
1 parent a8faa24 commit 76f8d4d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

go-selinux/label/label_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
3030
if !selinux.GetEnabled() {
3131
return "", "", nil
3232
}
33+
if selinux.GetContainerLabelsSize() == int(selinux.CategoryRange*(selinux.CategoryRange-1)/2) {
34+
return "", "", fmt.Errorf("SELinux label exhaustion: %d labels used", selinux.GetContainerLabelsSize())
35+
}
3336
processLabel, mountLabel := selinux.ContainerLabels()
3437
if processLabel != "" {
3538
defer func() {

go-selinux/label/label_linux_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package label
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67
"testing"
78

@@ -53,6 +54,24 @@ func TestInit(t *testing.T) {
5354
}
5455
}
5556

57+
func TestSELinuxLabelExhaustion(t *testing.T) {
58+
needSELinux(t)
59+
selinux.CategoryRange = 5
60+
var testNull []string
61+
for i := 0; i < 20; i++ {
62+
_, _, err := InitLabels(testNull)
63+
if i == 19 {
64+
if err == nil {
65+
t.Fatal("err should not be nil")
66+
} else {
67+
if err.Error() != fmt.Sprintf("SELinux label exhaustion: %d labels used", selinux.GetContainerLabelsSize()) {
68+
t.Fatalf("unexpected error %s", err.Error())
69+
}
70+
}
71+
}
72+
}
73+
74+
}
5675
func TestRelabel(t *testing.T) {
5776
needSELinux(t)
5877

go-selinux/selinux_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ var (
8585
labels map[string]string
8686
)
8787

88+
func GetContainerLabelsSize() int {
89+
state.Lock()
90+
defer state.Unlock()
91+
return len(state.mcsList)
92+
}
93+
8894
func policyRoot() string {
8995
policyRootOnce.Do(func() {
9096
policyRootVal = filepath.Join(selinuxDir, readConfig(selinuxTypeTag))

0 commit comments

Comments
 (0)