~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/prctl/set-anon-vma-name-test.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * This test covers the anonymous VMA naming functionality through prctl calls
  4  */
  5 
  6 #include <errno.h>
  7 #include <sys/prctl.h>
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <sys/mman.h>
 11 #include <string.h>
 12 
 13 #include "../kselftest_harness.h"
 14 
 15 #define AREA_SIZE 1024
 16 
 17 #define GOOD_NAME "goodname"
 18 #define BAD_NAME "badname\1"
 19 
 20 #ifndef PR_SET_VMA
 21 #define PR_SET_VMA 0x53564d41
 22 #define PR_SET_VMA_ANON_NAME 0
 23 #endif
 24 
 25 
 26 int rename_vma(unsigned long addr, unsigned long size, char *name)
 27 {
 28         int res;
 29 
 30         res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, size, name);
 31         if (res < 0)
 32                 return -errno;
 33         return res;
 34 }
 35 
 36 int was_renaming_successful(char *target_name, unsigned long ptr)
 37 {
 38         FILE *maps_file;
 39 
 40         char line_buf[512], name[128], mode[8];
 41         unsigned long start_addr, end_addr, offset;
 42         unsigned int major_id, minor_id, node_id;
 43 
 44         char target_buf[128];
 45         int res = 0, sscanf_res;
 46 
 47         // The entry name in maps will be in format [anon:<target_name>]
 48         sprintf(target_buf, "[anon:%s]", target_name);
 49         maps_file = fopen("/proc/self/maps", "r");
 50         if (!maps_file) {
 51                 printf("## /proc/self/maps file opening error\n");
 52                 return 0;
 53         }
 54 
 55         // Parse the maps file to find the entry we renamed
 56         while (fgets(line_buf, sizeof(line_buf), maps_file)) {
 57                 sscanf_res = sscanf(line_buf, "%lx-%lx %7s %lx %u:%u %u %s", &start_addr,
 58                                         &end_addr, mode, &offset, &major_id,
 59                                         &minor_id, &node_id, name);
 60                 if (sscanf_res == EOF) {
 61                         res = 0;
 62                         printf("## EOF while parsing the maps file\n");
 63                         break;
 64                 }
 65                 if (!strcmp(name, target_buf) && start_addr == ptr) {
 66                         res = 1;
 67                         break;
 68                 }
 69         }
 70         fclose(maps_file);
 71         return res;
 72 }
 73 
 74 FIXTURE(vma) {
 75         void *ptr_anon, *ptr_not_anon;
 76 };
 77 
 78 FIXTURE_SETUP(vma) {
 79         self->ptr_anon = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE,
 80                                         MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 81         ASSERT_NE(self->ptr_anon, NULL);
 82         self->ptr_not_anon = mmap(NULL, AREA_SIZE, PROT_READ | PROT_WRITE,
 83                                         MAP_PRIVATE, 0, 0);
 84         ASSERT_NE(self->ptr_not_anon, NULL);
 85 }
 86 
 87 FIXTURE_TEARDOWN(vma) {
 88         munmap(self->ptr_anon, AREA_SIZE);
 89         munmap(self->ptr_not_anon, AREA_SIZE);
 90 }
 91 
 92 TEST_F(vma, renaming) {
 93         TH_LOG("Try to rename the VMA with correct parameters");
 94         EXPECT_GE(rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, GOOD_NAME), 0);
 95         EXPECT_TRUE(was_renaming_successful(GOOD_NAME, (unsigned long)self->ptr_anon));
 96 
 97         TH_LOG("Try to pass invalid name (with non-printable character \\1) to rename the VMA");
 98         EXPECT_EQ(rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, BAD_NAME), -EINVAL);
 99 
100         TH_LOG("Try to rename non-anonymous VMA");
101         EXPECT_EQ(rename_vma((unsigned long) self->ptr_not_anon, AREA_SIZE, GOOD_NAME), -EINVAL);
102 }
103 
104 TEST_HARNESS_MAIN
105 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php