From 2c8703930720f7cad1758f2a9e5f83928cdcd7de Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 3 Jul 2012 17:59:30 +0930 Subject: [PATCH] Replace overlay_render_sid() with alloca_tohex_sid() To eliminate timebomb bugs caused by rotor wraparound --- commandline.c | 16 +++++++--------- dataformats.c | 2 +- dna_helper.c | 2 +- keyring.c | 10 +++++----- monitor.c | 20 +++++++++----------- overlay.c | 14 +++++++------- overlay_abbreviations.c | 4 ++-- overlay_advertise.c | 2 +- overlay_interface.c | 2 +- overlay_mdp.c | 10 +++++----- overlay_packetformats.c | 2 +- overlay_payload.c | 10 +++++----- overlay_route.c | 31 +++++++++++-------------------- serval.h | 6 ++---- vomp.c | 9 ++++----- 15 files changed, 62 insertions(+), 78 deletions(-) diff --git a/commandline.c b/commandline.c index 0761a7cc..5f037990 100644 --- a/commandline.c +++ b/commandline.c @@ -944,9 +944,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option * } /* XXX Eventually we should try to resolve SID to phone number and vice versa */ - printf("MDP PING %s (%s): 12 data bytes\n", - overlay_render_sid(ping_sid), - overlay_render_sid(ping_sid)); + printf("MDP PING %s (%s): 12 data bytes\n", alloca_tohex_sid(ping_sid), alloca_tohex_sid(ping_sid)); long long rx_mintime=-1; long long rx_maxtime=-1; @@ -1000,7 +998,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option * long long *txtime=(long long *)&mdp.in.payload[4]; long long delay=overlay_gettime_ms()-*txtime; printf("%s: seq=%d time=%lld ms%s%s\n", - overlay_render_sid(mdp.in.src.sid),(*rxseq)-firstSeq+1,delay, + alloca_tohex_sid(mdp.in.src.sid),(*rxseq)-firstSeq+1,delay, mdp.packetTypeAndFlags&MDP_NOCRYPT?"":" ENCRYPTED", mdp.packetTypeAndFlags&MDP_NOSIGN?"":" SIGNED"); // TODO Put duplicate pong detection here so that stats work properly. @@ -1031,7 +1029,7 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option * rx_stddev=sqrtf(rx_stddev); /* XXX Report final statistics before going */ - fprintf(stderr,"--- %s ping statistics ---\n",overlay_render_sid(ping_sid)); + fprintf(stderr,"--- %s ping statistics ---\n", alloca_tohex_sid(ping_sid)); fprintf(stderr,"%lld packets transmitted, %lld packets received, %3.1f%% packet loss\n", tx_count,rx_count,tx_count?(tx_count-rx_count)*100.0/tx_count:0); fprintf(stderr,"round-trip min/avg/max/stddev%s = %lld/%.3f/%lld/%.3f ms\n", @@ -1559,7 +1557,7 @@ int app_keyring_list(int argc, const char *const *argv, struct command_line_opti if (kp->type==KEYTYPE_DID) { did=kp->private_key; name=kp->public_key; } } if (sid||did) { - if (sid) cli_printf("%s",overlay_render_sid(sid)); + if (sid) cli_printf("%s", alloca_tohex_sid(sid)); cli_delim(":"); if (did) cli_puts((char*)did); cli_delim(":"); @@ -1647,7 +1645,7 @@ int app_id_self(int argc, const char *const *argv, struct command_line_option *o int i; for(i=0;i + if (snprintf(buffer, sizeof(buffer) - 1, "%s|%s|\n", alloca_tohex_sid(requestorSid), did) > sizeof(buffer) - 1) return WHY("Command to helper is too long"); diff --git a/keyring.c b/keyring.c index e3d1f83f..f0fb9d1e 100644 --- a/keyring.c +++ b/keyring.c @@ -1200,8 +1200,8 @@ int keyring_mapping_request(keyring_file *k,overlay_mdp_frame *req) DEBUG("Sent SID:SAS mapping mutual-signature"); printf("%d byte reply is from %s:%u\n to %s:%u\n", req->out.payload_length, - overlay_render_sid(req->out.src.sid),req->out.src.port, - overlay_render_sid(req->out.dst.sid),req->out.dst.port); + alloca_tohex_sid(req->out.src.sid),req->out.src.port, + alloca_tohex_sid(req->out.dst.sid),req->out.dst.port); return overlay_mdp_dispatch(req,1,NULL,0); } else { /* It's probably a response. */ @@ -1254,8 +1254,8 @@ int keyring_mapping_request(keyring_file *k,overlay_mdp_frame *req) crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES); fprintf(stderr,"Mapping #%d (count=%d) SID=%s to SAS=%s*\n",i, sid_sas_mapping_count, - overlay_render_sid(sid_sas_mappings[i].sid), - overlay_render_sid(sid_sas_mappings[i].sas_public)); + alloca_tohex_sid(sid_sas_mappings[i].sid), + alloca_tohex_sid(sid_sas_mappings[i].sas_public)); sid_sas_mappings[i].validP=1; sid_sas_mappings[i].last_request_time_in_ms=0; DEBUG("Stored mapping"); @@ -1302,7 +1302,7 @@ unsigned char *keyring_find_sas_public(keyring_file *k,unsigned char *sid) record. */ break; } - WHYF("Asking for SAS mapping for %s",overlay_render_sid(sid)); + WHYF("Asking for SAS mapping for %s", alloca_tohex_sid(sid)); /* allocate mapping slot or replace one at random, depending on how full things are */ diff --git a/monitor.c b/monitor.c index 4eea5e38..31b48624 100644 --- a/monitor.c +++ b/monitor.c @@ -389,10 +389,9 @@ int monitor_process_command(struct monitor_context *c) for(bin=0;binalarm.poll.fd,"\nERROR:no local identity, so cannot place call\n"); } else { - bcopy(keyring->contexts[cn]->identities[in] - ->keypairs[kp]->public_key, - &mdp.vompevent.local_sid[0],SID_SIZE); + bcopy(keyring->contexts[cn]->identities[in]->keypairs[kp]->public_key, + &mdp.vompevent.local_sid[0], SID_SIZE); stowSid(&mdp.vompevent.remote_sid[0],0,sid); vomp_mdp_event(&mdp,NULL,0); } @@ -498,7 +496,7 @@ int monitor_announce_bundle(rhizome_manifest *m) const char *recipient = rhizome_manifest_get(m, "recipient", NULL, 0); snprintf(msg,1024,"\nBUNDLE:%s:%s:%lld:%lld:%s:%s:%s\n", /* XXX bit of a hack here, since SIDs and cryptosign public keys have the same length */ - overlay_render_sid(m->cryptoSignPublic), + alloca_tohex_sid(m->cryptoSignPublic), service ? service : "", m->version, m->fileLength, @@ -542,8 +540,8 @@ int monitor_call_status(vomp_call_state *call) call->local.session,call->remote.session, call->local.state,call->remote.state, call->fast_audio, - overlay_render_sid(call->local.sid), - overlay_render_sid(call->remote.sid), + alloca_tohex_sid(call->local.sid), + alloca_tohex_sid(call->remote.sid), call->local.did,call->remote.did); msg[1023]=0; for(i=monitor_socket_count -1;i>=0;i--) @@ -568,7 +566,7 @@ int monitor_call_status(vomp_call_state *call) int monitor_announce_peer(const unsigned char *sid) { char msg[1024]; - int n = snprintf(msg, sizeof msg, "\nNEWPEER:%s\n",overlay_render_sid(sid)); + int n = snprintf(msg, sizeof msg, "\nNEWPEER:%s\n", alloca_tohex_sid(sid)); monitor_tell_clients(msg, n, MONITOR_PEERS); return 0; } diff --git a/overlay.c b/overlay.c index 751c2103..cb9280ef 100644 --- a/overlay.c +++ b/overlay.c @@ -316,7 +316,7 @@ int overlay_frame_process(struct overlay_interface *interface,overlay_frame *f) if (0) DEBUGF("considering forwarding frame to %s (forme=%d, bcast=%d, dup=%d)", - overlay_render_sid(f->destination),ultimatelyForMe,broadcast, + alloca_tohex_sid(f->destination),ultimatelyForMe,broadcast, duplicateBroadcast); if (overlay_address_is_broadcast(f->destination)) @@ -332,9 +332,9 @@ int overlay_frame_process(struct overlay_interface *interface,overlay_frame *f) if ((!sameAsNextHop)&&overlay_broadcast_drop_check(f->destination)) duplicateBroadcast=1; if (duplicateBroadcast) { - DEBUGF("reject src is %s", overlay_render_sid(f->source)); - DEBUGF("reject nexthop is %s", overlay_render_sid(f->nexthop)); - DEBUGF("reject destination is %s", overlay_render_sid(f->destination)); + DEBUGF("reject src is %s", alloca_tohex_sid(f->source)); + DEBUGF("reject nexthop is %s", alloca_tohex_sid(f->nexthop)); + DEBUGF("reject destination is %s", alloca_tohex_sid(f->destination)); RETURN(WHY("Not forwarding or reading duplicate broadcast")); } } @@ -390,9 +390,9 @@ int overlay_frame_process(struct overlay_interface *interface,overlay_frame *f) case OF_TYPE_DATA_VOICE: if (0) { DEBUG("saw mdp containing frame"); - DEBUGF(" src = %s\n",overlay_render_sid(f->source)); - DEBUGF(" nxt = %s\n",overlay_render_sid(f->nexthop)); - DEBUGF(" dst = %s\n",overlay_render_sid(f->destination)); + DEBUGF(" src = %s\n", alloca_tohex_sid(f->source)); + DEBUGF(" nxt = %s\n", alloca_tohex_sid(f->nexthop)); + DEBUGF(" dst = %s\n", alloca_tohex_sid(f->destination)); dump("payload", f->payload->bytes, f->payload->length); } overlay_saw_mdp_containing_frame(f,now); diff --git a/overlay_abbreviations.c b/overlay_abbreviations.c index e62c2f99..8ffd2baa 100644 --- a/overlay_abbreviations.c +++ b/overlay_abbreviations.c @@ -230,7 +230,7 @@ int overlay_abbreviate_append_address(overlay_buffer *b,unsigned char *a) int r=overlay_abbreviate_address(a,&b->bytes[b->length],&count); if (debug&DEBUG_PACKETCONSTRUCTION) { fprintf(stderr,"address %s abbreviates as shown in this ", - overlay_render_sid(a)); + alloca_tohex_sid(a)); dump(NULL,&b->bytes[b->length],count); } if (r) return r; @@ -553,7 +553,7 @@ int overlay_abbreviate_set_most_recent_address(unsigned char *in) { bcopy(in,&overlay_abbreviate_previous_address.b[0],SID_SIZE); if (debug&DEBUG_OVERLAYABBREVIATIONS) fprintf(stderr,"Most recent address=%s\n", - overlay_render_sid(in)); + alloca_tohex_sid(in)); return 0; } diff --git a/overlay_advertise.c b/overlay_advertise.c index 793535d7..9656738c 100644 --- a/overlay_advertise.c +++ b/overlay_advertise.c @@ -186,7 +186,7 @@ int overlay_route_saw_advertisements(int i,overlay_frame *f, long long now) } int sender_score=sender->best_link_score; if (debug&DEBUG_OVERLAYROUTEMONITOR) - DEBUGF("score to reach %s is %d", overlay_render_sid(f->source),sender_score); + DEBUGF("score to reach %s is %d", alloca_tohex_sid(f->source),sender_score); while(ofspayload->length) { diff --git a/overlay_interface.c b/overlay_interface.c index 3533bec5..c0eca71d 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -725,7 +725,7 @@ int overlay_stuff_packet_from_queue(int i,overlay_buffer *e,int q,long long now, } } else { DEBUG("bummer, I couldn't find an open route to that node"); - DEBUGF("sid=%s",overlay_render_sid((*p)->destination)); + DEBUGF("sid=%s", alloca_tohex_sid((*p)->destination)); } } else if (!(*p)->broadcast_sent_via[i]) { diff --git a/overlay_mdp.c b/overlay_mdp.c index 9ebe7cca..2f2843bd 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -325,7 +325,7 @@ unsigned char *overlay_mdp_decrypt(overlay_frame *f,overlay_mdp_frame *mdp, if (0) { fflush(stderr); printf("crypted MDP frame for %s\n", - overlay_render_sid(mdp->out.dst.sid)); + alloca_tohex_sid(mdp->out.dst.sid)); fflush(stdout); } @@ -542,8 +542,8 @@ int overlay_saw_mdp_frame(overlay_mdp_frame *mdp,long long now) bcopy(packedSid,&mdpreply.out.src.sid[0],SID_SIZE); /* and build reply as did\nname\nURI */ snprintf((char *)&mdpreply.out.payload[0],512,"%s|sid://%s/%s|%s|%s|", - overlay_render_sid(packedSid), - overlay_render_sid(packedSid),unpackedDid, + alloca_tohex_sid(packedSid), + alloca_tohex_sid(packedSid),unpackedDid, unpackedDid,name); mdpreply.out.payload_length=strlen((char *)mdpreply.out.payload)+1; @@ -682,7 +682,7 @@ int overlay_mdp_sanitytest_sourceaddr(sockaddr_mdp *src,int userGeneratedFrameP, } printf("addr=%s port=%u (0x%x)\n", - overlay_render_sid(src->sid),src->port,src->port); + alloca_tohex_sid(src->sid),src->port,src->port); if (recvaddr) printf("recvaddr='%s'\n", recvaddr->sun_path); return WHY("No such socket binding:unix domain socket tuple exists -- someone might be trying to spoof someone else's connection"); @@ -1330,7 +1330,7 @@ int overlay_mdp_getmyaddr(int index,unsigned char *sid) } if ((a.packetTypeAndFlags&MDP_TYPE_MASK)!=MDP_ADDRLIST) return WHY("MDP Server returned something other than an address list"); - if (0) DEBUGF("local addr 0 = %s",overlay_render_sid(a.addrlist.sids[0])); + if (0) DEBUGF("local addr 0 = %s",alloca_tohex_sid(a.addrlist.sids[0])); bcopy(&a.addrlist.sids[0][0],sid,SID_SIZE); return 0; } diff --git a/overlay_packetformats.c b/overlay_packetformats.c index 2fd29b2b..40fffa20 100644 --- a/overlay_packetformats.c +++ b/overlay_packetformats.c @@ -180,7 +180,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s f.nexthop_address_status=overlay_abbreviate_expand_address(packet,&offset,f.nexthop,&alen); if (debug&DEBUG_PACKETFORMATS) { if (f.nexthop_address_status==OA_RESOLVED) - DEBUGF("next hop address is %s", overlay_render_sid(f.nexthop)); + DEBUGF("next hop address is %s", alloca_tohex_sid(f.nexthop)); } /* Now just make the rest of the frame available via the received frame structure, as the diff --git a/overlay_payload.c b/overlay_payload.c index a402f36e..5a7f8b35 100644 --- a/overlay_payload.c +++ b/overlay_payload.c @@ -82,7 +82,7 @@ int overlay_frame_package_fmt1(overlay_frame *p,overlay_buffer *b) if (p->nexthop_address_status!=OA_RESOLVED) { if (0) WHYF("next hop is NOT resolved for packet to %s", - overlay_render_sid(p->destination)); + alloca_tohex_sid(p->destination)); if (overlay_address_is_broadcast(p->destination)) { /* Broadcast frames are broadcast rather than unicast to next hop. Just check if the broadcast frame should be dropped first. */ @@ -101,7 +101,7 @@ int overlay_frame_package_fmt1(overlay_frame *p,overlay_buffer *b) } } else { if (0) WHYF("next hop IS resolved for packet to %s", - overlay_render_sid(p->destination)); + alloca_tohex_sid(p->destination)); } @@ -223,10 +223,10 @@ int dump_payload(overlay_frame *p,char *message) fflush(stdout); fprintf(stderr, "+++++\nFrame from %s to %s of type 0x%02x %s:\n", - overlay_render_sid(p->source), - overlay_render_sid(p->destination),p->type, + alloca_tohex_sid(p->source), + alloca_tohex_sid(p->destination),p->type, message?message:""); - fprintf(stderr," next hop is %s\n",overlay_render_sid(p->nexthop)); + fprintf(stderr," next hop is %s\n",alloca_tohex_sid(p->nexthop)); fflush(stderr); if (p->payload) dump("payload contents", &p->payload->bytes[0],p->payload->length); diff --git a/overlay_route.c b/overlay_route.c index c337fc8d..7a753b7a 100644 --- a/overlay_route.c +++ b/overlay_route.c @@ -395,11 +395,11 @@ int overlay_get_nexthop(unsigned char *d,unsigned char *nexthop,int *nexthoplen, if (neh->scores[*interface]<1) { if (debug&DEBUG_OVERLAYROUTING) { *interface=-1; - DEBUGF("No open path to %s",overlay_render_sid(neh->node->sid)); + DEBUGF("No open path to %s",alloca_tohex_sid(neh->node->sid)); } return -1; } - if (0) DEBUGF("nexthop is %s",overlay_render_sid(nexthop)); + if (0) DEBUGF("nexthop is %s",alloca_tohex_sid(nexthop)); return 0; } else { /* Is not a direct neighbour. @@ -428,10 +428,10 @@ int overlay_get_nexthop(unsigned char *d,unsigned char *nexthop,int *nexthoplen, if (best_o>-1) { return 0; } else { - return -1; // WHYF("No open path to %s",overlay_render_sid(d)); + return -1; // WHYF("No open path to %s",alloca_tohex_sid(d)); } } else { - return -1; // WHYF("No open path to %s",overlay_render_sid(d)); + return -1; // WHYF("No open path to %s",alloca_tohex_sid(d)); } } } @@ -704,7 +704,7 @@ int overlay_route_i_can_hear_node(unsigned char *who,int sender_interface, long long now) { if (0) DEBUGF("I can hear node %s (but I really only care who can hear me)", - overlay_render_sid(who)); + alloca_tohex_sid(who)); return 0; } @@ -1010,17 +1010,8 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now) int ors_rotor=0; char ors_out[4][SID_STRLEN+1]; -char *overlay_render_sid(unsigned char *sid) -{ - int zero=0; - ors_rotor++; - ors_rotor&=3; - extractSid(sid,&zero,ors_out[ors_rotor]); - ors_out[ors_rotor][SID_STRLEN] = '\0'; - return ors_out[ors_rotor]; -} -char *overlay_render_sid_prefix(unsigned char *sid,int l) +char *overlay_render_sid_prefix(const unsigned char *sid,int l) { int zero=0; @@ -1074,7 +1065,7 @@ int overlay_route_saw_selfannounce_ack(overlay_frame *f,long long now) if (f->source_address_status==OA_RESOLVED&& f->destination_address_status==OA_RESOLVED) { if (0) DEBUGF("f->source=%s, f->destination=%s", - overlay_render_sid(f->source),overlay_render_sid(f->destination)); + alloca_tohex_sid(f->source),alloca_tohex_sid(f->destination)); overlay_route_record_link(now,f->source,f->source,iface,s1,s2, 0 /* no associated score */, 0 /* no gateways in between */); @@ -1093,7 +1084,7 @@ int overlay_route_record_link(long long now,unsigned char *to, int i,slot=-1; if (0) DEBUGF("to=%s, via=%s, iface=%d, s1=%d, s2=%d", - overlay_render_sid(to),overlay_render_sid(via), + alloca_tohex_sid(to),alloca_tohex_sid(via), sender_interface,s1,s2); @@ -1102,16 +1093,16 @@ int overlay_route_record_link(long long now,unsigned char *to, /* Don't record routes to ourselves */ if (overlay_address_is_local(to)) { if (0) DEBUGF("Ignoring self announce ack addressed to me (%s).", - overlay_render_sid(to)); + alloca_tohex_sid(to)); return 0; } - else if (0) DEBUGF("Recording link to %s",overlay_render_sid(to)); + else if (0) DEBUGF("Recording link to %s",alloca_tohex_sid(to)); for(i=0;i %s/%06x\n (%s -> %s)\n", - overlay_render_sid(vomp_call_states[i].local.sid), + alloca_tohex_sid(vomp_call_states[i].local.sid), vomp_call_states[i].local.session, - overlay_render_sid(vomp_call_states[i].remote.sid), + alloca_tohex_sid(vomp_call_states[i].remote.sid), vomp_call_states[i].remote.session, vomp_call_states[i].local.did, vomp_call_states[i].remote.did); @@ -1231,9 +1231,8 @@ int app_vomp_dial(int argc, const char *const *argv, struct command_line_option mdp.vompevent.flags=VOMPEVENT_DIAL; if (overlay_mdp_getmyaddr(0,&mdp.vompevent.local_sid[0])) return -1; stowSid(&mdp.vompevent.remote_sid[0],0,sid); - printf("local_sid=%s\n",overlay_render_sid(mdp.vompevent.local_sid)); - printf("remote_sid=%s from %s\n", - overlay_render_sid(mdp.vompevent.remote_sid),sid); + printf("local_sid=%s\n",alloca_tohex_sid(mdp.vompevent.local_sid)); + printf("remote_sid=%s from %s\n", alloca_tohex_sid(mdp.vompevent.remote_sid),sid); if (overlay_mdp_send(&mdp,MDP_AWAITREPLY,5000)) {