xref: /freebsd/tools/regression/mlock/mlock.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 
30 #include <err.h>
31 #include <errno.h>
32 #include <pwd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #define	NOBODY	"nobody"
38 
39 /*
40  * Simple exercise for the mlock() system call -- confirm that mlock() and
41  * munlock() return success on an anonymously mapped memory page when running
42  * with privilege; confirm that they fail with EPERM when running
43  * unprivileged.
44  */
45 int
46 main(int argc, char *argv[])
47 {
48 	struct passwd *pwd;
49 	int pagesize;
50 	char *page;
51 
52 	if (geteuid() != 0)
53 		errx(-1, "mlock must run as root");
54 
55 	errno = 0;
56 	pwd = getpwnam(NOBODY);
57 	if (pwd == NULL && errno == 0)
58 		errx(-1, "getpwnam: user \"%s\" not found", NOBODY);
59 	if (pwd == NULL)
60 		errx(-1, "getpwnam: %s", strerror(errno));
61 	if (pwd->pw_uid == 0)
62 		errx(-1, "getpwnam: user \"%s\" has uid 0", NOBODY);
63 
64 	pagesize = getpagesize();
65 	page = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
66 	if (page == MAP_FAILED)
67 		errx(-1, "mmap: %s", strerror(errno));
68 
69 	if (mlock(page, pagesize) < 0)
70 		errx(-1, "mlock privileged: %s", strerror(errno));
71 
72 	if (munlock(page, pagesize) < 0)
73 		errx(-1, "munlock privileged: %s", strerror(errno));
74 
75 	if (seteuid(pwd->pw_uid) < 0)
76 		errx(-1, "seteuid: %s", strerror(errno));
77 
78 	if (mlock(page, pagesize) == 0)
79 		errx(-1, "mlock unprivileged: succeeded but shouldn't have");
80 	if (errno != EPERM)
81 		errx(-1, "mlock unprivileged: %s", strerror(errno));
82 
83 	if (munlock(page, pagesize) == 0)
84 		errx(-1, "munlock unprivileged: succeeded but shouldn't have");
85 	if (errno != EPERM)
86 		errx(-1, "munlock unprivileged: %s", strerror(errno));
87 
88 	return (0);
89 }
90