@@ -2,22 +2,11 @@ package label
22
33import (
44 "errors"
5- "fmt"
65 "strings"
76
87 "github.com/opencontainers/selinux/go-selinux"
98)
109
11- // Valid Label Options
12- var validOptions = map [string ]bool {
13- "disable" : true ,
14- "type" : true ,
15- "filetype" : true ,
16- "user" : true ,
17- "role" : true ,
18- "level" : true ,
19- }
20-
2110var ErrIncompatibleLabel = errors .New ("bad SELinux option: z and Z can not be used together" )
2211
2312// InitLabels returns the process label and file labels to be used within
@@ -27,55 +16,7 @@ var ErrIncompatibleLabel = errors.New("bad SELinux option: z and Z can not be us
2716// If the disabled flag is passed in, the process label will not be set, but the mount label will be set
2817// to the container_file label with the maximum category. This label is not usable by any confined label.
2918func InitLabels (options []string ) (plabel string , mlabel string , retErr error ) {
30- if ! selinux .GetEnabled () {
31- return "" , "" , nil
32- }
33- processLabel , mountLabel := selinux .ContainerLabels ()
34- if processLabel == "" {
35- // processLabel is required; if empty, do nothing.
36- return processLabel , mountLabel , nil
37- }
38- defer func () {
39- if retErr != nil {
40- selinux .ReleaseLabel (mountLabel )
41- }
42- }()
43- pcon , err := selinux .NewContext (processLabel )
44- if err != nil {
45- return "" , "" , err
46- }
47- mcsLevel := pcon ["level" ]
48- mcon , err := selinux .NewContext (mountLabel )
49- if err != nil {
50- return "" , "" , err
51- }
52- for _ , opt := range options {
53- if opt == "disable" {
54- selinux .ReleaseLabel (mountLabel )
55- return "" , selinux .PrivContainerMountLabel (), nil
56- }
57- k , v , ok := strings .Cut (opt , ":" )
58- if ! ok || ! validOptions [k ] {
59- return "" , "" , fmt .Errorf ("bad label option %q, valid options 'disable' or \n 'user, role, level, type, filetype' followed by ':' and a value" , opt )
60- }
61- if k == "filetype" {
62- mcon ["type" ] = v
63- continue
64- }
65- pcon [k ] = v
66- if k == "level" || k == "user" {
67- mcon [k ] = v
68- }
69- }
70- if p := pcon .Get (); p != processLabel {
71- if pcon ["level" ] != mcsLevel {
72- selinux .ReleaseLabel (processLabel )
73- }
74- selinux .ReserveLabel (p )
75- processLabel = p
76- }
77- mountLabel = mcon .Get ()
78- return processLabel , mountLabel , nil
19+ return selinux .InitLabels (options )
7920}
8021
8122// SetFileLabel modifies the "path" label to the specified file label
0 commit comments