Skip to content

Commit d083957

Browse files
committed
fix: uniqMcs use all cpu
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent a8faa24 commit d083957

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

go-selinux/label/label_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var validOptions = map[string]bool{
1717
"role": true,
1818
"level": true,
1919
}
20+
var maxSelinuxLabelSize = int(selinux.CategoryRange * (selinux.CategoryRange - 1) / 2)
2021

2122
var ErrIncompatibleLabel = errors.New("bad SELinux option: z and Z can not be used together")
2223

@@ -30,6 +31,9 @@ func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
3031
if !selinux.GetEnabled() {
3132
return "", "", nil
3233
}
34+
if selinux.ContainerLabelsSize() == maxSelinuxLabelSize {
35+
return "", "", fmt.Errorf("SELinux label exhaustion: %d labels used", selinux.ContainerLabelsSize())
36+
}
3337
processLabel, mountLabel := selinux.ContainerLabels()
3438
if processLabel != "" {
3539
defer func() {

go-selinux/label/label_linux_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package label
33
import (
44
"errors"
55
"os"
6+
"strings"
67
"testing"
78

89
"github.com/opencontainers/selinux/go-selinux"
@@ -53,6 +54,22 @@ 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 if !strings.Contains(err.Error(), "SELinux label exhaustion") {
67+
t.Fatalf("unexpected error %s", err.Error())
68+
}
69+
}
70+
}
71+
}
72+
5673
func TestRelabel(t *testing.T) {
5774
needSELinux(t)
5875

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 ContainerLabelsSize() 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)