From 6f7397819e8329efdbe2917c9fb2174a0d212241 Mon Sep 17 00:00:00 2001 From: Luis Garcia Date: Thu, 6 Jul 2023 21:47:25 +0800 Subject: [PATCH 1/3] Changed files to deal with -Wstringop-truncation warning. --- lib/netdev.c | 2 +- oflib-exp/ofl-exp-openflow.c | 4 +++- oflib/ofl-structs-pack.c | 6 ++++-- udatapath/datapath.c | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/netdev.c b/lib/netdev.c index 480641c1..aebcbbb9 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -1453,7 +1453,7 @@ netdev_arp_lookup(const struct netdev *netdev, pa->sin_port = 0; r.arp_ha.sa_family = ARPHRD_ETHER; r.arp_flags = 0; - strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev); + strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev -1); retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0; if (!retval) { memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN); diff --git a/oflib-exp/ofl-exp-openflow.c b/oflib-exp/ofl-exp-openflow.c index 51c69eaf..a6f899cd 100644 --- a/oflib-exp/ofl-exp-openflow.c +++ b/oflib-exp/ofl-exp-openflow.c @@ -74,7 +74,9 @@ ofl_exp_openflow_msg_pack(struct ofl_msg_experimenter *msg, uint8_t **buf, size_ ofp = (struct openflow_ext_set_dp_desc *)(*buf); ofp->header.vendor = htonl(exp->header.experimenter_id); ofp->header.subtype = htonl(exp->type); - strncpy(ofp->dp_desc, s->dp_desc, DESC_STR_LEN); + //We need a a byte less on DESC_STR_LEN + memset(ofp->dp_desc, 0, DESC_STR_LEN) + strncpy(ofp->dp_desc, s->dp_desc, DESC_STR_LEN-1); return 0; } diff --git a/oflib/ofl-structs-pack.c b/oflib/ofl-structs-pack.c index 1ab892aa..747f3dd0 100644 --- a/oflib/ofl-structs-pack.c +++ b/oflib/ofl-structs-pack.c @@ -440,7 +440,8 @@ ofl_structs_table_features_pack(struct ofl_table_features *src, struct ofp_table total_len = sizeof(struct ofp_table_features) + ofl_structs_table_features_properties_ofp_total_len(src->properties,src->properties_num,exp); dst->table_id = src->table_id; memset(dst->pad, 0x0,5); - strncpy(dst->name,src->name, OFP_MAX_TABLE_NAME_LEN); + memset(dst->name, 0x0,OFP_MAX_TABLE_NAME_LEN); + strncpy(dst->name,src->name, OFP_MAX_TABLE_NAME_LEN-1); dst->metadata_match = hton64(src->metadata_match); dst->metadata_write = hton64(src->metadata_write); dst->config = htonl(src->config); @@ -835,7 +836,8 @@ ofl_structs_port_pack(struct ofl_port *src, struct ofp_port *dst) { memset(dst->pad, 0x00, 4); memcpy(dst->hw_addr, src->hw_addr, ETH_ADDR_LEN); memset(dst->pad2, 0x00, 2); - strncpy(dst->name, src->name, OFP_MAX_PORT_NAME_LEN); + memset(dst->name, 0x00, OFP_MAX_PORT_NAME_LEN); + strncpy(dst->name, src->name, OFP_MAX_PORT_NAME_LEN-1); dst->config = htonl(src->config); dst->state = htonl(src->state); dst->curr = htonl(src->curr); diff --git a/udatapath/datapath.c b/udatapath/datapath.c index df45af71..5b27abe2 100644 --- a/udatapath/datapath.c +++ b/udatapath/datapath.c @@ -162,7 +162,7 @@ dp_new(void) { char pid[10]; gethostname(hostnametmp,sizeof hostnametmp); sprintf(pid, "%u", getpid()); - snprintf(dp->dp_desc, strlen(hostnametmp) + 5 + strlen(pid),"%s pid=%s",hostnametmp, pid); + snprintf(dp->dp_desc, strlen(hostnametmp) + 6 + strlen(pid),"%s pid=%s",hostnametmp, pid); } /* FIXME: Should not depend on udatapath_as_lib */ From 63ac775ce10c42333a59f9ef02e6b2a130246898 Mon Sep 17 00:00:00 2001 From: Luis Garcia Date: Thu, 6 Jul 2023 21:57:02 +0800 Subject: [PATCH 2/3] Fix snprint with possible overflow of buffer. --- udatapath/datapath.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/udatapath/datapath.c b/udatapath/datapath.c index 5b27abe2..d48313a5 100644 --- a/udatapath/datapath.c +++ b/udatapath/datapath.c @@ -162,7 +162,7 @@ dp_new(void) { char pid[10]; gethostname(hostnametmp,sizeof hostnametmp); sprintf(pid, "%u", getpid()); - snprintf(dp->dp_desc, strlen(hostnametmp) + 6 + strlen(pid),"%s pid=%s",hostnametmp, pid); + snprintf(dp->dp_desc, DESC_STR_LEN-1,"%s pid=%s",hostnametmp, pid); } /* FIXME: Should not depend on udatapath_as_lib */ From 91b5d86447ca663bbab4d18987098b447cb4d114 Mon Sep 17 00:00:00 2001 From: Luis Garcia Date: Thu, 6 Jul 2023 23:22:09 +0800 Subject: [PATCH 3/3] Overlow fix on snprintf --- oflib-exp/ofl-exp-openflow.c | 2 +- udatapath/datapath.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/oflib-exp/ofl-exp-openflow.c b/oflib-exp/ofl-exp-openflow.c index a6f899cd..cc608525 100644 --- a/oflib-exp/ofl-exp-openflow.c +++ b/oflib-exp/ofl-exp-openflow.c @@ -75,7 +75,7 @@ ofl_exp_openflow_msg_pack(struct ofl_msg_experimenter *msg, uint8_t **buf, size_ ofp->header.vendor = htonl(exp->header.experimenter_id); ofp->header.subtype = htonl(exp->type); //We need a a byte less on DESC_STR_LEN - memset(ofp->dp_desc, 0, DESC_STR_LEN) + memset(ofp->dp_desc, 0, DESC_STR_LEN); strncpy(ofp->dp_desc, s->dp_desc, DESC_STR_LEN-1); return 0; diff --git a/udatapath/datapath.c b/udatapath/datapath.c index d48313a5..53dfc77e 100644 --- a/udatapath/datapath.c +++ b/udatapath/datapath.c @@ -158,7 +158,9 @@ dp_new(void) { if(strlen(dp->dp_desc) == 0) { /* just use "$HOSTNAME pid=$$" */ - char hostnametmp[DESC_STR_LEN]; + // We need to adjust for it to fit dp + size_t prefix_len = strlen(" pid=") + 10; + char hostnametmp[DESC_STR_LEN-prefix_len-1]; char pid[10]; gethostname(hostnametmp,sizeof hostnametmp); sprintf(pid, "%u", getpid());