From b069da90d67b49dce041f513a3855b8da3d82f80 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 3 Jan 2009 22:06:10 +0100 Subject: [PATCH] Consistently allocate device and iface variables on the heap. This fixes a segfault when no Device has been specified and tinc exits, and it would try to free() a static string. Thanks to Borg for spottin. --- src/bsd/device.c | 11 ++++++----- src/cygwin/device.c | 2 +- src/linux/device.c | 11 ++++++----- src/mingw/device.c | 4 ++-- src/raw_socket/device.c | 12 ++++++------ src/solaris/device.c | 5 +++-- src/uml_socket/device.c | 4 ++-- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/bsd/device.c b/src/bsd/device.c index 1d4aeee5..01b7071a 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -27,6 +27,7 @@ #include "net.h" #include "route.h" #include "utils.h" +#include "xalloc.h" #define DEFAULT_DEVICE "/dev/tun0" @@ -37,9 +38,9 @@ typedef enum device_type { } device_type_t; int device_fd = -1; -char *device; -char *iface; -char *device_info; +char *device = NULL; +char *iface = NULL; +static char *device_info = NULL; static int device_total_in = 0; static int device_total_out = 0; #if defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) @@ -54,10 +55,10 @@ bool setup_device(void) { cp(); if(!get_config_string(lookup_config(config_tree, "Device"), &device)) - device = DEFAULT_DEVICE; + device = xstrdup(DEFAULT_DEVICE); if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) - iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; + iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device); if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); diff --git a/src/cygwin/device.c b/src/cygwin/device.c index 3be59ce9..acb0cfae 100644 --- a/src/cygwin/device.c +++ b/src/cygwin/device.c @@ -38,7 +38,7 @@ int device_fd = -1; static HANDLE device_handle = INVALID_HANDLE_VALUE; char *device = NULL; char *iface = NULL; -char *device_info = NULL; +static char *device_info = NULL; static int device_total_in = 0; static int device_total_out = 0; diff --git a/src/linux/device.c b/src/linux/device.c index 302fa628..87f72ea2 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -34,6 +34,7 @@ #include "net.h" #include "route.h" #include "utils.h" +#include "xalloc.h" typedef enum device_type_t { DEVICE_TYPE_ETHERTAP, @@ -45,8 +46,8 @@ int device_fd = -1; static device_type_t device_type; char *device; char *iface; -char ifrname[IFNAMSIZ]; -char *device_info; +static char ifrname[IFNAMSIZ]; +static char *device_info; static int device_total_in = 0; static int device_total_out = 0; @@ -58,13 +59,13 @@ bool setup_device(void) cp(); if(!get_config_string(lookup_config(config_tree, "Device"), &device)) - device = DEFAULT_DEVICE; + device = xstrdup(DEFAULT_DEVICE); if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) #ifdef HAVE_LINUX_IF_TUN_H - iface = netname; + iface = xstrdup(netname); #else - iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; + iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device); #endif device_fd = open(device, O_RDWR | O_NONBLOCK); diff --git a/src/mingw/device.c b/src/mingw/device.c index 2813a883..5494796a 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -38,7 +38,7 @@ int device_fd = 0; static HANDLE device_handle = INVALID_HANDLE_VALUE; char *device = NULL; char *iface = NULL; -char *device_info = NULL; +static char *device_info = NULL; static int device_total_in = 0; static int device_total_out = 0; @@ -52,7 +52,7 @@ static struct packetbuf { static int nbufs = 64; -DWORD WINAPI tapreader(void *bla) { +static DWORD WINAPI tapreader(void *bla) { int sock, err, status; struct addrinfo *ai; struct addrinfo hint = { diff --git a/src/raw_socket/device.c b/src/raw_socket/device.c index 33857ba1..b96f06f4 100644 --- a/src/raw_socket/device.c +++ b/src/raw_socket/device.c @@ -29,12 +29,13 @@ #include "logger.h" #include "utils.h" #include "route.h" +#include "xalloc.h" int device_fd = -1; char *device; char *iface; -char ifrname[IFNAMSIZ]; -char *device_info; +static char ifrname[IFNAMSIZ]; +static char *device_info; static int device_total_in = 0; static int device_total_out = 0; @@ -46,12 +47,11 @@ bool setup_device(void) cp(); - if(!get_config_string - (lookup_config(config_tree, "Interface"), &iface)) - iface = "eth0"; + if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) + iface = xstrdup("eth0"); if(!get_config_string(lookup_config(config_tree, "Device"), &device)) - device = iface; + device = xstrdup(iface); device_info = _("raw socket"); diff --git a/src/solaris/device.c b/src/solaris/device.c index 9c2a762a..04a36a85 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -31,13 +31,14 @@ #include "logger.h" #include "net.h" #include "utils.h" +#include "xalloc.h" #define DEFAULT_DEVICE "/dev/tun" int device_fd = -1; char *device = NULL; char *iface = NULL; -char *device_info = NULL; +static char *device_info = NULL; static int device_total_in = 0; static int device_total_out = 0; @@ -51,7 +52,7 @@ bool setup_device(void) cp(); if(!get_config_string(lookup_config(config_tree, "Device"), &device)) - device = DEFAULT_DEVICE; + device = xstrdup(DEFAULT_DEVICE); if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) { logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno)); diff --git a/src/uml_socket/device.c b/src/uml_socket/device.c index a7a70fb3..f81c88f6 100644 --- a/src/uml_socket/device.c +++ b/src/uml_socket/device.c @@ -36,9 +36,9 @@ static int request_fd = -1; static int data_fd = -1; static int write_fd = -1; static int state = 0; -char *device; +char *device = NULL; char *iface = NULL; -char *device_info; +static char *device_info; extern char *identname; extern bool running; -- 2.20.1