selftests: prctl: Add new prctl test for PR_SET_VMA action
authorIvan Orlov <ivan.orlov0322@gmail.com>
Sat, 25 Mar 2023 20:59:02 +0000 (00:59 +0400)
committerShuah Khan <skhan@linuxfoundation.org>
Tue, 28 Mar 2023 16:28:33 +0000 (10:28 -0600)
This patch will add the new test, which covers the prctl call with
PR_SET_VMA command. The test tries to give a name to the anonymous
VMA within the process memory map, and then checks the result of
the operation by parsing 'maps' virtual file.

Additionally, the test tries to call the prctl PR_SET_VMA command
with invalid arguments, and checks the error codes for correctness.

At the moment anonymous VMA naming through prctl call functionality
is not covered with any tests, so I think implementing it makes sense.

In version 2 of this patch I consider the selftest Makefile rule about
TARGETS entries order - I moved the 'prctl' entry in the Makefile to
follow the lexicographic order. In version 1 it was placed at the
end of the list.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/Makefile
tools/testing/selftests/prctl/.gitignore
tools/testing/selftests/prctl/Makefile
tools/testing/selftests/prctl/config [new file with mode: 0644]
tools/testing/selftests/prctl/set-anon-vma-name-test.c [new file with mode: 0644]

index 13a6837a0c6bc62902833292e10d055cafdbc6c1..97dcdaa656f6e6bcff0d765e66a638785a7393ca 100644 (file)
@@ -58,6 +58,7 @@ TARGETS += nsfs
 TARGETS += pidfd
 TARGETS += pid_namespace
 TARGETS += powerpc
+TARGETS += prctl
 TARGETS += proc
 TARGETS += pstore
 TARGETS += ptrace
index 91af2b631bc96037875a0e6bdb3dd8305229e412..7a657b25f686d38e125a82d15a5279285bfd83ad 100644 (file)
@@ -2,3 +2,4 @@
 disable-tsc-ctxt-sw-stress-test
 disable-tsc-on-off-stress-test
 disable-tsc-test
+set-anon-vma-name-test
index c7923b205222dd0361a8971426780fde5498c19a..c058b81eeb41860b8dd915a72146340f253c0f18 100644 (file)
@@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | 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
+               disable-tsc-test set-anon-vma-name-test
 all: $(TEST_PROGS)
 
 include ../lib.mk
diff --git a/tools/testing/selftests/prctl/config b/tools/testing/selftests/prctl/config
new file mode 100644 (file)
index 0000000..c6ed03c
--- /dev/null
@@ -0,0 +1 @@
+CONFIG_ANON_VMA_NAME=y
diff --git a/tools/testing/selftests/prctl/set-anon-vma-name-test.c b/tools/testing/selftests/prctl/set-anon-vma-name-test.c
new file mode 100644 (file)
index 0000000..26d853c
--- /dev/null
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This test covers the anonymous VMA naming functionality through prctl calls
+ */
+
+#include <errno.h>
+#include <sys/prctl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <string.h>
+
+#include "../kselftest_harness.h"
+
+#define AREA_SIZE 1024
+
+#define GOOD_NAME "goodname"
+#define BAD_NAME "badname\1"
+
+#ifndef PR_SET_VMA
+#define PR_SET_VMA 0x53564d41
+#define PR_SET_VMA_ANON_NAME 0
+#endif
+
+
+int rename_vma(unsigned long addr, unsigned long size, char *name)
+{
+       int res;
+
+       res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, size, name);
+       if (res < 0)
+               return -errno;
+       return res;
+}
+
+int was_renaming_successful(char *target_name, unsigned long ptr)
+{
+       FILE *maps_file;
+
+       char line_buf[512], name[128], mode[8];
+       unsigned long start_addr, end_addr, offset;
+       unsigned int major_id, minor_id, node_id;
+
+       char target_buf[128];
+       int res = 0, sscanf_res;
+
+       // The entry name in maps will be in format [anon:<target_name>]
+       sprintf(target_buf, "[anon:%s]", target_name);
+       maps_file = fopen("/proc/self/maps", "r");
+       if (!maps_file) {
+               printf("## /proc/self/maps file opening error\n");
+               return 0;
+       }
+
+       // Parse the maps file to find the entry we renamed
+       while (fgets(line_buf, sizeof(line_buf), maps_file)) {
+               sscanf_res = sscanf(line_buf, "%lx-%lx %7s %lx %u:%u %u %s", &start_addr,
+                                       &end_addr, mode, &offset, &major_id,
+                                       &minor_id, &node_id, name);
+               if (sscanf_res == EOF) {
+                       res = 0;
+                       printf("## EOF while parsing the maps file\n");
+                       break;
+               }
+               if (!strcmp(name, target_buf) && start_addr == ptr) {
+                       res = 1;
+                       break;
+               }
+       }
+       fclose(maps_file);
+       return res;
+}
+
+FIXTURE(vma) {
+       void *ptr_anon, *ptr_not_anon;
+};
+
+FIXTURE_SETUP(vma) {
+       self->ptr_anon = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE,
+                                       MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+       ASSERT_NE(self->ptr_anon, NULL);
+       self->ptr_not_anon = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE,
+                                       MAP_PRIVATE, 0, 0);
+       ASSERT_NE(self->ptr_not_anon, NULL);
+}
+
+FIXTURE_TEARDOWN(vma) {
+       munmap(self->ptr_anon, AREA_SIZE);
+       munmap(self->ptr_not_anon, AREA_SIZE);
+}
+
+TEST_F(vma, renaming) {
+       TH_LOG("Try to rename the VMA with correct parameters");
+       EXPECT_GE(rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, GOOD_NAME), 0);
+       EXPECT_TRUE(was_renaming_successful(GOOD_NAME, (unsigned long)self->ptr_anon));
+
+       TH_LOG("Try to pass invalid name (with non-printable character \\1) to rename the VMA");
+       EXPECT_EQ(rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, BAD_NAME), -EINVAL);
+
+       TH_LOG("Try to rename non-anonynous VMA");
+       EXPECT_EQ(rename_vma((unsigned long) self->ptr_not_anon, AREA_SIZE, GOOD_NAME), -EINVAL);
+}
+
+TEST_HARNESS_MAIN