Skip to content

Commit a77e742

Browse files
committed
[no-relnote] Add basic regression test for nvidiascape
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 81e31b0 commit a77e742

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

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)