From 9ffb78c9c19ae6ed9d05fbaaac3da09e5e676b62 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 13 Jan 2021 15:41:01 +0100 Subject: [PATCH] The maximum length of an interface name is 16 This is described in POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/net_if.h.html The header shall define the following symbolic constant for the length of a buffer containing an interface name (including the terminating NULL character): IF_NAMESIZE Interface name length. Note that proper macro name is IF_NAMESIZE, IFNAMSIZ is the legacy name. The value of IF_NAMESIZE is usually 16 as far as I can tell, at least on recent Linux and macOS machines. --- src/config.h | 3 ++- src/main.c | 4 ++-- src/tunnel.c | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/config.h b/src/config.h index be534b81..1508f287 100644 --- a/src/config.h +++ b/src/config.h @@ -19,6 +19,7 @@ #define OPENFORTIVPN_CONFIG_H #include +#include #include #include @@ -92,7 +93,7 @@ struct vpn_config { unsigned int otp_delay; int no_ftm_push; char *pinentry; - char iface_name[FIELD_SIZE + 1]; + char iface_name[IF_NAMESIZE]; char realm[REALM_SIZE + 1]; int set_routes; diff --git a/src/main.c b/src/main.c index ad15332a..7412df6f 100644 --- a/src/main.c +++ b/src/main.c @@ -433,8 +433,8 @@ int main(int argc, char **argv) } if (strcmp(long_options[option_index].name, "ifname") == 0) { - strncpy(cli_cfg.iface_name, optarg, FIELD_SIZE); - cli_cfg.iface_name[FIELD_SIZE] = '\0'; + strncpy(cli_cfg.iface_name, optarg, IF_NAMESIZE - 1); + cli_cfg.iface_name[IF_NAMESIZE - 1] = '\0'; break; } if (strcmp(long_options[option_index].name, diff --git a/src/tunnel.c b/src/tunnel.c index b384b2a3..46d2d54f 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -585,7 +585,7 @@ static int tcp_connect(struct tunnel *tunnel) int ret, handle; struct sockaddr_in server; char *env_proxy; - const int iface_len = strnlen(tunnel->config->iface_name, IFNAMSIZ); + const int iface_len = strnlen(tunnel->config->iface_name, IF_NAMESIZE); handle = socket(AF_INET, SOCK_STREAM, 0); @@ -647,7 +647,7 @@ static int tcp_connect(struct tunnel *tunnel) log_debug("SO_RCVBUF: %d\n", ret); #endif - if (iface_len == IFNAMSIZ) { + if (iface_len == IF_NAMESIZE) { log_error("socket: Too long iface name\n"); goto err_post_socket; } @@ -659,8 +659,8 @@ static int tcp_connect(struct tunnel *tunnel) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); - if (strlcpy(ifr.ifr_name, tunnel->config->iface_name, IFNAMSIZ) - >= IFNAMSIZ) { + if (strlcpy(ifr.ifr_name, tunnel->config->iface_name, IF_NAMESIZE) + >= IF_NAMESIZE) { log_error("interface name too long\n"); goto err_post_socket; }