From c67509689979ba44f2c0b4fadbd6f920d1e10906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B6ppner?= Date: Thu, 8 Nov 2018 13:11:47 +0100 Subject: [PATCH] ip_watcher: Copy device name correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DEV_NAME_SIZE is defined as IFNAMSIZ. IFNAMSIZ is the maximum buffer size for an interface name, including its terminating null byte. [1] The buffers dev_name and ifr_name are both defined with a size of DEV_NAME_SIZE and IFNAMSIZ respectively. Given these facts, only a size of 'IFNAMSIZ - 1' should be copied and the destination string should then be null-terminated properly. Use util_strlcpy() to correctly copy the strings and rid of the followin GCC8 compile warnings: xcec-bridge.c: In function ‘open_incoming_socket’: xcec-bridge.c:94:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-tru ncation] ..strncpy(if_req.ifr_name,dev_name,DEV_NAME_LEN); ..^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ xcec-bridge.c: In function ‘read_sys’: xcec-bridge.c:284:4: warning: ‘strncpy’ output may be truncated copying 16 bytes from a string of length 255 [-Wstringop-truncation] ....strncpy(is->dev_name, if_name, DEV_NAME_LEN); ....^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1]: https://www.gnu.org/software/libc/manual/html_node/Interface-Naming.html Signed-off-by: Jan Höppner --- ip_watcher/Makefile | 4 +++- ip_watcher/xcec-bridge.c | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ip_watcher/Makefile b/ip_watcher/Makefile index 0e9df39e..22f93bfb 100644 --- a/ip_watcher/Makefile +++ b/ip_watcher/Makefile @@ -2,7 +2,9 @@ include ../common.mak all: xcec-bridge -xcec-bridge: xcec-bridge.o +libs = $(rootdir)/libutil/libutil.a + +xcec-bridge: xcec-bridge.o $(libs) clean: rm -f *.o core xcec-bridge diff --git a/ip_watcher/xcec-bridge.c b/ip_watcher/xcec-bridge.c index d342428e..c774fc48 100644 --- a/ip_watcher/xcec-bridge.c +++ b/ip_watcher/xcec-bridge.c @@ -31,6 +31,7 @@ #include #include +#include "lib/util_libc.h" #include "lib/zt_common.h" /* a signal causes the interfaces to be re-checked */ @@ -91,7 +92,7 @@ int open_incoming_socket(char *dev_name) return -1; } - strncpy(if_req.ifr_name,dev_name,DEV_NAME_SIZE); + util_strlcpy(if_req.ifr_name, dev_name, DEV_NAME_SIZE); retval=ioctl(fd,SIOCGIFINDEX,&if_req); if (retval==-1) { syslog(LOG_ERR,"can't ioctl on raw packet socket, " \ @@ -281,7 +282,7 @@ int read_sys(struct int_sock **nlist) do_broadcast_bridging=0; is->mtu_warning=0; - strncpy(is->dev_name, if_name, DEV_NAME_SIZE); + util_strlcpy(is->dev_name, if_name, DEV_NAME_SIZE); if (!strncmp(if_name,"hsi",3)) { is->features=I_S_FEATURE_PASSTHROUGH; } @@ -360,7 +361,7 @@ void update_interfaces() continue; } - strncpy(new_int->dev_name,i->dev_name,DEV_NAME_SIZE); + util_strlcpy(new_int->dev_name, i->dev_name, DEV_NAME_SIZE); new_int->i_fd=i_fd; new_int->o_fd=o_fd; new_int->features=i->features;