Fix: validate file path creds in autoload mode
[lttng-tools.git] / src / common / runas.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sched.h>
31 #include <sys/signal.h>
32
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/mman.h>
36 #include <common/compat/clone.h>
37
38 #include "runas.h"
39
40 #define RUNAS_CHILD_STACK_SIZE 10485760
41
42 #ifndef MAP_STACK
43 #define MAP_STACK 0
44 #endif
45
46 #ifdef __FreeBSD__
47 /* FreeBSD MAP_STACK always return -ENOMEM */
48 #define LTTNG_MAP_STACK 0
49 #else
50 #define LTTNG_MAP_STACK MAP_STACK
51 #endif
52
53 #ifndef MAP_GROWSDOWN
54 #define MAP_GROWSDOWN 0
55 #endif
56
57 #ifndef MAP_ANONYMOUS
58 #define MAP_ANONYMOUS MAP_ANON
59 #endif
60
61 struct run_as_data {
62 int (*cmd)(void *data);
63 void *data;
64 uid_t uid;
65 gid_t gid;
66 int retval_pipe;
67 };
68
69 struct run_as_mkdir_data {
70 const char *path;
71 mode_t mode;
72 };
73
74 struct run_as_open_data {
75 const char *path;
76 int flags;
77 mode_t mode;
78 };
79
80 #ifdef VALGRIND
81 static
82 int use_clone(void)
83 {
84 return 0;
85 }
86 #else
87 static
88 int use_clone(void)
89 {
90 return !getenv("LTTNG_DEBUG_NOCLONE");
91 }
92 #endif
93
94 /*
95 * Create recursively directory using the FULL path.
96 */
97 static
98 int _mkdir_recursive(void *_data)
99 {
100 struct run_as_mkdir_data *data = _data;
101 const char *path;
102 mode_t mode;
103
104 path = data->path;
105 mode = data->mode;
106
107 return utils_mkdir_recursive(path, mode);
108 }
109
110 static
111 int _mkdir(void *_data)
112 {
113 int ret;
114 struct run_as_mkdir_data *data = _data;
115
116 ret = mkdir(data->path, data->mode);
117 if (ret < 0) {
118 ret = -errno;
119 }
120
121 return ret;
122 }
123
124 static
125 int _open(void *_data)
126 {
127 struct run_as_open_data *data = _data;
128 return open(data->path, data->flags, data->mode);
129 }
130
131 static
132 int child_run_as(void *_data)
133 {
134 int ret;
135 struct run_as_data *data = _data;
136 ssize_t writelen;
137 int sendret;
138
139 /*
140 * Child: it is safe to drop egid and euid while sharing the
141 * file descriptors with the parent process, since we do not
142 * drop "uid": therefore, the user we are dropping egid/euid to
143 * cannot attach to this process with, e.g. ptrace, nor map this
144 * process memory.
145 */
146 if (data->gid != getegid()) {
147 ret = setegid(data->gid);
148 if (ret < 0) {
149 PERROR("setegid");
150 sendret = -1;
151 goto write_return;
152 }
153 }
154 if (data->uid != geteuid()) {
155 ret = seteuid(data->uid);
156 if (ret < 0) {
157 PERROR("seteuid");
158 sendret = -1;
159 goto write_return;
160 }
161 }
162 /*
163 * Also set umask to 0 for mkdir executable bit.
164 */
165 umask(0);
166 sendret = (*data->cmd)(data->data);
167
168 write_return:
169 /* send back return value */
170 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
171 if (writelen < sizeof(sendret)) {
172 PERROR("lttng_write error");
173 return EXIT_FAILURE;
174 } else {
175 return EXIT_SUCCESS;
176 }
177 }
178
179 static
180 int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
181 {
182 struct run_as_data run_as_data;
183 int ret = 0;
184 ssize_t readlen;
185 int status;
186 pid_t pid;
187 int retval_pipe[2];
188 void *child_stack;
189 int retval;
190
191 /*
192 * If we are non-root, we can only deal with our own uid.
193 */
194 if (geteuid() != 0) {
195 if (uid != geteuid()) {
196 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
197 uid, geteuid());
198 return -EPERM;
199 }
200 }
201
202 ret = pipe(retval_pipe);
203 if (ret < 0) {
204 PERROR("pipe");
205 retval = ret;
206 goto end;
207 }
208 run_as_data.data = data;
209 run_as_data.cmd = cmd;
210 run_as_data.uid = uid;
211 run_as_data.gid = gid;
212 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
213 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
214 PROT_WRITE | PROT_READ,
215 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
216 -1, 0);
217 if (child_stack == MAP_FAILED) {
218 PERROR("mmap");
219 retval = -ENOMEM;
220 goto close_pipe;
221 }
222 /*
223 * Pointing to the middle of the stack to support architectures
224 * where the stack grows up (HPPA).
225 */
226 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
227 &run_as_data);
228 if (pid < 0) {
229 PERROR("clone");
230 retval = pid;
231 goto unmap_stack;
232 }
233 /* receive return value */
234 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
235 if (readlen < sizeof(retval)) {
236 ret = -1;
237 }
238
239 /*
240 * Parent: wait for child to return, in which case the
241 * shared memory map will have been created.
242 */
243 pid = waitpid(pid, &status, 0);
244 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
245 PERROR("wait");
246 retval = -1;
247 }
248 unmap_stack:
249 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
250 if (ret < 0) {
251 PERROR("munmap");
252 retval = ret;
253 }
254 close_pipe:
255 ret = close(retval_pipe[0]);
256 if (ret) {
257 PERROR("close");
258 }
259 ret = close(retval_pipe[1]);
260 if (ret) {
261 PERROR("close");
262 }
263 end:
264 return retval;
265 }
266
267 /*
268 * To be used on setups where gdb has issues debugging programs using
269 * clone/rfork. Note that this is for debuging ONLY, and should not be
270 * considered secure.
271 */
272 static
273 int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
274 {
275 int ret;
276 mode_t old_mask;
277
278 old_mask = umask(0);
279 ret = cmd(data);
280 umask(old_mask);
281
282 return ret;
283 }
284
285 static
286 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
287 {
288 if (use_clone()) {
289 int ret;
290
291 DBG("Using run_as_clone");
292 pthread_mutex_lock(&lttng_libc_state_lock);
293 ret = run_as_clone(cmd, data, uid, gid);
294 pthread_mutex_unlock(&lttng_libc_state_lock);
295 return ret;
296 } else {
297 DBG("Using run_as_noclone");
298 return run_as_noclone(cmd, data, uid, gid);
299 }
300 }
301
302 LTTNG_HIDDEN
303 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
304 {
305 struct run_as_mkdir_data data;
306
307 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
308 path, mode, uid, gid);
309 data.path = path;
310 data.mode = mode;
311 return run_as(_mkdir_recursive, &data, uid, gid);
312 }
313
314 LTTNG_HIDDEN
315 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
316 {
317 struct run_as_mkdir_data data;
318
319 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
320 path, mode, uid, gid);
321 data.path = path;
322 data.mode = mode;
323 return run_as(_mkdir, &data, uid, gid);
324 }
325
326 /*
327 * Note: open_run_as is currently not working. We'd need to pass the fd
328 * opened in the child to the parent.
329 */
330 LTTNG_HIDDEN
331 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
332 {
333 struct run_as_open_data data;
334
335 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
336 path, flags, mode, uid, gid);
337 data.path = path;
338 data.flags = flags;
339 data.mode = mode;
340 return run_as(_open, &data, uid, gid);
341 }
This page took 0.037631 seconds and 4 git commands to generate.