19fa5f6b4SRobert Watson /*- 29fa5f6b4SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc. 39fa5f6b4SRobert Watson * All rights reserved. 49fa5f6b4SRobert Watson * 59fa5f6b4SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD 69fa5f6b4SRobert Watson * Project under contract to nCircle Network Security, Inc. 79fa5f6b4SRobert Watson * 89fa5f6b4SRobert Watson * Redistribution and use in source and binary forms, with or without 99fa5f6b4SRobert Watson * modification, are permitted provided that the following conditions 109fa5f6b4SRobert Watson * are met: 119fa5f6b4SRobert Watson * 1. Redistributions of source code must retain the above copyright 129fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer. 139fa5f6b4SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 149fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer in the 159fa5f6b4SRobert Watson * documentation and/or other materials provided with the distribution. 169fa5f6b4SRobert Watson * 179fa5f6b4SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 189fa5f6b4SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199fa5f6b4SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209fa5f6b4SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY, 219fa5f6b4SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 229fa5f6b4SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 239fa5f6b4SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 249fa5f6b4SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 259fa5f6b4SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 269fa5f6b4SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 279fa5f6b4SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 289fa5f6b4SRobert Watson * 299fa5f6b4SRobert Watson * $FreeBSD$ 309fa5f6b4SRobert Watson */ 319fa5f6b4SRobert Watson 329fa5f6b4SRobert Watson /* 339fa5f6b4SRobert Watson * Test that raising current resource limits above hard resource limits 349fa5f6b4SRobert Watson * requires privilege. There is one privilege check, but two conditions: 359fa5f6b4SRobert Watson * 369fa5f6b4SRobert Watson * - To raise the current above the maximum. 379fa5f6b4SRobert Watson * 389fa5f6b4SRobert Watson * - To raise the maximum. 399fa5f6b4SRobert Watson */ 409fa5f6b4SRobert Watson 419fa5f6b4SRobert Watson #include <sys/types.h> 429fa5f6b4SRobert Watson #include <sys/time.h> 439fa5f6b4SRobert Watson #include <sys/resource.h> 449fa5f6b4SRobert Watson 459fa5f6b4SRobert Watson #include <err.h> 469fa5f6b4SRobert Watson #include <errno.h> 479fa5f6b4SRobert Watson #include <unistd.h> 489fa5f6b4SRobert Watson 499fa5f6b4SRobert Watson #include "main.h" 509fa5f6b4SRobert Watson 519fa5f6b4SRobert Watson void 529fa5f6b4SRobert Watson priv_proc_setrlimit(void) 539fa5f6b4SRobert Watson { 549fa5f6b4SRobert Watson struct rlimit rl, rl_lower, rl_raise_max, rl_raise_cur; 559fa5f6b4SRobert Watson int error; 569fa5f6b4SRobert Watson 579fa5f6b4SRobert Watson assert_root(); 589fa5f6b4SRobert Watson 599fa5f6b4SRobert Watson /* 609fa5f6b4SRobert Watson * To make sure that there is room to raise the resource limit, we 619fa5f6b4SRobert Watson * must first lower it. Otherwise, if the resource limit is already 629fa5f6b4SRobert Watson * at the global maximum, that complicates matters. In principle, we 639fa5f6b4SRobert Watson * can bump into privilege failures during setup, but there's not 649fa5f6b4SRobert Watson * much we can do about that. Keep this prototypical setting around 659fa5f6b4SRobert Watson * as the target to restore to later. 669fa5f6b4SRobert Watson */ 679fa5f6b4SRobert Watson if (getrlimit(RLIMIT_DATA, &rl) < 0) 689fa5f6b4SRobert Watson err(-1, "getrlimit(RLIMIT_DATA)"); 699fa5f6b4SRobert Watson 709fa5f6b4SRobert Watson /* 719fa5f6b4SRobert Watson * What to lower to before trying to raise. 729fa5f6b4SRobert Watson */ 739fa5f6b4SRobert Watson rl_lower = rl; 749fa5f6b4SRobert Watson rl_lower.rlim_cur -= 10; 759fa5f6b4SRobert Watson rl_lower.rlim_max = rl_lower.rlim_cur; 769fa5f6b4SRobert Watson 779fa5f6b4SRobert Watson /* 789fa5f6b4SRobert Watson * Raise the maximum. 799fa5f6b4SRobert Watson */ 809fa5f6b4SRobert Watson rl_raise_max = rl; 819fa5f6b4SRobert Watson rl_raise_max.rlim_max += 10; 829fa5f6b4SRobert Watson 839fa5f6b4SRobert Watson /* 849fa5f6b4SRobert Watson * Raise the current above the maximum. 859fa5f6b4SRobert Watson */ 869fa5f6b4SRobert Watson rl_raise_cur = rl; 879fa5f6b4SRobert Watson rl_raise_cur.rlim_cur += 10; 889fa5f6b4SRobert Watson 899fa5f6b4SRobert Watson /* 909fa5f6b4SRobert Watson * Test raising the maximum with privilege. 919fa5f6b4SRobert Watson */ 929fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_lower) < 0) 939fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, lower) as root"); 949fa5f6b4SRobert Watson 959fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_raise_max) < 0) 969fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, raise_max) as root"); 979fa5f6b4SRobert Watson 989fa5f6b4SRobert Watson /* 999fa5f6b4SRobert Watson * Test raising the current above the maximum with privilege. 1009fa5f6b4SRobert Watson */ 1019fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_lower) < 0) 1029fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, lower) as root"); 1039fa5f6b4SRobert Watson 1049fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_raise_cur) < 0) 1059fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, raise_cur) as root"); 1069fa5f6b4SRobert Watson 1079fa5f6b4SRobert Watson /* 1089fa5f6b4SRobert Watson * Test raising the maximum without privilege. 1099fa5f6b4SRobert Watson */ 1109fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_lower) < 0) 1119fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, lower) as root"); 1129fa5f6b4SRobert Watson 1139fa5f6b4SRobert Watson set_euid(UID_OTHER); 1149fa5f6b4SRobert Watson error = setrlimit(RLIMIT_DATA, &rl_raise_max); 1159fa5f6b4SRobert Watson if (error == 0) 1169fa5f6b4SRobert Watson errx(-1, 1179fa5f6b4SRobert Watson "setrlimit(RLIMIT_DATA, raise_max) succeeded as !root"); 1189fa5f6b4SRobert Watson if (errno != EPERM) 1199fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, raise_max) wrong errno %d " 1209fa5f6b4SRobert Watson "as !root", errno); 1219fa5f6b4SRobert Watson 1229fa5f6b4SRobert Watson /* 1239fa5f6b4SRobert Watson * Test raising the current above the maximum without privilege. 1249fa5f6b4SRobert Watson */ 1259fa5f6b4SRobert Watson set_euid(UID_ROOT); 1269fa5f6b4SRobert Watson if (setrlimit(RLIMIT_DATA, &rl_lower) < 0) 1279fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, lower) as root"); 1289fa5f6b4SRobert Watson set_euid(UID_OTHER); 1299fa5f6b4SRobert Watson 1309fa5f6b4SRobert Watson error = setrlimit(RLIMIT_DATA, &rl_raise_cur); 1319fa5f6b4SRobert Watson if (error == 0) 1329fa5f6b4SRobert Watson errx(-1, 1339fa5f6b4SRobert Watson "setrlimit(RLIMIT_DATA, raise_cur) succeeded as !root"); 1349fa5f6b4SRobert Watson if (errno != EPERM) 1359fa5f6b4SRobert Watson err(-1, "setrlimit(RLIMIT_DATA, raise_cur) wrong errno %d " 1369fa5f6b4SRobert Watson "as !root", errno); 1379fa5f6b4SRobert Watson } 138