]>
code.delx.au - pulseaudio/blob - src/daemon/caps.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
30 #include <sys/types.h>
32 #include <pulse/i18n.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/log.h>
38 #ifdef HAVE_SYS_CAPABILITY_H
39 #include <sys/capability.h>
41 #ifdef HAVE_SYS_PRCTL_H
42 #include <sys/prctl.h>
47 /* Glibc <= 2.2 has broken unistd.h */
48 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
49 int setresgid(gid_t r
, gid_t e
, gid_t s
);
50 int setresuid(uid_t r
, uid_t e
, uid_t s
);
55 /* Drop root rights when called SUID root */
56 void pa_drop_root(void) {
59 if (uid
== 0 || geteuid() != 0)
62 pa_log_info(_("Dropping root priviliges."));
64 #if defined(HAVE_SETRESUID)
65 pa_assert_se(setresuid(uid
, uid
, uid
) >= 0);
66 #elif defined(HAVE_SETREUID)
67 pa_assert_se(setreuid(uid
, uid
) >= 0);
69 pa_assert_se(setuid(uid
) >= 0);
70 pa_assert_se(seteuid(uid
) >= 0);
73 pa_assert_se(getuid() == uid
);
74 pa_assert_se(geteuid() == uid
);
79 void pa_drop_root(void) {
84 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_SYS_PRCTL_H)
86 /* Limit permitted capabilities set to CAPSYS_NICE */
87 void pa_limit_caps(void) {
89 cap_value_t nice_cap
= CAP_SYS_NICE
;
91 pa_assert_se(caps
= cap_init());
92 pa_assert_se(cap_clear(caps
) == 0);
93 pa_assert_se(cap_set_flag(caps
, CAP_EFFECTIVE
, 1, &nice_cap
, CAP_SET
) == 0);
94 pa_assert_se(cap_set_flag(caps
, CAP_PERMITTED
, 1, &nice_cap
, CAP_SET
) == 0);
96 if (cap_set_proc(caps
) < 0)
97 /* Hmm, so we couldn't limit our caps, which probably means we
98 * hadn't any in the first place, so let's just make sure of
102 pa_log_info(_("Limited capabilities successfully to CAP_SYS_NICE."));
104 pa_assert_se(cap_free(caps
) == 0);
106 pa_assert_se(prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0) == 0);
109 /* Drop all capabilities, effectively becoming a normal user */
110 void pa_drop_caps(void) {
114 /* Valgrind doesn't not know set_caps, so we bypass it here -- but
115 * only in development builts.*/
117 if (getenv("VALGRIND") && !pa_have_caps())
121 pa_assert_se(prctl(PR_SET_KEEPCAPS
, 0, 0, 0, 0) == 0);
123 pa_assert_se(caps
= cap_init());
124 pa_assert_se(cap_clear(caps
) == 0);
125 pa_assert_se(cap_set_proc(caps
) == 0);
126 pa_assert_se(cap_free(caps
) == 0);
128 pa_assert_se(!pa_have_caps());
131 pa_bool_t
pa_have_caps(void) {
133 cap_flag_value_t flag
= CAP_CLEAR
;
136 pa_assert_se(caps
= cap_get_proc());
138 if (!(caps
= cap_get_proc()))
141 pa_assert_se(cap_get_flag(caps
, CAP_SYS_NICE
, CAP_EFFECTIVE
, &flag
) >= 0);
142 pa_assert_se(cap_free(caps
) == 0);
144 return flag
== CAP_SET
;
149 /* NOOPs in case capabilities are not available. */
150 void pa_limit_caps(void) {
153 void pa_drop_caps(void) {
157 pa_bool_t
pa_have_caps(void) {