Skip to content

Commit b179712

Browse files
authored
Merge pull request #1327 from elezar/add-nvidiascape-test
Add test when using LD_PRELOAD in a container
2 parents e03ac36 + a77e742 commit b179712

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

tests/e2e/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ test-preinstalled: test
3636
test-preinstalled: E2E_INSTALL_CTK = false
3737
test-preinstalled: E2E_SSH_HOST =
3838

39-
39+
GINKGO_VERSION = $(shell grep -Eo "github.com/onsi/ginkgo/v2.*$$" ./tests/go.mod | sed -e 's&github.com/onsi/ginkgo/v2[[:space:]]&&g')
40+
ginkgo: $(GINKGO_BIN)
4041
$(GINKGO_BIN):
4142
mkdir -p $(CURDIR)/bin
42-
GOBIN=$(CURDIR)/bin go install github.com/onsi/ginkgo/v2/ginkgo@latest
43+
GOBIN=$(CURDIR)/bin go install github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION)

tests/e2e/nvidia-container-toolkit_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,94 @@ EOF`)
444444
Expect(stderr).To(ContainSubstring("nvidia-container-cli.real: mount error: path error:"))
445445
})
446446
})
447+
448+
When("Running a container with LD_PRELOAD", Ordered, func() {
449+
BeforeAll(func(ctx context.Context) {
450+
// Create the source for the poc.
451+
_, _, err := runner.Run(`cat <<EOF > poc.c
452+
/**
453+
Code taken from https://youtu.be/56vcNIh35PA?si=gmh7Cx9P-lNTbl4L&t=328
454+
**/
455+
#include <stdio.h>
456+
#include <stdlib.h>
457+
#include <unistd.h>
458+
#include <fcntl.h>
459+
__attribute__((constructor))
460+
void init() {
461+
// Ultra-minimal exploit just create a marker file
462+
int fd = open("/owned", O_CREAT | O_WRONLY, 0644);
463+
if (fd >= 0) {
464+
write(fd, "EXPLOITED\n", 10);
465+
close(fd);
466+
}
467+
}
468+
EOF`)
469+
Expect(err).ToNot(HaveOccurred())
470+
471+
// Create the local Dockerfile
472+
_, _, err = runner.Run(`cat <<EOF > Dockerfile.nvidiascape
473+
FROM ubuntu AS build
474+
RUN apt-get update && \
475+
apt-get install -y gcc \
476+
&& \
477+
rm -rf /var/lib/apt/lists/*
478+
ADD poc.c .
479+
RUN gcc -shared -fPIC -o poc.so poc.c
480+
FROM ubuntu
481+
ENV LD_PRELOAD=/proc/self/cwd/poc.so
482+
COPY --from=build poc.so /
483+
EOF`)
484+
Expect(err).ToNot(HaveOccurred())
485+
486+
// Build the test image.
487+
_, _, err = runner.Run(`docker build -t nvidiascape-test -f Dockerfile.nvidiascape .`)
488+
Expect(err).ToNot(HaveOccurred())
489+
490+
_, _, err = runner.Run("rm -f /owned")
491+
Expect(err).ToNot(HaveOccurred())
492+
})
493+
494+
AfterAll(func(ctx context.Context) {
495+
_, _, err := runner.Run("rm -f poc.c")
496+
Expect(err).ToNot(HaveOccurred())
497+
498+
_, _, err = runner.Run("rm -f Dockerfile.nvidiascape")
499+
Expect(err).ToNot(HaveOccurred())
500+
})
501+
502+
AfterEach(func(ctx context.Context) {
503+
_, _, err := runner.Run("rm -f /owned")
504+
Expect(err).ToNot(HaveOccurred())
505+
})
506+
507+
It("should not escape when using CDI", func(ctx context.Context) {
508+
_, _, err := runner.Run("docker run --rm --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=runtime.nvidia.com/gpu=all nvidiascape-test")
509+
Expect(err).ToNot(HaveOccurred())
510+
511+
stdout, stderr, err := runner.Run(`cat /owned || echo "Unsuccessful"`)
512+
Expect(err).ToNot(HaveOccurred())
513+
Expect(stderr).To(BeEmpty())
514+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
515+
})
516+
517+
It("should not escape when using the nvidia-container-runtime", func(ctx context.Context) {
518+
_, _, err := runner.Run("docker run --rm --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=all nvidiascape-test")
519+
Expect(err).ToNot(HaveOccurred())
520+
521+
stdout, stderr, err := runner.Run(`cat /owned || echo "Unsuccessful"`)
522+
Expect(err).ToNot(HaveOccurred())
523+
Expect(stderr).To(BeEmpty())
524+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
525+
})
526+
527+
It("should not escape when using the nvidia-container-runtime-hook", Label("legacy"), func(ctx context.Context) {
528+
_, _, err := runner.Run("docker run --rm --runtime=runc --gpus=all nvidiascape-test")
529+
Expect(err).ToNot(HaveOccurred())
530+
531+
stdout, stderr, err := runner.Run(`cat /owned || echo "Unsuccessful"`)
532+
Expect(err).ToNot(HaveOccurred())
533+
Expect(stderr).To(BeEmpty())
534+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
535+
})
536+
})
447537
})

0 commit comments

Comments
 (0)