Commit | Line | Data |
---|---|---|
60b6c79c | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
60b6c79c | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
60b6c79c | 7 | * |
60b6c79c MD |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
60b6c79c MD |
11 | #include <errno.h> |
12 | #include <limits.h> | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <sys/wait.h> | |
17 | #include <sys/types.h> | |
18 | #include <sys/stat.h> | |
19 | #include <unistd.h> | |
20 | #include <fcntl.h> | |
c2b75c49 | 21 | #include <sched.h> |
0452bf08 | 22 | #include <signal.h> |
749b7a0c | 23 | #include <assert.h> |
e1055edb | 24 | #include <signal.h> |
60b6c79c | 25 | |
0ef03255 | 26 | #include <common/lttng-kernel.h> |
90e535ef | 27 | #include <common/common.h> |
3fd15a74 | 28 | #include <common/utils.h> |
e8fa9fb0 | 29 | #include <common/compat/getenv.h> |
e1055edb | 30 | #include <common/compat/prctl.h> |
2038dd6c JG |
31 | #include <common/unix.h> |
32 | #include <common/defaults.h> | |
241e0a5a FD |
33 | #include <common/lttng-elf.h> |
34 | ||
35 | #include <lttng/constant.h> | |
60b6c79c | 36 | |
0857097f DG |
37 | #include "runas.h" |
38 | ||
7567352f | 39 | struct run_as_data; |
fe9f7760 FD |
40 | struct run_as_ret; |
41 | typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value); | |
c2b75c49 | 42 | |
93bed9fe JG |
43 | enum run_as_cmd { |
44 | RUN_AS_MKDIR, | |
45 | RUN_AS_MKDIRAT, | |
46 | RUN_AS_MKDIR_RECURSIVE, | |
47 | RUN_AS_MKDIRAT_RECURSIVE, | |
48 | RUN_AS_OPEN, | |
49 | RUN_AS_OPENAT, | |
50 | RUN_AS_UNLINK, | |
51 | RUN_AS_UNLINKAT, | |
52 | RUN_AS_RMDIR, | |
53 | RUN_AS_RMDIRAT, | |
54 | RUN_AS_RMDIR_RECURSIVE, | |
55 | RUN_AS_RMDIRAT_RECURSIVE, | |
56 | RUN_AS_RENAME, | |
57 | RUN_AS_RENAMEAT, | |
58 | RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, | |
59 | RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, | |
60b6c79c MD |
60 | }; |
61 | ||
93bed9fe JG |
62 | struct run_as_mkdir_data { |
63 | int dirfd; | |
64 | char path[LTTNG_PATH_MAX]; | |
65 | mode_t mode; | |
66 | } LTTNG_PACKED; | |
67 | ||
e11d277b | 68 | struct run_as_open_data { |
93bed9fe JG |
69 | int dirfd; |
70 | char path[LTTNG_PATH_MAX]; | |
60b6c79c MD |
71 | int flags; |
72 | mode_t mode; | |
93bed9fe | 73 | } LTTNG_PACKED; |
60b6c79c | 74 | |
4628484a | 75 | struct run_as_unlink_data { |
93bed9fe JG |
76 | int dirfd; |
77 | char path[LTTNG_PATH_MAX]; | |
78 | } LTTNG_PACKED; | |
4628484a | 79 | |
93bed9fe JG |
80 | struct run_as_rmdir_data { |
81 | int dirfd; | |
82 | char path[LTTNG_PATH_MAX]; | |
f75c5439 | 83 | int flags; /* enum lttng_directory_handle_rmdir_recursive_flags */ |
93bed9fe | 84 | } LTTNG_PACKED; |
7567352f | 85 | |
241e0a5a | 86 | struct run_as_extract_elf_symbol_offset_data { |
93bed9fe | 87 | int fd; |
241e0a5a | 88 | char function[LTTNG_SYMBOL_NAME_LEN]; |
93bed9fe | 89 | } LTTNG_PACKED; |
241e0a5a | 90 | |
0ef03255 | 91 | struct run_as_extract_sdt_probe_offsets_data { |
93bed9fe | 92 | int fd; |
0ef03255 FD |
93 | char probe_name[LTTNG_SYMBOL_NAME_LEN]; |
94 | char provider_name[LTTNG_SYMBOL_NAME_LEN]; | |
93bed9fe | 95 | } LTTNG_PACKED; |
0ef03255 | 96 | |
93bed9fe JG |
97 | struct run_as_rename_data { |
98 | /* | |
99 | * [0] = old_dirfd | |
100 | * [1] = new_dirfd | |
101 | */ | |
102 | int dirfds[2]; | |
103 | char old_path[LTTNG_PATH_MAX]; | |
104 | char new_path[LTTNG_PATH_MAX]; | |
105 | } LTTNG_PACKED; | |
fe9f7760 FD |
106 | |
107 | struct run_as_open_ret { | |
93bed9fe JG |
108 | int fd; |
109 | } LTTNG_PACKED; | |
fe9f7760 | 110 | |
241e0a5a FD |
111 | struct run_as_extract_elf_symbol_offset_ret { |
112 | uint64_t offset; | |
93bed9fe | 113 | } LTTNG_PACKED; |
241e0a5a | 114 | |
0ef03255 FD |
115 | struct run_as_extract_sdt_probe_offsets_ret { |
116 | uint32_t num_offset; | |
117 | uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM]; | |
93bed9fe | 118 | } LTTNG_PACKED; |
7567352f MD |
119 | |
120 | struct run_as_data { | |
121 | enum run_as_cmd cmd; | |
122 | union { | |
93bed9fe | 123 | struct run_as_mkdir_data mkdir; |
7567352f MD |
124 | struct run_as_open_data open; |
125 | struct run_as_unlink_data unlink; | |
93bed9fe JG |
126 | struct run_as_rmdir_data rmdir; |
127 | struct run_as_rename_data rename; | |
241e0a5a | 128 | struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset; |
0ef03255 | 129 | struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets; |
7567352f MD |
130 | } u; |
131 | uid_t uid; | |
132 | gid_t gid; | |
93bed9fe | 133 | } LTTNG_PACKED; |
4628484a | 134 | |
fe9f7760 FD |
135 | /* |
136 | * The run_as_ret structure holds the returned value and status of the command. | |
137 | * | |
138 | * The `u` union field holds the return value of the command; in most cases it | |
139 | * represents the success or the failure of the command. In more complex | |
140 | * commands, it holds a computed value. | |
141 | * | |
142 | * The _errno field is the errno recorded after the execution of the command. | |
143 | * | |
144 | * The _error fields is used the signify that return status of the command. For | |
145 | * simple commands returning `int` the _error field will be the same as the | |
146 | * ret_int field. In complex commands, it signify the success or failure of the | |
147 | * command. | |
148 | * | |
149 | */ | |
df5b86c8 | 150 | struct run_as_ret { |
fe9f7760 | 151 | union { |
93bed9fe | 152 | int ret; |
fe9f7760 | 153 | struct run_as_open_ret open; |
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; |
93bed9fe JG |
159 | } LTTNG_PACKED; |
160 | ||
161 | #define COMMAND_IN_FDS(data_ptr) ({ \ | |
162 | int *fds = NULL; \ | |
163 | if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \ | |
164 | fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \ | |
165 | } \ | |
166 | fds; \ | |
167 | }) | |
168 | ||
169 | #define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \ | |
170 | int *fds = NULL; \ | |
171 | if (command_properties[cmd].out_fds_offset != -1) { \ | |
172 | fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \ | |
173 | } \ | |
174 | fds; \ | |
175 | }) | |
176 | ||
177 | #define COMMAND_IN_FD_COUNT(data_ptr) ({ \ | |
178 | command_properties[data_ptr->cmd].in_fd_count; \ | |
179 | }) | |
180 | ||
181 | #define COMMAND_OUT_FD_COUNT(cmd) ({ \ | |
182 | command_properties[cmd].out_fd_count; \ | |
183 | }) | |
184 | ||
185 | #define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd | |
186 | ||
187 | struct run_as_command_properties { | |
188 | /* Set to -1 when not applicable. */ | |
189 | ptrdiff_t in_fds_offset, out_fds_offset; | |
190 | unsigned int in_fd_count, out_fd_count; | |
191 | bool use_cwd_fd; | |
192 | }; | |
193 | ||
194 | static const struct run_as_command_properties command_properties[] = { | |
195 | [RUN_AS_MKDIR] = { | |
196 | .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd), | |
197 | .in_fd_count = 1, | |
198 | .out_fds_offset = -1, | |
199 | .out_fd_count = 0, | |
200 | .use_cwd_fd = true, | |
201 | }, | |
202 | [RUN_AS_MKDIRAT] = { | |
203 | .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd), | |
204 | .in_fd_count = 1, | |
205 | .out_fds_offset = -1, | |
206 | .out_fd_count = 0, | |
207 | .use_cwd_fd = false, | |
208 | }, | |
209 | [RUN_AS_MKDIR_RECURSIVE] = { | |
210 | .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd), | |
211 | .in_fd_count = 1, | |
212 | .out_fds_offset = -1, | |
213 | .out_fd_count = 0, | |
214 | .use_cwd_fd = true, | |
215 | }, | |
216 | [RUN_AS_MKDIRAT_RECURSIVE] = { | |
217 | .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd), | |
218 | .in_fd_count = 1, | |
219 | .out_fds_offset = -1, | |
220 | .out_fd_count = 0, | |
221 | .use_cwd_fd = false, | |
222 | }, | |
223 | [RUN_AS_OPEN] = { | |
224 | .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd), | |
225 | .in_fd_count = 1, | |
226 | .out_fds_offset = offsetof(struct run_as_ret, u.open.fd), | |
227 | .out_fd_count = 1, | |
228 | .use_cwd_fd = true, | |
229 | }, | |
230 | [RUN_AS_OPENAT] = { | |
231 | .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd), | |
232 | .in_fd_count = 1, | |
233 | .out_fds_offset = offsetof(struct run_as_ret, u.open.fd), | |
234 | .out_fd_count = 1, | |
235 | .use_cwd_fd = false, | |
236 | }, | |
237 | [RUN_AS_UNLINK] = { | |
238 | .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd), | |
239 | .in_fd_count = 1, | |
240 | .out_fds_offset = -1, | |
241 | .out_fd_count = 0, | |
242 | .use_cwd_fd = true, | |
243 | }, | |
244 | [RUN_AS_UNLINKAT] = { | |
245 | .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd), | |
246 | .in_fd_count = 1, | |
247 | .out_fds_offset = -1, | |
248 | .out_fd_count = 0, | |
249 | .use_cwd_fd = false, | |
250 | }, | |
251 | [RUN_AS_RMDIR_RECURSIVE] = { | |
252 | .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd), | |
253 | .in_fd_count = 1, | |
254 | .out_fds_offset = -1, | |
255 | .out_fd_count = 0, | |
256 | .use_cwd_fd = true, | |
257 | }, | |
258 | [RUN_AS_RMDIRAT_RECURSIVE] = { | |
259 | .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd), | |
260 | .in_fd_count = 1, | |
261 | .out_fds_offset = -1, | |
262 | .out_fd_count = 0, | |
263 | .use_cwd_fd = false, | |
264 | }, | |
265 | [RUN_AS_RMDIR] = { | |
266 | .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd), | |
267 | .in_fd_count = 1, | |
268 | .out_fds_offset = -1, | |
269 | .out_fd_count = 0, | |
270 | .use_cwd_fd = true, | |
271 | }, | |
272 | [RUN_AS_RMDIRAT] = { | |
273 | .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd), | |
274 | .in_fd_count = 1, | |
275 | .out_fds_offset = -1, | |
276 | .out_fd_count = 0, | |
277 | .use_cwd_fd = false, | |
278 | }, | |
279 | [RUN_AS_RENAME] = { | |
280 | .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds), | |
281 | .in_fd_count = 2, | |
282 | .out_fds_offset = -1, | |
283 | .out_fd_count = 0, | |
284 | .use_cwd_fd = true, | |
285 | }, | |
286 | [RUN_AS_RENAMEAT] = { | |
287 | .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds), | |
288 | .in_fd_count = 2, | |
289 | .out_fds_offset = -1, | |
290 | .out_fd_count = 0, | |
291 | .use_cwd_fd = false, | |
292 | }, | |
293 | [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET] = { | |
294 | .in_fds_offset = offsetof(struct run_as_data, | |
295 | u.extract_elf_symbol_offset.fd), | |
296 | .in_fd_count = 1, | |
297 | .out_fds_offset = -1, | |
298 | .out_fd_count = 0, | |
299 | .use_cwd_fd = false, | |
300 | }, | |
301 | [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS] = { | |
302 | .in_fds_offset = offsetof(struct run_as_data, | |
303 | u.extract_sdt_probe_offsets.fd), | |
304 | .in_fd_count = 1, | |
305 | .out_fds_offset = -1, | |
306 | .out_fd_count = 0, | |
307 | .use_cwd_fd = false, | |
308 | }, | |
df5b86c8 MD |
309 | }; |
310 | ||
7567352f MD |
311 | struct run_as_worker { |
312 | pid_t pid; /* Worker PID. */ | |
313 | int sockpair[2]; | |
314 | char *procname; | |
315 | }; | |
316 | ||
317 | /* Single global worker per process (for now). */ | |
318 | static struct run_as_worker *global_worker; | |
319 | /* Lock protecting the worker. */ | |
320 | static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER; | |
321 | ||
8f0044bf MD |
322 | #ifdef VALGRIND |
323 | static | |
324 | int use_clone(void) | |
325 | { | |
326 | return 0; | |
327 | } | |
328 | #else | |
329 | static | |
330 | int use_clone(void) | |
331 | { | |
e8fa9fb0 | 332 | return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE"); |
8f0044bf MD |
333 | } |
334 | #endif | |
335 | ||
60b6c79c MD |
336 | /* |
337 | * Create recursively directory using the FULL path. | |
338 | */ | |
339 | static | |
18710679 | 340 | int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 341 | { |
60b6c79c | 342 | const char *path; |
60b6c79c | 343 | mode_t mode; |
cbf53d23 | 344 | struct lttng_directory_handle *handle; |
60b6c79c | 345 | |
93bed9fe JG |
346 | path = data->u.mkdir.path; |
347 | mode = data->u.mkdir.mode; | |
60b6c79c | 348 | |
cbf53d23 JG |
349 | handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd); |
350 | if (!handle) { | |
351 | ret_value->_errno = errno; | |
352 | ret_value->_error = true; | |
353 | ret_value->u.ret = -1; | |
354 | goto end; | |
355 | } | |
87bbb856 | 356 | /* Ownership of dirfd is transferred to the handle. */ |
93bed9fe | 357 | data->u.mkdir.dirfd = -1; |
d77dded2 | 358 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
cbf53d23 JG |
359 | ret_value->u.ret = lttng_directory_handle_create_subdirectory_recursive( |
360 | handle, path, mode); | |
fe9f7760 | 361 | ret_value->_errno = errno; |
93bed9fe | 362 | ret_value->_error = (ret_value->u.ret) ? true : false; |
cbf53d23 JG |
363 | lttng_directory_handle_put(handle); |
364 | end: | |
93bed9fe | 365 | return ret_value->u.ret; |
60b6c79c MD |
366 | } |
367 | ||
368 | static | |
18710679 | 369 | int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 370 | { |
18710679 JG |
371 | const char *path; |
372 | mode_t mode; | |
cbf53d23 | 373 | struct lttng_directory_handle *handle; |
18710679 | 374 | |
93bed9fe JG |
375 | path = data->u.mkdir.path; |
376 | mode = data->u.mkdir.mode; | |
18710679 | 377 | |
cbf53d23 JG |
378 | handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd); |
379 | if (!handle) { | |
380 | ret_value->u.ret = -1; | |
381 | ret_value->_errno = errno; | |
382 | ret_value->_error = true; | |
383 | goto end; | |
384 | } | |
87bbb856 | 385 | /* Ownership of dirfd is transferred to the handle. */ |
93bed9fe | 386 | data->u.mkdir.dirfd = -1; |
18710679 | 387 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
cbf53d23 JG |
388 | ret_value->u.ret = lttng_directory_handle_create_subdirectory( |
389 | handle, path, mode); | |
fe9f7760 | 390 | ret_value->_errno = errno; |
93bed9fe | 391 | ret_value->_error = (ret_value->u.ret) ? true : false; |
cbf53d23 JG |
392 | lttng_directory_handle_put(handle); |
393 | end: | |
93bed9fe | 394 | return ret_value->u.ret; |
7567352f | 395 | } |
7ce36756 | 396 | |
7567352f | 397 | static |
fe9f7760 | 398 | int _open(struct run_as_data *data, struct run_as_ret *ret_value) |
7567352f | 399 | { |
93bed9fe | 400 | int fd; |
cbf53d23 | 401 | struct lttng_directory_handle *handle; |
93bed9fe | 402 | |
cbf53d23 JG |
403 | handle = lttng_directory_handle_create_from_dirfd(data->u.open.dirfd); |
404 | if (!handle) { | |
405 | ret_value->_errno = errno; | |
406 | ret_value->_error = true; | |
407 | ret_value->u.ret = -1; | |
408 | goto end; | |
409 | } | |
93bed9fe JG |
410 | /* Ownership of dirfd is transferred to the handle. */ |
411 | data->u.open.dirfd = -1; | |
412 | ||
cbf53d23 | 413 | fd = lttng_directory_handle_open_file(handle, |
93bed9fe JG |
414 | data->u.open.path, data->u.open.flags, |
415 | data->u.open.mode); | |
416 | if (fd < 0) { | |
417 | ret_value->u.ret = -1; | |
418 | ret_value->u.open.fd = -1; | |
419 | } else { | |
420 | ret_value->u.ret = 0; | |
421 | ret_value->u.open.fd = fd; | |
422 | } | |
423 | ||
fe9f7760 | 424 | ret_value->_errno = errno; |
93bed9fe | 425 | ret_value->_error = fd < 0; |
cbf53d23 JG |
426 | lttng_directory_handle_put(handle); |
427 | end: | |
93bed9fe | 428 | return ret_value->u.ret; |
7567352f MD |
429 | } |
430 | ||
431 | static | |
fe9f7760 | 432 | int _unlink(struct run_as_data *data, struct run_as_ret *ret_value) |
7567352f | 433 | { |
cbf53d23 | 434 | struct lttng_directory_handle *handle; |
93bed9fe | 435 | |
cbf53d23 JG |
436 | handle = lttng_directory_handle_create_from_dirfd(data->u.unlink.dirfd); |
437 | if (!handle) { | |
438 | ret_value->u.ret = -1; | |
439 | ret_value->_errno = errno; | |
440 | ret_value->_error = true; | |
441 | goto end; | |
442 | } | |
93bed9fe JG |
443 | |
444 | /* Ownership of dirfd is transferred to the handle. */ | |
445 | data->u.unlink.dirfd = -1; | |
446 | ||
cbf53d23 | 447 | ret_value->u.ret = lttng_directory_handle_unlink_file(handle, |
93bed9fe JG |
448 | data->u.unlink.path); |
449 | ret_value->_errno = errno; | |
450 | ret_value->_error = (ret_value->u.ret) ? true : false; | |
cbf53d23 JG |
451 | lttng_directory_handle_put(handle); |
452 | end: | |
93bed9fe JG |
453 | return ret_value->u.ret; |
454 | } | |
455 | ||
456 | static | |
457 | int _rmdir(struct run_as_data *data, struct run_as_ret *ret_value) | |
458 | { | |
cbf53d23 | 459 | struct lttng_directory_handle *handle; |
93bed9fe | 460 | |
cbf53d23 JG |
461 | handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd); |
462 | if (!handle) { | |
463 | ret_value->u.ret = -1; | |
464 | ret_value->_errno = errno; | |
465 | ret_value->_error = true; | |
466 | goto end; | |
467 | } | |
93bed9fe JG |
468 | |
469 | /* Ownership of dirfd is transferred to the handle. */ | |
470 | data->u.rmdir.dirfd = -1; | |
471 | ||
472 | ret_value->u.ret = lttng_directory_handle_remove_subdirectory( | |
cbf53d23 | 473 | handle, data->u.rmdir.path); |
fe9f7760 | 474 | ret_value->_errno = errno; |
93bed9fe | 475 | ret_value->_error = (ret_value->u.ret) ? true : false; |
cbf53d23 JG |
476 | lttng_directory_handle_put(handle); |
477 | end: | |
93bed9fe | 478 | return ret_value->u.ret; |
60b6c79c MD |
479 | } |
480 | ||
481 | static | |
fe9f7760 | 482 | int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
60b6c79c | 483 | { |
cbf53d23 | 484 | struct lttng_directory_handle *handle; |
93bed9fe | 485 | |
cbf53d23 JG |
486 | handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd); |
487 | if (!handle) { | |
488 | ret_value->u.ret = -1; | |
489 | ret_value->_errno = errno; | |
490 | ret_value->_error = true; | |
491 | goto end; | |
492 | } | |
93bed9fe JG |
493 | |
494 | /* Ownership of dirfd is transferred to the handle. */ | |
495 | data->u.rmdir.dirfd = -1; | |
496 | ||
497 | ret_value->u.ret = lttng_directory_handle_remove_subdirectory_recursive( | |
cbf53d23 | 498 | handle, data->u.rmdir.path, data->u.rmdir.flags); |
fe9f7760 | 499 | ret_value->_errno = errno; |
93bed9fe | 500 | ret_value->_error = (ret_value->u.ret) ? true : false; |
cbf53d23 JG |
501 | lttng_directory_handle_put(handle); |
502 | end: | |
93bed9fe JG |
503 | return ret_value->u.ret; |
504 | } | |
505 | ||
506 | static | |
507 | int _rename(struct run_as_data *data, struct run_as_ret *ret_value) | |
508 | { | |
509 | const char *old_path, *new_path; | |
cbf53d23 | 510 | struct lttng_directory_handle *old_handle = NULL, *new_handle = NULL; |
93bed9fe JG |
511 | |
512 | old_path = data->u.rename.old_path; | |
513 | new_path = data->u.rename.new_path; | |
514 | ||
cbf53d23 | 515 | old_handle = lttng_directory_handle_create_from_dirfd( |
93bed9fe | 516 | data->u.rename.dirfds[0]); |
cbf53d23 JG |
517 | if (!old_handle) { |
518 | ret_value->u.ret = -1; | |
519 | goto end; | |
520 | } | |
521 | new_handle = lttng_directory_handle_create_from_dirfd( | |
93bed9fe | 522 | data->u.rename.dirfds[1]); |
cbf53d23 JG |
523 | if (!new_handle) { |
524 | ret_value->u.ret = -1; | |
525 | goto end; | |
526 | } | |
93bed9fe JG |
527 | |
528 | /* Ownership of dirfds are transferred to the handles. */ | |
529 | data->u.rename.dirfds[0] = data->u.rename.dirfds[1] = -1; | |
530 | ||
531 | /* Safe to call as we have transitioned to the requested uid/gid. */ | |
532 | ret_value->u.ret = lttng_directory_handle_rename( | |
cbf53d23 JG |
533 | old_handle, old_path, new_handle, new_path); |
534 | end: | |
535 | lttng_directory_handle_put(old_handle); | |
536 | lttng_directory_handle_put(new_handle); | |
93bed9fe JG |
537 | ret_value->_errno = errno; |
538 | ret_value->_error = (ret_value->u.ret) ? true : false; | |
93bed9fe | 539 | return ret_value->u.ret; |
7567352f | 540 | } |
df5b86c8 | 541 | |
b1b34226 | 542 | #ifdef HAVE_ELF_H |
241e0a5a FD |
543 | static |
544 | int _extract_elf_symbol_offset(struct run_as_data *data, | |
545 | struct run_as_ret *ret_value) | |
546 | { | |
547 | int ret = 0; | |
65eefd4c | 548 | uint64_t offset; |
241e0a5a | 549 | |
93bed9fe JG |
550 | ret_value->_error = false; |
551 | ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd, | |
241e0a5a | 552 | data->u.extract_elf_symbol_offset.function, |
65eefd4c | 553 | &offset); |
241e0a5a FD |
554 | if (ret) { |
555 | DBG("Failed to extract ELF function offset"); | |
556 | ret_value->_error = true; | |
557 | } | |
65eefd4c | 558 | ret_value->u.extract_elf_symbol_offset.offset = offset; |
241e0a5a FD |
559 | |
560 | return ret; | |
561 | } | |
562 | ||
0ef03255 FD |
563 | static |
564 | int _extract_sdt_probe_offsets(struct run_as_data *data, | |
565 | struct run_as_ret *ret_value) | |
566 | { | |
567 | int ret = 0; | |
568 | uint64_t *offsets = NULL; | |
569 | uint32_t num_offset; | |
570 | ||
571 | ret_value->_error = false; | |
572 | ||
573 | /* On success, this call allocates the offsets paramater. */ | |
93bed9fe JG |
574 | ret = lttng_elf_get_sdt_probe_offsets( |
575 | data->u.extract_sdt_probe_offsets.fd, | |
0ef03255 FD |
576 | data->u.extract_sdt_probe_offsets.provider_name, |
577 | data->u.extract_sdt_probe_offsets.probe_name, | |
578 | &offsets, &num_offset); | |
579 | ||
580 | if (ret) { | |
581 | DBG("Failed to extract SDT probe offsets"); | |
582 | ret_value->_error = true; | |
583 | goto end; | |
584 | } | |
585 | ||
586 | if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) { | |
587 | DBG("Wrong number of probes."); | |
588 | ret = -1; | |
589 | ret_value->_error = true; | |
590 | goto free_offset; | |
591 | } | |
592 | ||
593 | /* Copy the content of the offsets array to the ret struct. */ | |
594 | memcpy(ret_value->u.extract_sdt_probe_offsets.offsets, | |
595 | offsets, num_offset * sizeof(uint64_t)); | |
596 | ||
597 | ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset; | |
598 | ||
599 | free_offset: | |
600 | free(offsets); | |
601 | end: | |
602 | return ret; | |
603 | } | |
b1b34226 MJ |
604 | #else |
605 | static | |
606 | int _extract_elf_symbol_offset(struct run_as_data *data, | |
607 | struct run_as_ret *ret_value) | |
608 | { | |
609 | ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET"); | |
610 | return -1; | |
611 | } | |
612 | ||
613 | static | |
614 | int _extract_sdt_probe_offsets(struct run_as_data *data, | |
615 | struct run_as_ret *ret_value) | |
616 | { | |
617 | ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS"); | |
618 | return -1; | |
619 | } | |
620 | #endif | |
241e0a5a | 621 | |
7567352f MD |
622 | static |
623 | run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd) | |
624 | { | |
625 | switch (cmd) { | |
626 | case RUN_AS_MKDIR: | |
18710679 JG |
627 | case RUN_AS_MKDIRAT: |
628 | return _mkdirat; | |
629 | case RUN_AS_MKDIR_RECURSIVE: | |
630 | case RUN_AS_MKDIRAT_RECURSIVE: | |
631 | return _mkdirat_recursive; | |
7567352f | 632 | case RUN_AS_OPEN: |
2912cead | 633 | case RUN_AS_OPENAT: |
7567352f MD |
634 | return _open; |
635 | case RUN_AS_UNLINK: | |
2912cead | 636 | case RUN_AS_UNLINKAT: |
7567352f | 637 | return _unlink; |
93bed9fe JG |
638 | case RUN_AS_RMDIR: |
639 | case RUN_AS_RMDIRAT: | |
640 | return _rmdir; | |
7567352f | 641 | case RUN_AS_RMDIR_RECURSIVE: |
93bed9fe | 642 | case RUN_AS_RMDIRAT_RECURSIVE: |
7567352f | 643 | return _rmdir_recursive; |
93bed9fe JG |
644 | case RUN_AS_RENAME: |
645 | case RUN_AS_RENAMEAT: | |
646 | return _rename; | |
241e0a5a FD |
647 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
648 | return _extract_elf_symbol_offset; | |
0ef03255 FD |
649 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
650 | return _extract_sdt_probe_offsets; | |
7567352f | 651 | default: |
62a7b8ed | 652 | ERR("Unknown command %d", (int) cmd); |
7567352f MD |
653 | return NULL; |
654 | } | |
60b6c79c MD |
655 | } |
656 | ||
4628484a | 657 | static |
93bed9fe | 658 | int do_send_fds(int sock, const int *fds, unsigned int fd_count) |
4628484a | 659 | { |
7567352f | 660 | ssize_t len; |
93bed9fe JG |
661 | unsigned int i; |
662 | ||
663 | for (i = 0; i < fd_count; i++) { | |
664 | if (fds[i] < 0) { | |
665 | ERR("Attempt to send invalid file descriptor to master (fd = %i)", | |
666 | fds[i]); | |
667 | /* Return 0 as this is not a fatal error. */ | |
668 | return 0; | |
669 | } | |
4a76dfd3 | 670 | } |
4628484a | 671 | |
4a76dfd3 | 672 | len = lttcomm_send_fds_unix_sock(sock, fds, fd_count); |
93bed9fe | 673 | return len < 0 ? -1 : 0; |
4628484a MD |
674 | } |
675 | ||
676 | static | |
93bed9fe | 677 | int do_recv_fds(int sock, int *fds, unsigned int fd_count) |
4628484a | 678 | { |
93bed9fe JG |
679 | int ret = 0; |
680 | unsigned int i; | |
7567352f | 681 | ssize_t len; |
4628484a | 682 | |
93bed9fe JG |
683 | len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count); |
684 | if (len == 0) { | |
685 | ret = -1; | |
686 | goto end; | |
da9ee832 | 687 | } else if (len < 0) { |
93bed9fe JG |
688 | PERROR("Failed to receive file descriptors from socket"); |
689 | ret = -1; | |
690 | goto end; | |
ca9eb994 JG |
691 | } |
692 | ||
93bed9fe JG |
693 | for (i = 0; i < fd_count; i++) { |
694 | if (fds[i] < 0) { | |
695 | ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]); | |
696 | /* Return 0 as this is not a fatal error. */ | |
697 | } | |
55cb0d6f | 698 | } |
93bed9fe | 699 | end: |
55cb0d6f | 700 | return ret; |
4628484a MD |
701 | } |
702 | ||
fe9f7760 | 703 | static |
93bed9fe JG |
704 | int send_fds_to_worker(const struct run_as_worker *worker, |
705 | const struct run_as_data *data) | |
fe9f7760 FD |
706 | { |
707 | int ret = 0; | |
93bed9fe | 708 | unsigned int i; |
fe9f7760 | 709 | |
93bed9fe JG |
710 | if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) { |
711 | goto end; | |
fe9f7760 FD |
712 | } |
713 | ||
93bed9fe JG |
714 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { |
715 | if (COMMAND_IN_FDS(data)[i] < 0) { | |
716 | ERR("Refusing to send invalid fd to worker (fd = %i)", | |
717 | COMMAND_IN_FDS(data)[i]); | |
718 | ret = -1; | |
719 | goto end; | |
720 | } | |
55cb0d6f | 721 | } |
ca9eb994 | 722 | |
55cb0d6f | 723 | ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data), |
93bed9fe | 724 | COMMAND_IN_FD_COUNT(data)); |
fe9f7760 | 725 | if (ret < 0) { |
93bed9fe | 726 | PERROR("Failed to send file descriptor to run-as worker"); |
fe9f7760 | 727 | ret = -1; |
93bed9fe | 728 | goto end; |
fe9f7760 | 729 | } |
93bed9fe | 730 | end: |
fe9f7760 FD |
731 | return ret; |
732 | } | |
733 | ||
734 | static | |
93bed9fe JG |
735 | int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, |
736 | struct run_as_ret *run_as_ret) | |
fe9f7760 | 737 | { |
93bed9fe JG |
738 | int ret = 0; |
739 | unsigned int i; | |
fe9f7760 | 740 | |
93bed9fe JG |
741 | if (COMMAND_OUT_FD_COUNT(cmd) == 0) { |
742 | goto end; | |
fe9f7760 FD |
743 | } |
744 | ||
93bed9fe JG |
745 | ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret), |
746 | COMMAND_OUT_FD_COUNT(cmd)); | |
fe9f7760 | 747 | if (ret < 0) { |
93bed9fe JG |
748 | PERROR("Failed to send file descriptor to master process"); |
749 | goto end; | |
fe9f7760 FD |
750 | } |
751 | ||
93bed9fe JG |
752 | for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) { |
753 | int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]); | |
ca9eb994 | 754 | |
93bed9fe JG |
755 | if (ret_close < 0) { |
756 | PERROR("Failed to close result file descriptor"); | |
757 | } | |
758 | } | |
759 | end: | |
fe9f7760 FD |
760 | return ret; |
761 | } | |
762 | ||
763 | static | |
93bed9fe JG |
764 | int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd, |
765 | struct run_as_ret *run_as_ret) | |
fe9f7760 FD |
766 | { |
767 | int ret = 0; | |
768 | ||
93bed9fe JG |
769 | if (COMMAND_OUT_FD_COUNT(cmd) == 0) { |
770 | goto end; | |
fe9f7760 FD |
771 | } |
772 | ||
93bed9fe JG |
773 | ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret), |
774 | COMMAND_OUT_FD_COUNT(cmd)); | |
fe9f7760 | 775 | if (ret < 0) { |
93bed9fe | 776 | PERROR("Failed to receive file descriptor from run-as worker"); |
fe9f7760 FD |
777 | ret = -1; |
778 | } | |
93bed9fe | 779 | end: |
fe9f7760 FD |
780 | return ret; |
781 | } | |
782 | ||
783 | static | |
93bed9fe | 784 | int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data) |
fe9f7760 FD |
785 | { |
786 | int ret = 0; | |
787 | ||
93bed9fe JG |
788 | if (COMMAND_USE_CWD_FD(data)) { |
789 | unsigned int i; | |
790 | ||
791 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { | |
792 | COMMAND_IN_FDS(data)[i] = AT_FDCWD; | |
793 | } | |
794 | goto end; | |
fe9f7760 FD |
795 | } |
796 | ||
93bed9fe JG |
797 | ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data), |
798 | COMMAND_IN_FD_COUNT(data)); | |
fe9f7760 | 799 | if (ret < 0) { |
93bed9fe | 800 | PERROR("Failed to receive file descriptors from master process"); |
fe9f7760 FD |
801 | ret = -1; |
802 | } | |
93bed9fe | 803 | end: |
fe9f7760 FD |
804 | return ret; |
805 | } | |
806 | ||
807 | static | |
93bed9fe | 808 | int cleanup_received_fds(struct run_as_data *data) |
fe9f7760 | 809 | { |
93bed9fe | 810 | int ret = 0, i; |
fe9f7760 | 811 | |
93bed9fe JG |
812 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { |
813 | if (COMMAND_IN_FDS(data)[i] == -1) { | |
814 | continue; | |
815 | } | |
816 | ret = close(COMMAND_IN_FDS(data)[i]); | |
817 | if (ret) { | |
818 | PERROR("Failed to close file descriptor received fd in run-as worker"); | |
819 | goto end; | |
820 | } | |
fe9f7760 | 821 | } |
93bed9fe | 822 | end: |
fe9f7760 FD |
823 | return ret; |
824 | } | |
0ef03255 | 825 | |
7567352f MD |
826 | /* |
827 | * Return < 0 on error, 0 if OK, 1 on hangup. | |
828 | */ | |
c2b75c49 | 829 | static |
7567352f | 830 | int handle_one_cmd(struct run_as_worker *worker) |
c2b75c49 | 831 | { |
7567352f | 832 | int ret = 0; |
55cb0d6f JG |
833 | struct run_as_data data = {}; |
834 | ssize_t readlen, writelen; | |
835 | struct run_as_ret sendret = {}; | |
836 | run_as_fct cmd; | |
7567352f MD |
837 | uid_t prev_euid; |
838 | ||
fe9f7760 FD |
839 | /* |
840 | * Stage 1: Receive run_as_data struct from the master. | |
841 | * The structure contains the command type and all the parameters needed for | |
842 | * its execution | |
843 | */ | |
7567352f MD |
844 | readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data, |
845 | sizeof(data)); | |
846 | if (readlen == 0) { | |
847 | /* hang up */ | |
848 | ret = 1; | |
849 | goto end; | |
850 | } | |
851 | if (readlen < sizeof(data)) { | |
852 | PERROR("lttcomm_recv_unix_sock error"); | |
853 | ret = -1; | |
854 | goto end; | |
855 | } | |
c2b75c49 | 856 | |
7567352f MD |
857 | cmd = run_as_enum_to_fct(data.cmd); |
858 | if (!cmd) { | |
859 | ret = -1; | |
860 | goto end; | |
861 | } | |
862 | ||
fe9f7760 FD |
863 | /* |
864 | * Stage 2: Receive file descriptor from master. | |
865 | * Some commands need a file descriptor as input so if it's needed we | |
866 | * receive the fd using the Unix socket. | |
867 | */ | |
93bed9fe | 868 | ret = recv_fds_from_master(worker, &data); |
fe9f7760 FD |
869 | if (ret < 0) { |
870 | PERROR("recv_fd_from_master error"); | |
871 | ret = -1; | |
872 | goto end; | |
873 | } | |
874 | ||
7567352f MD |
875 | prev_euid = getuid(); |
876 | if (data.gid != getegid()) { | |
877 | ret = setegid(data.gid); | |
1576d582 | 878 | if (ret < 0) { |
561f5f2c JG |
879 | sendret._error = true; |
880 | sendret._errno = errno; | |
4c462e79 | 881 | PERROR("setegid"); |
6d73c4ef | 882 | goto write_return; |
1576d582 | 883 | } |
c2b75c49 | 884 | } |
7567352f MD |
885 | if (data.uid != prev_euid) { |
886 | ret = seteuid(data.uid); | |
1576d582 | 887 | if (ret < 0) { |
561f5f2c JG |
888 | sendret._error = true; |
889 | sendret._errno = errno; | |
4c462e79 | 890 | PERROR("seteuid"); |
6d73c4ef | 891 | goto write_return; |
1576d582 | 892 | } |
c2b75c49 | 893 | } |
fe9f7760 | 894 | |
c2b75c49 MD |
895 | /* |
896 | * Also set umask to 0 for mkdir executable bit. | |
897 | */ | |
898 | umask(0); | |
fe9f7760 FD |
899 | |
900 | /* | |
901 | * Stage 3: Execute the command | |
902 | */ | |
903 | ret = (*cmd)(&data, &sendret); | |
904 | if (ret < 0) { | |
905 | DBG("Execution of command returned an error"); | |
906 | } | |
6d73c4ef MD |
907 | |
908 | write_return: | |
93bed9fe | 909 | ret = cleanup_received_fds(&data); |
fe9f7760 FD |
910 | if (ret < 0) { |
911 | ERR("Error cleaning up FD"); | |
912 | goto end; | |
913 | } | |
914 | ||
915 | /* | |
916 | * Stage 4: Send run_as_ret structure to the master. | |
917 | * This structure contain the return value of the command and the errno. | |
918 | */ | |
7567352f MD |
919 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
920 | sizeof(sendret)); | |
6cd525e8 | 921 | if (writelen < sizeof(sendret)) { |
7567352f MD |
922 | PERROR("lttcomm_send_unix_sock error"); |
923 | ret = -1; | |
924 | goto end; | |
925 | } | |
fe9f7760 FD |
926 | |
927 | /* | |
93bed9fe | 928 | * Stage 5: Send resulting file descriptors to the master. |
fe9f7760 | 929 | */ |
93bed9fe | 930 | ret = send_fds_to_master(worker, data.cmd, &sendret); |
fe9f7760 FD |
931 | if (ret < 0) { |
932 | DBG("Sending FD to master returned an error"); | |
7567352f MD |
933 | goto end; |
934 | } | |
fe9f7760 | 935 | |
7567352f MD |
936 | if (seteuid(prev_euid) < 0) { |
937 | PERROR("seteuid"); | |
938 | ret = -1; | |
939 | goto end; | |
940 | } | |
941 | ret = 0; | |
942 | end: | |
943 | return ret; | |
944 | } | |
945 | ||
946 | static | |
947 | int run_as_worker(struct run_as_worker *worker) | |
948 | { | |
949 | int ret; | |
950 | ssize_t writelen; | |
951 | struct run_as_ret sendret; | |
952 | size_t proc_orig_len; | |
953 | ||
954 | /* | |
955 | * Initialize worker. Set a different process cmdline. | |
956 | */ | |
957 | proc_orig_len = strlen(worker->procname); | |
958 | memset(worker->procname, 0, proc_orig_len); | |
959 | strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len); | |
960 | ||
e1055edb JG |
961 | ret = lttng_prctl(PR_SET_NAME, |
962 | (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); | |
963 | if (ret && ret != -ENOSYS) { | |
b8090274 JG |
964 | /* Don't fail as this is not essential. */ |
965 | PERROR("prctl PR_SET_NAME"); | |
6cd525e8 | 966 | } |
7567352f | 967 | |
fe9f7760 FD |
968 | memset(&sendret, 0, sizeof(sendret)); |
969 | ||
7567352f MD |
970 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
971 | sizeof(sendret)); | |
972 | if (writelen < sizeof(sendret)) { | |
973 | PERROR("lttcomm_send_unix_sock error"); | |
974 | ret = EXIT_FAILURE; | |
975 | goto end; | |
976 | } | |
977 | ||
978 | for (;;) { | |
979 | ret = handle_one_cmd(worker); | |
980 | if (ret < 0) { | |
981 | ret = EXIT_FAILURE; | |
982 | goto end; | |
983 | } else if (ret > 0) { | |
984 | break; | |
985 | } else { | |
986 | continue; /* Next command. */ | |
987 | } | |
988 | } | |
989 | ret = EXIT_SUCCESS; | |
990 | end: | |
991 | return ret; | |
c2b75c49 MD |
992 | } |
993 | ||
60b6c79c | 994 | static |
7567352f MD |
995 | int run_as_cmd(struct run_as_worker *worker, |
996 | enum run_as_cmd cmd, | |
997 | struct run_as_data *data, | |
fe9f7760 | 998 | struct run_as_ret *ret_value, |
7567352f | 999 | uid_t uid, gid_t gid) |
60b6c79c | 1000 | { |
fe9f7760 | 1001 | int ret = 0; |
7567352f | 1002 | ssize_t readlen, writelen; |
60b6c79c MD |
1003 | |
1004 | /* | |
1005 | * If we are non-root, we can only deal with our own uid. | |
1006 | */ | |
1007 | if (geteuid() != 0) { | |
1008 | if (uid != geteuid()) { | |
fe9f7760 FD |
1009 | ret = -1; |
1010 | ret_value->_errno = EPERM; | |
60b6c79c | 1011 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", |
08797918 | 1012 | (int) uid, (int) geteuid()); |
df5b86c8 | 1013 | goto end; |
60b6c79c | 1014 | } |
60b6c79c MD |
1015 | } |
1016 | ||
7567352f MD |
1017 | data->cmd = cmd; |
1018 | data->uid = uid; | |
1019 | data->gid = gid; | |
1020 | ||
fe9f7760 FD |
1021 | /* |
1022 | * Stage 1: Send the run_as_data struct to the worker process | |
1023 | */ | |
7567352f MD |
1024 | writelen = lttcomm_send_unix_sock(worker->sockpair[0], data, |
1025 | sizeof(*data)); | |
1026 | if (writelen < sizeof(*data)) { | |
1027 | PERROR("Error writing message to run_as"); | |
fe9f7760 FD |
1028 | ret = -1; |
1029 | ret_value->_errno = EIO; | |
60b6c79c | 1030 | goto end; |
c2b75c49 | 1031 | } |
7567352f | 1032 | |
fe9f7760 FD |
1033 | /* |
1034 | * Stage 2: Send file descriptor to the worker process if needed | |
1035 | */ | |
93bed9fe | 1036 | ret = send_fds_to_worker(worker, data); |
fe9f7760 FD |
1037 | if (ret) { |
1038 | PERROR("do_send_fd error"); | |
1039 | ret = -1; | |
1040 | ret_value->_errno = EIO; | |
1041 | goto end; | |
1042 | } | |
1043 | ||
1044 | /* | |
1045 | * Stage 3: Wait for the execution of the command | |
1046 | */ | |
1047 | ||
1048 | /* | |
1049 | * Stage 4: Receive the run_as_ret struct containing the return value and | |
1050 | * errno | |
1051 | */ | |
1052 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value, | |
1053 | sizeof(*ret_value)); | |
da9ee832 JG |
1054 | if (!readlen) { |
1055 | ERR("Run-as worker has hung-up during run_as_cmd"); | |
fe9f7760 FD |
1056 | ret = -1; |
1057 | ret_value->_errno = EIO; | |
da9ee832 | 1058 | goto end; |
fe9f7760 | 1059 | } else if (readlen < sizeof(*ret_value)) { |
7567352f | 1060 | PERROR("Error reading response from run_as"); |
fe9f7760 FD |
1061 | ret = -1; |
1062 | ret_value->_errno = errno; | |
033b58a7 | 1063 | goto end; |
6cd525e8 | 1064 | } |
fe9f7760 | 1065 | |
ca9eb994 JG |
1066 | if (ret_value->_error) { |
1067 | /* Skip stage 5 on error as there will be no fd to receive. */ | |
1068 | goto end; | |
1069 | } | |
1070 | ||
fe9f7760 FD |
1071 | /* |
1072 | * Stage 5: Receive file descriptor if needed | |
1073 | */ | |
93bed9fe | 1074 | ret = recv_fds_from_worker(worker, cmd, ret_value); |
fe9f7760 FD |
1075 | if (ret < 0) { |
1076 | ERR("Error receiving fd"); | |
1077 | ret = -1; | |
1078 | ret_value->_errno = EIO; | |
4c462e79 | 1079 | } |
7567352f | 1080 | |
60b6c79c | 1081 | end: |
fe9f7760 | 1082 | return ret; |
60b6c79c MD |
1083 | } |
1084 | ||
2d85a600 | 1085 | /* |
7567352f | 1086 | * This is for debugging ONLY and should not be considered secure. |
2d85a600 MD |
1087 | */ |
1088 | static | |
fe9f7760 FD |
1089 | int run_as_noworker(enum run_as_cmd cmd, |
1090 | struct run_as_data *data, struct run_as_ret *ret_value, | |
1091 | uid_t uid, gid_t gid) | |
2d85a600 | 1092 | { |
df5b86c8 | 1093 | int ret, saved_errno; |
5b73926f | 1094 | mode_t old_mask; |
7567352f | 1095 | run_as_fct fct; |
5b73926f | 1096 | |
7567352f MD |
1097 | fct = run_as_enum_to_fct(cmd); |
1098 | if (!fct) { | |
1099 | errno = -ENOSYS; | |
1100 | ret = -1; | |
1101 | goto end; | |
1102 | } | |
5b73926f | 1103 | old_mask = umask(0); |
fe9f7760 FD |
1104 | ret = fct(data, ret_value); |
1105 | saved_errno = ret_value->_errno; | |
5b73926f | 1106 | umask(old_mask); |
df5b86c8 | 1107 | errno = saved_errno; |
7567352f | 1108 | end: |
5b73926f | 1109 | return ret; |
2d85a600 MD |
1110 | } |
1111 | ||
8fec83ea JG |
1112 | static |
1113 | int reset_sighandler(void) | |
1114 | { | |
1115 | int sig; | |
1116 | ||
1117 | DBG("Resetting run_as worker signal handlers to default"); | |
1118 | for (sig = 1; sig <= 31; sig++) { | |
1119 | (void) signal(sig, SIG_DFL); | |
1120 | } | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | static | |
1125 | void worker_sighandler(int sig) | |
1126 | { | |
1127 | const char *signame; | |
1128 | ||
1129 | /* | |
1130 | * The worker will inherit its parent's signals since they are part of | |
1131 | * the same process group. However, in the case of SIGINT and SIGTERM, | |
1132 | * we want to give the worker a chance to teardown gracefully when its | |
1133 | * parent closes the command socket. | |
1134 | */ | |
1135 | switch (sig) { | |
1136 | case SIGINT: | |
1137 | signame = "SIGINT"; | |
1138 | break; | |
1139 | case SIGTERM: | |
1140 | signame = "SIGTERM"; | |
1141 | break; | |
1142 | default: | |
1143 | signame = NULL; | |
1144 | } | |
1145 | ||
1146 | if (signame) { | |
1147 | DBG("run_as worker received signal %s", signame); | |
1148 | } else { | |
1149 | DBG("run_as_worker received signal %d", sig); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | static | |
1154 | int set_worker_sighandlers(void) | |
1155 | { | |
1156 | int ret = 0; | |
1157 | sigset_t sigset; | |
1158 | struct sigaction sa; | |
1159 | ||
1160 | if ((ret = sigemptyset(&sigset)) < 0) { | |
1161 | PERROR("sigemptyset"); | |
1162 | goto end; | |
1163 | } | |
1164 | ||
1165 | sa.sa_handler = worker_sighandler; | |
1166 | sa.sa_mask = sigset; | |
1167 | sa.sa_flags = 0; | |
1168 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
1169 | PERROR("sigaction SIGINT"); | |
1170 | goto end; | |
1171 | } | |
1172 | ||
1173 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
1174 | PERROR("sigaction SIGTERM"); | |
1175 | goto end; | |
1176 | } | |
1177 | ||
1178 | DBG("run_as signal handler set for SIGTERM and SIGINT"); | |
1179 | end: | |
1180 | return ret; | |
1181 | } | |
1182 | ||
1183 | static | |
929f71ec JG |
1184 | int run_as_create_worker_no_lock(const char *procname, |
1185 | post_fork_cleanup_cb clean_up_func, | |
1186 | void *clean_up_user_data) | |
8fec83ea JG |
1187 | { |
1188 | pid_t pid; | |
1189 | int i, ret = 0; | |
1190 | ssize_t readlen; | |
1191 | struct run_as_ret recvret; | |
1192 | struct run_as_worker *worker; | |
1193 | ||
1194 | assert(!global_worker); | |
1195 | if (!use_clone()) { | |
1196 | /* | |
1197 | * Don't initialize a worker, all run_as tasks will be performed | |
1198 | * in the current process. | |
1199 | */ | |
1200 | ret = 0; | |
1201 | goto end; | |
1202 | } | |
1203 | worker = zmalloc(sizeof(*worker)); | |
1204 | if (!worker) { | |
1205 | ret = -ENOMEM; | |
1206 | goto end; | |
1207 | } | |
1208 | worker->procname = strdup(procname); | |
1209 | if (!worker->procname) { | |
1210 | ret = -ENOMEM; | |
8c96eded | 1211 | goto error_procname_alloc; |
8fec83ea JG |
1212 | } |
1213 | /* Create unix socket. */ | |
1214 | if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) { | |
1215 | ret = -1; | |
1216 | goto error_sock; | |
1217 | } | |
1218 | ||
1219 | /* Fork worker. */ | |
1220 | pid = fork(); | |
1221 | if (pid < 0) { | |
1222 | PERROR("fork"); | |
1223 | ret = -1; | |
1224 | goto error_fork; | |
1225 | } else if (pid == 0) { | |
1226 | /* Child */ | |
1227 | ||
1228 | reset_sighandler(); | |
1229 | ||
1230 | set_worker_sighandlers(); | |
929f71ec JG |
1231 | if (clean_up_func) { |
1232 | if (clean_up_func(clean_up_user_data) < 0) { | |
1233 | ERR("Run-as post-fork clean-up failed, exiting."); | |
1234 | exit(EXIT_FAILURE); | |
1235 | } | |
1236 | } | |
8fec83ea JG |
1237 | |
1238 | /* Just close, no shutdown. */ | |
1239 | if (close(worker->sockpair[0])) { | |
1240 | PERROR("close"); | |
1241 | exit(EXIT_FAILURE); | |
1242 | } | |
1243 | ||
1244 | /* | |
1245 | * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1] | |
1246 | * Sockpair[1] is used as a control channel with the master | |
1247 | */ | |
1248 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
1249 | if (i != worker->sockpair[1]) { | |
1250 | (void) close(i); | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | worker->sockpair[0] = -1; | |
1255 | ret = run_as_worker(worker); | |
1256 | if (lttcomm_close_unix_sock(worker->sockpair[1])) { | |
1257 | PERROR("close"); | |
1258 | ret = -1; | |
1259 | } | |
1260 | worker->sockpair[1] = -1; | |
55cb0d6f | 1261 | free(worker->procname); |
340cf672 | 1262 | free(worker); |
8fec83ea JG |
1263 | LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret); |
1264 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
1265 | } else { | |
1266 | /* Parent */ | |
1267 | ||
1268 | /* Just close, no shutdown. */ | |
1269 | if (close(worker->sockpair[1])) { | |
1270 | PERROR("close"); | |
1271 | ret = -1; | |
1272 | goto error_fork; | |
1273 | } | |
1274 | worker->sockpair[1] = -1; | |
1275 | worker->pid = pid; | |
1276 | /* Wait for worker to become ready. */ | |
1277 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], | |
1278 | &recvret, sizeof(recvret)); | |
1279 | if (readlen < sizeof(recvret)) { | |
1280 | ERR("readlen: %zd", readlen); | |
1281 | PERROR("Error reading response from run_as at creation"); | |
1282 | ret = -1; | |
1283 | goto error_fork; | |
1284 | } | |
1285 | global_worker = worker; | |
1286 | } | |
1287 | end: | |
1288 | return ret; | |
1289 | ||
1290 | /* Error handling. */ | |
1291 | error_fork: | |
1292 | for (i = 0; i < 2; i++) { | |
1293 | if (worker->sockpair[i] < 0) { | |
1294 | continue; | |
1295 | } | |
1296 | if (lttcomm_close_unix_sock(worker->sockpair[i])) { | |
1297 | PERROR("close"); | |
1298 | } | |
1299 | worker->sockpair[i] = -1; | |
1300 | } | |
1301 | error_sock: | |
8c96eded FD |
1302 | free(worker->procname); |
1303 | error_procname_alloc: | |
8fec83ea JG |
1304 | free(worker); |
1305 | return ret; | |
1306 | } | |
1307 | ||
a01c682b JR |
1308 | static |
1309 | void run_as_destroy_worker_no_lock(void) | |
1310 | { | |
1311 | struct run_as_worker *worker = global_worker; | |
1312 | ||
1313 | DBG("Destroying run_as worker"); | |
1314 | if (!worker) { | |
1315 | return; | |
1316 | } | |
1317 | /* Close unix socket */ | |
1318 | DBG("Closing run_as worker socket"); | |
1319 | if (lttcomm_close_unix_sock(worker->sockpair[0])) { | |
1320 | PERROR("close"); | |
1321 | } | |
1322 | worker->sockpair[0] = -1; | |
1323 | /* Wait for worker. */ | |
1324 | for (;;) { | |
1325 | int status; | |
1326 | pid_t wait_ret; | |
1327 | ||
1328 | wait_ret = waitpid(worker->pid, &status, 0); | |
1329 | if (wait_ret < 0) { | |
1330 | if (errno == EINTR) { | |
1331 | continue; | |
1332 | } | |
1333 | PERROR("waitpid"); | |
1334 | break; | |
1335 | } | |
1336 | ||
1337 | if (WIFEXITED(status)) { | |
1338 | LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR, | |
1339 | DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d", | |
55cb0d6f | 1340 | WEXITSTATUS(status)); |
a01c682b JR |
1341 | break; |
1342 | } else if (WIFSIGNALED(status)) { | |
1343 | ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d", | |
1344 | WTERMSIG(status)); | |
1345 | break; | |
1346 | } | |
1347 | } | |
1348 | free(worker->procname); | |
1349 | free(worker); | |
1350 | global_worker = NULL; | |
1351 | } | |
1352 | ||
2d85a600 | 1353 | static |
fe9f7760 | 1354 | int run_as_restart_worker(struct run_as_worker *worker) |
2d85a600 | 1355 | { |
fe9f7760 FD |
1356 | int ret = 0; |
1357 | char *procname = NULL; | |
1358 | ||
1359 | procname = worker->procname; | |
1360 | ||
1361 | /* Close socket to run_as worker process and clean up the zombie process */ | |
a01c682b | 1362 | run_as_destroy_worker_no_lock(); |
fe9f7760 FD |
1363 | |
1364 | /* Create a new run_as worker process*/ | |
929f71ec | 1365 | ret = run_as_create_worker_no_lock(procname, NULL, NULL); |
fe9f7760 FD |
1366 | if (ret < 0 ) { |
1367 | ERR("Restarting the worker process failed"); | |
1368 | ret = -1; | |
1369 | goto err; | |
1370 | } | |
1371 | err: | |
1372 | return ret; | |
1373 | } | |
1374 | ||
1375 | static | |
1376 | int run_as(enum run_as_cmd cmd, struct run_as_data *data, | |
1377 | struct run_as_ret *ret_value, uid_t uid, gid_t gid) | |
1378 | { | |
1379 | int ret, saved_errno; | |
7567352f | 1380 | |
8fec83ea | 1381 | pthread_mutex_lock(&worker_lock); |
749b7a0c | 1382 | if (use_clone()) { |
7567352f | 1383 | DBG("Using run_as worker"); |
8fec83ea | 1384 | |
749b7a0c | 1385 | assert(global_worker); |
749b7a0c | 1386 | |
fe9f7760 FD |
1387 | ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid); |
1388 | saved_errno = ret_value->_errno; | |
1389 | ||
fe9f7760 FD |
1390 | /* |
1391 | * If the worker thread crashed the errno is set to EIO. we log | |
1392 | * the error and start a new worker process. | |
1393 | */ | |
1394 | if (ret == -1 && saved_errno == EIO) { | |
1395 | DBG("Socket closed unexpectedly... " | |
1396 | "Restarting the worker process"); | |
1397 | ret = run_as_restart_worker(global_worker); | |
fe9f7760 FD |
1398 | if (ret == -1) { |
1399 | ERR("Failed to restart worker process."); | |
1400 | goto err; | |
1401 | } | |
1402 | } | |
2d85a600 | 1403 | } else { |
7567352f | 1404 | DBG("Using run_as without worker"); |
fe9f7760 | 1405 | ret = run_as_noworker(cmd, data, ret_value, uid, gid); |
2d85a600 | 1406 | } |
fe9f7760 | 1407 | err: |
8fec83ea | 1408 | pthread_mutex_unlock(&worker_lock); |
7567352f | 1409 | return ret; |
2d85a600 MD |
1410 | } |
1411 | ||
90e535ef | 1412 | LTTNG_HIDDEN |
e11d277b | 1413 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 1414 | { |
18710679 JG |
1415 | return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid); |
1416 | } | |
1417 | ||
1418 | LTTNG_HIDDEN | |
1419 | int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode, | |
1420 | uid_t uid, gid_t gid) | |
1421 | { | |
1422 | int ret; | |
93bed9fe JG |
1423 | struct run_as_data data = {}; |
1424 | struct run_as_ret run_as_ret = {}; | |
60b6c79c | 1425 | |
18710679 JG |
1426 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", |
1427 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1428 | path, (int) mode, (int) uid, (int) gid); |
93bed9fe JG |
1429 | ret = lttng_strncpy(data.u.mkdir.path, path, |
1430 | sizeof(data.u.mkdir.path)); | |
18710679 JG |
1431 | if (ret) { |
1432 | ERR("Failed to copy path argument of mkdirat recursive command"); | |
1433 | goto error; | |
1434 | } | |
93bed9fe JG |
1435 | data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0'; |
1436 | data.u.mkdir.mode = mode; | |
1437 | data.u.mkdir.dirfd = dirfd; | |
18710679 JG |
1438 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE, |
1439 | &data, &run_as_ret, uid, gid); | |
1440 | errno = run_as_ret._errno; | |
93bed9fe | 1441 | ret = run_as_ret.u.ret; |
18710679 JG |
1442 | error: |
1443 | return ret; | |
60b6c79c MD |
1444 | } |
1445 | ||
90e535ef | 1446 | LTTNG_HIDDEN |
e11d277b | 1447 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 1448 | { |
18710679 JG |
1449 | return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid); |
1450 | } | |
1451 | ||
1452 | LTTNG_HIDDEN | |
1453 | int run_as_mkdirat(int dirfd, const char *path, mode_t mode, | |
1454 | uid_t uid, gid_t gid) | |
1455 | { | |
1456 | int ret; | |
93bed9fe JG |
1457 | struct run_as_data data = {}; |
1458 | struct run_as_ret run_as_ret = {}; | |
fe9f7760 | 1459 | |
18710679 JG |
1460 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", |
1461 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1462 | path, (int) mode, (int) uid, (int) gid); |
93bed9fe JG |
1463 | ret = lttng_strncpy(data.u.mkdir.path, path, |
1464 | sizeof(data.u.mkdir.path)); | |
18710679 JG |
1465 | if (ret) { |
1466 | ERR("Failed to copy path argument of mkdirat command"); | |
1467 | goto error; | |
1468 | } | |
93bed9fe JG |
1469 | data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0'; |
1470 | data.u.mkdir.mode = mode; | |
1471 | data.u.mkdir.dirfd = dirfd; | |
18710679 JG |
1472 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT, |
1473 | &data, &run_as_ret, uid, gid); | |
1474 | errno = run_as_ret._errno; | |
93bed9fe | 1475 | ret = run_as_ret.u.ret; |
18710679 JG |
1476 | error: |
1477 | return ret; | |
60b6c79c MD |
1478 | } |
1479 | ||
90e535ef | 1480 | LTTNG_HIDDEN |
2912cead | 1481 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, |
55cb0d6f | 1482 | gid_t gid) |
2912cead JG |
1483 | { |
1484 | return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid); | |
1485 | } | |
1486 | ||
1487 | LTTNG_HIDDEN | |
1488 | int run_as_openat(int dirfd, const char *path, int flags, mode_t mode, | |
1489 | uid_t uid, gid_t gid) | |
60b6c79c | 1490 | { |
93bed9fe | 1491 | int ret; |
55cb0d6f JG |
1492 | struct run_as_data data = {}; |
1493 | struct run_as_ret run_as_ret = {}; | |
fe9f7760 | 1494 | |
2912cead JG |
1495 | DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d", |
1496 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1497 | path, flags, (int) mode, (int) uid, (int) gid); |
93bed9fe JG |
1498 | ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path)); |
1499 | if (ret) { | |
1500 | ERR("Failed to copy path argument of open command"); | |
1501 | goto error; | |
1502 | } | |
7567352f MD |
1503 | data.u.open.flags = flags; |
1504 | data.u.open.mode = mode; | |
93bed9fe | 1505 | data.u.open.dirfd = dirfd; |
2912cead | 1506 | run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT, |
93bed9fe JG |
1507 | &data, &run_as_ret, uid, gid); |
1508 | errno = run_as_ret._errno; | |
1509 | ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret : | |
1510 | run_as_ret.u.open.fd; | |
1511 | error: | |
1512 | return ret; | |
60b6c79c | 1513 | } |
4628484a MD |
1514 | |
1515 | LTTNG_HIDDEN | |
1516 | int run_as_unlink(const char *path, uid_t uid, gid_t gid) | |
2912cead JG |
1517 | { |
1518 | return run_as_unlinkat(AT_FDCWD, path, uid, gid); | |
1519 | } | |
1520 | ||
1521 | LTTNG_HIDDEN | |
1522 | int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid) | |
4628484a | 1523 | { |
93bed9fe JG |
1524 | int ret; |
1525 | struct run_as_data data = {}; | |
1526 | struct run_as_ret run_as_ret = {}; | |
fe9f7760 | 1527 | |
2912cead JG |
1528 | DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d", |
1529 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1530 | path, (int) uid, (int) gid); |
93bed9fe JG |
1531 | ret = lttng_strncpy(data.u.unlink.path, path, |
1532 | sizeof(data.u.unlink.path)); | |
1533 | if (ret) { | |
1534 | goto error; | |
1535 | } | |
1536 | data.u.unlink.dirfd = dirfd; | |
1537 | run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data, | |
1538 | &run_as_ret, uid, gid); | |
1539 | errno = run_as_ret._errno; | |
1540 | ret = run_as_ret.u.ret; | |
1541 | error: | |
1542 | return ret; | |
1543 | } | |
1544 | ||
1545 | LTTNG_HIDDEN | |
1546 | int run_as_rmdir(const char *path, uid_t uid, gid_t gid) | |
1547 | { | |
1548 | return run_as_rmdirat(AT_FDCWD, path, uid, gid); | |
1549 | } | |
1550 | ||
1551 | LTTNG_HIDDEN | |
1552 | int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid) | |
1553 | { | |
1554 | int ret; | |
1555 | struct run_as_data data = {}; | |
1556 | struct run_as_ret run_as_ret = {}; | |
1557 | ||
1558 | DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d", | |
1559 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
1560 | path, (int) uid, (int) gid); | |
1561 | ret = lttng_strncpy(data.u.rmdir.path, path, | |
1562 | sizeof(data.u.rmdir.path)); | |
1563 | if (ret) { | |
1564 | goto error; | |
1565 | } | |
1566 | data.u.rmdir.dirfd = dirfd; | |
1567 | run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data, | |
1568 | &run_as_ret, uid, gid); | |
1569 | errno = run_as_ret._errno; | |
1570 | ret = run_as_ret.u.ret; | |
1571 | error: | |
1572 | return ret; | |
4628484a MD |
1573 | } |
1574 | ||
1575 | LTTNG_HIDDEN | |
f75c5439 | 1576 | int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags) |
4628484a | 1577 | { |
f75c5439 | 1578 | return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags); |
93bed9fe | 1579 | } |
fe9f7760 | 1580 | |
93bed9fe | 1581 | LTTNG_HIDDEN |
f75c5439 | 1582 | int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) |
93bed9fe JG |
1583 | { |
1584 | int ret; | |
1585 | struct run_as_data data = {}; | |
1586 | struct run_as_ret run_as_ret = {}; | |
4628484a | 1587 | |
93bed9fe JG |
1588 | DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d", |
1589 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
08797918 | 1590 | path, (int) uid, (int) gid); |
93bed9fe JG |
1591 | ret = lttng_strncpy(data.u.rmdir.path, path, |
1592 | sizeof(data.u.rmdir.path)); | |
1593 | if (ret) { | |
1594 | goto error; | |
1595 | } | |
1596 | data.u.rmdir.dirfd = dirfd; | |
f75c5439 | 1597 | data.u.rmdir.flags = flags; |
93bed9fe JG |
1598 | run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE, |
1599 | &data, &run_as_ret, uid, gid); | |
1600 | errno = run_as_ret._errno; | |
1601 | ret = run_as_ret.u.ret; | |
1602 | error: | |
1603 | return ret; | |
1604 | } | |
1605 | ||
1606 | LTTNG_HIDDEN | |
1607 | int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid) | |
1608 | { | |
1609 | return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid); | |
1610 | } | |
1611 | ||
1612 | LTTNG_HIDDEN | |
1613 | int run_as_renameat(int old_dirfd, const char *old_name, | |
1614 | int new_dirfd, const char *new_name, uid_t uid, gid_t gid) | |
1615 | { | |
1616 | int ret; | |
1617 | struct run_as_data data = {}; | |
1618 | struct run_as_ret run_as_ret = {}; | |
1619 | ||
1620 | DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d", | |
1621 | old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
1622 | old_name, | |
1623 | new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", | |
1624 | new_name, (int) uid, (int) gid); | |
1625 | ret = lttng_strncpy(data.u.rename.old_path, old_name, | |
1626 | sizeof(data.u.rename.old_path)); | |
1627 | if (ret) { | |
1628 | goto error; | |
1629 | } | |
1630 | ret = lttng_strncpy(data.u.rename.new_path, new_name, | |
1631 | sizeof(data.u.rename.new_path)); | |
1632 | if (ret) { | |
1633 | goto error; | |
1634 | } | |
1635 | ||
1636 | data.u.rename.dirfds[0] = old_dirfd; | |
1637 | data.u.rename.dirfds[1] = new_dirfd; | |
1638 | run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ? | |
1639 | RUN_AS_RENAME : RUN_AS_RENAMEAT, | |
1640 | &data, &run_as_ret, uid, gid); | |
1641 | errno = run_as_ret._errno; | |
1642 | ret = run_as_ret.u.ret; | |
1643 | error: | |
1644 | return ret; | |
7567352f MD |
1645 | } |
1646 | ||
241e0a5a FD |
1647 | LTTNG_HIDDEN |
1648 | int run_as_extract_elf_symbol_offset(int fd, const char* function, | |
1649 | uid_t uid, gid_t gid, uint64_t *offset) | |
1650 | { | |
93bed9fe JG |
1651 | int ret; |
1652 | struct run_as_data data = {}; | |
55cb0d6f | 1653 | struct run_as_ret run_as_ret = {}; |
f726677b | 1654 | |
241e0a5a | 1655 | DBG3("extract_elf_symbol_offset() on fd=%d and function=%s " |
93bed9fe JG |
1656 | "with for uid %d and gid %d", fd, function, |
1657 | (int) uid, (int) gid); | |
241e0a5a | 1658 | |
93bed9fe | 1659 | data.u.extract_elf_symbol_offset.fd = fd; |
241e0a5a FD |
1660 | |
1661 | strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1); | |
241e0a5a | 1662 | data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
93bed9fe JG |
1663 | ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function, |
1664 | function, | |
1665 | sizeof(data.u.extract_elf_symbol_offset.function)); | |
1666 | if (ret) { | |
1667 | goto error; | |
1668 | } | |
241e0a5a | 1669 | |
93bed9fe JG |
1670 | run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid); |
1671 | errno = run_as_ret._errno; | |
1672 | if (run_as_ret._error) { | |
1673 | ret = -1; | |
1674 | goto error; | |
241e0a5a FD |
1675 | } |
1676 | ||
93bed9fe JG |
1677 | *offset = run_as_ret.u.extract_elf_symbol_offset.offset; |
1678 | error: | |
1679 | return ret; | |
241e0a5a FD |
1680 | } |
1681 | ||
0ef03255 FD |
1682 | LTTNG_HIDDEN |
1683 | int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name, | |
1684 | const char* probe_name, uid_t uid, gid_t gid, | |
1685 | uint64_t **offsets, uint32_t *num_offset) | |
1686 | { | |
93bed9fe JG |
1687 | int ret; |
1688 | struct run_as_data data = {}; | |
1689 | struct run_as_ret run_as_ret = {}; | |
f726677b | 1690 | |
0ef03255 | 1691 | DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and " |
93bed9fe JG |
1692 | "provider_name=%s with for uid %d and gid %d", fd, |
1693 | probe_name, provider_name, (int) uid, (int) gid); | |
0ef03255 | 1694 | |
93bed9fe | 1695 | data.u.extract_sdt_probe_offsets.fd = fd; |
0ef03255 | 1696 | |
93bed9fe JG |
1697 | ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, |
1698 | sizeof(data.u.extract_sdt_probe_offsets.probe_name)); | |
1699 | if (ret) { | |
1700 | goto error; | |
1701 | } | |
1702 | ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name, | |
1703 | provider_name, | |
1704 | sizeof(data.u.extract_sdt_probe_offsets.provider_name)); | |
1705 | if (ret) { | |
1706 | goto error; | |
0ef03255 FD |
1707 | } |
1708 | ||
93bed9fe JG |
1709 | run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid); |
1710 | errno = run_as_ret._errno; | |
1711 | if (run_as_ret._error) { | |
1712 | ret = -1; | |
1713 | goto error; | |
1714 | } | |
0ef03255 | 1715 | |
93bed9fe | 1716 | *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset; |
0ef03255 FD |
1717 | *offsets = zmalloc(*num_offset * sizeof(uint64_t)); |
1718 | if (!*offsets) { | |
93bed9fe JG |
1719 | ret = -ENOMEM; |
1720 | goto error; | |
0ef03255 FD |
1721 | } |
1722 | ||
93bed9fe JG |
1723 | memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets, |
1724 | *num_offset * sizeof(uint64_t)); | |
1725 | error: | |
1726 | return ret; | |
0ef03255 FD |
1727 | } |
1728 | ||
7567352f | 1729 | LTTNG_HIDDEN |
929f71ec JG |
1730 | int run_as_create_worker(const char *procname, |
1731 | post_fork_cleanup_cb clean_up_func, | |
1732 | void *clean_up_user_data) | |
7567352f | 1733 | { |
8fec83ea | 1734 | int ret; |
7567352f | 1735 | |
749b7a0c | 1736 | pthread_mutex_lock(&worker_lock); |
929f71ec JG |
1737 | ret = run_as_create_worker_no_lock(procname, clean_up_func, |
1738 | clean_up_user_data); | |
749b7a0c | 1739 | pthread_mutex_unlock(&worker_lock); |
7567352f MD |
1740 | return ret; |
1741 | } | |
1742 | ||
1743 | LTTNG_HIDDEN | |
1744 | void run_as_destroy_worker(void) | |
1745 | { | |
749b7a0c | 1746 | pthread_mutex_lock(&worker_lock); |
a01c682b | 1747 | run_as_destroy_worker_no_lock(); |
749b7a0c | 1748 | pthread_mutex_unlock(&worker_lock); |
4628484a | 1749 | } |