ip_watcher: Copy device name correctly

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 <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Höppner
2018-11-08 13:11:47 +01:00
parent 5a9e93fb5b
commit c675096899
2 changed files with 7 additions and 4 deletions

View File

@@ -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

View File

@@ -31,6 +31,7 @@
#include <syslog.h>
#include <unistd.h>
#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;