Add run_as_unlink implementation
[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 struct run_as_unlink_data {
81 const char *path;
82 };
83
84 #ifdef VALGRIND
85 static
86 int use_clone(void)
87 {
88 return 0;
89 }
90 #else
91 static
92 int use_clone(void)
93 {
94 return !getenv("LTTNG_DEBUG_NOCLONE");
95 }
96 #endif
97
98 LTTNG_HIDDEN
99 int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
100
101 /*
102 * Create recursively directory using the FULL path.
103 */
104 static
105 int _mkdir_recursive(void *_data)
106 {
107 struct run_as_mkdir_data *data = _data;
108 const char *path;
109 mode_t mode;
110
111 path = data->path;
112 mode = data->mode;
113
114 /* Safe to call as we have transitioned to the requested uid/gid. */
115 return _utils_mkdir_recursive_unsafe(path, mode);
116 }
117
118 static
119 int _mkdir(void *_data)
120 {
121 int ret;
122 struct run_as_mkdir_data *data = _data;
123
124 ret = mkdir(data->path, data->mode);
125 if (ret < 0) {
126 ret = -errno;
127 }
128
129 return ret;
130 }
131
132 static
133 int _open(void *_data)
134 {
135 struct run_as_open_data *data = _data;
136 return open(data->path, data->flags, data->mode);
137 }
138
139 static
140 int _unlink(void *_data)
141 {
142 int ret;
143 struct run_as_unlink_data *data = _data;
144
145 ret = unlink(data->path);
146 if (ret < 0) {
147 ret = -errno;
148 }
149
150 return ret;
151 }
152
153 static
154 int child_run_as(void *_data)
155 {
156 int ret;
157 struct run_as_data *data = _data;
158 ssize_t writelen;
159 int sendret;
160
161 /*
162 * Child: it is safe to drop egid and euid while sharing the
163 * file descriptors with the parent process, since we do not
164 * drop "uid": therefore, the user we are dropping egid/euid to
165 * cannot attach to this process with, e.g. ptrace, nor map this
166 * process memory.
167 */
168 if (data->gid != getegid()) {
169 ret = setegid(data->gid);
170 if (ret < 0) {
171 PERROR("setegid");
172 sendret = -1;
173 goto write_return;
174 }
175 }
176 if (data->uid != geteuid()) {
177 ret = seteuid(data->uid);
178 if (ret < 0) {
179 PERROR("seteuid");
180 sendret = -1;
181 goto write_return;
182 }
183 }
184 /*
185 * Also set umask to 0 for mkdir executable bit.
186 */
187 umask(0);
188 sendret = (*data->cmd)(data->data);
189
190 write_return:
191 /* send back return value */
192 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
193 if (writelen < sizeof(sendret)) {
194 PERROR("lttng_write error");
195 return EXIT_FAILURE;
196 } else {
197 return EXIT_SUCCESS;
198 }
199 }
200
201 static
202 int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
203 {
204 struct run_as_data run_as_data;
205 int ret = 0;
206 ssize_t readlen;
207 int status;
208 pid_t pid;
209 int retval_pipe[2];
210 void *child_stack;
211 int retval;
212
213 /*
214 * If we are non-root, we can only deal with our own uid.
215 */
216 if (geteuid() != 0) {
217 if (uid != geteuid()) {
218 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
219 uid, geteuid());
220 return -EPERM;
221 }
222 }
223
224 ret = pipe(retval_pipe);
225 if (ret < 0) {
226 PERROR("pipe");
227 retval = ret;
228 goto end;
229 }
230 run_as_data.data = data;
231 run_as_data.cmd = cmd;
232 run_as_data.uid = uid;
233 run_as_data.gid = gid;
234 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
235 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
236 PROT_WRITE | PROT_READ,
237 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
238 -1, 0);
239 if (child_stack == MAP_FAILED) {
240 PERROR("mmap");
241 retval = -ENOMEM;
242 goto close_pipe;
243 }
244 /*
245 * Pointing to the middle of the stack to support architectures
246 * where the stack grows up (HPPA).
247 */
248 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
249 &run_as_data);
250 if (pid < 0) {
251 PERROR("clone");
252 retval = pid;
253 goto unmap_stack;
254 }
255 /* receive return value */
256 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
257 if (readlen < sizeof(retval)) {
258 ret = -1;
259 }
260
261 /*
262 * Parent: wait for child to return, in which case the
263 * shared memory map will have been created.
264 */
265 pid = waitpid(pid, &status, 0);
266 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
267 PERROR("wait");
268 retval = -1;
269 }
270 unmap_stack:
271 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
272 if (ret < 0) {
273 PERROR("munmap");
274 retval = ret;
275 }
276 close_pipe:
277 ret = close(retval_pipe[0]);
278 if (ret) {
279 PERROR("close");
280 }
281 ret = close(retval_pipe[1]);
282 if (ret) {
283 PERROR("close");
284 }
285 end:
286 return retval;
287 }
288
289 /*
290 * To be used on setups where gdb has issues debugging programs using
291 * clone/rfork. Note that this is for debuging ONLY, and should not be
292 * considered secure.
293 */
294 static
295 int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
296 {
297 int ret;
298 mode_t old_mask;
299
300 old_mask = umask(0);
301 ret = cmd(data);
302 umask(old_mask);
303
304 return ret;
305 }
306
307 static
308 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
309 {
310 if (use_clone()) {
311 int ret;
312
313 DBG("Using run_as_clone");
314 pthread_mutex_lock(&lttng_libc_state_lock);
315 ret = run_as_clone(cmd, data, uid, gid);
316 pthread_mutex_unlock(&lttng_libc_state_lock);
317 return ret;
318 } else {
319 DBG("Using run_as_noclone");
320 return run_as_noclone(cmd, data, uid, gid);
321 }
322 }
323
324 LTTNG_HIDDEN
325 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
326 {
327 struct run_as_mkdir_data data;
328
329 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
330 path, mode, uid, gid);
331 data.path = path;
332 data.mode = mode;
333 return run_as(_mkdir_recursive, &data, uid, gid);
334 }
335
336 LTTNG_HIDDEN
337 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
338 {
339 struct run_as_mkdir_data data;
340
341 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
342 path, mode, uid, gid);
343 data.path = path;
344 data.mode = mode;
345 return run_as(_mkdir, &data, uid, gid);
346 }
347
348 /*
349 * Note: open_run_as is currently not working. We'd need to pass the fd
350 * opened in the child to the parent.
351 */
352 LTTNG_HIDDEN
353 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
354 {
355 struct run_as_open_data data;
356
357 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
358 path, flags, mode, uid, gid);
359 data.path = path;
360 data.flags = flags;
361 data.mode = mode;
362 return run_as(_open, &data, uid, gid);
363 }
364
365 LTTNG_HIDDEN
366 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
367 {
368 struct run_as_unlink_data data;
369
370 DBG3("unlink() %s with for uid %d and gid %d",
371 path, uid, gid);
372 data.path = path;
373 return run_as(_unlink, &data, uid, gid);
374 }
This page took 0.037578 seconds and 4 git commands to generate.