11de7b4b8SPedro F. Giffuni /*- 2d78e98d2SNate Williams * privs.h - header for privileged operations 31de7b4b8SPedro F. Giffuni * 4*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 51de7b4b8SPedro F. Giffuni * 6b89321a5SAndrey A. Chernov * Copyright (C) 1993 Thomas Koenig 7d78e98d2SNate Williams * 8ddcf8022SAndrey A. Chernov * Redistribution and use in source and binary forms, with or without 9ddcf8022SAndrey A. Chernov * modification, are permitted provided that the following conditions 10ddcf8022SAndrey A. Chernov * are met: 11ddcf8022SAndrey A. Chernov * 1. Redistributions of source code must retain the above copyright 12ddcf8022SAndrey A. Chernov * notice, this list of conditions and the following disclaimer. 13ddcf8022SAndrey A. Chernov * 2. The name of the author(s) may not be used to endorse or promote 14ddcf8022SAndrey A. Chernov * products derived from this software without specific prior written 15ddcf8022SAndrey A. Chernov * permission. 16d78e98d2SNate Williams * 17ddcf8022SAndrey A. Chernov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18ddcf8022SAndrey A. Chernov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19ddcf8022SAndrey A. Chernov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20ddcf8022SAndrey A. Chernov * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21ddcf8022SAndrey A. Chernov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22ddcf8022SAndrey A. Chernov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23ddcf8022SAndrey A. Chernov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24ddcf8022SAndrey A. Chernov * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25ddcf8022SAndrey A. Chernov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26ddcf8022SAndrey A. Chernov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27d78e98d2SNate Williams */ 28d78e98d2SNate Williams 29d78e98d2SNate Williams #ifndef _PRIVS_H 30d78e98d2SNate Williams #define _PRIVS_H 31d78e98d2SNate Williams 32d78e98d2SNate Williams #include <unistd.h> 33d78e98d2SNate Williams 34b89321a5SAndrey A. Chernov /* Relinquish privileges temporarily for a setuid or setgid program 359dd887f1SRuslan Ermilov * with the option of getting them back later. This is done by 369dd887f1SRuslan Ermilov * utilizing POSIX saved user and group IDs. Call RELINQUISH_PRIVS once 3781c8c7a4SPhilippe Charnier * at the beginning of the main program. This will cause all operations 38d78e98d2SNate Williams * to be executed with the real userid. When you need the privileges 39b89321a5SAndrey A. Chernov * of the setuid/setgid invocation, call PRIV_START; when you no longer 40d78e98d2SNate Williams * need it, call PRIV_END. Note that it is an error to call PRIV_START 41d78e98d2SNate Williams * and not PRIV_END within the same function. 42d78e98d2SNate Williams * 43b89321a5SAndrey A. Chernov * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running 44d78e98d2SNate Williams * as root, and you want to drop back the effective userid to a 45d78e98d2SNate Williams * and the effective group id to b, with the option to get them back 46d78e98d2SNate Williams * later. 47d78e98d2SNate Williams * 48d78e98d2SNate Williams * If you no longer need root privileges, but those of some other 49b89321a5SAndrey A. Chernov * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective 50d78e98d2SNate Williams * is the user's. 51d78e98d2SNate Williams * 52d78e98d2SNate Williams * Problems: Do not use return between PRIV_START and PRIV_END; this 53d78e98d2SNate Williams * will cause the program to continue running in an unprivileged 54d78e98d2SNate Williams * state. 55d78e98d2SNate Williams * 56d78e98d2SNate Williams * It is NOT safe to call exec(), system() or popen() with a user- 57d78e98d2SNate Williams * supplied program (i.e. without carefully checking PATH and any 58d78e98d2SNate Williams * library load paths) with relinquished privileges; the called program 5981c8c7a4SPhilippe Charnier * can acquire them just as easily. Set both effective and real userid 60d78e98d2SNate Williams * to the real userid before calling any of them. 61d78e98d2SNate Williams */ 62d78e98d2SNate Williams 63bf70beceSEd Schouten extern uid_t real_uid, effective_uid; 64bf70beceSEd Schouten extern gid_t real_gid, effective_gid; 65a35353deSEd Schouten 66bf70beceSEd Schouten #ifdef MAIN 67bf70beceSEd Schouten uid_t real_uid, effective_uid; 68a35353deSEd Schouten gid_t real_gid, effective_gid; 69bf70beceSEd Schouten #endif 70b89321a5SAndrey A. Chernov 71d78e98d2SNate Williams #define RELINQUISH_PRIVS { \ 72d78e98d2SNate Williams real_uid = getuid(); \ 73d78e98d2SNate Williams effective_uid = geteuid(); \ 74b89321a5SAndrey A. Chernov real_gid = getgid(); \ 75b89321a5SAndrey A. Chernov effective_gid = getegid(); \ 761d1d4a47SEitan Adler if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \ 771d1d4a47SEitan Adler if (setegid(real_gid) != 0) err(1, "setegid failed"); \ 78d78e98d2SNate Williams } 79d78e98d2SNate Williams 80b89321a5SAndrey A. Chernov #define RELINQUISH_PRIVS_ROOT(a, b) { \ 81d78e98d2SNate Williams real_uid = (a); \ 82d78e98d2SNate Williams effective_uid = geteuid(); \ 83b89321a5SAndrey A. Chernov real_gid = (b); \ 84b89321a5SAndrey A. Chernov effective_gid = getegid(); \ 851d1d4a47SEitan Adler if (setegid(real_gid) != 0) err(1, "setegid failed"); \ 861d1d4a47SEitan Adler if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \ 87d78e98d2SNate Williams } 88d78e98d2SNate Williams 89d78e98d2SNate Williams #define PRIV_START { \ 901d1d4a47SEitan Adler if (seteuid(effective_uid) != 0) err(1, "seteuid failed"); \ 911d1d4a47SEitan Adler if (setegid(effective_gid) != 0) err(1, "setegid failed"); \ 929dd887f1SRuslan Ermilov } 93d78e98d2SNate Williams 949dd887f1SRuslan Ermilov #define PRIV_END { \ 951d1d4a47SEitan Adler if (setegid(real_gid) != 0) err(1, "setegid failed"); \ 961d1d4a47SEitan Adler if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \ 97d78e98d2SNate Williams } 98d78e98d2SNate Williams 99b89321a5SAndrey A. Chernov #define REDUCE_PRIV(a, b) { \ 1009dd887f1SRuslan Ermilov PRIV_START \ 101d78e98d2SNate Williams effective_uid = (a); \ 102b89321a5SAndrey A. Chernov effective_gid = (b); \ 1031d1d4a47SEitan Adler if (setregid((gid_t)-1, effective_gid) != 0) err(1, "setregid failed"); \ 104b9e6237dSPietro Cerutti if (setreuid((uid_t)-1, effective_uid) != 0) err(1, "setreuid failed"); \ 1059dd887f1SRuslan Ermilov PRIV_END \ 106d78e98d2SNate Williams } 107d78e98d2SNate Williams #endif 108