From a6448291834ca7419553a807ee367c719c2956d0 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 6 Oct 2018 17:51:41 +0200 Subject: [PATCH] Fix compiler warnings. --- src/conf.c | 6 +++++- src/control.c | 2 ++ src/invitation.c | 12 ++++++++++-- src/linux/device.c | 2 ++ src/logger.c | 2 +- src/net_socket.c | 1 + src/raw_socket_device.c | 1 + src/tincctl.c | 36 +++++++++++++++++++++++++++++++----- src/uml_device.c | 1 + src/upnp.c | 2 +- 10 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/conf.c b/src/conf.c index 5304ab5..5706570 100644 --- a/src/conf.c +++ b/src/conf.c @@ -433,7 +433,11 @@ bool read_server_config(void) { // And we try to read the ones that end with ".conf" if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) { - snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name); + if(snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) { + logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name); + return false; + } + x = read_config_file(config_tree, fname, true); } } diff --git a/src/control.c b/src/control.c index 76f262e..beda6c4 100644 --- a/src/control.c +++ b/src/control.c @@ -201,6 +201,8 @@ bool init_control(void) { strncpy(sa_un.sun_path, unixsocketname, sizeof(sa_un.sun_path)); + sa_un.sun_path[sizeof(sa_un.sun_path) - 1] = 0; + if(connect(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un)) >= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname); return false; diff --git a/src/invitation.c b/src/invitation.c index c059ca3..4c8597a 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -353,7 +353,11 @@ int cmd_invite(int argc, char *argv[]) { char invname[PATH_MAX]; struct stat st; - snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name); + + if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", filename, ent->d_name); + continue; + } if(!stat(invname, &st)) { if(deadline < st.st_mtime) { @@ -955,7 +959,11 @@ ask_netname: line[strlen(line) - 1] = 0; char newbase[PATH_MAX]; - snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line); + + if(snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) { + fprintf(stderr, "Filename too long: " CONFDIR SLASH "tinc" SLASH "%s\n", line); + goto ask_netname; + } if(rename(confbase, newbase)) { fprintf(stderr, "Error trying to rename %s to %s: %s\n", confbase, newbase, strerror(errno)); diff --git a/src/linux/device.c b/src/linux/device.c index 2c40ead..0abafa9 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -103,10 +103,12 @@ static bool setup_device(void) { if(iface) { strncpy(ifr.ifr_name, iface, IFNAMSIZ); + ifr.ifr_name[IFNAMSIZ - 1] = 0; } if(!ioctl(device_fd, TUNSETIFF, &ifr)) { strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); + ifrname[IFNAMSIZ - 1] = 0; free(iface); iface = xstrdup(ifrname); } else { diff --git a/src/logger.c b/src/logger.c index 7fb1629..bcadae1 100644 --- a/src/logger.c +++ b/src/logger.c @@ -186,7 +186,7 @@ void openlogger(const char *ident, logmode_t mode) { loghandle = RegisterEventSource(NULL, logident); if(!loghandle) { - fprintf(stderr, "Could not open log handle!"); + fprintf(stderr, "Could not open log handle!\n"); logmode = LOGMODE_NULL; } diff --git a/src/net_socket.c b/src/net_socket.c index 15d32db..75b20ec 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -210,6 +210,7 @@ int setup_listen_socket(const sockaddr_t *sa) { memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); + ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0; if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) { closesocket(nfd); diff --git a/src/raw_socket_device.c b/src/raw_socket_device.c index 3ec8e57..02f6afa 100644 --- a/src/raw_socket_device.c +++ b/src/raw_socket_device.c @@ -60,6 +60,7 @@ static bool setup_device(void) { #endif strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); + ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0; if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) { close(device_fd); diff --git a/src/tincctl.c b/src/tincctl.c index 7244779..2fdab2a 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -355,6 +355,8 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo char buf[PATH_MAX]; char buf2[PATH_MAX]; +ask_filename: + /* Check stdin and stdout */ if(ask && tty) { /* Ask for a file and/or directory name. */ @@ -385,7 +387,17 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo #endif /* The directory is a relative path or a filename. */ getcwd(directory, sizeof(directory)); - snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename); + + if(snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename) >= sizeof(buf2)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", directory, filename); + + if(ask && tty) { + goto ask_filename; + } else { + return NULL; + } + } + filename = buf2; } @@ -825,6 +837,8 @@ bool connect_tincd(bool verbose) { strncpy(sa.sun_path, unixsocketname, sizeof(sa.sun_path)); + sa.sun_path[sizeof(sa.sun_path) - 1] = 0; + fd = socket(AF_UNIX, SOCK_STREAM, 0); if(fd < 0) { @@ -1161,7 +1175,12 @@ static int dump_invitations(void) { } char fname[PATH_MAX]; - snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name); + + if(snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name) >= sizeof(fname)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", dname, ent->d_name); + continue; + } + FILE *f = fopen(fname, "r"); if(!f) { @@ -1930,7 +1949,11 @@ static int cmd_config(int argc, char *argv[]) { FILE *tf = NULL; if(action >= -1) { - snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename); + if(snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename) >= sizeof(tmpfile)) { + fprintf(stderr, "Filename too long: %s.config.tmp\n", filename); + return 1; + } + tf = fopen(tmpfile, "w"); if(!tf) { @@ -2537,7 +2560,10 @@ static int cmd_import(int argc, char *argv[]) { fclose(out); } - snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name); + if(snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name) >= sizeof(filename)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, name); + return 1; + } if(!force && !access(filename, F_OK)) { fprintf(stderr, "Host configuration file %s already exists, skipping.\n", filename); @@ -3264,7 +3290,7 @@ int main(int argc, char *argv[]) { static struct WSAData wsa_state; if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) { - fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError())); + fprintf(stderr, "System call `%s' failed: %s\n", "WSAStartup", winerror(GetLastError())); return false; } diff --git a/src/uml_device.c b/src/uml_device.c index 38ebd6f..be60911 100644 --- a/src/uml_device.c +++ b/src/uml_device.c @@ -133,6 +133,7 @@ static bool setup_device(void) { listen_sun.sun_family = AF_UNIX; strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path)); + listen_sun.sun_path[sizeof(listen_sun.sun_path) - 1] = 0; if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno)); diff --git a/src/upnp.c b/src/upnp.c index 3e90210..5149714 100644 --- a/src/upnp.c +++ b/src/upnp.c @@ -61,7 +61,7 @@ static struct UPNPDev *upnp_discover(int delay, int *error) { #else -#if MINIUPNPC_API_VERSION > 15 +#if MINIUPNPC_API_VERSION > 17 #warning "The version of libminiupnpc you're building against seems to be too recent. Expect trouble." #endif -- 2.25.1