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