Skip to content

Commit 861be7b

Browse files
authored
Merge pull request #196 from rhatdan/fscontext
Add ability to set fscontext mounts points
2 parents 46964d1 + c78503f commit 861be7b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

go-selinux/label/label.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,37 @@ func ReleaseLabel(label string) error {
7878
// Deprecated: use selinux.DupSecOpt
7979
var DupSecOpt = selinux.DupSecOpt
8080

81+
// FormatMountLabel returns a string to be used by the mount command. Using
82+
// the SELinux `context` mount option. Changing labels of files on mount
83+
// points with this option can never be changed.
8184
// FormatMountLabel returns a string to be used by the mount command.
8285
// The format of this string will be used to alter the labeling of the mountpoint.
8386
// The string returned is suitable to be used as the options field of the mount command.
8487
// If you need to have additional mount point options, you can pass them in as
8588
// the first parameter. Second parameter is the label that you wish to apply
8689
// to all content in the mount point.
8790
func FormatMountLabel(src, mountLabel string) string {
91+
return FormatMountLabelByType(src, mountLabel, "context")
92+
}
93+
94+
// FormatMountLabelByType returns a string to be used by the mount command.
95+
// Allow caller to specify the mount options. For example using the SELinux
96+
// `fscontext` mount option would allow certain container processes to change
97+
// labels of files created on the mount points, where as `context` option does
98+
// not.
99+
// FormatMountLabelByType returns a string to be used by the mount command.
100+
// The format of this string will be used to alter the labeling of the mountpoint.
101+
// The string returned is suitable to be used as the options field of the mount command.
102+
// If you need to have additional mount point options, you can pass them in as
103+
// the first parameter. Second parameter is the label that you wish to apply
104+
// to all content in the mount point.
105+
func FormatMountLabelByType(src, mountLabel, contextType string) string {
88106
if mountLabel != "" {
89107
switch src {
90108
case "":
91-
src = fmt.Sprintf("context=%q", mountLabel)
109+
src = fmt.Sprintf("%s=%q", contextType, mountLabel)
92110
default:
93-
src = fmt.Sprintf("%s,context=%q", src, mountLabel)
111+
src = fmt.Sprintf("%s,%s=%q", src, contextType, mountLabel)
94112
}
95113
}
96114
return src

go-selinux/label/label_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,19 @@ func TestFormatMountLabel(t *testing.T) {
1717
if test := FormatMountLabel("src", ""); test != expected {
1818
t.Fatalf("Format failed. Expected %s, got %s", expected, test)
1919
}
20+
21+
expected = `fscontext="foobar"`
22+
if test := FormatMountLabelByType("", "foobar", "fscontext"); test != expected {
23+
t.Fatalf("Format failed. Expected %s, got %s", expected, test)
24+
}
25+
26+
expected = `src,fscontext="foobar"`
27+
if test := FormatMountLabelByType("src", "foobar", "fscontext"); test != expected {
28+
t.Fatalf("Format failed. Expected %s, got %s", expected, test)
29+
}
30+
31+
expected = `src`
32+
if test := FormatMountLabelByType("src", "", "rootcontext"); test != expected {
33+
t.Fatalf("Format failed. Expected %s, got %s", expected, test)
34+
}
2035
}

0 commit comments

Comments
 (0)