Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: Share selinux (process and mount) label of first stage with other stages in same build #3693

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions buildah.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ type BuilderOptions struct {
// OciDecryptConfig contains the config that can be used to decrypt an image if it is
// encrypted if non-nil. If nil, it does not attempt to decrypt an image.
OciDecryptConfig *encconfig.DecryptConfig
// ProcessLabel is the SELinux process label associated with the container
ProcessLabel string
// MountLabel is the SELinux mount label associated with the container
MountLabel string
}

// ImportOptions are used to initialize a Builder from an existing container
Expand Down
2 changes: 2 additions & 0 deletions imagebuildah/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type Executor struct {
sshsources map[string]*sshagent.Source
logPrefix string
unsetEnvs []string
processLabel string // Shares processLabel of first stage container with containers of other stages in same build
mountLabel string // Shares mountLabel of first stage container with containers of other stages in same build
}

type imageTypeAndHistoryAndDiffIDs struct {
Expand Down
12 changes: 12 additions & 0 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,25 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
PullRetryDelay: s.executor.retryPullPushDelay,
OciDecryptConfig: s.executor.ociDecryptConfig,
Logger: s.executor.logger,
ProcessLabel: s.executor.processLabel,
MountLabel: s.executor.mountLabel,
}

builder, err = buildah.NewBuilder(ctx, s.executor.store, builderOptions)
if err != nil {
return nil, errors.Wrapf(err, "error creating build container")
}

// If executor's ProcessLabel and MountLabel is empty means this is the first stage
// Make sure we share first stage's ProcessLabel and MountLabel with all other subsequent stages
// Doing this will ensure and one stage in same build can mount another stage even if `selinux`
// is enabled.

if s.executor.mountLabel == "" && s.executor.processLabel == "" {
s.executor.mountLabel = builder.MountLabel
s.executor.processLabel = builder.ProcessLabel
}

if initializeIBConfig {
volumes := map[string]struct{}{}
for _, v := range builder.Volumes() {
Expand Down
7 changes: 2 additions & 5 deletions internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/containers/storage"
"github.com/containers/storage/pkg/idtools"
specs "github.com/opencontainers/runtime-spec/specs-go"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -113,8 +112,7 @@ func GetBindMount(ctx *types.SystemContext, args []string, contextDir string, st
isImageMounted := false
if fromImage != "" {
mountPoint := ""
//TODO: remove this selinux check when comment is resolved. https://github.com/containers/buildah/pull/3590#issuecomment-956349109
if additionalMountPoints != nil && (selinux.EnforceMode() != 1) {
if additionalMountPoints != nil {
if val, ok := additionalMountPoints[fromImage]; ok {
mountPoint = val.MountPoint
}
Expand Down Expand Up @@ -280,8 +278,7 @@ func GetCacheMount(args []string, store storage.Store, imageMountLabel string, a
// do not create cache on host
// instead use read-only mounted stage as cache
mountPoint := ""
//TODO: remove this selinux check when comment is resolved. https://github.com/containers/buildah/pull/3590#issuecomment-956349109
if additionalMountPoints != nil && (selinux.EnforceMode() != 1) {
if additionalMountPoints != nil {
if val, ok := additionalMountPoints[fromStage]; ok {
if val.IsStage {
mountPoint = val.MountPoint
Expand Down
11 changes: 11 additions & 0 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,20 @@ func newBuilder(ctx context.Context, store storage.Store, options BuilderOptions

conflict := 100
for {

var flags map[string]interface{}
// check if we have predefined ProcessLabel and MountLabel
// this could be true if this is another stage in a build
if options.ProcessLabel != "" && options.MountLabel != "" {
flags = map[string]interface{}{
"ProcessLabel": options.ProcessLabel,
"MountLabel": options.MountLabel,
}
}
coptions := storage.ContainerOptions{
LabelOpts: options.CommonBuildOpts.LabelOpts,
IDMappingOptions: newContainerIDMappingOptions(options.IDMappingOptions),
Flags: flags,
Volatile: true,
}
container, err = store.CreateContainer("", []string{tmpName}, imageID, "", "", &coptions)
Expand Down
44 changes: 0 additions & 44 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3759,13 +3759,6 @@ _EOF
}

@test "bud-with-writeable-mount-bind-from-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi
skip_if_no_runtime
skip_if_in_container
mkdir ${TESTDIR}/bud
Expand Down Expand Up @@ -3807,13 +3800,6 @@ _EOF
}

@test "bud-with-mount-cache-from-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi
skip_if_no_runtime
skip_if_in_container
mkdir ${TESTDIR}/bud
Expand All @@ -3826,13 +3812,6 @@ _EOF

# following test must fail
@test "bud-with-mount-cache-image-from-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi
skip_if_no_runtime
skip_if_in_container
mkdir ${TESTDIR}/bud
Expand All @@ -3846,13 +3825,6 @@ _EOF
}

@test "bud-with-mount-cache-multiple-from-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi
skip_if_no_runtime
skip_if_in_container
mkdir ${TESTDIR}/bud
Expand All @@ -3879,14 +3851,6 @@ _EOF
}

@test "bud-with-mount-bind-from-multistage-relative-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi

mkdir ${TESTDIR}/bud
cp -R ${TESTSDIR}/bud/buildkit-mount-from ${TESTDIR}/bud/buildkit-mount-from
skip_if_no_runtime
Expand All @@ -3898,14 +3862,6 @@ _EOF
}

@test "bud-with-mount-bind-from-cache-multistage-relative-like-buildkit" {
if which selinuxenabled > /dev/null 2> /dev/null ; then
if selinuxenabled ; then
#TODO: Once pending commit from https://github.com/containers/buildah/pull/3590 is merged
#See comment: https://github.com/containers/buildah/pull/3590#issuecomment-956349109
skip "skip if selinux enabled, since stages have different selinux label"
fi
fi

skip_if_no_runtime
skip_if_in_container
mkdir ${TESTDIR}/bud
Expand Down