From f727c233c49cd3c7f94e0c15108d508f762a0ece Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 26 Jun 2023 15:01:04 +0200 Subject: [PATCH] platform: rename NMP_SYSCTL_PATHID_NETDIR() to have "_A" suffix The macro uses g_alloca(). Using alloca() is potentially dangerous. For example, it must never be used in an unbounded loop. This should be immediately obvious from the name, so we don't accidentally use them in the wrong context. All other alloca() macros should have such a prefix already. And they always have to be macros, because you couldn't use alloca() to return memory from a function. --- src/core/devices/nm-device-infiniband.c | 2 +- src/core/platform/tests/test-link.c | 4 +- src/libnm-platform/nm-linux-platform.c | 26 +++++------ src/libnm-platform/nm-platform.c | 60 +++++++++++++------------ src/libnm-platform/nm-platform.h | 4 +- 5 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/core/devices/nm-device-infiniband.c b/src/core/devices/nm-device-infiniband.c index c420b9d7cb..6ce61d0b12 100644 --- a/src/core/devices/nm-device-infiniband.c +++ b/src/core/devices/nm-device-infiniband.c @@ -87,7 +87,7 @@ act_stage1_prepare(NMDevice *device, NMDeviceStateReason *out_failure_reason) /* With some drivers the interface must be down to set transport mode */ nm_device_take_down(device, TRUE); ok = nm_platform_sysctl_set(nm_device_get_platform(device), - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_verified, "mode"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_verified, "mode"), transport_mode); nm_device_bring_up(device); diff --git a/src/core/platform/tests/test-link.c b/src/core/platform/tests/test-link.c index 5985d8953c..41a3afb3c3 100644 --- a/src/core/platform/tests/test-link.c +++ b/src/core/platform/tests/test-link.c @@ -3603,7 +3603,7 @@ test_sysctl_rename(void) ==, (gint32) nm_platform_sysctl_get_int32( PL, - NMP_SYSCTL_PATHID_NETDIR(dirfd, s ?: "", "ifindex"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, s ?: "", "ifindex"), -1)); break; } @@ -3675,7 +3675,7 @@ test_sysctl_netns_switch(void) ==, (gint32) nm_platform_sysctl_get_int32( PL, - NMP_SYSCTL_PATHID_NETDIR(dirfd, s ?: "", "ifindex"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, s ?: "", "ifindex"), -1)); g_assert_cmpint( ifindex, diff --git a/src/libnm-platform/nm-linux-platform.c b/src/libnm-platform/nm-linux-platform.c index 9181595b0d..99eab9c784 100644 --- a/src/libnm-platform/nm-linux-platform.c +++ b/src/libnm-platform/nm-linux-platform.c @@ -8669,10 +8669,10 @@ link_supports_sriov(NMPlatform *platform, int ifindex) if (dirfd < 0) return FALSE; - num = - nm_platform_sysctl_get_int32(platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_numvfs"), - -1); + num = nm_platform_sysctl_get_int32( + platform, + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_numvfs"), + -1); return num != -1; } @@ -8806,7 +8806,7 @@ link_set_sriov_params_async(NMPlatform *platform, total = nm_platform_sysctl_get_int_checked( platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_totalvfs"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_totalvfs"), 10, 0, G_MAXUINT, @@ -8824,14 +8824,14 @@ link_set_sriov_params_async(NMPlatform *platform, */ current_num = nm_platform_sysctl_get_int_checked( platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_numvfs"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_numvfs"), 10, 0, G_MAXUINT, -1); current_autoprobe = nm_platform_sysctl_get_int_checked( platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_drivers_autoprobe"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_drivers_autoprobe"), 10, 0, 1, @@ -8851,7 +8851,7 @@ link_set_sriov_params_async(NMPlatform *platform, && current_autoprobe != autoprobe && !nm_platform_sysctl_set( platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_drivers_autoprobe"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_drivers_autoprobe"), nm_sprintf_buf(buf, "%d", (int) autoprobe))) { g_set_error(&error, NM_UTILS_ERROR, @@ -8873,7 +8873,7 @@ link_set_sriov_params_async(NMPlatform *platform, values[i++] = NULL; sysctl_set_async(platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_numvfs"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "device/sriov_numvfs"), values, callback, data, @@ -9109,7 +9109,7 @@ link_get_physical_port_id(NMPlatform *platform, int ifindex) dirfd = nm_platform_sysctl_open_netdir(platform, ifindex, ifname_verified); if (dirfd < 0) return NULL; - return sysctl_get(platform, NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_verified, "phys_port_id")); + return sysctl_get(platform, NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_verified, "phys_port_id")); } static guint @@ -9123,7 +9123,7 @@ link_get_dev_id(NMPlatform *platform, int ifindex) return 0; return nm_platform_sysctl_get_int_checked( platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_verified, "dev_id"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_verified, "dev_id"), 16, 0, G_MAXUINT16, @@ -9373,12 +9373,12 @@ _infiniband_partition_action(NMPlatform *platform, if (action == INFINIBAND_ACTION_CREATE_CHILD) success = nm_platform_sysctl_set(platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_parent, "create_child"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_parent, "create_child"), id); else success = nm_platform_sysctl_set(platform, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_parent, "delete_child"), + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_parent, "delete_child"), id); if (!success) { diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index 165b9f0f5d..041354cf44 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -2771,7 +2771,7 @@ link_set_option(NMPlatform *self, path = nm_sprintf_buf_unsafe_a(strlen(category) + strlen(option) + 2, "%s/%s", category, option); return nm_platform_sysctl_set(self, - NMP_SYSCTL_PATHID_NETDIR_unsafe(dirfd, ifname_verified, path), + NMP_SYSCTL_PATHID_NETDIR_UNSAFE_A(dirfd, ifname_verified, path), value); } @@ -2792,7 +2792,7 @@ link_get_option(NMPlatform *self, int ifindex, const char *category, const char path = nm_sprintf_buf_unsafe_a(strlen(category) + strlen(option) + 2, "%s/%s", category, option); return nm_platform_sysctl_get(self, - NMP_SYSCTL_PATHID_NETDIR_unsafe(dirfd, ifname_verified, path)); + NMP_SYSCTL_PATHID_NETDIR_UNSAFE_A(dirfd, ifname_verified, path)); } static const char * @@ -3073,7 +3073,7 @@ nm_platform_link_infiniband_get_properties(NMPlatform *self, return FALSE; contents = - nm_platform_sysctl_get(self, NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_verified, "mode")); + nm_platform_sysctl_get(self, NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_verified, "mode")); if (!contents) return FALSE; if (strstr(contents, "datagram")) @@ -3084,13 +3084,13 @@ nm_platform_link_infiniband_get_properties(NMPlatform *self, mode = NULL; g_free(contents); - p_key = - nm_platform_sysctl_get_int_checked(self, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname_verified, "pkey"), - 16, - 0, - 0xFFFF, - -1); + p_key = nm_platform_sysctl_get_int_checked( + self, + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname_verified, "pkey"), + 16, + 0, + 0xFFFF, + -1); if (p_key < 0) return FALSE; @@ -3192,31 +3192,33 @@ nm_platform_link_tun_get_properties(NMPlatform *self, int ifindex, NMPlatformLnk if (dirfd < 0) return FALSE; - owner = nm_platform_sysctl_get_int_checked(self, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "owner"), - 10, - -1, - G_MAXUINT32, - -2); + owner = + nm_platform_sysctl_get_int_checked(self, + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "owner"), + 10, + -1, + G_MAXUINT32, + -2); if (owner == -2) return FALSE; - group = nm_platform_sysctl_get_int_checked(self, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "group"), - 10, - -1, - G_MAXUINT32, - -2); + group = + nm_platform_sysctl_get_int_checked(self, + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "group"), + 10, + -1, + G_MAXUINT32, + -2); if (group == -2) return FALSE; - flags = - nm_platform_sysctl_get_int_checked(self, - NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "tun_flags"), - 16, - 0, - G_MAXINT64, - -1); + flags = nm_platform_sysctl_get_int_checked( + self, + NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, "tun_flags"), + 16, + 0, + G_MAXINT64, + -1); if (flags == -1) return FALSE; } diff --git a/src/libnm-platform/nm-platform.h b/src/libnm-platform/nm-platform.h index 8480ef1ca3..83247f2ec6 100644 --- a/src/libnm-platform/nm-platform.h +++ b/src/libnm-platform/nm-platform.h @@ -1534,7 +1534,7 @@ const char *nm_link_type_to_string(NMLinkType link_type); #define NMP_SYSCTL_PATHID_ABSOLUTE(path) ((const char *) NULL), -1, (path) -#define NMP_SYSCTL_PATHID_NETDIR_unsafe(dirfd, ifname, path) \ +#define NMP_SYSCTL_PATHID_NETDIR_UNSAFE_A(dirfd, ifname, path) \ nm_sprintf_buf_unsafe_a(NM_STRLEN("net:/sys/class/net//\0") + NM_IFNAMSIZ + ({ \ const gsize _l = strlen(path); \ \ @@ -1546,7 +1546,7 @@ const char *nm_link_type_to_string(NMLinkType link_type); (path)), \ (dirfd), (path) -#define NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, path) \ +#define NMP_SYSCTL_PATHID_NETDIR_A(dirfd, ifname, path) \ nm_sprintf_bufa(NM_STRLEN("net:/sys/class/net//" path "/\0") + NM_IFNAMSIZ, \ "net:/sys/class/net/%s/%s", \ (ifname), \