1 /* $OpenBSD: t_mlock.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */
2 /* $NetBSD: t_mlock.c,v 1.8 2020/01/24 08:45:16 skrll Exp $ */
3
4 /*-
5 * Copyright (c) 2012 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "macros.h"
34
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
38 #include <sys/wait.h>
39
40 #include <errno.h>
41 #include "atf-c.h"
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 static long page = 0;
49
50 ATF_TC(mlock_clip);
ATF_TC_HEAD(mlock_clip,tc)51 ATF_TC_HEAD(mlock_clip, tc)
52 {
53 atf_tc_set_md_var(tc, "descr", "Test with mlock(2) that UVM only "
54 "clips if the clip address is within the entry (PR kern/44788)");
55 }
56
ATF_TC_BODY(mlock_clip,tc)57 ATF_TC_BODY(mlock_clip, tc)
58 {
59 void *buf;
60 int err1, err2;
61
62 buf = malloc(page);
63 ATF_REQUIRE(buf != NULL);
64 fprintf(stderr, "mlock_clip: buf = %p (page=%ld)\n", buf, page);
65
66 if (page < 1024)
67 atf_tc_skip("page size too small");
68
69 for (size_t i = page; i >= 1; i = i - 1024) {
70 err1 = mlock(buf, page - i);
71 if (err1 != 0)
72 fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
73 " mlock(%p, %ld): %s\n", page, i, buf, page - i,
74 strerror(errno));
75 err2 = munlock(buf, page - i);
76 if (err2 != 0)
77 fprintf(stderr, "mlock_clip: page=%ld i=%zu,"
78 " munlock(%p, %ld): %s (mlock %s)\n", page, i,
79 buf, page - i, strerror(errno), err1?"failed":"ok");
80 }
81
82 free(buf);
83 }
84
85 ATF_TC(mlock_err);
ATF_TC_HEAD(mlock_err,tc)86 ATF_TC_HEAD(mlock_err, tc)
87 {
88 atf_tc_set_md_var(tc, "descr",
89 "Test error conditions in mlock(2) and munlock(2)");
90 }
91
ATF_TC_BODY(mlock_err,tc)92 ATF_TC_BODY(mlock_err, tc)
93 {
94 void *invalid_ptr;
95 void *buf;
96 int mlock_err, munlock_err;
97
98 /*
99 * Any bad address must return ENOMEM (for lock & unlock)
100 */
101 errno = 0;
102 ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
103
104 errno = 0;
105 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
106
107 errno = 0;
108 #ifdef __OpenBSD__
109 ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
110 #else
111 ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
112 #endif
113
114 errno = 0;
115 ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
116
117 errno = 0;
118 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
119
120 errno = 0;
121 #ifdef __OpenBSD__
122 ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
123 #else
124 ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
125 #endif
126
127 buf = malloc(page);
128 ATF_REQUIRE(buf != NULL);
129 fprintf(stderr, "mlock_err: buf = %p (page=%ld)\n", buf, page);
130
131 /*
132 * unlocking memory that is not locked is an error...
133 */
134
135 #ifndef __OpenBSD__
136 errno = 0;
137 ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
138 #endif
139
140 /*
141 * These are permitted to fail (EINVAL) but do not on NetBSD
142 */
143 mlock_err = mlock((void *)(((uintptr_t)buf) + page/3), page/5);
144 if (mlock_err != 0)
145 fprintf(stderr, "mlock_err: mlock(%p, %ld): %d [%d] %s\n",
146 (void *)(((uintptr_t)buf) + page/3), page/5, mlock_err,
147 errno, strerror(errno));
148 ATF_REQUIRE(mlock_err == 0);
149 munlock_err= munlock((void *)(((uintptr_t)buf) + page/3), page/5);
150 if (munlock_err != 0)
151 fprintf(stderr, "mlock_err: munlock(%p, %ld): %d [%d] %s\n",
152 (void *)(((uintptr_t)buf) + page/3), page/5, munlock_err,
153 errno, strerror(errno));
154 ATF_REQUIRE(munlock_err == 0);
155
156 (void)free(buf);
157
158 /*
159 * Try to create a pointer to an unmapped page - first after current
160 * brk will likely do.
161 */
162 invalid_ptr = (void*)(((uintptr_t)sbrk(0)+page) & ~(page-1));
163 printf("testing with (hopefully) invalid pointer %p\n", invalid_ptr);
164
165 errno = 0;
166 ATF_REQUIRE_ERRNO(ENOMEM, mlock(invalid_ptr, page) == -1);
167
168 errno = 0;
169 ATF_REQUIRE_ERRNO(ENOMEM, munlock(invalid_ptr, page) == -1);
170 }
171
172 ATF_TC(mlock_limits);
ATF_TC_HEAD(mlock_limits,tc)173 ATF_TC_HEAD(mlock_limits, tc)
174 {
175 atf_tc_set_md_var(tc, "descr", "Test system limits with mlock(2)");
176 }
177
ATF_TC_BODY(mlock_limits,tc)178 ATF_TC_BODY(mlock_limits, tc)
179 {
180 struct rlimit res;
181 void *buf;
182 pid_t pid;
183 int sta;
184
185 buf = malloc(page);
186 ATF_REQUIRE(buf != NULL);
187 fprintf(stderr, "mlock_limits: buf = %p (page=%ld)\n", buf, page);
188
189 pid = fork();
190 ATF_REQUIRE(pid >= 0);
191
192 if (pid == 0) {
193
194 for (ssize_t i = page; i >= 2; i -= 100) {
195
196 res.rlim_cur = i - 1;
197 res.rlim_max = i - 1;
198
199 (void)fprintf(stderr, "trying to lock %zu bytes "
200 "with %zu byte limit\n", i, (size_t)res.rlim_cur);
201
202 if (setrlimit(RLIMIT_MEMLOCK, &res) != 0)
203 _exit(EXIT_FAILURE);
204
205 errno = 0;
206
207 if ((sta = mlock(buf, i)) != -1 || errno != EAGAIN) {
208 fprintf(stderr, "mlock(%p, %zu): %d [%d] %s\n",
209 buf, i, sta, errno, strerror(errno));
210 (void)munlock(buf, i);
211 _exit(EXIT_FAILURE);
212 }
213 }
214
215 _exit(EXIT_SUCCESS);
216 }
217
218 (void)wait(&sta);
219
220 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
221 atf_tc_fail("mlock(2) locked beyond system limits");
222
223 free(buf);
224 }
225
226 ATF_TC(mlock_mmap);
ATF_TC_HEAD(mlock_mmap,tc)227 ATF_TC_HEAD(mlock_mmap, tc)
228 {
229 atf_tc_set_md_var(tc, "descr", "Test mlock(2)-mmap(2) interaction");
230 }
231
ATF_TC_BODY(mlock_mmap,tc)232 ATF_TC_BODY(mlock_mmap, tc)
233 {
234 #ifdef __OpenBSD__
235 static const int flags = MAP_ANON | MAP_PRIVATE;
236 #else
237 static const int flags = MAP_ANON | MAP_PRIVATE | MAP_WIRED;
238 #endif
239 void *buf;
240
241 /*
242 * Make a wired RW mapping and check that mlock(2)
243 * does not fail for the (already locked) mapping.
244 */
245 buf = mmap(NULL, page, PROT_READ | PROT_WRITE, flags, -1, 0);
246
247 if (buf == MAP_FAILED)
248 fprintf(stderr,
249 "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
250 " [%d] %s\n", page, PROT_READ | PROT_WRITE, flags, errno,
251 strerror(errno));
252
253 ATF_REQUIRE(buf != MAP_FAILED);
254
255 fprintf(stderr, "mlock_mmap: buf=%p, page=%ld\n", buf, page);
256
257 ATF_REQUIRE(mlock(buf, page) == 0);
258 ATF_REQUIRE(munlock(buf, page) == 0);
259 ATF_REQUIRE(munmap(buf, page) == 0);
260 ATF_REQUIRE(munlock(buf, page) != 0);
261
262 fprintf(stderr, "mlock_mmap: first test succeeded\n");
263
264 /*
265 * But it should be impossible to mlock(2) a PROT_NONE mapping.
266 */
267 buf = mmap(NULL, page, PROT_NONE, flags, -1, 0);
268
269 if (buf == MAP_FAILED)
270 fprintf(stderr,
271 "mlock_mmap: mmap(NULL, %ld, %#x, %#x, -1, 0): MAP_FAILED"
272 " [%d] %s\n", page, PROT_NONE, flags, errno,
273 strerror(errno));
274
275 ATF_REQUIRE(buf != MAP_FAILED);
276 ATF_REQUIRE(mlock(buf, page) != 0);
277 ATF_REQUIRE(munmap(buf, page) == 0);
278
279 fprintf(stderr, "mlock_mmap: second test succeeded\n");
280 }
281
282 ATF_TC(mlock_nested);
ATF_TC_HEAD(mlock_nested,tc)283 ATF_TC_HEAD(mlock_nested, tc)
284 {
285 atf_tc_set_md_var(tc, "descr",
286 "Test that consecutive mlock(2) calls succeed");
287 }
288
ATF_TC_BODY(mlock_nested,tc)289 ATF_TC_BODY(mlock_nested, tc)
290 {
291 const size_t maxiter = 100;
292 void *buf;
293 int err;
294
295 buf = malloc(page);
296 ATF_REQUIRE(buf != NULL);
297 fprintf(stderr, "mlock_nested: buf = %p (page=%ld)\n", buf, page);
298
299 for (size_t i = 0; i < maxiter; i++) {
300 err = mlock(buf, page);
301 if (err != 0)
302 fprintf(stderr,
303 "mlock_nested: i=%zu (of %zu) mlock(%p, %ld): %d [%d] %s\n",
304 i, maxiter, buf, page, err, errno, strerror(errno));
305 ATF_REQUIRE(err == 0);
306 }
307
308 err = munlock(buf, page);
309 if (err != 0)
310 fprintf(stderr, "mlock_nested: munlock(%p, %ld): %d [%d] %s\n",
311 buf, page, err, errno, strerror(errno));
312 ATF_REQUIRE(err == 0);
313 free(buf);
314 }
315
ATF_TP_ADD_TCS(tp)316 ATF_TP_ADD_TCS(tp)
317 {
318
319 page = sysconf(_SC_PAGESIZE);
320 ATF_REQUIRE(page >= 0);
321
322 ATF_TP_ADD_TC(tp, mlock_clip);
323 ATF_TP_ADD_TC(tp, mlock_err);
324 ATF_TP_ADD_TC(tp, mlock_limits);
325 ATF_TP_ADD_TC(tp, mlock_mmap);
326 ATF_TP_ADD_TC(tp, mlock_nested);
327
328 return atf_no_error();
329 }
330