Skip to contentSkip to conversation

gcc-8 warning fixes

PATCH Superseded Stephen Hemminger 3 participants 12 messages Mar 20, 2018 – Mar 21, 2018

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover gcc-8 warning fixes

This fixes most of the warning seen build iproute2 with gcc-8. There are still a couple in namespace and bpf to resolve related to PATH_MAX.

Stephen Hemminger (9):
bridge: avoid snprint truncation on time
pedit: fix strncpy warning
ip: use strlcpy() to avoid truncation
tunnel: use strlcpy to avoid strncpy warnings
namespace: fix warning snprintf buffer
tc_class: fix snprintf warning
ematch: fix possible snprintf overflow
misc: avoid snprintf warnings in ss and nstat
bpf: avoid compiler warnings about strncpy

 bridge/mdb.c    |  4 ++--
 ip/iplink.c     | 14 +++++++-------
 ip/tunnel.c     | 12 ++++++------
 lib/bpf.c       |  6 +++---
 lib/namespace.c |  6 ++++--
 misc/nstat.c    |  4 ++--
 misc/ss.c       |  2 +-
 tc/m_ematch.c   |  2 +-
 tc/m_pedit.c    |  2 +-
 tc/tc_class.c   |  5 +++--
 10 files changed, 30 insertions(+), 27 deletions(-)
Signature · 1 line
2.16.2
#1 lore raw

PATCH v2 1/9 bridge: avoid snprint truncation on time

This fixes new gcc warning about possible string overflow.

mdb.c: In function ‘__print_router_port_stats’:
mdb.c:61:11: warning: ‘%.2i’ directive output may be truncated
writing between 2 and 7 bytes into a region of size between 0 and 4 [-Wformat-truncation=]

      "%4i.%.2i", (int)tv.tv_sec,
           ^~~~

Note: already fixed in iproute2-next.

---
 bridge/mdb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
bridge/mdb.c +2 -2
index 58c20b82b8a6..659cac3ff20a 100644
@@ -55,7 +55,7 @@ static void __print_router_port_stats(FILE *f, struct rtattr *pattr)
 		__jiffies_to_tv(&tv,
 				rta_getattr_u32(tb[MDBA_ROUTER_PATTR_TIMER]));
 		if (jw_global) {
-			char formatted_time[9];
+			char formatted_time[32];
 
 			snprintf(formatted_time, sizeof(formatted_time),
 				 "%4i.%.2i", (int)tv.tv_sec,
@@ -184,7 +184,7 @@ static void print_mdb_entry(FILE *f, int ifindex, struct br_mdb_entry *e,
 
 		__jiffies_to_tv(&tv, rta_getattr_u32(tb[MDBA_MDB_EATTR_TIMER]));
 		if (jw_global) {
-			char formatted_time[9];
+			char formatted_time[32];
 
 			snprintf(formatted_time, sizeof(formatted_time),
 				 "%4i.%.2i", (int)tv.tv_sec,
Signature · 1 line
2.16.2
#2 lore raw

PATCH v2 2/9 pedit: fix strncpy warning

In reply to Stephen Hemminger

Newer versions of Gcc warn about string truncation. Fix by using strlcpy.

---
 tc/m_pedit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tc/m_pedit.c +1 -1
index 26549eeea899..8577f875a7c0 100644
@@ -111,7 +111,7 @@ reg:
 noexist:
 	p = calloc(1, sizeof(*p));
 	if (p) {
-		strncpy(p->id, str, sizeof(p->id) - 1);
+		strlcpy(p->id, str, sizeof(p->id));
 		p->parse_peopt = pedit_parse_nopopt;
 		goto reg;
 	}
Signature · 1 line
2.16.2
#3 lore raw

PATCH v2 3/9 ip: use strlcpy() to avoid truncation

In reply to Stephen Hemminger

This fixes gcc-8 warnings about strncpy bounds by using strlcpy instead.

---
 ip/iplink.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
ip/iplink.c +7 -7
index d401311bcad9..cca530eeeb09 100644
@@ -1114,7 +1114,7 @@ static int do_chflags(const char *dev, __u32 flags, __u32 mask)
 	int fd;
 	int err;
 
-	strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
 	fd = get_ctl_fd();
 	if (fd < 0)
 		return -1;
@@ -1141,8 +1141,8 @@ static int do_changename(const char *dev, const char *newdev)
 	int fd;
 	int err;
 
-	strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-	strncpy(ifr.ifr_newname, newdev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr.ifr_newname, newdev, IFNAMSIZ);
 	fd = get_ctl_fd();
 	if (fd < 0)
 		return -1;
@@ -1165,7 +1165,7 @@ static int set_qlen(const char *dev, int qlen)
 	if (s < 0)
 		return -1;
 
-	strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
 	if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
 		perror("SIOCSIFXQLEN");
 		close(s);
@@ -1185,7 +1185,7 @@ static int set_mtu(const char *dev, int mtu)
 	if (s < 0)
 		return -1;
 
-	strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
 	if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
 		perror("SIOCSIFMTU");
 		close(s);
@@ -1212,7 +1212,7 @@ static int get_address(const char *dev, int *htype)
 		return -1;
 	}
 
-	strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
 	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
 		perror("SIOCGIFINDEX");
 		close(s);
@@ -1243,7 +1243,7 @@ static int parse_address(const char *dev, int hatype, int halen,
 	int alen;
 
 	memset(ifr, 0, sizeof(*ifr));
-	strncpy(ifr->ifr_name, dev, IFNAMSIZ);
+	strlcpy(ifr->ifr_name, dev, IFNAMSIZ);
 	ifr->ifr_hwaddr.sa_family = hatype;
 	alen = ll_addr_a2n(ifr->ifr_hwaddr.sa_data, 14, lla);
 	if (alen < 0)
Signature · 1 line
2.16.2
#4 lore raw

PATCH v2 4/9 tunnel: use strlcpy to avoid strncpy warnings

In reply to Stephen Hemminger

Fixes warnings about strncpy size by using strlcpy.

tunnel.c: In function ‘tnl_gen_ioctl’:
tunnel.c:145:2: warning: ‘strncpy’ specified bound
16 equals destination size [-Wstringop-truncation]
strncpy(ifr.ifr_name, name, IFNAMSIZ); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 ip/tunnel.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
ip/tunnel.c +6 -6
index 948d5f7c90f6..abd9fa2ffe0c 100644
@@ -64,7 +64,7 @@ int tnl_get_ioctl(const char *basedev, void *p)
 	int fd;
 	int err;
 
-	strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
 	ifr.ifr_ifru.ifru_data = (void *)p;
 
 	fd = socket(preferred_family, SOCK_DGRAM, 0);
@@ -89,9 +89,9 @@ int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
 	int err;
 
 	if (cmd == SIOCCHGTUNNEL && name[0])
-		strncpy(ifr.ifr_name, name, IFNAMSIZ);
+		strlcpy(ifr.ifr_name, name, IFNAMSIZ);
 	else
-		strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+		strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
 	ifr.ifr_ifru.ifru_data = p;
 
 	fd = socket(preferred_family, SOCK_DGRAM, 0);
@@ -115,9 +115,9 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p)
 	int err;
 
 	if (name[0])
-		strncpy(ifr.ifr_name, name, IFNAMSIZ);
+		strlcpy(ifr.ifr_name, name, IFNAMSIZ);
 	else
-		strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+		strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
 
 	ifr.ifr_ifru.ifru_data = p;
 
@@ -142,7 +142,7 @@ static int tnl_gen_ioctl(int cmd, const char *name,
 	int fd;
 	int err;
 
-	strncpy(ifr.ifr_name, name, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, name, IFNAMSIZ);
 	ifr.ifr_ifru.ifru_data = p;
 
 	fd = socket(preferred_family, SOCK_DGRAM, 0);
Signature · 1 line
2.16.2
#5 lore raw

PATCH v2 5/9 namespace: fix warning snprintf buffer

In reply to Stephen Hemminger

It is possible that user could request really long namespace name and overrun the path buffer.

---
 lib/namespace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/namespace.c +4 -2
index 6f3356d0fa08..682634028587 100644
@@ -23,7 +23,8 @@ static void bind_etc(const char *name)
 	struct dirent *entry;
 	DIR *dir;
 
-	snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
+	snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s",
+		 NETNS_ETC_DIR, name);
 	dir = opendir(etc_netns_path);
 	if (!dir)
 		return;
@@ -33,7 +34,8 @@ static void bind_etc(const char *name)
 			continue;
 		if (strcmp(entry->d_name, "..") == 0)
 			continue;
-		snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
+		snprintf(netns_name, sizeof(netns_name),
+			 "%s/%s", etc_netns_path, entry->d_name);
 		snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
 		if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
 			fprintf(stderr, "Bind %s -> %s failed: %s\n",
Signature · 1 line
2.16.2
#6 lore raw

Re: [PATCH iproute2 v2 5/9] namespace: fix warning snprintf buffer

Hello!

Quoted 23 lines · Stephen Hemminger

It is possible that user could request really long namespace name and overrun the path buffer.

---
  lib/namespace.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
lib/namespace.c +2 -1
index 6f3356d0fa08..682634028587 100644
@@ -23,7 +23,8 @@ static void bind_etc(const char *name)
  	struct dirent *entry;
  	DIR *dir;
 
-	snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
+	snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s",
+		 NETNS_ETC_DIR, name);
  	dir = opendir(etc_netns_path);
  	if (!dir)
  		return;

lib/namespace.c static void bind_etc(const char *name) · line 41

Quoted 9 lines
lib/namespace.c +2 -1
@@ -33,7 +34,8 @@ static void bind_etc(const char *name)
  			continue;
  		if (strcmp(entry->d_name, "..") == 0)
  			continue;
-		snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
+		snprintf(netns_name, sizeof(netns_name),
+			 "%s/%s", etc_netns_path, entry->d_name);
  		snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
  		if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
  			fprintf(stderr, "Bind %s -> %s failed: %s\n",

Hm... not seeing any changes other than the line wrapping. Am I just
blind? :-)

MBR, Sergei

#7 lore raw

PATCH v2 6/9 tc_class: fix snprintf warning

In reply to Stephen Hemminger

Size buffer big enough to avoid any possible overflow.

---
 tc/tc_class.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tc/tc_class.c +3 -2
index 1b214b82c702..91802518bb27 100644
@@ -219,7 +219,7 @@ static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
 	char cls_id_str[256] = {};
 	struct rtattr *tb[TCA_MAX + 1];
 	struct qdisc_util *q;
-	char str[100] = {};
+	char str[300] = {};
 
 	hlist_for_each_safe(n, tmp_cls, root_list) {
 		struct hlist_node *c, *tmp_chld;
@@ -242,7 +242,8 @@ static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
 		graph_indent(buf, cls, 0, 0);
 
 		print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id);
-		sprintf(str, "+---(%s)", cls_id_str);
+		snprintf(str, sizeof(str),
+			 "+---(%s)", cls_id_str);
 		strcat(buf, str);
 
 		parse_rtattr(tb, TCA_MAX, (struct rtattr *)cls->data,
Signature · 1 line
2.16.2
#8 lore raw

PATCH v2 7/9 ematch: fix possible snprintf overflow

In reply to Stephen Hemminger

Fixes gcc 8 warning about possible snprint overflow

---
 tc/m_ematch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tc/m_ematch.c +1 -1
index d2bb5c380382..0d66dc682314 100644
@@ -161,7 +161,7 @@ static struct ematch_util *get_ematch_kind(char *kind)
 
 static struct ematch_util *get_ematch_kind_num(__u16 kind)
 {
-	char name[32];
+	char name[513];
 
 	if (lookup_map(kind, name, sizeof(name), EMATCH_MAP) < 0)
 		return NULL;
Signature · 1 line
2.16.2
#9 lore raw

PATCH v2 8/9 misc: avoid snprintf warnings in ss and nstat

In reply to Stephen Hemminger

Gcc 8 checks that target buffer is big enough.

---
 misc/nstat.c | 4 ++--
 misc/ss.c    | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
misc/nstat.c +2 -2
index a4dd405d43a9..433a1f483be3 100644
@@ -178,12 +178,12 @@ static int count_spaces(const char *line)
 
 static void load_ugly_table(FILE *fp)
 {
-	char buf[4096];
+	char buf[2048];
 	struct nstat_ent *db = NULL;
 	struct nstat_ent *n;
 
 	while (fgets(buf, sizeof(buf), fp) != NULL) {
-		char idbuf[sizeof(buf)];
+		char idbuf[4096];
 		int  off;
 		char *p;
 		int count1, count2, skip = 0;
misc/ss.c +1 -1
index e087bef739b0..a03fa4a7c174 100644
@@ -4032,7 +4032,7 @@ static int netlink_show_one(struct filter *f,
 
 		if (!pid) {
 			done = 1;
-			strncpy(procname, "kernel", 6);
+			strncpy(procname, "kernel", 7);
 		} else if (pid > 0) {
 			FILE *fp;
 
Signature · 1 line
2.16.2
#10 lore raw

PATCH v2 9/9 bpf: avoid compiler warnings about strncpy

In reply to Stephen Hemminger

Use strlcpy to avoid cases where sizeof(buf) == strlen(buf)

---
 lib/bpf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/bpf.c +3 -3
index c38d92d87759..04bc5a5685d5 100644
@@ -2593,7 +2593,7 @@ bpf_map_set_send(int fd, struct sockaddr_un *addr, unsigned int addr_len,
 	char *amsg_buf;
 	int i;
 
-	strncpy(msg.aux.obj_name, aux->obj, sizeof(msg.aux.obj_name));
+	strlcpy(msg.aux.obj_name, aux->obj, sizeof(msg.aux.obj_name));
 	memcpy(&msg.aux.obj_st, aux->st, sizeof(msg.aux.obj_st));
 
 	cmsg_buf = bpf_map_set_init(&msg, addr, addr_len);
@@ -2682,7 +2682,7 @@ int bpf_send_map_fds(const char *path, const char *obj)
 		return -1;
 	}
 
-	strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+	strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
 
 	ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
 	if (ret < 0) {
@@ -2715,7 +2715,7 @@ int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
 		return -1;
 	}
 
-	strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+	strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
 
 	ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
 	if (ret < 0) {
Signature · 1 line
2.16.2