Fix a possible crash when sending the HUP signal.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 20 Oct 2009 20:14:47 +0000 (22:14 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 20 Oct 2009 20:14:47 +0000 (22:14 +0200)
When the HUP signal is sent while some outgoing connections have not been made
yet, or are being retried, a NULL pointer could be dereferenced resulting in
tinc crashing. We fix this by more careful handling of outgoing_ts, and by
deleting all connections that have not been fully activated yet at the HUP
signal is received.

src/event.h
src/net.c
src/net.h
src/net_setup.c
src/net_socket.c

index 345a5f913b18d3aca324644f41b9c8c6d294d9bd..da2e741a6f0b2d7406f985b8a00ade29d7582545 100644 (file)
@@ -27,7 +27,7 @@ extern avl_tree_t *event_tree;
 
 typedef void (*event_handler_t)(void *);
 
-typedef struct {
+typedef struct event {
        time_t time;
        int id;
        event_handler_t handler;
index 31970366d8cfc6a69d08be633d64c685ccbf68bd..59dd39b30c327b8f15f5deb6360148a043deb466 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -431,7 +431,7 @@ int main_loop(void) {
 
                if(sighup) {
                        connection_t *c;
-                       avl_node_t *node;
+                       avl_node_t *node, *next;
                        char *fname;
                        struct stat s;
                        
@@ -447,6 +447,31 @@ int main_loop(void) {
                                return 1;
                        }
 
+                       /* Cancel non-active outgoing connections */
+
+                       for(node = connection_tree->head; node; node = next) {
+                               next = node->next;
+                               c = node->data;
+
+                               c->outgoing = NULL;
+
+                               if(c->status.connecting) {
+                                       terminate_connection(c, false);
+                                       connection_del(c);
+                               }
+                       }
+
+                       /* Wipe list of outgoing connections */
+
+                       for(list_node_t *node = outgoing_list->head; node; node = node->next) {
+                               outgoing_t *outgoing = node->data;
+
+                               if(outgoing->event)
+                                       event_del(outgoing->event);
+                       }
+
+                       list_delete_list(outgoing_list);
+
                        /* Close connections to hosts that have a changed or deleted host config file */
                        
                        for(node = connection_tree->head; node; node = node->next) {
index 31438e52d6179786cf35c394588986a99ff945c6..fe4b54b776e58dd16d180ea1765b54e161ef0580 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -98,6 +98,7 @@ typedef struct outgoing_t {
        struct config_t *cfg;
        struct addrinfo *ai;
        struct addrinfo *aip;
+       struct event *event;
 } outgoing_t;
 
 extern list_t *outgoing_list;
index a08981f96b64679925defcf16e3bdc41bad15532..cbf631f5f211b61ccc545e3676c097a0484689be 100644 (file)
@@ -541,10 +541,17 @@ void close_network_connections(void) {
        for(node = connection_tree->head; node; node = next) {
                next = node->next;
                c = node->data;
-               c->outgoing = false;
+               c->outgoing = NULL;
                terminate_connection(c, false);
        }
 
+       for(list_node_t *node = outgoing_list->head; node; node = node->next) {
+               outgoing_t *outgoing = node->data;
+
+               if(outgoing->event)
+                       event_del(outgoing->event);
+       }
+
        list_delete_list(outgoing_list);
 
        if(myself && myself->connection) {
index 7189025c71e158384e78efb890b8a3c699b8f07e..baf0227a3cfd61c762b9bf0bff4a68d03a7491e3 100644 (file)
@@ -306,18 +306,18 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
 } /* int setup_vpn_in_socket */
 
 void retry_outgoing(outgoing_t *outgoing) {
-       event_t *event;
-
        outgoing->timeout += 5;
 
        if(outgoing->timeout > maxtimeout)
                outgoing->timeout = maxtimeout;
 
-       event = new_event();
-       event->handler = (event_handler_t) setup_outgoing_connection;
-       event->time = now + outgoing->timeout;
-       event->data = outgoing;
-       event_add(event);
+       if(outgoing->event)
+               event_del(outgoing->event);
+       outgoing->event = new_event();
+       outgoing->event->handler = (event_handler_t) setup_outgoing_connection;
+       outgoing->event->time = now + outgoing->timeout;
+       outgoing->event->data = outgoing;
+       event_add(outgoing->event);
 
        ifdebug(CONNECTIONS) logger(LOG_NOTICE,
                           "Trying to re-establish outgoing connection in %d seconds",
@@ -338,6 +338,11 @@ void do_outgoing_connection(connection_t *c) {
        char *address, *port;
        int result;
 
+       if(!c->outgoing) {
+               logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
+               abort();
+       }
+
 begin:
        if(!c->outgoing->ai) {
                if(!c->outgoing->cfg) {
@@ -345,6 +350,7 @@ begin:
                                           c->name);
                        c->status.remove = true;
                        retry_outgoing(c->outgoing);
+                       c->outgoing = NULL;
                        return;
                }
 
@@ -431,6 +437,8 @@ void setup_outgoing_connection(outgoing_t *outgoing) {
        connection_t *c;
        node_t *n;
 
+       outgoing->event = NULL;
+
        n = lookup_node(outgoing->name);
 
        if(n)
@@ -525,18 +533,7 @@ void try_outgoing_connections(void) {
        static config_t *cfg = NULL;
        char *name;
        outgoing_t *outgoing;
-       connection_t *c;
-       avl_node_t *node;
        
-       if(outgoing_list) {
-               for(node = connection_tree->head; node; node = node->next) {
-                       c = node->data;
-                       c->outgoing = NULL;
-               }
-
-               list_delete_list(outgoing_list);
-       }
-
        outgoing_list = list_alloc((list_action_t)free_outgoing);
                        
        for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {