]> git.meshlink.io Git - catta/blob - avahi-daemon/caps.c
* add chroot() support on Linux
[catta] / avahi-daemon / caps.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/capability.h>
30 #include <sys/prctl.h>
31
32 #include <avahi-core/log.h>
33
34 #include "caps.h"
35
36 int avahi_caps_reduce(void) {
37     int ret = 0;
38     cap_t caps;
39     static cap_value_t cap_values[] = { CAP_SYS_CHROOT, CAP_SETUID, CAP_SETGID };
40     
41     /* Let's reduce our caps to the minimum set and tell Linux to keep
42      * them accross setuid(). This is called before we droppped
43      * priviliges. */
44     
45     caps = cap_init();
46     assert(caps);
47     cap_clear(caps);
48
49     cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_values, CAP_SET);
50     cap_set_flag(caps, CAP_PERMITTED, 3, cap_values, CAP_SET);
51     
52     if (cap_set_proc(caps) < 0) {
53         avahi_log_error("cap_set_proc() failed: %s", strerror(errno));
54         ret = -1;
55     }
56     cap_free(caps);
57
58     /* Retain capabilities accros setuid() */
59     if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
60         avahi_log_error("prctl(PR_SET_KEEPCAPS) failed: %s", strerror(errno));
61         ret = -1;
62     }
63
64     return ret;
65 }
66
67 int avahi_caps_reduce2(void) {
68     int ret = 0;
69     cap_t caps;
70     static cap_value_t cap_values[] = { CAP_SYS_CHROOT };
71
72     /* Reduce our caps to the bare minimum and tell Linux not to keep
73      * them across setuid(). This is called after we dropped
74      * privilige. */
75     
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));
79         ret = -1;
80     }
81
82     caps = cap_init();
83     assert(caps);
84     cap_clear(caps);
85
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);
89
90     if (cap_set_proc(caps) < 0) {
91         avahi_log_error("cap_set_proc() failed: %s", strerror(errno));
92         ret = -1;
93     }
94     cap_free(caps);
95
96     return ret;
97 }
98
99 int avahi_caps_drop_all(void) {
100     cap_t caps;
101     int ret = 0;
102
103     /* Drop all capabilities and turn ourselves into a normal user process */
104
105     caps = cap_init();
106     assert(caps);
107     cap_clear(caps);
108     
109     if (cap_set_proc(caps) < 0) {
110         avahi_log_error("cap_set_proc() failed: %s", strerror(errno));
111         ret = -1;
112     }
113     cap_free(caps);
114     
115     return ret;
116 }