]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
f92db74394b1e4c29be7461358429b219ce42e60
[pulseaudio] / src / daemon / caps.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/types.h>
34
35 #ifdef HAVE_SYS_CAPABILITY_H
36 #include <sys/capability.h>
37 #endif
38 #ifdef HAVE_SYS_PRCTL_H
39 #include <sys/prctl.h>
40 #endif
41
42 #include <pulsecore/core-error.h>
43
44 #include <pulsecore/log.h>
45
46 #include "caps.h"
47
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);
52 #endif
53
54 #ifdef HAVE_GETUID
55
56 /* Drop root rights when called SUID root */
57 void pa_drop_root(void) {
58 uid_t uid = getuid();
59
60 if (uid == 0 || geteuid() != 0)
61 return;
62
63 pa_log_info("dropping root rights.");
64
65 #if defined(HAVE_SETRESUID)
66 setresuid(uid, uid, uid);
67 #elif defined(HAVE_SETREUID)
68 setreuid(uid, uid);
69 #else
70 setuid(uid);
71 seteuid(uid);
72 #endif
73 }
74
75 #else
76
77 void pa_drop_root(void) {
78 }
79
80 #endif
81
82 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_SYS_PRCTL_H)
83
84 /* Limit permitted capabilities set to CAPSYS_NICE */
85 int pa_limit_caps(void) {
86 int r = -1;
87 cap_t caps;
88 cap_value_t nice_cap = CAP_SYS_NICE;
89
90 caps = cap_init();
91 assert(caps);
92 cap_clear(caps);
93 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
94
95 if (cap_set_proc(caps) < 0)
96 goto fail;
97
98 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0)
99 goto fail;
100
101 pa_log_info("dropped capabilities successfully.");
102
103 r = 1;
104
105 fail:
106 cap_free(caps);
107
108 return r;
109 }
110
111 /* Drop all capabilities, effectively becoming a normal user */
112 int pa_drop_caps(void) {
113 cap_t caps;
114 int r = -1;
115
116 caps = cap_init();
117 assert(caps);
118
119 cap_clear(caps);
120
121 prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0);
122
123 if (cap_set_proc(caps) < 0) {
124 pa_log("failed to drop capabilities: %s", pa_cstrerror(errno));
125 goto fail;
126 }
127
128 r = 0;
129
130 fail:
131 cap_free(caps);
132
133 return r;
134 }
135
136 #else
137
138 /* NOOPs in case capabilities are not available. */
139 int pa_limit_caps(void) {
140 return 0;
141 }
142
143 int pa_drop_caps(void) {
144 pa_drop_root();
145 return 0;
146 }
147
148 #endif
149