2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/capability.h>
23 #include "alloc-util.h"
24 #include "audit-util.h"
25 #include "bus-creds.h"
26 #include "bus-label.h"
27 #include "bus-message.h"
29 #include "capability-util.h"
30 #include "cgroup-util.h"
33 #include "format-util.h"
34 #include "hexdecoct.h"
35 #include "parse-util.h"
36 #include "process-util.h"
37 #include "string-util.h"
39 #include "terminal-util.h"
40 #include "user-util.h"
44 CAP_OFFSET_INHERITABLE = 0,
45 CAP_OFFSET_PERMITTED = 1,
46 CAP_OFFSET_EFFECTIVE = 2,
47 CAP_OFFSET_BOUNDING = 3
50 void bus_creds_done(sd_bus_creds *c) {
53 /* For internal bus cred structures that are allocated by
61 free(c->unescaped_description);
62 free(c->supplementary_gids);
65 free(c->well_known_names); /* note that this is an strv, but
66 * we only free the array, not the
67 * strings the array points to. The
68 * full strv we only free if
69 * c->allocated is set, see
72 strv_free(c->cmdline_array);
75 _public_ sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
86 /* If this is an embedded creds structure, then
87 * forward ref counting to the message */
88 m = container_of(c, sd_bus_message, creds);
89 sd_bus_message_ref(m);
95 _public_ sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
101 assert(c->n_ref > 0);
112 free(c->unique_name);
113 free(c->cgroup_root);
114 free(c->description);
116 c->supplementary_gids = mfree(c->supplementary_gids);
118 c->well_known_names = strv_free(c->well_known_names);
127 m = container_of(c, sd_bus_message, creds);
128 sd_bus_message_unref(m);
135 _public_ uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c) {
141 _public_ uint64_t sd_bus_creds_get_augmented_mask(const sd_bus_creds *c) {
147 sd_bus_creds* bus_creds_new(void) {
150 c = new0(sd_bus_creds, 1);
159 _public_ int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t mask) {
163 assert_return(pid >= 0, -EINVAL);
164 assert_return(mask <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP);
165 assert_return(ret, -EINVAL);
174 r = bus_creds_add_more(c, mask | SD_BUS_CREDS_AUGMENT, pid, 0);
176 sd_bus_creds_unref(c);
180 /* Check if the process existed at all, in case we haven't
181 * figured that out already */
182 if (!pid_is_alive(pid)) {
183 sd_bus_creds_unref(c);
191 _public_ int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid) {
192 assert_return(c, -EINVAL);
193 assert_return(uid, -EINVAL);
195 if (!(c->mask & SD_BUS_CREDS_UID))
202 _public_ int sd_bus_creds_get_euid(sd_bus_creds *c, uid_t *euid) {
203 assert_return(c, -EINVAL);
204 assert_return(euid, -EINVAL);
206 if (!(c->mask & SD_BUS_CREDS_EUID))
213 _public_ int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *suid) {
214 assert_return(c, -EINVAL);
215 assert_return(suid, -EINVAL);
217 if (!(c->mask & SD_BUS_CREDS_SUID))
225 _public_ int sd_bus_creds_get_fsuid(sd_bus_creds *c, uid_t *fsuid) {
226 assert_return(c, -EINVAL);
227 assert_return(fsuid, -EINVAL);
229 if (!(c->mask & SD_BUS_CREDS_FSUID))
236 _public_ int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid) {
237 assert_return(c, -EINVAL);
238 assert_return(gid, -EINVAL);
240 if (!(c->mask & SD_BUS_CREDS_GID))
247 _public_ int sd_bus_creds_get_egid(sd_bus_creds *c, gid_t *egid) {
248 assert_return(c, -EINVAL);
249 assert_return(egid, -EINVAL);
251 if (!(c->mask & SD_BUS_CREDS_EGID))
258 _public_ int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *sgid) {
259 assert_return(c, -EINVAL);
260 assert_return(sgid, -EINVAL);
262 if (!(c->mask & SD_BUS_CREDS_SGID))
269 _public_ int sd_bus_creds_get_fsgid(sd_bus_creds *c, gid_t *fsgid) {
270 assert_return(c, -EINVAL);
271 assert_return(fsgid, -EINVAL);
273 if (!(c->mask & SD_BUS_CREDS_FSGID))
280 _public_ int sd_bus_creds_get_supplementary_gids(sd_bus_creds *c, const gid_t **gids) {
281 assert_return(c, -EINVAL);
282 assert_return(gids, -EINVAL);
284 if (!(c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS))
287 *gids = c->supplementary_gids;
288 return (int) c->n_supplementary_gids;
291 _public_ int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid) {
292 assert_return(c, -EINVAL);
293 assert_return(pid, -EINVAL);
295 if (!(c->mask & SD_BUS_CREDS_PID))
303 _public_ int sd_bus_creds_get_ppid(sd_bus_creds *c, pid_t *ppid) {
304 assert_return(c, -EINVAL);
305 assert_return(ppid, -EINVAL);
307 if (!(c->mask & SD_BUS_CREDS_PPID))
310 /* PID 1 has no parent process. Let's distinguish the case of
311 * not knowing and not having a parent process by the returned
320 _public_ int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid) {
321 assert_return(c, -EINVAL);
322 assert_return(tid, -EINVAL);
324 if (!(c->mask & SD_BUS_CREDS_TID))
332 _public_ int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **ret) {
333 assert_return(c, -EINVAL);
335 if (!(c->mask & SD_BUS_CREDS_SELINUX_CONTEXT))
343 _public_ int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) {
344 assert_return(c, -EINVAL);
345 assert_return(ret, -EINVAL);
347 if (!(c->mask & SD_BUS_CREDS_COMM))
355 _public_ int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **ret) {
356 assert_return(c, -EINVAL);
357 assert_return(ret, -EINVAL);
359 if (!(c->mask & SD_BUS_CREDS_TID_COMM))
367 _public_ int sd_bus_creds_get_exe(sd_bus_creds *c, const char **ret) {
368 assert_return(c, -EINVAL);
369 assert_return(ret, -EINVAL);
371 if (!(c->mask & SD_BUS_CREDS_EXE))
381 _public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) {
382 assert_return(c, -EINVAL);
383 assert_return(ret, -EINVAL);
385 if (!(c->mask & SD_BUS_CREDS_CGROUP))
393 #if 0 /// UNNEEDED by elogind
394 _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
397 assert_return(c, -EINVAL);
398 assert_return(ret, -EINVAL);
400 if (!(c->mask & SD_BUS_CREDS_UNIT))
408 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
412 r = cg_path_get_unit(shifted, (char**) &c->unit);
421 _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
424 assert_return(c, -EINVAL);
425 assert_return(ret, -EINVAL);
427 if (!(c->mask & SD_BUS_CREDS_USER_UNIT))
435 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
439 r = cg_path_get_user_unit(shifted, (char**) &c->user_unit);
448 _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
451 assert_return(c, -EINVAL);
452 assert_return(ret, -EINVAL);
454 if (!(c->mask & SD_BUS_CREDS_SLICE))
462 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
466 r = cg_path_get_slice(shifted, (char**) &c->slice);
475 _public_ int sd_bus_creds_get_user_slice(sd_bus_creds *c, const char **ret) {
478 assert_return(c, -EINVAL);
479 assert_return(ret, -EINVAL);
481 if (!(c->mask & SD_BUS_CREDS_USER_SLICE))
486 if (!c->user_slice) {
489 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
493 r = cg_path_get_user_slice(shifted, (char**) &c->user_slice);
498 *ret = c->user_slice;
503 _public_ int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) {
506 assert_return(c, -EINVAL);
507 assert_return(ret, -EINVAL);
509 if (!(c->mask & SD_BUS_CREDS_SESSION))
517 log_debug_elogind("Shifting cgroup \"%s\", root \"%s\"",
518 c->cgroup, c->cgroup_root ? c->cgroup_root : "NULL");
519 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
523 log_debug_elogind("Shifted: \"%s\"", shifted);
524 r = cg_path_get_session(shifted, (char**) &c->session);
533 _public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) {
537 assert_return(c, -EINVAL);
538 assert_return(uid, -EINVAL);
540 if (!(c->mask & SD_BUS_CREDS_OWNER_UID))
545 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
549 #if 0 /// elogind does not support systemd slices
550 return cg_path_get_owner_uid(shifted, uid);
557 _public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
558 assert_return(c, -EINVAL);
560 if (!(c->mask & SD_BUS_CREDS_CMDLINE))
566 if (!c->cmdline_array) {
567 c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size);
568 if (!c->cmdline_array)
572 *cmdline = c->cmdline_array;
576 _public_ int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessionid) {
577 assert_return(c, -EINVAL);
578 assert_return(sessionid, -EINVAL);
580 if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID))
583 if (c->audit_session_id == AUDIT_SESSION_INVALID)
586 *sessionid = c->audit_session_id;
590 _public_ int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *uid) {
591 assert_return(c, -EINVAL);
592 assert_return(uid, -EINVAL);
594 if (!(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID))
597 if (c->audit_login_uid == UID_INVALID)
600 *uid = c->audit_login_uid;
604 _public_ int sd_bus_creds_get_tty(sd_bus_creds *c, const char **ret) {
605 assert_return(c, -EINVAL);
606 assert_return(ret, -EINVAL);
608 if (!(c->mask & SD_BUS_CREDS_TTY))
618 _public_ int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **unique_name) {
619 assert_return(c, -EINVAL);
620 assert_return(unique_name, -EINVAL);
622 if (!(c->mask & SD_BUS_CREDS_UNIQUE_NAME))
625 *unique_name = c->unique_name;
629 _public_ int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***well_known_names) {
630 assert_return(c, -EINVAL);
631 assert_return(well_known_names, -EINVAL);
633 if (!(c->mask & SD_BUS_CREDS_WELL_KNOWN_NAMES))
636 /* As a special hack we return the bus driver as well-known
637 * names list when this is requested. */
638 if (c->well_known_names_driver) {
639 static const char* const wkn[] = {
640 "org.freedesktop.DBus",
644 *well_known_names = (char**) wkn;
648 if (c->well_known_names_local) {
649 static const char* const wkn[] = {
650 "org.freedesktop.DBus.Local",
654 *well_known_names = (char**) wkn;
658 *well_known_names = c->well_known_names;
662 _public_ int sd_bus_creds_get_description(sd_bus_creds *c, const char **ret) {
663 assert_return(c, -EINVAL);
664 assert_return(ret, -EINVAL);
666 if (!(c->mask & SD_BUS_CREDS_DESCRIPTION))
669 assert(c->description);
671 if (!c->unescaped_description) {
672 c->unescaped_description = bus_label_unescape(c->description);
673 if (!c->unescaped_description)
677 *ret = c->unescaped_description;
681 static int has_cap(sd_bus_creds *c, unsigned offset, int capability) {
685 assert(capability >= 0);
686 assert(c->capability);
688 if ((unsigned) capability > cap_last_cap())
691 sz = DIV_ROUND_UP(cap_last_cap(), 32U);
693 return !!(c->capability[offset * sz + CAP_TO_INDEX(capability)] & CAP_TO_MASK(capability));
696 _public_ int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) {
697 assert_return(c, -EINVAL);
698 assert_return(capability >= 0, -EINVAL);
700 if (!(c->mask & SD_BUS_CREDS_EFFECTIVE_CAPS))
703 return has_cap(c, CAP_OFFSET_EFFECTIVE, capability);
706 _public_ int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) {
707 assert_return(c, -EINVAL);
708 assert_return(capability >= 0, -EINVAL);
710 if (!(c->mask & SD_BUS_CREDS_PERMITTED_CAPS))
713 return has_cap(c, CAP_OFFSET_PERMITTED, capability);
716 _public_ int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability) {
717 assert_return(c, -EINVAL);
718 assert_return(capability >= 0, -EINVAL);
720 if (!(c->mask & SD_BUS_CREDS_INHERITABLE_CAPS))
723 return has_cap(c, CAP_OFFSET_INHERITABLE, capability);
726 _public_ int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability) {
727 assert_return(c, -EINVAL);
728 assert_return(capability >= 0, -EINVAL);
730 if (!(c->mask & SD_BUS_CREDS_BOUNDING_CAPS))
733 return has_cap(c, CAP_OFFSET_BOUNDING, capability);
736 static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) {
743 max = DIV_ROUND_UP(cap_last_cap(), 32U);
744 p += strspn(p, WHITESPACE);
754 if (!c->capability) {
755 c->capability = new0(uint32_t, max * 4);
760 for (i = 0; i < sz; i ++) {
763 for (j = 0; j < 8; ++j) {
773 c->capability[offset * max + (sz - i - 1)] = v;
779 int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
784 assert(c->allocated);
786 if (!(mask & SD_BUS_CREDS_AUGMENT))
789 /* Try to retrieve PID from creds if it wasn't passed to us */
792 c->mask |= SD_BUS_CREDS_PID;
793 } else if (c->mask & SD_BUS_CREDS_PID)
796 /* Without pid we cannot do much... */
799 /* Try to retrieve TID from creds if it wasn't passed to us */
800 if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID))
803 /* Calculate what we shall and can add */
804 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);
810 c->mask |= SD_BUS_CREDS_TID;
813 if (missing & (SD_BUS_CREDS_PPID |
814 SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_SUID | SD_BUS_CREDS_FSUID |
815 SD_BUS_CREDS_GID | SD_BUS_CREDS_EGID | SD_BUS_CREDS_SGID | SD_BUS_CREDS_FSGID |
816 SD_BUS_CREDS_SUPPLEMENTARY_GIDS |
817 SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS |
818 SD_BUS_CREDS_PERMITTED_CAPS | SD_BUS_CREDS_BOUNDING_CAPS)) {
820 _cleanup_fclose_ FILE *f = NULL;
823 p = procfs_file_alloca(pid, "status");
829 else if (errno != EPERM && errno != EACCES)
834 FOREACH_LINE(line, f, return -errno) {
837 if (missing & SD_BUS_CREDS_PPID) {
838 p = startswith(line, "PPid:");
840 p += strspn(p, WHITESPACE);
842 /* Explicitly check for PPID 0 (which is the case for PID 1) */
843 if (!streq(p, "0")) {
844 r = parse_pid(p, &c->ppid);
851 c->mask |= SD_BUS_CREDS_PPID;
856 if (missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID)) {
857 p = startswith(line, "Uid:");
859 unsigned long uid, euid, suid, fsuid;
861 p += strspn(p, WHITESPACE);
862 if (sscanf(p, "%lu %lu %lu %lu", &uid, &euid, &suid, &fsuid) != 4)
865 if (missing & SD_BUS_CREDS_UID)
866 c->uid = (uid_t) uid;
867 if (missing & SD_BUS_CREDS_EUID)
868 c->euid = (uid_t) euid;
869 if (missing & SD_BUS_CREDS_SUID)
870 c->suid = (uid_t) suid;
871 if (missing & SD_BUS_CREDS_FSUID)
872 c->fsuid = (uid_t) fsuid;
874 c->mask |= missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID);
879 if (missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID)) {
880 p = startswith(line, "Gid:");
882 unsigned long gid, egid, sgid, fsgid;
884 p += strspn(p, WHITESPACE);
885 if (sscanf(p, "%lu %lu %lu %lu", &gid, &egid, &sgid, &fsgid) != 4)
888 if (missing & SD_BUS_CREDS_GID)
889 c->gid = (gid_t) gid;
890 if (missing & SD_BUS_CREDS_EGID)
891 c->egid = (gid_t) egid;
892 if (missing & SD_BUS_CREDS_SGID)
893 c->sgid = (gid_t) sgid;
894 if (missing & SD_BUS_CREDS_FSGID)
895 c->fsgid = (gid_t) fsgid;
897 c->mask |= missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID);
902 if (missing & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
903 p = startswith(line, "Groups:");
905 size_t allocated = 0;
911 p += strspn(p, WHITESPACE);
915 if (sscanf(p, "%lu%n", &g, &n) != 1)
918 if (!GREEDY_REALLOC(c->supplementary_gids, allocated, c->n_supplementary_gids+1))
921 c->supplementary_gids[c->n_supplementary_gids++] = (gid_t) g;
925 c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
930 if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) {
931 p = startswith(line, "CapEff:");
933 r = parse_caps(c, CAP_OFFSET_EFFECTIVE, p);
937 c->mask |= SD_BUS_CREDS_EFFECTIVE_CAPS;
942 if (missing & SD_BUS_CREDS_PERMITTED_CAPS) {
943 p = startswith(line, "CapPrm:");
945 r = parse_caps(c, CAP_OFFSET_PERMITTED, p);
949 c->mask |= SD_BUS_CREDS_PERMITTED_CAPS;
954 if (missing & SD_BUS_CREDS_INHERITABLE_CAPS) {
955 p = startswith(line, "CapInh:");
957 r = parse_caps(c, CAP_OFFSET_INHERITABLE, p);
961 c->mask |= SD_BUS_CREDS_INHERITABLE_CAPS;
966 if (missing & SD_BUS_CREDS_BOUNDING_CAPS) {
967 p = startswith(line, "CapBnd:");
969 r = parse_caps(c, CAP_OFFSET_BOUNDING, p);
973 c->mask |= SD_BUS_CREDS_BOUNDING_CAPS;
981 if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) {
984 p = procfs_file_alloca(pid, "attr/current");
985 r = read_one_line_file(p, &c->label);
987 if (r != -ENOENT && r != -EINVAL && r != -EPERM && r != -EACCES)
990 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
993 if (missing & SD_BUS_CREDS_COMM) {
994 r = get_process_comm(pid, &c->comm);
996 if (r != -EPERM && r != -EACCES)
999 c->mask |= SD_BUS_CREDS_COMM;
1002 if (missing & SD_BUS_CREDS_EXE) {
1003 r = get_process_exe(pid, &c->exe);
1005 /* Unfortunately we cannot really distinguish
1006 * the case here where the process does not
1007 * exist, and /proc/$PID/exe being unreadable
1008 * because $PID is a kernel thread. Hence,
1009 * assume it is a kernel thread, and rely on
1010 * that this case is caught with a later
1013 c->mask |= SD_BUS_CREDS_EXE;
1015 if (r != -EPERM && r != -EACCES)
1018 c->mask |= SD_BUS_CREDS_EXE;
1021 if (missing & SD_BUS_CREDS_CMDLINE) {
1024 p = procfs_file_alloca(pid, "cmdline");
1025 r = read_full_file(p, &c->cmdline, &c->cmdline_size);
1029 if (r != -EPERM && r != -EACCES)
1032 if (c->cmdline_size == 0)
1033 c->cmdline = mfree(c->cmdline);
1035 c->mask |= SD_BUS_CREDS_CMDLINE;
1039 if (tid > 0 && (missing & SD_BUS_CREDS_TID_COMM)) {
1040 _cleanup_free_ char *p = NULL;
1042 if (asprintf(&p, "/proc/"PID_FMT"/task/"PID_FMT"/comm", pid, tid) < 0)
1045 r = read_one_line_file(p, &c->tid_comm);
1049 if (r != -EPERM && r != -EACCES)
1052 c->mask |= SD_BUS_CREDS_TID_COMM;
1055 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)) {
1058 r = cg_pid_get_path(NULL, pid, &c->cgroup);
1060 if (r != -EPERM && r != -EACCES)
1065 if (!c->cgroup_root) {
1066 r = cg_get_root_path(&c->cgroup_root);
1072 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);
1075 if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) {
1076 r = audit_session_from_pid(pid, &c->audit_session_id);
1077 if (r == -ENODATA) {
1078 /* ENODATA means: no audit session id assigned */
1079 c->audit_session_id = AUDIT_SESSION_INVALID;
1080 c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1082 if (r != -EOPNOTSUPP && r != -ENOENT && r != -EPERM && r != -EACCES)
1085 c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1088 if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
1089 r = audit_loginuid_from_pid(pid, &c->audit_login_uid);
1090 if (r == -ENODATA) {
1091 /* ENODATA means: no audit login uid assigned */
1092 c->audit_login_uid = UID_INVALID;
1093 c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1095 if (r != -EOPNOTSUPP && r != -ENOENT && r != -EPERM && r != -EACCES)
1098 c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1101 if (missing & SD_BUS_CREDS_TTY) {
1102 r = get_ctty(pid, NULL, &c->tty);
1104 /* ENXIO means: process has no controlling TTY */
1106 c->mask |= SD_BUS_CREDS_TTY;
1108 if (r != -EPERM && r != -EACCES && r != -ENOENT)
1111 c->mask |= SD_BUS_CREDS_TTY;
1114 /* In case only the exe path was to be read we cannot
1115 * distinguish the case where the exe path was unreadable
1116 * because the process was a kernel thread, or when the
1117 * process didn't exist at all. Hence, let's do a final check,
1119 if (!pid_is_alive(pid))
1122 if (tid > 0 && tid != pid && !pid_is_unwaited(tid))
1125 c->augmented = missing & c->mask;
1130 int bus_creds_extend_by_pid(sd_bus_creds *c, uint64_t mask, sd_bus_creds **ret) {
1131 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *n = NULL;
1137 if ((mask & ~c->mask) == 0 || (!(mask & SD_BUS_CREDS_AUGMENT))) {
1138 /* There's already all data we need, or augmentation
1139 * wasn't turned on. */
1141 *ret = sd_bus_creds_ref(c);
1145 n = bus_creds_new();
1149 /* Copy the original data over */
1151 if (c->mask & mask & SD_BUS_CREDS_PID) {
1153 n->mask |= SD_BUS_CREDS_PID;
1156 if (c->mask & mask & SD_BUS_CREDS_TID) {
1158 n->mask |= SD_BUS_CREDS_TID;
1161 if (c->mask & mask & SD_BUS_CREDS_PPID) {
1163 n->mask |= SD_BUS_CREDS_PPID;
1166 if (c->mask & mask & SD_BUS_CREDS_UID) {
1168 n->mask |= SD_BUS_CREDS_UID;
1171 if (c->mask & mask & SD_BUS_CREDS_EUID) {
1173 n->mask |= SD_BUS_CREDS_EUID;
1176 if (c->mask & mask & SD_BUS_CREDS_SUID) {
1178 n->mask |= SD_BUS_CREDS_SUID;
1181 if (c->mask & mask & SD_BUS_CREDS_FSUID) {
1182 n->fsuid = c->fsuid;
1183 n->mask |= SD_BUS_CREDS_FSUID;
1186 if (c->mask & mask & SD_BUS_CREDS_GID) {
1188 n->mask |= SD_BUS_CREDS_GID;
1191 if (c->mask & mask & SD_BUS_CREDS_EGID) {
1193 n->mask |= SD_BUS_CREDS_EGID;
1196 if (c->mask & mask & SD_BUS_CREDS_SGID) {
1198 n->mask |= SD_BUS_CREDS_SGID;
1201 if (c->mask & mask & SD_BUS_CREDS_FSGID) {
1202 n->fsgid = c->fsgid;
1203 n->mask |= SD_BUS_CREDS_FSGID;
1206 if (c->mask & mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
1207 if (c->supplementary_gids) {
1208 n->supplementary_gids = newdup(gid_t, c->supplementary_gids, c->n_supplementary_gids);
1209 if (!n->supplementary_gids)
1211 n->n_supplementary_gids = c->n_supplementary_gids;
1213 n->supplementary_gids = NULL;
1214 n->n_supplementary_gids = 0;
1217 n->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
1220 if (c->mask & mask & SD_BUS_CREDS_COMM) {
1223 n->comm = strdup(c->comm);
1227 n->mask |= SD_BUS_CREDS_COMM;
1230 if (c->mask & mask & SD_BUS_CREDS_TID_COMM) {
1231 assert(c->tid_comm);
1233 n->tid_comm = strdup(c->tid_comm);
1237 n->mask |= SD_BUS_CREDS_TID_COMM;
1240 if (c->mask & mask & SD_BUS_CREDS_EXE) {
1242 n->exe = strdup(c->exe);
1248 n->mask |= SD_BUS_CREDS_EXE;
1251 if (c->mask & mask & SD_BUS_CREDS_CMDLINE) {
1253 n->cmdline = memdup(c->cmdline, c->cmdline_size);
1257 n->cmdline_size = c->cmdline_size;
1260 n->cmdline_size = 0;
1263 n->mask |= SD_BUS_CREDS_CMDLINE;
1266 if (c->mask & mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_OWNER_UID)) {
1269 n->cgroup = strdup(c->cgroup);
1273 n->cgroup_root = strdup(c->cgroup_root);
1274 if (!n->cgroup_root)
1277 n->mask |= mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_USER_SLICE|SD_BUS_CREDS_OWNER_UID);
1280 if (c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)) {
1281 assert(c->capability);
1283 n->capability = memdup(c->capability, DIV_ROUND_UP(cap_last_cap(), 32U) * 4 * 4);
1287 n->mask |= c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS);
1290 if (c->mask & mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
1293 n->label = strdup(c->label);
1296 n->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
1299 if (c->mask & mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
1300 n->audit_session_id = c->audit_session_id;
1301 n->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1303 if (c->mask & mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
1304 n->audit_login_uid = c->audit_login_uid;
1305 n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1308 if (c->mask & mask & SD_BUS_CREDS_TTY) {
1310 n->tty = strdup(c->tty);
1315 n->mask |= SD_BUS_CREDS_TTY;
1318 if (c->mask & mask & SD_BUS_CREDS_UNIQUE_NAME) {
1319 assert(c->unique_name);
1321 n->unique_name = strdup(c->unique_name);
1322 if (!n->unique_name)
1324 n->mask |= SD_BUS_CREDS_UNIQUE_NAME;
1327 if (c->mask & mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
1328 if (strv_isempty(c->well_known_names))
1329 n->well_known_names = NULL;
1331 n->well_known_names = strv_copy(c->well_known_names);
1332 if (!n->well_known_names)
1335 n->well_known_names_driver = c->well_known_names_driver;
1336 n->well_known_names_local = c->well_known_names_local;
1337 n->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
1340 if (c->mask & mask & SD_BUS_CREDS_DESCRIPTION) {
1341 assert(c->description);
1342 n->description = strdup(c->description);
1343 if (!n->description)
1345 n->mask |= SD_BUS_CREDS_DESCRIPTION;
1348 n->augmented = c->augmented & n->mask;
1352 r = bus_creds_add_more(n, mask, 0, 0);