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