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