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

feat: chroot isolation #2169

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
62c7e78
feat: chroot implemenation
Jul 11, 2022
1599183
feat(chroot): isolate every command
Jul 11, 2022
9a40a39
chore: go fmt
Jul 11, 2022
1fe5759
refactor(isolation): error on unknown isolation method
Jul 11, 2022
f5d8e61
refactor(chroot): use os.UserHomeDir
Jul 11, 2022
630ae61
fix(chroot): override config.RootDir and config.KanikoDir during isol…
Jul 11, 2022
2c47533
fix(chroot): unpack fs after isolation
Jul 11, 2022
edbb94a
fix(chroot): isolate stages and mount savedStages into chroot
Jul 11, 2022
5d7efa7
tests: use common function for kaniko cmd creation
Jul 11, 2022
484276d
fix(chroot): isolate stages instead of cmd and mount networking files
Jul 12, 2022
45b3881
test(chroot): fix k8s-job.yaml annotations
Jul 12, 2022
e8fe623
tests: use kanikoCmdCreater func for TestCache
Jul 12, 2022
1201d62
chore: setup for mount namespace isolation
Jul 12, 2022
8eb6d1e
fix(chroot): use seperate dir for multistage files
Jul 12, 2022
1ea25d3
Revert "tests: use common function for kaniko cmd creation"
Jul 12, 2022
2384cb6
tests: add securityFlags to all tests
Jul 12, 2022
8d10499
tests: fix buildcachedImages calls
Jul 12, 2022
3dbca6b
fix(chroot): extract extra stages into KanikoDependencyDir
Jul 12, 2022
cfda674
fix: unpack fs for every stage instead of every command
Jul 13, 2022
ddd4825
tests: add securityFlags to TestExitCodePropagation
Jul 13, 2022
64d57fc
chore: move spam logs into trace level
Jul 13, 2022
71c4198
format
Jul 21, 2022
70fcf2f
Merge branch 'main' into chroot-isolation
Jul 21, 2022
e71461a
feat(chroot): implement user lookup
Jul 21, 2022
404e410
refactor(chroot): user and group lookup
Jul 22, 2022
1a20ed9
refactor: user lookup
Jul 22, 2022
b7867ec
refactor(chroot): use chroot on command
Jul 22, 2022
f4e34b0
fix(userlookup): syscallcredentials were parsed wrong
Jul 22, 2022
17d0453
tests(chroot): user and group lookup
Jul 22, 2022
d61467d
refactor: remove config.RootDir var
Jul 24, 2022
63db4b2
fix(chroot): exit chroot loop
Jul 24, 2022
70d25d5
refactor: remove rootDir prefix in tarball from layer
Jul 24, 2022
d8020be
tests(snapshot): use stripped paths
Jul 24, 2022
2823d50
tests(copycmd): set rootDir to test tempDir
Jul 24, 2022
0762095
tests(runcmd): extractedFiles must have testDir prefix
Jul 24, 2022
f33b342
chore(tar): remove unused rootDir attr
Jul 24, 2022
7482653
fix(chroot): create random dir in root with 755 perm
Jul 24, 2022
02327ef
tests(UnTar): prefix destination with testDir
Jul 24, 2022
99ff650
chore: format and fix linter issues
Jul 24, 2022
7089972
fix(chroot): delay ignoreList init after isolation
Jul 24, 2022
d8b5435
fix(add): chown in rootDir failed
Jul 24, 2022
0d04db8
feat(chroot): isolation with reexec and namespace implementation
Jul 28, 2022
f948465
feat(chroot): newidmap support
Jul 28, 2022
d9d164a
refactor(chroot): create seperate unshare package
Jul 28, 2022
a60076e
tests(unshare): namespace and mapping tests
Jul 29, 2022
87a408d
refactor(unshare): set unshareFlags directly in Command()
Jul 29, 2022
886433d
refactor(unshare): mapping logic
Jul 29, 2022
766f8e8
fix(unshare): write user lookup fails to continueWriter
Jul 29, 2022
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
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().BoolVarP(&opts.CacheCopyLayers, "cache-copy-layers", "", false, "Caches copy layers")
RootCmd.PersistentFlags().VarP(&opts.IgnorePaths, "ignore-path", "", "Ignore these paths when taking a snapshot. Set it repeatedly for multiple paths.")
RootCmd.PersistentFlags().BoolVarP(&opts.ForceBuildMetadata, "force-build-metadata", "", false, "Force add metadata layers to build image")
RootCmd.PersistentFlags().StringVarP(&opts.Isolation, "isolation", "", "chroot", "Which isolation method to use (possible implementation: chroot, none)")

// Allow setting --registry-mirror using an environment variable.
if val, ok := os.LookupEnv("KANIKO_REGISTRY_MIRROR"); ok {
Expand Down
37 changes: 34 additions & 3 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,37 @@ RUN \
FROM debian:bullseye-slim AS certs
RUN apt update && apt install -y ca-certificates

FROM debian:bullseye-slim AS passwd
RUN echo "root:x:0:" > /etc/group
RUN echo "root:x:0:0:root:/root:/sbin/nologin" > /etc/passwd

# idmap runnable without --privileged (but still requires seccomp=unconfined apparmor=unconfined)
FROM alpine AS idmap
ARG SHADOW_VERSION="v4.11.1"
ARG PATCHELF_VERSION="0.15.0"
ENV DEBIAN_FRONTEND=noninteractive
RUN apk add --no-cache automake autoconf gettext git build-base libcap-dev libtool make gettext gettext-dev linux-pam-dev expect byacc
RUN git clone https://github.com/shadow-maint/shadow.git /shadow
WORKDIR /shadow
RUN git fetch && git checkout $SHADOW_VERSION
RUN ./autogen.sh --disable-nls --disable-man --without-audit --without-selinux --without-acl --without-attr --without-tcb --without-nscd \
--disable-shared --enable-static=yes
RUN make
# set setgid and setuid filemode bits on new{gid,uid}map
RUN chmod g+s src/newgidmap && chmod u+s src/newuidmap

RUN wget -O patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-$(uname -m).tar.gz && \
tar xf patchelf.tar.gz && \
cp bin/patchelf /bin

# use patchelf to modify the library path so lib musl can reside in /kaniko/lib dir
RUN patchelf --set-interpreter /kaniko/lib/ld-musl-$(uname -m).so.1 src/newuidmap
RUN patchelf --set-interpreter /kaniko/lib/ld-musl-$(uname -m).so.1 src/newgidmap

FROM scratch
# Create kaniko directory with world write permission to allow non root run
# use musl busybox since it's staticly compiled
RUN --mount=from=busybox:musl,dst=/usr/ ["busybox", "sh", "-c", "mkdir -p /kaniko && chmod 777 /kaniko"]
RUN --mount=from=busybox:musl,dst=/usr/ ["busybox", "sh", "-c", "mkdir -p /kaniko /root /etc && chmod 777 /kaniko"]

COPY --from=0 /src/out/executor /kaniko/executor
COPY --from=0 /usr/local/bin/docker-credential-gcr /kaniko/docker-credential-gcr
Expand All @@ -56,8 +83,12 @@ COPY --from=0 /usr/local/bin/docker-credential-acr-env /kaniko/docker-credential
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /kaniko/ssl/certs/
COPY --from=0 /kaniko/.docker /kaniko/.docker
COPY files/nsswitch.conf /etc/nsswitch.conf
ENV HOME /root
ENV USER root
COPY --from=passwd /etc/passwd /etc/group /etc/
COPY --from=idmap /shadow/src/newuidmap /shadow/src/newgidmap /kaniko/
# shadowutils is not a static binary, need to use musl libc
COPY --from=idmap /lib/ld-musl-*.so.1 /lib/libc.musl-*.so.1 /kaniko/lib/

USER root
ENV PATH /usr/local/bin:/kaniko
ENV SSL_CERT_DIR=/kaniko/ssl/certs
ENV DOCKER_CONFIG /kaniko/.docker/
Expand Down
39 changes: 36 additions & 3 deletions deploy/Dockerfile_debug
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,40 @@ RUN \
FROM debian:bullseye-slim AS certs
RUN apt update && apt install -y ca-certificates


FROM debian:bullseye-slim AS passwd
RUN echo "root:x:0:" > /etc/group
RUN echo "root:x:0:0:root:/root:/sbin/nologin" > /etc/passwd

# idmap runnable without --privileged (but still requires seccomp=unconfined apparmor=unconfined)
FROM alpine AS idmap
ARG SHADOW_VERSION="v4.11.1"
ARG PATCHELF_VERSION="0.15.0"
ENV DEBIAN_FRONTEND=noninteractive
RUN apk add --no-cache automake autoconf gettext git build-base libcap-dev libtool make gettext gettext-dev linux-pam-dev expect byacc
RUN git clone https://github.com/shadow-maint/shadow.git /shadow
WORKDIR /shadow
RUN git fetch && git checkout $SHADOW_VERSION
RUN ./autogen.sh --disable-nls --disable-man --without-audit --without-selinux --without-acl --without-attr --without-tcb --without-nscd \
--disable-shared --enable-static=yes
RUN make
# set setgid and setuid filemode bits on new{gid,uid}map
RUN chmod g+s src/newgidmap && chmod u+s src/newuidmap

RUN wget -O patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-$(uname -m).tar.gz && \
tar xf patchelf.tar.gz && \
cp bin/patchelf /bin

# use patchelf to modify the library path so lib musl can reside in /kaniko/lib dir
RUN patchelf --set-interpreter /kaniko/lib/ld-musl-$(uname -m).so.1 src/newuidmap
RUN patchelf --set-interpreter /kaniko/lib/ld-musl-$(uname -m).so.1 src/newgidmap


# use musl busybox since it's staticly compiled on all platforms
FROM busybox:musl as busybox
FROM scratch
# Create kaniko directory with world write permission to allow non root run
RUN --mount=from=busybox,dst=/usr/ ["busybox", "sh", "-c", "mkdir -p /kaniko && chmod 777 /kaniko"]
RUN --mount=from=busybox,dst=/usr/ ["busybox", "sh", "-c", "mkdir -p /kaniko /root && chmod 777 /kaniko"]

COPY --from=0 /src/out/executor /kaniko/executor
COPY --from=0 /src/out/warmer /kaniko/warmer
Expand All @@ -63,8 +92,12 @@ VOLUME /busybox
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /kaniko/ssl/certs/
COPY --from=0 /kaniko/.docker /kaniko/.docker
COPY files/nsswitch.conf /etc/nsswitch.conf
ENV HOME /root
ENV USER root
COPY --from=passwd /etc/passwd /etc/group /etc/
COPY --from=idmap /shadow/src/newuidmap /shadow/src/newgidmap /kaniko/
# shadowutils is not a static binary, need to use musl libc
COPY --from=idmap /lib/ld-musl-*.so.1 /lib/libc.musl-*.so.1 /kaniko/lib/

USER root
ENV PATH /usr/local/bin:/kaniko:/busybox
ENV SSL_CERT_DIR=/kaniko/ssl/certs
ENV DOCKER_CONFIG /kaniko/.docker/
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/sys/mount v0.3.0 // indirect
github.com/moby/sys/mount v0.3.0
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/sys/symlink v0.2.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
Expand Down
3 changes: 3 additions & 0 deletions integration/dockerfiles-isolation/Dockerfile_test_chroot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine

RUN ls -al
1 change: 1 addition & 0 deletions integration/dockerfiles/Dockerfile_test_user_nonexisting
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

FROM debian:9.11
USER 1001:1001
RUN ["echo", "hello"]
RUN echo "hey2" > /tmp/foo
USER 1001
RUN echo "hello" > /tmp/foobar
Expand Down
26 changes: 19 additions & 7 deletions integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ func addServiceAccountFlags(flags []string, serviceAccount string) []string {
return flags
}

// addSecurityFlags adds seccomp and apparmor profile disable and adds cap SYS_ADMIN
func addSecurityFlags(flags []string) []string {
return append(flags, []string{
// chroot needs CAP_SYS_ADMIN
"--cap-add", "SYS_ADMIN",
// disable apparmor and seccomp because it permits mounts
"--security-opt", "apparmor=unconfined",
"--security-opt", "seccomp=unconfined",
}...)
}

func (d *DockerFileBuilder) BuildDockerImage(t *testing.T, imageRepo, dockerfilesPath, dockerfile, contextDir string) error {
t.Logf("Building image for Dockerfile %s\n", dockerfile)

Expand Down Expand Up @@ -283,7 +294,6 @@ func (d *DockerFileBuilder) BuildDockerImage(t *testing.T, imageRepo, dockerfile
func (d *DockerFileBuilder) BuildImage(t *testing.T, config *integrationTestConfig, dockerfilesPath, dockerfile string) error {
_, ex, _, _ := runtime.Caller(0)
cwd := filepath.Dir(ex)

return d.BuildImageWithContext(t, config, dockerfilesPath, dockerfile, cwd)
}

Expand Down Expand Up @@ -375,16 +385,17 @@ func (d *DockerFileBuilder) buildCachedImages(config *integrationTestConfig, cac
"-v", cwd + ":/workspace",
"-e", benchmarkEnv}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)

dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", path.Join(buildContextPath, dockerfilesPath, dockerfile),
"-d", kanikoImage,
"-c", buildContextPath,
cacheFlag,
"--cache-repo", cacheRepo,
"--cache-dir", cacheDir)
for _, v := range args {
dockerRunFlags = append(dockerRunFlags, v)
}
"--cache-dir", cacheDir,
)
dockerRunFlags = append(dockerRunFlags, args...)
kanikoCmd := exec.Command("docker", dockerRunFlags...)

_, err := RunCommandWithoutTest(kanikoCmd)
Expand Down Expand Up @@ -420,6 +431,7 @@ func (d *DockerFileBuilder) buildRelativePathsImage(imageRepo, dockerfile, servi

dockerRunFlags := []string{"run", "--net=host", "-v", cwd + ":/workspace"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -481,15 +493,15 @@ func buildKanikoImage(
"-v", contextDir + ":/workspace",
"-v", benchmarkDir + ":/kaniko/benchmarks",
}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)

if env, ok := envsMap[dockerfile]; ok {
for _, envVariable := range env {
dockerRunFlags = append(dockerRunFlags, "-e", envVariable)
}
}

dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)

kanikoDockerfilePath := path.Join(buildContextPath, dockerfilesPath, dockerfile)
if dockerfilesPath == "" {
kanikoDockerfilePath = path.Join(buildContextPath, "Dockerfile")
Expand Down
7 changes: 7 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func testGitBuildcontextHelper(t *testing.T, repo string) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -327,6 +328,7 @@ func TestGitBuildcontextSubPath(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(
dockerRunFlags,
ExecutorImage,
Expand Down Expand Up @@ -369,6 +371,7 @@ func TestBuildViaRegistryMirrors(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_registry_mirror")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -410,6 +413,7 @@ func TestKanikoDir(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_registry_mirror")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -452,6 +456,7 @@ func TestBuildWithLabels(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_label:mylabel")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -492,6 +497,7 @@ func TestBuildWithHTTPError(t *testing.T) {
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_add_404")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
Expand Down Expand Up @@ -668,6 +674,7 @@ func TestExitCodePropagation(t *testing.T) {
"-v", contextVolume,
}
dockerFlags = addServiceAccountFlags(dockerFlags, "")
dockerFlags = addSecurityFlags(dockerFlags)
dockerFlags = append(dockerFlags, ExecutorImage,
"-c", "dir:///workspace/",
"-f", "./Dockerfile_exit_code_propagation",
Expand Down
3 changes: 2 additions & 1 deletion integration/integration_with_stdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestBuildWithStdin(t *testing.T) {
gw := gzip.NewWriter(tarFile)
defer gw.Close()

tw := util.NewTar(gw)
tw := util.NewTar(testDirLongPath, gw)
defer tw.Close()

if err := tw.AddFileToTar(dockerfile); err != nil {
Expand Down Expand Up @@ -103,6 +103,7 @@ func TestBuildWithStdin(t *testing.T) {

dockerRunFlags := []string{"run", "--interactive", "--net=host", "-v", cwd + ":/workspace"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addSecurityFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags,
ExecutorImage,
"-f", dockerfile,
Expand Down
9 changes: 9 additions & 0 deletions integration/k8s-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ metadata:
name: kaniko-test-{{.Name}}
spec:
template:
metadata:
annotations:
# disable seccomp and apparmor for chroot mounts
container.apparmor.security.beta.kubernetes.io/kaniko: unconfined
container.seccomp.security.alpha.kubernetes.io/kaniko: unconfined
spec:
hostNetwork: true
containers:
Expand All @@ -15,6 +20,10 @@ spec:
volumeMounts:
- name: context
mountPath: /workspace
securityContext:
capabilities:
add:
- SYS_ADMIN
restartPolicy: Never
volumes:
- name: context
Expand Down
2 changes: 1 addition & 1 deletion integration/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func CreateIntegrationTarball() (string, error) {
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()

err = util.CreateTarballOfDirectory(dir, file)
err = util.CreateTarballOfDirectory("/", dir, file)
if err != nil {
return "", fmt.Errorf("creating tarball of integration dir: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildcontext/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestBuildWithLocalTar(t *testing.T) {
gw := gzip.NewWriter(validTarFile)
defer gw.Close()

tw := util.NewTar(gw)
tw := util.NewTar(testDirLongPath, gw)
defer tw.Close()

if err := tw.AddFileToTar(validDockerfile); err != nil {
Expand Down
49 changes: 49 additions & 0 deletions pkg/chroot/capabilities_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build linux
// +build linux

package chroot

import (
"fmt"

"github.com/syndtr/gocapability/capability"
)

// defaultCapabilities returns a Linux kernel default capabilities
var defaultCapabilities = []capability.Cap{
capability.CAP_CHOWN,
capability.CAP_DAC_OVERRIDE,
capability.CAP_FSETID,
capability.CAP_FOWNER,
capability.CAP_MKNOD,
capability.CAP_NET_RAW,
capability.CAP_SETGID,
capability.CAP_SETUID,
capability.CAP_SETFCAP,
capability.CAP_SETPCAP,
capability.CAP_NET_BIND_SERVICE,
capability.CAP_KILL,
capability.CAP_AUDIT_WRITE,
}

// setCapabilities sets capabilities for ourselves, to be more or less inherited by any processes that we'll start.
func setCapabilities() error {
caps, err := capability.NewPid2(0)
if err != nil {
return err
}
capMap := map[capability.CapType][]capability.Cap{
capability.BOUNDING: defaultCapabilities,
capability.EFFECTIVE: defaultCapabilities,
capability.INHERITABLE: {},
capability.PERMITTED: defaultCapabilities,
}
for capType, capList := range capMap {
caps.Set(capType, capList...)
}
err = caps.Apply(capability.CAPS | capability.BOUNDS | capability.AMBS)
if err != nil {
return fmt.Errorf("applying capabiliies: %w", err)
}
return nil
}
Loading