os.c revision d1e4b08844175357a925ddd6dcfa750cccd2b116
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * Copyright (C) 1999, 2000 Internet Software Consortium.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * Permission to use, copy, modify, and distribute this software for any
5347c0fcb04eaea19d9f39795646239f487c6207Tinderbox User * purpose with or without fee is hereby granted, provided that the above
5347c0fcb04eaea19d9f39795646239f487c6207Tinderbox User * copyright notice and this permission notice appear in all copies.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
d6fa26d0adaec6c910115be34fe7a5a5f402c14fMark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User/* $Id: os.c,v 1.31 2000/08/10 18:56:58 bwelling Exp $ */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User#include <grp.h> /* Required for initgroups() on IRIX. */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic isc_boolean_t non_root_caps = ISC_FALSE;
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * We define _LINUX_FS_H to prevent it from being included. We don't need
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * anything from it, and the files it includes cause warnings with 2.2
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * kernels, and compilation failures (due to conflicts between <linux/string.h>
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * and <string.h>) on 2.3 kernels.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User#include <sys/syscall.h> /* Required for syscall(). */
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont#include <linux/capability.h> /* Required for _LINUX_CAPABILITY_VERSION. */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User#include <sys/prctl.h> /* Required for prctl(). */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * If the value of PR_SET_KEEPCAPS is not in <linux/prctl.h>, define it
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User * here. This allows setuid() to work on systems running a new enough
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * kernel but with /usr/include/linux pointing to "standard" kernel
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont#endif /* HAVE_LINUX_PRCTL_H */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if ((getuid() != 0 && !non_root_caps) || non_root)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("capset failed: %s", strerror(errno));
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont unsigned int caps;
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * We don't need most privileges, so we drop them right away.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * Later on linux_minprivs() will be called, which will drop our
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * capabilities to the minimum needed to run the server.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * We need to be able to bind() to privileged ports, notably port 53!
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * We need chroot() initially too.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * If the kernel supports keeping capabilities after setuid(), we
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * also want the setuid capability. We don't know until we've tried.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * Since we call initgroups, we need this.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * XXX We might want to add CAP_SYS_RESOURCE, though it's not
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * clear it would work right given the way linuxthreads work.
2eeb74d1cf5355dd98f6d507a10086e16bb08c4bTinderbox User unsigned int caps;
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * Drop all privileges except the ability to bind() to privileged
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * It's important that we drop CAP_SYS_CHROOT. If we didn't, it
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * chroot() could be used to escape from the chrooted area.
adabefa84c3dcf048566cc23fd457c577f208eeaTinderbox User * Ask the kernel to allow us to keep our capabilities after we
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User#endif /* HAVE_LINUX_CAPABILITY_H */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("fork(): %s", strerror(errno));
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * We're the child.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("setsid(): %s", strerror(errno));
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * Try to set stdin, stdout, and stderr to /dev/null, but press
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * on even if it fails.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * XXXMLG The close() calls here are unneeded on all but NetBSD, but
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * are harmless to include everywhere. dup2() is supposed to close
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * the FD if it is in use, but unproven-pthreads-0.16 is broken
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * and will end up closing the wrong FD. This will be fixed eventually,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * and these calls will be removed.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userall_digits(const char *s) {
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont if (*s == '\0')
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User while (*s != '\0') {
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont ns_main_earlyfatal("chroot(): %s", strerror(errno));
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("chdir(/): %s", strerror(errno));
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("user '%s' unknown", username);
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont if (getuid() == 0) {
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (initgroups(runas_pw->pw_name, runas_pw->pw_gid) < 0)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("initgroups(): %s", strerror(errno));
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont "-u not supported on Linux kernels older than 2.3.99-pre3");
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("setgid(): %s", strerror(errno));
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont ns_main_earlyfatal("setuid(): %s", strerror(errno));
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User#endif /* HAVE_LINUX_CAPABILITY_H */
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User return (open(filename, O_WRONLY|O_CREAT|O_EXCL,
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont * The caller must ensure any required synchronization.
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont ns_main_earlyfatal("couldn't malloc '%s': %s",
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont /* This is safe. */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("couldn't open pid file '%s': %s",
7768b049c0b39662baca604ec32b32c6480f8dc6Francis Dupont ns_main_earlyfatal("could not fdopen() pid file '%s': %s",
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (fprintf(lockfile, "%ld\n", (long)pid) < 0)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("fprintf() to pid file '%s' failed",
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ns_main_earlyfatal("fflush() to pid file '%s' failed",