Bug Summary

File:build-scan/../src/libsystemd/sd-bus/bus-creds.c
Warning:line 1201, column 33
Potential leak of memory pointed to by 'n'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bus-creds.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I src/libsystemd/libsystemd_static.a.p -I src/libsystemd -I ../src/libsystemd -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -I . -I .. -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility default -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/libsystemd/sd-bus/bus-creds.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3***/
4
5#include <linux1/capability.h>
6#include <stdlib.h>
7
8#include "alloc-util.h"
9#include "audit-util.h"
10#include "bus-creds.h"
11#include "bus-label.h"
12#include "bus-message.h"
13#include "bus-util.h"
14#include "capability-util.h"
15#include "cgroup-util.h"
16#include "fd-util.h"
17#include "fileio.h"
18#include "format-util.h"
19#include "hexdecoct.h"
20#include "parse-util.h"
21#include "process-util.h"
22#include "string-util.h"
23#include "strv.h"
24#include "terminal-util.h"
25#include "user-util.h"
26#include "util.h"
27
28enum {
29 CAP_OFFSET_INHERITABLE = 0,
30 CAP_OFFSET_PERMITTED = 1,
31 CAP_OFFSET_EFFECTIVE = 2,
32 CAP_OFFSET_BOUNDING = 3
33};
34
35void bus_creds_done(sd_bus_creds *c) {
36 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 36, __PRETTY_FUNCTION__); } while (0)
;
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
60_public___attribute__ ((visibility("default"))) sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
61
62 if (!c)
63 return NULL((void*)0);
64
65 if (c->allocated) {
66 assert(c->n_ref > 0)do { if ((__builtin_expect(!!(!(c->n_ref > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->n_ref > 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 66, __PRETTY_FUNCTION__); } while (0)
;
67 c->n_ref++;
68 } else {
69 sd_bus_message *m;
70
71 /* If this is an embedded creds structure, then
72 * forward ref counting to the message */
73 m = container_of(c, sd_bus_message, creds)__extension__ ({ const typeof( ((sd_bus_message*)0)->creds
) *__unique_prefix_A23 = ((c)); (sd_bus_message*)( (char *)__unique_prefix_A23
- __builtin_offsetof(sd_bus_message, creds) ); })
;
74 sd_bus_message_ref(m);
75 }
76
77 return c;
78}
79
80_public___attribute__ ((visibility("default"))) sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
81
82 if (!c)
83 return NULL((void*)0);
84
85 if (c->allocated) {
86 assert(c->n_ref > 0)do { if ((__builtin_expect(!!(!(c->n_ref > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->n_ref > 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 86, __PRETTY_FUNCTION__); } while (0)
;
87 c->n_ref--;
88
89 if (c->n_ref == 0) {
90 free(c->comm);
91 free(c->tid_comm);
92 free(c->exe);
93 free(c->cmdline);
94 free(c->cgroup);
95 free(c->capability);
96 free(c->label);
97 free(c->unique_name);
98 free(c->cgroup_root);
99 free(c->description);
100
101 c->supplementary_gids = mfree(c->supplementary_gids);
102
103 c->well_known_names = strv_free(c->well_known_names);
104
105 bus_creds_done(c);
106
107 free(c);
108 }
109 } else {
110 sd_bus_message *m;
111
112 m = container_of(c, sd_bus_message, creds)__extension__ ({ const typeof( ((sd_bus_message*)0)->creds
) *__unique_prefix_A24 = ((c)); (sd_bus_message*)( (char *)__unique_prefix_A24
- __builtin_offsetof(sd_bus_message, creds) ); })
;
113 sd_bus_message_unref(m);
114 }
115
116 return NULL((void*)0);
117}
118
119_public___attribute__ ((visibility("default"))) uint64_t sd_bus_creds_get_mask(const sd_bus_creds *c) {
120 assert_return(c, 0)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 120, __PRETTY_FUNCTION__), 0))) return (0); } while (0)
;
121
122 return c->mask;
123}
124
125_public___attribute__ ((visibility("default"))) uint64_t sd_bus_creds_get_augmented_mask(const sd_bus_creds *c) {
126 assert_return(c, 0)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 126, __PRETTY_FUNCTION__), 0))) return (0); } while (0)
;
127
128 return c->augmented;
129}
130
131sd_bus_creds* bus_creds_new(void) {
132 sd_bus_creds *c;
133
134 c = new0(sd_bus_creds, 1)((sd_bus_creds*) calloc((1), sizeof(sd_bus_creds)));
11
Memory is allocated
135 if (!c)
12
Assuming 'c' is non-null
13
Taking false branch
136 return NULL((void*)0);
137
138 c->allocated = true1;
139 c->n_ref = 1;
140 return c;
141}
142
143_public___attribute__ ((visibility("default"))) int sd_bus_creds_new_from_pid(sd_bus_creds **ret, pid_t pid, uint64_t mask) {
144 sd_bus_creds *c;
145 int r;
146
147 assert_return(pid >= 0, -EINVAL)do { if (!(((__builtin_expect(!!(pid >= 0),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("pid >= 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 147, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
148 assert_return(mask <= _SD_BUS_CREDS_ALL, -EOPNOTSUPP)do { if (!(((__builtin_expect(!!(mask <= _SD_BUS_CREDS_ALL
),1))) ? (1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD
, ("mask <= _SD_BUS_CREDS_ALL"), "../src/libsystemd/sd-bus/bus-creds.c"
, 148, __PRETTY_FUNCTION__), 0))) return (-95); } while (0)
;
149 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 149, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
150
151 if (pid == 0)
152 pid = getpid_cached();
153
154 c = bus_creds_new();
155 if (!c)
156 return -ENOMEM12;
157
158 r = bus_creds_add_more(c, mask | SD_BUS_CREDS_AUGMENT, pid, 0);
159 if (r < 0) {
160 sd_bus_creds_unref(c);
161 return r;
162 }
163
164 /* Check if the process existed at all, in case we haven't
165 * figured that out already */
166 if (!pid_is_alive(pid)) {
167 sd_bus_creds_unref(c);
168 return -ESRCH3;
169 }
170
171 *ret = c;
172 return 0;
173}
174
175_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid) {
176 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 176, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
177 assert_return(uid, -EINVAL)do { if (!(((__builtin_expect(!!(uid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("uid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 177, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
178
179 if (!(c->mask & SD_BUS_CREDS_UID))
180 return -ENODATA61;
181
182 *uid = c->uid;
183 return 0;
184}
185
186_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_euid(sd_bus_creds *c, uid_t *euid) {
187 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 187, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
188 assert_return(euid, -EINVAL)do { if (!(((__builtin_expect(!!(euid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("euid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 188, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
189
190 if (!(c->mask & SD_BUS_CREDS_EUID))
191 return -ENODATA61;
192
193 *euid = c->euid;
194 return 0;
195}
196
197_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_suid(sd_bus_creds *c, uid_t *suid) {
198 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 198, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
199 assert_return(suid, -EINVAL)do { if (!(((__builtin_expect(!!(suid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("suid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 199, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
200
201 if (!(c->mask & SD_BUS_CREDS_SUID))
202 return -ENODATA61;
203
204 *suid = c->suid;
205 return 0;
206}
207
208_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_fsuid(sd_bus_creds *c, uid_t *fsuid) {
209 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 209, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
210 assert_return(fsuid, -EINVAL)do { if (!(((__builtin_expect(!!(fsuid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("fsuid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 210, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
211
212 if (!(c->mask & SD_BUS_CREDS_FSUID))
213 return -ENODATA61;
214
215 *fsuid = c->fsuid;
216 return 0;
217}
218
219_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid) {
220 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 220, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
221 assert_return(gid, -EINVAL)do { if (!(((__builtin_expect(!!(gid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("gid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 221, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
222
223 if (!(c->mask & SD_BUS_CREDS_GID))
224 return -ENODATA61;
225
226 *gid = c->gid;
227 return 0;
228}
229
230_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_egid(sd_bus_creds *c, gid_t *egid) {
231 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 231, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
232 assert_return(egid, -EINVAL)do { if (!(((__builtin_expect(!!(egid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("egid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 232, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
233
234 if (!(c->mask & SD_BUS_CREDS_EGID))
235 return -ENODATA61;
236
237 *egid = c->egid;
238 return 0;
239}
240
241_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_sgid(sd_bus_creds *c, gid_t *sgid) {
242 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 242, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
243 assert_return(sgid, -EINVAL)do { if (!(((__builtin_expect(!!(sgid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("sgid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 243, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
244
245 if (!(c->mask & SD_BUS_CREDS_SGID))
246 return -ENODATA61;
247
248 *sgid = c->sgid;
249 return 0;
250}
251
252_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_fsgid(sd_bus_creds *c, gid_t *fsgid) {
253 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 253, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
254 assert_return(fsgid, -EINVAL)do { if (!(((__builtin_expect(!!(fsgid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("fsgid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 254, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
255
256 if (!(c->mask & SD_BUS_CREDS_FSGID))
257 return -ENODATA61;
258
259 *fsgid = c->fsgid;
260 return 0;
261}
262
263_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_supplementary_gids(sd_bus_creds *c, const gid_t **gids) {
264 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 264, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
265 assert_return(gids, -EINVAL)do { if (!(((__builtin_expect(!!(gids),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("gids"), "../src/libsystemd/sd-bus/bus-creds.c"
, 265, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
266
267 if (!(c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS))
268 return -ENODATA61;
269
270 *gids = c->supplementary_gids;
271 return (int) c->n_supplementary_gids;
272}
273
274_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid) {
275 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 275, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
276 assert_return(pid, -EINVAL)do { if (!(((__builtin_expect(!!(pid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("pid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 276, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
277
278 if (!(c->mask & SD_BUS_CREDS_PID))
279 return -ENODATA61;
280
281 assert(c->pid > 0)do { if ((__builtin_expect(!!(!(c->pid > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->pid > 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 281, __PRETTY_FUNCTION__); } while (0)
;
282 *pid = c->pid;
283 return 0;
284}
285
286_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_ppid(sd_bus_creds *c, pid_t *ppid) {
287 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 287, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
288 assert_return(ppid, -EINVAL)do { if (!(((__builtin_expect(!!(ppid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ppid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 288, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
289
290 if (!(c->mask & SD_BUS_CREDS_PPID))
291 return -ENODATA61;
292
293 /* PID 1 has no parent process. Let's distinguish the case of
294 * not knowing and not having a parent process by the returned
295 * error code. */
296 if (c->ppid == 0)
297 return -ENXIO6;
298
299 *ppid = c->ppid;
300 return 0;
301}
302
303_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid) {
304 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 304, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
305 assert_return(tid, -EINVAL)do { if (!(((__builtin_expect(!!(tid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("tid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 305, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
306
307 if (!(c->mask & SD_BUS_CREDS_TID))
308 return -ENODATA61;
309
310 assert(c->tid > 0)do { if ((__builtin_expect(!!(!(c->tid > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->tid > 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 310, __PRETTY_FUNCTION__); } while (0)
;
311 *tid = c->tid;
312 return 0;
313}
314
315_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **ret) {
316 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 316, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
317
318 if (!(c->mask & SD_BUS_CREDS_SELINUX_CONTEXT))
319 return -ENODATA61;
320
321 assert(c->label)do { if ((__builtin_expect(!!(!(c->label)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->label"), "../src/libsystemd/sd-bus/bus-creds.c"
, 321, __PRETTY_FUNCTION__); } while (0)
;
322 *ret = c->label;
323 return 0;
324}
325
326_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) {
327 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 327, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
328 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 328, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
329
330 if (!(c->mask & SD_BUS_CREDS_COMM))
331 return -ENODATA61;
332
333 assert(c->comm)do { if ((__builtin_expect(!!(!(c->comm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->comm"), "../src/libsystemd/sd-bus/bus-creds.c"
, 333, __PRETTY_FUNCTION__); } while (0)
;
334 *ret = c->comm;
335 return 0;
336}
337
338_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **ret) {
339 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 339, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
340 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 340, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
341
342 if (!(c->mask & SD_BUS_CREDS_TID_COMM))
343 return -ENODATA61;
344
345 assert(c->tid_comm)do { if ((__builtin_expect(!!(!(c->tid_comm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->tid_comm"), "../src/libsystemd/sd-bus/bus-creds.c"
, 345, __PRETTY_FUNCTION__); } while (0)
;
346 *ret = c->tid_comm;
347 return 0;
348}
349
350_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_exe(sd_bus_creds *c, const char **ret) {
351 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 351, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
352 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 352, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
353
354 if (!(c->mask & SD_BUS_CREDS_EXE))
355 return -ENODATA61;
356
357 if (!c->exe)
358 return -ENXIO6;
359
360 *ret = c->exe;
361 return 0;
362}
363
364_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) {
365 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 365, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
366 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 366, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
367
368 if (!(c->mask & SD_BUS_CREDS_CGROUP))
369 return -ENODATA61;
370
371 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 371, __PRETTY_FUNCTION__); } while (0)
;
372 *ret = c->cgroup;
373 return 0;
374}
375
376_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
377 int r;
378
379 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 379, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
380 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 380, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
381
382 if (!(c->mask & SD_BUS_CREDS_UNIT))
383 return -ENODATA61;
384
385 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 385, __PRETTY_FUNCTION__); } while (0)
;
386
387 if (!c->unit) {
388 const char *shifted;
389
390 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
391 if (r < 0)
392 return r;
393
394 r = cg_path_get_unit(shifted, (char**) &c->unit);
395 if (r < 0)
396 return r;
397 }
398
399 *ret = c->unit;
400 return 0;
401}
402
403_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
404 int r;
405
406 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 406, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
407 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 407, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
408
409 if (!(c->mask & SD_BUS_CREDS_USER_UNIT))
410 return -ENODATA61;
411
412 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 412, __PRETTY_FUNCTION__); } while (0)
;
413
414 if (!c->user_unit) {
415 const char *shifted;
416
417 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
418 if (r < 0)
419 return r;
420
421 r = cg_path_get_user_unit(shifted, (char**) &c->user_unit);
422 if (r < 0)
423 return r;
424 }
425
426 *ret = c->user_unit;
427 return 0;
428}
429
430_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
431 int r;
432
433 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 433, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
434 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 434, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
435
436 if (!(c->mask & SD_BUS_CREDS_SLICE))
437 return -ENODATA61;
438
439 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 439, __PRETTY_FUNCTION__); } while (0)
;
440
441 if (!c->slice) {
442 const char *shifted;
443
444 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
445 if (r < 0)
446 return r;
447
448 r = cg_path_get_slice(shifted, (char**) &c->slice);
449 if (r < 0)
450 return r;
451 }
452
453 *ret = c->slice;
454 return 0;
455}
456
457_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_user_slice(sd_bus_creds *c, const char **ret) {
458 int r;
459
460 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 460, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
461 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 461, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
462
463 if (!(c->mask & SD_BUS_CREDS_USER_SLICE))
464 return -ENODATA61;
465
466 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 466, __PRETTY_FUNCTION__); } while (0)
;
467
468 if (!c->user_slice) {
469 const char *shifted;
470
471 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
472 if (r < 0)
473 return r;
474
475 r = cg_path_get_user_slice(shifted, (char**) &c->user_slice);
476 if (r < 0)
477 return r;
478 }
479
480 *ret = c->user_slice;
481 return 0;
482}
483
484_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) {
485 int r;
486
487 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 487, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
488 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 488, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
489
490 if (!(c->mask & SD_BUS_CREDS_SESSION))
491 return -ENODATA61;
492
493 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 493, __PRETTY_FUNCTION__); } while (0)
;
494
495 if (!c->session) {
496 const char *shifted;
497
498 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
499 if (r < 0)
500 return r;
501
502 r = cg_path_get_session(shifted, (char**) &c->session);
503 if (r < 0)
504 return r;
505 }
506
507 *ret = c->session;
508 return 0;
509}
510
511_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) {
512 const char *shifted;
513 int r;
514
515 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 515, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
516 assert_return(uid, -EINVAL)do { if (!(((__builtin_expect(!!(uid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("uid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 516, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
517
518 if (!(c->mask & SD_BUS_CREDS_OWNER_UID))
519 return -ENODATA61;
520
521 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 521, __PRETTY_FUNCTION__); } while (0)
;
522
523 r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
524 if (r < 0)
525 return r;
526
527 return cg_path_get_owner_uid(shifted, uid);
528}
529
530_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
531 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 531, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
532
533 if (!(c->mask & SD_BUS_CREDS_CMDLINE))
534 return -ENODATA61;
535
536 if (!c->cmdline)
537 return -ENXIO6;
538
539 if (!c->cmdline_array) {
540 c->cmdline_array = strv_parse_nulstr(c->cmdline, c->cmdline_size);
541 if (!c->cmdline_array)
542 return -ENOMEM12;
543 }
544
545 *cmdline = c->cmdline_array;
546 return 0;
547}
548
549_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessionid) {
550 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 550, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
551 assert_return(sessionid, -EINVAL)do { if (!(((__builtin_expect(!!(sessionid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("sessionid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 551, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
552
553 if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID))
554 return -ENODATA61;
555
556 if (!audit_session_is_valid(c->audit_session_id))
557 return -ENXIO6;
558
559 *sessionid = c->audit_session_id;
560 return 0;
561}
562
563_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *uid) {
564 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 564, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
565 assert_return(uid, -EINVAL)do { if (!(((__builtin_expect(!!(uid),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("uid"), "../src/libsystemd/sd-bus/bus-creds.c"
, 565, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
566
567 if (!(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID))
568 return -ENODATA61;
569
570 if (!uid_is_valid(c->audit_login_uid))
571 return -ENXIO6;
572
573 *uid = c->audit_login_uid;
574 return 0;
575}
576
577_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_tty(sd_bus_creds *c, const char **ret) {
578 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 578, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
579 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 579, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
580
581 if (!(c->mask & SD_BUS_CREDS_TTY))
582 return -ENODATA61;
583
584 if (!c->tty)
585 return -ENXIO6;
586
587 *ret = c->tty;
588 return 0;
589}
590
591_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_unique_name(sd_bus_creds *c, const char **unique_name) {
592 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 592, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
593 assert_return(unique_name, -EINVAL)do { if (!(((__builtin_expect(!!(unique_name),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("unique_name"), "../src/libsystemd/sd-bus/bus-creds.c"
, 593, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
594
595 if (!(c->mask & SD_BUS_CREDS_UNIQUE_NAME))
596 return -ENODATA61;
597
598 *unique_name = c->unique_name;
599 return 0;
600}
601
602_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_well_known_names(sd_bus_creds *c, char ***well_known_names) {
603 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 603, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
604 assert_return(well_known_names, -EINVAL)do { if (!(((__builtin_expect(!!(well_known_names),1))) ? (1)
: (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("well_known_names"
), "../src/libsystemd/sd-bus/bus-creds.c", 604, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
605
606 if (!(c->mask & SD_BUS_CREDS_WELL_KNOWN_NAMES))
607 return -ENODATA61;
608
609 /* As a special hack we return the bus driver as well-known
610 * names list when this is requested. */
611 if (c->well_known_names_driver) {
612 static const char* const wkn[] = {
613 "org.freedesktop.DBus",
614 NULL((void*)0)
615 };
616
617 *well_known_names = (char**) wkn;
618 return 0;
619 }
620
621 if (c->well_known_names_local) {
622 static const char* const wkn[] = {
623 "org.freedesktop.DBus.Local",
624 NULL((void*)0)
625 };
626
627 *well_known_names = (char**) wkn;
628 return 0;
629 }
630
631 *well_known_names = c->well_known_names;
632 return 0;
633}
634
635_public___attribute__ ((visibility("default"))) int sd_bus_creds_get_description(sd_bus_creds *c, const char **ret) {
636 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 636, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
637 assert_return(ret, -EINVAL)do { if (!(((__builtin_expect(!!(ret),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 637, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
638
639 if (!(c->mask & SD_BUS_CREDS_DESCRIPTION))
640 return -ENODATA61;
641
642 assert(c->description)do { if ((__builtin_expect(!!(!(c->description)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->description"), "../src/libsystemd/sd-bus/bus-creds.c"
, 642, __PRETTY_FUNCTION__); } while (0)
;
643
644 if (!c->unescaped_description) {
645 c->unescaped_description = bus_label_unescape(c->description);
646 if (!c->unescaped_description)
647 return -ENOMEM12;
648 }
649
650 *ret = c->unescaped_description;
651 return 0;
652}
653
654static int has_cap(sd_bus_creds *c, size_t offset, int capability) {
655 unsigned long lc;
656 size_t sz;
657
658 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 658, __PRETTY_FUNCTION__); } while (0)
;
659 assert(capability >= 0)do { if ((__builtin_expect(!!(!(capability >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("capability >= 0"), "../src/libsystemd/sd-bus/bus-creds.c"
, 659, __PRETTY_FUNCTION__); } while (0)
;
660 assert(c->capability)do { if ((__builtin_expect(!!(!(c->capability)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->capability"), "../src/libsystemd/sd-bus/bus-creds.c"
, 660, __PRETTY_FUNCTION__); } while (0)
;
661
662 lc = cap_last_cap();
663
664 if ((unsigned long) capability > lc)
665 return 0;
666
667 sz = DIV_ROUND_UP(lc, 32LU)({ const typeof((lc)) __unique_prefix_X25 = ((lc)); const typeof
((32LU)) __unique_prefix_Y26 = ((32LU)); (__unique_prefix_X25
/ __unique_prefix_Y26 + !!(__unique_prefix_X25 % __unique_prefix_Y26
)); })
;
668
669 return !!(c->capability[offset * sz + CAP_TO_INDEX((uint32_t) capability)(((uint32_t) capability) >> 5)] & CAP_TO_MASK_CORRECTED((uint32_t) capability)(1U << (((uint32_t) capability) & 31U)));
670}
671
672_public___attribute__ ((visibility("default"))) int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) {
673 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 673, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
674 assert_return(capability >= 0, -EINVAL)do { if (!(((__builtin_expect(!!(capability >= 0),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("capability >= 0"
), "../src/libsystemd/sd-bus/bus-creds.c", 674, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
675
676 if (!(c->mask & SD_BUS_CREDS_EFFECTIVE_CAPS))
677 return -ENODATA61;
678
679 return has_cap(c, CAP_OFFSET_EFFECTIVE, capability);
680}
681
682_public___attribute__ ((visibility("default"))) int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) {
683 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 683, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
684 assert_return(capability >= 0, -EINVAL)do { if (!(((__builtin_expect(!!(capability >= 0),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("capability >= 0"
), "../src/libsystemd/sd-bus/bus-creds.c", 684, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
685
686 if (!(c->mask & SD_BUS_CREDS_PERMITTED_CAPS))
687 return -ENODATA61;
688
689 return has_cap(c, CAP_OFFSET_PERMITTED, capability);
690}
691
692_public___attribute__ ((visibility("default"))) int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability) {
693 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 693, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
694 assert_return(capability >= 0, -EINVAL)do { if (!(((__builtin_expect(!!(capability >= 0),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("capability >= 0"
), "../src/libsystemd/sd-bus/bus-creds.c", 694, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
695
696 if (!(c->mask & SD_BUS_CREDS_INHERITABLE_CAPS))
697 return -ENODATA61;
698
699 return has_cap(c, CAP_OFFSET_INHERITABLE, capability);
700}
701
702_public___attribute__ ((visibility("default"))) int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability) {
703 assert_return(c, -EINVAL)do { if (!(((__builtin_expect(!!(c),1))) ? (1) : (log_assert_failed_return_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 703, __PRETTY_FUNCTION__), 0))) return (-22); } while (0)
;
704 assert_return(capability >= 0, -EINVAL)do { if (!(((__builtin_expect(!!(capability >= 0),1))) ? (
1) : (log_assert_failed_return_realm(LOG_REALM_SYSTEMD, ("capability >= 0"
), "../src/libsystemd/sd-bus/bus-creds.c", 704, __PRETTY_FUNCTION__
), 0))) return (-22); } while (0)
;
705
706 if (!(c->mask & SD_BUS_CREDS_BOUNDING_CAPS))
707 return -ENODATA61;
708
709 return has_cap(c, CAP_OFFSET_BOUNDING, capability);
710}
711
712static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) {
713 size_t sz, max;
714 unsigned i, j;
715
716 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 716, __PRETTY_FUNCTION__); } while (0)
;
717 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/libsystemd/sd-bus/bus-creds.c"
, 717, __PRETTY_FUNCTION__); } while (0)
;
718
719 max = DIV_ROUND_UP(cap_last_cap(), 32U)({ const typeof((cap_last_cap())) __unique_prefix_X27 = ((cap_last_cap
())); const typeof((32U)) __unique_prefix_Y28 = ((32U)); (__unique_prefix_X27
/ __unique_prefix_Y28 + !!(__unique_prefix_X27 % __unique_prefix_Y28
)); })
;
720 p += strspn(p, WHITESPACE" \t\n\r");
721
722 sz = strlen(p);
723 if (sz % 8 != 0)
724 return -EINVAL22;
725
726 sz /= 8;
727 if (sz > max)
728 return -EINVAL22;
729
730 if (!c->capability) {
731 c->capability = new0(uint32_t, max * 4)((uint32_t*) calloc((max * 4), sizeof(uint32_t)));
732 if (!c->capability)
733 return -ENOMEM12;
734 }
735
736 for (i = 0; i < sz; i ++) {
737 uint32_t v = 0;
738
739 for (j = 0; j < 8; ++j) {
740 int t;
741
742 t = unhexchar(*p++);
743 if (t < 0)
744 return -EINVAL22;
745
746 v = (v << 4) | t;
747 }
748
749 c->capability[offset * max + (sz - i - 1)] = v;
750 }
751
752 return 0;
753}
754
755int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
756 uint64_t missing;
757 int r;
758
759 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 759, __PRETTY_FUNCTION__); } while (0)
;
760 assert(c->allocated)do { if ((__builtin_expect(!!(!(c->allocated)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->allocated"), "../src/libsystemd/sd-bus/bus-creds.c"
, 760, __PRETTY_FUNCTION__); } while (0)
;
761
762 if (!(mask & SD_BUS_CREDS_AUGMENT))
763 return 0;
764
765 /* Try to retrieve PID from creds if it wasn't passed to us */
766 if (pid > 0) {
767 c->pid = pid;
768 c->mask |= SD_BUS_CREDS_PID;
769 } else if (c->mask & SD_BUS_CREDS_PID)
770 pid = c->pid;
771 else
772 /* Without pid we cannot do much... */
773 return 0;
774
775 /* Try to retrieve TID from creds if it wasn't passed to us */
776 if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID))
777 tid = c->tid;
778
779 /* Calculate what we shall and can add */
780 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);
781 if (missing == 0)
782 return 0;
783
784 if (tid > 0) {
785 c->tid = tid;
786 c->mask |= SD_BUS_CREDS_TID;
787 }
788
789 if (missing & (SD_BUS_CREDS_PPID |
790 SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_SUID | SD_BUS_CREDS_FSUID |
791 SD_BUS_CREDS_GID | SD_BUS_CREDS_EGID | SD_BUS_CREDS_SGID | SD_BUS_CREDS_FSGID |
792 SD_BUS_CREDS_SUPPLEMENTARY_GIDS |
793 SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS |
794 SD_BUS_CREDS_PERMITTED_CAPS | SD_BUS_CREDS_BOUNDING_CAPS)) {
795
796 _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0);
797 const char *p;
798
799 p = procfs_file_alloca(pid, "status")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_
= ("/proc/self/" "status"); } else { _r_ = __builtin_alloca (
(sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof
(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof(pid_t
) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]))) + 1
+ sizeof("status")); sprintf((char*) _r_, "/proc/""%" "i""/"
"status", _pid_); } _r_; })
;
800
801 f = fopen(p, "re");
802 if (!f) {
803 if (errno(*__errno_location ()) == ENOENT2)
804 return -ESRCH3;
805 else if (!IN_SET(errno, EPERM, EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){1, 13})/sizeof(int)]; switch((*__errno_location
())) { case 1: case 13: _found = 1; break; default: break; }
_found; })
)
806 return -errno(*__errno_location ());
807 } else {
808 char line[LINE_MAX2048];
809
810 FOREACH_LINE(line, f, return -errno)for (;;) if (!fgets(line, sizeof(line), f)) { if (ferror(f)) {
return -(*__errno_location ()); } break; } else
{
811 truncate_nl(line);
812
813 if (missing & SD_BUS_CREDS_PPID) {
814 p = startswith(line, "PPid:");
815 if (p) {
816 p += strspn(p, WHITESPACE" \t\n\r");
817
818 /* Explicitly check for PPID 0 (which is the case for PID 1) */
819 if (!streq(p, "0")(strcmp((p),("0")) == 0)) {
820 r = parse_pid(p, &c->ppid);
821 if (r < 0)
822 return r;
823
824 } else
825 c->ppid = 0;
826
827 c->mask |= SD_BUS_CREDS_PPID;
828 continue;
829 }
830 }
831
832 if (missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID)) {
833 p = startswith(line, "Uid:");
834 if (p) {
835 unsigned long uid, euid, suid, fsuid;
836
837 p += strspn(p, WHITESPACE" \t\n\r");
838 if (sscanf(p, "%lu %lu %lu %lu", &uid, &euid, &suid, &fsuid) != 4)
839 return -EIO5;
840
841 if (missing & SD_BUS_CREDS_UID)
842 c->uid = (uid_t) uid;
843 if (missing & SD_BUS_CREDS_EUID)
844 c->euid = (uid_t) euid;
845 if (missing & SD_BUS_CREDS_SUID)
846 c->suid = (uid_t) suid;
847 if (missing & SD_BUS_CREDS_FSUID)
848 c->fsuid = (uid_t) fsuid;
849
850 c->mask |= missing & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID);
851 continue;
852 }
853 }
854
855 if (missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID)) {
856 p = startswith(line, "Gid:");
857 if (p) {
858 unsigned long gid, egid, sgid, fsgid;
859
860 p += strspn(p, WHITESPACE" \t\n\r");
861 if (sscanf(p, "%lu %lu %lu %lu", &gid, &egid, &sgid, &fsgid) != 4)
862 return -EIO5;
863
864 if (missing & SD_BUS_CREDS_GID)
865 c->gid = (gid_t) gid;
866 if (missing & SD_BUS_CREDS_EGID)
867 c->egid = (gid_t) egid;
868 if (missing & SD_BUS_CREDS_SGID)
869 c->sgid = (gid_t) sgid;
870 if (missing & SD_BUS_CREDS_FSGID)
871 c->fsgid = (gid_t) fsgid;
872
873 c->mask |= missing & (SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID);
874 continue;
875 }
876 }
877
878 if (missing & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
879 p = startswith(line, "Groups:");
880 if (p) {
881 size_t allocated = 0;
882
883 for (;;) {
884 unsigned long g;
885 int n = 0;
886
887 p += strspn(p, WHITESPACE" \t\n\r");
888 if (*p == 0)
889 break;
890
891 if (sscanf(p, "%lu%n", &g, &n) != 1)
892 return -EIO5;
893
894 if (!GREEDY_REALLOC(c->supplementary_gids, allocated, c->n_supplementary_gids+1)greedy_realloc((void**) &(c->supplementary_gids), &
(allocated), (c->n_supplementary_gids+1), sizeof((c->supplementary_gids
)[0]))
)
895 return -ENOMEM12;
896
897 c->supplementary_gids[c->n_supplementary_gids++] = (gid_t) g;
898 p += n;
899 }
900
901 c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
902 continue;
903 }
904 }
905
906 if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) {
907 p = startswith(line, "CapEff:");
908 if (p) {
909 r = parse_caps(c, CAP_OFFSET_EFFECTIVE, p);
910 if (r < 0)
911 return r;
912
913 c->mask |= SD_BUS_CREDS_EFFECTIVE_CAPS;
914 continue;
915 }
916 }
917
918 if (missing & SD_BUS_CREDS_PERMITTED_CAPS) {
919 p = startswith(line, "CapPrm:");
920 if (p) {
921 r = parse_caps(c, CAP_OFFSET_PERMITTED, p);
922 if (r < 0)
923 return r;
924
925 c->mask |= SD_BUS_CREDS_PERMITTED_CAPS;
926 continue;
927 }
928 }
929
930 if (missing & SD_BUS_CREDS_INHERITABLE_CAPS) {
931 p = startswith(line, "CapInh:");
932 if (p) {
933 r = parse_caps(c, CAP_OFFSET_INHERITABLE, p);
934 if (r < 0)
935 return r;
936
937 c->mask |= SD_BUS_CREDS_INHERITABLE_CAPS;
938 continue;
939 }
940 }
941
942 if (missing & SD_BUS_CREDS_BOUNDING_CAPS) {
943 p = startswith(line, "CapBnd:");
944 if (p) {
945 r = parse_caps(c, CAP_OFFSET_BOUNDING, p);
946 if (r < 0)
947 return r;
948
949 c->mask |= SD_BUS_CREDS_BOUNDING_CAPS;
950 continue;
951 }
952 }
953 }
954 }
955 }
956
957 if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) {
958 const char *p;
959
960 p = procfs_file_alloca(pid, "attr/current")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_
= ("/proc/self/" "attr/current"); } else { _r_ = __builtin_alloca
((sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 :
sizeof(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof
(pid_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)])
)) + 1 + sizeof("attr/current")); sprintf((char*) _r_, "/proc/"
"%" "i""/" "attr/current", _pid_); } _r_; })
;
961 r = read_one_line_file(p, &c->label);
962 if (r < 0) {
963 if (!IN_SET(r, -ENOENT, -EINVAL, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-2, -22, -1, -13})/sizeof(int)]; switch(
r) { case -2: case -22: case -1: case -13: _found = 1; break;
default: break; } _found; })
)
964 return r;
965 } else
966 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
967 }
968
969 if (missing & SD_BUS_CREDS_COMM) {
970 r = get_process_comm(pid, &c->comm);
971 if (r < 0) {
972 if (!IN_SET(r, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13})/sizeof(int)]; switch(r) { case
-1: case -13: _found = 1; break; default: break; } _found; }
)
)
973 return r;
974 } else
975 c->mask |= SD_BUS_CREDS_COMM;
976 }
977
978 if (missing & SD_BUS_CREDS_EXE) {
979 r = get_process_exe(pid, &c->exe);
980 if (r == -ESRCH3) {
981 /* Unfortunately we cannot really distinguish
982 * the case here where the process does not
983 * exist, and /proc/$PID/exe being unreadable
984 * because $PID is a kernel thread. Hence,
985 * assume it is a kernel thread, and rely on
986 * that this case is caught with a later
987 * call. */
988 c->exe = NULL((void*)0);
989 c->mask |= SD_BUS_CREDS_EXE;
990 } else if (r < 0) {
991 if (!IN_SET(r, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13})/sizeof(int)]; switch(r) { case
-1: case -13: _found = 1; break; default: break; } _found; }
)
)
992 return r;
993 } else
994 c->mask |= SD_BUS_CREDS_EXE;
995 }
996
997 if (missing & SD_BUS_CREDS_CMDLINE) {
998 const char *p;
999
1000 p = procfs_file_alloca(pid, "cmdline")({ pid_t _pid_ = (pid); const char *_r_; if (_pid_ == 0) { _r_
= ("/proc/self/" "cmdline"); } else { _r_ = __builtin_alloca
((sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 :
sizeof(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof
(pid_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)])
)) + 1 + sizeof("cmdline")); sprintf((char*) _r_, "/proc/""%"
"i""/" "cmdline", _pid_); } _r_; })
;
1001 r = read_full_file(p, &c->cmdline, &c->cmdline_size);
1002 if (r == -ENOENT2)
1003 return -ESRCH3;
1004 if (r < 0) {
1005 if (!IN_SET(r, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13})/sizeof(int)]; switch(r) { case
-1: case -13: _found = 1; break; default: break; } _found; }
)
)
1006 return r;
1007 } else {
1008 if (c->cmdline_size == 0)
1009 c->cmdline = mfree(c->cmdline);
1010
1011 c->mask |= SD_BUS_CREDS_CMDLINE;
1012 }
1013 }
1014
1015 if (tid > 0 && (missing & SD_BUS_CREDS_TID_COMM)) {
1016 _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0);
1017
1018 if (asprintf(&p, "/proc/"PID_FMT"%" "i""/task/"PID_FMT"%" "i""/comm", pid, tid) < 0)
1019 return -ENOMEM12;
1020
1021 r = read_one_line_file(p, &c->tid_comm);
1022 if (r == -ENOENT2)
1023 return -ESRCH3;
1024 if (r < 0) {
1025 if (!IN_SET(r, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13})/sizeof(int)]; switch(r) { case
-1: case -13: _found = 1; break; default: break; } _found; }
)
)
1026 return r;
1027 } else
1028 c->mask |= SD_BUS_CREDS_TID_COMM;
1029 }
1030
1031 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)) {
1032
1033 if (!c->cgroup) {
1034 r = cg_pid_get_path(NULL((void*)0), pid, &c->cgroup);
1035 if (r < 0) {
1036 if (!IN_SET(r, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13})/sizeof(int)]; switch(r) { case
-1: case -13: _found = 1; break; default: break; } _found; }
)
)
1037 return r;
1038 }
1039 }
1040
1041 if (!c->cgroup_root) {
1042 r = cg_get_root_path(&c->cgroup_root);
1043 if (r < 0)
1044 return r;
1045 }
1046
1047 if (c->cgroup)
1048 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);
1049 }
1050
1051 if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) {
1052 r = audit_session_from_pid(pid, &c->audit_session_id);
1053 if (r == -ENODATA61) {
1054 /* ENODATA means: no audit session id assigned */
1055 c->audit_session_id = AUDIT_SESSION_INVALID((uint32_t) -1);
1056 c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1057 } else if (r < 0) {
1058 if (!IN_SET(r, -EOPNOTSUPP, -ENOENT, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-95, -2, -1, -13})/sizeof(int)]; switch(
r) { case -95: case -2: case -1: case -13: _found = 1; break;
default: break; } _found; })
)
1059 return r;
1060 } else
1061 c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1062 }
1063
1064 if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
1065 r = audit_loginuid_from_pid(pid, &c->audit_login_uid);
1066 if (r == -ENODATA61) {
1067 /* ENODATA means: no audit login uid assigned */
1068 c->audit_login_uid = UID_INVALID((uid_t) -1);
1069 c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1070 } else if (r < 0) {
1071 if (!IN_SET(r, -EOPNOTSUPP, -ENOENT, -EPERM, -EACCES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-95, -2, -1, -13})/sizeof(int)]; switch(
r) { case -95: case -2: case -1: case -13: _found = 1; break;
default: break; } _found; })
)
1072 return r;
1073 } else
1074 c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1075 }
1076
1077 if (missing & SD_BUS_CREDS_TTY) {
1078 r = get_ctty(pid, NULL((void*)0), &c->tty);
1079 if (r == -ENXIO6) {
1080 /* ENXIO means: process has no controlling TTY */
1081 c->tty = NULL((void*)0);
1082 c->mask |= SD_BUS_CREDS_TTY;
1083 } else if (r < 0) {
1084 if (!IN_SET(r, -EPERM, -EACCES, -ENOENT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){-1, -13, -2})/sizeof(int)]; switch(r) { case
-1: case -13: case -2: _found = 1; break; default: break; } _found
; })
)
1085 return r;
1086 } else
1087 c->mask |= SD_BUS_CREDS_TTY;
1088 }
1089
1090 /* In case only the exe path was to be read we cannot
1091 * distinguish the case where the exe path was unreadable
1092 * because the process was a kernel thread, or when the
1093 * process didn't exist at all. Hence, let's do a final check,
1094 * to be sure. */
1095 if (!pid_is_alive(pid))
1096 return -ESRCH3;
1097
1098 if (tid > 0 && tid != pid && !pid_is_unwaited(tid))
1099 return -ESRCH3;
1100
1101 c->augmented = missing & c->mask;
1102
1103 return 0;
1104}
1105
1106int bus_creds_extend_by_pid(sd_bus_creds *c, uint64_t mask, sd_bus_creds **ret) {
1107 _cleanup_(sd_bus_creds_unrefp)__attribute__((cleanup(sd_bus_creds_unrefp))) sd_bus_creds *n = NULL((void*)0);
1108 int r;
1109
1110 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1110, __PRETTY_FUNCTION__); } while (0)
;
1
Assuming 'c' is non-null
2
Taking false branch
3
Loop condition is false. Exiting loop
1111 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1111, __PRETTY_FUNCTION__); } while (0)
;
4
Assuming 'ret' is non-null
5
Taking false branch
6
Loop condition is false. Exiting loop
1112
1113 if ((mask & ~c->mask) == 0 || (!(mask & SD_BUS_CREDS_AUGMENT))) {
7
Assuming the condition is false
8
Assuming the condition is false
9
Taking false branch
1114 /* There's already all data we need, or augmentation
1115 * wasn't turned on. */
1116
1117 *ret = sd_bus_creds_ref(c);
1118 return 0;
1119 }
1120
1121 n = bus_creds_new();
10
Calling 'bus_creds_new'
14
Returned allocated memory
1122 if (!n
14.1
'n' is non-null
)
15
Taking false branch
1123 return -ENOMEM12;
1124
1125 /* Copy the original data over */
1126
1127 if (c->mask & mask & SD_BUS_CREDS_PID) {
16
Assuming the condition is false
17
Taking false branch
1128 n->pid = c->pid;
1129 n->mask |= SD_BUS_CREDS_PID;
1130 }
1131
1132 if (c->mask & mask & SD_BUS_CREDS_TID) {
18
Assuming the condition is false
19
Taking false branch
1133 n->tid = c->tid;
1134 n->mask |= SD_BUS_CREDS_TID;
1135 }
1136
1137 if (c->mask & mask & SD_BUS_CREDS_PPID) {
20
Assuming the condition is false
21
Taking false branch
1138 n->ppid = c->ppid;
1139 n->mask |= SD_BUS_CREDS_PPID;
1140 }
1141
1142 if (c->mask & mask & SD_BUS_CREDS_UID) {
22
Assuming the condition is false
23
Taking false branch
1143 n->uid = c->uid;
1144 n->mask |= SD_BUS_CREDS_UID;
1145 }
1146
1147 if (c->mask & mask & SD_BUS_CREDS_EUID) {
24
Assuming the condition is false
25
Taking false branch
1148 n->euid = c->euid;
1149 n->mask |= SD_BUS_CREDS_EUID;
1150 }
1151
1152 if (c->mask & mask & SD_BUS_CREDS_SUID) {
26
Assuming the condition is false
27
Taking false branch
1153 n->suid = c->suid;
1154 n->mask |= SD_BUS_CREDS_SUID;
1155 }
1156
1157 if (c->mask & mask & SD_BUS_CREDS_FSUID) {
28
Assuming the condition is false
29
Taking false branch
1158 n->fsuid = c->fsuid;
1159 n->mask |= SD_BUS_CREDS_FSUID;
1160 }
1161
1162 if (c->mask & mask & SD_BUS_CREDS_GID) {
30
Assuming the condition is false
31
Taking false branch
1163 n->gid = c->gid;
1164 n->mask |= SD_BUS_CREDS_GID;
1165 }
1166
1167 if (c->mask & mask & SD_BUS_CREDS_EGID) {
32
Assuming the condition is false
33
Taking false branch
1168 n->egid = c->egid;
1169 n->mask |= SD_BUS_CREDS_EGID;
1170 }
1171
1172 if (c->mask & mask & SD_BUS_CREDS_SGID) {
34
Assuming the condition is false
35
Taking false branch
1173 n->sgid = c->sgid;
1174 n->mask |= SD_BUS_CREDS_SGID;
1175 }
1176
1177 if (c->mask & mask & SD_BUS_CREDS_FSGID) {
36
Assuming the condition is false
37
Taking false branch
1178 n->fsgid = c->fsgid;
1179 n->mask |= SD_BUS_CREDS_FSGID;
1180 }
1181
1182 if (c->mask & mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
38
Assuming the condition is false
39
Taking false branch
1183 if (c->supplementary_gids) {
1184 n->supplementary_gids = newdup(gid_t, c->supplementary_gids, c->n_supplementary_gids)((gid_t*) memdup_multiply(c->supplementary_gids, sizeof(gid_t
), (c->n_supplementary_gids)))
;
1185 if (!n->supplementary_gids)
1186 return -ENOMEM12;
1187 n->n_supplementary_gids = c->n_supplementary_gids;
1188 } else {
1189 n->supplementary_gids = NULL((void*)0);
1190 n->n_supplementary_gids = 0;
1191 }
1192
1193 n->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
1194 }
1195
1196 if (c->mask & mask & SD_BUS_CREDS_COMM) {
40
Assuming the condition is true
41
Taking true branch
1197 assert(c->comm)do { if ((__builtin_expect(!!(!(c->comm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->comm"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1197, __PRETTY_FUNCTION__); } while (0)
;
42
Assuming field 'comm' is non-null
43
Taking false branch
44
Loop condition is false. Exiting loop
1198
1199 n->comm = strdup(c->comm);
1200 if (!n->comm)
45
Assuming field 'comm' is null
46
Taking true branch
1201 return -ENOMEM12;
47
Potential leak of memory pointed to by 'n'
1202
1203 n->mask |= SD_BUS_CREDS_COMM;
1204 }
1205
1206 if (c->mask & mask & SD_BUS_CREDS_TID_COMM) {
1207 assert(c->tid_comm)do { if ((__builtin_expect(!!(!(c->tid_comm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->tid_comm"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1207, __PRETTY_FUNCTION__); } while (0)
;
1208
1209 n->tid_comm = strdup(c->tid_comm);
1210 if (!n->tid_comm)
1211 return -ENOMEM12;
1212
1213 n->mask |= SD_BUS_CREDS_TID_COMM;
1214 }
1215
1216 if (c->mask & mask & SD_BUS_CREDS_EXE) {
1217 if (c->exe) {
1218 n->exe = strdup(c->exe);
1219 if (!n->exe)
1220 return -ENOMEM12;
1221 } else
1222 n->exe = NULL((void*)0);
1223
1224 n->mask |= SD_BUS_CREDS_EXE;
1225 }
1226
1227 if (c->mask & mask & SD_BUS_CREDS_CMDLINE) {
1228 if (c->cmdline) {
1229 n->cmdline = memdup(c->cmdline, c->cmdline_size);
1230 if (!n->cmdline)
1231 return -ENOMEM12;
1232
1233 n->cmdline_size = c->cmdline_size;
1234 } else {
1235 n->cmdline = NULL((void*)0);
1236 n->cmdline_size = 0;
1237 }
1238
1239 n->mask |= SD_BUS_CREDS_CMDLINE;
1240 }
1241
1242 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)) {
1243 assert(c->cgroup)do { if ((__builtin_expect(!!(!(c->cgroup)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->cgroup"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1243, __PRETTY_FUNCTION__); } while (0)
;
1244
1245 n->cgroup = strdup(c->cgroup);
1246 if (!n->cgroup)
1247 return -ENOMEM12;
1248
1249 n->cgroup_root = strdup(c->cgroup_root);
1250 if (!n->cgroup_root)
1251 return -ENOMEM12;
1252
1253 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);
1254 }
1255
1256 if (c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)) {
1257 assert(c->capability)do { if ((__builtin_expect(!!(!(c->capability)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->capability"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1257, __PRETTY_FUNCTION__); } while (0)
;
1258
1259 n->capability = memdup(c->capability, DIV_ROUND_UP(cap_last_cap(), 32U)({ const typeof((cap_last_cap())) __unique_prefix_X29 = ((cap_last_cap
())); const typeof((32U)) __unique_prefix_Y30 = ((32U)); (__unique_prefix_X29
/ __unique_prefix_Y30 + !!(__unique_prefix_X29 % __unique_prefix_Y30
)); })
* 4 * 4);
1260 if (!n->capability)
1261 return -ENOMEM12;
1262
1263 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);
1264 }
1265
1266 if (c->mask & mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
1267 assert(c->label)do { if ((__builtin_expect(!!(!(c->label)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->label"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1267, __PRETTY_FUNCTION__); } while (0)
;
1268
1269 n->label = strdup(c->label);
1270 if (!n->label)
1271 return -ENOMEM12;
1272 n->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
1273 }
1274
1275 if (c->mask & mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
1276 n->audit_session_id = c->audit_session_id;
1277 n->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
1278 }
1279 if (c->mask & mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
1280 n->audit_login_uid = c->audit_login_uid;
1281 n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
1282 }
1283
1284 if (c->mask & mask & SD_BUS_CREDS_TTY) {
1285 if (c->tty) {
1286 n->tty = strdup(c->tty);
1287 if (!n->tty)
1288 return -ENOMEM12;
1289 } else
1290 n->tty = NULL((void*)0);
1291 n->mask |= SD_BUS_CREDS_TTY;
1292 }
1293
1294 if (c->mask & mask & SD_BUS_CREDS_UNIQUE_NAME) {
1295 assert(c->unique_name)do { if ((__builtin_expect(!!(!(c->unique_name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->unique_name"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1295, __PRETTY_FUNCTION__); } while (0)
;
1296
1297 n->unique_name = strdup(c->unique_name);
1298 if (!n->unique_name)
1299 return -ENOMEM12;
1300 n->mask |= SD_BUS_CREDS_UNIQUE_NAME;
1301 }
1302
1303 if (c->mask & mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
1304 if (strv_isempty(c->well_known_names))
1305 n->well_known_names = NULL((void*)0);
1306 else {
1307 n->well_known_names = strv_copy(c->well_known_names);
1308 if (!n->well_known_names)
1309 return -ENOMEM12;
1310 }
1311 n->well_known_names_driver = c->well_known_names_driver;
1312 n->well_known_names_local = c->well_known_names_local;
1313 n->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
1314 }
1315
1316 if (c->mask & mask & SD_BUS_CREDS_DESCRIPTION) {
1317 assert(c->description)do { if ((__builtin_expect(!!(!(c->description)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c->description"), "../src/libsystemd/sd-bus/bus-creds.c"
, 1317, __PRETTY_FUNCTION__); } while (0)
;
1318 n->description = strdup(c->description);
1319 if (!n->description)
1320 return -ENOMEM12;
1321 n->mask |= SD_BUS_CREDS_DESCRIPTION;
1322 }
1323
1324 n->augmented = c->augmented & n->mask;
1325
1326 /* Get more data */
1327
1328 r = bus_creds_add_more(n, mask, 0, 0);
1329 if (r < 0)
1330 return r;
1331
1332 *ret = TAKE_PTR(n)({ typeof(n) _ptr_ = (n); (n) = ((void*)0); _ptr_; });
1333
1334 return 0;
1335}