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