Skip to content

Commit b45c76e

Browse files
pmachatakuba-moo
authored andcommitted
mlxsw: Warn about invalid accesses to array fields
A forgotten or buggy variable initialization can cause out-of-bounds access to a register or other item array field. For an overflow, such access would mangle adjacent parts of the register payload. For an underflow, due to all variables being unsigned, the access would likely trample unrelated memory. Since neither is correct, replace these accesses with accesses at the index of 0, and warn about the issue. Suggested-by: Ido Schimmel <[email protected]> Signed-off-by: Petr Machata <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Reviewed-by: Przemek Kitszel <[email protected]> Link: https://patch.msgid.link/b988fb265c2f6c1206fe12d5bfdcfa188b7672d1.1720447210.git.petrm@nvidia.com Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 746d684 commit b45c76e

File tree

1 file changed

+4
-0
lines changed
  • drivers/net/ethernet/mellanox/mlxsw

1 file changed

+4
-0
lines changed

drivers/net/ethernet/mellanox/mlxsw/item.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ __mlxsw_item_bit_array_offset(const struct mlxsw_item *item,
218218
}
219219

220220
max_index = (item->size.bytes << 3) / item->element_size - 1;
221+
if (WARN_ONCE(index > max_index,
222+
"name=%s,index=%u,max_index=%u\n", item->name, index,
223+
max_index))
224+
index = 0;
221225
be_index = max_index - index;
222226
offset = be_index * item->element_size >> 3;
223227
in_byte_index = index % (BITS_PER_BYTE / item->element_size);

0 commit comments

Comments
 (0)