| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/capability.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "audit-util.h" |
| 9 |
#include "bus-creds.h" |
| 10 |
#include "bus-label.h" |
| 11 |
#include "bus-message.h" |
| 12 |
#include "capability-util.h" |
| 13 |
#include "cgroup-util.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "fileio.h" |
| 17 |
#include "format-util.h" |
| 18 |
#include "hexdecoct.h" |
| 19 |
#include "nulstr-util.h" |
| 20 |
#include "parse-util.h" |
| 21 |
#include "pidref.h" |
| 22 |
#include "process-util.h" |
| 23 |
#include "string-util.h" |
| 24 |
#include "strv.h" |
| 25 |
#include "terminal-util.h" |
| 26 |
#include "user-util.h" |
| 27 |
|
| 28 |
enum { |
| 29 |
CAP_OFFSET_INHERITABLE = 0, |
| 30 |
CAP_OFFSET_PERMITTED = 1, |
| 31 |
CAP_OFFSET_EFFECTIVE = 2, |
| 32 |
CAP_OFFSET_BOUNDING = 3 |
| 33 |
}; |
| 34 |
|
| 35 |
void bus_creds_done(sd_bus_creds *c) { |
| 36 |
assert(c); |
| 37 |
|
| 38 |
/* For internal bus cred structures that are allocated by |
| 39 |
* something else */ |
| 40 |
|
| 41 |
free(c->session); |
| 42 |
free(c->unit); |
| 43 |
free(c->user_unit); |
| 44 |
free(c->slice); |
| 45 |
free(c->user_slice); |
| 46 |
free(c->unescaped_description); |
| 47 |
free(c->supplementary_gids); |
| 48 |
free(c->tty); |
| 49 |
|
| 50 |
free(c->well_known_names); /* note that this is an strv, but |
| 51 |
* we only free the array, not the |
| 52 |
* strings the array points to. The |
| 53 |
* full strv we only free if |
| 54 |
* c->allocated is set, see |
| 55 |
* below. */ |
| 56 |
|
| 57 |
strv_free(c->cmdline_array); |
| 58 |
|
| 59 |
safe_close(c->pidfd); |
| 60 |
} |
| 61 |
|
| 62 |
_public_ sd_bus_creds* sd_bus_creds_ref(sd_bus_creds *c) { |
| 63 |
|
| 64 |
if (!c) |
| 65 |
return NULL; |
| 66 |
|
| 67 |
if (c->allocated) { |
| 68 |
assert(c->n_ref > 0); |
| 69 |
c->n_ref++; |
| 70 |
} else { |
| 71 |
sd_bus_message *m; |
| 72 |
|
| 73 |
/* If this is an embedded creds structure, then |
| 74 |
* forward ref counting to the message */ |
| 75 |
m = container_of(c, sd_bus_message, creds); |
| 76 |
sd_bus_message_ref(m); |
| 77 |
} |
| 78 |
|
| 79 |
return c; |
| 80 |
} |
| 81 |
|
| 82 |
_public_ sd_bus_creds* sd_bus_creds_unref(sd_bus_creds *c) { |
| 83 |
|
| 84 |
if (!c) |
| 85 |
return NULL; |
| 86 |
|
| 87 |
if (c->allocated) { |
| 88 |
assert(c->n_ref > 0); |
| 89 |
c->n_ref--; |
| 90 |
|
| 91 |
if (c->n_ref == 0) { |
| 92 |
free(c->comm); |
| 93 |
free(c->tid_comm); |
| 94 |
free(c->exe); |
| 95 |
free(c->cmdline); |
| 96 |
free(c->cgroup); |
| 97 |
free(c->capability); |
| 98 |
free(c->label); |
| 99 |
free(c->unique_name); |
| 100 |
free(c->cgroup_root); |
| 101 |
free(c->description); |
| 102 |
|
| 103 |
c->supplementary_gids = mfree(c->supplementary_gids); |
| 104 |
|
| 105 |
c->well_known_names = strv_free(c->well_known_names); |
| 106 |
|
| 107 |
bus_creds_done(c); |
| 108 |
|
| 109 |
free(c); |
| 110 |
} |
| 111 |
} else { |
| 112 |
sd_bus_message *m; |
| 113 |
|
| 114 |
m = container_of(c, sd_bus_message, creds); |
| 115 |
sd_bus_message_unref(m); |
| 116 |
} |
| 117 |
|
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
_public_ uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c) { |
| 122 |
assert_return(c, 0); |
| 123 |
|
| 124 |
return c->mask; |
| 125 |
} |
| 126 |
|
| 127 |
_public_ uint64_t sd_bus_creds_get_augmented_mask(const sd_bus_creds *c) { |
| 128 |
assert_return(c, 0); |
| 129 |
|
| 130 |
return c->augmented; |
| 131 |
} |
| 132 |
|
| 133 |
sd_bus_creds* bus_creds_new(void) { |
| 134 |
sd_bus_creds *c; |
| 135 |
|
| 136 |
c = new(sd_bus_creds, 1); |
| 137 |
if (!c) |
| 138 |
return NULL; |
| 139 |
|
| 140 |
*c = (sd_bus_creds) { |
| 141 |
.allocated = true, |
| 142 |
.n_ref = 1, |
| 143 |
SD_BUS_CREDS_INIT_FIELDS, |
| 144 |
}; |
| 145 |
|
| 146 |
return c; |
| 147 |
} |
| 148 |
|
| 149 |
static int bus_creds_new_from_pidref(sd_bus_creds **ret, PidRef *pidref, uint64_t mask) { |
| 150 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL; |
| 151 |
int r; |
| 152 |
|
| 153 |
assert_return(mask <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP); |
| 154 |
assert_return(ret, -EINVAL); |
| 155 |
|
| 156 |
c = bus_creds_new(); |
| 157 |
if (!c) |
| 158 |
return -ENOMEM; |
| 159 |
|
| 160 |
r = bus_creds_add_more(c, mask | SD_BUS_CREDS_AUGMENT, pidref, 0); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
r = pidref_verify(pidref); |
| 165 |
if (r < 0) |
| 166 |
return r; |
| 167 |
|
| 168 |
*ret = TAKE_PTR(c); |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
_public_ int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t mask) { |
| 173 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 174 |
int r; |
| 175 |
|
| 176 |
assert_return(pid >= 0, -EINVAL); |
| 177 |
assert_return(mask <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP); |
| 178 |
assert_return(ret, -EINVAL); |
| 179 |
|
| 180 |
r = pidref_set_pid(&pidref, pid); |
| 181 |
if (r < 0) |
| 182 |
return r; |
| 183 |
|
| 184 |
return bus_creds_new_from_pidref(ret, &pidref, mask); |
| 185 |
} |
| 186 |
|
| 187 |
_public_ int sd_bus_creds_new_from_pidfd(sd_bus_creds **ret, int pidfd, uint64_t mask) { |
| 188 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 189 |
int r; |
| 190 |
|
| 191 |
assert_return(mask <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP); |
| 192 |
assert_return(ret, -EINVAL); |
| 193 |
assert_return(pidfd >= 0, -EBADF); |
| 194 |
|
| 195 |
r = pidref_set_pidfd(&pidref, pidfd); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
return bus_creds_new_from_pidref(ret, &pidref, mask); |
| 200 |
} |
| 201 |
|
| 202 |
_public_ int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *ret) { |
| 203 |
assert_return(c, -EINVAL); |
| 204 |
assert_return(ret, -EINVAL); |
| 205 |
|
| 206 |
if (!(c->mask & SD_BUS_CREDS_UID)) |
| 207 |
return -ENODATA; |
| 208 |
|
| 209 |
*ret = c->uid; |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
_public_ int sd_bus_creds_get_euid(sd_bus_creds *c, uid_t *ret) { |
| 214 |
assert_return(c, -EINVAL); |
| 215 |
assert_return(ret, -EINVAL); |
| 216 |
|
| 217 |
if (!(c->mask & SD_BUS_CREDS_EUID)) |
| 218 |
return -ENODATA; |
| 219 |
|
| 220 |
*ret = c->euid; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
_public_ int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *ret) { |
| 225 |
assert_return(c, -EINVAL); |
| 226 |
assert_return(ret, -EINVAL); |
| 227 |
|
| 228 |
if (!(c->mask & SD_BUS_CREDS_SUID)) |
| 229 |
return -ENODATA; |
| 230 |
|
| 231 |
*ret = c->suid; |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
_public_ int sd_bus_creds_get_fsuid(sd_bus_creds *c, uid_t *ret) { |
| 236 |
assert_return(c, -EINVAL); |
| 237 |
assert_return(ret, -EINVAL); |
| 238 |
|
| 239 |
if (!(c->mask & SD_BUS_CREDS_FSUID)) |
| 240 |
return -ENODATA; |
| 241 |
|
| 242 |
*ret = c->fsuid; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
_public_ int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *ret) { |
| 247 |
assert_return(c, -EINVAL); |
| 248 |
assert_return(ret, -EINVAL); |
| 249 |
|
| 250 |
if (!(c->mask & SD_BUS_CREDS_GID)) |
| 251 |
return -ENODATA; |
| 252 |
|
| 253 |
*ret = c->gid; |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
_public_ int sd_bus_creds_get_egid(sd_bus_creds *c, gid_t *ret) { |
| 258 |
assert_return(c, -EINVAL); |
| 259 |
assert_return(ret, -EINVAL); |
| 260 |
|
| 261 |
if (!(c->mask & SD_BUS_CREDS_EGID)) |
| 262 |
return -ENODATA; |
| 263 |
|
| 264 |
*ret = c->egid; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
_public_ int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *ret) { |
| 269 |
assert_return(c, -EINVAL); |
| 270 |
assert_return(ret, -EINVAL); |
| 271 |
|
| 272 |
if (!(c->mask & SD_BUS_CREDS_SGID)) |
| 273 |
return -ENODATA; |
| 274 |
|
| 275 |
*ret = c->sgid; |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
_public_ int sd_bus_creds_get_fsgid(sd_bus_creds *c, gid_t *ret) { |
| 280 |
assert_return(c, -EINVAL); |
| 281 |
assert_return(ret, -EINVAL); |
| 282 |
|
| 283 |
if (!(c->mask & SD_BUS_CREDS_FSGID)) |
| 284 |
return -ENODATA; |
| 285 |
|
| 286 |
*ret = c->fsgid; |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
_public_ int sd_bus_creds_get_supplementary_gids(sd_bus_creds *c, const gid_t **ret) { |
| 291 |
assert_return(c, -EINVAL); |
| 292 |
assert_return(ret, -EINVAL); |
| 293 |
|
| 294 |
if (!(c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS)) |
| 295 |
return -ENODATA; |
| 296 |
|
| 297 |
*ret = c->supplementary_gids; |
| 298 |
return (int) c->n_supplementary_gids; |
| 299 |
} |
| 300 |
|
| 301 |
_public_ int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *ret) { |
| 302 |
assert_return(c, -EINVAL); |
| 303 |
assert_return(ret, -EINVAL); |
| 304 |
|
| 305 |
if (!(c->mask & SD_BUS_CREDS_PID)) |
| 306 |
return -ENODATA; |
| 307 |
|
| 308 |
assert(c->pid > 0); |
| 309 |
*ret = c->pid; |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
_public_ int sd_bus_creds_get_pidfd_dup(sd_bus_creds *c, int *ret) { |
| 314 |
_cleanup_close_ int copy = -EBADF; |
| 315 |
|
| 316 |
assert_return(c, -EINVAL); |
| 317 |
assert_return(ret, -EINVAL); |
| 318 |
|
| 319 |
if (!(c->mask & SD_BUS_CREDS_PIDFD)) |
| 320 |
return -ENODATA; |
| 321 |
|
| 322 |
copy = fcntl(c->pidfd, F_DUPFD_CLOEXEC, 3); |
| 323 |
if (copy < 0) |
| 324 |
return -errno; |
| 325 |
|
| 326 |
*ret = TAKE_FD(copy); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
_public_ int sd_bus_creds_get_ppid(sd_bus_creds *c, pid_t *ret) { |
| 331 |
assert_return(c, -EINVAL); |
| 332 |
assert_return(ret, -EINVAL); |
| 333 |
|
| 334 |
if (!(c->mask & SD_BUS_CREDS_PPID)) |
| 335 |
return -ENODATA; |
| 336 |
|
| 337 |
/* PID 1 has no parent process. Let's distinguish the case of |
| 338 |
* not knowing and not having a parent process by the returned |
| 339 |
* error code. */ |
| 340 |
if (c->ppid == 0) |
| 341 |
return -ENXIO; |
| 342 |
|
| 343 |
*ret = c->ppid; |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
_public_ int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *ret) { |
| 348 |
assert_return(c, -EINVAL); |
| 349 |
assert_return(ret, -EINVAL); |
| 350 |
|
| 351 |
if (!(c->mask & SD_BUS_CREDS_TID)) |
| 352 |
return -ENODATA; |
| 353 |
|
| 354 |
assert(c->tid > 0); |
| 355 |
*ret = c->tid; |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
_public_ int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **ret) { |
| 360 |
assert_return(c, -EINVAL); |
| 361 |
assert_return(ret, -EINVAL); |
| 362 |
|
| 363 |
if (!(c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)) |
| 364 |
return -ENODATA; |
| 365 |
|
| 366 |
assert(c->label); |
| 367 |
*ret = c->label; |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
_public_ int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) { |
| 372 |
assert_return(c, -EINVAL); |
| 373 |
assert_return(ret, -EINVAL); |
| 374 |
|
| 375 |
if (!(c->mask & SD_BUS_CREDS_COMM)) |
| 376 |
return -ENODATA; |
| 377 |
|
| 378 |
assert(c->comm); |
| 379 |
*ret = c->comm; |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
_public_ int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **ret) { |
| 384 |
assert_return(c, -EINVAL); |
| 385 |
assert_return(ret, -EINVAL); |
| 386 |
|
| 387 |
if (!(c->mask & SD_BUS_CREDS_TID_COMM)) |
| 388 |
return -ENODATA; |
| 389 |
|
| 390 |
assert(c->tid_comm); |
| 391 |
*ret = c->tid_comm; |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
_public_ int sd_bus_creds_get_exe(sd_bus_creds *c, const char **ret) { |
| 396 |
assert_return(c, -EINVAL); |
| 397 |
assert_return(ret, -EINVAL); |
| 398 |
|
| 399 |
if (!(c->mask & SD_BUS_CREDS_EXE)) |
| 400 |
return -ENODATA; |
| 401 |
|
| 402 |
if (!c->exe) |
| 403 |
return -ENXIO; |
| 404 |
|
| 405 |
*ret = c->exe; |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
_public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) { |
| 410 |
assert_return(c, -EINVAL); |
| 411 |
assert_return(ret, -EINVAL); |
| 412 |
|
| 413 |
if (!(c->mask & SD_BUS_CREDS_CGROUP)) |
| 414 |
return -ENODATA; |
| 415 |
|
| 416 |
assert(c->cgroup); |
| 417 |
*ret = c->cgroup; |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
_public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) { |
| 422 |
int r; |
| 423 |
|
| 424 |
assert_return(c, -EINVAL); |
| 425 |
assert_return(ret, -EINVAL); |
| 426 |
|
| 427 |
if (!(c->mask & SD_BUS_CREDS_UNIT)) |
| 428 |
return -ENODATA; |
| 429 |
|
| 430 |
assert(c->cgroup); |
| 431 |
|
| 432 |
if (!c->unit) { |
| 433 |
const char *shifted; |
| 434 |
|
| 435 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 436 |
if (r < 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
r = cg_path_get_unit(shifted, &c->unit); |
| 440 |
if (r < 0) |
| 441 |
return r; |
| 442 |
} |
| 443 |
|
| 444 |
*ret = c->unit; |
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
_public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) { |
| 449 |
int r; |
| 450 |
|
| 451 |
assert_return(c, -EINVAL); |
| 452 |
assert_return(ret, -EINVAL); |
| 453 |
|
| 454 |
if (!(c->mask & SD_BUS_CREDS_USER_UNIT)) |
| 455 |
return -ENODATA; |
| 456 |
|
| 457 |
assert(c->cgroup); |
| 458 |
|
| 459 |
if (!c->user_unit) { |
| 460 |
const char *shifted; |
| 461 |
|
| 462 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 463 |
if (r < 0) |
| 464 |
return r; |
| 465 |
|
| 466 |
r = cg_path_get_user_unit(shifted, &c->user_unit); |
| 467 |
if (r < 0) |
| 468 |
return r; |
| 469 |
} |
| 470 |
|
| 471 |
*ret = c->user_unit; |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
_public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) { |
| 476 |
int r; |
| 477 |
|
| 478 |
assert_return(c, -EINVAL); |
| 479 |
assert_return(ret, -EINVAL); |
| 480 |
|
| 481 |
if (!(c->mask & SD_BUS_CREDS_SLICE)) |
| 482 |
return -ENODATA; |
| 483 |
|
| 484 |
assert(c->cgroup); |
| 485 |
|
| 486 |
if (!c->slice) { |
| 487 |
const char *shifted; |
| 488 |
|
| 489 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 490 |
if (r < 0) |
| 491 |
return r; |
| 492 |
|
| 493 |
r = cg_path_get_slice(shifted, &c->slice); |
| 494 |
if (r < 0) |
| 495 |
return r; |
| 496 |
} |
| 497 |
|
| 498 |
*ret = c->slice; |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
_public_ int sd_bus_creds_get_user_slice(sd_bus_creds *c, const char **ret) { |
| 503 |
int r; |
| 504 |
|
| 505 |
assert_return(c, -EINVAL); |
| 506 |
assert_return(ret, -EINVAL); |
| 507 |
|
| 508 |
if (!(c->mask & SD_BUS_CREDS_USER_SLICE)) |
| 509 |
return -ENODATA; |
| 510 |
|
| 511 |
assert(c->cgroup); |
| 512 |
|
| 513 |
if (!c->user_slice) { |
| 514 |
const char *shifted; |
| 515 |
|
| 516 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 517 |
if (r < 0) |
| 518 |
return r; |
| 519 |
|
| 520 |
r = cg_path_get_user_slice(shifted, &c->user_slice); |
| 521 |
if (r < 0) |
| 522 |
return r; |
| 523 |
} |
| 524 |
|
| 525 |
*ret = c->user_slice; |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
_public_ int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) { |
| 530 |
int r; |
| 531 |
|
| 532 |
assert_return(c, -EINVAL); |
| 533 |
assert_return(ret, -EINVAL); |
| 534 |
|
| 535 |
if (!(c->mask & SD_BUS_CREDS_SESSION)) |
| 536 |
return -ENODATA; |
| 537 |
|
| 538 |
assert(c->cgroup); |
| 539 |
|
| 540 |
if (!c->session) { |
| 541 |
const char *shifted; |
| 542 |
|
| 543 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 544 |
if (r < 0) |
| 545 |
return r; |
| 546 |
|
| 547 |
r = cg_path_get_session(shifted, &c->session); |
| 548 |
if (r < 0) |
| 549 |
return r; |
| 550 |
} |
| 551 |
|
| 552 |
*ret = c->session; |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
_public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *ret) { |
| 557 |
const char *shifted; |
| 558 |
int r; |
| 559 |
|
| 560 |
assert_return(c, -EINVAL); |
| 561 |
assert_return(ret, -EINVAL); |
| 562 |
|
| 563 |
if (!(c->mask & SD_BUS_CREDS_OWNER_UID)) |
| 564 |
return -ENODATA; |
| 565 |
|
| 566 |
assert(c->cgroup); |
| 567 |
|
| 568 |
r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); |
| 569 |
if (r < 0) |
| 570 |
return r; |
| 571 |
|
| 572 |
return cg_path_get_owner_uid(shifted, ret); |
| 573 |
} |
| 574 |
|
| 575 |
_public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***ret) { |
| 576 |
assert_return(c, -EINVAL); |
| 577 |
assert_return(ret, -EINVAL); |
| 578 |
|
| 579 |
if (!(c->mask & SD_BUS_CREDS_CMDLINE)) |
| 580 |
return -ENODATA; |
| 581 |
|
| 582 |
if (!c->cmdline) |
| 583 |
return -ENXIO; |
| 584 |
|
| 585 |
if (!c->cmdline_array) { |
| 586 |
c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size); |
| 587 |
if (!c->cmdline_array) |
| 588 |
return -ENOMEM; |
| 589 |
} |
| 590 |
|
| 591 |
*ret = c->cmdline_array; |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
_public_ int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *ret) { |
| 596 |
assert_return(c, -EINVAL); |
| 597 |
assert_return(ret, -EINVAL); |
| 598 |
|
| 599 |
if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID)) |
| 600 |
return -ENODATA; |
| 601 |
|
| 602 |
if (!audit_session_is_valid(c->audit_session_id)) |
| 603 |
return -ENXIO; |
| 604 |
|
| 605 |
*ret = c->audit_session_id; |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
_public_ int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *ret) { |
| 610 |
assert_return(c, -EINVAL); |
| 611 |
assert_return(ret, -EINVAL); |
| 612 |
|
| 613 |
if (!(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID)) |
| 614 |
return -ENODATA; |
| 615 |
|
| 616 |
if (!uid_is_valid(c->audit_login_uid)) |
| 617 |
return -ENXIO; |
| 618 |
|
| 619 |
*ret = c->audit_login_uid; |
| 620 |
return 0; |
| 621 |
} |
| 622 |
|
| 623 |
_public_ int sd_bus_creds_get_tty(sd_bus_creds *c, const char **ret) { |
| 624 |
assert_return(c, -EINVAL); |
| 625 |
assert_return(ret, -EINVAL); |
| 626 |
|
| 627 |
if (!(c->mask & SD_BUS_CREDS_TTY)) |
| 628 |
return -ENODATA; |
| 629 |
|
| 630 |
if (!c->tty) |
| 631 |
return -ENXIO; |
| 632 |
|
| 633 |
*ret = c->tty; |
| 634 |
return 0; |
| 635 |
} |
| 636 |
|
| 637 |
_public_ int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **ret) { |
| 638 |
assert_return(c, -EINVAL); |
| 639 |
assert_return(ret, -EINVAL); |
| 640 |
|
| 641 |
if (!(c->mask & SD_BUS_CREDS_UNIQUE_NAME)) |
| 642 |
return -ENODATA; |
| 643 |
|
| 644 |
*ret = c->unique_name; |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
_public_ int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***ret) { |
| 649 |
assert_return(c, -EINVAL); |
| 650 |
assert_return(ret, -EINVAL); |
| 651 |
|
| 652 |
if (!(c->mask & SD_BUS_CREDS_WELL_KNOWN_NAMES)) |
| 653 |
return -ENODATA; |
| 654 |
|
| 655 |
/* As a special hack we return the bus driver as well-known |
| 656 |
* names list when this is requested. */ |
| 657 |
if (c->well_known_names_driver) { |
| 658 |
static const char* const wkn[] = { |
| 659 |
"org.freedesktop.DBus", |
| 660 |
NULL |
| 661 |
}; |
| 662 |
|
| 663 |
*ret = (char**) wkn; |
| 664 |
return 0; |
| 665 |
} |
| 666 |
|
| 667 |
if (c->well_known_names_local) { |
| 668 |
static const char* const wkn[] = { |
| 669 |
"org.freedesktop.DBus.Local", |
| 670 |
NULL |
| 671 |
}; |
| 672 |
|
| 673 |
*ret = (char**) wkn; |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
*ret = c->well_known_names; |
| 678 |
return 0; |
| 679 |
} |
| 680 |
|
| 681 |
_public_ int sd_bus_creds_get_description(sd_bus_creds *c, const char **ret) { |
| 682 |
assert_return(c, -EINVAL); |
| 683 |
assert_return(ret, -EINVAL); |
| 684 |
|
| 685 |
if (!(c->mask & SD_BUS_CREDS_DESCRIPTION)) |
| 686 |
return -ENODATA; |
| 687 |
|
| 688 |
assert(c->description); |
| 689 |
|
| 690 |
if (!c->unescaped_description) { |
| 691 |
c->unescaped_description = bus_label_unescape(c->description); |
| 692 |
if (!c->unescaped_description) |
| 693 |
return -ENOMEM; |
| 694 |
} |
| 695 |
|
| 696 |
*ret = c->unescaped_description; |
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
static int has_cap(sd_bus_creds *c, size_t offset, int capability) { |
| 701 |
size_t sz; |
| 702 |
|
| 703 |
assert(c); |
| 704 |
assert(capability >= 0); |
| 705 |
assert(c->capability); |
| 706 |
|
| 707 |
unsigned lc = cap_last_cap(); |
| 708 |
|
| 709 |
if ((unsigned) capability > lc) |
| 710 |
return 0; |
| 711 |
|
| 712 |
/* If the last cap is 63, then there are 64 caps defined, and we need 2 entries à 32-bit hence. * |
| 713 |
* If the last cap is 64, then there are 65 caps defined, and we need 3 entries à 32-bit hence. */ |
| 714 |
sz = DIV_ROUND_UP(lc+1, 32LU); |
| 715 |
|
| 716 |
return !!(c->capability[offset * sz + CAP_TO_INDEX((uint32_t) capability)] & CAP_TO_MASK_CORRECTED((uint32_t) capability)); |
| 717 |
} |
| 718 |
|
| 719 |
_public_ int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) { |
| 720 |
assert_return(c, -EINVAL); |
| 721 |
assert_return(capability >= 0, -EINVAL); |
| 722 |
|
| 723 |
if (!(c->mask & SD_BUS_CREDS_EFFECTIVE_CAPS)) |
| 724 |
return -ENODATA; |
| 725 |
|
| 726 |
return has_cap(c, CAP_OFFSET_EFFECTIVE, capability); |
| 727 |
} |
| 728 |
|
| 729 |
_public_ int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) { |
| 730 |
assert_return(c, -EINVAL); |
| 731 |
assert_return(capability >= 0, -EINVAL); |
| 732 |
|
| 733 |
if (!(c->mask & SD_BUS_CREDS_PERMITTED_CAPS)) |
| 734 |
return -ENODATA; |
| 735 |
|
| 736 |
return has_cap(c, CAP_OFFSET_PERMITTED, capability); |
| 737 |
} |
| 738 |
|
| 739 |
_public_ int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability) { |
| 740 |
assert_return(c, -EINVAL); |
| 741 |
assert_return(capability >= 0, -EINVAL); |
| 742 |
|
| 743 |
if (!(c->mask & SD_BUS_CREDS_INHERITABLE_CAPS)) |
| 744 |
return -ENODATA; |
| 745 |
|
| 746 |
return has_cap(c, CAP_OFFSET_INHERITABLE, capability); |
| 747 |
} |
| 748 |
|
| 749 |
_public_ int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability) { |
| 750 |
assert_return(c, -EINVAL); |
| 751 |
assert_return(capability >= 0, -EINVAL); |
| 752 |
|
| 753 |
if (!(c->mask & SD_BUS_CREDS_BOUNDING_CAPS)) |
| 754 |
return -ENODATA; |
| 755 |
|
| 756 |
return has_cap(c, CAP_OFFSET_BOUNDING, capability); |
| 757 |
} |
| 758 |
|
| 759 |
static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) { |
| 760 |
size_t sz, max; |
| 761 |
|
| 762 |
assert(c); |
| 763 |
assert(p); |
| 764 |
|
| 765 |
max = DIV_ROUND_UP(cap_last_cap()+1, 32U); |
| 766 |
|
| 767 |
sz = strlen(p); |
| 768 |
if (sz % 8 != 0) |
| 769 |
return -EINVAL; |
| 770 |
|
| 771 |
sz /= 8; |
| 772 |
if (sz > max) |
| 773 |
return -EINVAL; |
| 774 |
|
| 775 |
if (!c->capability) { |
| 776 |
c->capability = new0(uint32_t, max * 4); |
| 777 |
if (!c->capability) |
| 778 |
return -ENOMEM; |
| 779 |
} |
| 780 |
|
| 781 |
for (unsigned i = 0; i < sz; i++) { |
| 782 |
uint32_t v = 0; |
| 783 |
|
| 784 |
for (unsigned j = 0; j < 8; j++) { |
| 785 |
int t; |
| 786 |
|
| 787 |
t = unhexchar(*p++); |
| 788 |
if (t < 0) |
| 789 |
return -EINVAL; |
| 790 |
|
| 791 |
v = (v << 4) | t; |
| 792 |
} |
| 793 |
|
| 794 |
c->capability[offset * max + (sz - i - 1)] = v; |
| 795 |
} |
| 796 |
|
| 797 |
return 0; |
| 798 |
} |
| 799 |
|
| 800 |
int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, PidRef *pidref, pid_t tid) { |
| 801 |
_cleanup_(pidref_done) PidRef pidref_buf = PIDREF_NULL; |
| 802 |
uint64_t missing; |
| 803 |
int r; |
| 804 |
|
| 805 |
assert(c); |
| 806 |
assert(c->allocated); |
| 807 |
|
| 808 |
if (!(mask & SD_BUS_CREDS_AUGMENT)) |
| 809 |
return 0; |
| 810 |
|
| 811 |
/* Try to retrieve PID from creds if it wasn't passed to us */ |
| 812 |
if (pidref_is_set(pidref)) { |
| 813 |
if ((c->mask & SD_BUS_CREDS_PID) && c->pid != pidref->pid) /* Insist that things match if already set */ |
| 814 |
return -EBUSY; |
| 815 |
|
| 816 |
c->pid = pidref->pid; |
| 817 |
c->mask |= SD_BUS_CREDS_PID; |
| 818 |
} else if (c->mask & SD_BUS_CREDS_PIDFD) { |
| 819 |
r = pidref_set_pidfd(&pidref_buf, c->pidfd); |
| 820 |
if (r < 0) |
| 821 |
return r; |
| 822 |
|
| 823 |
pidref = &pidref_buf; |
| 824 |
|
| 825 |
} else if (c->mask & SD_BUS_CREDS_PID) { |
| 826 |
r = pidref_set_pid(&pidref_buf, c->pid); |
| 827 |
if (r < 0) |
| 828 |
return r; |
| 829 |
|
| 830 |
pidref = &pidref_buf; |
| 831 |
} else |
| 832 |
/* Without pid we cannot do much... */ |
| 833 |
return 0; |
| 834 |
|
| 835 |
/* Try to retrieve TID from creds if it wasn't passed to us */ |
| 836 |
if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID)) |
| 837 |
tid = c->tid; |
| 838 |
|
| 839 |
/* Calculate what we shall and can add */ |
| 840 |
missing = mask & ~(c->mask|SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_DESCRIPTION|SD_BUS_CREDS_AUGMENT); |
| 841 |
if (missing == 0) |
| 842 |
return 0; |
| 843 |
|
| 844 |
if (tid > 0) { |
| 845 |
c->tid = tid; |
| 846 |
c->mask |= SD_BUS_CREDS_TID; |
| 847 |
} |
| 848 |
|
| 849 |
if ((missing & SD_BUS_CREDS_PIDFD) && pidref->fd >= 0) { |
| 850 |
c->pidfd = fcntl(pidref->fd, F_DUPFD_CLOEXEC, 3); |
| 851 |
if (c->pidfd < 0) |
| 852 |
return -errno; |
| 853 |
|
| 854 |
c->mask |= SD_BUS_CREDS_PIDFD; |
| 855 |
} |
| 856 |
|
| 857 |
if (missing & (SD_BUS_CREDS_PPID | |
| 858 |
SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_SUID | SD_BUS_CREDS_FSUID | |
| 859 |
SD_BUS_CREDS_GID | SD_BUS_CREDS_EGID | SD_BUS_CREDS_SGID | SD_BUS_CREDS_FSGID | |
| 860 |
SD_BUS_CREDS_SUPPLEMENTARY_GIDS | |
| 861 |
SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS | |
| 862 |
SD_BUS_CREDS_PERMITTED_CAPS | SD_BUS_CREDS_BOUNDING_CAPS)) { |
| 863 |
|
| 864 |
_cleanup_fclose_ FILE *f = NULL; |
| 865 |
const char *p; |
| 866 |
|
| 867 |
p = procfs_file_alloca(pidref->pid, "status"); |
| 868 |
|
| 869 |
f = fopen(p, "re"); |
| 870 |
if (!f) { |
| 871 |
if (errno == ENOENT) |
| 872 |
return -ESRCH; |
| 873 |
if (!ERRNO_IS_PRIVILEGE(errno)) |
| 874 |
return -errno; |
| 875 |
} else { |
| 876 |
|
| 877 |
for (;;) { |
| 878 |
_cleanup_free_ char *line = NULL; |
| 879 |
|
| 880 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 881 |
if (r < 0) |
| 882 |
return r; |
| 883 |
if (r == 0) |
| 884 |
break; |
| 885 |
|
| 886 |
if (missing & SD_BUS_CREDS_PPID) { |
| 887 |
p = first_word(line, "PPid:"); |
| 888 |
if (p) { |
| 889 |
/* Explicitly check for PPID 0 (which is the case for PID 1) */ |
| 890 |
if (!streq(p, "0")) { |
| 891 |
r = parse_pid(p, &c->ppid); |
| 892 |
if (r < 0) |
| 893 |
return r; |
| 894 |
} else |
| 895 |
c->ppid = 0; |
| 896 |
|
| 897 |
c->mask |= SD_BUS_CREDS_PPID; |
| 898 |
continue; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
if (missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID)) { |
| 903 |
p = first_word(line, "Uid:"); |
| 904 |
if (p) { |
| 905 |
unsigned long uid, euid, suid, fsuid; |
| 906 |
|
| 907 |
if (sscanf(p, "%lu %lu %lu %lu", &uid, &euid, &suid, &fsuid) != 4) |
| 908 |
return -EIO; |
| 909 |
|
| 910 |
if (missing & SD_BUS_CREDS_UID) |
| 911 |
c->uid = (uid_t) uid; |
| 912 |
if (missing & SD_BUS_CREDS_EUID) |
| 913 |
c->euid = (uid_t) euid; |
| 914 |
if (missing & SD_BUS_CREDS_SUID) |
| 915 |
c->suid = (uid_t) suid; |
| 916 |
if (missing & SD_BUS_CREDS_FSUID) |
| 917 |
c->fsuid = (uid_t) fsuid; |
| 918 |
|
| 919 |
c->mask |= missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID); |
| 920 |
continue; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
if (missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID)) { |
| 925 |
p = first_word(line, "Gid:"); |
| 926 |
if (p) { |
| 927 |
unsigned long gid, egid, sgid, fsgid; |
| 928 |
|
| 929 |
if (sscanf(p, "%lu %lu %lu %lu", &gid, &egid, &sgid, &fsgid) != 4) |
| 930 |
return -EIO; |
| 931 |
|
| 932 |
if (missing & SD_BUS_CREDS_GID) |
| 933 |
c->gid = (gid_t) gid; |
| 934 |
if (missing & SD_BUS_CREDS_EGID) |
| 935 |
c->egid = (gid_t) egid; |
| 936 |
if (missing & SD_BUS_CREDS_SGID) |
| 937 |
c->sgid = (gid_t) sgid; |
| 938 |
if (missing & SD_BUS_CREDS_FSGID) |
| 939 |
c->fsgid = (gid_t) fsgid; |
| 940 |
|
| 941 |
c->mask |= missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID); |
| 942 |
continue; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
if (missing & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) { |
| 947 |
p = startswith(line, "Groups:"); |
| 948 |
if (p) { |
| 949 |
for (;;) { |
| 950 |
unsigned long g; |
| 951 |
int n = 0; |
| 952 |
|
| 953 |
p = skip_leading_chars(p, /* bad= */ NULL); |
| 954 |
if (*p == 0) |
| 955 |
break; |
| 956 |
|
| 957 |
if (sscanf(p, "%lu%n", &g, &n) != 1) |
| 958 |
return -EIO; |
| 959 |
|
| 960 |
if (!GREEDY_REALLOC(c->supplementary_gids, c->n_supplementary_gids+1)) |
| 961 |
return -ENOMEM; |
| 962 |
|
| 963 |
c->supplementary_gids[c->n_supplementary_gids++] = (gid_t) g; |
| 964 |
p += n; |
| 965 |
} |
| 966 |
|
| 967 |
c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS; |
| 968 |
continue; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) { |
| 973 |
p = first_word(line, "CapEff:"); |
| 974 |
if (p) { |
| 975 |
r = parse_caps(c, CAP_OFFSET_EFFECTIVE, p); |
| 976 |
if (r < 0) |
| 977 |
return r; |
| 978 |
|
| 979 |
c->mask |= SD_BUS_CREDS_EFFECTIVE_CAPS; |
| 980 |
continue; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
if (missing & SD_BUS_CREDS_PERMITTED_CAPS) { |
| 985 |
p = first_word(line, "CapPrm:"); |
| 986 |
if (p) { |
| 987 |
r = parse_caps(c, CAP_OFFSET_PERMITTED, p); |
| 988 |
if (r < 0) |
| 989 |
return r; |
| 990 |
|
| 991 |
c->mask |= SD_BUS_CREDS_PERMITTED_CAPS; |
| 992 |
continue; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
if (missing & SD_BUS_CREDS_INHERITABLE_CAPS) { |
| 997 |
p = first_word(line, "CapInh:"); |
| 998 |
if (p) { |
| 999 |
r = parse_caps(c, CAP_OFFSET_INHERITABLE, p); |
| 1000 |
if (r < 0) |
| 1001 |
return r; |
| 1002 |
|
| 1003 |
c->mask |= SD_BUS_CREDS_INHERITABLE_CAPS; |
| 1004 |
continue; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (missing & SD_BUS_CREDS_BOUNDING_CAPS) { |
| 1009 |
p = first_word(line, "CapBnd:"); |
| 1010 |
if (p) { |
| 1011 |
r = parse_caps(c, CAP_OFFSET_BOUNDING, p); |
| 1012 |
if (r < 0) |
| 1013 |
return r; |
| 1014 |
|
| 1015 |
c->mask |= SD_BUS_CREDS_BOUNDING_CAPS; |
| 1016 |
continue; |
| 1017 |
} |
| 1018 |
} |
| 1019 |
} |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) { |
| 1024 |
const char *p; |
| 1025 |
|
| 1026 |
p = procfs_file_alloca(pidref->pid, "attr/current"); |
| 1027 |
r = read_one_line_file(p, &c->label); |
| 1028 |
if (r < 0) { |
| 1029 |
if (!IN_SET(r, -ENOENT, -EINVAL, -EPERM, -EACCES)) |
| 1030 |
return r; |
| 1031 |
} else |
| 1032 |
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (missing & SD_BUS_CREDS_COMM) { |
| 1036 |
r = pid_get_comm(pidref->pid, &c->comm); |
| 1037 |
if (r < 0) { |
| 1038 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 1039 |
return r; |
| 1040 |
} else |
| 1041 |
c->mask |= SD_BUS_CREDS_COMM; |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (missing & SD_BUS_CREDS_EXE) { |
| 1045 |
r = get_process_exe(pidref->pid, &c->exe); |
| 1046 |
if (r == -ESRCH) { |
| 1047 |
/* Unfortunately we cannot really distinguish |
| 1048 |
* the case here where the process does not |
| 1049 |
* exist, and /proc/$PID/exe being unreadable |
| 1050 |
* because $PID is a kernel thread. Hence, |
| 1051 |
* assume it is a kernel thread, and rely on |
| 1052 |
* that this case is caught with a later |
| 1053 |
* call. */ |
| 1054 |
c->exe = NULL; |
| 1055 |
c->mask |= SD_BUS_CREDS_EXE; |
| 1056 |
} else if (r < 0) { |
| 1057 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 1058 |
return r; |
| 1059 |
} else |
| 1060 |
c->mask |= SD_BUS_CREDS_EXE; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (missing & SD_BUS_CREDS_CMDLINE) { |
| 1064 |
const char *p; |
| 1065 |
|
| 1066 |
p = procfs_file_alloca(pidref->pid, "cmdline"); |
| 1067 |
r = read_full_file(p, &c->cmdline, &c->cmdline_size); |
| 1068 |
if (r == -ENOENT) |
| 1069 |
return -ESRCH; |
| 1070 |
if (r < 0) { |
| 1071 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 1072 |
return r; |
| 1073 |
} else { |
| 1074 |
if (c->cmdline_size == 0) |
| 1075 |
c->cmdline = mfree(c->cmdline); |
| 1076 |
|
| 1077 |
c->mask |= SD_BUS_CREDS_CMDLINE; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (tid > 0 && (missing & SD_BUS_CREDS_TID_COMM)) { |
| 1082 |
_cleanup_free_ char *p = NULL; |
| 1083 |
|
| 1084 |
if (asprintf(&p, "/proc/"PID_FMT"/task/"PID_FMT"/comm", pidref->pid, tid) < 0) |
| 1085 |
return -ENOMEM; |
| 1086 |
|
| 1087 |
r = read_one_line_file(p, &c->tid_comm); |
| 1088 |
if (r == -ENOENT) |
| 1089 |
return -ESRCH; |
| 1090 |
if (r < 0) { |
| 1091 |
if (!ERRNO_IS_PRIVILEGE(r)) |
| 1092 |
return r; |
| 1093 |
} else |
| 1094 |
c->mask |= SD_BUS_CREDS_TID_COMM; |
| 1095 |
} |
| 1096 |
|
| 1097 |
if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) { |
| 1098 |
|
| 1099 |
if (!c->cgroup) { |
| 1100 |
r = cg_pid_get_path(pidref->pid, &c->cgroup); |
| 1101 |
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r)) |
| 1102 |
return r; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (!c->cgroup_root) { |
| 1106 |
r = cg_get_root_path(&c->cgroup_root); |
| 1107 |
if (r < 0) |
| 1108 |
return r; |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (c->cgroup) |
| 1112 |
c->mask |= missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID); |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) { |
| 1116 |
r = audit_session_from_pid(pidref, &c->audit_session_id); |
| 1117 |
if (r == -ENODATA) { |
| 1118 |
/* ENODATA means: no audit session id assigned */ |
| 1119 |
c->audit_session_id = AUDIT_SESSION_INVALID; |
| 1120 |
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID; |
| 1121 |
} else if (r < 0) { |
| 1122 |
if (!IN_SET(r, -EOPNOTSUPP, -ENOENT, -EPERM, -EACCES)) |
| 1123 |
return r; |
| 1124 |
} else |
| 1125 |
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) { |
| 1129 |
r = audit_loginuid_from_pid(pidref, &c->audit_login_uid); |
| 1130 |
if (r == -ENODATA) { |
| 1131 |
/* ENODATA means: no audit login uid assigned */ |
| 1132 |
c->audit_login_uid = UID_INVALID; |
| 1133 |
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID; |
| 1134 |
} else if (r < 0) { |
| 1135 |
if (!IN_SET(r, -EOPNOTSUPP, -ENOENT, -EPERM, -EACCES)) |
| 1136 |
return r; |
| 1137 |
} else |
| 1138 |
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (missing & SD_BUS_CREDS_TTY) { |
| 1142 |
r = get_ctty(pidref->pid, NULL, &c->tty); |
| 1143 |
if (r == -ENXIO) { |
| 1144 |
/* ENXIO means: process has no controlling TTY */ |
| 1145 |
c->tty = NULL; |
| 1146 |
c->mask |= SD_BUS_CREDS_TTY; |
| 1147 |
} else if (r < 0) { |
| 1148 |
if (!IN_SET(r, -EPERM, -EACCES, -ENOENT)) |
| 1149 |
return r; |
| 1150 |
} else |
| 1151 |
c->mask |= SD_BUS_CREDS_TTY; |
| 1152 |
} |
| 1153 |
|
| 1154 |
r = pidref_verify(pidref); |
| 1155 |
if (r < 0) |
| 1156 |
return r; |
| 1157 |
|
| 1158 |
/* Validate tid is still valid, too */ |
| 1159 |
if (tid > 0 && tid != pidref->pid && pid_is_unwaited(tid) == 0) |
| 1160 |
return -ESRCH; |
| 1161 |
|
| 1162 |
c->augmented = missing & c->mask; |
| 1163 |
|
| 1164 |
return 0; |
| 1165 |
} |