Commit | Line | Data |
---|---|---|
60b6c79c MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
60b6c79c MD |
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 | |
d14d33bf | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
60b6c79c MD |
12 | * more details. |
13 | * | |
d14d33bf AM |
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. | |
60b6c79c MD |
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> | |
c2b75c49 | 30 | #include <sched.h> |
730389d9 | 31 | #include <sys/signal.h> |
60b6c79c | 32 | |
db758600 | 33 | #include <common/error.h> |
730389d9 DG |
34 | #include <common/compat/mman.h> |
35 | #include <common/compat/clone.h> | |
60b6c79c | 36 | |
0857097f DG |
37 | #include "runas.h" |
38 | ||
e11d277b | 39 | #define RUNAS_CHILD_STACK_SIZE 10485760 |
c2b75c49 | 40 | |
1e4035fc AS |
41 | #ifndef MAP_STACK |
42 | #define MAP_STACK 0 | |
43 | #endif | |
44 | ||
0756d557 MD |
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 | |
059e1d3d MD |
50 | #endif |
51 | ||
a5ae566a | 52 | #ifndef MAP_GROWSDOWN |
edb025e8 | 53 | #define MAP_GROWSDOWN 0 |
a5ae566a MD |
54 | #endif |
55 | ||
56 | #ifndef MAP_ANONYMOUS | |
57 | #define MAP_ANONYMOUS MAP_ANON | |
58 | #endif | |
59 | ||
c2b75c49 MD |
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 | ||
e11d277b | 68 | struct run_as_mkdir_data { |
60b6c79c MD |
69 | const char *path; |
70 | mode_t mode; | |
71 | }; | |
72 | ||
e11d277b | 73 | struct run_as_open_data { |
60b6c79c MD |
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 | { | |
e11d277b | 85 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
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 | { | |
e11d277b | 142 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
143 | return mkdir(data->path, data->mode); |
144 | } | |
145 | ||
146 | static | |
147 | int _open(void *_data) | |
148 | { | |
e11d277b | 149 | struct run_as_open_data *data = _data; |
60b6c79c MD |
150 | return open(data->path, data->flags, data->mode); |
151 | } | |
152 | ||
c2b75c49 MD |
153 | static |
154 | int child_run_as(void *_data) | |
155 | { | |
6775595e | 156 | int ret; |
c2b75c49 | 157 | struct run_as_data *data = _data; |
6775595e DG |
158 | ssize_t writelen; |
159 | size_t writeleft, index; | |
c2b75c49 MD |
160 | union { |
161 | int i; | |
162 | char c[sizeof(int)]; | |
163 | } sendret; | |
c2b75c49 MD |
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 | */ | |
1576d582 MD |
172 | if (data->gid != getegid()) { |
173 | ret = setegid(data->gid); | |
174 | if (ret < 0) { | |
4c462e79 | 175 | PERROR("setegid"); |
6da9f915 | 176 | return EXIT_FAILURE; |
1576d582 | 177 | } |
c2b75c49 | 178 | } |
1576d582 MD |
179 | if (data->uid != geteuid()) { |
180 | ret = seteuid(data->uid); | |
181 | if (ret < 0) { | |
4c462e79 | 182 | PERROR("seteuid"); |
6da9f915 | 183 | return EXIT_FAILURE; |
1576d582 | 184 | } |
c2b75c49 MD |
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 { | |
6f94560a MD |
195 | do { |
196 | writelen = write(data->retval_pipe, &sendret.c[index], | |
197 | writeleft); | |
198 | } while (writelen < 0 && errno == EINTR); | |
c2b75c49 | 199 | if (writelen < 0) { |
4c462e79 | 200 | PERROR("write"); |
6da9f915 | 201 | return EXIT_FAILURE; |
c2b75c49 MD |
202 | } |
203 | writeleft -= writelen; | |
204 | index += writelen; | |
205 | } while (writeleft > 0); | |
6da9f915 | 206 | return EXIT_SUCCESS; |
c2b75c49 MD |
207 | } |
208 | ||
60b6c79c | 209 | static |
2d85a600 | 210 | int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) |
60b6c79c | 211 | { |
c2b75c49 | 212 | struct run_as_data run_as_data; |
60b6c79c | 213 | int ret = 0; |
c2b75c49 | 214 | int status; |
60b6c79c | 215 | pid_t pid; |
c2b75c49 MD |
216 | int retval_pipe[2]; |
217 | ssize_t readlen, readleft, index; | |
d5bd7573 | 218 | void *child_stack; |
c2b75c49 MD |
219 | union { |
220 | int i; | |
221 | char c[sizeof(int)]; | |
222 | } retval; | |
60b6c79c MD |
223 | |
224 | /* | |
225 | * If we are non-root, we can only deal with our own uid. | |
226 | */ | |
227 | if (geteuid() != 0) { | |
228 | if (uid != geteuid()) { | |
229 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", | |
230 | uid, geteuid()); | |
231 | return -EPERM; | |
232 | } | |
60b6c79c MD |
233 | } |
234 | ||
c2b75c49 MD |
235 | ret = pipe(retval_pipe); |
236 | if (ret < 0) { | |
4c462e79 | 237 | PERROR("pipe"); |
def5e0e8 | 238 | retval.i = ret; |
60b6c79c | 239 | goto end; |
c2b75c49 MD |
240 | } |
241 | run_as_data.data = data; | |
242 | run_as_data.cmd = cmd; | |
243 | run_as_data.uid = uid; | |
244 | run_as_data.gid = gid; | |
245 | run_as_data.retval_pipe = retval_pipe[1]; /* write end */ | |
e11d277b | 246 | child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, |
d5bd7573 | 247 | PROT_WRITE | PROT_READ, |
0756d557 | 248 | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, |
d5bd7573 MD |
249 | -1, 0); |
250 | if (child_stack == MAP_FAILED) { | |
4c462e79 | 251 | PERROR("mmap"); |
def5e0e8 | 252 | retval.i = -ENOMEM; |
d5bd7573 MD |
253 | goto close_pipe; |
254 | } | |
255 | /* | |
256 | * Pointing to the middle of the stack to support architectures | |
257 | * where the stack grows up (HPPA). | |
258 | */ | |
89831a74 MD |
259 | pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), |
260 | &run_as_data); | |
c2b75c49 | 261 | if (pid < 0) { |
4c462e79 | 262 | PERROR("clone"); |
def5e0e8 | 263 | retval.i = pid; |
d5bd7573 | 264 | goto unmap_stack; |
c2b75c49 MD |
265 | } |
266 | /* receive return value */ | |
267 | readleft = sizeof(retval); | |
268 | index = 0; | |
269 | do { | |
270 | readlen = read(retval_pipe[0], &retval.c[index], readleft); | |
271 | if (readlen < 0) { | |
4c462e79 | 272 | PERROR("read"); |
c2b75c49 MD |
273 | ret = -1; |
274 | break; | |
60b6c79c | 275 | } |
c2b75c49 MD |
276 | readleft -= readlen; |
277 | index += readlen; | |
278 | } while (readleft > 0); | |
279 | ||
280 | /* | |
281 | * Parent: wait for child to return, in which case the | |
282 | * shared memory map will have been created. | |
283 | */ | |
47fb7563 | 284 | pid = waitpid(pid, &status, 0); |
c2b75c49 | 285 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
4c462e79 | 286 | PERROR("wait"); |
def5e0e8 | 287 | retval.i = -1; |
60b6c79c | 288 | } |
d5bd7573 | 289 | unmap_stack: |
e11d277b | 290 | ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); |
d5bd7573 | 291 | if (ret < 0) { |
4c462e79 | 292 | PERROR("munmap"); |
def5e0e8 | 293 | retval.i = ret; |
d5bd7573 | 294 | } |
c2b75c49 | 295 | close_pipe: |
4c462e79 MD |
296 | ret = close(retval_pipe[0]); |
297 | if (ret) { | |
298 | PERROR("close"); | |
299 | } | |
300 | ret = close(retval_pipe[1]); | |
301 | if (ret) { | |
302 | PERROR("close"); | |
303 | } | |
60b6c79c | 304 | end: |
c2b75c49 | 305 | return retval.i; |
60b6c79c MD |
306 | } |
307 | ||
2d85a600 MD |
308 | /* |
309 | * To be used on setups where gdb has issues debugging programs using | |
310 | * clone/rfork. Note that this is for debuging ONLY, and should not be | |
311 | * considered secure. | |
312 | */ | |
313 | static | |
314 | int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
315 | { | |
5b73926f DG |
316 | int ret; |
317 | mode_t old_mask; | |
318 | ||
319 | old_mask = umask(0); | |
320 | ret = cmd(data); | |
321 | umask(old_mask); | |
322 | ||
323 | return ret; | |
2d85a600 MD |
324 | } |
325 | ||
326 | static | |
327 | int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
328 | { | |
329 | if (!getenv("LTTNG_DEBUG_NOCLONE")) { | |
9ef70f87 MD |
330 | int ret; |
331 | ||
2d85a600 | 332 | DBG("Using run_as_clone"); |
9ef70f87 MD |
333 | pthread_mutex_lock(<tng_libc_state_lock); |
334 | ret = run_as_clone(cmd, data, uid, gid); | |
335 | pthread_mutex_unlock(<tng_libc_state_lock); | |
336 | return ret; | |
2d85a600 MD |
337 | } else { |
338 | DBG("Using run_as_noclone"); | |
339 | return run_as_noclone(cmd, data, uid, gid); | |
340 | } | |
341 | } | |
342 | ||
32dd26fb | 343 | __attribute__((visibility("hidden"))) |
e11d277b | 344 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 345 | { |
e11d277b | 346 | struct run_as_mkdir_data data; |
60b6c79c MD |
347 | |
348 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", | |
349 | path, mode, uid, gid); | |
350 | data.path = path; | |
351 | data.mode = mode; | |
352 | return run_as(_mkdir_recursive, &data, uid, gid); | |
353 | } | |
354 | ||
32dd26fb | 355 | __attribute__((visibility("hidden"))) |
e11d277b | 356 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 357 | { |
e11d277b | 358 | struct run_as_mkdir_data data; |
60b6c79c MD |
359 | |
360 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", | |
361 | path, mode, uid, gid); | |
362 | data.path = path; | |
363 | data.mode = mode; | |
364 | return run_as(_mkdir, &data, uid, gid); | |
365 | } | |
366 | ||
367 | /* | |
368 | * Note: open_run_as is currently not working. We'd need to pass the fd | |
369 | * opened in the child to the parent. | |
370 | */ | |
32dd26fb | 371 | __attribute__((visibility("hidden"))) |
e11d277b | 372 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 373 | { |
e11d277b | 374 | struct run_as_open_data data; |
c2b75c49 | 375 | |
47fb7563 MD |
376 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
377 | path, flags, mode, uid, gid); | |
60b6c79c MD |
378 | data.path = path; |
379 | data.flags = flags; | |
380 | data.mode = mode; | |
381 | return run_as(_open, &data, uid, gid); | |
60b6c79c | 382 | } |