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