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