| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 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. |
| 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 |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #define _LGPL_SOURCE |
| 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> |
| 31 | #include <sched.h> |
| 32 | #include <signal.h> |
| 33 | #include <assert.h> |
| 34 | #include <signal.h> |
| 35 | |
| 36 | #include <common/lttng-kernel.h> |
| 37 | #include <common/common.h> |
| 38 | #include <common/utils.h> |
| 39 | #include <common/compat/getenv.h> |
| 40 | #include <common/compat/prctl.h> |
| 41 | #include <common/unix.h> |
| 42 | #include <common/defaults.h> |
| 43 | #include <common/lttng-elf.h> |
| 44 | |
| 45 | #include <lttng/constant.h> |
| 46 | |
| 47 | #include "runas.h" |
| 48 | |
| 49 | struct run_as_data; |
| 50 | struct run_as_ret; |
| 51 | typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value); |
| 52 | |
| 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, |
| 70 | }; |
| 71 | |
| 72 | struct run_as_mkdir_data { |
| 73 | int dirfd; |
| 74 | char path[LTTNG_PATH_MAX]; |
| 75 | mode_t mode; |
| 76 | } LTTNG_PACKED; |
| 77 | |
| 78 | struct run_as_open_data { |
| 79 | int dirfd; |
| 80 | char path[LTTNG_PATH_MAX]; |
| 81 | int flags; |
| 82 | mode_t mode; |
| 83 | } LTTNG_PACKED; |
| 84 | |
| 85 | struct run_as_unlink_data { |
| 86 | int dirfd; |
| 87 | char path[LTTNG_PATH_MAX]; |
| 88 | } LTTNG_PACKED; |
| 89 | |
| 90 | struct run_as_rmdir_data { |
| 91 | int dirfd; |
| 92 | char path[LTTNG_PATH_MAX]; |
| 93 | int flags; /* enum lttng_directory_handle_rmdir_recursive_flags */ |
| 94 | } LTTNG_PACKED; |
| 95 | |
| 96 | struct run_as_extract_elf_symbol_offset_data { |
| 97 | int fd; |
| 98 | char function[LTTNG_SYMBOL_NAME_LEN]; |
| 99 | } LTTNG_PACKED; |
| 100 | |
| 101 | struct run_as_extract_sdt_probe_offsets_data { |
| 102 | int fd; |
| 103 | char probe_name[LTTNG_SYMBOL_NAME_LEN]; |
| 104 | char provider_name[LTTNG_SYMBOL_NAME_LEN]; |
| 105 | } LTTNG_PACKED; |
| 106 | |
| 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; |
| 116 | |
| 117 | struct run_as_open_ret { |
| 118 | int fd; |
| 119 | } LTTNG_PACKED; |
| 120 | |
| 121 | struct run_as_extract_elf_symbol_offset_ret { |
| 122 | uint64_t offset; |
| 123 | } LTTNG_PACKED; |
| 124 | |
| 125 | struct run_as_extract_sdt_probe_offsets_ret { |
| 126 | uint32_t num_offset; |
| 127 | uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM]; |
| 128 | } LTTNG_PACKED; |
| 129 | |
| 130 | struct run_as_data { |
| 131 | enum run_as_cmd cmd; |
| 132 | union { |
| 133 | struct run_as_mkdir_data mkdir; |
| 134 | struct run_as_open_data open; |
| 135 | struct run_as_unlink_data unlink; |
| 136 | struct run_as_rmdir_data rmdir; |
| 137 | struct run_as_rename_data rename; |
| 138 | struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset; |
| 139 | struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets; |
| 140 | } u; |
| 141 | uid_t uid; |
| 142 | gid_t gid; |
| 143 | } LTTNG_PACKED; |
| 144 | |
| 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 | */ |
| 160 | struct run_as_ret { |
| 161 | union { |
| 162 | int ret; |
| 163 | struct run_as_open_ret open; |
| 164 | struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset; |
| 165 | struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets; |
| 166 | } u; |
| 167 | int _errno; |
| 168 | bool _error; |
| 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 | }, |
| 319 | }; |
| 320 | |
| 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 | |
| 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 | { |
| 342 | return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE"); |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | /* |
| 347 | * Create recursively directory using the FULL path. |
| 348 | */ |
| 349 | static |
| 350 | int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
| 351 | { |
| 352 | const char *path; |
| 353 | mode_t mode; |
| 354 | struct lttng_directory_handle handle; |
| 355 | |
| 356 | path = data->u.mkdir.path; |
| 357 | mode = data->u.mkdir.mode; |
| 358 | |
| 359 | (void) lttng_directory_handle_init_from_dirfd(&handle, |
| 360 | data->u.mkdir.dirfd); |
| 361 | /* Ownership of dirfd is transferred to the handle. */ |
| 362 | data->u.mkdir.dirfd = -1; |
| 363 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
| 364 | ret_value->u.ret = |
| 365 | lttng_directory_handle_create_subdirectory_recursive( |
| 366 | &handle, path, mode); |
| 367 | ret_value->_errno = errno; |
| 368 | ret_value->_error = (ret_value->u.ret) ? true : false; |
| 369 | lttng_directory_handle_fini(&handle); |
| 370 | return ret_value->u.ret; |
| 371 | } |
| 372 | |
| 373 | static |
| 374 | int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value) |
| 375 | { |
| 376 | const char *path; |
| 377 | mode_t mode; |
| 378 | struct lttng_directory_handle handle; |
| 379 | |
| 380 | path = data->u.mkdir.path; |
| 381 | mode = data->u.mkdir.mode; |
| 382 | |
| 383 | (void) lttng_directory_handle_init_from_dirfd(&handle, |
| 384 | data->u.mkdir.dirfd); |
| 385 | /* Ownership of dirfd is transferred to the handle. */ |
| 386 | data->u.mkdir.dirfd = -1; |
| 387 | /* Safe to call as we have transitioned to the requested uid/gid. */ |
| 388 | ret_value->u.ret = |
| 389 | lttng_directory_handle_create_subdirectory( |
| 390 | &handle, path, mode); |
| 391 | ret_value->_errno = errno; |
| 392 | ret_value->_error = (ret_value->u.ret) ? true : false; |
| 393 | lttng_directory_handle_fini(&handle); |
| 394 | return ret_value->u.ret; |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | int _open(struct run_as_data *data, struct run_as_ret *ret_value) |
| 399 | { |
| 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 | |
| 419 | ret_value->_errno = errno; |
| 420 | ret_value->_error = fd < 0; |
| 421 | lttng_directory_handle_fini(&handle); |
| 422 | return ret_value->u.ret; |
| 423 | } |
| 424 | |
| 425 | static |
| 426 | int _unlink(struct run_as_data *data, struct run_as_ret *ret_value) |
| 427 | { |
| 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); |
| 457 | ret_value->_errno = errno; |
| 458 | ret_value->_error = (ret_value->u.ret) ? true : false; |
| 459 | lttng_directory_handle_fini(&handle); |
| 460 | return ret_value->u.ret; |
| 461 | } |
| 462 | |
| 463 | static |
| 464 | int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value) |
| 465 | { |
| 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( |
| 475 | &handle, data->u.rmdir.path, data->u.rmdir.flags); |
| 476 | ret_value->_errno = errno; |
| 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; |
| 507 | } |
| 508 | |
| 509 | #ifdef HAVE_ELF_H |
| 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; |
| 515 | uint64_t offset; |
| 516 | |
| 517 | ret_value->_error = false; |
| 518 | ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd, |
| 519 | data->u.extract_elf_symbol_offset.function, |
| 520 | &offset); |
| 521 | if (ret) { |
| 522 | DBG("Failed to extract ELF function offset"); |
| 523 | ret_value->_error = true; |
| 524 | } |
| 525 | ret_value->u.extract_elf_symbol_offset.offset = offset; |
| 526 | |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | static |
| 531 | int _extract_sdt_probe_offsets(struct run_as_data *data, |
| 532 | struct run_as_ret *ret_value) |
| 533 | { |
| 534 | int ret = 0; |
| 535 | uint64_t *offsets = NULL; |
| 536 | uint32_t num_offset; |
| 537 | |
| 538 | ret_value->_error = false; |
| 539 | |
| 540 | /* On success, this call allocates the offsets paramater. */ |
| 541 | ret = lttng_elf_get_sdt_probe_offsets( |
| 542 | data->u.extract_sdt_probe_offsets.fd, |
| 543 | data->u.extract_sdt_probe_offsets.provider_name, |
| 544 | data->u.extract_sdt_probe_offsets.probe_name, |
| 545 | &offsets, &num_offset); |
| 546 | |
| 547 | if (ret) { |
| 548 | DBG("Failed to extract SDT probe offsets"); |
| 549 | ret_value->_error = true; |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) { |
| 554 | DBG("Wrong number of probes."); |
| 555 | ret = -1; |
| 556 | ret_value->_error = true; |
| 557 | goto free_offset; |
| 558 | } |
| 559 | |
| 560 | /* Copy the content of the offsets array to the ret struct. */ |
| 561 | memcpy(ret_value->u.extract_sdt_probe_offsets.offsets, |
| 562 | offsets, num_offset * sizeof(uint64_t)); |
| 563 | |
| 564 | ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset; |
| 565 | |
| 566 | free_offset: |
| 567 | free(offsets); |
| 568 | end: |
| 569 | return ret; |
| 570 | } |
| 571 | #else |
| 572 | static |
| 573 | int _extract_elf_symbol_offset(struct run_as_data *data, |
| 574 | struct run_as_ret *ret_value) |
| 575 | { |
| 576 | ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET"); |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | static |
| 581 | int _extract_sdt_probe_offsets(struct run_as_data *data, |
| 582 | struct run_as_ret *ret_value) |
| 583 | { |
| 584 | ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS"); |
| 585 | return -1; |
| 586 | } |
| 587 | #endif |
| 588 | |
| 589 | static |
| 590 | run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd) |
| 591 | { |
| 592 | switch (cmd) { |
| 593 | case RUN_AS_MKDIR: |
| 594 | case RUN_AS_MKDIRAT: |
| 595 | return _mkdirat; |
| 596 | case RUN_AS_MKDIR_RECURSIVE: |
| 597 | case RUN_AS_MKDIRAT_RECURSIVE: |
| 598 | return _mkdirat_recursive; |
| 599 | case RUN_AS_OPEN: |
| 600 | case RUN_AS_OPENAT: |
| 601 | return _open; |
| 602 | case RUN_AS_UNLINK: |
| 603 | case RUN_AS_UNLINKAT: |
| 604 | return _unlink; |
| 605 | case RUN_AS_RMDIR: |
| 606 | case RUN_AS_RMDIRAT: |
| 607 | return _rmdir; |
| 608 | case RUN_AS_RMDIR_RECURSIVE: |
| 609 | case RUN_AS_RMDIRAT_RECURSIVE: |
| 610 | return _rmdir_recursive; |
| 611 | case RUN_AS_RENAME: |
| 612 | case RUN_AS_RENAMEAT: |
| 613 | return _rename; |
| 614 | case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET: |
| 615 | return _extract_elf_symbol_offset; |
| 616 | case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS: |
| 617 | return _extract_sdt_probe_offsets; |
| 618 | default: |
| 619 | ERR("Unknown command %d", (int) cmd); |
| 620 | return NULL; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static |
| 625 | int do_send_fds(int sock, const int *fds, unsigned int fd_count) |
| 626 | { |
| 627 | ssize_t len; |
| 628 | unsigned int i; |
| 629 | |
| 630 | for (i = 0; i < fd_count; i++) { |
| 631 | if (fds[i] < 0) { |
| 632 | ERR("Attempt to send invalid file descriptor to master (fd = %i)", |
| 633 | fds[i]); |
| 634 | /* Return 0 as this is not a fatal error. */ |
| 635 | return 0; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | len = lttcomm_send_fds_unix_sock(sock, fds, fd_count); |
| 640 | return len < 0 ? -1 : 0; |
| 641 | } |
| 642 | |
| 643 | static |
| 644 | int do_recv_fds(int sock, int *fds, unsigned int fd_count) |
| 645 | { |
| 646 | int ret = 0; |
| 647 | unsigned int i; |
| 648 | ssize_t len; |
| 649 | |
| 650 | len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count); |
| 651 | if (len == 0) { |
| 652 | ret = -1; |
| 653 | goto end; |
| 654 | } else if (len < 0) { |
| 655 | PERROR("Failed to receive file descriptors from socket"); |
| 656 | ret = -1; |
| 657 | goto end; |
| 658 | } |
| 659 | |
| 660 | for (i = 0; i < fd_count; i++) { |
| 661 | if (fds[i] < 0) { |
| 662 | ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]); |
| 663 | /* Return 0 as this is not a fatal error. */ |
| 664 | } |
| 665 | } |
| 666 | end: |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | static |
| 671 | int send_fds_to_worker(const struct run_as_worker *worker, |
| 672 | const struct run_as_data *data) |
| 673 | { |
| 674 | int ret = 0; |
| 675 | unsigned int i; |
| 676 | |
| 677 | if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) { |
| 678 | goto end; |
| 679 | } |
| 680 | |
| 681 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { |
| 682 | if (COMMAND_IN_FDS(data)[i] < 0) { |
| 683 | ERR("Refusing to send invalid fd to worker (fd = %i)", |
| 684 | COMMAND_IN_FDS(data)[i]); |
| 685 | ret = -1; |
| 686 | goto end; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data), |
| 691 | COMMAND_IN_FD_COUNT(data)); |
| 692 | if (ret < 0) { |
| 693 | PERROR("Failed to send file descriptor to run-as worker"); |
| 694 | ret = -1; |
| 695 | goto end; |
| 696 | } |
| 697 | end: |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | static |
| 702 | int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, |
| 703 | struct run_as_ret *run_as_ret) |
| 704 | { |
| 705 | int ret = 0; |
| 706 | unsigned int i; |
| 707 | |
| 708 | if (COMMAND_OUT_FD_COUNT(cmd) == 0) { |
| 709 | goto end; |
| 710 | } |
| 711 | |
| 712 | ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret), |
| 713 | COMMAND_OUT_FD_COUNT(cmd)); |
| 714 | if (ret < 0) { |
| 715 | PERROR("Failed to send file descriptor to master process"); |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) { |
| 720 | int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]); |
| 721 | |
| 722 | if (ret_close < 0) { |
| 723 | PERROR("Failed to close result file descriptor"); |
| 724 | } |
| 725 | } |
| 726 | end: |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | static |
| 731 | int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd, |
| 732 | struct run_as_ret *run_as_ret) |
| 733 | { |
| 734 | int ret = 0; |
| 735 | |
| 736 | if (COMMAND_OUT_FD_COUNT(cmd) == 0) { |
| 737 | goto end; |
| 738 | } |
| 739 | |
| 740 | ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret), |
| 741 | COMMAND_OUT_FD_COUNT(cmd)); |
| 742 | if (ret < 0) { |
| 743 | PERROR("Failed to receive file descriptor from run-as worker"); |
| 744 | ret = -1; |
| 745 | } |
| 746 | end: |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | static |
| 751 | int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data) |
| 752 | { |
| 753 | int ret = 0; |
| 754 | |
| 755 | if (COMMAND_USE_CWD_FD(data)) { |
| 756 | unsigned int i; |
| 757 | |
| 758 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { |
| 759 | COMMAND_IN_FDS(data)[i] = AT_FDCWD; |
| 760 | } |
| 761 | goto end; |
| 762 | } |
| 763 | |
| 764 | ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data), |
| 765 | COMMAND_IN_FD_COUNT(data)); |
| 766 | if (ret < 0) { |
| 767 | PERROR("Failed to receive file descriptors from master process"); |
| 768 | ret = -1; |
| 769 | } |
| 770 | end: |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | static |
| 775 | int cleanup_received_fds(struct run_as_data *data) |
| 776 | { |
| 777 | int ret = 0, i; |
| 778 | |
| 779 | for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) { |
| 780 | if (COMMAND_IN_FDS(data)[i] == -1) { |
| 781 | continue; |
| 782 | } |
| 783 | ret = close(COMMAND_IN_FDS(data)[i]); |
| 784 | if (ret) { |
| 785 | PERROR("Failed to close file descriptor received fd in run-as worker"); |
| 786 | goto end; |
| 787 | } |
| 788 | } |
| 789 | end: |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Return < 0 on error, 0 if OK, 1 on hangup. |
| 795 | */ |
| 796 | static |
| 797 | int handle_one_cmd(struct run_as_worker *worker) |
| 798 | { |
| 799 | int ret = 0; |
| 800 | struct run_as_data data = {}; |
| 801 | ssize_t readlen, writelen; |
| 802 | struct run_as_ret sendret = {}; |
| 803 | run_as_fct cmd; |
| 804 | uid_t prev_euid; |
| 805 | |
| 806 | /* |
| 807 | * Stage 1: Receive run_as_data struct from the master. |
| 808 | * The structure contains the command type and all the parameters needed for |
| 809 | * its execution |
| 810 | */ |
| 811 | readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data, |
| 812 | sizeof(data)); |
| 813 | if (readlen == 0) { |
| 814 | /* hang up */ |
| 815 | ret = 1; |
| 816 | goto end; |
| 817 | } |
| 818 | if (readlen < sizeof(data)) { |
| 819 | PERROR("lttcomm_recv_unix_sock error"); |
| 820 | ret = -1; |
| 821 | goto end; |
| 822 | } |
| 823 | |
| 824 | cmd = run_as_enum_to_fct(data.cmd); |
| 825 | if (!cmd) { |
| 826 | ret = -1; |
| 827 | goto end; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Stage 2: Receive file descriptor from master. |
| 832 | * Some commands need a file descriptor as input so if it's needed we |
| 833 | * receive the fd using the Unix socket. |
| 834 | */ |
| 835 | ret = recv_fds_from_master(worker, &data); |
| 836 | if (ret < 0) { |
| 837 | PERROR("recv_fd_from_master error"); |
| 838 | ret = -1; |
| 839 | goto end; |
| 840 | } |
| 841 | |
| 842 | prev_euid = getuid(); |
| 843 | if (data.gid != getegid()) { |
| 844 | ret = setegid(data.gid); |
| 845 | if (ret < 0) { |
| 846 | sendret._error = true; |
| 847 | sendret._errno = errno; |
| 848 | PERROR("setegid"); |
| 849 | goto write_return; |
| 850 | } |
| 851 | } |
| 852 | if (data.uid != prev_euid) { |
| 853 | ret = seteuid(data.uid); |
| 854 | if (ret < 0) { |
| 855 | sendret._error = true; |
| 856 | sendret._errno = errno; |
| 857 | PERROR("seteuid"); |
| 858 | goto write_return; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Also set umask to 0 for mkdir executable bit. |
| 864 | */ |
| 865 | umask(0); |
| 866 | |
| 867 | /* |
| 868 | * Stage 3: Execute the command |
| 869 | */ |
| 870 | ret = (*cmd)(&data, &sendret); |
| 871 | if (ret < 0) { |
| 872 | DBG("Execution of command returned an error"); |
| 873 | } |
| 874 | |
| 875 | write_return: |
| 876 | ret = cleanup_received_fds(&data); |
| 877 | if (ret < 0) { |
| 878 | ERR("Error cleaning up FD"); |
| 879 | goto end; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Stage 4: Send run_as_ret structure to the master. |
| 884 | * This structure contain the return value of the command and the errno. |
| 885 | */ |
| 886 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
| 887 | sizeof(sendret)); |
| 888 | if (writelen < sizeof(sendret)) { |
| 889 | PERROR("lttcomm_send_unix_sock error"); |
| 890 | ret = -1; |
| 891 | goto end; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Stage 5: Send resulting file descriptors to the master. |
| 896 | */ |
| 897 | ret = send_fds_to_master(worker, data.cmd, &sendret); |
| 898 | if (ret < 0) { |
| 899 | DBG("Sending FD to master returned an error"); |
| 900 | goto end; |
| 901 | } |
| 902 | |
| 903 | if (seteuid(prev_euid) < 0) { |
| 904 | PERROR("seteuid"); |
| 905 | ret = -1; |
| 906 | goto end; |
| 907 | } |
| 908 | ret = 0; |
| 909 | end: |
| 910 | return ret; |
| 911 | } |
| 912 | |
| 913 | static |
| 914 | int run_as_worker(struct run_as_worker *worker) |
| 915 | { |
| 916 | int ret; |
| 917 | ssize_t writelen; |
| 918 | struct run_as_ret sendret; |
| 919 | size_t proc_orig_len; |
| 920 | |
| 921 | /* |
| 922 | * Initialize worker. Set a different process cmdline. |
| 923 | */ |
| 924 | proc_orig_len = strlen(worker->procname); |
| 925 | memset(worker->procname, 0, proc_orig_len); |
| 926 | strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len); |
| 927 | |
| 928 | ret = lttng_prctl(PR_SET_NAME, |
| 929 | (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); |
| 930 | if (ret && ret != -ENOSYS) { |
| 931 | /* Don't fail as this is not essential. */ |
| 932 | PERROR("prctl PR_SET_NAME"); |
| 933 | } |
| 934 | |
| 935 | memset(&sendret, 0, sizeof(sendret)); |
| 936 | |
| 937 | writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret, |
| 938 | sizeof(sendret)); |
| 939 | if (writelen < sizeof(sendret)) { |
| 940 | PERROR("lttcomm_send_unix_sock error"); |
| 941 | ret = EXIT_FAILURE; |
| 942 | goto end; |
| 943 | } |
| 944 | |
| 945 | for (;;) { |
| 946 | ret = handle_one_cmd(worker); |
| 947 | if (ret < 0) { |
| 948 | ret = EXIT_FAILURE; |
| 949 | goto end; |
| 950 | } else if (ret > 0) { |
| 951 | break; |
| 952 | } else { |
| 953 | continue; /* Next command. */ |
| 954 | } |
| 955 | } |
| 956 | ret = EXIT_SUCCESS; |
| 957 | end: |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | static |
| 962 | int run_as_cmd(struct run_as_worker *worker, |
| 963 | enum run_as_cmd cmd, |
| 964 | struct run_as_data *data, |
| 965 | struct run_as_ret *ret_value, |
| 966 | uid_t uid, gid_t gid) |
| 967 | { |
| 968 | int ret = 0; |
| 969 | ssize_t readlen, writelen; |
| 970 | |
| 971 | /* |
| 972 | * If we are non-root, we can only deal with our own uid. |
| 973 | */ |
| 974 | if (geteuid() != 0) { |
| 975 | if (uid != geteuid()) { |
| 976 | ret = -1; |
| 977 | ret_value->_errno = EPERM; |
| 978 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", |
| 979 | (int) uid, (int) geteuid()); |
| 980 | goto end; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | data->cmd = cmd; |
| 985 | data->uid = uid; |
| 986 | data->gid = gid; |
| 987 | |
| 988 | /* |
| 989 | * Stage 1: Send the run_as_data struct to the worker process |
| 990 | */ |
| 991 | writelen = lttcomm_send_unix_sock(worker->sockpair[0], data, |
| 992 | sizeof(*data)); |
| 993 | if (writelen < sizeof(*data)) { |
| 994 | PERROR("Error writing message to run_as"); |
| 995 | ret = -1; |
| 996 | ret_value->_errno = EIO; |
| 997 | goto end; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Stage 2: Send file descriptor to the worker process if needed |
| 1002 | */ |
| 1003 | ret = send_fds_to_worker(worker, data); |
| 1004 | if (ret) { |
| 1005 | PERROR("do_send_fd error"); |
| 1006 | ret = -1; |
| 1007 | ret_value->_errno = EIO; |
| 1008 | goto end; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Stage 3: Wait for the execution of the command |
| 1013 | */ |
| 1014 | |
| 1015 | /* |
| 1016 | * Stage 4: Receive the run_as_ret struct containing the return value and |
| 1017 | * errno |
| 1018 | */ |
| 1019 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value, |
| 1020 | sizeof(*ret_value)); |
| 1021 | if (!readlen) { |
| 1022 | ERR("Run-as worker has hung-up during run_as_cmd"); |
| 1023 | ret = -1; |
| 1024 | ret_value->_errno = EIO; |
| 1025 | goto end; |
| 1026 | } else if (readlen < sizeof(*ret_value)) { |
| 1027 | PERROR("Error reading response from run_as"); |
| 1028 | ret = -1; |
| 1029 | ret_value->_errno = errno; |
| 1030 | goto end; |
| 1031 | } |
| 1032 | |
| 1033 | if (ret_value->_error) { |
| 1034 | /* Skip stage 5 on error as there will be no fd to receive. */ |
| 1035 | goto end; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Stage 5: Receive file descriptor if needed |
| 1040 | */ |
| 1041 | ret = recv_fds_from_worker(worker, cmd, ret_value); |
| 1042 | if (ret < 0) { |
| 1043 | ERR("Error receiving fd"); |
| 1044 | ret = -1; |
| 1045 | ret_value->_errno = EIO; |
| 1046 | } |
| 1047 | |
| 1048 | end: |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * This is for debugging ONLY and should not be considered secure. |
| 1054 | */ |
| 1055 | static |
| 1056 | int run_as_noworker(enum run_as_cmd cmd, |
| 1057 | struct run_as_data *data, struct run_as_ret *ret_value, |
| 1058 | uid_t uid, gid_t gid) |
| 1059 | { |
| 1060 | int ret, saved_errno; |
| 1061 | mode_t old_mask; |
| 1062 | run_as_fct fct; |
| 1063 | |
| 1064 | fct = run_as_enum_to_fct(cmd); |
| 1065 | if (!fct) { |
| 1066 | errno = -ENOSYS; |
| 1067 | ret = -1; |
| 1068 | goto end; |
| 1069 | } |
| 1070 | old_mask = umask(0); |
| 1071 | ret = fct(data, ret_value); |
| 1072 | saved_errno = ret_value->_errno; |
| 1073 | umask(old_mask); |
| 1074 | errno = saved_errno; |
| 1075 | end: |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
| 1079 | static |
| 1080 | int reset_sighandler(void) |
| 1081 | { |
| 1082 | int sig; |
| 1083 | |
| 1084 | DBG("Resetting run_as worker signal handlers to default"); |
| 1085 | for (sig = 1; sig <= 31; sig++) { |
| 1086 | (void) signal(sig, SIG_DFL); |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static |
| 1092 | void worker_sighandler(int sig) |
| 1093 | { |
| 1094 | const char *signame; |
| 1095 | |
| 1096 | /* |
| 1097 | * The worker will inherit its parent's signals since they are part of |
| 1098 | * the same process group. However, in the case of SIGINT and SIGTERM, |
| 1099 | * we want to give the worker a chance to teardown gracefully when its |
| 1100 | * parent closes the command socket. |
| 1101 | */ |
| 1102 | switch (sig) { |
| 1103 | case SIGINT: |
| 1104 | signame = "SIGINT"; |
| 1105 | break; |
| 1106 | case SIGTERM: |
| 1107 | signame = "SIGTERM"; |
| 1108 | break; |
| 1109 | default: |
| 1110 | signame = NULL; |
| 1111 | } |
| 1112 | |
| 1113 | if (signame) { |
| 1114 | DBG("run_as worker received signal %s", signame); |
| 1115 | } else { |
| 1116 | DBG("run_as_worker received signal %d", sig); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | static |
| 1121 | int set_worker_sighandlers(void) |
| 1122 | { |
| 1123 | int ret = 0; |
| 1124 | sigset_t sigset; |
| 1125 | struct sigaction sa; |
| 1126 | |
| 1127 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 1128 | PERROR("sigemptyset"); |
| 1129 | goto end; |
| 1130 | } |
| 1131 | |
| 1132 | sa.sa_handler = worker_sighandler; |
| 1133 | sa.sa_mask = sigset; |
| 1134 | sa.sa_flags = 0; |
| 1135 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 1136 | PERROR("sigaction SIGINT"); |
| 1137 | goto end; |
| 1138 | } |
| 1139 | |
| 1140 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 1141 | PERROR("sigaction SIGTERM"); |
| 1142 | goto end; |
| 1143 | } |
| 1144 | |
| 1145 | DBG("run_as signal handler set for SIGTERM and SIGINT"); |
| 1146 | end: |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | static |
| 1151 | int run_as_create_worker_no_lock(const char *procname, |
| 1152 | post_fork_cleanup_cb clean_up_func, |
| 1153 | void *clean_up_user_data) |
| 1154 | { |
| 1155 | pid_t pid; |
| 1156 | int i, ret = 0; |
| 1157 | ssize_t readlen; |
| 1158 | struct run_as_ret recvret; |
| 1159 | struct run_as_worker *worker; |
| 1160 | |
| 1161 | assert(!global_worker); |
| 1162 | if (!use_clone()) { |
| 1163 | /* |
| 1164 | * Don't initialize a worker, all run_as tasks will be performed |
| 1165 | * in the current process. |
| 1166 | */ |
| 1167 | ret = 0; |
| 1168 | goto end; |
| 1169 | } |
| 1170 | worker = zmalloc(sizeof(*worker)); |
| 1171 | if (!worker) { |
| 1172 | ret = -ENOMEM; |
| 1173 | goto end; |
| 1174 | } |
| 1175 | worker->procname = strdup(procname); |
| 1176 | if (!worker->procname) { |
| 1177 | ret = -ENOMEM; |
| 1178 | goto error_procname_alloc; |
| 1179 | } |
| 1180 | /* Create unix socket. */ |
| 1181 | if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) { |
| 1182 | ret = -1; |
| 1183 | goto error_sock; |
| 1184 | } |
| 1185 | |
| 1186 | /* Fork worker. */ |
| 1187 | pid = fork(); |
| 1188 | if (pid < 0) { |
| 1189 | PERROR("fork"); |
| 1190 | ret = -1; |
| 1191 | goto error_fork; |
| 1192 | } else if (pid == 0) { |
| 1193 | /* Child */ |
| 1194 | |
| 1195 | reset_sighandler(); |
| 1196 | |
| 1197 | set_worker_sighandlers(); |
| 1198 | if (clean_up_func) { |
| 1199 | if (clean_up_func(clean_up_user_data) < 0) { |
| 1200 | ERR("Run-as post-fork clean-up failed, exiting."); |
| 1201 | exit(EXIT_FAILURE); |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /* Just close, no shutdown. */ |
| 1206 | if (close(worker->sockpair[0])) { |
| 1207 | PERROR("close"); |
| 1208 | exit(EXIT_FAILURE); |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1] |
| 1213 | * Sockpair[1] is used as a control channel with the master |
| 1214 | */ |
| 1215 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { |
| 1216 | if (i != worker->sockpair[1]) { |
| 1217 | (void) close(i); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | worker->sockpair[0] = -1; |
| 1222 | ret = run_as_worker(worker); |
| 1223 | if (lttcomm_close_unix_sock(worker->sockpair[1])) { |
| 1224 | PERROR("close"); |
| 1225 | ret = -1; |
| 1226 | } |
| 1227 | worker->sockpair[1] = -1; |
| 1228 | free(worker->procname); |
| 1229 | free(worker); |
| 1230 | LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret); |
| 1231 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); |
| 1232 | } else { |
| 1233 | /* Parent */ |
| 1234 | |
| 1235 | /* Just close, no shutdown. */ |
| 1236 | if (close(worker->sockpair[1])) { |
| 1237 | PERROR("close"); |
| 1238 | ret = -1; |
| 1239 | goto error_fork; |
| 1240 | } |
| 1241 | worker->sockpair[1] = -1; |
| 1242 | worker->pid = pid; |
| 1243 | /* Wait for worker to become ready. */ |
| 1244 | readlen = lttcomm_recv_unix_sock(worker->sockpair[0], |
| 1245 | &recvret, sizeof(recvret)); |
| 1246 | if (readlen < sizeof(recvret)) { |
| 1247 | ERR("readlen: %zd", readlen); |
| 1248 | PERROR("Error reading response from run_as at creation"); |
| 1249 | ret = -1; |
| 1250 | goto error_fork; |
| 1251 | } |
| 1252 | global_worker = worker; |
| 1253 | } |
| 1254 | end: |
| 1255 | return ret; |
| 1256 | |
| 1257 | /* Error handling. */ |
| 1258 | error_fork: |
| 1259 | for (i = 0; i < 2; i++) { |
| 1260 | if (worker->sockpair[i] < 0) { |
| 1261 | continue; |
| 1262 | } |
| 1263 | if (lttcomm_close_unix_sock(worker->sockpair[i])) { |
| 1264 | PERROR("close"); |
| 1265 | } |
| 1266 | worker->sockpair[i] = -1; |
| 1267 | } |
| 1268 | error_sock: |
| 1269 | free(worker->procname); |
| 1270 | error_procname_alloc: |
| 1271 | free(worker); |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | static |
| 1276 | void run_as_destroy_worker_no_lock(void) |
| 1277 | { |
| 1278 | struct run_as_worker *worker = global_worker; |
| 1279 | |
| 1280 | DBG("Destroying run_as worker"); |
| 1281 | if (!worker) { |
| 1282 | return; |
| 1283 | } |
| 1284 | /* Close unix socket */ |
| 1285 | DBG("Closing run_as worker socket"); |
| 1286 | if (lttcomm_close_unix_sock(worker->sockpair[0])) { |
| 1287 | PERROR("close"); |
| 1288 | } |
| 1289 | worker->sockpair[0] = -1; |
| 1290 | /* Wait for worker. */ |
| 1291 | for (;;) { |
| 1292 | int status; |
| 1293 | pid_t wait_ret; |
| 1294 | |
| 1295 | wait_ret = waitpid(worker->pid, &status, 0); |
| 1296 | if (wait_ret < 0) { |
| 1297 | if (errno == EINTR) { |
| 1298 | continue; |
| 1299 | } |
| 1300 | PERROR("waitpid"); |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | if (WIFEXITED(status)) { |
| 1305 | LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR, |
| 1306 | DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d", |
| 1307 | WEXITSTATUS(status)); |
| 1308 | break; |
| 1309 | } else if (WIFSIGNALED(status)) { |
| 1310 | ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d", |
| 1311 | WTERMSIG(status)); |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | free(worker->procname); |
| 1316 | free(worker); |
| 1317 | global_worker = NULL; |
| 1318 | } |
| 1319 | |
| 1320 | static |
| 1321 | int run_as_restart_worker(struct run_as_worker *worker) |
| 1322 | { |
| 1323 | int ret = 0; |
| 1324 | char *procname = NULL; |
| 1325 | |
| 1326 | procname = worker->procname; |
| 1327 | |
| 1328 | /* Close socket to run_as worker process and clean up the zombie process */ |
| 1329 | run_as_destroy_worker_no_lock(); |
| 1330 | |
| 1331 | /* Create a new run_as worker process*/ |
| 1332 | ret = run_as_create_worker_no_lock(procname, NULL, NULL); |
| 1333 | if (ret < 0 ) { |
| 1334 | ERR("Restarting the worker process failed"); |
| 1335 | ret = -1; |
| 1336 | goto err; |
| 1337 | } |
| 1338 | err: |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static |
| 1343 | int run_as(enum run_as_cmd cmd, struct run_as_data *data, |
| 1344 | struct run_as_ret *ret_value, uid_t uid, gid_t gid) |
| 1345 | { |
| 1346 | int ret, saved_errno; |
| 1347 | |
| 1348 | pthread_mutex_lock(&worker_lock); |
| 1349 | if (use_clone()) { |
| 1350 | DBG("Using run_as worker"); |
| 1351 | |
| 1352 | assert(global_worker); |
| 1353 | |
| 1354 | ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid); |
| 1355 | saved_errno = ret_value->_errno; |
| 1356 | |
| 1357 | /* |
| 1358 | * If the worker thread crashed the errno is set to EIO. we log |
| 1359 | * the error and start a new worker process. |
| 1360 | */ |
| 1361 | if (ret == -1 && saved_errno == EIO) { |
| 1362 | DBG("Socket closed unexpectedly... " |
| 1363 | "Restarting the worker process"); |
| 1364 | ret = run_as_restart_worker(global_worker); |
| 1365 | |
| 1366 | if (ret == -1) { |
| 1367 | ERR("Failed to restart worker process."); |
| 1368 | goto err; |
| 1369 | } |
| 1370 | } |
| 1371 | } else { |
| 1372 | DBG("Using run_as without worker"); |
| 1373 | ret = run_as_noworker(cmd, data, ret_value, uid, gid); |
| 1374 | } |
| 1375 | err: |
| 1376 | pthread_mutex_unlock(&worker_lock); |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | LTTNG_HIDDEN |
| 1381 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
| 1382 | { |
| 1383 | return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid); |
| 1384 | } |
| 1385 | |
| 1386 | LTTNG_HIDDEN |
| 1387 | int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode, |
| 1388 | uid_t uid, gid_t gid) |
| 1389 | { |
| 1390 | int ret; |
| 1391 | struct run_as_data data = {}; |
| 1392 | struct run_as_ret run_as_ret = {}; |
| 1393 | |
| 1394 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", |
| 1395 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1396 | path, (int) mode, (int) uid, (int) gid); |
| 1397 | ret = lttng_strncpy(data.u.mkdir.path, path, |
| 1398 | sizeof(data.u.mkdir.path)); |
| 1399 | if (ret) { |
| 1400 | ERR("Failed to copy path argument of mkdirat recursive command"); |
| 1401 | goto error; |
| 1402 | } |
| 1403 | data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0'; |
| 1404 | data.u.mkdir.mode = mode; |
| 1405 | data.u.mkdir.dirfd = dirfd; |
| 1406 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE, |
| 1407 | &data, &run_as_ret, uid, gid); |
| 1408 | errno = run_as_ret._errno; |
| 1409 | ret = run_as_ret.u.ret; |
| 1410 | error: |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | LTTNG_HIDDEN |
| 1415 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
| 1416 | { |
| 1417 | return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid); |
| 1418 | } |
| 1419 | |
| 1420 | LTTNG_HIDDEN |
| 1421 | int run_as_mkdirat(int dirfd, const char *path, mode_t mode, |
| 1422 | uid_t uid, gid_t gid) |
| 1423 | { |
| 1424 | int ret; |
| 1425 | struct run_as_data data = {}; |
| 1426 | struct run_as_ret run_as_ret = {}; |
| 1427 | |
| 1428 | DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d", |
| 1429 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1430 | path, (int) mode, (int) uid, (int) gid); |
| 1431 | ret = lttng_strncpy(data.u.mkdir.path, path, |
| 1432 | sizeof(data.u.mkdir.path)); |
| 1433 | if (ret) { |
| 1434 | ERR("Failed to copy path argument of mkdirat command"); |
| 1435 | goto error; |
| 1436 | } |
| 1437 | data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0'; |
| 1438 | data.u.mkdir.mode = mode; |
| 1439 | data.u.mkdir.dirfd = dirfd; |
| 1440 | run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT, |
| 1441 | &data, &run_as_ret, uid, gid); |
| 1442 | errno = run_as_ret._errno; |
| 1443 | ret = run_as_ret.u.ret; |
| 1444 | error: |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
| 1448 | LTTNG_HIDDEN |
| 1449 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, |
| 1450 | gid_t gid) |
| 1451 | { |
| 1452 | return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid); |
| 1453 | } |
| 1454 | |
| 1455 | LTTNG_HIDDEN |
| 1456 | int run_as_openat(int dirfd, const char *path, int flags, mode_t mode, |
| 1457 | uid_t uid, gid_t gid) |
| 1458 | { |
| 1459 | int ret; |
| 1460 | struct run_as_data data = {}; |
| 1461 | struct run_as_ret run_as_ret = {}; |
| 1462 | |
| 1463 | DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d", |
| 1464 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1465 | path, flags, (int) mode, (int) uid, (int) gid); |
| 1466 | ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path)); |
| 1467 | if (ret) { |
| 1468 | ERR("Failed to copy path argument of open command"); |
| 1469 | goto error; |
| 1470 | } |
| 1471 | data.u.open.flags = flags; |
| 1472 | data.u.open.mode = mode; |
| 1473 | data.u.open.dirfd = dirfd; |
| 1474 | run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT, |
| 1475 | &data, &run_as_ret, uid, gid); |
| 1476 | errno = run_as_ret._errno; |
| 1477 | ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret : |
| 1478 | run_as_ret.u.open.fd; |
| 1479 | error: |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | LTTNG_HIDDEN |
| 1484 | int run_as_unlink(const char *path, uid_t uid, gid_t gid) |
| 1485 | { |
| 1486 | return run_as_unlinkat(AT_FDCWD, path, uid, gid); |
| 1487 | } |
| 1488 | |
| 1489 | LTTNG_HIDDEN |
| 1490 | int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid) |
| 1491 | { |
| 1492 | int ret; |
| 1493 | struct run_as_data data = {}; |
| 1494 | struct run_as_ret run_as_ret = {}; |
| 1495 | |
| 1496 | DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d", |
| 1497 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1498 | path, (int) uid, (int) gid); |
| 1499 | ret = lttng_strncpy(data.u.unlink.path, path, |
| 1500 | sizeof(data.u.unlink.path)); |
| 1501 | if (ret) { |
| 1502 | goto error; |
| 1503 | } |
| 1504 | data.u.unlink.dirfd = dirfd; |
| 1505 | run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data, |
| 1506 | &run_as_ret, uid, gid); |
| 1507 | errno = run_as_ret._errno; |
| 1508 | ret = run_as_ret.u.ret; |
| 1509 | error: |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | LTTNG_HIDDEN |
| 1514 | int run_as_rmdir(const char *path, uid_t uid, gid_t gid) |
| 1515 | { |
| 1516 | return run_as_rmdirat(AT_FDCWD, path, uid, gid); |
| 1517 | } |
| 1518 | |
| 1519 | LTTNG_HIDDEN |
| 1520 | int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid) |
| 1521 | { |
| 1522 | int ret; |
| 1523 | struct run_as_data data = {}; |
| 1524 | struct run_as_ret run_as_ret = {}; |
| 1525 | |
| 1526 | DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d", |
| 1527 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1528 | path, (int) uid, (int) gid); |
| 1529 | ret = lttng_strncpy(data.u.rmdir.path, path, |
| 1530 | sizeof(data.u.rmdir.path)); |
| 1531 | if (ret) { |
| 1532 | goto error; |
| 1533 | } |
| 1534 | data.u.rmdir.dirfd = dirfd; |
| 1535 | run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data, |
| 1536 | &run_as_ret, uid, gid); |
| 1537 | errno = run_as_ret._errno; |
| 1538 | ret = run_as_ret.u.ret; |
| 1539 | error: |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | LTTNG_HIDDEN |
| 1544 | int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags) |
| 1545 | { |
| 1546 | return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags); |
| 1547 | } |
| 1548 | |
| 1549 | LTTNG_HIDDEN |
| 1550 | int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) |
| 1551 | { |
| 1552 | int ret; |
| 1553 | struct run_as_data data = {}; |
| 1554 | struct run_as_ret run_as_ret = {}; |
| 1555 | |
| 1556 | DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d", |
| 1557 | dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1558 | path, (int) uid, (int) gid); |
| 1559 | ret = lttng_strncpy(data.u.rmdir.path, path, |
| 1560 | sizeof(data.u.rmdir.path)); |
| 1561 | if (ret) { |
| 1562 | goto error; |
| 1563 | } |
| 1564 | data.u.rmdir.dirfd = dirfd; |
| 1565 | data.u.rmdir.flags = flags; |
| 1566 | run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE, |
| 1567 | &data, &run_as_ret, uid, gid); |
| 1568 | errno = run_as_ret._errno; |
| 1569 | ret = run_as_ret.u.ret; |
| 1570 | error: |
| 1571 | return ret; |
| 1572 | } |
| 1573 | |
| 1574 | LTTNG_HIDDEN |
| 1575 | int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid) |
| 1576 | { |
| 1577 | return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid); |
| 1578 | } |
| 1579 | |
| 1580 | LTTNG_HIDDEN |
| 1581 | int run_as_renameat(int old_dirfd, const char *old_name, |
| 1582 | int new_dirfd, const char *new_name, uid_t uid, gid_t gid) |
| 1583 | { |
| 1584 | int ret; |
| 1585 | struct run_as_data data = {}; |
| 1586 | struct run_as_ret run_as_ret = {}; |
| 1587 | |
| 1588 | DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d", |
| 1589 | old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1590 | old_name, |
| 1591 | new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "", |
| 1592 | new_name, (int) uid, (int) gid); |
| 1593 | ret = lttng_strncpy(data.u.rename.old_path, old_name, |
| 1594 | sizeof(data.u.rename.old_path)); |
| 1595 | if (ret) { |
| 1596 | goto error; |
| 1597 | } |
| 1598 | ret = lttng_strncpy(data.u.rename.new_path, new_name, |
| 1599 | sizeof(data.u.rename.new_path)); |
| 1600 | if (ret) { |
| 1601 | goto error; |
| 1602 | } |
| 1603 | |
| 1604 | data.u.rename.dirfds[0] = old_dirfd; |
| 1605 | data.u.rename.dirfds[1] = new_dirfd; |
| 1606 | run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ? |
| 1607 | RUN_AS_RENAME : RUN_AS_RENAMEAT, |
| 1608 | &data, &run_as_ret, uid, gid); |
| 1609 | errno = run_as_ret._errno; |
| 1610 | ret = run_as_ret.u.ret; |
| 1611 | error: |
| 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | LTTNG_HIDDEN |
| 1616 | int run_as_extract_elf_symbol_offset(int fd, const char* function, |
| 1617 | uid_t uid, gid_t gid, uint64_t *offset) |
| 1618 | { |
| 1619 | int ret; |
| 1620 | struct run_as_data data = {}; |
| 1621 | struct run_as_ret run_as_ret = {}; |
| 1622 | |
| 1623 | DBG3("extract_elf_symbol_offset() on fd=%d and function=%s " |
| 1624 | "with for uid %d and gid %d", fd, function, |
| 1625 | (int) uid, (int) gid); |
| 1626 | |
| 1627 | data.u.extract_elf_symbol_offset.fd = fd; |
| 1628 | |
| 1629 | strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1); |
| 1630 | data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 1631 | ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function, |
| 1632 | function, |
| 1633 | sizeof(data.u.extract_elf_symbol_offset.function)); |
| 1634 | if (ret) { |
| 1635 | goto error; |
| 1636 | } |
| 1637 | |
| 1638 | run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid); |
| 1639 | errno = run_as_ret._errno; |
| 1640 | if (run_as_ret._error) { |
| 1641 | ret = -1; |
| 1642 | goto error; |
| 1643 | } |
| 1644 | |
| 1645 | *offset = run_as_ret.u.extract_elf_symbol_offset.offset; |
| 1646 | error: |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | LTTNG_HIDDEN |
| 1651 | int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name, |
| 1652 | const char* probe_name, uid_t uid, gid_t gid, |
| 1653 | uint64_t **offsets, uint32_t *num_offset) |
| 1654 | { |
| 1655 | int ret; |
| 1656 | struct run_as_data data = {}; |
| 1657 | struct run_as_ret run_as_ret = {}; |
| 1658 | |
| 1659 | DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and " |
| 1660 | "provider_name=%s with for uid %d and gid %d", fd, |
| 1661 | probe_name, provider_name, (int) uid, (int) gid); |
| 1662 | |
| 1663 | data.u.extract_sdt_probe_offsets.fd = fd; |
| 1664 | |
| 1665 | ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, |
| 1666 | sizeof(data.u.extract_sdt_probe_offsets.probe_name)); |
| 1667 | if (ret) { |
| 1668 | goto error; |
| 1669 | } |
| 1670 | ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name, |
| 1671 | provider_name, |
| 1672 | sizeof(data.u.extract_sdt_probe_offsets.provider_name)); |
| 1673 | if (ret) { |
| 1674 | goto error; |
| 1675 | } |
| 1676 | |
| 1677 | run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid); |
| 1678 | errno = run_as_ret._errno; |
| 1679 | if (run_as_ret._error) { |
| 1680 | ret = -1; |
| 1681 | goto error; |
| 1682 | } |
| 1683 | |
| 1684 | *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset; |
| 1685 | *offsets = zmalloc(*num_offset * sizeof(uint64_t)); |
| 1686 | if (!*offsets) { |
| 1687 | ret = -ENOMEM; |
| 1688 | goto error; |
| 1689 | } |
| 1690 | |
| 1691 | memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets, |
| 1692 | *num_offset * sizeof(uint64_t)); |
| 1693 | error: |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
| 1697 | LTTNG_HIDDEN |
| 1698 | int run_as_create_worker(const char *procname, |
| 1699 | post_fork_cleanup_cb clean_up_func, |
| 1700 | void *clean_up_user_data) |
| 1701 | { |
| 1702 | int ret; |
| 1703 | |
| 1704 | pthread_mutex_lock(&worker_lock); |
| 1705 | ret = run_as_create_worker_no_lock(procname, clean_up_func, |
| 1706 | clean_up_user_data); |
| 1707 | pthread_mutex_unlock(&worker_lock); |
| 1708 | return ret; |
| 1709 | } |
| 1710 | |
| 1711 | LTTNG_HIDDEN |
| 1712 | void run_as_destroy_worker(void) |
| 1713 | { |
| 1714 | pthread_mutex_lock(&worker_lock); |
| 1715 | run_as_destroy_worker_no_lock(); |
| 1716 | pthread_mutex_unlock(&worker_lock); |
| 1717 | } |