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