From b890466df179aea4d506c160a245affa67c911b4 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Fri, 4 Jul 2014 15:05:51 +0100 Subject: [PATCH] Randomize disconnect --- bus/connection.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bus/connection.c b/bus/connection.c index ea2d155..5545ae1 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -30,6 +30,9 @@ #include "signals.h" #include "expirelist.h" #include "selinux.h" + +#include + #include #include #include @@ -695,23 +698,21 @@ bus_connections_setup_connection (BusConnections *connections, */ bus_connections_expire_incomplete (connections); - /* And we might also disconnect ourselves here, but again it - * only takes effect on return to main loop. - */ if (connections->n_incomplete > bus_context_get_max_incomplete_connections (connections->context)) { - _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n"); - - _dbus_assert (connections->incomplete != NULL); - /* Disconnect the oldest unauthenticated connection. FIXME - * would it be more secure to drop a *random* connection? This - * algorithm seems to mean that if someone can create new - * connections quickly enough, they can keep anyone else from - * completing authentication. But random may or may not really - * help with that, a more elaborate solution might be required. - */ - dbus_connection_close (connections->incomplete->data); + int i; + int r = rand() % (connections->n_incomplete); + DBusList *link = connections->incomplete; + + _dbus_verbose ("Number of incomplete connections exceeds max, dropping a random one\n"); + + for (i = 0 ; i < r ; i++) + { + _dbus_assert (link != NULL); + link = link->next; + } + dbus_connection_close (link->data); } retval = TRUE; -- 1.8.5.3