2022-03-31 18:38:57 +08:00
|
|
|
//go:build linux
|
2018-09-05 22:44:28 +08:00
|
|
|
|
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2022-07-27 03:27:30 +08:00
|
|
|
"errors"
|
2021-11-16 23:56:04 +08:00
|
|
|
"fmt"
|
2022-03-31 18:38:57 +08:00
|
|
|
"os"
|
2021-11-16 23:56:04 +08:00
|
|
|
|
2018-09-05 22:44:28 +08:00
|
|
|
"github.com/opencontainers/runtime-tools/generate"
|
2019-04-24 03:09:01 +08:00
|
|
|
selinux "github.com/opencontainers/selinux/go-selinux"
|
2018-09-05 22:44:28 +08:00
|
|
|
)
|
|
|
|
|
2019-07-25 22:10:03 +08:00
|
|
|
func selinuxGetEnabled() bool {
|
|
|
|
return selinux.GetEnabled()
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:44:28 +08:00
|
|
|
func setupSelinux(g *generate.Generator, processLabel, mountLabel string) {
|
2019-04-24 03:09:01 +08:00
|
|
|
if processLabel != "" && selinux.GetEnabled() {
|
|
|
|
g.SetProcessSelinuxLabel(processLabel)
|
|
|
|
g.SetLinuxMountLabel(mountLabel)
|
|
|
|
}
|
2018-09-05 22:44:28 +08:00
|
|
|
}
|
2021-11-16 23:56:04 +08:00
|
|
|
|
|
|
|
func runLabelStdioPipes(stdioPipe [][]int, processLabel, mountLabel string) error {
|
|
|
|
if !selinuxGetEnabled() || processLabel == "" || mountLabel == "" {
|
|
|
|
// SELinux is completely disabled, or we're not doing anything at all with labeling
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pipeContext, err := selinux.ComputeCreateContext(processLabel, mountLabel, "fifo_file")
|
|
|
|
if err != nil {
|
2022-07-06 17:14:06 +08:00
|
|
|
return fmt.Errorf("computing file creation context for pipes: %w", err)
|
2021-11-16 23:56:04 +08:00
|
|
|
}
|
|
|
|
for i := range stdioPipe {
|
|
|
|
pipeFdName := fmt.Sprintf("/proc/self/fd/%d", stdioPipe[i][0])
|
2022-07-27 03:27:30 +08:00
|
|
|
if err := selinux.SetFileLabel(pipeFdName, pipeContext); err != nil && !errors.Is(err, os.ErrNotExist) {
|
2022-07-06 17:14:06 +08:00
|
|
|
return fmt.Errorf("setting file label on %q: %w", pipeFdName, err)
|
2021-11-16 23:56:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|