]> code.delx.au - pulseaudio/blob - src/daemon/caps.c
big s/polyp/pulse/g
[pulseaudio] / src / daemon / caps.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; 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 <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30
31 #ifdef HAVE_SYS_CAPABILITY_H
32 #include <sys/capability.h>
33 #endif
34
35 #include <pulsecore/core-error.h>
36
37 #include <pulsecore/log.h>
38
39 #include "caps.h"
40
41 /* Glibc <= 2.2 has broken unistd.h */
42 #if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
43 int setresgid(gid_t r, gid_t e, gid_t s);
44 int setresuid(uid_t r, uid_t e, uid_t s);
45 #endif
46
47 #ifdef HAVE_GETUID
48
49 /* Drop root rights when called SUID root */
50 void pa_drop_root(void) {
51 uid_t uid = getuid();
52
53 if (uid == 0 || geteuid() != 0)
54 return;
55
56 pa_log_info(__FILE__": dropping root rights.");
57
58 #if defined(HAVE_SETRESUID)
59 setresuid(uid, uid, uid);
60 #elif defined(HAVE_SETREUID)
61 setreuid(uid, uid);
62 #else
63 setuid(uid);
64 seteuid(uid);
65 #endif
66 }
67
68 #else
69
70 void pa_drop_root(void) {
71 }
72
73 #endif
74
75 #ifdef HAVE_SYS_CAPABILITY_H
76
77 /* Limit capabilities set to CAPSYS_NICE */
78 int pa_limit_caps(void) {
79 int r = -1;
80 cap_t caps;
81 cap_value_t nice_cap = CAP_SYS_NICE;
82
83 caps = cap_init();
84 assert(caps);
85
86 cap_clear(caps);
87
88 cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET);
89 cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
90
91 if (cap_set_proc(caps) < 0)
92 goto fail;
93
94 pa_log_info(__FILE__": dropped capabilities successfully.");
95
96 r = 0;
97
98 fail:
99 cap_free (caps);
100
101 return r;
102 }
103
104 /* Drop all capabilities, effectively becoming a normal user */
105 int pa_drop_caps(void) {
106 cap_t caps;
107 int r = -1;
108
109 caps = cap_init();
110 assert(caps);
111
112 cap_clear(caps);
113
114 if (cap_set_proc(caps) < 0) {
115 pa_log(__FILE__": failed to drop capabilities: %s", pa_cstrerror(errno));
116 goto fail;
117 }
118
119 r = 0;
120
121 fail:
122 cap_free (caps);
123
124 return r;
125 }
126
127 #else
128
129 /* NOOPs in case capabilities are not available. */
130 int pa_limit_caps(void) {
131 return 0;
132 }
133
134 int pa_drop_caps(void) {
135 pa_drop_root();
136 return 0;
137 }
138
139 #endif
140