[PATCH 11/32] nl80211: Use mem_to_flex_dup() with struct cfg80211_cqm_config

Kees Cook
2022-05-04T01:47:41Z

As part of the work to perform bounds checking on all memcpy() uses,
replace the open-coded a deserialization of bytes out of memory into a
trailing flexible array by using a flex_array.h helper to perform the
allocation, bounds checking, and copying.

Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: linux-wireless@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: Eric Dumazet <edumazet@google.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
net/wireless/core.h | 4 ++--
net/wireless/nl80211.c | 15 ++++-----------
2 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/net/wireless/core.h b/net/wireless/core.h
index 3a7dbd63d8c6..899d111993c6 100644
--- a/net/wireless/core.h
+++ b/net/wireless/core.h
@@ -295,8 +295,8 @@ struct cfg80211_beacon_registration {
struct cfg80211_cqm_config {
u32 rssi_hyst;
s32 last_rssi_event_value;
- int n_rssi_thresholds;
- s32 rssi_thresholds[];
+ DECLARE_FLEX_ARRAY_ELEMENTS_COUNT(int, n_rssi_thresholds);
+ DECLARE_FLEX_ARRAY_ELEMENTS(s32, rssi_thresholds);
};

void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev);
diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
index 945ed87d12e0..70df7132cce8 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -12096,21 +12096,14 @@ static int nl80211_set_cqm_rssi(struct genl_info *info,

wdev_lock(wdev);
if (n_thresholds) {
- struct cfg80211_cqm_config *cqm_config;
+ struct cfg80211_cqm_config *cqm_config = NULL;

- cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
- n_thresholds),
- GFP_KERNEL);
- if (!cqm_config) {
- err = -ENOMEM;
+ err = mem_to_flex_dup(&cqm_config, thresholds, n_thresholds,
+ GFP_KERNEL);
+ if (err)
goto unlock;
- }

cqm_config->rssi_hyst = hysteresis;
- cqm_config->n_rssi_thresholds = n_thresholds;
- memcpy(cqm_config->rssi_thresholds, thresholds,
- flex_array_size(cqm_config, rssi_thresholds,
- n_thresholds));

wdev->cqm_config = cqm_config;
}
--
2.32.0