| 1 |
/* |
| 2 |
* D-Bus Messages |
| 3 |
* |
| 4 |
* This encapsulates incoming and outgoing D-Bus messages. This is used to hold the |
| 5 |
* message data, the attached FDs and optional the cached metadata. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <c-dvar.h> |
| 9 |
#include <c-dvar-type.h> |
| 10 |
#include <c-stdaux.h> |
| 11 |
#include <endian.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include "dbus/message.h" |
| 14 |
#include "dbus/protocol.h" |
| 15 |
#include "util/error.h" |
| 16 |
#include "util/fdlist.h" |
| 17 |
#include "util/log.h" |
| 18 |
|
| 19 |
static_assert(_DBUS_MESSAGE_FIELD_N <= 8 * sizeof(unsigned int), "Header fields exceed bitmap"); |
| 20 |
|
| 21 |
static int message_new(Message **messagep, bool big_endian, size_t n_extra) { |
| 22 |
_c_cleanup_(message_unrefp) Message *message = NULL; |
| 23 |
|
| 24 |
static_assert(alignof(message->extra) >= 8, |
| 25 |
"Message payload has insufficient alignment"); |
| 26 |
|
| 27 |
message = malloc(sizeof(*message) + c_align_to(n_extra, 8)); |
| 28 |
if (!message) |
| 29 |
return error_origin(-ENOMEM); |
| 30 |
|
| 31 |
*message = (Message)MESSAGE_INIT(big_endian); |
| 32 |
|
| 33 |
*messagep = message; |
| 34 |
message = NULL; |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* message_new_incoming() - create new incoming message object |
| 40 |
* @messagep: output pointer to new message object |
| 41 |
* @header: header of new message |
| 42 |
* |
| 43 |
* This creates a new message object in @messagep, to hold an incoming message with |
| 44 |
* header @header. Only the header is initialized, the backing memory for the message |
| 45 |
* payload is allocated, but not yet initialized. |
| 46 |
* |
| 47 |
* Return: 0 on success, MESSAGE_E_CORRUPT_HEADER if unknown endianness, |
| 48 |
* MESSAGE_E_TOO_LARGE if the declared message size violates the spec, |
| 49 |
* or a negative error code on failure. |
| 50 |
*/ |
| 51 |
int message_new_incoming(Message **messagep, MessageHeader header) { |
| 52 |
_c_cleanup_(message_unrefp) Message *message = NULL; |
| 53 |
uint64_t n_header, n_body, n_data; |
| 54 |
int r; |
| 55 |
|
| 56 |
if (_c_likely_(header.endian == 'l')) { |
| 57 |
n_header = sizeof(header) + (uint64_t)le32toh(header.n_fields); |
| 58 |
n_body = (uint64_t)le32toh(header.n_body); |
| 59 |
} else if (header.endian == 'B') { |
| 60 |
n_header = sizeof(header) + (uint64_t)be32toh(header.n_fields); |
| 61 |
n_body = (uint64_t)be32toh(header.n_body); |
| 62 |
} else { |
| 63 |
return MESSAGE_E_CORRUPT_HEADER; |
| 64 |
} |
| 65 |
|
| 66 |
n_data = c_align_to(n_header, 8) + n_body; |
| 67 |
if (n_data > MESSAGE_SIZE_MAX) |
| 68 |
return MESSAGE_E_TOO_LARGE; |
| 69 |
|
| 70 |
r = message_new(&message, (header.endian == 'B'), n_data); |
| 71 |
if (r) |
| 72 |
return error_trace(r); |
| 73 |
|
| 74 |
message->n_data = n_data; |
| 75 |
message->n_header = n_header; |
| 76 |
message->n_body = n_body; |
| 77 |
message->data = message->extra; |
| 78 |
message->header = (void *)message->data; |
| 79 |
message->body = message->data + c_align_to(n_header, 8); |
| 80 |
message->vecs[0] = (struct iovec){ message->header, c_align_to(n_header, 8) }; |
| 81 |
message->vecs[1] = (struct iovec){ NULL, 0 }; |
| 82 |
message->vecs[2] = (struct iovec){ NULL, 0 }; |
| 83 |
message->vecs[3] = (struct iovec){ message->body, n_body }; |
| 84 |
|
| 85 |
message->n_copied += sizeof(header); |
| 86 |
c_memcpy(message->data, &header, sizeof(header)); |
| 87 |
|
| 88 |
*messagep = message; |
| 89 |
message = NULL; |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* message_new_outgoing() - create a new outgoing message object |
| 95 |
* @messagep: return pointer to new message object |
| 96 |
* @data: the message contents |
| 97 |
* @n_data: the size of the message contents |
| 98 |
* |
| 99 |
* The consumes the provided @data, which must be a valid D-Bus message and |
| 100 |
* creates an outgoing message representing it. |
| 101 |
* |
| 102 |
* Return: 0 on success, or a negative error code on failure. |
| 103 |
*/ |
| 104 |
int message_new_outgoing(Message **messagep, void *data, size_t n_data) { |
| 105 |
_c_cleanup_(message_unrefp) Message *message = NULL; |
| 106 |
MessageHeader *header = data; |
| 107 |
uint64_t n_header, n_body; |
| 108 |
int r; |
| 109 |
|
| 110 |
c_assert(n_data >= sizeof(MessageHeader)); |
| 111 |
c_assert(!((unsigned long)data & 0x7)); |
| 112 |
c_assert((header->endian == 'B') == (__BYTE_ORDER == __BIG_ENDIAN) && |
| 113 |
(header->endian == 'l') == (__BYTE_ORDER == __LITTLE_ENDIAN)); |
| 114 |
c_assert(n_data >= sizeof(MessageHeader) + c_align_to(header->n_fields, 8)); |
| 115 |
|
| 116 |
n_header = sizeof(MessageHeader) + header->n_fields; |
| 117 |
n_body = n_data - c_align_to(n_header, 8); |
| 118 |
|
| 119 |
header->n_body = n_data - sizeof(MessageHeader) - c_align_to(header->n_fields, 8); |
| 120 |
|
| 121 |
r = message_new(&message, (header->endian == 'B'), 0); |
| 122 |
if (r) |
| 123 |
return error_trace(r); |
| 124 |
|
| 125 |
message->allocated_data = true; |
| 126 |
message->n_data = n_data; |
| 127 |
message->n_header = n_header; |
| 128 |
message->n_body = n_body; |
| 129 |
message->data = data; |
| 130 |
message->header = (void *)message->data; |
| 131 |
message->body = message->data + c_align_to(n_header, 8); |
| 132 |
message->vecs[0] = (struct iovec){ message->header, c_align_to(n_header, 8) }; |
| 133 |
message->vecs[1] = (struct iovec){ NULL, 0 }; |
| 134 |
message->vecs[2] = (struct iovec){ NULL, 0 }; |
| 135 |
message->vecs[3] = (struct iovec){ message->body, n_body }; |
| 136 |
|
| 137 |
*messagep = message; |
| 138 |
message = NULL; |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
/* internal callback for message_unref() */ |
| 143 |
void message_free(_Atomic unsigned long *n_refs, void *userdata) { |
| 144 |
Message *message = c_container_of(n_refs, Message, n_refs); |
| 145 |
|
| 146 |
if (message->allocated_data) |
| 147 |
free(message->data); |
| 148 |
fdlist_free(message->fds); |
| 149 |
free(message); |
| 150 |
} |
| 151 |
|
| 152 |
static int message_parse_header(Message *message, MessageMetadata *metadata) { |
| 153 |
static const CDVarType type[] = { |
| 154 |
C_DVAR_T_INIT( |
| 155 |
C_DVAR_T_TUPLE7( |
| 156 |
C_DVAR_T_y, |
| 157 |
C_DVAR_T_y, |
| 158 |
C_DVAR_T_y, |
| 159 |
C_DVAR_T_y, |
| 160 |
C_DVAR_T_u, |
| 161 |
C_DVAR_T_u, |
| 162 |
C_DVAR_T_ARRAY( |
| 163 |
C_DVAR_T_TUPLE2( |
| 164 |
C_DVAR_T_y, |
| 165 |
C_DVAR_T_v |
| 166 |
) |
| 167 |
) |
| 168 |
) |
| 169 |
), /* (yyyyuua(yv)) */ |
| 170 |
}; |
| 171 |
_c_cleanup_(c_dvar_deinit) CDVar v = C_DVAR_INIT; |
| 172 |
unsigned int mask; |
| 173 |
uint8_t field; |
| 174 |
int r; |
| 175 |
|
| 176 |
c_dvar_begin_read(&v, message->big_endian, type, 1, message->header, message->n_header); |
| 177 |
|
| 178 |
/* |
| 179 |
* Validate static header fields. Byte-order and body-length are part |
| 180 |
* of the stream-validation, and skipped here. The others are: |
| 181 |
* type: |
| 182 |
* Anything but "INVALID" is accepted. |
| 183 |
* flags: |
| 184 |
* Anything is accepted. |
| 185 |
* version: |
| 186 |
* Must be '1'. |
| 187 |
* serial: |
| 188 |
* Anything but 0 is accepted. |
| 189 |
*/ |
| 190 |
|
| 191 |
c_dvar_read(&v, "(yyyyuu[", |
| 192 |
NULL, |
| 193 |
&metadata->header.type, |
| 194 |
&metadata->header.flags, |
| 195 |
&metadata->header.version, |
| 196 |
NULL, |
| 197 |
&metadata->header.serial); |
| 198 |
|
| 199 |
if (metadata->header.type == DBUS_MESSAGE_TYPE_INVALID) |
| 200 |
return MESSAGE_E_INVALID_HEADER; |
| 201 |
if (metadata->header.version != 1) |
| 202 |
return MESSAGE_E_INVALID_HEADER; |
| 203 |
if (!metadata->header.serial) |
| 204 |
return MESSAGE_E_INVALID_HEADER; |
| 205 |
|
| 206 |
/* |
| 207 |
* Validate header fields one-by-one. We follow exactly what |
| 208 |
* dbus-daemon(1) does: |
| 209 |
* - Unknown fields are ignored |
| 210 |
* - Duplicates are rejected (except if they are unknown) |
| 211 |
* - Types must match expected types |
| 212 |
* |
| 213 |
* Additionally, each header field has some restrictions on its own |
| 214 |
* validity. |
| 215 |
*/ |
| 216 |
|
| 217 |
while (c_dvar_more(&v)) { |
| 218 |
c_dvar_read(&v, "(y", &field); |
| 219 |
|
| 220 |
if (field >= _DBUS_MESSAGE_FIELD_N) { |
| 221 |
c_dvar_skip(&v, "*)"); |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
if (metadata->fields.available & (1U << field)) |
| 226 |
return MESSAGE_E_INVALID_HEADER; |
| 227 |
|
| 228 |
metadata->fields.available |= 1U << field; |
| 229 |
|
| 230 |
switch (field) { |
| 231 |
case DBUS_MESSAGE_FIELD_INVALID: |
| 232 |
return MESSAGE_E_INVALID_HEADER; |
| 233 |
|
| 234 |
case DBUS_MESSAGE_FIELD_PATH: |
| 235 |
c_dvar_read(&v, "<o>)", c_dvar_type_o, &metadata->fields.path); |
| 236 |
|
| 237 |
if (!strcmp(metadata->fields.path, "/org/freedesktop/DBus/Local")) |
| 238 |
return MESSAGE_E_INVALID_HEADER; |
| 239 |
|
| 240 |
break; |
| 241 |
|
| 242 |
case DBUS_MESSAGE_FIELD_INTERFACE: |
| 243 |
c_dvar_read(&v, "<s>)", c_dvar_type_s, &metadata->fields.interface); |
| 244 |
|
| 245 |
if (!strcmp(metadata->fields.interface, "org.freedesktop.DBus.Local")) |
| 246 |
return MESSAGE_E_INVALID_HEADER; |
| 247 |
|
| 248 |
if (!dbus_validate_interface(metadata->fields.interface, strlen(metadata->fields.interface))) |
| 249 |
return MESSAGE_E_INVALID_HEADER; |
| 250 |
|
| 251 |
break; |
| 252 |
|
| 253 |
case DBUS_MESSAGE_FIELD_MEMBER: |
| 254 |
c_dvar_read(&v, "<s>)", c_dvar_type_s, &metadata->fields.member); |
| 255 |
|
| 256 |
if (!dbus_validate_member(metadata->fields.member, strlen(metadata->fields.member))) |
| 257 |
return MESSAGE_E_INVALID_HEADER; |
| 258 |
|
| 259 |
break; |
| 260 |
|
| 261 |
case DBUS_MESSAGE_FIELD_ERROR_NAME: |
| 262 |
c_dvar_read(&v, "<s>)", c_dvar_type_s, &metadata->fields.error_name); |
| 263 |
|
| 264 |
if (!dbus_validate_error_name(metadata->fields.error_name, strlen(metadata->fields.error_name))) |
| 265 |
return MESSAGE_E_INVALID_HEADER; |
| 266 |
|
| 267 |
break; |
| 268 |
|
| 269 |
case DBUS_MESSAGE_FIELD_REPLY_SERIAL: |
| 270 |
c_dvar_read(&v, "<u>)", c_dvar_type_u, &metadata->fields.reply_serial); |
| 271 |
|
| 272 |
if (!metadata->fields.reply_serial) |
| 273 |
return MESSAGE_E_INVALID_HEADER; |
| 274 |
|
| 275 |
break; |
| 276 |
|
| 277 |
case DBUS_MESSAGE_FIELD_DESTINATION: |
| 278 |
c_dvar_read(&v, "<s>)", c_dvar_type_s, &metadata->fields.destination); |
| 279 |
|
| 280 |
if (!dbus_validate_name(metadata->fields.destination, strlen(metadata->fields.destination))) |
| 281 |
return MESSAGE_E_INVALID_HEADER; |
| 282 |
|
| 283 |
break; |
| 284 |
|
| 285 |
case DBUS_MESSAGE_FIELD_SENDER: |
| 286 |
c_dvar_read(&v, "<s>)", c_dvar_type_s, &metadata->fields.sender); |
| 287 |
|
| 288 |
if (!dbus_validate_name(metadata->fields.sender, strlen(metadata->fields.sender))) |
| 289 |
return MESSAGE_E_INVALID_HEADER; |
| 290 |
|
| 291 |
/* cache sender in case it needs to be stitched out */ |
| 292 |
message->original_sender = (void *)metadata->fields.sender; |
| 293 |
break; |
| 294 |
|
| 295 |
case DBUS_MESSAGE_FIELD_SIGNATURE: |
| 296 |
c_dvar_read(&v, "<g>)", c_dvar_type_g, &metadata->fields.signature); |
| 297 |
break; |
| 298 |
|
| 299 |
case DBUS_MESSAGE_FIELD_UNIX_FDS: |
| 300 |
c_dvar_read(&v, "<u>)", c_dvar_type_u, &metadata->fields.unix_fds); |
| 301 |
|
| 302 |
if (metadata->fields.unix_fds > fdlist_count(message->fds)) |
| 303 |
return MESSAGE_E_MISSING_FDS; |
| 304 |
|
| 305 |
break; |
| 306 |
|
| 307 |
default: |
| 308 |
return error_origin(-ENOTRECOVERABLE); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* Check mandatory fields. That is, depending on the message types, all |
| 314 |
* mandatory fields must be present. |
| 315 |
*/ |
| 316 |
|
| 317 |
switch (metadata->header.type) { |
| 318 |
case DBUS_MESSAGE_TYPE_METHOD_CALL: |
| 319 |
mask = (1U << DBUS_MESSAGE_FIELD_PATH) | |
| 320 |
(1U << DBUS_MESSAGE_FIELD_MEMBER); |
| 321 |
break; |
| 322 |
case DBUS_MESSAGE_TYPE_METHOD_RETURN: |
| 323 |
mask = (1U << DBUS_MESSAGE_FIELD_REPLY_SERIAL); |
| 324 |
break; |
| 325 |
case DBUS_MESSAGE_TYPE_ERROR: |
| 326 |
mask = (1U << DBUS_MESSAGE_FIELD_ERROR_NAME) | |
| 327 |
(1U << DBUS_MESSAGE_FIELD_REPLY_SERIAL); |
| 328 |
break; |
| 329 |
case DBUS_MESSAGE_TYPE_SIGNAL: |
| 330 |
mask = (1U << DBUS_MESSAGE_FIELD_PATH) | |
| 331 |
(1U << DBUS_MESSAGE_FIELD_INTERFACE) | |
| 332 |
(1U << DBUS_MESSAGE_FIELD_MEMBER); |
| 333 |
break; |
| 334 |
default: |
| 335 |
mask = 0; |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
if ((metadata->fields.available & mask) != mask) |
| 340 |
return MESSAGE_E_INVALID_HEADER; |
| 341 |
|
| 342 |
/* |
| 343 |
* Fix up the signature. The DBus spec states that missing signatures |
| 344 |
* should be treated as empty. |
| 345 |
*/ |
| 346 |
|
| 347 |
metadata->fields.signature = metadata->fields.signature ?: ""; |
| 348 |
|
| 349 |
/* |
| 350 |
* Finish the variant parser. If anything went wobbly in between, we |
| 351 |
* will be told here. |
| 352 |
*/ |
| 353 |
|
| 354 |
c_dvar_read(&v, "])"); |
| 355 |
|
| 356 |
r = c_dvar_end_read(&v); |
| 357 |
if (r > 0) |
| 358 |
return MESSAGE_E_INVALID_HEADER; |
| 359 |
else if (r) |
| 360 |
return error_fold(r); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
static int message_parse_body(Message *message, MessageMetadata *metadata) { |
| 366 |
_c_cleanup_(c_dvar_deinit) CDVar v = C_DVAR_INIT; |
| 367 |
const char *signature = metadata->fields.signature; |
| 368 |
size_t i, n_signature, n_types; |
| 369 |
CDVarType *t, *types; |
| 370 |
int r; |
| 371 |
|
| 372 |
/* |
| 373 |
* Parse body-signature into CDVarType array. We use a single array |
| 374 |
* with all the argument-types concatenated. |
| 375 |
*/ |
| 376 |
|
| 377 |
n_signature = strlen(signature); |
| 378 |
c_assert(n_signature < 256); |
| 379 |
types = alloca(n_signature * sizeof(CDVarType)); |
| 380 |
n_types = 0; |
| 381 |
|
| 382 |
for (i = 0; i < n_signature; i += types[i].length) { |
| 383 |
t = types + i; |
| 384 |
r = c_dvar_type_new_from_signature(&t, signature + i, n_signature - i); |
| 385 |
if (r) |
| 386 |
return r < 0 ? error_origin(r) : MESSAGE_E_INVALID_HEADER; |
| 387 |
|
| 388 |
++n_types; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* Now that we know the argument types, use c_dvar_skip() to verify |
| 393 |
* them. While at it, cache all the string/path arguments, so the match |
| 394 |
* rule processing can access them directly. |
| 395 |
*/ |
| 396 |
|
| 397 |
c_dvar_begin_read(&v, message->big_endian, types, n_types, message->body, message->n_body); |
| 398 |
|
| 399 |
for (i = 0, t = types; i < n_types; ++i, t += t->length) { |
| 400 |
switch (t->element) { |
| 401 |
case 's': |
| 402 |
case 'o': |
| 403 |
if (i < C_ARRAY_SIZE(metadata->args)) { |
| 404 |
metadata->args[i].element = t->element; |
| 405 |
c_dvar_read(&v, (char[2]){ t->element, 0 }, &metadata->args[i].value); |
| 406 |
metadata->n_args = i + 1; |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
/* fallthrough */ |
| 411 |
default: |
| 412 |
c_dvar_skip(&v, "*"); |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
r = c_dvar_end_read(&v); |
| 418 |
if (r) |
| 419 |
return r < 0 ? error_origin(r) : MESSAGE_E_INVALID_BODY; |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* message_parse_metadata() - parse message metadata |
| 426 |
* @message: message to operate on |
| 427 |
* |
| 428 |
* This parses the message, verifies its complience to the spec, and caches its metadata. If |
| 429 |
* the message contains more FDs than expected, the excess ones are dropped, otherwise the |
| 430 |
* message object is not altered. |
| 431 |
* |
| 432 |
* This method is idempotent. |
| 433 |
* |
| 434 |
* Return: 0 on success, |
| 435 |
* MESSAGE_E_MISSING_FDS if the message contains fewer FDs than declared in the metadata, |
| 436 |
* MESSAGE_E_INVALID_HEADER if the header violates the spec in other ways, |
| 437 |
* MESSAGE_E_INVALID_BODY if the body could not be parsed. |
| 438 |
*/ |
| 439 |
int message_parse_metadata(Message *message) { |
| 440 |
void *p; |
| 441 |
int r; |
| 442 |
|
| 443 |
if (message->parsed) |
| 444 |
return 0; |
| 445 |
|
| 446 |
/* |
| 447 |
* As first step, parse the static header and the dynamic header |
| 448 |
* fields. Any error there is fatal. |
| 449 |
*/ |
| 450 |
r = message_parse_header(message, &message->metadata); |
| 451 |
if (r) |
| 452 |
return error_trace(r); |
| 453 |
|
| 454 |
/* |
| 455 |
* Validate the padding between the header and body. Those must be 0! |
| 456 |
* We usually wouldn't care but must be compatible to dbus-daemon(1), |
| 457 |
* so lets verify them. |
| 458 |
*/ |
| 459 |
for (p = (void *)message->header + message->n_header; p < message->body; ++p) |
| 460 |
if (*(const uint8_t *)p) |
| 461 |
return MESSAGE_E_INVALID_HEADER; |
| 462 |
|
| 463 |
/* |
| 464 |
* Now that the header is validated, we read through the message body. |
| 465 |
* Again, this is required for compatibility with dbus-daemon(1), but |
| 466 |
* also to fetch the arguments for match-filters used by broadcasts. |
| 467 |
*/ |
| 468 |
r = message_parse_body(message, &message->metadata); |
| 469 |
if (r) |
| 470 |
return error_trace(r); |
| 471 |
|
| 472 |
/* |
| 473 |
* dbus-daemon(1) only ever fetches the correct number of FDs from its |
| 474 |
* stream. This violates the D-Bus specification, which requires FDs to |
| 475 |
* be sent together with the message, and in a single hunk. Therefore, |
| 476 |
* we try to stick to dbus-daemon(1) behavior as close as possible, by |
| 477 |
* rejecting if the requested count exceeds the passed count. However, |
| 478 |
* we always discard any remaining FDs silently. |
| 479 |
*/ |
| 480 |
if (message->fds) |
| 481 |
fdlist_truncate(message->fds, message->metadata.fields.unix_fds); |
| 482 |
|
| 483 |
message->parsed = true; |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* message_stitch_sender() - stitch in new sender field |
| 489 |
* @message: message to operate on |
| 490 |
* @sender_id: sender id to stitch in |
| 491 |
* |
| 492 |
* When the broker forwards messages, it needs to fill in the sender-field |
| 493 |
* reliably. Unfortunately, this requires modifying the fields-array of the |
| 494 |
* D-Bus header. Since we do not want to re-write the entire array, we allow |
| 495 |
* some stitching magic here to happen. |
| 496 |
* |
| 497 |
* This means, we use some nice properties of tuple-arrays in the D-Bus |
| 498 |
* marshalling (namely, they're 8-byte aligned, thus statically discoverable |
| 499 |
* when we know the offset), and simply cut out the existing sender field and |
| 500 |
* append a new one. |
| 501 |
* |
| 502 |
* This function must not be called more than once on any message (it will |
| 503 |
* throw a fatal error). Furthermore, this will cut the message in parts, such |
| 504 |
* that it is no longer readable linearly. However, none of the fields are |
| 505 |
* relocated nor overwritten. That is, any cached pointer stays valid, though |
| 506 |
* maybe no longer part of the actual message. |
| 507 |
*/ |
| 508 |
void message_stitch_sender(Message *message, uint64_t sender_id) { |
| 509 |
size_t n, n_stitch, n_field, n_sender; |
| 510 |
const char *sender; |
| 511 |
void *end, *field; |
| 512 |
|
| 513 |
/* |
| 514 |
* Must not be called more than once. We reserve the 2 iovecs between |
| 515 |
* the original header and body to stitch the sender field. The caller |
| 516 |
* must have parsed the metadata before. |
| 517 |
*/ |
| 518 |
c_assert(message->parsed); |
| 519 |
c_assert(!message->vecs[1].iov_base && !message->vecs[1].iov_len); |
| 520 |
c_assert(!message->vecs[2].iov_base && !message->vecs[2].iov_len); |
| 521 |
|
| 522 |
/* |
| 523 |
* Convert the sender id to a unique name. This should never fail on |
| 524 |
* a valid sender id. |
| 525 |
*/ |
| 526 |
sender = address_to_string(&(Address)ADDRESS_INIT_ID(sender_id)); |
| 527 |
message->metadata.sender_id = sender_id; |
| 528 |
|
| 529 |
/* |
| 530 |
* Calculate string, field, and buffer lengths. We need to possibly cut |
| 531 |
* out a `(yv)' and insert another one at the end. See the D-Bus |
| 532 |
* marshalling for details, but shortly this means: |
| 533 |
* |
| 534 |
* - Tuples are always 8-byte aligned. Hence, we can reliably |
| 535 |
* calculate field offsets. |
| 536 |
* |
| 537 |
* - A string-field needs `1 + 3 + 4 + n + 1' bytes: |
| 538 |
* |
| 539 |
* - length of 'y': 1 |
| 540 |
* - length of 'v': 3 + 4 + n + 1 |
| 541 |
* - type 'g' needs: |
| 542 |
* - size field byte: 1 |
| 543 |
* - type string 's': 1 |
| 544 |
* - zero termination: 1 |
| 545 |
* - sender string needs: |
| 546 |
* - alignment to 4: 0 |
| 547 |
* - size field int: 4 |
| 548 |
* - sender string: n |
| 549 |
* - zero termination: 1 |
| 550 |
*/ |
| 551 |
n_sender = strlen(sender); |
| 552 |
n_field = 1 + 3 + 4 + n_sender + 1; |
| 553 |
n_stitch = c_align_to(n_field, 8); |
| 554 |
|
| 555 |
/* |
| 556 |
* The patch buffer is pre-allocated. Verify its size is sufficient to |
| 557 |
* hold the stitched sender. |
| 558 |
*/ |
| 559 |
{ |
| 560 |
static_assert(1 + 3 + 4 + ADDRESS_ID_STRING_MAX + 1 <= sizeof(message->patch), |
| 561 |
"Message patch buffer has insufficient size"); |
| 562 |
static_assert(alignof(message->patch) >= 8, |
| 563 |
"Message patch buffer has insufficient alignment"); |
| 564 |
c_assert(n_stitch <= sizeof(message->patch)); |
| 565 |
c_assert(n_sender <= ADDRESS_ID_STRING_MAX); |
| 566 |
} |
| 567 |
|
| 568 |
if (message->original_sender) { |
| 569 |
/* |
| 570 |
* If @message already has a sender field, we need to remove it |
| 571 |
* first, so we can append the correct sender. The message |
| 572 |
* parser cached the start of a possible sender field as |
| 573 |
* @message->original_sender (pointing to the start of the |
| 574 |
* sender string!). Hence, calculate the offset to its |
| 575 |
* surrounding field and cut it out. |
| 576 |
* See above for size-calculations of `(yv)' fields. |
| 577 |
*/ |
| 578 |
n = strlen(message->original_sender); |
| 579 |
end = (void *)message->header + c_align_to(message->n_header, 8); |
| 580 |
field = message->original_sender - (1 + 3 + 4); |
| 581 |
|
| 582 |
c_assert(message->original_sender >= (void *)message->header); |
| 583 |
c_assert(message->original_sender + n + 1 <= end); |
| 584 |
|
| 585 |
/* fold remaining fields into following vector */ |
| 586 |
message->vecs[1].iov_base = field + c_align_to(1 + 3 + 4 + n + 1, 8); |
| 587 |
message->vecs[1].iov_len = message->vecs[0].iov_len; |
| 588 |
message->vecs[1].iov_len -= message->vecs[1].iov_base - message->vecs[0].iov_base; |
| 589 |
|
| 590 |
/* cut field from previous vector */ |
| 591 |
message->vecs[0].iov_len = field - message->vecs[0].iov_base; |
| 592 |
|
| 593 |
/* |
| 594 |
* @message->n_header as well as @message->header->n_fields are |
| 595 |
* screwed here, but fixed up below. |
| 596 |
* |
| 597 |
* Note that we cannot fix them here, since we can only |
| 598 |
* calculate them if we actually append data. Otherwise, we |
| 599 |
* cannot know the length of the last field, and as such cannot |
| 600 |
* subtract the trailing padding. |
| 601 |
*/ |
| 602 |
} |
| 603 |
|
| 604 |
/* |
| 605 |
* Now that any possible sender field was cut out, we can append the |
| 606 |
* new sender field at the end. The 3rd iovec is reserved for that |
| 607 |
* purpose. |
| 608 |
*/ |
| 609 |
|
| 610 |
message->vecs[2].iov_base = message->patch; |
| 611 |
message->vecs[2].iov_len = n_stitch; |
| 612 |
|
| 613 |
/* fill in `(yv)' with sender and padding */ |
| 614 |
message->patch[0] = DBUS_MESSAGE_FIELD_SENDER; |
| 615 |
message->patch[1] = 1; |
| 616 |
message->patch[2] = 's'; |
| 617 |
message->patch[3] = 0; |
| 618 |
if (message->big_endian) |
| 619 |
c_memcpy(message->patch + 4, (uint32_t[1]){ htobe32(n_sender) }, sizeof(uint32_t)); |
| 620 |
else |
| 621 |
c_memcpy(message->patch + 4, (uint32_t[1]){ htole32(n_sender) }, sizeof(uint32_t)); |
| 622 |
c_memcpy(message->patch + 8, sender, n_sender + 1); |
| 623 |
c_memset(message->patch + 8 + n_sender + 1, 0, n_stitch - n_field); |
| 624 |
|
| 625 |
/* |
| 626 |
* After we cut the previous sender field and inserted the new, adjust |
| 627 |
* all the size-counters in the message again. |
| 628 |
*/ |
| 629 |
|
| 630 |
message->n_header = message->vecs[0].iov_len + |
| 631 |
message->vecs[1].iov_len + |
| 632 |
n_field; |
| 633 |
message->n_data = c_align_to(message->n_header, 8) + message->n_body; |
| 634 |
|
| 635 |
if (message->big_endian) |
| 636 |
message->header->n_fields = htobe32(message->n_header - sizeof(*message->header)); |
| 637 |
else |
| 638 |
message->header->n_fields = htole32(message->n_header - sizeof(*message->header)); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* message_log_append() - append message metadata to the log |
| 643 |
* @message: message to operate on |
| 644 |
* @log: log to append to |
| 645 |
* |
| 646 |
* This appends the metadata of @message to the next log message written |
| 647 |
* to @log. |
| 648 |
*/ |
| 649 |
void message_log_append(Message *message, Log *log) { |
| 650 |
log_appendf(log, |
| 651 |
"DBUS_BROKER_MESSAGE_DESTINATION=%s\n" |
| 652 |
"DBUS_BROKER_MESSAGE_SERIAL=%"PRIu32"\n" |
| 653 |
"DBUS_BROKER_MESSAGE_SIGNATURE=%s\n" |
| 654 |
"DBUS_BROKER_MESSAGE_UNIX_FDS=%"PRIu32"\n", |
| 655 |
message->metadata.fields.destination ?: "<broadcast>", |
| 656 |
message->metadata.header.serial, |
| 657 |
message->metadata.fields.signature ?: "<missing>", |
| 658 |
message->metadata.fields.unix_fds); |
| 659 |
|
| 660 |
switch (message->metadata.header.type) { |
| 661 |
case DBUS_MESSAGE_TYPE_METHOD_CALL: |
| 662 |
log_appendf(log, |
| 663 |
"DBUS_BROKER_MESSAGE_TYPE=method_call\n" |
| 664 |
"DBUS_BROKER_MESSAGE_PATH=%s\n" |
| 665 |
"DBUS_BROKER_MESSAGE_INTERFACE=%s\n" |
| 666 |
"DBUS_BROKER_MESSAGE_MEMBER=%s\n", |
| 667 |
message->metadata.fields.path ?: "<missing>", |
| 668 |
message->metadata.fields.interface ?: "<missing>", |
| 669 |
message->metadata.fields.member ?: "<missing>"); |
| 670 |
break; |
| 671 |
case DBUS_MESSAGE_TYPE_SIGNAL: |
| 672 |
log_appendf(log, |
| 673 |
"DBUS_BROKER_MESSAGE_TYPE=signal\n" |
| 674 |
"DBUS_BROKER_MESSAGE_PATH=%s\n" |
| 675 |
"DBUS_BROKER_MESSAGE_INTERFACE=%s\n" |
| 676 |
"DBUS_BROKER_MESSAGE_MEMBER=%s\n", |
| 677 |
message->metadata.fields.path ?: "<missing>", |
| 678 |
message->metadata.fields.interface ?: "<missing>", |
| 679 |
message->metadata.fields.member ?: "<missing>"); |
| 680 |
break; |
| 681 |
case DBUS_MESSAGE_TYPE_METHOD_RETURN: |
| 682 |
log_appendf(log, |
| 683 |
"DBUS_BROKER_MESSAGE_TYPE=method_return\n" |
| 684 |
"MESSAGE_REPLY_SERIAL=%"PRIu32"\n", |
| 685 |
message->metadata.fields.reply_serial); |
| 686 |
break; |
| 687 |
case DBUS_MESSAGE_TYPE_ERROR: |
| 688 |
log_appendf(log, |
| 689 |
"DBUS_BROKER_MESSAGE_TYPE=method_return\n" |
| 690 |
"DBUS_BROKER_MESSAGE_ERROR_NAME=%s\n" |
| 691 |
"DBUS_BROKER_MESSAGE_REPLY_SERIAL=%"PRIu32"\n", |
| 692 |
message->metadata.fields.error_name, |
| 693 |
message->metadata.fields.reply_serial); |
| 694 |
break; |
| 695 |
default: |
| 696 |
log_appendf(log, "DBUS_BROKER_MESSAGE_TYPE=%u\n", message->metadata.header.type); |
| 697 |
} |
| 698 |
} |