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