diff options
author | David Zeuthen <davidz@redhat.com> | 2012-04-12 11:16:48 -0400 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2012-04-12 11:18:07 -0400 |
commit | 870351439769a35344ead435f2490ed933dd8ff4 (patch) | |
tree | 6088d7b654716e4fe71255871b1271f262396bb8 /dbus | |
parent | 6edbb08b0ee52096b54c496ef33df684642a02f8 (diff) | |
download | dbus-870351439769a35344ead435f2490ed933dd8ff4.tar.gz |
Avoid using monotonic time in the DBUS_COOKIE_SHA1 authentication method
When libdbus-1 moved to using monotonic time support for the
DBUS_COOKIE_SHA1 authentication was broken, in particular
interoperability with non-libdbus-1 implementations such as GDBus.
The problem is that if monotonic clocks are available in the OS,
_dbus_get_current_time() will not return the number of seconds since
the Epoch so using it for DBUS_COOKIE_SHA1 will violate the D-Bus
specification. If both peers are using libdbus-1 it's not a problem
since both ends will use the wrong time and thus agree. However, if
the other end is another implementation and following the spec it will
not work.
First, we change _dbus_get_current_time() back so it always returns
time since the Epoch and we then rename it _dbus_get_real_time() to
make this clear. We then introduce _dbus_get_monotonic_time() and
carefully make all current users of _dbus_get_current_time() use it,
if applicable. During this audit, one of the callers,
_dbus_generate_uuid(), was currently using monotonic time but it was
decided to make it use real time instead.
Signed-off-by: David Zeuthen <davidz@redhat.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=48580
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/dbus-connection.c | 4 | ||||
-rw-r--r-- | dbus/dbus-internals.c | 2 | ||||
-rw-r--r-- | dbus/dbus-keyring.c | 6 | ||||
-rw-r--r-- | dbus/dbus-mainloop.c | 8 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-unix.c | 25 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-win.c | 18 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.c | 2 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.h | 7 |
8 files changed, 55 insertions, 17 deletions
diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 8b8310dd..4f2eb6b8 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -2377,7 +2377,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) * below */ timeout = _dbus_pending_call_get_timeout_unlocked (pending); - _dbus_get_current_time (&start_tv_sec, &start_tv_usec); + _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec); if (timeout) { timeout_milliseconds = dbus_timeout_get_interval (timeout); @@ -2434,7 +2434,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) return; } - _dbus_get_current_time (&tv_sec, &tv_usec); + _dbus_get_monotonic_time (&tv_sec, &tv_usec); elapsed_milliseconds = (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000; diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index 95a491f9..8c1dd222 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -595,7 +595,7 @@ _dbus_generate_uuid (DBusGUID *uuid) { long now; - _dbus_get_current_time (&now, NULL); + _dbus_get_real_time (&now, NULL); uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now); diff --git a/dbus/dbus-keyring.c b/dbus/dbus-keyring.c index 35e8d74e..23b9df5a 100644 --- a/dbus/dbus-keyring.c +++ b/dbus/dbus-keyring.c @@ -353,7 +353,7 @@ add_new_key (DBusKey **keys_p, goto out; } - _dbus_get_current_time (×tamp, NULL); + _dbus_get_real_time (×tamp, NULL); keys[n_keys-1].id = id; keys[n_keys-1].creation_time = timestamp; @@ -428,7 +428,7 @@ _dbus_keyring_reload (DBusKeyring *keyring, retval = FALSE; have_lock = FALSE; - _dbus_get_current_time (&now, NULL); + _dbus_get_real_time (&now, NULL); if (add_new) { @@ -908,7 +908,7 @@ find_recent_key (DBusKeyring *keyring) int i; long tv_sec, tv_usec; - _dbus_get_current_time (&tv_sec, &tv_usec); + _dbus_get_real_time (&tv_sec, &tv_usec); i = 0; while (i < keyring->n_keys) diff --git a/dbus/dbus-mainloop.c b/dbus/dbus-mainloop.c index 43159a70..1ff8425c 100644 --- a/dbus/dbus-mainloop.c +++ b/dbus/dbus-mainloop.c @@ -136,8 +136,8 @@ timeout_callback_new (DBusTimeout *timeout, cb->timeout = timeout; cb->function = function; - _dbus_get_current_time (&cb->last_tv_sec, - &cb->last_tv_usec); + _dbus_get_monotonic_time (&cb->last_tv_sec, + &cb->last_tv_usec); cb->callback.refcount = 1; cb->callback.type = CALLBACK_TIMEOUT; cb->callback.data = data; @@ -653,7 +653,7 @@ _dbus_loop_iterate (DBusLoop *loop, unsigned long tv_sec; unsigned long tv_usec; - _dbus_get_current_time (&tv_sec, &tv_usec); + _dbus_get_monotonic_time (&tv_sec, &tv_usec); link = _dbus_list_get_first_link (&loop->callbacks); while (link != NULL) @@ -723,7 +723,7 @@ _dbus_loop_iterate (DBusLoop *loop, unsigned long tv_sec; unsigned long tv_usec; - _dbus_get_current_time (&tv_sec, &tv_usec); + _dbus_get_monotonic_time (&tv_sec, &tv_usec); /* It'd be nice to avoid this O(n) thingy here */ link = _dbus_list_get_first_link (&loop->callbacks); diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 1a8f6fc2..aec03e83 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -2529,8 +2529,8 @@ _dbus_poll (DBusPollFD *fds, * @param tv_usec return location for number of microseconds (thousandths) */ void -_dbus_get_current_time (long *tv_sec, - long *tv_usec) +_dbus_get_monotonic_time (long *tv_sec, + long *tv_usec) { struct timeval t; @@ -2553,6 +2553,27 @@ _dbus_get_current_time (long *tv_sec, } /** + * Get current time, as in gettimeofday(). Never uses the monotonic + * clock. + * + * @param tv_sec return location for number of seconds + * @param tv_usec return location for number of microseconds (thousandths) + */ +void +_dbus_get_real_time (long *tv_sec, + long *tv_usec) +{ + struct timeval t; + + gettimeofday (&t, NULL); + + if (tv_sec) + *tv_sec = t.tv_sec; + if (tv_usec) + *tv_usec = t.tv_usec; +} + +/** * Creates a directory; succeeds if the directory * is created or already existed. * diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index aea704d2..b22a3718 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -1888,8 +1888,8 @@ _dbus_sleep_milliseconds (int milliseconds) * @param tv_usec return location for number of microseconds */ void -_dbus_get_current_time (long *tv_sec, - long *tv_usec) +_dbus_get_real_time (long *tv_sec, + long *tv_usec) { FILETIME ft; dbus_uint64_t time64; @@ -1911,6 +1911,20 @@ _dbus_get_current_time (long *tv_sec, *tv_usec = time64 % 1000000; } +/** + * Get current time, as in gettimeofday(). Use the monotonic clock if ++ * available, to avoid problems when the system time changes. + * + * @param tv_sec return location for number of seconds + * @param tv_usec return location for number of microseconds + */ +void +_dbus_get_monotonic_time (long *tv_sec, + long *tv_usec) +{ + /* no implementation yet, fall back to wall-clock time */ + _dbus_get_real_time (tv_sec, tv_usec); +} /** * signal (SIGPIPE, SIG_IGN); diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 89062764..1b220313 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -805,7 +805,7 @@ _dbus_generate_pseudorandom_bytes_buffer (char *buffer, _dbus_verbose ("Falling back to pseudorandom for %d bytes\n", n_bytes); - _dbus_get_current_time (NULL, &tv_usec); + _dbus_get_monotonic_time (NULL, &tv_usec); srand (tv_usec); i = 0; diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index d4883c1b..a784db27 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -310,8 +310,11 @@ int _dbus_poll (DBusPollFD *fds, void _dbus_sleep_milliseconds (int milliseconds); -void _dbus_get_current_time (long *tv_sec, - long *tv_usec); +void _dbus_get_monotonic_time (long *tv_sec, + long *tv_usec); + +void _dbus_get_real_time (long *tv_sec, + long *tv_usec); /** * directory interface |