diff options
author | Thomas Haller <thaller@redhat.com> | 2021-01-05 14:08:24 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-01-08 16:32:48 +0100 |
commit | 511b4ab4112bf21bf0b42ccea85db228ffe720b5 (patch) | |
tree | 1bf0159d7d5a22fe6594e60b94f1a634ca2c8bf3 /clients/cloud-setup | |
parent | 2ba984a80a1a6b299692e6144c63f2068e3d21f4 (diff) | |
download | NetworkManager-511b4ab4112bf21bf0b42ccea85db228ffe720b5.tar.gz |
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.
Diffstat (limited to 'clients/cloud-setup')
-rw-r--r-- | clients/cloud-setup/nm-cloud-setup-utils.c | 11 | ||||
-rw-r--r-- | clients/cloud-setup/nm-cloud-setup-utils.h | 10 | ||||
-rw-r--r-- | clients/cloud-setup/nmcs-provider-azure.c | 2 | ||||
-rw-r--r-- | clients/cloud-setup/nmcs-provider-gcp.c | 2 |
4 files changed, 22 insertions, 3 deletions
diff --git a/clients/cloud-setup/nm-cloud-setup-utils.c b/clients/cloud-setup/nm-cloud-setup-utils.c index f09d1e5988..24b3ee6a29 100644 --- a/clients/cloud-setup/nm-cloud-setup-utils.c +++ b/clients/cloud-setup/nm-cloud-setup-utils.c @@ -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; diff --git a/clients/cloud-setup/nm-cloud-setup-utils.h b/clients/cloud-setup/nm-cloud-setup-utils.h index c2a3d30949..217f07550e 100644 --- a/clients/cloud-setup/nm-cloud-setup-utils.h +++ b/clients/cloud-setup/nm-cloud-setup-utils.h @@ -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); diff --git a/clients/cloud-setup/nmcs-provider-azure.c b/clients/cloud-setup/nmcs-provider-azure.c index a18a0d64b4..65ff058c77 100644 --- a/clients/cloud-setup/nmcs-provider-azure.c +++ b/clients/cloud-setup/nmcs-provider-azure.c @@ -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; diff --git a/clients/cloud-setup/nmcs-provider-gcp.c b/clients/cloud-setup/nmcs-provider-gcp.c index a8b50f98f7..d30749484f 100644 --- a/clients/cloud-setup/nmcs-provider-gcp.c +++ b/clients/cloud-setup/nmcs-provider-gcp.c @@ -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", |