priv_proc_setrlimit.c revision 162271
1/*-
2 * Copyright (c) 2006 nCircle Network Security, Inc.
3 * All rights reserved.
4 *
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project under contract to nCircle Network Security, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD: head/tools/regression/priv/priv_proc_setrlimit.c 162271 2006-09-13 09:05:39Z rwatson $
30 */
31
32/*
33 * Test that raising current resource limits above hard resource limits
34 * requires privilege.  There is one privilege check, but two conditions:
35 *
36 * - To raise the current above the maximum.
37 *
38 * - To raise the maximum.
39 */
40
41#include <sys/types.h>
42#include <sys/time.h>
43#include <sys/resource.h>
44
45#include <err.h>
46#include <errno.h>
47#include <unistd.h>
48
49#include "main.h"
50
51void
52priv_proc_setrlimit(void)
53{
54	struct rlimit rl, rl_lower, rl_raise_max, rl_raise_cur;
55	int error;
56
57	assert_root();
58
59	/*
60	 * To make sure that there is room to raise the resource limit, we
61	 * must first lower it.  Otherwise, if the resource limit is already
62	 * at the global maximum, that complicates matters.  In principle, we
63	 * can bump into privilege failures during setup, but there's not
64	 * much we can do about that.  Keep this prototypical setting around
65	 * as the target to restore to later.
66	 */
67	if (getrlimit(RLIMIT_DATA, &rl) < 0)
68		err(-1, "getrlimit(RLIMIT_DATA)");
69
70	/*
71	 * What to lower to before trying to raise.
72	 */
73	rl_lower = rl;
74	rl_lower.rlim_cur -= 10;
75	rl_lower.rlim_max = rl_lower.rlim_cur;
76
77	/*
78	 * Raise the maximum.
79	 */
80	rl_raise_max = rl;
81	rl_raise_max.rlim_max += 10;
82
83	/*
84	 * Raise the current above the maximum.
85	 */
86	rl_raise_cur = rl;
87	rl_raise_cur.rlim_cur += 10;
88
89	/*
90	 * Test raising the maximum with privilege.
91	 */
92	if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
93		err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
94
95	if (setrlimit(RLIMIT_DATA, &rl_raise_max) < 0)
96		err(-1, "setrlimit(RLIMIT_DATA, raise_max) as root");
97
98	/*
99	 * Test raising the current above the maximum with privilege.
100	 */
101	if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
102		err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
103
104	if (setrlimit(RLIMIT_DATA, &rl_raise_cur) < 0)
105		err(-1, "setrlimit(RLIMIT_DATA, raise_cur) as root");
106
107	/*
108	 * Test raising the maximum without privilege.
109	 */
110	if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
111		err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
112
113	set_euid(UID_OTHER);
114	error = setrlimit(RLIMIT_DATA, &rl_raise_max);
115	if (error == 0)
116		errx(-1,
117		    "setrlimit(RLIMIT_DATA, raise_max) succeeded as !root");
118	if (errno != EPERM)
119		err(-1, "setrlimit(RLIMIT_DATA, raise_max) wrong errno %d "
120		    "as !root", errno);
121
122	/*
123	 * Test raising the current above the maximum without privilege.
124	 */
125	set_euid(UID_ROOT);
126	if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
127		err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
128	set_euid(UID_OTHER);
129
130	error = setrlimit(RLIMIT_DATA, &rl_raise_cur);
131	if (error == 0)
132		errx(-1,
133		    "setrlimit(RLIMIT_DATA, raise_cur) succeeded as !root");
134	if (errno != EPERM)
135		err(-1, "setrlimit(RLIMIT_DATA, raise_cur) wrong errno %d "
136		    "as !root", errno);
137}
138