4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <sys/types.h>
33 #include <pulsecore/macro.h>
35 #ifdef HAVE_SYS_CAPABILITY_H
36 #include <sys/capability.h>
38 #ifdef HAVE_SYS_PRCTL_H
39 #include <sys/prctl.h>
42 #include <pulsecore/core-error.h>
44 #include <pulsecore/log.h>
48 /* Glibc <= 2.2 has broken unistd.h */
49 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
50 int setresgid(gid_t r, gid_t e, gid_t s);
51 int setresuid(uid_t r, uid_t e, uid_t s);
56 /* Drop root rights when called SUID root */
57 void pa_drop_root(void) {
60 if (uid == 0 || geteuid() != 0)
63 pa_log_info("Dropping root priviliges.");
65 #if defined(HAVE_SETRESUID)
66 pa_assert_se(setresuid(uid, uid, uid) >= 0);
67 #elif defined(HAVE_SETREUID)
68 pa_assert_se(setreuid(uid, uid) >= 0);
70 pa_assert_se(setuid(uid) >= 0);
71 pa_assert_se(seteuid(uid) >= 0);
74 pa_assert_se(getuid() == uid);
75 pa_assert_se(geteuid() == uid);
80 void pa_drop_root(void) {
85 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_SYS_PRCTL_H)
87 /* Limit permitted capabilities set to CAPSYS_NICE */
88 void pa_limit_caps(void) {
90 cap_value_t nice_cap = CAP_SYS_NICE;
92 pa_assert_se(caps = cap_init());
93 pa_assert_se(cap_clear(caps) == 0);
94 pa_assert_se(cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET) == 0);
95 pa_assert_se(cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET) == 0);
96 pa_assert_se(cap_set_proc(caps) == 0);
98 pa_assert_se(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == 0);
100 pa_log_info("Dropped capabilities successfully.");
102 pa_assert_se(cap_free(caps) == 0);
105 /* Drop all capabilities, effectively becoming a normal user */
106 void pa_drop_caps(void) {
109 pa_assert_se(prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) == 0);
111 pa_assert_se(caps = cap_init());
112 pa_assert_se(cap_clear(caps) == 0);
113 pa_assert_se(cap_set_proc(caps) == 0);
114 pa_assert_se(cap_free(caps) == 0);
119 /* NOOPs in case capabilities are not available. */
120 int pa_limit_caps(void) {
124 void pa_drop_caps(void) {