Verify that a shortened vector supplied through PR_SET_MM_AUXV has a
valid AT_NULL terminator when exposed through /proc/self/auxv. This
catches stale AUXV entries left beyond the supplied vector.

Signed-off-by: Sahaj Chaudhari <[email protected]>
---
 tools/testing/selftests/prctl/.gitignore      |  1 +
 tools/testing/selftests/prctl/Makefile        |  1 +
 .../testing/selftests/prctl/pr_set_mm_auxv.c  | 42 +++++++++++++++++++
 3 files changed, 44 insertions(+)
 create mode 100644 tools/testing/selftests/prctl/pr_set_mm_auxv.c

diff --git a/tools/testing/selftests/prctl/.gitignore 
b/tools/testing/selftests/prctl/.gitignore
index 05d5e31661dfd..089af0296aeca 100644
--- a/tools/testing/selftests/prctl/.gitignore
+++ b/tools/testing/selftests/prctl/.gitignore
@@ -4,3 +4,4 @@ disable-tsc-on-off-stress-test
 disable-tsc-test
 set-anon-vma-name-test
 set-process-name
+pr_set_mm_auxv
diff --git a/tools/testing/selftests/prctl/Makefile 
b/tools/testing/selftests/prctl/Makefile
index e770e86fad9ac..a21811a8f6a06 100644
--- a/tools/testing/selftests/prctl/Makefile
+++ b/tools/testing/selftests/prctl/Makefile
@@ -6,6 +6,7 @@ override ARCH := $(shell echo $(ARCH) | sed -e s/i.86/x86/ -e 
s/x86_64/x86/)
 ifeq ($(ARCH),x86)
 TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
                disable-tsc-test set-anon-vma-name-test set-process-name
+TEST_GEN_PROGS := pr_set_mm_auxv
 all: $(TEST_PROGS)
 
 include ../lib.mk
diff --git a/tools/testing/selftests/prctl/pr_set_mm_auxv.c 
b/tools/testing/selftests/prctl/pr_set_mm_auxv.c
new file mode 100644
index 0000000000000..934ff480c0f4d
--- /dev/null
+++ b/tools/testing/selftests/prctl/pr_set_mm_auxv.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <elf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/prctl.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+TEST(set_mm_auxv_terminator)
+{
+       unsigned long auxv[] = {
+               AT_UID, 0x1111,
+               AT_GID, 0x2222,
+       };
+       unsigned long result[64];
+       ssize_t len;
+       int fd;
+
+       if (prctl(PR_SET_MM, PR_SET_MM_AUXV, (unsigned long)auxv,
+                 sizeof(auxv), 0)) {
+               if (errno == EPERM)
+                       SKIP(return, "requires CAP_SYS_RESOURCE");
+               ASSERT_EQ(errno, 0);
+       }
+
+       fd = open("/proc/self/auxv", O_RDONLY);
+       ASSERT_GE(fd, 0);
+
+       len = read(fd, result, sizeof(result));
+       close(fd);
+
+       ASSERT_GE(len, (ssize_t)(6 * sizeof(unsigned long)));
+       EXPECT_EQ(result[0], (unsigned long)AT_UID);
+       EXPECT_EQ(result[1], 0x1111UL);
+       EXPECT_EQ(result[2], (unsigned long)AT_GID);
+       EXPECT_EQ(result[3], 0x2222UL);
+       EXPECT_EQ(result[4], (unsigned long)AT_NULL);
+       EXPECT_EQ(result[5], 0UL);
+}
+
+TEST_HARNESS_MAIN
-- 
2.43.0


Reply via email to