cloud-setup: add and use nmcs_utils_hwaddr_normalize_gbytes()

Previously we would call

    nmcs_utils_hwaddr_normalize(g_bytes_get_data(response, NULL), -1);

which treats the data in response as NUL terminated. That is not
entirely wrong, because the HTTP request's response is guaranteed
to have a NUL termination at the end. However, it doesn't seam to good
either.

For one, we already have the length. Use it. But also, if the response
contains any NUL bytes in the middle, then this would wrongly only
consider the first line. We should not accept "00:11:22:33:44:55\0bogus"
as valid.

While at it, reject NUL characters from nmcs_utils_hwaddr_normalize() --
except one NUL at the end.
This commit is contained in:
Thomas Haller 2021-01-05 14:08:24 +01:00
parent 2ba984a80a
commit 511b4ab411
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
4 changed files with 22 additions and 3 deletions

View file

@ -495,8 +495,17 @@ nmcs_utils_hwaddr_normalize(const char *hwaddr, gssize len)
if (!hwaddr)
return NULL;
l = strlen(hwaddr);
} else
} else {
l = len;
if (l > 0 && hwaddr[l - 1] == '\0') {
/* we accept one '\0' at the end of the string. */
l--;
}
if (memchr(hwaddr, '\0', l)) {
/* but we don't accept other NUL characters in the middle. */
return NULL;
}
}
if (l == 0)
return NULL;

View file

@ -78,6 +78,16 @@ gboolean nmcs_utils_poll_finish(GAsyncResult *result, gpointer *probe_user_data,
char *nmcs_utils_hwaddr_normalize(const char *hwaddr, gssize len);
static inline char *
nmcs_utils_hwaddr_normalize_gbytes(GBytes *hwaddr)
{
const char *str;
gsize len;
str = g_bytes_get_data(hwaddr, &len);
return nmcs_utils_hwaddr_normalize(str, len);
}
/*****************************************************************************/
const char *nmcs_utils_parse_memmem(GBytes *mem, const char *needle);

View file

@ -315,7 +315,7 @@ _get_config_iface_cb(GObject *source, GAsyncResult *result, gpointer user_data)
if (error)
goto out_done;
iface_data->hwaddr = nmcs_utils_hwaddr_normalize(g_bytes_get_data(response, NULL), -1);
iface_data->hwaddr = nmcs_utils_hwaddr_normalize_gbytes(response);
if (!iface_data->hwaddr)
goto out_done;

View file

@ -261,7 +261,7 @@ _get_config_iface_cb(GObject *source, GAsyncResult *result, gpointer user_data)
if (error)
goto out_error;
hwaddr = nmcs_utils_hwaddr_normalize(g_bytes_get_data(response, NULL), -1);
hwaddr = nmcs_utils_hwaddr_normalize_gbytes(response);
iface_data->iface_get_config = g_hash_table_lookup(get_config_data->result_dict, hwaddr);
if (!iface_data->iface_get_config) {
_LOGI("GCP interface[%" G_GSSIZE_FORMAT "]: did not find a matching device",