From 98b9d73682733072d66d6db3beb15e7309805cf5 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 12 Aug 2014 22:03:32 +0200 Subject: [PATCH] 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. --- src/protocol_auth.c | 17 +++++++++++++---- src/sptps.c | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) 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); -- 2.39.5