Skip to content

Commit 9b6a02f

Browse files
committed
run: ensure that stdio pipes are labeled correctly
Label stdio pipes to ensure that processes we run can read through /dev/stdin and write through the /dev/stdout and /dev/stderr links. Signed-off-by: Nalin Dahyabhai <[email protected]>
1 parent e6f7320 commit 9b6a02f

File tree

12 files changed

+143
-9
lines changed

12 files changed

+143
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/opencontainers/runc v1.0.2
2727
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
2828
github.com/opencontainers/runtime-tools v0.9.0
29-
github.com/opencontainers/selinux v1.9.1
29+
github.com/opencontainers/selinux v1.10.0
3030
github.com/openshift/imagebuilder v1.2.2-0.20210415181909-87f3e48c2656
3131
github.com/pkg/errors v0.9.1
3232
github.com/seccomp/libseccomp-golang v0.9.2-0.20200616122406-847368b35ebf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3
672672
github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8=
673673
github.com/opencontainers/selinux v1.8.4/go.mod h1:HTvjPFoGMbpQsG886e3lQwnsRWtE4TC1OF3OUvG9FAo=
674674
github.com/opencontainers/selinux v1.8.5/go.mod h1:HTvjPFoGMbpQsG886e3lQwnsRWtE4TC1OF3OUvG9FAo=
675-
github.com/opencontainers/selinux v1.9.1 h1:b4VPEF3O5JLZgdTDBmGepaaIbAo0GqoF6EBRq5f/g3Y=
676-
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
675+
github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU=
676+
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
677677
github.com/openshift/imagebuilder v1.2.2-0.20210415181909-87f3e48c2656 h1:WaxyNFpmIDu4i6so9r6LVFIbSaXqsj8oitMitt86ae4=
678678
github.com/openshift/imagebuilder v1.2.2-0.20210415181909-87f3e48c2656/go.mod h1:9aJRczxCH0mvT6XQ+5STAQaPWz7OsWcU5/mRkt8IWeo=
679679
github.com/ostreedev/ostree-go v0.0.0-20190702140239-759a8c1ac913 h1:TnbXhKzrTOyuvWrjI8W6pcoI9XPbLHFXCdN2dtUw7Rw=

run_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,9 @@ func runUsingRuntime(isolation define.Isolation, options RunOptions, configureNe
863863
if stdioPipe, err = runMakeStdioPipe(int(uid), int(gid)); err != nil {
864864
return 1, err
865865
}
866+
if err = runLabelStdioPipes(stdioPipe, spec.Process.SelinuxLabel, spec.Linux.MountLabel); err != nil {
867+
return 1, err
868+
}
866869
errorFds = []int{stdioPipe[unix.Stdout][0], stdioPipe[unix.Stderr][0]}
867870
closeBeforeReadingErrorFds = []int{stdioPipe[unix.Stdout][1], stdioPipe[unix.Stderr][1]}
868871
// Set stdio to our pipes.

selinux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
package buildah
44

55
import (
6+
"fmt"
7+
68
"github.com/opencontainers/runtime-tools/generate"
79
selinux "github.com/opencontainers/selinux/go-selinux"
10+
"github.com/opencontainers/selinux/go-selinux/label"
11+
"github.com/pkg/errors"
812
)
913

1014
func selinuxGetEnabled() bool {
@@ -17,3 +21,21 @@ func setupSelinux(g *generate.Generator, processLabel, mountLabel string) {
1721
g.SetLinuxMountLabel(mountLabel)
1822
}
1923
}
24+
25+
func runLabelStdioPipes(stdioPipe [][]int, processLabel, mountLabel string) error {
26+
if !selinuxGetEnabled() || processLabel == "" || mountLabel == "" {
27+
// SELinux is completely disabled, or we're not doing anything at all with labeling
28+
return nil
29+
}
30+
pipeContext, err := selinux.ComputeCreateContext(processLabel, mountLabel, "fifo_file")
31+
if err != nil {
32+
return errors.Wrapf(err, "computing file creation context for pipes")
33+
}
34+
for i := range stdioPipe {
35+
pipeFdName := fmt.Sprintf("/proc/self/fd/%d", stdioPipe[i][0])
36+
if err := label.Relabel(pipeFdName, pipeContext, false); err != nil {
37+
return errors.Wrapf(err, "setting file label on %q", pipeFdName)
38+
}
39+
}
40+
return nil
41+
}

selinux_unsupported.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ func selinuxGetEnabled() bool {
1212

1313
func setupSelinux(g *generate.Generator, processLabel, mountLabel string) {
1414
}
15+
16+
func runLabelStdioPipes(stdioPipe [][]int, processLabel, mountLabel string) error {
17+
return nil
18+
}

tests/bud.bats

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ load helpers
77
expect_output --substring "non-directory/Dockerfile: not a directory"
88
}
99

10+
@test "bud stdio is usable pipes" {
11+
run_buildah build ${TESTSDIR}/bud/stdio
12+
}
13+
1014
@test "bud with --dns* flags" {
1115
_prefetch alpine
1216
run_buildah build --dns-search=example.com --dns=223.5.5.5 --dns-option=use-vc --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/dns/Dockerfile ${TESTSDIR}/bud/dns

tests/bud/stdio/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM alpine
2+
# Will stall if this is connected to a terminal, or fail if it's not readable
3+
RUN cat /dev/stdin
4+
# Will fail if it's not writable
5+
RUN echo foo > /dev/stdout
6+
# Will fail if it's not writable
7+
RUN echo foo > /dev/stderr

vendor/github.com/opencontainers/selinux/go-selinux/selinux.go

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/selinux/go-selinux/selinux_linux.go

Lines changed: 42 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/selinux/go-selinux/selinux_stub.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)