1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-License-Identifier: GPL-2.0 or later
3 /*
4 * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
5 * Email : code@zilogic.com
6 */
7
8 /*
9 * test cases for madvise(2) system call, advise value as "MADV_WIPEONFORK".
10 *
11 * DESCRIPTION
12 * Present the child process with zero-filled memory in this
13 * range after a fork(2).
14 * The MADV_WIPEONFORK operation can be applied only to
15 * private anonymous pages.
16 * Within the child created by fork(2), the MADV_WIPEONFORK
17 * setting remains in place on the specified map_address range.
18 * The MADV_KEEPONFORK operation undo the effect of MADV_WIPEONFORK.
19 *
20 * Test-Case 1 : madvise with "MADV_WIPEONFORK"
21 * flow : Map memory area as private anonymous page.
22 * Mark memory area as wipe-on-fork.
23 * On fork, child process memory should be zeroed.
24 *
25 * Test-Case 2 : madvise with "MADV_WIPEONFORK" and "ZERO" length
26 * flow : Map memory area as private anonymous page.
27 * Mark memory area as wipe-on-fork, with length zero.
28 * On fork, child process memory should be accessible.
29 *
30 * Test-Case 3 : "MADV_WIPEONFORK" on Grand child
31 * flow : Map memory area as private anonymous.
32 * Mark memory areas as wipe-on-fork.
33 * On fork, child process memory should be zeroed.
34 * In child, fork to create grand-child,
35 * memory should be zeroed.
36 *
37 * Test-Case 4 : Undo "MADV_WIPEONFORK" by "MADV_KEEPONFORK"
38 * flow : Map memory area as private anonymous page.
39 * Mark memory area as wipe-on-fork.
40 * Mark memory area as keep-on-fork.
41 * On fork, child process memory should be retained.
42 **/
43
44 #include <stdio.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48
49 #include "lapi/mmap.h"
50 #include "tst_test.h"
51 #include "tst_safe_macros.h"
52
53 #define MAP_SIZE (16 * 1024)
54
55 static char pattern[MAP_SIZE];
56 static char zero[MAP_SIZE];
57
58 static const struct test_case {
59 int size;
60 int advise1;
61 int advise2;
62 char *exp;
63 int grand_child;
64 const char *desc;
65 } tcases[] = {
66 {MAP_SIZE, MADV_NORMAL, MADV_WIPEONFORK, zero, 0,
67 "MADV_WIPEONFORK zeroes memory in child"},
68 {0, MADV_NORMAL, MADV_WIPEONFORK, pattern, 0,
69 "MADV_WIPEONFORK with zero length does nothing"},
70 {MAP_SIZE, MADV_NORMAL, MADV_WIPEONFORK, zero, 1,
71 "MADV_WIPEONFORK zeroes memory in grand-child"},
72 {MAP_SIZE, MADV_WIPEONFORK, MADV_KEEPONFORK, pattern, 0,
73 "MADV_KEEPONFORK will undo MADV_WIPEONFORK"},
74 };
75
cmp_area(char * addr,const struct test_case * tc)76 static void cmp_area(char *addr, const struct test_case *tc)
77 {
78 int i;
79
80 for (i = 0; i < tc->size; i++) {
81 if (addr[i] != tc->exp[i]) {
82 tst_res(TFAIL, "In PID %d, addr[%d] = 0x%02x, "
83 "expected[%d] = 0x%02x", getpid(),
84 i, addr[i], i, tc->exp[i]);
85 break;
86 }
87 }
88
89 tst_res(TPASS, "In PID %d, Matched expected pattern", getpid());
90 }
91
set_advice(char * addr,int size,int advise)92 static int set_advice(char *addr, int size, int advise)
93 {
94 TEST(madvise(addr, size, advise));
95
96 if (TST_RET == -1) {
97 if (TST_ERR == EINVAL) {
98 tst_res(TCONF, "madvise(%p, %d, 0x%x) is not supported",
99 addr, size, advise);
100 } else {
101 tst_res(TFAIL | TTERRNO, "madvise(%p, %d, 0x%x)",
102 addr, size, advise);
103 }
104
105 return 1;
106 }
107
108 tst_res(TPASS, "madvise(%p, %d, 0x%x)", addr, size, advise);
109 return 0;
110 }
111
mem_map(void)112 static char *mem_map(void)
113 {
114 char *ptr;
115
116 ptr = SAFE_MMAP(NULL, MAP_SIZE,
117 PROT_READ | PROT_WRITE,
118 MAP_PRIVATE | MAP_ANONYMOUS,
119 -1, 0);
120
121 memcpy(ptr, pattern, MAP_SIZE);
122
123 return ptr;
124 }
125
test_madvise(unsigned int test_nr)126 static void test_madvise(unsigned int test_nr)
127 {
128 const struct test_case *tc = &tcases[test_nr];
129 char *addr;
130 pid_t pid;
131
132 addr = mem_map();
133
134 tst_res(TINFO, "%s", tc->desc);
135 if (set_advice(addr, tc->size, tc->advise1))
136 goto un_map;
137
138 if (!set_advice(addr, tc->size, tc->advise2)) {
139 pid = SAFE_FORK();
140
141 if (!pid) {
142 if (tc->grand_child) {
143 pid = SAFE_FORK();
144
145 if (!pid) {
146 cmp_area(addr, tc);
147 exit(0);
148 }
149 } else {
150 cmp_area(addr, tc);
151 exit(0);
152 }
153 }
154 tst_reap_children();
155 }
156
157 un_map:
158 SAFE_MUNMAP(addr, MAP_SIZE);
159 }
160
setup(void)161 static void setup(void)
162 {
163 unsigned int i;
164
165 for (i = 0; i < MAP_SIZE; i++)
166 pattern[i] = i % 0x03;
167 }
168
169 static struct tst_test test = {
170 .tcnt = ARRAY_SIZE(tcases),
171 .forks_child = 1,
172 .test = test_madvise,
173 .setup = setup,
174 };
175