os.c revision 9935447b51456f598b45246d0114b8006049244d
7d98a1783f222964bcde7d56dab77b822706204dBob Halley * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * Copyright (C) 1999-2002 Internet Software Consortium.
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * Permission to use, copy, modify, and/or distribute this software for any
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * purpose with or without fee is hereby granted, provided that the above
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * copyright notice and this permission notice appear in all copies.
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * PERFORMANCE OF THIS SOFTWARE.
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence/* $Id: os.c,v 1.90 2008/12/01 03:51:47 marka Exp $ */
de8661e517ed679cfaa12e47eb9a8e23829ed320David Lawrence#include <grp.h> /* Required for initgroups() on IRIX. */
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * If there's no <linux/capability.h>, we don't care about <sys/prctl.h>
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * Linux defines:
77771185071bf74d53378f1a3099a04d2af5153eBrian Wellington * (T) HAVE_LINUXTHREADS
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * (C) HAVE_SYS_CAPABILITY_H (or HAVE_LINUX_CAPABILITY_H)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * (P) HAVE_SYS_PRCTL_H
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * The possible cases are:
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * none: setuid() normally
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * T: no setuid()
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * C: setuid() normally, drop caps (keep CAP_SETUID)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * T+C: no setuid(), drop caps (don't keep CAP_SETUID)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * T+C+P: setuid() early, drop caps (keep CAP_SETUID)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * C+P: setuid() normally, drop caps (keep CAP_SETUID)
9b2267b5ba9d0640512a41e139a4a36caa43730dBob Halley * P: not possible
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * T+P: not possible
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * caps = BIND_SERVICE + CHROOT + SETGID
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * if ((T && C && P) || !T)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * caps += SETUID
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * capset(caps)
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * if (T && C && P && -u)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * else if (T && -u)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * --> start threads
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * if (!T && -u)
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * if (C && (P || !-u))
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * caps = BIND_SERVICE
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * capset(caps)
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * It will be nice when Linux threads work properly with setuid().
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley * We define _LINUX_FS_H to prevent it from being included. We don't need
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * anything from it, and the files it includes cause warnings with 2.2
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * kernels, and compilation failures (due to conflicts between <linux/string.h>
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * and <string.h>) on 2.3 kernels.
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley#include <asm/unistd.h> /* Slackware 4.0 needs this. */
5e4b7294d88ab58371d8c98e05ea80086dcb67cdBob Halley#endif /* __NR_capset */
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley#endif /* SYS_capset */
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley#endif /* HAVE_SYS_CAPABILITY_H */
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley#include <sys/prctl.h> /* Required for prctl(). */
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * If the value of PR_SET_KEEPCAPS is not in <sys/prctl.h>, define it
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * here. This allows setuid() to work on systems running a new enough
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * kernel but with /usr/include/linux pointing to "standard" kernel
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley#endif /* HAVE_SYS_PRCTL_H */
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halleytypedef unsigned int cap_t;
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley#endif /* HAVE_LIBCAP */
1fc4929aa610263a2362afed516d7dc8e689397dBob Halley if ((getuid() != 0 && !non_root_caps) || non_root)
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley " please ensure that the capset kernel"
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley " module is loaded. see insmod(8)",
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley err = cap_get_flag(curcaps, capval, CAP_PERMITTED, &curval); \
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley err = cap_set_flag(caps, CAP_EFFECTIVE, 1, &capval, CAP_SET); \
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley ns_main_earlyfatal("cap_set_proc failed: %s", strbuf); \
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley err = cap_set_flag(caps, CAP_PERMITTED, 1, &capval, CAP_SET); \
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley ns_main_earlyfatal("cap_set_proc failed: %s", strbuf); \
2aa67e804d85f4d88153368ce65ce4df7b5390e6Bob Halley ns_main_earlyfatal("cap_init failed: %s", strbuf); \
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley ns_main_earlyfatal("cap_get_proc failed: %s", strbuf); \
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley#define SET_CAP(flag) do { caps |= (1 << (flag)); } while (0)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley#endif /* HAVE_LIBCAP */
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * We don't need most privileges, so we drop them right away.
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * Later on linux_minprivs() will be called, which will drop our
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * capabilities to the minimum needed to run the server.
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * We need to be able to bind() to privileged ports, notably port 53!
e5afb85e525b2d2ed248dca0a954a124a704b206Andreas Gustafsson * We need chroot() initially too.
e5afb85e525b2d2ed248dca0a954a124a704b206Andreas Gustafsson#if defined(HAVE_SYS_PRCTL_H) || !defined(HAVE_LINUXTHREADS)
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * We can setuid() only if either the kernel supports keeping
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * capabilities after setuid() (which we don't know until we've
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * tried) or we're not using threads. If either of these is
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * true, we want the setuid capability.
2dfd6bca9aa6d9279b4278d6fa18ea5f63ba0ec9Bob Halley * Since we call initgroups, we need this.
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * Without this, we run into problems reading a configuration file
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * owned by a non-root user and non-world-readable on startup.
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * XXX We might want to add CAP_SYS_RESOURCE, though it's not
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * clear it would work right given the way linuxthreads work.
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * XXXDCL But since we need to be able to set the maximum number
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * of files, the stack size, data size, and core dump size to
134ba0e08a0ae9a564a8d8628fc633377d3fc239Bob Halley * support named.conf options, this is now being added to test.
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * Drop all privileges except the ability to bind() to privileged
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * It's important that we drop CAP_SYS_CHROOT. If we didn't, it
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * chroot() could be used to escape from the chrooted area.
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * XXX We might want to add CAP_SYS_RESOURCE, though it's not
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * clear it would work right given the way linuxthreads work.
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * XXXDCL But since we need to be able to set the maximum number
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * of files, the stack size, data size, and core dump size to
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * support named.conf options, this is now being added to test.
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley * Ask the kernel to allow us to keep our capabilities after we
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley ns_main_earlyfatal("prctl() failed: %s", strbuf);
9ee5efde7df57cbe70fb9b32c9d898e8ef7eca1eBob Halley#endif /* HAVE_LINUX_CAPABILITY_H */
#ifdef LOG_NDELAY
#ifdef HAVE_LINUX_CAPABILITY_H
#ifdef HAVE_LINUXTHREADS
#ifdef SIGXFSZ
ns_os_daemonize(void) {
if (pid != 0) {
char buf;
_exit(0);
#ifdef HAVE_LINUXTHREADS
ns_os_started(void) {
char buf = 0;
ns_os_opendevnull(void) {
ns_os_closedevnull(void) {
static isc_boolean_t
all_digits(const char *s) {
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_TRUE);
#ifdef HAVE_LIBSCF
ns_smf_chroot = 0;
#ifdef HAVE_CHROOT
#ifdef HAVE_LIBSCF
endpwent();
if (getuid() == 0) {
ns_os_changeuser(void) {
#ifdef HAVE_LINUXTHREADS
#ifdef HAVE_LINUX_CAPABILITY_H
if (!non_root_caps)
strbuf);
#ifdef HAVE_LINUXTHREADS
ns_os_minprivs(void) {
#ifdef HAVE_SYS_PRCTL_H
#ifdef HAVE_LINUXTHREADS
int fd;
if (append)
return (fd);
cleanup_pidfile(void) {
int fd;
void (*report)(const char *, ...);
unsigned int mode;
char *slash;
strbuf);
if (fd < 0) {
#ifdef HAVE_LINUXTHREADS
ns_os_shutdown(void) {
closelog();
char *res;
return (res);
#ifdef HAVE_LINUXTHREADS
ns_os_tzset(void) {
#ifdef HAVE_TZSET
tzset();