| 1 |
/* |
| 2 |
* D-Bus Input/Output Queues |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-stdaux.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include "dbus/queue.h" |
| 8 |
#include "util/fdlist.h" |
| 9 |
#include "util/error.h" |
| 10 |
|
| 11 |
/** |
| 12 |
* iqueue_init() - XXX |
| 13 |
*/ |
| 14 |
void iqueue_init(IQueue *iq, User *user) { |
| 15 |
*iq = (IQueue)IQUEUE_NULL(*iq); |
| 16 |
iq->user = user_ref(user); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* iqueue_deinit() - XXX |
| 21 |
*/ |
| 22 |
void iqueue_deinit(IQueue *iq) { |
| 23 |
iqueue_flush(iq); |
| 24 |
|
| 25 |
c_assert(!iq->fds); |
| 26 |
c_assert(!iq->pending.data); |
| 27 |
c_assert(!iq->pending.fds); |
| 28 |
|
| 29 |
if (iq->data != iq->buffer) { |
| 30 |
free(iq->data); |
| 31 |
iq->data = iq->buffer; |
| 32 |
iq->data_size = sizeof(iq->buffer); |
| 33 |
} |
| 34 |
|
| 35 |
user_charge_deinit(&iq->pending.charge_fds); |
| 36 |
user_charge_deinit(&iq->pending.charge_data); |
| 37 |
user_charge_deinit(&iq->charge_fds); |
| 38 |
user_charge_deinit(&iq->charge_data); |
| 39 |
|
| 40 |
iq->user = user_unref(iq->user); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* iqueue_flush() - XXX |
| 45 |
*/ |
| 46 |
void iqueue_flush(IQueue *iq) { |
| 47 |
iq->data_start = 0; |
| 48 |
iq->data_end = 0; |
| 49 |
iq->data_cursor = 0; |
| 50 |
iq->fds = fdlist_free(iq->fds); |
| 51 |
|
| 52 |
iq->pending.data = NULL; |
| 53 |
iq->pending.n_data = 0; |
| 54 |
iq->pending.n_copied = 0; |
| 55 |
iq->pending.fds = fdlist_free(iq->pending.fds); |
| 56 |
|
| 57 |
user_charge_deinit(&iq->charge_fds); |
| 58 |
user_charge_deinit(&iq->pending.charge_fds); |
| 59 |
user_charge_deinit(&iq->pending.charge_data); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* iqueue_set_pending() - XXX |
| 64 |
*/ |
| 65 |
int iqueue_set_target(IQueue *iq, void *data, size_t n_data) { |
| 66 |
int r; |
| 67 |
|
| 68 |
c_assert(data); |
| 69 |
c_assert(!iq->pending.data); |
| 70 |
|
| 71 |
/* |
| 72 |
* This temporarily charges the pending buffer on @iq->user, so a |
| 73 |
* single user cannot pin arbitrary memory as pending input buffers |
| 74 |
* without every completing a message. |
| 75 |
* Note that this charge only applies to the *pinned* buffer. As long |
| 76 |
* as the message is not pinned, it is not charged on anyone. We |
| 77 |
* consider each dispatch thread to be capable of holding at least a |
| 78 |
* single message at all times, so no need to charge them unless we pin |
| 79 |
* them on a peer. |
| 80 |
*/ |
| 81 |
r = user_charge(iq->user, |
| 82 |
&iq->pending.charge_data, |
| 83 |
NULL, |
| 84 |
USER_SLOT_BYTES, |
| 85 |
n_data); |
| 86 |
if (r) |
| 87 |
return (r == USER_E_QUOTA) ? IQUEUE_E_QUOTA : error_fold(r); |
| 88 |
|
| 89 |
iq->pending.data = data; |
| 90 |
iq->pending.n_data = n_data; |
| 91 |
iq->pending.n_copied = 0; |
| 92 |
/* FDs stay untouched and are merged into the next blob */ |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* iqueue_get_cursor() - XXX |
| 99 |
*/ |
| 100 |
int iqueue_get_cursor(IQueue *iq, |
| 101 |
void **bufferp, |
| 102 |
size_t **fromp, |
| 103 |
size_t *top, |
| 104 |
FDList ***fdsp, |
| 105 |
UserCharge **charge_fdsp) { |
| 106 |
void *p; |
| 107 |
int r; |
| 108 |
|
| 109 |
/* |
| 110 |
* Always shift the input buffer. In case of the line-parser this |
| 111 |
* should never happen in normal operation: the only way to leave |
| 112 |
* behind a partial line is by filling the whole buffer, in that case |
| 113 |
* at most IQUEUE_RECV_MAX bytes need to be moved. And for the |
| 114 |
* message-parser, there can be at most one message header left |
| 115 |
* behind (16 bytes). |
| 116 |
* |
| 117 |
* Long story short: We never shift more than 16 bytes in a fast-path, |
| 118 |
* or you are doing something wrong. |
| 119 |
*/ |
| 120 |
memmove(iq->data, |
| 121 |
iq->data + iq->data_start, |
| 122 |
iq->data_end - iq->data_start); |
| 123 |
iq->data_cursor -= iq->data_start; |
| 124 |
iq->data_end -= iq->data_start; |
| 125 |
iq->data_start = 0; |
| 126 |
|
| 127 |
/* |
| 128 |
* Never ever read data if we did not finish parsing our input buffer! |
| 129 |
* |
| 130 |
* This is crucial! The kernel provides auxiliary data that is attached |
| 131 |
* to specific SKBs, and as such part of the stream. We must never |
| 132 |
* merge them across D-Bus message boundaries (see the FD handling on |
| 133 |
* recvmsg(2) for details). Hence, you must always dispatch your entire |
| 134 |
* input-queue before reading more data. |
| 135 |
*/ |
| 136 |
if (iq->data_cursor < iq->data_end) |
| 137 |
return IQUEUE_E_PENDING; |
| 138 |
|
| 139 |
/* |
| 140 |
* In case our input buffer is full, we need to resize it. This can |
| 141 |
* only happen for the line-reader, since otherwise we always read into |
| 142 |
* separate buffers. |
| 143 |
* The line-reader, however, parses the entire line into the input |
| 144 |
* buffer. Hence, in case the normal buffer size is exceeded, we |
| 145 |
* re-allocate to its maximum *ONCE*. |
| 146 |
* |
| 147 |
* Once we finished reading lines *AND* we processed all the data in |
| 148 |
* the input buffer, we can safely de-allocate the buffer and fall back |
| 149 |
* to the input buffer again. |
| 150 |
*/ |
| 151 |
if (_c_unlikely_(iq->data_size <= iq->data_end)) { |
| 152 |
if (iq->data_size >= IQUEUE_LINE_MAX) |
| 153 |
return IQUEUE_E_VIOLATION; |
| 154 |
|
| 155 |
p = malloc(IQUEUE_LINE_MAX); |
| 156 |
if (!p) |
| 157 |
return error_origin(-ENOMEM); |
| 158 |
|
| 159 |
r = user_charge(iq->user, |
| 160 |
&iq->charge_data, |
| 161 |
NULL, |
| 162 |
USER_SLOT_BYTES, |
| 163 |
IQUEUE_LINE_MAX); |
| 164 |
if (r) { |
| 165 |
free(p); |
| 166 |
return (r == USER_E_QUOTA) ? IQUEUE_E_QUOTA : error_fold(r); |
| 167 |
} |
| 168 |
|
| 169 |
/* we always shift so data_start must be 0 */ |
| 170 |
c_assert(!iq->data_start); |
| 171 |
c_assert(iq->data == iq->buffer); |
| 172 |
|
| 173 |
c_memcpy(p, iq->data, iq->data_end); |
| 174 |
iq->data = p; |
| 175 |
iq->data_size = IQUEUE_LINE_MAX; |
| 176 |
} else if (_c_unlikely_(iq->data != iq->buffer && iq->pending.data)) { |
| 177 |
c_assert(!iq->data_start); |
| 178 |
c_assert(iq->data_end <= sizeof(iq->buffer)); |
| 179 |
|
| 180 |
c_memcpy(iq->buffer, iq->data, iq->data_end); |
| 181 |
free(iq->data); |
| 182 |
user_charge_deinit(&iq->charge_data); |
| 183 |
iq->data = iq->buffer; |
| 184 |
iq->data_size = sizeof(iq->buffer); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* If there is a pending buffer, we try to read directly into it, |
| 189 |
* skipping the separate input buffer. However, we only do this if the |
| 190 |
* chunk of data to fetch is bigger than (or equal to) our input |
| 191 |
* buffer. This avoids fetching small amounts of data from the kernel, |
| 192 |
* while we could fetch big chunks of consecutive small messages. |
| 193 |
* |
| 194 |
* In other words: If a message is considerably big, we will read it |
| 195 |
* directly into its message object (single copy). In |
| 196 |
* all other cases, we first read from the kernel into |
| 197 |
* our input buffer (possibly fetching many messages at |
| 198 |
* once), and then copy over the actual data into the |
| 199 |
* message objects (double copy). |
| 200 |
* This is a trade-off between double-copy and reducing |
| 201 |
* the number of calls to recvmsg(2). |
| 202 |
*/ |
| 203 |
if (iq->pending.n_data - iq->pending.n_copied >= iq->data_size - iq->data_end) { |
| 204 |
*bufferp = iq->pending.data; |
| 205 |
*fromp = &iq->pending.n_copied; |
| 206 |
*top = iq->pending.n_data; |
| 207 |
*fdsp = &iq->pending.fds; |
| 208 |
*charge_fdsp = &iq->pending.charge_fds; |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* Read more data into the input buffer, and store the file-descriptors |
| 214 |
* in the buffer as well. |
| 215 |
* |
| 216 |
* Only ever read in IQUEUE_RECV_MAX in order to limit the number of |
| 217 |
* incoming messages we may have in the buffer at once. |
| 218 |
* |
| 219 |
* Note that the kernel always breaks recvmsg() calls after an SKB with |
| 220 |
* file-descriptor payload. Hence, this could be improvded with |
| 221 |
* recvmmsg() so we get multiple messages at all cost. However, FD |
| 222 |
* passing is no fast-path and should never be, so there is little |
| 223 |
* reason to resort to recvmmsg() (which would be non-trivial, anyway, |
| 224 |
* since we would need multiple input buffers). |
| 225 |
*/ |
| 226 |
*bufferp = iq->data; |
| 227 |
*fromp = &iq->data_end; |
| 228 |
*top = (iq->data_size - iq->data_end) > IQUEUE_RECV_MAX ? iq->data_end + IQUEUE_RECV_MAX : iq->data_size; |
| 229 |
*fdsp = &iq->fds; |
| 230 |
*charge_fdsp = &iq->charge_fds; |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* iqueue_pop_line() - XXX |
| 236 |
*/ |
| 237 |
int iqueue_pop_line(IQueue *iq, const char **linep, size_t *np) { |
| 238 |
char *line; |
| 239 |
size_t n; |
| 240 |
|
| 241 |
c_assert(!iq->pending.data); |
| 242 |
|
| 243 |
/* |
| 244 |
* Advance our cursor byte by byte and look for an end-of-line. We |
| 245 |
* remember the cursor position, so no byte is ever parsed twice. |
| 246 |
*/ |
| 247 |
for ( ; iq->data_cursor < iq->data_end; ++iq->data_cursor) { |
| 248 |
/* |
| 249 |
* If we are at the end of the input-queue, we must consume |
| 250 |
* any possible FD array that we received alongside it. |
| 251 |
* The kernel always breaks _after_ skbs with FDs, but not |
| 252 |
* before them. Hence, FDs are attached to the LAST byte of our |
| 253 |
* input-queue, rather than the first. |
| 254 |
* |
| 255 |
* During line-handling, we consider receiving FDs a protocl |
| 256 |
* violation, and the DBus spec clearly states that no |
| 257 |
* extension shall pass FDs during authentication. |
| 258 |
*/ |
| 259 |
if (iq->data_cursor + 1 >= iq->data_end) { |
| 260 |
if (_c_unlikely_(fdlist_count(iq->fds) > 0)) |
| 261 |
return IQUEUE_E_VIOLATION; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* If we find an \r\n, return the pointer and length to the |
| 266 |
* caller and cut out the line. |
| 267 |
* We do NOT copy the line. We leave it in the buffer untouched |
| 268 |
* and return a direct pointer into the buffer. The pointer is |
| 269 |
* only valid until the next call into this object. |
| 270 |
* While we replace \r by NUL, this is not meant to be relied |
| 271 |
* upon by the caller. It is a pure safety belt. The caller |
| 272 |
* better not accesses the buffer beyond the returned line |
| 273 |
* length. |
| 274 |
*/ |
| 275 |
if (iq->data_cursor > 0 && |
| 276 |
iq->data[iq->data_cursor] == '\n' && |
| 277 |
iq->data[iq->data_cursor - 1] == '\r') { |
| 278 |
/* remember start and length without \r\n */ |
| 279 |
line = iq->data + iq->data_start; |
| 280 |
n = iq->data_cursor - iq->data_start - 1; |
| 281 |
|
| 282 |
/* advance cursor and cut buffer */ |
| 283 |
iq->data_start = ++iq->data_cursor; |
| 284 |
|
| 285 |
/* replace \r by safety NUL and return to caller */ |
| 286 |
line[n] = 0; |
| 287 |
*linep = (const char *)line; |
| 288 |
*np = n; |
| 289 |
return 0; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return IQUEUE_E_PENDING; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* iqueue_pop_data() - XXX |
| 298 |
*/ |
| 299 |
int iqueue_pop_data(IQueue *iq, FDList **fdsp) { |
| 300 |
size_t n, n_data; |
| 301 |
|
| 302 |
c_assert(iq->pending.data); |
| 303 |
c_assert(iq->pending.n_copied <= iq->pending.n_data); |
| 304 |
|
| 305 |
n_data = iq->data_end - iq->data_start; |
| 306 |
|
| 307 |
/* |
| 308 |
* As long as there is data in our input-queue, and the pending buffer |
| 309 |
* is not fully read, we continously copy over data from the |
| 310 |
* input-queue into the pending buffer. Note that this step might be |
| 311 |
* short-cut by the socket-layer by directly reading data into the |
| 312 |
* pending buffer. |
| 313 |
*/ |
| 314 |
if (n_data > 0) { |
| 315 |
n = c_min(n_data, iq->pending.n_data - iq->pending.n_copied); |
| 316 |
|
| 317 |
c_memcpy(iq->pending.data + iq->pending.n_copied, |
| 318 |
iq->data + iq->data_start, |
| 319 |
n); |
| 320 |
|
| 321 |
n_data -= n; |
| 322 |
iq->data_start += n; |
| 323 |
iq->data_cursor += n; |
| 324 |
iq->pending.n_copied += n; |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* Auxiliary file-descriptors are returned by the kernel together with |
| 329 |
* message data. The kernel breaks the receiption *after* each skbuff |
| 330 |
* that carried FDs. Hence, FDs are always attached to the *last* byte |
| 331 |
* of the data-buffer they were returned with. |
| 332 |
* D-Bus clients are required to send FDs together with the bytes of |
| 333 |
* their message. Hence, they cannot merge multiple messages into a |
| 334 |
* single buffer, if they carry FDs. The worst that can happen is that |
| 335 |
* many non-fd-carrying messages are merged into a single SKB up until |
| 336 |
* (and including) a last message that carries FDs. Since we attribute |
| 337 |
* FDs to the last byte of our received chunks, we will correctly |
| 338 |
* attribute the FDs to the last message. |
| 339 |
* |
| 340 |
* So, whenever we copied over the last byte from the input-queue into |
| 341 |
* a message, this means we also need to copy the FDs. Note that the |
| 342 |
* D-Bus spec does *NOT* allow multiple FD-Sets to be transferred with |
| 343 |
* a single message (all FDs must be transferred in a single shot). It |
| 344 |
* is, thus, a protocol violation if there are multiple FDsets for a |
| 345 |
* single message. |
| 346 |
*/ |
| 347 |
if (_c_unlikely_(!n_data && iq->fds)) { |
| 348 |
if (_c_unlikely_(iq->pending.fds)) |
| 349 |
return IQUEUE_E_VIOLATION; |
| 350 |
|
| 351 |
iq->pending.fds = iq->fds; |
| 352 |
iq->pending.charge_fds = iq->charge_fds; |
| 353 |
iq->fds = NULL; |
| 354 |
iq->charge_fds = (UserCharge)USER_CHARGE_INIT; |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* If the pending message is not finished, yet, let the caller know |
| 359 |
* that we need more data. |
| 360 |
*/ |
| 361 |
if (iq->pending.n_copied < iq->pending.n_data) |
| 362 |
return IQUEUE_E_PENDING; |
| 363 |
|
| 364 |
/* |
| 365 |
* The pending message has been fully received. Deaccount it, since it |
| 366 |
* is now owned by the calling thread and will be charged on whoever |
| 367 |
* pins the message next. Unpin the message from the input-queue and |
| 368 |
* transfer ownership to the caller. |
| 369 |
*/ |
| 370 |
if (fdsp) { |
| 371 |
*fdsp = iq->pending.fds; |
| 372 |
iq->pending.fds = NULL; |
| 373 |
user_charge_deinit(&iq->pending.charge_fds); |
| 374 |
} |
| 375 |
iq->pending.data = NULL; |
| 376 |
iq->pending.n_data = 0; |
| 377 |
iq->pending.n_copied = 0; |
| 378 |
user_charge_deinit(&iq->pending.charge_data); |
| 379 |
return 0; |
| 380 |
} |