Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1693559) sd-bus: deal with cookie overruns #322

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/libsystemd/sd-bus/sd-bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,50 @@ _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
return 0;
}

#define COOKIE_CYCLED (UINT32_C(1) << 31)

static uint64_t cookie_inc(uint64_t cookie) {

/* Stay within the 32bit range, since classic D-Bus can't deal with more */
if (cookie >= UINT32_MAX)
return COOKIE_CYCLED; /* Don't go back to zero, but use the highest bit for checking
* whether we are looping. */

return cookie + 1;
}

static int next_cookie(sd_bus *b) {
uint64_t new_cookie;

assert(b);

new_cookie = cookie_inc(b->cookie);

/* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
* least once, but then do it thorougly. */
if (FLAGS_SET(new_cookie, COOKIE_CYCLED)) {
uint32_t i;

/* Check if the cookie is currently in use. If so, pick the next one */
for (i = 0; i < COOKIE_CYCLED; i++) {
if (!ordered_hashmap_contains(b->reply_callbacks, &new_cookie))
goto good;

new_cookie = cookie_inc(new_cookie);
}

/* Can't fulfill request */
return -EBUSY;
}

good:
b->cookie = new_cookie;
return 0;
}

static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
int r;

assert(b);
assert(m);

Expand All @@ -1510,7 +1553,11 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
if (timeout == 0)
timeout = BUS_DEFAULT_TIMEOUT;

return bus_message_seal(m, ++b->cookie, timeout);
r = next_cookie(b);
if (r < 0)
return r;

return bus_message_seal(m, b->cookie, timeout);
}

static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ do { \

#define SET_FLAG(v, flag, b) \
(v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
#define FLAGS_SET(v, flags) \
((~(v) & (flags)) == 0)

#define IN_SET(x, y, ...) \
({ \
Expand Down