Skip to contentSkip to conversation

net/mlx5: Annotate struct mlx5_flow_handle with __counted_by

PATCH Applied Kees Cook 5 participants 5 messages Oct 3, 2023 – Oct 12, 2023

Subsystems (4)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 net/mlx5: Annotate struct mlx5_flow_handle 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 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 mlx5_flow_handle.

---
 drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +1 -1
index 4aed1768b85f..78eb6b7097e1 100644
@@ -181,7 +181,7 @@ struct mlx5_flow_rule {
 
 struct mlx5_flow_handle {
 	int num_rules;
-	struct mlx5_flow_rule *rule[];
+	struct mlx5_flow_rule *rule[] __counted_by(num_rules);
 };
 
 /* Type of children is mlx5_flow_group */
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Kees Cook

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 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 mlx5_flow_handle.

---
 drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +0 -0
index 4aed1768b85f..78eb6b7097e1 100644

drivers/net/ethernet/mellanox/mlx5/core/fs_core.h struct mlx5_flow_rule { · line 185

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +1 -1
@@ -181,7 +181,7 @@ struct mlx5_flow_rule {
 
 struct mlx5_flow_handle {
        int num_rules;
-       struct mlx5_flow_rule *rule[];
+       struct mlx5_flow_rule *rule[] __counted_by(num_rules);
 };

Great patch!

handle->num_rules is properly assigned to before handle->rule has any accesses.

        handle = alloc_handle((dest_num) ? dest_num : 1);

then

        static struct mlx5_flow_handle *alloc_handle(int num_rules) {
                ...
               handle->num_rules = num_rules;

then

        handle->rule[i] = rule;
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +0 -1
 /* Type of children is mlx5_flow_group */
--

2.34.1

Thanks
Justin

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 8 lines · Kees Cook

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 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 mlx5_flow_handle.

Quoted 10 lines

Thanks
--
Gustavo

Quoted 17 lines
---
  drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +1 -1
index 4aed1768b85f..78eb6b7097e1 100644
@@ -181,7 +181,7 @@ struct mlx5_flow_rule {
 
  struct mlx5_flow_handle {
  	int num_rules;
-	struct mlx5_flow_rule *rule[];
+	struct mlx5_flow_rule *rule[] __counted_by(num_rules);
  };
 
  /* Type of children is mlx5_flow_group */
Reviewed-by on v1 1/1
#3 lore raw
In reply to Kees Cook
Quoted 18 lines · Kees Cook

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 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 mlx5_flow_handle.

Quoted 4 lines
---
 drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks,

#4 lore raw
In reply to Kees Cook
Quoted 8 lines · Kees Cook

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 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 mlx5_flow_handle.

Quoted 10 lines

applied to net-next-mlx5.