2 * Copyright (c) 2006 nCircle Network Security, Inc.
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project under contract to nCircle Network Security, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 * Test that raising current resource limits above hard resource limits
34 * requires privilege. There is one privilege check, but two conditions:
36 * - To raise the current above the maximum.
38 * - To raise the maximum.
41 #include <sys/types.h>
43 #include <sys/resource.h>
52 priv_proc_setrlimit(void)
54 struct rlimit rl, rl_lower, rl_raise_max, rl_raise_cur;
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.
67 if (getrlimit(RLIMIT_DATA, &rl) < 0)
68 err(-1, "getrlimit(RLIMIT_DATA)");
71 * What to lower to before trying to raise.
74 rl_lower.rlim_cur -= 10;
75 rl_lower.rlim_max = rl_lower.rlim_cur;
81 rl_raise_max.rlim_max += 10;
84 * Raise the current above the maximum.
87 rl_raise_cur.rlim_cur += 10;
90 * Test raising the maximum with privilege.
92 if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
93 err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
95 if (setrlimit(RLIMIT_DATA, &rl_raise_max) < 0)
96 err(-1, "setrlimit(RLIMIT_DATA, raise_max) as root");
99 * Test raising the current above the maximum with privilege.
101 if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
102 err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
104 if (setrlimit(RLIMIT_DATA, &rl_raise_cur) < 0)
105 err(-1, "setrlimit(RLIMIT_DATA, raise_cur) as root");
108 * Test raising the maximum without privilege.
110 if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
111 err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
114 error = setrlimit(RLIMIT_DATA, &rl_raise_max);
117 "setrlimit(RLIMIT_DATA, raise_max) succeeded as !root");
119 err(-1, "setrlimit(RLIMIT_DATA, raise_max) wrong errno %d "
123 * Test raising the current above the maximum without privilege.
126 if (setrlimit(RLIMIT_DATA, &rl_lower) < 0)
127 err(-1, "setrlimit(RLIMIT_DATA, lower) as root");
130 error = setrlimit(RLIMIT_DATA, &rl_raise_cur);
133 "setrlimit(RLIMIT_DATA, raise_cur) succeeded as !root");
135 err(-1, "setrlimit(RLIMIT_DATA, raise_cur) wrong errno %d "