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 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
60b6c79c MD |
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> |
0452bf08 | 31 | #include <signal.h> |
749b7a0c | 32 | #include <assert.h> |
e1055edb | 33 | #include <signal.h> |
60b6c79c | 34 | |
0ef03255 | 35 | #include <common/lttng-kernel.h> |
90e535ef | 36 | #include <common/common.h> |
3fd15a74 | 37 | #include <common/utils.h> |
e8fa9fb0 | 38 | #include <common/compat/getenv.h> |
e1055edb | 39 | #include <common/compat/prctl.h> |
2038dd6c JG |
40 | #include <common/unix.h> |
41 | #include <common/defaults.h> | |
241e0a5a FD |
42 | #include <common/lttng-elf.h> |
43 | ||
44 | #include <lttng/constant.h> | |
60b6c79c | 45 | |
0857097f DG |
46 | #include "runas.h" |
47 | ||
7567352f | 48 | struct run_as_data; |
fe9f7760 FD |
49 | struct run_as_ret; |
50 | typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value); | |
c2b75c49 | 51 | |
a42089bd | 52 | struct run_as_mkdirat_data { |
7567352f | 53 | char path[PATH_MAX]; |
60b6c79c MD |
54 | mode_t mode; |
55 | }; | |
56 | ||
e11d277b | 57 | struct run_as_open_data { |
7567352f | 58 | char path[PATH_MAX]; |
60b6c79c MD |
59 | int flags; |
60 | mode_t mode; | |
61 | }; | |
62 | ||
4628484a | 63 | struct run_as_unlink_data { |
7567352f | 64 | char path[PATH_MAX]; |
4628484a MD |
65 | }; |
66 | ||
7567352f MD |
67 | struct run_as_rmdir_recursive_data { |
68 | char path[PATH_MAX]; | |
69 | }; | |
70 | ||
241e0a5a FD |
71 | struct run_as_extract_elf_symbol_offset_data { |
72 | char function[LTTNG_SYMBOL_NAME_LEN]; | |
73 | }; | |
74 | ||
0ef03255 FD |
75 | struct run_as_extract_sdt_probe_offsets_data { |
76 | char probe_name[LTTNG_SYMBOL_NAME_LEN]; | |
77 | char provider_name[LTTNG_SYMBOL_NAME_LEN]; | |
78 | }; | |
79 | ||
a42089bd | 80 | struct run_as_mkdirat_ret { |
fe9f7760 FD |
81 | int ret; |
82 | }; | |
83 | ||
84 | struct run_as_open_ret { | |
85 | int ret; | |
86 | }; | |
87 | ||
88 | struct run_as_unlink_ret { | |
89 | int ret; | |
90 | }; | |
91 | ||
92 | struct run_as_rmdir_recursive_ret { | |
93 | int ret; | |
94 | }; | |
95 | ||
241e0a5a FD |
96 | struct run_as_extract_elf_symbol_offset_ret { |
97 | uint64_t offset; | |
98 | }; | |
99 | ||
0ef03255 FD |
100 | struct run_as_extract_sdt_probe_offsets_ret { |
101 | uint32_t num_offset; | |
102 | uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM]; | |
103 | }; | |
104 | ||
7567352f MD |
105 | enum run_as_cmd { |
106 | RUN_AS_MKDIR, | |
a42089bd JG |
107 | RUN_AS_MKDIRAT, |
108 | RUN_AS_MKDIR_RECURSIVE, | |
109 | RUN_AS_MKDIRAT_RECURSIVE, | |
7567352f MD |
110 | RUN_AS_OPEN, |
111 | RUN_AS_UNLINK, | |
112 | RUN_AS_RMDIR_RECURSIVE, | |
241e0a5a | 113 | RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, |
0ef03255 | 114 | RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, |
7567352f MD |
115 | }; |
116 | ||
117 | struct run_as_data { | |
118 | enum run_as_cmd cmd; | |
fe9f7760 | 119 | int fd; |
7567352f | 120 | union { |
a42089bd | 121 | struct run_as_mkdirat_data mkdirat; |
7567352f MD |
122 | struct run_as_open_data open; |
123 | struct run_as_unlink_data unlink; | |
124 | struct run_as_rmdir_recursive_data rmdir_recursive; | |
241e0a5a | 125 | struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset; |
0ef03255 | 126 | struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets; |
7567352f MD |
127 | } u; |
128 | uid_t uid; | |
129 | gid_t gid; | |
4628484a MD |
130 | }; |
131 | ||
fe9f7760 FD |
132 | /* |
133 | * The run_as_ret structure holds the returned value and status of the command. | |
134 | * | |
135 | * The `u` union field holds the return value of the command; in most cases it | |
136 | * represents the success or the failure of the command. In more complex | |
137 | * commands, it holds a computed value. | |
138 | * | |
139 | * The _errno field is the errno recorded after the execution of the command. | |
140 | * | |
141 | * The _error fields is used the signify that return status of the command. For | |
142 | * simple commands returning `int` the _error field will be the same as the | |
143 | * ret_int field. In complex commands, it signify the success or failure of the | |
144 | * command. | |
145 | * | |
146 | */ | |
df5b86c8 | 147 | struct run_as_ret { |
fe9f7760 FD |
148 | int fd; |
149 | union { | |
a42089bd | 150 | struct run_as_mkdirat_ret mkdirat; |
fe9f7760 FD |
151 | struct run_as_open_ret open; |
152 | struct run_as_unlink_ret unlink; | |
153 | struct run_as_rmdir_recursive_ret rmdir_recursive; | |
241e0a5a | 154 | struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset; |
0ef03255 | 155 | struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets; |
fe9f7760 | 156 | } u; |
df5b86c8 | 157 | int _errno; |
fe9f7760 | 158 | bool _error; |
df5b86c8 MD |
159 | }; |
160 | ||
7567352f MD |
161 | struct run_as_worker { |
162 | pid_t pid; /* Worker PID. */ | |
163 | int sockpair[2]; | |
164 | char *procname; | |
165 | }; | |
166 | ||
167 | /* Single global worker per process (for now). */ | |
168 | static struct run_as_worker *global_worker; | |
169 | /* Lock protecting the worker. */ | |
170 | static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER; | |
171 | ||
8f0044bf MD |
172 | #ifdef VALGRIND |
173 | static | |
174 | int use_clone(void) | |
175 | { | |
176 | return 0; | |
177 | } | |
178 | #else | |
179 | static | |
180 | int use_clone(void) | |
181 | { | |
e8fa9fb0 | 182 | return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE"); |
8f0044bf MD |
183 | } |
184 | #endif | |
185 | ||
60b6c79c MD |
186 | /* |
187 | * Create recursively directory using the FULL path. | |
188 | */ | |
189 | static | |
a42089bd | 190 | int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 191 | { |
60b6c79c | 192 | const char *path; |
60b6c79c | 193 | mode_t mode; |
a42089bd | 194 | struct lttng_directory_handle handle; |
60b6c79c | 195 | |
a42089bd JG |
196 | path = data->u.mkdirat.path; |
197 | mode = data->u.mkdirat.mode; | |
60b6c79c | 198 | |
a42089bd | 199 | (void) lttng_directory_handle_init_from_dirfd(&handle, data->fd); |
d77dded2 | 200 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
a42089bd JG |
201 | ret_value->u.mkdirat.ret = |
202 | lttng_directory_handle_create_subdirectory_recursive( | |
203 | &handle, path, mode); | |
fe9f7760 | 204 | ret_value->_errno = errno; |
a42089bd JG |
205 | ret_value->_error = (ret_value->u.mkdirat.ret) ? true : false; |
206 | lttng_directory_handle_fini(&handle); | |
207 | return ret_value->u.mkdirat.ret; | |
60b6c79c MD |
208 | } |
209 | ||
210 | static | |
a42089bd | 211 | int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 212 | { |
a42089bd JG |
213 | const char *path; |
214 | mode_t mode; | |
215 | struct lttng_directory_handle handle; | |
216 | ||
217 | path = data->u.mkdirat.path; | |
218 | mode = data->u.mkdirat.mode; | |
219 | ||
220 | (void) lttng_directory_handle_init_from_dirfd(&handle, data->fd); | |
221 | /* Safe to call as we have transitioned to the requested uid/gid. */ | |
222 | ret_value->u.mkdirat.ret = | |
223 | lttng_directory_handle_create_subdirectory( | |
224 | &handle, path, mode); | |
fe9f7760 | 225 | ret_value->_errno = errno; |
a42089bd JG |
226 | ret_value->_error = (ret_value->u.mkdirat.ret) ? true : false; |
227 | lttng_directory_handle_fini(&handle); | |
228 | return ret_value->u.mkdirat.ret; | |
7567352f | 229 | } |
7ce36756 | 230 | |
7567352f | 231 | static |
fe9f7760 | 232 | int _open(struct run_as_data *data, struct run_as_ret *ret_value) |
7567352f | 233 | { |
fe9f7760 FD |
234 | ret_value->u.open.ret = open(data->u.open.path, data->u.open.flags, data->u.open.mode); |
235 | ret_value->fd = ret_value->u.open.ret; | |
236 | ret_value->_errno = errno; | |
40c10794 | 237 | ret_value->_error = ret_value->u.open.ret < 0; |
fe9f7760 | 238 | return ret_value->u.open.ret; |
7567352f MD |
239 | } |
240 | ||
241 | static | |
fe9f7760 | 242 | int _unlink(struct run_as_data *data, struct run_as_ret *ret_value) |
7567352f | 243 | { |
fe9f7760 FD |
244 | ret_value->u.unlink.ret = unlink(data->u.unlink.path); |
245 | ret_value->_errno = errno; | |
246 | ret_value->_error = (ret_value->u.unlink.ret) ? true : false; | |
247 | return ret_value->u.unlink.ret; | |
60b6c79c MD |
248 | } |
249 | ||
250 | static | |
fe9f7760 | 251 | int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 252 | { |
fe9f7760 FD |
253 | ret_value->u.rmdir_recursive.ret = utils_recursive_rmdir(data->u.rmdir_recursive.path); |
254 | ret_value->_errno = errno; | |
255 | ret_value->_error = (ret_value->u.rmdir_recursive.ret) ? true : false; | |
256 | return ret_value->u.rmdir_recursive.ret; | |
7567352f | 257 | } |
df5b86c8 | 258 | |
df458f8c | 259 | #ifdef HAVE_ELF_H |
241e0a5a FD |
260 | static |
261 | int _extract_elf_symbol_offset(struct run_as_data *data, | |
262 | struct run_as_ret *ret_value) | |
263 | { | |
264 | int ret = 0; | |
265 | ret_value->_error = false; | |
266 | ||
267 | ret = lttng_elf_get_symbol_offset(data->fd, | |
268 | data->u.extract_elf_symbol_offset.function, | |
269 | &ret_value->u.extract_elf_symbol_offset.offset); | |
270 | if (ret) { | |
271 | DBG("Failed to extract ELF function offset"); | |
272 | ret_value->_error = true; | |
273 | } | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
0ef03255 FD |
278 | static |
279 | int _extract_sdt_probe_offsets(struct run_as_data *data, | |
280 | struct run_as_ret *ret_value) | |
281 | { | |
282 | int ret = 0; | |
283 | uint64_t *offsets = NULL; | |
284 | uint32_t num_offset; | |
285 | ||
286 | ret_value->_error = false; | |
287 | ||
288 | /* On success, this call allocates the offsets paramater. */ | |
289 | ret = lttng_elf_get_sdt_probe_offsets(data->fd, | |
290 | data->u.extract_sdt_probe_offsets.provider_name, | |
291 | data->u.extract_sdt_probe_offsets.probe_name, | |
292 | &offsets, &num_offset); | |
293 | ||
294 | if (ret) { | |
295 | DBG("Failed to extract SDT probe offsets"); | |
296 | ret_value->_error = true; | |
297 | goto end; | |
298 | } | |
299 | ||
300 | if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) { | |
301 | DBG("Wrong number of probes."); | |
302 | ret = -1; | |
303 | ret_value->_error = true; | |
304 | goto free_offset; | |
305 | } | |
306 | ||
307 | /* Copy the content of the offsets array to the ret struct. */ | |
308 | memcpy(ret_value->u.extract_sdt_probe_offsets.offsets, | |
309 | offsets, num_offset * sizeof(uint64_t)); | |
310 | ||
311 | ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset; | |
312 | ||
313 | free_offset: | |
314 | free(offsets); | |
315 | end: | |
316 | return ret; | |
317 | } | |
df458f8c MJ |
318 | #else |
319 | static | |
320 | int _extract_elf_symbol_offset(struct run_as_data *data, | |
321 | struct run_as_ret *ret_value) | |
322 | { | |
323 | ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET"); | |
324 | return -1; | |
325 | } | |
326 | ||
327 | static | |
328 | int _extract_sdt_probe_offsets(struct run_as_data *data, | |
329 | struct run_as_ret *ret_value) | |
330 | { | |
331 | ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS"); | |
332 | return -1; | |
333 | } | |
334 | #endif | |
241e0a5a | 335 | |
7567352f MD |
336 | static |
337 | run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd) | |
338 | { | |
339 | switch (cmd) { | |
340 | case RUN_AS_MKDIR: | |
a42089bd JG |
341 | case RUN_AS_MKDIRAT: |
342 | return _mkdirat; | |
343 | case RUN_AS_MKDIR_RECURSIVE: | |
344 | case RUN_AS_MKDIRAT_RECURSIVE: | |
345 | return _mkdirat_recursive; | |
7567352f MD |
346 | case RUN_AS_OPEN: |
347 | return _open; | |
348 | case RUN_AS_UNLINK: | |
349 | return _unlink; | |
350 | case RUN_AS_RMDIR_RECURSIVE: | |
351 | return _rmdir_recursive; | |
241e0a5a FD |
352 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
353 | return _extract_elf_symbol_offset; | |
0ef03255 FD |
354 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
355 | return _extract_sdt_probe_offsets; | |
7567352f | 356 | default: |
62a7b8ed | 357 | ERR("Unknown command %d", (int) cmd); |
7567352f MD |
358 | return NULL; |
359 | } | |
60b6c79c MD |
360 | } |
361 | ||
4628484a | 362 | static |
fe9f7760 | 363 | int do_send_fd(int sock, int fd) |
4628484a | 364 | { |
7567352f | 365 | ssize_t len; |
4628484a | 366 | |
7567352f | 367 | if (fd < 0) { |
38069abf JG |
368 | ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd); |
369 | /* Return 0 as this is not a fatal error. */ | |
7567352f MD |
370 | return 0; |
371 | } | |
fe9f7760 FD |
372 | |
373 | len = lttcomm_send_fds_unix_sock(sock, &fd, 1); | |
7567352f MD |
374 | if (len < 0) { |
375 | PERROR("lttcomm_send_fds_unix_sock"); | |
376 | return -1; | |
377 | } | |
7567352f | 378 | return 0; |
4628484a MD |
379 | } |
380 | ||
381 | static | |
fe9f7760 | 382 | int do_recv_fd(int sock, int *fd) |
4628484a | 383 | { |
7567352f | 384 | ssize_t len; |
4628484a | 385 | |
fe9f7760 FD |
386 | len = lttcomm_recv_fds_unix_sock(sock, fd, 1); |
387 | ||
da9ee832 JG |
388 | if (!len) { |
389 | return -1; | |
390 | } else if (len < 0) { | |
7567352f MD |
391 | PERROR("lttcomm_recv_fds_unix_sock"); |
392 | return -1; | |
393 | } | |
38069abf JG |
394 | if (*fd < 0) { |
395 | ERR("Invalid file descriptor received from worker (fd = %i)", *fd); | |
396 | /* Return 0 as this is not a fatal error. */ | |
397 | return 0; | |
398 | } | |
399 | ||
7567352f | 400 | return 0; |
4628484a MD |
401 | } |
402 | ||
fe9f7760 FD |
403 | static |
404 | int send_fd_to_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int fd) | |
405 | { | |
406 | int ret = 0; | |
407 | ||
408 | switch (cmd) { | |
241e0a5a | 409 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
0ef03255 | 410 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
a42089bd JG |
411 | case RUN_AS_MKDIRAT: |
412 | case RUN_AS_MKDIRAT_RECURSIVE: | |
241e0a5a | 413 | break; |
fe9f7760 FD |
414 | default: |
415 | return 0; | |
416 | } | |
417 | ||
38069abf JG |
418 | if (fd < 0) { |
419 | ERR("Refusing to send invalid fd to worker (fd = %i)", fd); | |
420 | return -1; | |
421 | } | |
422 | ||
fe9f7760 FD |
423 | ret = do_send_fd(worker->sockpair[0], fd); |
424 | if (ret < 0) { | |
425 | PERROR("do_send_fd"); | |
426 | ret = -1; | |
427 | } | |
428 | ||
429 | return ret; | |
430 | } | |
431 | ||
432 | static | |
433 | int send_fd_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, int fd) | |
434 | { | |
435 | int ret = 0, ret_close = 0; | |
436 | ||
437 | switch (cmd) { | |
438 | case RUN_AS_OPEN: | |
439 | break; | |
440 | default: | |
441 | return 0; | |
442 | } | |
443 | ||
38069abf JG |
444 | if (fd < 0) { |
445 | DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd); | |
446 | return 0; | |
447 | } | |
fe9f7760 FD |
448 | ret = do_send_fd(worker->sockpair[1], fd); |
449 | if (ret < 0) { | |
450 | PERROR("do_send_fd error"); | |
451 | ret = -1; | |
452 | } | |
453 | ||
454 | ret_close = close(fd); | |
455 | if (ret_close < 0) { | |
456 | PERROR("close"); | |
457 | } | |
38069abf | 458 | |
fe9f7760 FD |
459 | return ret; |
460 | } | |
461 | ||
462 | static | |
463 | int recv_fd_from_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd) | |
464 | { | |
465 | int ret = 0; | |
466 | ||
467 | switch (cmd) { | |
468 | case RUN_AS_OPEN: | |
469 | break; | |
470 | default: | |
471 | return 0; | |
472 | } | |
473 | ||
474 | ret = do_recv_fd(worker->sockpair[0], fd); | |
475 | if (ret < 0) { | |
476 | PERROR("do_recv_fd error"); | |
477 | ret = -1; | |
478 | } | |
479 | ||
480 | return ret; | |
481 | } | |
482 | ||
483 | static | |
484 | int recv_fd_from_master(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd) | |
485 | { | |
486 | int ret = 0; | |
487 | ||
488 | switch (cmd) { | |
241e0a5a | 489 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
0ef03255 | 490 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
a42089bd JG |
491 | case RUN_AS_MKDIRAT: |
492 | case RUN_AS_MKDIRAT_RECURSIVE: | |
241e0a5a | 493 | break; |
a42089bd JG |
494 | case RUN_AS_MKDIR: |
495 | case RUN_AS_MKDIR_RECURSIVE: | |
496 | *fd = AT_FDCWD; | |
497 | /* fall-through */ | |
fe9f7760 FD |
498 | default: |
499 | return 0; | |
500 | } | |
501 | ||
502 | ret = do_recv_fd(worker->sockpair[1], fd); | |
503 | if (ret < 0) { | |
504 | PERROR("do_recv_fd error"); | |
505 | ret = -1; | |
506 | } | |
507 | ||
508 | return ret; | |
509 | } | |
510 | ||
511 | static | |
512 | int cleanup_received_fd(enum run_as_cmd cmd, int fd) | |
513 | { | |
514 | int ret = 0; | |
515 | ||
516 | switch (cmd) { | |
241e0a5a | 517 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
0ef03255 | 518 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
a42089bd JG |
519 | case RUN_AS_MKDIRAT: |
520 | case RUN_AS_MKDIRAT_RECURSIVE: | |
241e0a5a | 521 | break; |
fe9f7760 FD |
522 | default: |
523 | return 0; | |
524 | } | |
525 | ||
9abb7e4a JG |
526 | if (fd < 0) { |
527 | return 0; | |
528 | } | |
fe9f7760 FD |
529 | ret = close(fd); |
530 | if (ret < 0) { | |
531 | PERROR("close error"); | |
532 | ret = -1; | |
533 | } | |
534 | ||
535 | return ret; | |
536 | } | |
0ef03255 | 537 | |
7567352f MD |
538 | /* |
539 | * Return < 0 on error, 0 if OK, 1 on hangup. | |
540 | */ | |
c2b75c49 | 541 | static |
7567352f | 542 | int handle_one_cmd(struct run_as_worker *worker) |
c2b75c49 | 543 | { |
7567352f MD |
544 | int ret = 0; |
545 | struct run_as_data data; | |
546 | ssize_t readlen, writelen; | |
df5b86c8 | 547 | struct run_as_ret sendret; |
7567352f MD |
548 | run_as_fct cmd; |
549 | uid_t prev_euid; | |
550 | ||
ee5fcf1d JG |
551 | memset(&sendret, 0, sizeof(sendret)); |
552 | sendret.fd = -1; | |
553 | ||
fe9f7760 FD |
554 | /* |
555 | * Stage 1: Receive run_as_data struct from the master. | |
556 | * The structure contains the command type and all the parameters needed for | |
557 | * its execution | |
558 | */ | |
7567352f MD |
559 | readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data, |
560 | sizeof(data)); | |
561 | if (readlen == 0) { | |
562 | /* hang up */ | |
563 | ret = 1; | |
564 | goto end; | |
565 | } | |
566 | if (readlen < sizeof(data)) { | |
567 | PERROR("lttcomm_recv_unix_sock error"); | |
568 | ret = -1; | |
569 | goto end; | |
570 | } | |
c2b75c49 | 571 | |
7567352f MD |
572 | cmd = run_as_enum_to_fct(data.cmd); |
573 | if (!cmd) { | |
574 | ret = -1; | |
575 | goto end; | |
576 | } | |
577 | ||
fe9f7760 FD |
578 | /* |
579 | * Stage 2: Receive file descriptor from master. | |
580 | * Some commands need a file descriptor as input so if it's needed we | |
581 | * receive the fd using the Unix socket. | |
582 | */ | |
583 | ret = recv_fd_from_master(worker, data.cmd, &data.fd); | |
584 | if (ret < 0) { | |
585 | PERROR("recv_fd_from_master error"); | |
586 | ret = -1; | |
587 | goto end; | |
588 | } | |
589 | ||
7567352f MD |
590 | prev_euid = getuid(); |
591 | if (data.gid != getegid()) { | |
592 | ret = setegid(data.gid); | |
1576d582 | 593 | if (ret < 0) { |
561f5f2c JG |
594 | sendret._error = true; |
595 | sendret._errno = errno; | |
4c462e79 | 596 | PERROR("setegid"); |
6d73c4ef | 597 | goto write_return; |
1576d582 | 598 | } |
c2b75c49 | 599 | } |
7567352f MD |
600 | if (data.uid != prev_euid) { |
601 | ret = seteuid(data.uid); | |
1576d582 | 602 | if (ret < 0) { |
561f5f2c JG |
603 | sendret._error = true; |
604 | sendret._errno = errno; | |
4c462e79 | 605 | PERROR("seteuid"); |
6d73c4ef | 606 | goto write_return; |
1576d582 | 607 | } |
c2b75c49 | 608 | } |
fe9f7760 | 609 | |
c2b75c49 MD |
610 | /* |
611 | * Also set umask to 0 for mkdir executable bit. | |
612 | */ | |
613 | umask(0); | |
fe9f7760 FD |
614 | |
615 | /* | |
616 | * Stage 3: Execute the command | |
617 | */ | |
618 | ret = (*cmd)(&data, &sendret); | |
619 | if (ret < 0) { | |
620 | DBG("Execution of command returned an error"); | |
621 | } | |
6d73c4ef MD |
622 | |
623 | write_return: | |
fe9f7760 FD |
624 | ret = cleanup_received_fd(data.cmd, data.fd); |
625 | if (ret < 0) { | |
626 | ERR("Error cleaning up FD"); | |
627 | goto end; | |
628 | } | |
629 | ||
630 | /* | |
631 | * Stage 4: Send run_as_ret structure to the master. | |
632 | * This structure contain the return value of the command and the errno. | |
633 | */ | |
7567352f MD |
634 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
635 | sizeof(sendret)); | |
6cd525e8 | 636 | if (writelen < sizeof(sendret)) { |
7567352f MD |
637 | PERROR("lttcomm_send_unix_sock error"); |
638 | ret = -1; | |
639 | goto end; | |
640 | } | |
fe9f7760 FD |
641 | |
642 | /* | |
643 | * Stage 5: Send file descriptor to the master | |
644 | * Some commands return a file descriptor so if it's needed we pass it back | |
645 | * to the master using the Unix socket. | |
646 | */ | |
647 | ret = send_fd_to_master(worker, data.cmd, sendret.fd); | |
648 | if (ret < 0) { | |
649 | DBG("Sending FD to master returned an error"); | |
7567352f MD |
650 | goto end; |
651 | } | |
fe9f7760 | 652 | |
7567352f MD |
653 | if (seteuid(prev_euid) < 0) { |
654 | PERROR("seteuid"); | |
655 | ret = -1; | |
656 | goto end; | |
657 | } | |
658 | ret = 0; | |
659 | end: | |
660 | return ret; | |
661 | } | |
662 | ||
663 | static | |
664 | int run_as_worker(struct run_as_worker *worker) | |
665 | { | |
666 | int ret; | |
667 | ssize_t writelen; | |
668 | struct run_as_ret sendret; | |
669 | size_t proc_orig_len; | |
670 | ||
671 | /* | |
672 | * Initialize worker. Set a different process cmdline. | |
673 | */ | |
674 | proc_orig_len = strlen(worker->procname); | |
675 | memset(worker->procname, 0, proc_orig_len); | |
676 | strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len); | |
677 | ||
e1055edb JG |
678 | ret = lttng_prctl(PR_SET_NAME, |
679 | (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); | |
680 | if (ret && ret != -ENOSYS) { | |
b8090274 JG |
681 | /* Don't fail as this is not essential. */ |
682 | PERROR("prctl PR_SET_NAME"); | |
6cd525e8 | 683 | } |
7567352f | 684 | |
fe9f7760 FD |
685 | memset(&sendret, 0, sizeof(sendret)); |
686 | ||
7567352f MD |
687 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
688 | sizeof(sendret)); | |
689 | if (writelen < sizeof(sendret)) { | |
690 | PERROR("lttcomm_send_unix_sock error"); | |
691 | ret = EXIT_FAILURE; | |
692 | goto end; | |
693 | } | |
694 | ||
695 | for (;;) { | |
696 | ret = handle_one_cmd(worker); | |
697 | if (ret < 0) { | |
698 | ret = EXIT_FAILURE; | |
699 | goto end; | |
700 | } else if (ret > 0) { | |
701 | break; | |
702 | } else { | |
703 | continue; /* Next command. */ | |
704 | } | |
705 | } | |
706 | ret = EXIT_SUCCESS; | |
707 | end: | |
708 | return ret; | |
c2b75c49 MD |
709 | } |
710 | ||
60b6c79c | 711 | static |
7567352f MD |
712 | int run_as_cmd(struct run_as_worker *worker, |
713 | enum run_as_cmd cmd, | |
714 | struct run_as_data *data, | |
fe9f7760 | 715 | struct run_as_ret *ret_value, |
7567352f | 716 | uid_t uid, gid_t gid) |
60b6c79c | 717 | { |
fe9f7760 | 718 | int ret = 0; |
7567352f | 719 | ssize_t readlen, writelen; |
60b6c79c MD |
720 | |
721 | /* | |
722 | * If we are non-root, we can only deal with our own uid. | |
723 | */ | |
724 | if (geteuid() != 0) { | |
725 | if (uid != geteuid()) { | |
fe9f7760 FD |
726 | ret = -1; |
727 | ret_value->_errno = EPERM; | |
60b6c79c | 728 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", |
08797918 | 729 | (int) uid, (int) geteuid()); |
df5b86c8 | 730 | goto end; |
60b6c79c | 731 | } |
60b6c79c MD |
732 | } |
733 | ||
7567352f MD |
734 | data->cmd = cmd; |
735 | data->uid = uid; | |
736 | data->gid = gid; | |
737 | ||
fe9f7760 FD |
738 | /* |
739 | * Stage 1: Send the run_as_data struct to the worker process | |
740 | */ | |
7567352f MD |
741 | writelen = lttcomm_send_unix_sock(worker->sockpair[0], data, |
742 | sizeof(*data)); | |
743 | if (writelen < sizeof(*data)) { | |
744 | PERROR("Error writing message to run_as"); | |
fe9f7760 FD |
745 | ret = -1; |
746 | ret_value->_errno = EIO; | |
60b6c79c | 747 | goto end; |
c2b75c49 | 748 | } |
7567352f | 749 | |
fe9f7760 FD |
750 | /* |
751 | * Stage 2: Send file descriptor to the worker process if needed | |
752 | */ | |
753 | ret = send_fd_to_worker(worker, data->cmd, data->fd); | |
754 | if (ret) { | |
755 | PERROR("do_send_fd error"); | |
756 | ret = -1; | |
757 | ret_value->_errno = EIO; | |
758 | goto end; | |
759 | } | |
760 | ||
761 | /* | |
762 | * Stage 3: Wait for the execution of the command | |
763 | */ | |
764 | ||
765 | /* | |
766 | * Stage 4: Receive the run_as_ret struct containing the return value and | |
767 | * errno | |
768 | */ | |
769 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value, | |
770 | sizeof(*ret_value)); | |
da9ee832 JG |
771 | if (!readlen) { |
772 | ERR("Run-as worker has hung-up during run_as_cmd"); | |
fe9f7760 FD |
773 | ret = -1; |
774 | ret_value->_errno = EIO; | |
da9ee832 | 775 | goto end; |
fe9f7760 | 776 | } else if (readlen < sizeof(*ret_value)) { |
7567352f | 777 | PERROR("Error reading response from run_as"); |
fe9f7760 FD |
778 | ret = -1; |
779 | ret_value->_errno = errno; | |
033b58a7 | 780 | goto end; |
6cd525e8 | 781 | } |
fe9f7760 | 782 | |
38069abf JG |
783 | if (ret_value->_error) { |
784 | /* Skip stage 5 on error as there will be no fd to receive. */ | |
785 | goto end; | |
786 | } | |
787 | ||
fe9f7760 FD |
788 | /* |
789 | * Stage 5: Receive file descriptor if needed | |
790 | */ | |
791 | ret = recv_fd_from_worker(worker, data->cmd, &ret_value->fd); | |
792 | if (ret < 0) { | |
793 | ERR("Error receiving fd"); | |
794 | ret = -1; | |
795 | ret_value->_errno = EIO; | |
4c462e79 | 796 | } |
7567352f | 797 | |
60b6c79c | 798 | end: |
fe9f7760 | 799 | return ret; |
60b6c79c MD |
800 | } |
801 | ||
2d85a600 | 802 | /* |
7567352f | 803 | * This is for debugging ONLY and should not be considered secure. |
2d85a600 MD |
804 | */ |
805 | static | |
fe9f7760 FD |
806 | int run_as_noworker(enum run_as_cmd cmd, |
807 | struct run_as_data *data, struct run_as_ret *ret_value, | |
808 | uid_t uid, gid_t gid) | |
2d85a600 | 809 | { |
df5b86c8 | 810 | int ret, saved_errno; |
5b73926f | 811 | mode_t old_mask; |
7567352f | 812 | run_as_fct fct; |
5b73926f | 813 | |
7567352f MD |
814 | fct = run_as_enum_to_fct(cmd); |
815 | if (!fct) { | |
816 | errno = -ENOSYS; | |
817 | ret = -1; | |
818 | goto end; | |
819 | } | |
5b73926f | 820 | old_mask = umask(0); |
fe9f7760 FD |
821 | ret = fct(data, ret_value); |
822 | saved_errno = ret_value->_errno; | |
5b73926f | 823 | umask(old_mask); |
df5b86c8 | 824 | errno = saved_errno; |
7567352f | 825 | end: |
5b73926f | 826 | return ret; |
2d85a600 MD |
827 | } |
828 | ||
cd9251b8 JG |
829 | static |
830 | int reset_sighandler(void) | |
831 | { | |
832 | int sig; | |
833 | ||
834 | DBG("Resetting run_as worker signal handlers to default"); | |
835 | for (sig = 1; sig <= 31; sig++) { | |
836 | (void) signal(sig, SIG_DFL); | |
837 | } | |
838 | return 0; | |
839 | } | |
840 | ||
841 | static | |
842 | void worker_sighandler(int sig) | |
843 | { | |
844 | const char *signame; | |
845 | ||
846 | /* | |
847 | * The worker will inherit its parent's signals since they are part of | |
848 | * the same process group. However, in the case of SIGINT and SIGTERM, | |
849 | * we want to give the worker a chance to teardown gracefully when its | |
850 | * parent closes the command socket. | |
851 | */ | |
852 | switch (sig) { | |
853 | case SIGINT: | |
854 | signame = "SIGINT"; | |
855 | break; | |
856 | case SIGTERM: | |
857 | signame = "SIGTERM"; | |
858 | break; | |
859 | default: | |
860 | signame = NULL; | |
861 | } | |
862 | ||
863 | if (signame) { | |
864 | DBG("run_as worker received signal %s", signame); | |
865 | } else { | |
866 | DBG("run_as_worker received signal %d", sig); | |
867 | } | |
868 | } | |
869 | ||
870 | static | |
871 | int set_worker_sighandlers(void) | |
872 | { | |
873 | int ret = 0; | |
874 | sigset_t sigset; | |
875 | struct sigaction sa; | |
876 | ||
877 | if ((ret = sigemptyset(&sigset)) < 0) { | |
878 | PERROR("sigemptyset"); | |
879 | goto end; | |
880 | } | |
881 | ||
882 | sa.sa_handler = worker_sighandler; | |
883 | sa.sa_mask = sigset; | |
884 | sa.sa_flags = 0; | |
885 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
886 | PERROR("sigaction SIGINT"); | |
887 | goto end; | |
888 | } | |
889 | ||
890 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
891 | PERROR("sigaction SIGTERM"); | |
892 | goto end; | |
893 | } | |
894 | ||
895 | DBG("run_as signal handler set for SIGTERM and SIGINT"); | |
896 | end: | |
897 | return ret; | |
898 | } | |
899 | ||
900 | static | |
5bedd986 JG |
901 | int run_as_create_worker_no_lock(const char *procname, |
902 | post_fork_cleanup_cb clean_up_func, | |
903 | void *clean_up_user_data) | |
cd9251b8 JG |
904 | { |
905 | pid_t pid; | |
906 | int i, ret = 0; | |
907 | ssize_t readlen; | |
908 | struct run_as_ret recvret; | |
909 | struct run_as_worker *worker; | |
910 | ||
911 | assert(!global_worker); | |
912 | if (!use_clone()) { | |
913 | /* | |
914 | * Don't initialize a worker, all run_as tasks will be performed | |
915 | * in the current process. | |
916 | */ | |
917 | ret = 0; | |
918 | goto end; | |
919 | } | |
920 | worker = zmalloc(sizeof(*worker)); | |
921 | if (!worker) { | |
922 | ret = -ENOMEM; | |
923 | goto end; | |
924 | } | |
925 | worker->procname = strdup(procname); | |
926 | if (!worker->procname) { | |
927 | ret = -ENOMEM; | |
8b4bc28a | 928 | goto error_procname_alloc; |
cd9251b8 JG |
929 | } |
930 | /* Create unix socket. */ | |
931 | if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) { | |
932 | ret = -1; | |
933 | goto error_sock; | |
934 | } | |
935 | ||
936 | /* Fork worker. */ | |
937 | pid = fork(); | |
938 | if (pid < 0) { | |
939 | PERROR("fork"); | |
940 | ret = -1; | |
941 | goto error_fork; | |
942 | } else if (pid == 0) { | |
943 | /* Child */ | |
944 | ||
945 | reset_sighandler(); | |
946 | ||
947 | set_worker_sighandlers(); | |
5bedd986 JG |
948 | if (clean_up_func) { |
949 | if (clean_up_func(clean_up_user_data) < 0) { | |
950 | ERR("Run-as post-fork clean-up failed, exiting."); | |
951 | exit(EXIT_FAILURE); | |
952 | } | |
953 | } | |
cd9251b8 JG |
954 | |
955 | /* Just close, no shutdown. */ | |
956 | if (close(worker->sockpair[0])) { | |
957 | PERROR("close"); | |
958 | exit(EXIT_FAILURE); | |
959 | } | |
960 | ||
961 | /* | |
962 | * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1] | |
963 | * Sockpair[1] is used as a control channel with the master | |
964 | */ | |
965 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
966 | if (i != worker->sockpair[1]) { | |
967 | (void) close(i); | |
968 | } | |
969 | } | |
970 | ||
971 | worker->sockpair[0] = -1; | |
972 | ret = run_as_worker(worker); | |
973 | if (lttcomm_close_unix_sock(worker->sockpair[1])) { | |
974 | PERROR("close"); | |
975 | ret = -1; | |
976 | } | |
977 | worker->sockpair[1] = -1; | |
547aabb4 JG |
978 | free(worker->procname); |
979 | free(worker); | |
cd9251b8 JG |
980 | LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret); |
981 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
982 | } else { | |
983 | /* Parent */ | |
984 | ||
985 | /* Just close, no shutdown. */ | |
986 | if (close(worker->sockpair[1])) { | |
987 | PERROR("close"); | |
988 | ret = -1; | |
989 | goto error_fork; | |
990 | } | |
991 | worker->sockpair[1] = -1; | |
992 | worker->pid = pid; | |
993 | /* Wait for worker to become ready. */ | |
994 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], | |
995 | &recvret, sizeof(recvret)); | |
996 | if (readlen < sizeof(recvret)) { | |
997 | ERR("readlen: %zd", readlen); | |
998 | PERROR("Error reading response from run_as at creation"); | |
999 | ret = -1; | |
1000 | goto error_fork; | |
1001 | } | |
1002 | global_worker = worker; | |
1003 | } | |
1004 | end: | |
1005 | return ret; | |
1006 | ||
1007 | /* Error handling. */ | |
1008 | error_fork: | |
1009 | for (i = 0; i < 2; i++) { | |
1010 | if (worker->sockpair[i] < 0) { | |
1011 | continue; | |
1012 | } | |
1013 | if (lttcomm_close_unix_sock(worker->sockpair[i])) { | |
1014 | PERROR("close"); | |
1015 | } | |
1016 | worker->sockpair[i] = -1; | |
1017 | } | |
1018 | error_sock: | |
8b4bc28a FD |
1019 | free(worker->procname); |
1020 | error_procname_alloc: | |
cd9251b8 JG |
1021 | free(worker); |
1022 | return ret; | |
1023 | } | |
1024 | ||
3340c2a1 JR |
1025 | static |
1026 | void run_as_destroy_worker_no_lock(void) | |
1027 | { | |
1028 | struct run_as_worker *worker = global_worker; | |
1029 | ||
1030 | DBG("Destroying run_as worker"); | |
1031 | if (!worker) { | |
1032 | return; | |
1033 | } | |
1034 | /* Close unix socket */ | |
1035 | DBG("Closing run_as worker socket"); | |
1036 | if (lttcomm_close_unix_sock(worker->sockpair[0])) { | |
1037 | PERROR("close"); | |
1038 | } | |
1039 | worker->sockpair[0] = -1; | |
1040 | /* Wait for worker. */ | |
1041 | for (;;) { | |
1042 | int status; | |
1043 | pid_t wait_ret; | |
1044 | ||
1045 | wait_ret = waitpid(worker->pid, &status, 0); | |
1046 | if (wait_ret < 0) { | |
1047 | if (errno == EINTR) { | |
1048 | continue; | |
1049 | } | |
1050 | PERROR("waitpid"); | |
1051 | break; | |
1052 | } | |
1053 | ||
1054 | if (WIFEXITED(status)) { | |
1055 | LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR, | |
1056 | DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d", | |
1057 | WEXITSTATUS(status)); | |
1058 | break; | |
1059 | } else if (WIFSIGNALED(status)) { | |
1060 | ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d", | |
1061 | WTERMSIG(status)); | |
1062 | break; | |
1063 | } | |
1064 | } | |
1065 | free(worker->procname); | |
1066 | free(worker); | |
1067 | global_worker = NULL; | |
1068 | } | |
1069 | ||
2d85a600 | 1070 | static |
fe9f7760 | 1071 | int run_as_restart_worker(struct run_as_worker *worker) |
2d85a600 | 1072 | { |
fe9f7760 FD |
1073 | int ret = 0; |
1074 | char *procname = NULL; | |
1075 | ||
1076 | procname = worker->procname; | |
1077 | ||
1078 | /* Close socket to run_as worker process and clean up the zombie process */ | |
3340c2a1 | 1079 | run_as_destroy_worker_no_lock(); |
fe9f7760 FD |
1080 | |
1081 | /* Create a new run_as worker process*/ | |
5bedd986 | 1082 | ret = run_as_create_worker_no_lock(procname, NULL, NULL); |
fe9f7760 FD |
1083 | if (ret < 0 ) { |
1084 | ERR("Restarting the worker process failed"); | |
1085 | ret = -1; | |
1086 | goto err; | |
1087 | } | |
1088 | err: | |
1089 | return ret; | |
1090 | } | |
1091 | ||
1092 | static | |
1093 | int run_as(enum run_as_cmd cmd, struct run_as_data *data, | |
1094 | struct run_as_ret *ret_value, uid_t uid, gid_t gid) | |
1095 | { | |
1096 | int ret, saved_errno; | |
7567352f | 1097 | |
cd9251b8 | 1098 | pthread_mutex_lock(&worker_lock); |
749b7a0c | 1099 | if (use_clone()) { |
7567352f | 1100 | DBG("Using run_as worker"); |
cd9251b8 | 1101 | |
749b7a0c | 1102 | assert(global_worker); |
749b7a0c | 1103 | |
fe9f7760 FD |
1104 | ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid); |
1105 | saved_errno = ret_value->_errno; | |
1106 | ||
fe9f7760 FD |
1107 | /* |
1108 | * If the worker thread crashed the errno is set to EIO. we log | |
1109 | * the error and start a new worker process. | |
1110 | */ | |
1111 | if (ret == -1 && saved_errno == EIO) { | |
1112 | DBG("Socket closed unexpectedly... " | |
1113 | "Restarting the worker process"); | |
1114 | ret = run_as_restart_worker(global_worker); | |
1115 | ||
1116 | if (ret == -1) { | |
1117 | ERR("Failed to restart worker process."); | |
1118 | goto err; | |
1119 | } | |
1120 | } | |
2d85a600 | 1121 | } else { |
7567352f | 1122 | DBG("Using run_as without worker"); |
fe9f7760 | 1123 | ret = run_as_noworker(cmd, data, ret_value, uid, gid); |
2d85a600 | 1124 | } |
fe9f7760 | 1125 | err: |
cd9251b8 | 1126 | pthread_mutex_unlock(&worker_lock); |
7567352f | 1127 | return ret; |
2d85a600 MD |
1128 | } |
1129 | ||
90e535ef | 1130 | LTTNG_HIDDEN |
e11d277b | 1131 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 1132 | { |
a42089bd JG |
1133 | return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid); |
1134 | } | |
1135 | ||
1136 | LTTNG_HIDDEN | |
1137 | int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode, | |
1138 | uid_t uid, gid_t gid) | |
1139 | { | |
1140 | int ret; | |
7567352f | 1141 | struct run_as_data data; |
a42089bd | 1142 | struct run_as_ret run_as_ret; |
60b6c79c | 1143 | |
8446e5eb | 1144 | memset(&data, 0, sizeof(data)); |
a42089bd JG |
1145 | memset(&run_as_ret, 0, sizeof(run_as_ret)); |
1146 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", | |
1147 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1148 | path, (int) mode, (int) uid, (int) gid); |
a42089bd JG |
1149 | ret = lttng_strncpy(data.u.mkdirat.path, path, |
1150 | sizeof(data.u.mkdirat.path)); | |
1151 | if (ret) { | |
1152 | ERR("Failed to copy path argument of mkdirat recursive command"); | |
1153 | goto error; | |
1154 | } | |
1155 | data.u.mkdirat.path[PATH_MAX - 1] = '\0'; | |
1156 | data.u.mkdirat.mode = mode; | |
1157 | data.fd = dirfd; | |
1158 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE, | |
1159 | &data, &run_as_ret, uid, gid); | |
1160 | errno = run_as_ret._errno; | |
1161 | ret = run_as_ret.u.mkdirat.ret; | |
1162 | error: | |
1163 | return ret; | |
60b6c79c MD |
1164 | } |
1165 | ||
90e535ef | 1166 | LTTNG_HIDDEN |
e11d277b | 1167 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 1168 | { |
a42089bd JG |
1169 | return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid); |
1170 | } | |
1171 | ||
1172 | LTTNG_HIDDEN | |
1173 | int run_as_mkdirat(int dirfd, const char *path, mode_t mode, | |
1174 | uid_t uid, gid_t gid) | |
1175 | { | |
1176 | int ret; | |
7567352f | 1177 | struct run_as_data data; |
a42089bd | 1178 | struct run_as_ret run_as_ret; |
60b6c79c | 1179 | |
8446e5eb | 1180 | memset(&data, 0, sizeof(data)); |
a42089bd | 1181 | memset(&run_as_ret, 0, sizeof(run_as_ret)); |
fe9f7760 | 1182 | |
a42089bd JG |
1183 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", |
1184 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1185 | path, (int) mode, (int) uid, (int) gid); |
a42089bd JG |
1186 | ret = lttng_strncpy(data.u.mkdirat.path, path, |
1187 | sizeof(data.u.mkdirat.path)); | |
1188 | if (ret) { | |
1189 | ERR("Failed to copy path argument of mkdirat command"); | |
1190 | goto error; | |
1191 | } | |
1192 | data.u.mkdirat.path[PATH_MAX - 1] = '\0'; | |
1193 | data.u.mkdirat.mode = mode; | |
1194 | data.fd = dirfd; | |
1195 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT, | |
1196 | &data, &run_as_ret, uid, gid); | |
1197 | errno = run_as_ret._errno; | |
1198 | ret = run_as_ret._errno; | |
1199 | error: | |
1200 | return ret; | |
60b6c79c MD |
1201 | } |
1202 | ||
90e535ef | 1203 | LTTNG_HIDDEN |
e11d277b | 1204 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 1205 | { |
7567352f | 1206 | struct run_as_data data; |
fe9f7760 | 1207 | struct run_as_ret ret; |
c2b75c49 | 1208 | |
8446e5eb | 1209 | memset(&data, 0, sizeof(data)); |
fe9f7760 FD |
1210 | memset(&ret, 0, sizeof(ret)); |
1211 | ||
47fb7563 | 1212 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
08797918 | 1213 | path, flags, (int) mode, (int) uid, (int) gid); |
7567352f MD |
1214 | strncpy(data.u.open.path, path, PATH_MAX - 1); |
1215 | data.u.open.path[PATH_MAX - 1] = '\0'; | |
1216 | data.u.open.flags = flags; | |
1217 | data.u.open.mode = mode; | |
fe9f7760 FD |
1218 | run_as(RUN_AS_OPEN, &data, &ret, uid, gid); |
1219 | errno = ret._errno; | |
1220 | ret.u.open.ret = ret.fd; | |
1221 | return ret.u.open.ret; | |
60b6c79c | 1222 | } |
4628484a MD |
1223 | |
1224 | LTTNG_HIDDEN | |
1225 | int run_as_unlink(const char *path, uid_t uid, gid_t gid) | |
1226 | { | |
7567352f | 1227 | struct run_as_data data; |
fe9f7760 | 1228 | struct run_as_ret ret; |
4628484a | 1229 | |
8446e5eb | 1230 | memset(&data, 0, sizeof(data)); |
fe9f7760 FD |
1231 | memset(&ret, 0, sizeof(ret)); |
1232 | ||
4628484a | 1233 | DBG3("unlink() %s with for uid %d and gid %d", |
08797918 | 1234 | path, (int) uid, (int) gid); |
7567352f MD |
1235 | strncpy(data.u.unlink.path, path, PATH_MAX - 1); |
1236 | data.u.unlink.path[PATH_MAX - 1] = '\0'; | |
fe9f7760 FD |
1237 | run_as(RUN_AS_UNLINK, &data, &ret, uid, gid); |
1238 | errno = ret._errno; | |
1239 | return ret.u.unlink.ret; | |
4628484a MD |
1240 | } |
1241 | ||
1242 | LTTNG_HIDDEN | |
7567352f | 1243 | int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid) |
4628484a | 1244 | { |
7567352f | 1245 | struct run_as_data data; |
fe9f7760 FD |
1246 | struct run_as_ret ret; |
1247 | ||
1248 | memset(&data, 0, sizeof(data)); | |
1249 | memset(&ret, 0, sizeof(ret)); | |
4628484a | 1250 | |
7567352f | 1251 | DBG3("rmdir_recursive() %s with for uid %d and gid %d", |
08797918 | 1252 | path, (int) uid, (int) gid); |
7567352f MD |
1253 | strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1); |
1254 | data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0'; | |
fe9f7760 FD |
1255 | run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid); |
1256 | errno = ret._errno; | |
1257 | return ret.u.rmdir_recursive.ret; | |
7567352f MD |
1258 | } |
1259 | ||
241e0a5a FD |
1260 | LTTNG_HIDDEN |
1261 | int run_as_extract_elf_symbol_offset(int fd, const char* function, | |
1262 | uid_t uid, gid_t gid, uint64_t *offset) | |
1263 | { | |
1264 | struct run_as_data data; | |
1265 | struct run_as_ret ret; | |
1266 | ||
f726677b JG |
1267 | memset(&data, 0, sizeof(data)); |
1268 | memset(&ret, 0, sizeof(ret)); | |
1269 | ||
241e0a5a FD |
1270 | DBG3("extract_elf_symbol_offset() on fd=%d and function=%s " |
1271 | "with for uid %d and gid %d", fd, function, (int) uid, (int) gid); | |
1272 | ||
1273 | data.fd = fd; | |
1274 | ||
1275 | strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1); | |
1276 | ||
1277 | data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
1278 | ||
1279 | run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid); | |
1280 | ||
1281 | errno = ret._errno; | |
1282 | ||
1283 | if (ret._error) { | |
1284 | return -1; | |
1285 | } | |
1286 | ||
1287 | *offset = ret.u.extract_elf_symbol_offset.offset; | |
1288 | return 0; | |
1289 | } | |
1290 | ||
0ef03255 FD |
1291 | LTTNG_HIDDEN |
1292 | int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name, | |
1293 | const char* probe_name, uid_t uid, gid_t gid, | |
1294 | uint64_t **offsets, uint32_t *num_offset) | |
1295 | { | |
1296 | struct run_as_data data; | |
1297 | struct run_as_ret ret; | |
1298 | ||
f726677b JG |
1299 | memset(&data, 0, sizeof(data)); |
1300 | memset(&ret, 0, sizeof(ret)); | |
1301 | ||
0ef03255 FD |
1302 | DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and " |
1303 | "provider_name=%s with for uid %d and gid %d", fd, probe_name, | |
1304 | provider_name, (int) uid, (int) gid); | |
1305 | ||
1306 | data.fd = fd; | |
1307 | ||
1308 | strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1); | |
1309 | strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1); | |
1310 | ||
1311 | data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
1312 | data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
1313 | ||
1314 | run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid); | |
1315 | ||
1316 | errno = ret._errno; | |
1317 | ||
1318 | if (ret._error) { | |
1319 | return -1; | |
1320 | } | |
1321 | ||
1322 | *num_offset = ret.u.extract_sdt_probe_offsets.num_offset; | |
1323 | ||
1324 | *offsets = zmalloc(*num_offset * sizeof(uint64_t)); | |
1325 | if (!*offsets) { | |
1326 | return -ENOMEM; | |
1327 | } | |
1328 | ||
1329 | memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t)); | |
1330 | return 0; | |
1331 | } | |
1332 | ||
7567352f | 1333 | LTTNG_HIDDEN |
5bedd986 JG |
1334 | int run_as_create_worker(const char *procname, |
1335 | post_fork_cleanup_cb clean_up_func, | |
1336 | void *clean_up_user_data) | |
7567352f | 1337 | { |
cd9251b8 | 1338 | int ret; |
7567352f | 1339 | |
749b7a0c | 1340 | pthread_mutex_lock(&worker_lock); |
5bedd986 JG |
1341 | ret = run_as_create_worker_no_lock(procname, clean_up_func, |
1342 | clean_up_user_data); | |
749b7a0c | 1343 | pthread_mutex_unlock(&worker_lock); |
7567352f MD |
1344 | return ret; |
1345 | } | |
1346 | ||
1347 | LTTNG_HIDDEN | |
1348 | void run_as_destroy_worker(void) | |
1349 | { | |
749b7a0c | 1350 | pthread_mutex_lock(&worker_lock); |
3340c2a1 | 1351 | run_as_destroy_worker_no_lock(); |
749b7a0c | 1352 | pthread_mutex_unlock(&worker_lock); |
4628484a | 1353 | } |