Skip to content

Commit

Permalink
[usb-moded] Validate ip in case bad values get fed in
Browse files Browse the repository at this point in the history
It seems possible to feed bad ip's over dbus, which causes errors when setting
up the network and udhcpd. Also fixed a potential crash when updating the ip.

Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Apr 20, 2014
1 parent 51b35ae commit 00377f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/usb_moded-config.c
Expand Up @@ -44,6 +44,22 @@ static int get_conf_int(const gchar *entry, const gchar *key);
static const char * get_conf_string(const gchar *entry, const gchar *key);
static const char * get_kcmdline_string(const char *entry);

static int validate_ip(const char *ipadd)
{
unsigned int b1, b2, b3, b4;
unsigned char c;

if (sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
return(-1);

if ((b1 | b2 | b3 | b4) > 255)
return(-1);
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
return(-1);
/* all ok */
return 0;
}

const char *find_mounts(void)
{

Expand Down Expand Up @@ -124,7 +140,8 @@ const char * get_network_ip(void)
{
const char * ip = get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
return(ip);
if(!validate_ip(ip))
return(ip);

return(get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY));
}
Expand Down Expand Up @@ -362,13 +379,21 @@ int set_mode_setting(const char *mode)
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}

/*
* @param config : the key to be set
* @param setting : The value to be set
*/
int set_network_setting(const char *config, const char *setting)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
int ret = 0;
gchar *keyfile;

if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
if(validate_ip(setting) != 0)
return(1);

settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
Expand Down
2 changes: 2 additions & 0 deletions src/usb_moded-network.c
Expand Up @@ -746,6 +746,8 @@ int usb_network_update(void)
return(0);

data = get_usb_mode_data();
if(data == NULL)
return(0);
if(data->network)
{
usb_network_down(data);
Expand Down

0 comments on commit 00377f1

Please sign in to comment.