mlxsw: Annotate structs with __counted_by
Subsystems (3)
cover mlxsw: Annotate structs with __counted_by
Hi,
This annotates several mlxsw structures with the coming __counted_by attribute for bounds checking of flexible arrays at run-time. For more details, see commit dd06e72e68bc ("Compiler Attributes: Add __counted_by macro").
Thanks!
-Kees
Kees Cook (5):
mlxsw: Annotate struct mlxsw_linecards with __counted_by
mlxsw: core: Annotate struct mlxsw_env with __counted_by
mlxsw: spectrum: Annotate struct mlxsw_sp_counter_pool with
__counted_by
mlxsw: spectrum_router: Annotate struct mlxsw_sp_nexthop_group_info
with __counted_by
mlxsw: spectrum_span: Annotate struct mlxsw_sp_span with __counted_by
drivers/net/ethernet/mellanox/mlxsw/core.h | 2 +- drivers/net/ethernet/mellanox/mlxsw/core_env.c | 2 +- drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c | 2 +- drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c | 2 +- drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/5 mlxsw: Annotate struct mlxsw_linecards with __counted_by
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).
As found with Coccinelle[1], add __counted_by for struct mlxsw_linecards.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
--- drivers/net/ethernet/mellanox/mlxsw/core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlxsw/core.h +1 -1
@@ -624,7 +624,7 @@ struct mlxsw_linecards {struct mlxsw_linecard_types_info *types_info;struct list_head event_ops_list;struct mutex event_ops_list_lock;/* Locks accesses to event ops list */ -struct mlxsw_linecard linecards[]; + struct mlxsw_linecard linecards[] __counted_by(count); }; static inline struct mlxsw_linecard *
Signature · 1 line
2.34.1
Re: [PATCH 1/5] mlxsw: Annotate struct mlxsw_linecards with __counted_by
Quoted 10 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_linecards.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
PATCH 2/5 mlxsw: core: Annotate struct mlxsw_env with __counted_by
In reply to Kees CookPrepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).
As found with Coccinelle[1], add __counted_by for struct mlxsw_env.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
--- drivers/net/ethernet/mellanox/mlxsw/core_env.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlxsw/core_env.c +1 -1
@@ -34,7 +34,7 @@ struct mlxsw_env {u8 num_of_slots;/* Including the main board. */ u8 max_eeprom_len;/* Maximum module EEPROM transaction length. */ struct mutex line_cards_lock;/* Protects line cards. */ -struct mlxsw_env_line_card *line_cards[]; + struct mlxsw_env_line_card *line_cards[] __counted_by(num_of_slots); }; static bool __mlxsw_env_linecard_is_active(struct mlxsw_env *mlxsw_env,
Signature · 1 line
2.34.1
Re: [PATCH 2/5] mlxsw: core: Annotate struct mlxsw_env with __counted_by
Quoted 10 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_env.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
PATCH 3/5 mlxsw: spectrum: Annotate struct mlxsw_sp_counter_pool with __counted_by
In reply to Kees CookPrepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).
As found with Coccinelle[1], add __counted_by for struct mlxsw_sp_counter_pool.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
--- drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c +1 -1
@@ -24,7 +24,7 @@ struct mlxsw_sp_counter_pool {spinlock_t counter_pool_lock;/* Protects counter pool allocations */ atomic_t active_entries_count;unsigned int sub_pools_count; -struct mlxsw_sp_counter_sub_pool sub_pools[]; + struct mlxsw_sp_counter_sub_pool sub_pools[] __counted_by(sub_pools_count); };static const struct mlxsw_sp_counter_sub_pool mlxsw_sp_counter_sub_pools[] = {
Signature · 1 line
2.34.1
Re: [PATCH 3/5] mlxsw: spectrum: Annotate struct mlxsw_sp_counter_pool with __counted_by
Quoted 10 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_counter_pool.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Re: [PATCH 3/5] mlxsw: spectrum: Annotate struct mlxsw_sp_counter_pool with __counted_by
In reply to Kees CookQuoted 11 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_counter_pool.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 18 lines
--- drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c +1
-2@@ -24,7 +24,7 @@ struct mlxsw_sp_counter_pool {spinlock_t counter_pool_lock;/* Protects counter pool allocations */ atomic_t active_entries_count;unsigned int sub_pools_count; -struct mlxsw_sp_counter_sub_pool sub_pools[]; + struct mlxsw_sp_counter_sub_pool sub_pools[] __counted_by(sub_pools_count); };static const struct mlxsw_sp_counter_sub_pool mlxsw_sp_counter_sub_pools[] = { --
Quoted 3 lines
2.34.1
Thanks
Justin
PATCH 4/5 mlxsw: spectrum_router: Annotate struct mlxsw_sp_nexthop_group_info with __counted_by
In reply to Kees CookPrepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).
As found with Coccinelle[1], add __counted_by for struct mlxsw_sp_nexthop_group_info.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
--- drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c +1 -1
@@ -3107,7 +3107,7 @@ struct mlxsw_sp_nexthop_group_info { gateway:1 ,/* routes using the group use a gateway */ is_resilient:1;struct list_head list;/* member in nh_res_grp_list */ -struct mlxsw_sp_nexthop nexthops[]; + struct mlxsw_sp_nexthop nexthops[] __counted_by(count); }; static struct mlxsw_sp_rif *
Signature · 1 line
2.34.1
Re: [PATCH 4/5] mlxsw: spectrum_router: Annotate struct mlxsw_sp_nexthop_group_info with __counted_by
Quoted 10 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_nexthop_group_info.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Re: [PATCH 4/5] mlxsw: spectrum_router: Annotate struct mlxsw_sp_nexthop_group_info with __counted_by
In reply to Kees CookQuoted 19 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_nexthop_group_info.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 1 line
---
Quoted 17 lines
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c +1
-2@@ -3107,7 +3107,7 @@ struct mlxsw_sp_nexthop_group_info { gateway:1 ,/* routes using the group use a gateway */ is_resilient:1;struct list_head list;/* member in nh_res_grp_list */ -struct mlxsw_sp_nexthop nexthops[]; + struct mlxsw_sp_nexthop nexthops[] __counted_by(count); }; static struct mlxsw_sp_rif * --
Quoted 3 lines
2.34.1
Thanks
Justin
PATCH 5/5 mlxsw: spectrum_span: Annotate struct mlxsw_sp_span with __counted_by
In reply to Kees CookPrepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).
As found with Coccinelle[1], add __counted_by for struct mlxsw_sp_span.
[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
--- drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c +1 -1
@@ -31,7 +31,7 @@ struct mlxsw_sp_span {refcount_t policer_id_base_ref_count;atomic_t active_entries_count;int entries_count; -struct mlxsw_sp_span_entry entries[]; + struct mlxsw_sp_span_entry entries[] __counted_by(entries_count); };struct mlxsw_sp_span_analyzed_port {
Signature · 1 line
2.34.1
Re: [PATCH 5/5] mlxsw: spectrum_span: Annotate struct mlxsw_sp_span with __counted_by
Quoted 10 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_span.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Quoted 8 lines
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Thanks for taking care of that
Re: [PATCH 5/5] mlxsw: spectrum_span: Annotate struct mlxsw_sp_span with __counted_by
In reply to Kees CookQuoted 19 lines · Kees Cook
Prepare for the coming implementation by GCC and Clang of the
__counted_byattribute. Flexible array members annotated with__counted_bycan have their accesses bounds-checked at run-time checking viaCONFIG_UBSAN_BOUNDS(for array indexing) andCONFIG_FORTIFY_SOURCE(for strcpy/memcpy-family functions).As found with Coccinelle[1], add
__counted_byfor structmlxsw_sp_span.[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Cc Ido Schimmel <[email protected]>Cc Petr Machata <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 1 line
---
Great patch!
Crucially, span->entries_count is assigned before any flexible array accesses.
span->entries_count = entries_count;
...
for (i = 0; i < mlxsw_sp->span->entries_count; i++)
mlxsw_sp->span->entries[i].id = i;Quoted 17 lines
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c +1
-2@@ -31,7 +31,7 @@ struct mlxsw_sp_span {refcount_t policer_id_base_ref_count;atomic_t active_entries_count;int entries_count; -struct mlxsw_sp_span_entry entries[]; + struct mlxsw_sp_span_entry entries[] __counted_by(entries_count); };struct mlxsw_sp_span_analyzed_port { --
Quoted 3 lines
2.34.1
Thanks
Justin
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 29 Sep 2023 11:07:39 -0700 you wrote:
Hi,
This annotates several mlxsw structures with the coming
__counted_byattribute for bounds checking of flexible arrays at run-time. For more details, see commitdd06e72e68bc("Compiler Attributes: Add__counted_bymacro").Thanks!
[...]
Here is the summary with links:
- [1/5] mlxsw: Annotate struct mlxsw_linecards with __counted_by
https://git.kernel.org/netdev/net-next/c/0b7ed8183375 - [2/5] mlxsw: core: Annotate struct mlxsw_env with __counted_by https://git.kernel.org/netdev/net-next/c/c63da7d62893 - [3/5] mlxsw: spectrum: Annotate struct mlxsw_sp_counter_pool with __counted_by https://git.kernel.org/netdev/net-next/c/f7ebae83768f - [4/5] mlxsw: spectrum_router: Annotate struct mlxsw_sp_nexthop_group_info with __counted_by https://git.kernel.org/netdev/net-next/c/4d3a42ec5cff - [5/5] mlxsw: spectrum_span: Annotate struct mlxsw_sp_span with __counted_by https://git.kernel.org/netdev/net-next/c/18cee9da32cd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html