From: Guus Sliepen Date: Tue, 12 Aug 2014 20:03:32 +0000 (+0200) Subject: Fix segfault when two nodes that just joined a mesh want to autoconnect to each other. X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=98b9d73682733072d66d6db3beb15e7309805cf5 Fix segfault when two nodes that just joined a mesh want to autoconnect to each other. In this case, we have not exchanged public keys yet. That should not be a problem, but we blindly pass a NULL pointer to sptps_start() in this case which blindly dereferences it. Fix sptps_start() by making sure no arguments are NULL, and teach MeshLink to exchange keys between reachable nodes when it tries to make a meta-connection. --- diff --git a/src/protocol_auth.c b/src/protocol_auth.c index d359b9b6..9cb96b65 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -329,11 +329,20 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { logger(mesh, MESHLINK_ERROR, "Peer %s had unknown identity (%s)", c->hostname, c->name); return false; } + } - read_ecdsa_public_key(mesh, c); - } else { - if(c->protocol_minor && !ecdsa_active(c->ecdsa)) - c->protocol_minor = 1; + read_ecdsa_public_key(mesh, c); + + if(!ecdsa_active(c->ecdsa)) { + logger(mesh, MESHLINK_ERROR, "No key known for peer %s (%s)", c->name, c->hostname); + + node_t *n = lookup_node(mesh, c->name); + if(n && !n->status.waitingforkey) { + logger(mesh, MESHLINK_INFO, "Requesting key from peer %s (%s)", c->name, c->hostname); + send_req_key(mesh, n); + } + + return false; } /* Forbid version rollback for nodes whose ECDSA key we know */ diff --git a/src/sptps.c b/src/sptps.c index 49e0a336..2e9ac6fe 100644 --- a/src/sptps.c +++ b/src/sptps.c @@ -565,6 +565,9 @@ bool sptps_receive_data(sptps_t *s, const void *data, size_t len) { // Start a SPTPS session. bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_t *mykey, ecdsa_t *hiskey, const char *label, size_t labellen, send_data_t send_data, receive_record_t receive_record) { + if(!s || !mykey || !hiskey || !label || !labellen || !send_data || !receive_record) + return error(s, EINVAL, "Invalid argument to sptps_start()"); + // Initialise struct sptps memset(s, 0, sizeof *s);