Skip to content

Commit 109f5e6

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

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
@@ -215,4 +215,94 @@ var _ = Describe("docker", Ordered, ContinueOnFailure, func() {
215215
Expect(ldconfigOut).To(ContainSubstring("/usr/lib64"))
216216
})
217217
})
218+
219+
When("Running a container with LD_PRELOAD", Ordered, func() {
220+
BeforeAll(func(ctx context.Context) {
221+
// Create the source for the poc.
222+
_, _, err := r.Run(`cat <<EOF > poc.c
223+
/**
224+
Code taken from https://youtu.be/56vcNIh35PA?si=gmh7Cx9P-lNTbl4L&t=328
225+
**/
226+
#include <stdio.h>
227+
#include <stdlib.h>
228+
#include <unistd.h>
229+
#include <fcntl.h>
230+
__attribute__((constructor))
231+
void init() {
232+
// Ultra-minimal exploit just create a marker file
233+
int fd = open("/owned", O_CREAT | O_WRONLY, 0644);
234+
if (fd >= 0) {
235+
write(fd, "EXPLOITED\n", 10);
236+
close(fd);
237+
}
238+
}
239+
EOF`)
240+
Expect(err).ToNot(HaveOccurred())
241+
242+
// Create the local Dockerfile
243+
_, _, err = r.Run(`cat <<EOF > Dockerfile.nvidiascape
244+
FROM ubuntu AS build
245+
RUN apt-get update && \
246+
apt-get install -y gcc \
247+
&& \
248+
rm -rf /var/lib/apt/lists/*
249+
ADD poc.c .
250+
RUN gcc -shared -fPIC -o poc.so poc.c
251+
FROM ubuntu
252+
ENV LD_PRELOAD=/proc/self/cwd/poc.so
253+
COPY --from=build poc.so /
254+
EOF`)
255+
Expect(err).ToNot(HaveOccurred())
256+
257+
// Build the test image.
258+
_, _, err = r.Run(`docker build -t nvidiascape-test -f Dockerfile.nvidiascape .`)
259+
Expect(err).ToNot(HaveOccurred())
260+
261+
_, _, err = r.Run("rm -f /owned")
262+
Expect(err).ToNot(HaveOccurred())
263+
})
264+
265+
AfterAll(func(ctx context.Context) {
266+
_, _, err := r.Run("rm -f poc.c")
267+
Expect(err).ToNot(HaveOccurred())
268+
269+
_, _, err = r.Run("rm -f Dockerfile.nvidiascape")
270+
Expect(err).ToNot(HaveOccurred())
271+
})
272+
273+
AfterEach(func(ctx context.Context) {
274+
_, _, err := r.Run("rm -f /owned")
275+
Expect(err).ToNot(HaveOccurred())
276+
})
277+
278+
It("should not escape when using CDI", func(ctx context.Context) {
279+
_, _, err := r.Run("docker run --rm --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=runtime.nvidia.com/gpu=all nvidiascape-test")
280+
Expect(err).ToNot(HaveOccurred())
281+
282+
stdout, stderr, err := r.Run(`cat /owned || echo "Unsuccessful"`)
283+
Expect(err).ToNot(HaveOccurred())
284+
Expect(stderr).To(BeEmpty())
285+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
286+
})
287+
288+
It("should not escape when using the nvidia-container-runtime", func(ctx context.Context) {
289+
_, _, err := r.Run("docker run --rm --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=all nvidiascape-test")
290+
Expect(err).ToNot(HaveOccurred())
291+
292+
stdout, stderr, err := r.Run(`cat /owned || echo "Unsuccessful"`)
293+
Expect(err).ToNot(HaveOccurred())
294+
Expect(stderr).To(BeEmpty())
295+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
296+
})
297+
298+
It("should not escape when using the nvidia-container-runtime-hook", Label("legacy"), func(ctx context.Context) {
299+
_, _, err := r.Run("docker run --rm --runtime=runc --gpus=all nvidiascape-test")
300+
Expect(err).ToNot(HaveOccurred())
301+
302+
stdout, stderr, err := r.Run(`cat /owned || echo "Unsuccessful"`)
303+
Expect(err).ToNot(HaveOccurred())
304+
Expect(stderr).To(BeEmpty())
305+
Expect(strings.TrimSpace(stdout)).To(Equal("Unsuccessful"))
306+
})
307+
})
218308
})

0 commit comments

Comments
 (0)