settings: validate hostnames from D-Bus (bgo #711179)

Do some minimal verification of hostnames that come in via D-Bus, for
length and content.  Otherwise we'd get as far as asking glibc to set
the system hostname, which would reject us.
This commit is contained in:
Dan Williams
2013-10-23 21:41:33 -05:00
parent 0c3a2b1951
commit 6a60dc2fe9
2 changed files with 36 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ typedef enum {
NM_SETTINGS_ERROR_ADD_FAILED, /*< nick=AddFailed >*/ NM_SETTINGS_ERROR_ADD_FAILED, /*< nick=AddFailed >*/
NM_SETTINGS_ERROR_SAVE_HOSTNAME_NOT_SUPPORTED, /*< nick=SaveHostnameNotSupported >*/ NM_SETTINGS_ERROR_SAVE_HOSTNAME_NOT_SUPPORTED, /*< nick=SaveHostnameNotSupported >*/
NM_SETTINGS_ERROR_SAVE_HOSTNAME_FAILED, /*< nick=SaveHostnameFailed >*/ NM_SETTINGS_ERROR_SAVE_HOSTNAME_FAILED, /*< nick=SaveHostnameFailed >*/
NM_SETTINGS_ERROR_HOSTNAME_INVALID, /*< nick=HostnameInvalid >*/
NM_SETTINGS_ERROR_UUID_EXISTS, /*< nick=UuidExists >*/ NM_SETTINGS_ERROR_UUID_EXISTS, /*< nick=UuidExists >*/
} NMSettingsError; } NMSettingsError;

View File

@@ -1356,6 +1356,33 @@ pk_hostname_cb (NMAuthChain *chain,
nm_auth_chain_unref (chain); nm_auth_chain_unref (chain);
} }
static gboolean
validate_hostname (const char *hostname)
{
const char *p;
gboolean dot = TRUE;
if (!hostname || !hostname[0])
return FALSE;
for (p = hostname; *p; p++) {
if (*p == '.') {
if (dot)
return FALSE;
dot = TRUE;
} else {
if (!g_ascii_isalnum (*p) && (*p != '-') && (*p != '_'))
return FALSE;
dot = FALSE;
}
}
if (dot)
return FALSE;
return (p - hostname <= HOST_NAME_MAX);
}
static void static void
impl_settings_save_hostname (NMSettings *self, impl_settings_save_hostname (NMSettings *self,
const char *hostname, const char *hostname,
@@ -1365,6 +1392,14 @@ impl_settings_save_hostname (NMSettings *self,
NMAuthChain *chain; NMAuthChain *chain;
GError *error = NULL; GError *error = NULL;
/* Minimal validation of the hostname */
if (!validate_hostname (hostname)) {
error = g_error_new_literal (NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_HOSTNAME_INVALID,
"The hostname was too long or contained invalid characters.");
goto done;
}
/* Do any of the plugins support setting the hostname? */ /* Do any of the plugins support setting the hostname? */
if (!get_plugin (self, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME)) { if (!get_plugin (self, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME)) {
error = g_error_new_literal (NM_SETTINGS_ERROR, error = g_error_new_literal (NM_SETTINGS_ERROR,