]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
Merge dead branch 'lockfree'
[pulseaudio] / src / daemon / caps.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <pulsecore/macro.h>
32
33 #ifdef HAVE_SYS_CAPABILITY_H
34 #include <sys/capability.h>
35 #endif
36 #ifdef HAVE_SYS_PRCTL_H
37 #include <sys/prctl.h>
38 #endif
39
40 #include <pulsecore/core-error.h>
41
42 #include <pulsecore/log.h>
43
44 #include "caps.h"
45
46 /* Glibc <= 2.2 has broken unistd.h */
47 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
48 int setresgid(gid_t r, gid_t e, gid_t s);
49 int setresuid(uid_t r, uid_t e, uid_t s);
50 #endif
51
52 #ifdef HAVE_GETUID
53
54 /* Drop root rights when called SUID root */
55 void pa_drop_root(void) {
56 uid_t uid = getuid();
57
58 if (uid == 0 || geteuid() != 0)
59 return;
60
61 pa_log_info("Dropping root priviliges.");
62
63 #if defined(HAVE_SETRESUID)
64 pa_assert_se(setresuid(uid, uid, uid) >= 0);
65 #elif defined(HAVE_SETREUID)
66 pa_assert_se(setreuid(uid, uid) >= 0);
67 #else
68 pa_assert_se(setuid(uid) >= 0);
69 pa_assert_se(seteuid(uid) >= 0);
70 #endif
71
72 pa_assert_se(getuid() == uid);
73 pa_assert_se(geteuid() == uid);
74 }
75
76 #else
77
78 void pa_drop_root(void) {
79 }
80
81 #endif
82
83 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_SYS_PRCTL_H)
84
85 /* Limit permitted capabilities set to CAPSYS_NICE */
86 void pa_limit_caps(void) {
87 cap_t caps;
88 cap_value_t nice_cap = CAP_SYS_NICE;
89
90 pa_assert_se(caps = cap_init());
91 pa_assert_se(cap_clear(caps) == 0);
92 pa_assert_se(cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET) == 0);
93 pa_assert_se(cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET) == 0);
94
95 if (cap_set_proc(caps) < 0)
96 /* Hmm, so we couldn't limit our caps, which probably means we
97 * hadn't any in the first place, so let's just make sure of
98 * that */
99 pa_drop_caps();
100 else
101 pa_log_info("Limited capabilities successfully to CAP_SYS_NICE.");
102
103 pa_assert_se(cap_free(caps) == 0);
104
105 pa_assert_se(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == 0);
106 }
107
108 /* Drop all capabilities, effectively becoming a normal user */
109 void pa_drop_caps(void) {
110 cap_t caps;
111
112 pa_assert_se(prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) == 0);
113
114 pa_assert_se(caps = cap_init());
115 pa_assert_se(cap_clear(caps) == 0);
116 pa_assert_se(cap_set_proc(caps) == 0);
117 pa_assert_se(cap_free(caps) == 0);
118
119 pa_assert_se(!pa_have_caps());
120 }
121
122 pa_bool_t pa_have_caps(void) {
123 cap_t caps;
124 cap_flag_value_t flag = CAP_CLEAR;
125
126 pa_assert_se(caps = cap_get_proc());
127 pa_assert_se(cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &flag) >= 0);
128 pa_assert_se(cap_free(caps) == 0);
129
130 return flag == CAP_SET;
131 }
132
133 #else
134
135 /* NOOPs in case capabilities are not available. */
136 void pa_limit_caps(void) {
137 }
138
139 void pa_drop_caps(void) {
140 pa_drop_root();
141 }
142
143 pa_bool_t pa_have_caps(void) {
144 return FALSE;
145 }
146
147 #endif