19fa5f6b4SRobert Watson /*-
29fa5f6b4SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc.
3d903306aSRobert Watson * Copyright (c) 2007 Robert N. M. Watson
49fa5f6b4SRobert Watson * All rights reserved.
59fa5f6b4SRobert Watson *
69fa5f6b4SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD
79fa5f6b4SRobert Watson * Project under contract to nCircle Network Security, Inc.
89fa5f6b4SRobert Watson *
99fa5f6b4SRobert Watson * Redistribution and use in source and binary forms, with or without
109fa5f6b4SRobert Watson * modification, are permitted provided that the following conditions
119fa5f6b4SRobert Watson * are met:
129fa5f6b4SRobert Watson * 1. Redistributions of source code must retain the above copyright
139fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer.
149fa5f6b4SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
159fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer in the
169fa5f6b4SRobert Watson * documentation and/or other materials provided with the distribution.
179fa5f6b4SRobert Watson *
189fa5f6b4SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199fa5f6b4SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209fa5f6b4SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219fa5f6b4SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
229fa5f6b4SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239fa5f6b4SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
249fa5f6b4SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
259fa5f6b4SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
269fa5f6b4SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
279fa5f6b4SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
289fa5f6b4SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299fa5f6b4SRobert Watson */
309fa5f6b4SRobert Watson
31d903306aSRobert Watson /*-
329fa5f6b4SRobert Watson * Test that raising current resource limits above hard resource limits
33d903306aSRobert Watson * requires privilege. We test three cases:
349fa5f6b4SRobert Watson *
35d903306aSRobert Watson * - Raise the current above the maximum (privileged).
36d903306aSRobert Watson * - Raise the current to the maximum (unprivileged).
37d903306aSRobert Watson * - Raise the maximum (privileged).
389fa5f6b4SRobert Watson */
399fa5f6b4SRobert Watson
409fa5f6b4SRobert Watson #include <sys/types.h>
419fa5f6b4SRobert Watson #include <sys/time.h>
429fa5f6b4SRobert Watson #include <sys/resource.h>
439fa5f6b4SRobert Watson
449fa5f6b4SRobert Watson #include <err.h>
459fa5f6b4SRobert Watson #include <errno.h>
469fa5f6b4SRobert Watson #include <unistd.h>
479fa5f6b4SRobert Watson
489fa5f6b4SRobert Watson #include "main.h"
499fa5f6b4SRobert Watson
50d903306aSRobert Watson static int initialized;
51d903306aSRobert Watson static struct rlimit rl_base;
52d903306aSRobert Watson static struct rlimit rl_lowered;
53d903306aSRobert Watson
54d903306aSRobert Watson int
priv_proc_setrlimit_setup(int asroot,int injail,struct test * test)55d903306aSRobert Watson priv_proc_setrlimit_setup(int asroot, int injail, struct test *test)
569fa5f6b4SRobert Watson {
57d903306aSRobert Watson
58d903306aSRobert Watson if (getrlimit(RLIMIT_DATA, &rl_base) < 0) {
59d903306aSRobert Watson warn("priv_proc_setrlimit_setup: getrlimit");
60d903306aSRobert Watson return (-1);
61d903306aSRobert Watson }
62d903306aSRobert Watson
63d903306aSRobert Watson /*
64d903306aSRobert Watson * Must lower current and limit to make sure there's room to try to
65d903306aSRobert Watson * raise them during tests. Set current lower than max so we can
66d903306aSRobert Watson * raise it later also.
67d903306aSRobert Watson */
68d903306aSRobert Watson rl_lowered = rl_base;
69d903306aSRobert Watson rl_lowered.rlim_cur -= 20;
70d903306aSRobert Watson rl_lowered.rlim_max -= 10;
71d903306aSRobert Watson if (setrlimit(RLIMIT_DATA, &rl_lowered) < 0) {
72d903306aSRobert Watson warn("priv_proc_setrlimit_setup: setrlimit");
73d903306aSRobert Watson return (-1);
74d903306aSRobert Watson }
75d903306aSRobert Watson initialized = 1;
76d903306aSRobert Watson return (0);
77d903306aSRobert Watson }
78d903306aSRobert Watson
79d903306aSRobert Watson /*
80d903306aSRobert Watson * Try increasing the maximum limits on the process, which requires
81d903306aSRobert Watson * privilege.
82d903306aSRobert Watson */
83d903306aSRobert Watson void
priv_proc_setrlimit_raisemax(int asroot,int injail,struct test * test)84d903306aSRobert Watson priv_proc_setrlimit_raisemax(int asroot, int injail, struct test *test)
85d903306aSRobert Watson {
86d903306aSRobert Watson struct rlimit rl;
879fa5f6b4SRobert Watson int error;
889fa5f6b4SRobert Watson
89d903306aSRobert Watson rl = rl_lowered;
90d903306aSRobert Watson rl.rlim_max = rl_base.rlim_max;
91d903306aSRobert Watson error = setrlimit(RLIMIT_DATA, &rl);
92d903306aSRobert Watson if (asroot && injail)
93d903306aSRobert Watson expect("priv_proc_setrlimit_raisemax(asroot, injail)", error,
94d903306aSRobert Watson 0, 0);
95d903306aSRobert Watson if (asroot && !injail)
96d903306aSRobert Watson expect("priv_proc_setrlimit_raisemax(asroot, !injail)",
97d903306aSRobert Watson error, 0, 0);
98d903306aSRobert Watson if (!asroot && injail)
99d903306aSRobert Watson expect("priv_proc_setrlimit_raisemax(!asroot, injail)",
100d903306aSRobert Watson error, -1, EPERM);
101d903306aSRobert Watson if (!asroot && !injail)
102d903306aSRobert Watson expect("priv_proc_setrlimit_raisemax(!asroot, !injail)",
103d903306aSRobert Watson error, -1, EPERM);
104d903306aSRobert Watson }
1059fa5f6b4SRobert Watson
1069fa5f6b4SRobert Watson /*
107d903306aSRobert Watson * Try setting the current limit to the current maximum, which is allowed
108d903306aSRobert Watson * without privilege.
1099fa5f6b4SRobert Watson */
110d903306aSRobert Watson void
priv_proc_setrlimit_raisecur_nopriv(int asroot,int injail,struct test * test)111d903306aSRobert Watson priv_proc_setrlimit_raisecur_nopriv(int asroot, int injail,
112d903306aSRobert Watson struct test *test)
113d903306aSRobert Watson {
114d903306aSRobert Watson struct rlimit rl;
115d903306aSRobert Watson int error;
116d903306aSRobert Watson
117d903306aSRobert Watson rl = rl_lowered;
118d903306aSRobert Watson rl.rlim_cur = rl.rlim_max;
119d903306aSRobert Watson error = setrlimit(RLIMIT_DATA, &rl);
120d903306aSRobert Watson if (asroot && injail)
121d903306aSRobert Watson expect("priv_proc_setrlimit_raiscur_nopriv(asroot, injail)",
122d903306aSRobert Watson error, 0, 0);
123d903306aSRobert Watson if (asroot && !injail)
124d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur_nopriv(asroot, !injail)",
125d903306aSRobert Watson error, 0, 0);
126d903306aSRobert Watson if (!asroot && injail)
127d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur_nopriv(!asroot, injail)",
128d903306aSRobert Watson error, 0, 0);
129d903306aSRobert Watson if (!asroot && !injail)
130d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur_nopriv(!asroot, !injail)",
131d903306aSRobert Watson error, 0, 0);
132d903306aSRobert Watson }
1339fa5f6b4SRobert Watson
1349fa5f6b4SRobert Watson /*
135d903306aSRobert Watson * Try raising the current limits above the maximum, which requires
136d903306aSRobert Watson * privilege.
1379fa5f6b4SRobert Watson */
138d903306aSRobert Watson void
priv_proc_setrlimit_raisecur(int asroot,int injail,struct test * test)139d903306aSRobert Watson priv_proc_setrlimit_raisecur(int asroot, int injail, struct test *test)
140d903306aSRobert Watson {
141d903306aSRobert Watson struct rlimit rl;
142d903306aSRobert Watson int error;
1439fa5f6b4SRobert Watson
144d903306aSRobert Watson rl = rl_lowered;
145d903306aSRobert Watson rl.rlim_cur = rl.rlim_max + 10;
146d903306aSRobert Watson error = setrlimit(RLIMIT_DATA, &rl);
147d903306aSRobert Watson if (asroot && injail)
148d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur(asroot, injail)", error,
149d903306aSRobert Watson 0, 0);
150d903306aSRobert Watson if (asroot && !injail)
151d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur(asroot, !injail)",
152d903306aSRobert Watson error, 0, 0);
153d903306aSRobert Watson if (!asroot && injail)
154d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur(!asroot, injail)",
155d903306aSRobert Watson error, -1, EPERM);
156d903306aSRobert Watson if (!asroot && !injail)
157d903306aSRobert Watson expect("priv_proc_setrlimit_raisecur(!asroot, !injail)",
158d903306aSRobert Watson error, -1, EPERM);
159d903306aSRobert Watson }
1609fa5f6b4SRobert Watson
161d903306aSRobert Watson void
priv_proc_setrlimit_cleanup(int asroot,int injail,struct test * test)162d903306aSRobert Watson priv_proc_setrlimit_cleanup(int asroot, int injail, struct test *test)
163d903306aSRobert Watson {
1649fa5f6b4SRobert Watson
165d903306aSRobert Watson if (initialized)
166d903306aSRobert Watson (void)setrlimit(RLIMIT_DATA, &rl_base);
167d903306aSRobert Watson initialized = 0;
1689fa5f6b4SRobert Watson }
169