4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/capability.h>
30 #include <sys/prctl.h>
32 #include <avahi-core/log.h>
36 int avahi_caps_reduce(void) {
39 static cap_value_t cap_values[] = { CAP_SYS_CHROOT, CAP_SETUID, CAP_SETGID };
41 /* Let's reduce our caps to the minimum set and tell Linux to keep
42 * them across setuid(). This is called before we drop
49 cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_values, CAP_SET);
50 cap_set_flag(caps, CAP_PERMITTED, 3, cap_values, CAP_SET);
52 if (cap_set_proc(caps) < 0) {
53 avahi_log_error("cap_set_proc() failed: %s", strerror(errno));
58 /* Retain capabilities across setuid() */
59 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
60 avahi_log_error("prctl(PR_SET_KEEPCAPS) failed: %s", strerror(errno));
67 int avahi_caps_reduce2(void) {
70 static cap_value_t cap_values[] = { CAP_SYS_CHROOT };
72 /* Reduce our caps to the bare minimum and tell Linux not to keep
73 * them across setuid(). This is called after we drop
76 /* No longer retain caps across setuid() */
77 if (prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) < 0) {
78 avahi_log_error("prctl(PR_SET_KEEPCAPS) failed: %s", strerror(errno));
86 /* setuid() zeroed our effective caps, let's get them back */
87 cap_set_flag(caps, CAP_EFFECTIVE, 1, cap_values, CAP_SET);
88 cap_set_flag(caps, CAP_PERMITTED, 1, cap_values, CAP_SET);
90 if (cap_set_proc(caps) < 0) {
91 avahi_log_error("cap_set_proc() failed: %s", strerror(errno));
99 int avahi_caps_drop_all(void) {
103 /* Drop all capabilities and turn ourselves into a normal user process */
109 if (cap_set_proc(caps) < 0) {
110 avahi_log_error("cap_set_proc() failed: %s", strerror(errno));