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