Bug Summary

File:build-scan/../src/libsystemd/sd-bus/bus-dump.c
Warning:line 272, column 16
Potential leak of memory pointed to by 'prefix'

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-dump.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-dump.c

../src/libsystemd/sd-bus/bus-dump.c

1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3***/
4
5#include "alloc-util.h"
6#include "bus-dump.h"
7#include "bus-internal.h"
8#include "bus-message.h"
9#include "bus-type.h"
10#include "cap-list.h"
11#include "capability-util.h"
12#include "fileio.h"
13#include "format-util.h"
14#include "locale-util.h"
15#include "macro.h"
16#include "string-util.h"
17#include "strv.h"
18#include "terminal-util.h"
19#include "util.h"
20
21static char *indent(unsigned level, unsigned flags) {
22 char *p;
23 unsigned n, i = 0;
24
25 n = 0;
26
27 if (flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY && level > 0)
28 level -= 1;
29
30 if (flags & BUS_MESSAGE_DUMP_WITH_HEADER)
22
Taking false branch
31 n += 2;
32
33 p = new(char, n + level*8 + 1)((char*) malloc_multiply(sizeof(char), (n + level*8 + 1)));
23
Calling 'malloc_multiply'
26
Returned allocated memory
34 if (!p)
27
Assuming 'p' is non-null
28
Taking false branch
35 return NULL((void*)0);
36
37 if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
29
Taking false branch
38 p[i++] = ' ';
39 p[i++] = ' ';
40 }
41
42 memset(p + i, ' ', level*8);
43 p[i + level*8] = 0;
44
45 return p;
46}
47
48int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags) {
49 unsigned level = 1;
50 int r;
51
52 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/bus-dump.c"
, 52, __PRETTY_FUNCTION__); } while (0)
;
1
Assuming 'm' is non-null
2
Taking false branch
3
Loop condition is false. Exiting loop
53
54 if (!f)
4
Assuming 'f' is non-null
5
Taking false branch
55 f = stdoutstdout;
56
57 if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
6
Assuming the condition is false
7
Taking false branch
58 fprintf(f,
59 "%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u Priority=%"PRIi64"l" "i",
60 m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() :
61 m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() :
62 m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "",
63 special_glyph(TRIANGULAR_BULLET),
64 ansi_normal(),
65
66 ansi_highlight(),
67 bus_message_type_to_string(m->header->type) ?: "(unknown)",
68 ansi_normal(),
69
70 m->header->endian,
71 m->header->flags,
72 m->header->version,
73 m->priority);
74
75 /* Display synthetic message serial number in a more readable
76 * format than (uint32_t) -1 */
77 if (BUS_MESSAGE_COOKIE(m) == 0xFFFFFFFFULL)
78 fprintf(f, " Cookie=-1");
79 else
80 fprintf(f, " Cookie=%" PRIu64"l" "u", BUS_MESSAGE_COOKIE(m));
81
82 if (m->reply_cookie != 0)
83 fprintf(f, " ReplyCookie=%" PRIu64"l" "u", m->reply_cookie);
84
85 fputs("\n", f);
86
87 if (m->sender)
88 fprintf(f, " Sender=%s%s%s", ansi_highlight(), m->sender, ansi_normal());
89 if (m->destination)
90 fprintf(f, " Destination=%s%s%s", ansi_highlight(), m->destination, ansi_normal());
91 if (m->path)
92 fprintf(f, " Path=%s%s%s", ansi_highlight(), m->path, ansi_normal());
93 if (m->interface)
94 fprintf(f, " Interface=%s%s%s", ansi_highlight(), m->interface, ansi_normal());
95 if (m->member)
96 fprintf(f, " Member=%s%s%s", ansi_highlight(), m->member, ansi_normal());
97
98 if (m->sender || m->destination || m->path || m->interface || m->member)
99 fputs("\n", f);
100
101 if (sd_bus_error_is_set(&m->error))
102 fprintf(f,
103 " ErrorName=%s%s%s"
104 " ErrorMessage=%s\"%s\"%s\n",
105 ansi_highlight_red(), strna(m->error.name), ansi_normal(),
106 ansi_highlight_red(), strna(m->error.message), ansi_normal());
107
108 if (m->monotonic != 0)
109 fprintf(f, " Monotonic="USEC_FMT"%" "l" "u", m->monotonic);
110 if (m->realtime != 0)
111 fprintf(f, " Realtime="USEC_FMT"%" "l" "u", m->realtime);
112 if (m->seqnum != 0)
113 fprintf(f, " SequenceNumber=%"PRIu64"l" "u", m->seqnum);
114
115 if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0)
116 fputs("\n", f);
117
118 bus_creds_dump(&m->creds, f, true1);
119 }
120
121 r = sd_bus_message_rewind(m, !(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY));
8
Assuming the condition is true
122 if (r < 0)
9
Assuming 'r' is >= 0
10
Taking false branch
123 return log_error_errno(r, "Failed to rewind: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/bus-dump.c", 123, __func__, "Failed to rewind: %m"
) : -abs(_e); })
;
124
125 if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
11
Taking true branch
126 _cleanup_free___attribute__((cleanup(freep))) char *prefix = NULL((void*)0);
127
128 prefix = indent(0, flags);
129 if (!prefix
11.1
'prefix' is non-null
11.1
'prefix' is non-null
)
12
Taking false branch
130 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/libsystemd/sd-bus/bus-dump.c"
, 130, __func__)
;
131
132 fprintf(f, "%sMESSAGE \"%s\" {\n", prefix, strempty(m->root_container.signature));
133 }
134
135 for (;;) {
13
Loop condition is true. Entering loop body
136 _cleanup_free___attribute__((cleanup(freep))) char *prefix = NULL((void*)0);
137 const char *contents = NULL((void*)0);
138 char type;
139 union {
140 uint8_t u8;
141 uint16_t u16;
142 int16_t s16;
143 uint32_t u32;
144 int32_t s32;
145 uint64_t u64;
146 int64_t s64;
147 double d64;
148 const char *string;
149 int i;
150 } basic;
151
152 r = sd_bus_message_peek_type(m, &type, &contents);
153 if (r < 0)
14
Assuming 'r' is >= 0
15
Taking false branch
154 return log_error_errno(r, "Failed to peek type: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/bus-dump.c", 154, __func__, "Failed to peek type: %m"
) : -abs(_e); })
;
155
156 if (r == 0) {
16
Assuming 'r' is equal to 0
17
Taking true branch
157 if (level
17.1
'level' is <= 1
17.1
'level' is <= 1
<= 1)
18
Taking true branch
158 break;
19
Execution continues on line 262
159
160 r = sd_bus_message_exit_container(m);
161 if (r < 0)
162 return log_error_errno(r, "Failed to exit container: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/bus-dump.c", 162, __func__, "Failed to exit container: %m"
) : -abs(_e); })
;
163
164 level--;
165
166 prefix = indent(level, flags);
167 if (!prefix)
168 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/libsystemd/sd-bus/bus-dump.c"
, 168, __func__)
;
169
170 fprintf(f, "%s};\n", prefix);
171 continue;
172 }
173
174 prefix = indent(level, flags);
175 if (!prefix)
176 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/libsystemd/sd-bus/bus-dump.c"
, 176, __func__)
;
177
178 if (bus_type_is_container(type) > 0) {
179 r = sd_bus_message_enter_container(m, type, contents);
180 if (r < 0)
181 return log_error_errno(r, "Failed to enter container: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/bus-dump.c", 181, __func__, "Failed to enter container: %m"
) : -abs(_e); })
;
182
183 if (type == SD_BUS_TYPE_ARRAY)
184 fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
185 else if (type == SD_BUS_TYPE_VARIANT)
186 fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
187 else if (type == SD_BUS_TYPE_STRUCT)
188 fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
189 else if (type == SD_BUS_TYPE_DICT_ENTRY)
190 fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
191
192 level++;
193
194 continue;
195 }
196
197 r = sd_bus_message_read_basic(m, type, &basic);
198 if (r < 0)
199 return log_error_errno(r, "Failed to get basic: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/libsystemd/sd-bus/bus-dump.c", 199, __func__, "Failed to get basic: %m"
) : -abs(_e); })
;
200
201 assert(r > 0)do { if ((__builtin_expect(!!(!(r > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("r > 0"), "../src/libsystemd/sd-bus/bus-dump.c"
, 201, __PRETTY_FUNCTION__); } while (0)
;
202
203 switch (type) {
204
205 case SD_BUS_TYPE_BYTE:
206 fprintf(f, "%sBYTE %s%u%s;\n", prefix, ansi_highlight(), basic.u8, ansi_normal());
207 break;
208
209 case SD_BUS_TYPE_BOOLEAN:
210 fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_normal());
211 break;
212
213 case SD_BUS_TYPE_INT16:
214 fprintf(f, "%sINT16 %s%i%s;\n", prefix, ansi_highlight(), basic.s16, ansi_normal());
215 break;
216
217 case SD_BUS_TYPE_UINT16:
218 fprintf(f, "%sUINT16 %s%u%s;\n", prefix, ansi_highlight(), basic.u16, ansi_normal());
219 break;
220
221 case SD_BUS_TYPE_INT32:
222 fprintf(f, "%sINT32 %s%i%s;\n", prefix, ansi_highlight(), basic.s32, ansi_normal());
223 break;
224
225 case SD_BUS_TYPE_UINT32:
226 fprintf(f, "%sUINT32 %s%u%s;\n", prefix, ansi_highlight(), basic.u32, ansi_normal());
227 break;
228
229 case SD_BUS_TYPE_INT64:
230 fprintf(f, "%sINT64 %s%"PRIi64"l" "i""%s;\n", prefix, ansi_highlight(), basic.s64, ansi_normal());
231 break;
232
233 case SD_BUS_TYPE_UINT64:
234 fprintf(f, "%sUINT64 %s%"PRIu64"l" "u""%s;\n", prefix, ansi_highlight(), basic.u64, ansi_normal());
235 break;
236
237 case SD_BUS_TYPE_DOUBLE:
238 fprintf(f, "%sDOUBLE %s%g%s;\n", prefix, ansi_highlight(), basic.d64, ansi_normal());
239 break;
240
241 case SD_BUS_TYPE_STRING:
242 fprintf(f, "%sSTRING \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal());
243 break;
244
245 case SD_BUS_TYPE_OBJECT_PATH:
246 fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal());
247 break;
248
249 case SD_BUS_TYPE_SIGNATURE:
250 fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal());
251 break;
252
253 case SD_BUS_TYPE_UNIX_FD:
254 fprintf(f, "%sUNIX_FD %s%i%s;\n", prefix, ansi_highlight(), basic.i, ansi_normal());
255 break;
256
257 default:
258 assert_not_reached("Unknown basic type.")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unknown basic type."), "../src/libsystemd/sd-bus/bus-dump.c"
, 258, __PRETTY_FUNCTION__); } while (0)
;
259 }
260 }
261
262 if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
20
Taking true branch
263 _cleanup_free___attribute__((cleanup(freep))) char *prefix = NULL((void*)0);
264
265 prefix = indent(0, flags);
21
Calling 'indent'
30
Returned allocated memory
266 if (!prefix
30.1
'prefix' is non-null
30.1
'prefix' is non-null
)
31
Taking false branch
267 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/libsystemd/sd-bus/bus-dump.c"
, 267, __func__)
;
268
269 fprintf(f, "%s};\n\n", prefix);
270 }
271
272 return 0;
32
Potential leak of memory pointed to by 'prefix'
273}
274
275static void dump_capabilities(
276 sd_bus_creds *c,
277 FILE *f,
278 const char *name,
279 bool_Bool terse,
280 int (*has)(sd_bus_creds *c, int capability)) {
281
282 unsigned long i, last_cap;
283 unsigned n = 0;
284 int r;
285
286 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-dump.c"
, 286, __PRETTY_FUNCTION__); } while (0)
;
287 assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("f"), "../src/libsystemd/sd-bus/bus-dump.c"
, 287, __PRETTY_FUNCTION__); } while (0)
;
288 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/libsystemd/sd-bus/bus-dump.c"
, 288, __PRETTY_FUNCTION__); } while (0)
;
289 assert(has)do { if ((__builtin_expect(!!(!(has)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("has"), "../src/libsystemd/sd-bus/bus-dump.c"
, 289, __PRETTY_FUNCTION__); } while (0)
;
290
291 i = 0;
292 r = has(c, i);
293 if (r < 0)
294 return;
295
296 fprintf(f, "%s%s=%s", terse ? " " : "", name, terse ? "" : ansi_highlight());
297 last_cap = cap_last_cap();
298
299 for (;;) {
300 if (r > 0) {
301
302 if (n > 0)
303 fputc(' ', f);
304 if (n % 4 == 3)
305 fprintf(f, terse ? "\n " : "\n ");
306
307 fprintf(f, "%s", strna(capability_to_name(i)));
308 n++;
309 }
310
311 i++;
312
313 if (i > last_cap)
314 break;
315
316 r = has(c, i);
317 }
318
319 fputs("\n", f);
320
321 if (!terse)
322 fputs(ansi_normal(), f);
323}
324
325int bus_creds_dump(sd_bus_creds *c, FILE *f, bool_Bool terse) {
326 uid_t owner, audit_loginuid;
327 uint32_t audit_sessionid;
328 char **cmdline = NULL((void*)0), **well_known = NULL((void*)0);
329 const char *prefix, *color, *suffix, *s;
330 int r, q, v, w, z;
331
332 assert(c)do { if ((__builtin_expect(!!(!(c)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("c"), "../src/libsystemd/sd-bus/bus-dump.c"
, 332, __PRETTY_FUNCTION__); } while (0)
;
333
334 if (!f)
335 f = stdoutstdout;
336
337 if (terse) {
338 prefix = " ";
339 suffix = "";
340 color = "";
341 } else {
342 const char *off;
343
344 prefix = "";
345 color = ansi_highlight();
346
347 off = ansi_normal();
348 suffix = strjoina(off, "\n")({ const char *_appendees_[] = { off, "\n" }; char *_d_, *_p_
; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__
(__builtin_choose_expr( !__builtin_types_compatible_p(typeof
(_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_
)/sizeof((_appendees_)[0]), ((void)0))) && _appendees_
[_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca
(_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr
( !__builtin_types_compatible_p(typeof(_appendees_), typeof(&
*(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0]
), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy
(_p_, _appendees_[_i_]); *_p_ = 0; _d_; })
;
349 }
350
351 if (c->mask & SD_BUS_CREDS_PID)
352 fprintf(f, "%sPID=%s"PID_FMT"%" "i""%s", prefix, color, c->pid, suffix);
353 if (c->mask & SD_BUS_CREDS_TID)
354 fprintf(f, "%sTID=%s"PID_FMT"%" "i""%s", prefix, color, c->tid, suffix);
355 if (c->mask & SD_BUS_CREDS_PPID) {
356 if (c->ppid == 0)
357 fprintf(f, "%sPPID=%sn/a%s", prefix, color, suffix);
358 else
359 fprintf(f, "%sPPID=%s"PID_FMT"%" "i""%s", prefix, color, c->ppid, suffix);
360 }
361 if (c->mask & SD_BUS_CREDS_TTY)
362 fprintf(f, "%sTTY=%s%s%s", prefix, color, strna(c->tty), suffix);
363
364 if (terse && ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_PPID|SD_BUS_CREDS_TTY))))
365 fputs("\n", f);
366
367 if (c->mask & SD_BUS_CREDS_UID)
368 fprintf(f, "%sUID=%s"UID_FMT"%" "u""%s", prefix, color, c->uid, suffix);
369 if (c->mask & SD_BUS_CREDS_EUID)
370 fprintf(f, "%sEUID=%s"UID_FMT"%" "u""%s", prefix, color, c->euid, suffix);
371 if (c->mask & SD_BUS_CREDS_SUID)
372 fprintf(f, "%sSUID=%s"UID_FMT"%" "u""%s", prefix, color, c->suid, suffix);
373 if (c->mask & SD_BUS_CREDS_FSUID)
374 fprintf(f, "%sFSUID=%s"UID_FMT"%" "u""%s", prefix, color, c->fsuid, suffix);
375 r = sd_bus_creds_get_owner_uid(c, &owner);
376 if (r >= 0)
377 fprintf(f, "%sOwnerUID=%s"UID_FMT"%" "u""%s", prefix, color, owner, suffix);
378 if (c->mask & SD_BUS_CREDS_GID)
379 fprintf(f, "%sGID=%s"GID_FMT"%" "u""%s", prefix, color, c->gid, suffix);
380 if (c->mask & SD_BUS_CREDS_EGID)
381 fprintf(f, "%sEGID=%s"GID_FMT"%" "u""%s", prefix, color, c->egid, suffix);
382 if (c->mask & SD_BUS_CREDS_SGID)
383 fprintf(f, "%sSGID=%s"GID_FMT"%" "u""%s", prefix, color, c->sgid, suffix);
384 if (c->mask & SD_BUS_CREDS_FSGID)
385 fprintf(f, "%sFSGID=%s"GID_FMT"%" "u""%s", prefix, color, c->fsgid, suffix);
386
387 if (c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
388 unsigned i;
389
390 fprintf(f, "%sSupplementaryGIDs=%s", prefix, color);
391 for (i = 0; i < c->n_supplementary_gids; i++)
392 fprintf(f, "%s" GID_FMT"%" "u", i > 0 ? " " : "", c->supplementary_gids[i]);
393 fprintf(f, "%s", suffix);
394 }
395
396 if (terse && ((c->mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
397 SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
398 SD_BUS_CREDS_SUPPLEMENTARY_GIDS)) || r >= 0))
399 fputs("\n", f);
400
401 if (c->mask & SD_BUS_CREDS_COMM)
402 fprintf(f, "%sComm=%s%s%s", prefix, color, c->comm, suffix);
403 if (c->mask & SD_BUS_CREDS_TID_COMM)
404 fprintf(f, "%sTIDComm=%s%s%s", prefix, color, c->tid_comm, suffix);
405 if (c->mask & SD_BUS_CREDS_EXE)
406 fprintf(f, "%sExe=%s%s%s", prefix, color, strna(c->exe), suffix);
407
408 if (terse && (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)))
409 fputs("\n", f);
410
411 r = sd_bus_creds_get_cmdline(c, &cmdline);
412 if (r >= 0) {
413 char **i;
414
415 fprintf(f, "%sCommandLine=%s", prefix, color);
416 STRV_FOREACH(i, cmdline)for ((i) = (cmdline); (i) && *(i); (i)++) {
417 if (i != cmdline)
418 fputc(' ', f);
419
420 fputs(*i, f);
421 }
422
423 fprintf(f, "%s", suffix);
424 } else if (r != -ENODATA61)
425 fprintf(f, "%sCommandLine=%sn/a%s", prefix, color, suffix);
426
427 if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
428 fprintf(f, "%sLabel=%s%s%s", prefix, color, c->label, suffix);
429 if (c->mask & SD_BUS_CREDS_DESCRIPTION)
430 fprintf(f, "%sDescription=%s%s%s", prefix, color, c->description, suffix);
431
432 if (terse && (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION)))
433 fputs("\n", f);
434
435 if (c->mask & SD_BUS_CREDS_CGROUP)
436 fprintf(f, "%sCGroup=%s%s%s", prefix, color, c->cgroup, suffix);
437 s = NULL((void*)0);
438 r = sd_bus_creds_get_unit(c, &s);
439 if (r != -ENODATA61)
440 fprintf(f, "%sUnit=%s%s%s", prefix, color, strna(s), suffix);
441 s = NULL((void*)0);
442 v = sd_bus_creds_get_slice(c, &s);
443 if (v != -ENODATA61)
444 fprintf(f, "%sSlice=%s%s%s", prefix, color, strna(s), suffix);
445 s = NULL((void*)0);
446 q = sd_bus_creds_get_user_unit(c, &s);
447 if (q != -ENODATA61)
448 fprintf(f, "%sUserUnit=%s%s%s", prefix, color, strna(s), suffix);
449 s = NULL((void*)0);
450 w = sd_bus_creds_get_user_slice(c, &s);
451 if (w != -ENODATA61)
452 fprintf(f, "%sUserSlice=%s%s%s", prefix, color, strna(s), suffix);
453 s = NULL((void*)0);
454 z = sd_bus_creds_get_session(c, &s);
455 if (z != -ENODATA61)
456 fprintf(f, "%sSession=%s%s%s", prefix, color, strna(s), suffix);
457
458 if (terse && ((c->mask & SD_BUS_CREDS_CGROUP) || r != -ENODATA61 || q != -ENODATA61 || v != -ENODATA61 || w != -ENODATA61 || z != -ENODATA61))
459 fputs("\n", f);
460
461 r = sd_bus_creds_get_audit_login_uid(c, &audit_loginuid);
462 if (r >= 0)
463 fprintf(f, "%sAuditLoginUID=%s"UID_FMT"%" "u""%s", prefix, color, audit_loginuid, suffix);
464 else if (r != -ENODATA61)
465 fprintf(f, "%sAuditLoginUID=%sn/a%s", prefix, color, suffix);
466 q = sd_bus_creds_get_audit_session_id(c, &audit_sessionid);
467 if (q >= 0)
468 fprintf(f, "%sAuditSessionID=%s%"PRIu32"u""%s", prefix, color, audit_sessionid, suffix);
469 else if (q != -ENODATA61)
470 fprintf(f, "%sAuditSessionID=%sn/a%s", prefix, color, suffix);
471
472 if (terse && (r != -ENODATA61 || q != -ENODATA61))
473 fputs("\n", f);
474
475 if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
476 fprintf(f, "%sUniqueName=%s%s%s", prefix, color, c->unique_name, suffix);
477
478 if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
479 char **i;
480
481 fprintf(f, "%sWellKnownNames=%s", prefix, color);
482 STRV_FOREACH(i, well_known)for ((i) = (well_known); (i) && *(i); (i)++) {
483 if (i != well_known)
484 fputc(' ', f);
485
486 fputs(*i, f);
487 }
488
489 fprintf(f, "%s", suffix);
490 }
491
492 if (terse && (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known))
493 fputc('\n', f);
494
495 dump_capabilities(c, f, "EffectiveCapabilities", terse, sd_bus_creds_has_effective_cap);
496 dump_capabilities(c, f, "PermittedCapabilities", terse, sd_bus_creds_has_permitted_cap);
497 dump_capabilities(c, f, "InheritableCapabilities", terse, sd_bus_creds_has_inheritable_cap);
498 dump_capabilities(c, f, "BoundingCapabilities", terse, sd_bus_creds_has_bounding_cap);
499
500 return 0;
501}
502
503/*
504 * For details about the file format, see:
505 *
506 * http://wiki.wireshark.org/Development/LibpcapFileFormat
507 */
508
509typedef struct _packed___attribute__ ((packed)) pcap_hdr_s {
510 uint32_t magic_number; /* magic number */
511 uint16_t version_major; /* major version number */
512 uint16_t version_minor; /* minor version number */
513 int32_t thiszone; /* GMT to local correction */
514 uint32_t sigfigs; /* accuracy of timestamps */
515 uint32_t snaplen; /* max length of captured packets, in octets */
516 uint32_t network; /* data link type */
517} pcap_hdr_t ;
518
519typedef struct _packed___attribute__ ((packed)) pcaprec_hdr_s {
520 uint32_t ts_sec; /* timestamp seconds */
521 uint32_t ts_usec; /* timestamp microseconds */
522 uint32_t incl_len; /* number of octets of packet saved in file */
523 uint32_t orig_len; /* actual length of packet */
524} pcaprec_hdr_t;
525
526int bus_pcap_header(size_t snaplen, FILE *f) {
527
528 pcap_hdr_t hdr = {
529 .magic_number = 0xa1b2c3d4U,
530 .version_major = 2,
531 .version_minor = 4,
532 .thiszone = 0, /* UTC */
533 .sigfigs = 0,
534 .network = 231, /* D-Bus */
535 };
536
537 if (!f)
538 f = stdoutstdout;
539
540 assert(snaplen > 0)do { if ((__builtin_expect(!!(!(snaplen > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("snaplen > 0"), "../src/libsystemd/sd-bus/bus-dump.c"
, 540, __PRETTY_FUNCTION__); } while (0)
;
541 assert((size_t) (uint32_t) snaplen == snaplen)do { if ((__builtin_expect(!!(!((size_t) (uint32_t) snaplen ==
snaplen)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("(size_t) (uint32_t) snaplen == snaplen"
), "../src/libsystemd/sd-bus/bus-dump.c", 541, __PRETTY_FUNCTION__
); } while (0)
;
542
543 hdr.snaplen = (uint32_t) snaplen;
544
545 fwrite(&hdr, 1, sizeof(hdr), f);
546
547 return fflush_and_check(f);
548}
549
550int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) {
551 struct bus_body_part *part;
552 pcaprec_hdr_t hdr = {};
553 struct timeval tv;
554 unsigned i;
555 size_t w;
556
557 if (!f)
558 f = stdoutstdout;
559
560 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/libsystemd/sd-bus/bus-dump.c"
, 560, __PRETTY_FUNCTION__); } while (0)
;
561 assert(snaplen > 0)do { if ((__builtin_expect(!!(!(snaplen > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("snaplen > 0"), "../src/libsystemd/sd-bus/bus-dump.c"
, 561, __PRETTY_FUNCTION__); } while (0)
;
562 assert((size_t) (uint32_t) snaplen == snaplen)do { if ((__builtin_expect(!!(!((size_t) (uint32_t) snaplen ==
snaplen)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("(size_t) (uint32_t) snaplen == snaplen"
), "../src/libsystemd/sd-bus/bus-dump.c", 562, __PRETTY_FUNCTION__
); } while (0)
;
563
564 if (m->realtime != 0)
565 timeval_store(&tv, m->realtime);
566 else
567 assert_se(gettimeofday(&tv, NULL) >= 0)do { if ((__builtin_expect(!!(!(gettimeofday(&tv, ((void*
)0)) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD
, ("gettimeofday(&tv, NULL) >= 0"), "../src/libsystemd/sd-bus/bus-dump.c"
, 567, __PRETTY_FUNCTION__); } while (0)
;
568
569 hdr.ts_sec = tv.tv_sec;
570 hdr.ts_usec = tv.tv_usec;
571 hdr.orig_len = BUS_MESSAGE_SIZE(m);
572 hdr.incl_len = MIN(hdr.orig_len, snaplen)__extension__ ({ const typeof((hdr.orig_len)) __unique_prefix_A19
= ((hdr.orig_len)); const typeof((snaplen)) __unique_prefix_B20
= ((snaplen)); __unique_prefix_A19 < __unique_prefix_B20 ?
__unique_prefix_A19 : __unique_prefix_B20; })
;
573
574 /* write the pcap header */
575 fwrite(&hdr, 1, sizeof(hdr), f);
576
577 /* write the dbus header */
578 w = MIN(BUS_MESSAGE_BODY_BEGIN(m), snaplen)__extension__ ({ const typeof((BUS_MESSAGE_BODY_BEGIN(m))) __unique_prefix_A21
= ((BUS_MESSAGE_BODY_BEGIN(m))); const typeof((snaplen)) __unique_prefix_B22
= ((snaplen)); __unique_prefix_A21 < __unique_prefix_B22 ?
__unique_prefix_A21 : __unique_prefix_B22; })
;
579 fwrite(m->header, 1, w, f);
580 snaplen -= w;
581
582 /* write the dbus body */
583 MESSAGE_FOREACH_PART(part, i, m)for ((i) = 0, (part) = &(m)->body; (i) < (m)->n_body_parts
; (i)++, (part) = (part)->next)
{
584 if (snaplen <= 0)
585 break;
586
587 w = MIN(part->size, snaplen)__extension__ ({ const typeof((part->size)) __unique_prefix_A23
= ((part->size)); const typeof((snaplen)) __unique_prefix_B24
= ((snaplen)); __unique_prefix_A23 < __unique_prefix_B24 ?
__unique_prefix_A23 : __unique_prefix_B24; })
;
588 fwrite(part->data, 1, w, f);
589 snaplen -= w;
590 }
591
592 return fflush_and_check(f);
593}

../src/basic/alloc-util.h

1/* SPDX-License-Identifier: LGPL-2.1+ */
2#pragma once
3
4#include <alloca.h>
5#include <stddef.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "macro.h"
10
11#define new(t, n)((t*) malloc_multiply(sizeof(t), (n))) ((t*) malloc_multiply(sizeof(t), (n)))
12
13#define new0(t, n)((t*) calloc((n), sizeof(t))) ((t*) calloc((n), sizeof(t)))
14
15#define newa(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 15, __PRETTY_FUNCTION__); } while
(0); (t*) __builtin_alloca (sizeof(t)*(n)); })
\
16 ({ \
17 assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 17, __PRETTY_FUNCTION__); } while
(0)
; \
18 (t*) alloca(sizeof(t)*(n))__builtin_alloca (sizeof(t)*(n)); \
19 })
20
21#define newa0(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 21, __PRETTY_FUNCTION__); } while
(0); (t*) ({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_
= __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_)
; }); })
\
22 ({ \
23 assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 23, __PRETTY_FUNCTION__); } while
(0)
; \
24 (t*) alloca0(sizeof(t)*(n))({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca
(_len_); (void *) memset(_new_, 0, _len_); })
; \
25 })
26
27#define newdup(t, p, n)((t*) memdup_multiply(p, sizeof(t), (n))) ((t*) memdup_multiply(p, sizeof(t), (n)))
28
29#define newdup_suffix0(t, p, n)((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n)))
30
31#define malloc0(n)(calloc(1, (n))) (calloc(1, (n)))
32
33static inline void *mfree(void *memory) {
34 free(memory);
35 return NULL((void*)0);
36}
37
38#define free_and_replace(a, b)({ free(a); (a) = (b); (b) = ((void*)0); 0; }) \
39 ({ \
40 free(a); \
41 (a) = (b); \
42 (b) = NULL((void*)0); \
43 0; \
44 })
45
46void* memdup(const void *p, size_t l) _alloc_(2);
47void* memdup_suffix0(const void *p, size_t l) _alloc_(2);
48
49static inline void freep(void *p) {
50 free(*(void**) p);
51}
52
53#define _cleanup_free___attribute__((cleanup(freep))) _cleanup_(freep)__attribute__((cleanup(freep)))
54
55static inline bool_Bool size_multiply_overflow(size_t size, size_t need) {
56 return _unlikely_(need != 0 && size > (SIZE_MAX / need))(__builtin_expect(!!(need != 0 && size > ((18446744073709551615UL
) / need)),0))
;
57}
58
59_malloc___attribute__ ((malloc)) _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
60 if (size_multiply_overflow(size, need))
24
Taking false branch
61 return NULL((void*)0);
62
63 return malloc(size * need);
25
Memory is allocated
64}
65
66#if !HAVE_REALLOCARRAY1
67_alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
68 if (size_multiply_overflow(size, need))
69 return NULL((void*)0);
70
71 return realloc(p, size * need);
72}
73#endif
74
75_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
76 if (size_multiply_overflow(size, need))
77 return NULL((void*)0);
78
79 return memdup(p, size * need);
80}
81
82_alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
83 if (size_multiply_overflow(size, need))
84 return NULL((void*)0);
85
86 return memdup_suffix0(p, size * need);
87}
88
89void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
90void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
91
92#define GREEDY_REALLOC(array, allocated, need)greedy_realloc((void**) &(array), &(allocated), (need
), sizeof((array)[0]))
\
93 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
94
95#define GREEDY_REALLOC0(array, allocated, need)greedy_realloc0((void**) &(array), &(allocated), (need
), sizeof((array)[0]))
\
96 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
97
98#define alloca0(n)({ char *_new_; size_t _len_ = n; _new_ = __builtin_alloca (_len_
); (void *) memset(_new_, 0, _len_); })
\
99 ({ \
100 char *_new_; \
101 size_t _len_ = n; \
102 _new_ = alloca(_len_)__builtin_alloca (_len_); \
103 (void *) memset(_new_, 0, _len_); \
104 })
105
106/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
107#define alloca_align(size, align)({ void *_ptr_; size_t _mask_ = (align) - 1; _ptr_ = __builtin_alloca
((size) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) &
~_mask_); })
\
108 ({ \
109 void *_ptr_; \
110 size_t _mask_ = (align) - 1; \
111 _ptr_ = alloca((size) + _mask_)__builtin_alloca ((size) + _mask_); \
112 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
113 })
114
115#define alloca0_align(size, align)({ void *_new_; size_t _size_ = (size); _new_ = ({ void *_ptr_
; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_
) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_
); }); (void*)memset(_new_, 0, _size_); })
\
116 ({ \
117 void *_new_; \
118 size_t _size_ = (size); \
119 _new_ = alloca_align(_size_, (align))({ void *_ptr_; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca
((_size_) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) &
~_mask_); })
; \
120 (void*)memset(_new_, 0, _size_); \
121 })
122
123/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
124 * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
125#define TAKE_PTR(ptr)({ typeof(ptr) _ptr_ = (ptr); (ptr) = ((void*)0); _ptr_; }) \
126 ({ \
127 typeof(ptr) _ptr_ = (ptr); \
128 (ptr) = NULL((void*)0); \
129 _ptr_; \
130 })