Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[network] Improve network interface availability checking
The buffer used for constructing sysfs path from interface name is rather
short, which can lead to false negatives due to truncation. Additionally
the actual names of the interfaces checked are not logged in case of
errors and the helper function can crash if fed null strings.

Make the check_interface() helper function tolerate null arguments
and enlarge the path expansion buffer to make truncation less likely.

In case get_interface() fails to find valid a interface name, make it
emit diagnostic message that identifies the actual names attempted.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Jul 7, 2016
1 parent 58c11bc commit 21400d5
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/usb_moded-network.c
Expand Up @@ -81,41 +81,44 @@ static void free_ipforward_data (struct ipforward_data *ipforward)
/* This function checks if the configured interface exists */
static int check_interface(char *interface)
{
char path[25];
int ret = 0;
int ret = -1;

snprintf(path, 25, "/sys/class/net/%s", interface );
ret = access(path, F_OK);
if(interface)
{
char path[256];
snprintf(path, sizeof path, "/sys/class/net/%s", interface);
ret = access(path, F_OK);
}

return(ret);
return ret;
}

static char* get_interface(struct mode_list_elem *data)
{
char *interface = NULL;
int check = 0;

interface = get_network_setting(NETWORK_INTERFACE_KEY);
check = check_interface(interface);
char *interface = 0;
char *setting = get_network_setting(NETWORK_INTERFACE_KEY);

if(check != 0)
if(check_interface(setting) == 0)
{
if(interface != NULL)
free(interface);
/* no known interface configured and existing, falling back to usb0 */
interface = malloc(sizeof(default_interface)*sizeof(char));
strncpy(interface, default_interface, sizeof(default_interface));
/* Use the configured value */
interface = setting, setting = 0;
}

check = check_interface(interface);
if(check)
else
{
log_warning("Configured interface is incorrect, nor does usb0 exists. Check your config!\n");
free(interface);
return(0);
/* Fall back to default value */
interface = strdup(default_interface);
if(check_interface(interface) != 0)
{
log_warning("Neither configured %s nor fallback %s interface exists."
" Check your config!",
setting ?: "NULL",
interface ?: "NULL");
free(interface), interface = 0;
}
}

log_debug("interface = %s\n", interface);
log_debug("interface = %s\n", interface ?: "NULL");
free(setting);
return interface;
}

Expand Down

0 comments on commit 21400d5

Please sign in to comment.