Skip to content

Commit 45683dc

Browse files
committed
Reserver one Category for the privileged containers to use
Currently each privileged container is reserving a different category to make sure that other containers can not see their content. This wastes container categories. This PR will reserve one category for all privileged containers (s0:c1023,c1024), and free all of the other categories for confined containers. Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 8deba79 commit 45683dc

6 files changed

Lines changed: 33 additions & 13 deletions

File tree

go-selinux/label/label_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var ErrIncompatibleLabel = errors.New("Bad SELinux option z and Z can not be use
2525
// the container. A list of options can be passed into this function to alter
2626
// the labels. The labels returned will include a random MCS String, that is
2727
// guaranteed to be unique.
28+
// If the disabled flag is passed in, the process label will not be set, but the mount label will be set
29+
// to the container_file label with the maximum category. This label is not usable by any confined label.
2830
func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
2931
if !selinux.GetEnabled() {
3032
return "", "", nil
@@ -47,7 +49,8 @@ func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
4749
}
4850
for _, opt := range options {
4951
if opt == "disable" {
50-
return "", mountLabel, nil
52+
selinux.ReleaseLabel(mountLabel)
53+
return "", selinux.PrivContainerMountLabel(), nil
5154
}
5255
if i := strings.Index(opt, ":"); i == -1 {
5356
return "", "", errors.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type, filetype' followed by ':' and a value", opt)

go-selinux/label/label_linux_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ func TestInit(t *testing.T) {
2929
if roMountLabel == "" {
3030
t.Fatal("ROMountLabel: empty")
3131
}
32-
plabel, _, err := InitLabels(testDisabled)
32+
plabel, mlabel, err := InitLabels(testDisabled)
3333
if err != nil {
3434
t.Fatalf("InitLabels(disabled) failed: %v", err)
3535
}
3636
if plabel != "" {
3737
t.Fatalf("InitLabels(disabled): %q not empty", plabel)
3838
}
39+
if mlabel != "system_u:object_r:container_file_t:s0:c1022,c1023" {
40+
t.Fatalf("InitLabels Disabled mlabel Failed, %s", mlabel)
41+
}
42+
3943
testUser := []string{"user:user_u", "role:user_r", "type:user_t", "level:s0:c1,c15"}
40-
plabel, mlabel, err := InitLabels(testUser)
44+
plabel, mlabel, err = InitLabels(testUser)
4145
if err != nil {
4246
t.Fatalf("InitLabels(user) failed: %v", err)
4347
}
@@ -172,7 +176,7 @@ func TestSELinuxNoLevel(t *testing.T) {
172176
t.Fatal(err)
173177
}
174178
if con.Get() != tlabel {
175-
t.Errorf("NewContaxt and con.Get() failed on non mls label: expexcted %q, got %q", tlabel, con.Get())
179+
t.Errorf("NewContaxt and con.Get() failed on non mls label: expected %q, got %q", tlabel, con.Get())
176180
}
177181
}
178182

go-selinux/label/label_stub_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ func TestInit(t *testing.T) {
2222
if roMountLabel != "" {
2323
t.Errorf("ROMountLabel Failed")
2424
}
25-
plabel, _, err := InitLabels(testDisabled)
25+
plabel, mlabel, err := InitLabels(testDisabled)
2626
if err != nil {
2727
t.Log("InitLabels Disabled Failed")
2828
t.Fatal(err)
2929
}
3030
if plabel != "" {
31-
t.Log("InitLabels Disabled Failed")
32-
t.FailNow()
31+
t.Fatal("InitLabels Disabled Failed")
32+
}
33+
if mlabel != "" {
34+
t.Fatal("InitLabels Disabled mlabel Failed")
3335
}
3436
testUser := []string{"user:user_u", "role:user_r", "type:user_t", "level:s0:c1,c15"}
3537
_, _, err = InitLabels(testUser)

go-selinux/selinux.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const (
1111
Permissive = 0
1212
// Disabled constant to indicate SELinux is disabled
1313
Disabled = -1
14-
14+
// maxCategory is the maximum number of categories used within containers
15+
maxCategory = 1024
1516
// DefaultCategoryRange is the upper bound on the category range
16-
DefaultCategoryRange = uint32(1024)
17+
DefaultCategoryRange = uint32(maxCategory)
1718
)
1819

1920
var (
@@ -276,3 +277,8 @@ func DisableSecOpt() []string {
276277
func GetDefaultContextWithLevel(user, level, scon string) (string, error) {
277278
return getDefaultContextWithLevel(user, level, scon)
278279
}
280+
281+
// PrivContainerMountLabel returns mount label for privileged containers
282+
func PrivContainerMountLabel() string {
283+
return privContainerMountLabel
284+
}

go-selinux/selinux_linux.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,13 +892,13 @@ func openContextFile() (*os.File, error) {
892892
return os.Open(lxcPath)
893893
}
894894

895-
var labels = loadLabels()
895+
var labels, privContainerMountLabel = loadLabels()
896896

897-
func loadLabels() map[string]string {
897+
func loadLabels() (map[string]string, string) {
898898
labels := make(map[string]string)
899899
in, err := openContextFile()
900900
if err != nil {
901-
return labels
901+
return labels, ""
902902
}
903903
defer in.Close()
904904

@@ -920,7 +920,10 @@ func loadLabels() map[string]string {
920920
}
921921
}
922922

923-
return labels
923+
con, _ := NewContext(labels["file"])
924+
con["level"] = fmt.Sprintf("s0:c%d,c%d", maxCategory-2, maxCategory-1)
925+
reserveLabel(con.get())
926+
return labels, con.get()
924927
}
925928

926929
// kvmContainerLabels returns the default processLabel and mountLabel to be used

go-selinux/selinux_stub.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package selinux
44

5+
const privContainerMountLabel = ""
6+
57
func setDisabled() {
68
}
79

0 commit comments

Comments
 (0)