diff options
author | Thomas Haller <thaller@redhat.com> | 2022-08-31 09:33:06 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-08-31 09:42:23 +0200 |
commit | 4c48864972378c0b97b15914ee7039bbce8e7274 (patch) | |
tree | 34a6780b77187d154daceccd1608031113201240 | |
parent | ff7c5f402424ec4d6ae2e3d61762a4c64b99b903 (diff) | |
download | NetworkManager-4c48864972378c0b97b15914ee7039bbce8e7274.tar.gz |
initrd: avoid duplicate file check and NULL pointer dereference in nmi_ibft_read()
- move the second g_file_test() inside the if-block. No need to check
twice, if the file exists.
- load_one_nic() can return NULL. Use nm_g_hash_table_lookup() to avoid
NULL pointer assertion.
- use cleanup attribute for "nic" variable, and explicitly pass
ownership on with g_steal_pointer().
-rw-r--r-- | src/nm-initrd-generator/nmi-ibft-reader.c | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/nm-initrd-generator/nmi-ibft-reader.c b/src/nm-initrd-generator/nmi-ibft-reader.c index 37e2a20c68..f7145c9946 100644 --- a/src/nm-initrd-generator/nmi-ibft-reader.c +++ b/src/nm-initrd-generator/nmi-ibft-reader.c @@ -80,7 +80,7 @@ nmi_ibft_read(const char *sysfs_dir) gs_free char *ibft_path = NULL; GDir *ibft_dir; const char *dir_name; - GHashTable *ibft, *nic; + GHashTable *ibft; char *mac; gs_free_error GError *error = NULL; @@ -93,10 +93,11 @@ nmi_ibft_read(const char *sysfs_dir) g_free, (GDestroyNotify) g_hash_table_unref); - if (!g_file_test(ibft_path, G_FILE_TEST_IS_DIR)) + if (!g_file_test(ibft_path, G_FILE_TEST_IS_DIR)) { nmp_utils_modprobe(NULL, FALSE, "iscsi_ibft", NULL); - if (!g_file_test(ibft_path, G_FILE_TEST_IS_DIR)) - return ibft; + if (!g_file_test(ibft_path, G_FILE_TEST_IS_DIR)) + return ibft; + } ibft_dir = g_dir_open(ibft_path, 0, &error); if (!ibft_dir) { @@ -105,20 +106,21 @@ nmi_ibft_read(const char *sysfs_dir) } while ((dir_name = g_dir_read_name(ibft_dir))) { + gs_unref_hashtable GHashTable *nic = NULL; + if (!g_str_has_prefix(dir_name, "ethernet")) continue; nic = load_one_nic(ibft_path, dir_name); - mac = g_hash_table_lookup(nic, "mac"); + mac = nm_g_hash_table_lookup(nic, "mac"); if (!mac) { _LOGW(LOGD_CORE, "Ignoring an iBFT record without a MAC address"); - g_hash_table_unref(nic); continue; } mac = g_ascii_strup(mac, -1); - if (!g_hash_table_insert(ibft, mac, nic)) + if (!g_hash_table_insert(ibft, mac, g_steal_pointer(&nic))) _LOGW(LOGD_CORE, "Duplicate iBFT record for %s", mac); } |