Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Drop effective and permitted cap for non-root user.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <[email protected]>
  • Loading branch information
Random-Liu committed Feb 14, 2020
1 parent cf0e0a1 commit bdbc2ab
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
20 changes: 20 additions & 0 deletions integration/images/private-workdir/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM busybox

RUN mkdir /workdir
RUN chmod 700 /workdir
RUN chown nobody /workdir
WORKDIR /workdir
27 changes: 27 additions & 0 deletions integration/images/private-workdir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

all: build

PROJ=gcr.io/k8s-cri-containerd
VERSION=1.0
IMAGE=$(PROJ)/private-workdir:$(VERSION)

build:
docker build -t $(IMAGE) .

push:
gcloud docker -- push $(IMAGE)

.PHONY: build push
13 changes: 13 additions & 0 deletions integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ func WithSupplementalGroups(gids []int64) ContainerOpts {
}
}

// WithRunAsUser adds RunAsUser uid.
func WithRunAsUser(uid int64) ContainerOpts {
return func(c *runtime.ContainerConfig) {
if c.Linux == nil {
c.Linux = &runtime.LinuxContainerConfig{}
}
if c.Linux.SecurityContext == nil {
c.Linux.SecurityContext = &runtime.LinuxContainerSecurityContext{}
}
c.Linux.SecurityContext.RunAsUser = &runtime.Int64Value{Value: uid}
}
}

// ContainerConfig creates a container config given a name and image name
// and additional container config options
func ContainerConfig(name, image string, opts ...ContainerOpts) *runtime.ContainerConfig {
Expand Down
82 changes: 82 additions & 0 deletions integration/non_root_user_cap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

func TestNonRootUserCap(t *testing.T) {
for name, test := range map[string]struct {
uid int64
startOK bool
}{
"shouldn't be able to run container with a private workdir only accessible to nobody as a non-root user": {
uid: 1234,
startOK: false,
},
"shouldn be able to run container with a private workdir only accessible to nobody as root": {
uid: 0,
startOK: true,
},
} {
t.Run(name, func(t *testing.T) {
t.Log("Create a sandbox")
sbConfig := PodSandboxConfig("sandbox", "non-root-user-cap")
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
require.NoError(t, err)
// Make sure the sandbox is cleaned up.
defer func() {
assert.NoError(t, runtimeService.StopPodSandbox(sb))
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
}()

const (
testImage = "gcr.io/k8s-cri-containerd/private-workdir:1.0"
containerName = "test-container"
)
t.Logf("Pull test image %q", testImage)
img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil, sbConfig)
require.NoError(t, err)
defer func() {
assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img}))
}()

t.Log("Create a container to print capabilities")
cnConfig := ContainerConfig(
containerName,
testImage,
WithCommand("ls", "/"),
WithLogPath(containerName),
WithRunAsUser(test.uid),
)
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
require.NoError(t, err)

t.Log("Start the container")
if test.startOK {
require.NoError(t, runtimeService.StartContainer(cn))
} else {
require.Error(t, runtimeService.StartContainer(cn))
}
})
}
}
15 changes: 14 additions & 1 deletion pkg/containerd/opts/spec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func WithDevices(osi osinterface.OS, config *runtime.ContainerConfig) oci.SpecOp
}
}

// WithCapabilities sets the provided capabilties from the security context
// WithCapabilities sets the provided capabilities from the security context
func WithCapabilities(sc *runtime.LinuxContainerSecurityContext) oci.SpecOpts {
capabilities := sc.GetCapabilities()
if capabilities == nil {
Expand Down Expand Up @@ -637,3 +637,16 @@ func GetUTSNamespace(pid uint32) string {
func GetPIDNamespace(pid uint32) string {
return fmt.Sprintf(pidNSFormat, pid)
}

// WithCapDropForNonRootUser drops effective and permitted capabilities
// if the user is non-root.
// See https://github.com/moby/moby/pull/36587.
//
// This option should be behind options for setting users and capabilities.
func WithCapDropForNonRootUser(_ context.Context, _ oci.Client, _ *containers.Container, s *runtimespec.Spec) error {
if s.Process != nil && s.Process.User.UID != 0 && s.Process.Capabilities != nil {
s.Process.Capabilities.Effective = []string{}
s.Process.Capabilities.Permitted = []string{}
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/server/container_create_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
if seccompSpecOpts != nil {
specOpts = append(specOpts, seccompSpecOpts)
}

// Add this at the end to make sure that capabilities and users have both been set.
specOpts = append(specOpts, customopts.WithCapDropForNonRootUser)
return specOpts, nil
}

Expand Down

0 comments on commit bdbc2ab

Please sign in to comment.