Skip to content

Commit db3eeba

Browse files
committed
selinux.Chcon should check legal rather then just label.Relabel
Since label.Relabel ends up calling into selinux.chcon, we should do the check for invalid directories under chcon. This will allow the selinux.Chcon function to also be verified. Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 00d547f commit db3eeba

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

go-selinux/label/label_linux.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package label
33
import (
44
"errors"
55
"fmt"
6-
"os"
7-
"os/user"
86
"strings"
97

108
"github.com/opencontainers/selinux/go-selinux"
@@ -113,50 +111,6 @@ func Relabel(path string, fileLabel string, shared bool) error {
113111
return nil
114112
}
115113

116-
exclude_paths := map[string]bool{
117-
"/": true,
118-
"/bin": true,
119-
"/boot": true,
120-
"/dev": true,
121-
"/etc": true,
122-
"/etc/passwd": true,
123-
"/etc/pki": true,
124-
"/etc/shadow": true,
125-
"/home": true,
126-
"/lib": true,
127-
"/lib64": true,
128-
"/media": true,
129-
"/opt": true,
130-
"/proc": true,
131-
"/root": true,
132-
"/run": true,
133-
"/sbin": true,
134-
"/srv": true,
135-
"/sys": true,
136-
"/tmp": true,
137-
"/usr": true,
138-
"/var": true,
139-
"/var/lib": true,
140-
"/var/log": true,
141-
}
142-
143-
if home := os.Getenv("HOME"); home != "" {
144-
exclude_paths[home] = true
145-
}
146-
147-
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
148-
if usr, err := user.Lookup(sudoUser); err == nil {
149-
exclude_paths[usr.HomeDir] = true
150-
}
151-
}
152-
153-
if path != "/" {
154-
path = strings.TrimSuffix(path, "/")
155-
}
156-
if exclude_paths[path] {
157-
return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
158-
}
159-
160114
if shared {
161115
c, err := selinux.NewContext(fileLabel)
162116
if err != nil {

go-selinux/selinux_linux.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io/ioutil"
1212
"math/big"
1313
"os"
14+
"os/user"
1415
"path"
1516
"path/filepath"
1617
"strconv"
@@ -1083,6 +1084,50 @@ func chcon(fpath string, label string, recurse bool) error {
10831084
return nil
10841085
}
10851086

1087+
exclude_paths := map[string]bool{
1088+
"/": true,
1089+
"/bin": true,
1090+
"/boot": true,
1091+
"/dev": true,
1092+
"/etc": true,
1093+
"/etc/passwd": true,
1094+
"/etc/pki": true,
1095+
"/etc/shadow": true,
1096+
"/home": true,
1097+
"/lib": true,
1098+
"/lib64": true,
1099+
"/media": true,
1100+
"/opt": true,
1101+
"/proc": true,
1102+
"/root": true,
1103+
"/run": true,
1104+
"/sbin": true,
1105+
"/srv": true,
1106+
"/sys": true,
1107+
"/tmp": true,
1108+
"/usr": true,
1109+
"/var": true,
1110+
"/var/lib": true,
1111+
"/var/log": true,
1112+
}
1113+
1114+
if home := os.Getenv("HOME"); home != "" {
1115+
exclude_paths[home] = true
1116+
}
1117+
1118+
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
1119+
if usr, err := user.Lookup(sudoUser); err == nil {
1120+
exclude_paths[usr.HomeDir] = true
1121+
}
1122+
}
1123+
1124+
if fpath != "/" {
1125+
fpath = strings.TrimSuffix(fpath, "/")
1126+
}
1127+
if exclude_paths[fpath] {
1128+
return fmt.Errorf("SELinux relabeling of %s is not allowed", fpath)
1129+
}
1130+
10861131
if !recurse {
10871132
return setFileLabel(fpath, label)
10881133
}

0 commit comments

Comments
 (0)