print_eht_capability() takes the HE PHY capabilities from the HE Capabilities element of the same scan result. Without one, he_cap is NULL and iw crashes. The MCS fields are read straight from the element, so a short EHT element is read past its end. Copy the fields into zeroed buffers, as print_eht_info() does, and treat missing HE capabilities as zero. init_context() also reads the extension ID of an empty extension element and keeps an HE element of any length. Skip empty extension elements and keep only an HE element long enough to be printed. Fixes: a6ad3f11ead1 ("iw: scan: add eht capability parsing") Signed-off-by: Josef Schlehofer --- Checked with an AddressSanitizer build that feeds print_ies() crafted elements in exactly sized buffers. On master, an EHT element without an HE element crashes, and a short EHT element, a one-byte HE element and an empty extension element at the end are read past their end. With the patch they are clean, and the output for valid elements (80, 160 and 320 MHz from an AP, 20 MHz-only STA, 30 bytes) does not change. Without an HE element the MCS maps are now printed as if HE reported no wider channels. I can skip them instead if you prefer. It does not depend on my earlier patch "iw: util: fix the EHT PHY capabilities buffer size", both apply in either order. scan.c | 5 ++++- util.c | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/scan.c b/scan.c index 871f72f..8208dcc 100644 --- a/scan.c +++ b/scan.c @@ -2524,9 +2524,12 @@ static void init_context(struct ie_context *ctx, ctx->is_vht_cap = true; break; case EID_EXTENSION: + if (pos[1] < 1) + break; switch (pos[2]) { case EID_EXT_HE_CAPABILITY: - ctx->he_cap = pos + 3; + if (pos[1] - 1 >= ext_printers[EID_EXT_HE_CAPABILITY].minlen) + ctx->he_cap = pos + 3; break; } break; diff --git a/util.c b/util.c index 1bad4ea..4a1cfcf 100644 --- a/util.c +++ b/util.c @@ -1982,23 +1982,25 @@ void print_uhr_info(struct nlattr *nl_iftype) void print_eht_capability(const uint8_t *ie, int len, const uint8_t *he_cap, bool from_ap) { - const void *mac_cap, *phy_cap, *mcs_set, *he_phy_cap; + __u8 mac_cap[2] = { 0 }; + __u32 phy_cap[3] = { 0 }; + __u8 mcs_set[19] = { 0 }; /* up to 30 bytes minus the 11 fixed ones */ + __u16 he_phy_cap[6] = { 0 }; int mcs_len; - int i = 0; - mac_cap = &ie[i]; - i += 2; + memcpy(mac_cap, ie, sizeof(mac_cap)); + memcpy(phy_cap, &ie[2], 9); - phy_cap = &ie[i]; - i += 9; - - mcs_set = &ie[i]; - mcs_len = len - i; + mcs_len = len - 11; + if (mcs_len > (int)sizeof(mcs_set)) + mcs_len = sizeof(mcs_set); + memcpy(mcs_set, &ie[11], mcs_len); - he_phy_cap = &he_cap[6]; + if (he_cap) + memcpy(&((__u8 *)he_phy_cap)[1], &he_cap[6], 11); __print_eht_capa(NL80211_BAND_6GHZ, mac_cap, phy_cap, mcs_set, mcs_len, - NULL, 0, he_phy_cap - 1, from_ap, false); + NULL, 0, he_phy_cap, from_ap, false); } void print_he_capability(const uint8_t *ie, int len) -- 2.54.0 (Apple Git-157)