Browse code

bump opencontainers/selinux to v1.2

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/03/21 17:58:13
Showing 7 changed files
... ...
@@ -255,7 +255,10 @@ func (daemon *Daemon) generateSecurityOpt(hostConfig *containertypes.HostConfig)
255 255
 		if err != nil {
256 256
 			return nil, err
257 257
 		}
258
-		ipcLabel = label.DupSecOpt(c.ProcessLabel)
258
+		ipcLabel, err = label.DupSecOpt(c.ProcessLabel)
259
+		if err != nil {
260
+			return nil, err
261
+		}
259 262
 		if pidContainer == "" {
260 263
 			return toHostConfigSelinuxLabels(ipcLabel), err
261 264
 		}
... ...
@@ -266,7 +269,10 @@ func (daemon *Daemon) generateSecurityOpt(hostConfig *containertypes.HostConfig)
266 266
 			return nil, err
267 267
 		}
268 268
 
269
-		pidLabel = label.DupSecOpt(c.ProcessLabel)
269
+		pidLabel, err = label.DupSecOpt(c.ProcessLabel)
270
+		if err != nil {
271
+			return nil, err
272
+		}
270 273
 		if ipcContainer == "" {
271 274
 			return toHostConfigSelinuxLabels(pidLabel), err
272 275
 		}
... ...
@@ -161,4 +161,4 @@ github.com/morikuni/aec 39771216ff4c63d11f5e604076f9c45e8be1067b
161 161
 # metrics
162 162
 github.com/docker/go-metrics d466d4f6fd960e01820085bd7e1a24426ee7ef18
163 163
 
164
-github.com/opencontainers/selinux b6fa367ed7f534f9ba25391cc2d467085dbb445a
164
+github.com/opencontainers/selinux 9e2c5215628a2567782777efb2049f385484f918 # v1.2
... ...
@@ -5,3 +5,14 @@
5 5
 Common SELinux package used across the container ecosystem.
6 6
 
7 7
 Please see the [godoc](https://godoc.org/github.com/opencontainers/selinux) for more information.
8
+
9
+## Code of Conduct
10
+
11
+Participation in the OpenContainers community is governed by [OpenContainer's Code of Conduct][code-of-conduct].
12
+
13
+## Security
14
+
15
+If you find an issue, please follow the [security][security] protocol to report it.
16
+
17
+[security]: https://github.com/opencontainers/org/blob/master/security
18
+[code-of-conduct]: https://github.com/opencontainers/org/blob/master/CODE_OF_CONDUCT.md
... ...
@@ -9,7 +9,7 @@ func InitLabels(options []string) (string, string, error) {
9 9
 	return "", "", nil
10 10
 }
11 11
 
12
-func GetROMountLabel() string {
12
+func ROMountLabel() string {
13 13
 	return ""
14 14
 }
15 15
 
... ...
@@ -25,7 +25,27 @@ func SetProcessLabel(processLabel string) error {
25 25
 	return nil
26 26
 }
27 27
 
28
-func GetFileLabel(path string) (string, error) {
28
+func ProcessLabel() (string, error) {
29
+	return "", nil
30
+}
31
+
32
+func SetSocketLabel(processLabel string) error {
33
+	return nil
34
+}
35
+
36
+func SocketLabel() (string, error) {
37
+	return "", nil
38
+}
39
+
40
+func SetKeyLabel(processLabel string) error {
41
+	return nil
42
+}
43
+
44
+func KeyLabel() (string, error) {
45
+	return "", nil
46
+}
47
+
48
+func FileLabel(path string) (string, error) {
29 49
 	return "", nil
30 50
 }
31 51
 
... ...
@@ -41,13 +61,18 @@ func Relabel(path string, fileLabel string, shared bool) error {
41 41
 	return nil
42 42
 }
43 43
 
44
-func GetPidLabel(pid int) (string, error) {
44
+func PidLabel(pid int) (string, error) {
45 45
 	return "", nil
46 46
 }
47 47
 
48 48
 func Init() {
49 49
 }
50 50
 
51
+// ClearLabels clears all reserved labels
52
+func ClearLabels() {
53
+	return
54
+}
55
+
51 56
 func ReserveLabel(label string) error {
52 57
 	return nil
53 58
 }
... ...
@@ -58,8 +83,8 @@ func ReleaseLabel(label string) error {
58 58
 
59 59
 // DupSecOpt takes a process label and returns security options that
60 60
 // can be used to set duplicate labels on future container processes
61
-func DupSecOpt(src string) []string {
62
-	return nil
61
+func DupSecOpt(src string) ([]string, error) {
62
+	return nil, nil
63 63
 }
64 64
 
65 65
 // DisableSecOpt returns a security opt that can disable labeling
... ...
@@ -4,6 +4,8 @@ package label
4 4
 
5 5
 import (
6 6
 	"fmt"
7
+	"os"
8
+	"os/user"
7 9
 	"strings"
8 10
 
9 11
 	"github.com/opencontainers/selinux/go-selinux"
... ...
@@ -24,17 +26,29 @@ var ErrIncompatibleLabel = fmt.Errorf("Bad SELinux option z and Z can not be use
24 24
 // the container.  A list of options can be passed into this function to alter
25 25
 // the labels.  The labels returned will include a random MCS String, that is
26 26
 // guaranteed to be unique.
27
-func InitLabels(options []string) (string, string, error) {
27
+func InitLabels(options []string) (plabel string, mlabel string, Err error) {
28 28
 	if !selinux.GetEnabled() {
29 29
 		return "", "", nil
30 30
 	}
31 31
 	processLabel, mountLabel := selinux.ContainerLabels()
32 32
 	if processLabel != "" {
33
-		pcon := selinux.NewContext(processLabel)
34
-		mcon := selinux.NewContext(mountLabel)
33
+		defer func() {
34
+			if Err != nil {
35
+				ReleaseLabel(mountLabel)
36
+			}
37
+		}()
38
+		pcon, err := selinux.NewContext(processLabel)
39
+		if err != nil {
40
+			return "", "", err
41
+		}
42
+
43
+		mcon, err := selinux.NewContext(mountLabel)
44
+		if err != nil {
45
+			return "", "", err
46
+		}
35 47
 		for _, opt := range options {
36 48
 			if opt == "disable" {
37
-				return "", "", nil
49
+				return "", mountLabel, nil
38 50
 			}
39 51
 			if i := strings.Index(opt, ":"); i == -1 {
40 52
 				return "", "", fmt.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type' followed by ':' and a value", opt)
... ...
@@ -90,6 +104,28 @@ func SetProcessLabel(processLabel string) error {
90 90
 	return selinux.SetExecLabel(processLabel)
91 91
 }
92 92
 
93
+// SetSocketLabel takes a process label and tells the kernel to assign the
94
+// label to the next socket that gets created
95
+func SetSocketLabel(processLabel string) error {
96
+	return selinux.SetSocketLabel(processLabel)
97
+}
98
+
99
+// SocketLabel retrieves the current default socket label setting
100
+func SocketLabel() (string, error) {
101
+	return selinux.SocketLabel()
102
+}
103
+
104
+// SetKeyLabel takes a process label and tells the kernel to assign the
105
+// label to the next kernel keyring that gets created
106
+func SetKeyLabel(processLabel string) error {
107
+	return selinux.SetKeyLabel(processLabel)
108
+}
109
+
110
+// KeyLabel retrieves the current default kernel keyring label setting
111
+func KeyLabel() (string, error) {
112
+	return selinux.KeyLabel()
113
+}
114
+
93 115
 // ProcessLabel returns the process label that the kernel will assign
94 116
 // to the next program executed by the current process.  If "" is returned
95 117
 // this indicates that the default labeling will happen for the process.
... ...
@@ -97,7 +133,7 @@ func ProcessLabel() (string, error) {
97 97
 	return selinux.ExecLabel()
98 98
 }
99 99
 
100
-// GetFileLabel returns the label for specified path
100
+// FileLabel returns the label for specified path
101 101
 func FileLabel(path string) (string, error) {
102 102
 	return selinux.FileLabel(path)
103 103
 }
... ...
@@ -130,13 +166,56 @@ func Relabel(path string, fileLabel string, shared bool) error {
130 130
 		return nil
131 131
 	}
132 132
 
133
-	exclude_paths := map[string]bool{"/": true, "/usr": true, "/etc": true, "/tmp": true, "/home": true, "/run": true, "/var": true, "/root": true}
133
+	exclude_paths := map[string]bool{
134
+		"/":           true,
135
+		"/bin":        true,
136
+		"/boot":       true,
137
+		"/dev":        true,
138
+		"/etc":        true,
139
+		"/etc/passwd": true,
140
+		"/etc/pki":    true,
141
+		"/etc/shadow": true,
142
+		"/home":       true,
143
+		"/lib":        true,
144
+		"/lib64":      true,
145
+		"/media":      true,
146
+		"/opt":        true,
147
+		"/proc":       true,
148
+		"/root":       true,
149
+		"/run":        true,
150
+		"/sbin":       true,
151
+		"/srv":        true,
152
+		"/sys":        true,
153
+		"/tmp":        true,
154
+		"/usr":        true,
155
+		"/var":        true,
156
+		"/var/lib":    true,
157
+		"/var/log":    true,
158
+	}
159
+
160
+	if home := os.Getenv("HOME"); home != "" {
161
+		exclude_paths[home] = true
162
+	}
163
+
164
+	if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
165
+		if usr, err := user.Lookup(sudoUser); err == nil {
166
+			exclude_paths[usr.HomeDir] = true
167
+		}
168
+	}
169
+
170
+	if path != "/" {
171
+		path = strings.TrimSuffix(path, "/")
172
+	}
134 173
 	if exclude_paths[path] {
135 174
 		return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
136 175
 	}
137 176
 
138 177
 	if shared {
139
-		c := selinux.NewContext(fileLabel)
178
+		c, err := selinux.NewContext(fileLabel)
179
+		if err != nil {
180
+			return err
181
+		}
182
+
140 183
 		c["level"] = "s0"
141 184
 		fileLabel = c.Get()
142 185
 	}
... ...
@@ -156,6 +235,11 @@ func Init() {
156 156
 	selinux.GetEnabled()
157 157
 }
158 158
 
159
+// ClearLabels will clear all reserved labels
160
+func ClearLabels() {
161
+	selinux.ClearLabels()
162
+}
163
+
159 164
 // ReserveLabel will record the fact that the MCS label has already been used.
160 165
 // This will prevent InitLabels from using the MCS label in a newly created
161 166
 // container
... ...
@@ -174,7 +258,7 @@ func ReleaseLabel(label string) error {
174 174
 
175 175
 // DupSecOpt takes a process label and returns security options that
176 176
 // can be used to set duplicate labels on future container processes
177
-func DupSecOpt(src string) []string {
177
+func DupSecOpt(src string) ([]string, error) {
178 178
 	return selinux.DupSecOpt(src)
179 179
 }
180 180
 
... ...
@@ -52,6 +52,8 @@ var (
52 52
 	ErrMCSAlreadyExists = errors.New("MCS label already exists")
53 53
 	// ErrEmptyPath is returned when an empty path has been specified.
54 54
 	ErrEmptyPath = errors.New("empty path")
55
+	// InvalidLabel is returned when an invalid label is specified.
56
+	InvalidLabel = errors.New("Invalid Label")
55 57
 
56 58
 	assignRegex = regexp.MustCompile(`^([^=]+)=(.*)$`)
57 59
 	roFileLabel string
... ...
@@ -385,6 +387,28 @@ func SetExecLabel(label string) error {
385 385
 	return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()), label)
386 386
 }
387 387
 
388
+// SetSocketLabel takes a process label and tells the kernel to assign the
389
+// label to the next socket that gets created
390
+func SetSocketLabel(label string) error {
391
+	return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/sockcreate", syscall.Gettid()), label)
392
+}
393
+
394
+// SocketLabel retrieves the current socket label setting
395
+func SocketLabel() (string, error) {
396
+	return readCon(fmt.Sprintf("/proc/self/task/%d/attr/sockcreate", syscall.Gettid()))
397
+}
398
+
399
+// SetKeyLabel takes a process label and tells the kernel to assign the
400
+// label to the next kernel keyring that gets created
401
+func SetKeyLabel(label string) error {
402
+	return writeCon("/proc/self/attr/keycreate", label)
403
+}
404
+
405
+// KeyLabel retrieves the current kernel keyring label setting
406
+func KeyLabel() (string, error) {
407
+	return readCon("/proc/self/attr/keycreate")
408
+}
409
+
388 410
 // Get returns the Context as a string
389 411
 func (c Context) Get() string {
390 412
 	if c["level"] != "" {
... ...
@@ -394,11 +418,14 @@ func (c Context) Get() string {
394 394
 }
395 395
 
396 396
 // NewContext creates a new Context struct from the specified label
397
-func NewContext(label string) Context {
397
+func NewContext(label string) (Context, error) {
398 398
 	c := make(Context)
399 399
 
400 400
 	if len(label) != 0 {
401 401
 		con := strings.SplitN(label, ":", 4)
402
+		if len(con) < 3 {
403
+			return c, InvalidLabel
404
+		}
402 405
 		c["user"] = con[0]
403 406
 		c["role"] = con[1]
404 407
 		c["type"] = con[2]
... ...
@@ -406,7 +433,14 @@ func NewContext(label string) Context {
406 406
 			c["level"] = con[3]
407 407
 		}
408 408
 	}
409
-	return c
409
+	return c, nil
410
+}
411
+
412
+// ClearLabels clears all reserved labels
413
+func ClearLabels() {
414
+	state.Lock()
415
+	state.mcsList = make(map[string]bool)
416
+	state.Unlock()
410 417
 }
411 418
 
412 419
 // ReserveLabel reserves the MLS/MCS level component of the specified label
... ...
@@ -612,12 +646,12 @@ func ContainerLabels() (processLabel string, fileLabel string) {
612 612
 		roFileLabel = fileLabel
613 613
 	}
614 614
 exit:
615
-	scon := NewContext(processLabel)
615
+	scon, _ := NewContext(processLabel)
616 616
 	if scon["level"] != "" {
617 617
 		mcs := uniqMcs(1024)
618 618
 		scon["level"] = mcs
619 619
 		processLabel = scon.Get()
620
-		scon = NewContext(fileLabel)
620
+		scon, _ = NewContext(fileLabel)
621 621
 		scon["level"] = mcs
622 622
 		fileLabel = scon.Get()
623 623
 	}
... ...
@@ -643,8 +677,14 @@ func CopyLevel(src, dest string) (string, error) {
643 643
 	if err := SecurityCheckContext(dest); err != nil {
644 644
 		return "", err
645 645
 	}
646
-	scon := NewContext(src)
647
-	tcon := NewContext(dest)
646
+	scon, err := NewContext(src)
647
+	if err != nil {
648
+		return "", err
649
+	}
650
+	tcon, err := NewContext(dest)
651
+	if err != nil {
652
+		return "", err
653
+	}
648 654
 	mcsDelete(tcon["level"])
649 655
 	mcsAdd(scon["level"])
650 656
 	tcon["level"] = scon["level"]
... ...
@@ -680,7 +720,11 @@ func Chcon(fpath string, label string, recurse bool) error {
680 680
 		return err
681 681
 	}
682 682
 	callback := func(p string, info os.FileInfo, err error) error {
683
-		return SetFileLabel(p, label)
683
+		e := SetFileLabel(p, label)
684
+		if os.IsNotExist(e) {
685
+			return nil
686
+		}
687
+		return e
684 688
 	}
685 689
 
686 690
 	if recurse {
... ...
@@ -692,15 +736,18 @@ func Chcon(fpath string, label string, recurse bool) error {
692 692
 
693 693
 // DupSecOpt takes an SELinux process label and returns security options that
694 694
 // can be used to set the SELinux Type and Level for future container processes.
695
-func DupSecOpt(src string) []string {
695
+func DupSecOpt(src string) ([]string, error) {
696 696
 	if src == "" {
697
-		return nil
697
+		return nil, nil
698
+	}
699
+	con, err := NewContext(src)
700
+	if err != nil {
701
+		return nil, err
698 702
 	}
699
-	con := NewContext(src)
700 703
 	if con["user"] == "" ||
701 704
 		con["role"] == "" ||
702 705
 		con["type"] == "" {
703
-		return nil
706
+		return nil, nil
704 707
 	}
705 708
 	dup := []string{"user:" + con["user"],
706 709
 		"role:" + con["role"],
... ...
@@ -711,7 +758,7 @@ func DupSecOpt(src string) []string {
711 711
 		dup = append(dup, "level:"+con["level"])
712 712
 	}
713 713
 
714
-	return dup
714
+	return dup, nil
715 715
 }
716 716
 
717 717
 // DisableSecOpt returns a security opt that can be used to disable SELinux
... ...
@@ -96,15 +96,44 @@ func SetExecLabel(label string) error {
96 96
 	return nil
97 97
 }
98 98
 
99
+/*
100
+SetSocketLabel sets the SELinux label that the kernel will use for any programs
101
+that are executed by the current process thread, or an error.
102
+*/
103
+func SetSocketLabel(label string) error {
104
+	return nil
105
+}
106
+
107
+// SocketLabel retrieves the current socket label setting
108
+func SocketLabel() (string, error) {
109
+	return "", nil
110
+}
111
+
112
+// SetKeyLabel takes a process label and tells the kernel to assign the
113
+// label to the next kernel keyring that gets created
114
+func SetKeyLabel(label string) error {
115
+	return nil
116
+}
117
+
118
+// KeyLabel retrieves the current kernel keyring label setting
119
+func KeyLabel() (string, error) {
120
+	return "", nil
121
+}
122
+
99 123
 // Get returns the Context as a string
100 124
 func (c Context) Get() string {
101 125
 	return ""
102 126
 }
103 127
 
104 128
 // NewContext creates a new Context struct from the specified label
105
-func NewContext(label string) Context {
129
+func NewContext(label string) (Context, error) {
106 130
 	c := make(Context)
107
-	return c
131
+	return c, nil
132
+}
133
+
134
+// ClearLabels clears all reserved MLS/MCS levels
135
+func ClearLabels() {
136
+	return
108 137
 }
109 138
 
110 139
 // ReserveLabel reserves the MLS/MCS level component of the specified label
... ...
@@ -177,8 +206,8 @@ func Chcon(fpath string, label string, recurse bool) error {
177 177
 
178 178
 // DupSecOpt takes an SELinux process label and returns security options that
179 179
 // can be used to set the SELinux Type and Level for future container processes.
180
-func DupSecOpt(src string) []string {
181
-	return nil
180
+func DupSecOpt(src string) ([]string, error) {
181
+	return nil, nil
182 182
 }
183 183
 
184 184
 // DisableSecOpt returns a security opt that can be used to disable SELinux