From 04226c09c8811a0b87b6dc2988a8be62b424a193 Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Tue, 21 May 2019 22:53:55 +0200 Subject: [PATCH] Fix compilation warnings and little leak --- edge.c | 7 ++++++- edge_utils.c | 20 ++++++++++++++++---- sn.c | 3 ++- transform_aes.c | 3 +-- tuntap_linux.c | 3 ++- wire.c | 2 +- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/edge.c b/edge.c index 6a42bd3..5270795 100644 --- a/edge.c +++ b/edge.c @@ -100,7 +100,8 @@ static int scan_address(char * ip_addr, size_t addr_size, else { /* colon is not present */ - strncpy(ip_addr, s, addr_size); + strncpy(ip_addr, s, addr_size-1); + ip_addr[addr_size-1] = '\0'; } return retval; @@ -191,6 +192,7 @@ static int setOption(int optkey, char *optargument, n2n_priv_config_t *ec, n2n_e { memset(conf->community_name, 0, N2N_COMMUNITY_SIZE); strncpy((char *)conf->community_name, optargument, N2N_COMMUNITY_SIZE); + conf->community_name[N2N_COMMUNITY_SIZE-1] = '\0'; break; } @@ -226,6 +228,7 @@ static int setOption(int optkey, char *optargument, n2n_priv_config_t *ec, n2n_e case 'm' : /* TUNTAP MAC address */ { strncpy(ec->device_mac,optargument,N2N_MACNAMSIZ); + ec->device_mac[N2N_MACNAMSIZ-1] = '\0'; break; } @@ -274,6 +277,7 @@ static int setOption(int optkey, char *optargument, n2n_priv_config_t *ec, n2n_e case 'd': /* TUNTAP name */ { strncpy(ec->tuntap_dev_name, optargument, N2N_IFNAMSIZ); + ec->tuntap_dev_name[N2N_IFNAMSIZ-1] = '\0'; break; } #endif @@ -302,6 +306,7 @@ static int setOption(int optkey, char *optargument, n2n_priv_config_t *ec, n2n_e traceEvent(TRACE_WARNING, "Multiple subnet masks supplied"); } strncpy(ec->netmask, optargument, N2N_NETMASK_STR_SIZE); + ec->netmask[N2N_NETMASK_STR_SIZE - 1] = '\0'; ec->got_s = 1; break; } diff --git a/edge_utils.c b/edge_utils.c index 7ac5cbc..0a095f3 100644 --- a/edge_utils.c +++ b/edge_utils.c @@ -119,6 +119,17 @@ struct n2n_edge { /* ************************************** */ +static const char* transop_str(enum n2n_transform tr) { + switch(tr) { + case N2N_TRANSFORM_ID_NULL: return("null"); + case N2N_TRANSFORM_ID_TWOFISH: return("twofish"); + case N2N_TRANSFORM_ID_AESCBC: return("AES-CBC"); + default: return("invalid"); + }; +} + +/* ************************************** */ + /** Initialise an edge to defaults. * * This also initialises the NULL transform operation opstruct. @@ -577,7 +588,7 @@ static ssize_t sendto_sock(int fd, const void * buf, /** Send a REGISTER_SUPER packet to the current supernode. */ static void send_register_super(n2n_edge_t * eee, const n2n_sock_t * supernode) { - uint8_t pktbuf[N2N_PKT_BUF_SIZE]; + uint8_t pktbuf[N2N_PKT_BUF_SIZE] = {0}; size_t idx; /* ssize_t sent; */ n2n_common_t cmn; @@ -832,8 +843,9 @@ static int handle_PACKET(n2n_edge_t * eee, } else { - traceEvent(TRACE_ERROR, "invalid transop ID: %u, expected %u", - rx_transop_id, eee->conf.transop_id); + traceEvent(TRACE_ERROR, "invalid transop ID: expected %s(%u), got %s(%u)", + transop_str(eee->conf.transop_id), eee->conf.transop_id, + transop_str(rx_transop_id), rx_transop_id); } } @@ -1604,7 +1616,7 @@ void edge_term(n2n_edge_t * eee) { clear_peer_list(&(eee->known_peers)); eee->transop.deinit(&eee->transop); - memset(eee, 0, sizeof(*eee)); + free(eee); } /* ************************************** */ diff --git a/sn.c b/sn.c index 42a3968..e96a704 100644 --- a/sn.c +++ b/sn.c @@ -435,7 +435,8 @@ static int load_allowed_n2n_communities(char *path) { s = (struct n2n_allowed_communities*)malloc(sizeof(struct n2n_allowed_communities)); if(s != NULL) { - strncpy((char*)s->community, line, N2N_COMMUNITY_SIZE); + strncpy((char*)s->community, line, N2N_COMMUNITY_SIZE-1); + s->community[N2N_COMMUNITY_SIZE-1] = '\0'; HASH_ADD_STR(allowed_communities, community, s); num_communities++; traceEvent(TRACE_INFO, "Added allowed community '%s' [total: %u]", diff --git a/transform_aes.c b/transform_aes.c index cce51ff..fb15054 100644 --- a/transform_aes.c +++ b/transform_aes.c @@ -144,8 +144,7 @@ static int transop_encode_aes( n2n_trans_op_t * arg, * Using two calls to rand() because RAND_MAX is usually < 64bit * (e.g. linux) and sometimes < 32bit (e.g. Windows). */ - ((uint32_t*)&iv_seed)[0] = rand(); - ((uint32_t*)&iv_seed)[1] = rand(); + iv_seed = ((((uint64_t)rand() & 0xFFFFFFFF)) << 32) | rand(); encode_buf(outbuf, &idx, &iv_seed, sizeof(iv_seed)); /* Encrypt the assembly contents and write the ciphertext after the SA. */ diff --git a/tuntap_linux.c b/tuntap_linux.c index 6c619d0..c44b6db 100644 --- a/tuntap_linux.c +++ b/tuntap_linux.c @@ -84,7 +84,8 @@ int tuntap_open(tuntap_dev *device, memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ - strncpy(ifr.ifr_name, dev, IFNAMSIZ); + strncpy(ifr.ifr_name, dev, IFNAMSIZ-1); + ifr.ifr_name[IFNAMSIZ-1] = '\0'; rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); if(rc < 0) { diff --git a/wire.c b/wire.c index 228b426..e21d321 100644 --- a/wire.c +++ b/wire.c @@ -228,7 +228,7 @@ int decode_sock( n2n_sock_t * sock, size_t * idx ) { size_t * idx0=idx; - uint16_t f; + uint16_t f=0; decode_uint16( &f, base, rem, idx );