Skip to contentSkip to conversation

mlx4: supress fortify for inlined xmit

PATCH In review Josef Oskera 3 participants 4 messages Feb 17, 2023 – Feb 20, 2023

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mlx4: supress fortify for inlined xmit

This call "skb_copy_from_linear_data(skb, inl + 1, spc)" triggers FORTIFY memcpy() warning on ppc64 platform.

In function ‘fortify_memcpy_chk’,
inlined from ‘skb_copy_from_linear_data’ at ./include/linux/skbuff.h:4029:2,
inlined from ‘build_inline_wqe’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:722:4,
inlined from ‘mlx4_en_xmit’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:1066:3:
./include/linux/fortify-string.h:513:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]

  513 |                         __write_overflow_field(p_size_field, size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same behaviour on x86 you can get if you use "__always_inline" instead of "inline" for skb_copy_from_linear_data() in skbuff.h

The call here copies data into inlined tx destricptor, which has 104 bytes (MAX_INLINE) space for data payload. In this case "spc" is known in compile-time but the destination is used with hidden knowledge (real structure of destination is different from that the compiler can see). That cause the fortify warning because compiler can check bounds, but the real bounds are different. "spc" can't be bigger than 64 bytes (MLX4_INLINE_ALIGN), so the data can always fit into inlined tx descriptor.
The fact that "inl" points into inlined tx descriptor is determined earlier in mlx4_en_xmit().

Fixes f68f2ff91512c1 fortify: Detect struct member overflows in memcpy() at compile-time
---
 drivers/net/ethernet/mellanox/mlx4/en_tx.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -1
index c5758637b7bed6..f30ca9fe90e5b4 100644
@@ -719,7 +719,16 @@ static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
 			inl = (void *) (inl + 1) + spc;
 			memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
 		} else {
-			skb_copy_from_linear_data(skb, inl + 1, spc);
+			unsafe_memcpy(inl + 1, skb->data, spc,
+					/* This copies data into inlined tx descriptor, which has
+					 * 104 bytes (MAX_INLINE) space for data.
+					 * Real structure of destination is in this case hidden for
+					 * the compiler
+					 * "spc" is compile-time known variable and can't be bigger
+					 * than 64 (MLX4_INLINE_ALIGN).
+					 * Bounds and other conditions are checked in current
+					 * function and earlier in mlx4_en_xmit()
+					 */);
 			inl = (void *) (inl + 1) + spc;
 			skb_copy_from_linear_data_offset(skb, spc, inl + 1,
 							 hlen - spc);
Signature · 1 line
2.39.0
#1 lore raw
Quoted 35 lines · Josef Oskera

This call "skb_copy_from_linear_data(skb, inl + 1, spc)" triggers FORTIFY memcpy() warning on ppc64 platform.

In function ‘fortify_memcpy_chk’,
inlined from ‘skb_copy_from_linear_data’ at ./include/linux/skbuff.h:4029:2,
inlined from ‘build_inline_wqe’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:722:4,
inlined from ‘mlx4_en_xmit’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:1066:3:
./include/linux/fortify-string.h:513:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]

 513 |                         __write_overflow_field(p_size_field, size);
     |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same behaviour on x86 you can get if you use "__always_inline" instead of "inline" for skb_copy_from_linear_data() in skbuff.h

The call here copies data into inlined tx destricptor, which has 104 bytes (MAX_INLINE) space for data payload. In this case "spc" is known in compile-time but the destination is used with hidden knowledge (real structure of destination is different from that the compiler can see). That cause the fortify warning because compiler can check bounds, but the real bounds are different. "spc" can't be bigger than 64 bytes (MLX4_INLINE_ALIGN), so the data can always fit into inlined tx descriptor.
The fact that "inl" points into inlined tx descriptor is determined earlier in mlx4_en_xmit().

Fixes f68f2ff91512c1 fortify: Detect struct member overflows in memcpy() at compile-time

---
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 11 ++++++++++-

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
index c5758637b7bed6..f30ca9fe90e5b4 100644

drivers/net/ethernet/mellanox/mlx4/en_tx.c static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, · line 720

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
@@ -719,7 +719,16 @@ static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
 			inl = (void *) (inl + 1) + spc;
 			memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);

Using "unsafe" isn't the right solution here. What needs fixing is the "inl + 1" pattern which lacks any sense from the compilet's perspective. The struct of inl needs to end with a flex array, and it should be used for all the accesses. i.e. replace all the "inl + 1" instances with "inl->data". This makes it more readable for humans too. :)

I can send a patch...

-Kees

Quoted 15 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -1
 		} else {
-			skb_copy_from_linear_data(skb, inl + 1, spc);
+			unsafe_memcpy(inl + 1, skb->data, spc,
+					/* This copies data into inlined tx descriptor, which has
+					 * 104 bytes (MAX_INLINE) space for data.
+					 * Real structure of destination is in this case hidden for
+					 * the compiler
+					 * "spc" is compile-time known variable and can't be bigger
+					 * than 64 (MLX4_INLINE_ALIGN).
+					 * Bounds and other conditions are checked in current
+					 * function and earlier in mlx4_en_xmit()
+					 */);
 			inl = (void *) (inl + 1) + spc;
 			skb_copy_from_linear_data_offset(skb, spc, inl + 1,
 							 hlen - spc);
Signature · 1 line
Kees Cook
Reviewed-by
#2 lore raw
Quoted 41 lines · Kees Cook
Quoted 35 lines · Josef Oskera

This call "skb_copy_from_linear_data(skb, inl + 1, spc)" triggers FORTIFY memcpy() warning on ppc64 platform.

In function ‘fortify_memcpy_chk’,
inlined from ‘skb_copy_from_linear_data’ at ./include/linux/skbuff.h:4029:2,
inlined from ‘build_inline_wqe’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:722:4,
inlined from ‘mlx4_en_xmit’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:1066:3:
./include/linux/fortify-string.h:513:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]

  513 |                         __write_overflow_field(p_size_field, size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same behaviour on x86 you can get if you use "__always_inline" instead of "inline" for skb_copy_from_linear_data() in skbuff.h

The call here copies data into inlined tx destricptor, which has 104 bytes (MAX_INLINE) space for data payload. In this case "spc" is known in compile-time but the destination is used with hidden knowledge (real structure of destination is different from that the compiler can see). That cause the fortify warning because compiler can check bounds, but the real bounds are different. "spc" can't be bigger than 64 bytes (MLX4_INLINE_ALIGN), so the data can always fit into inlined tx descriptor.
The fact that "inl" points into inlined tx descriptor is determined earlier in mlx4_en_xmit().

Fixes f68f2ff91512c1 fortify: Detect struct member overflows in memcpy() at compile-time

---
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 11 ++++++++++-

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
index c5758637b7bed6..f30ca9fe90e5b4 100644

drivers/net/ethernet/mellanox/mlx4/en_tx.c static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, · line 720

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
@@ -719,7 +719,16 @@ static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
 			inl = (void *) (inl + 1) + spc;
 			memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);

Using "unsafe" isn't the right solution here. What needs fixing is the "inl + 1" pattern which lacks any sense from the compilet's perspective. The struct of inl needs to end with a flex array, and it should be used for all the accesses. i.e. replace all the "inl + 1" instances with "inl->data". This makes it more readable for humans too. :)

Quoted 2 lines

I can send a patch...

Although expanding the mlx4_wqe_inline_seg struct with a flex array sounds valid, I wouldn't go that way as it requires a larger change, touching common and RDMA code as well, for a driver in it's end-of-life stage.

We already have such unsafe_memcpy usage in mlx5 driver, so I can accept it here as well.

Let's keep the change as contained as possible.

Quoted 19 lines

-Kees

Quoted 15 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -1
 		} else {
-			skb_copy_from_linear_data(skb, inl + 1, spc);
+			unsafe_memcpy(inl + 1, skb->data, spc,
+					/* This copies data into inlined tx descriptor, which has
+					 * 104 bytes (MAX_INLINE) space for data.
+					 * Real structure of destination is in this case hidden for
+					 * the compiler
+					 * "spc" is compile-time known variable and can't be bigger
+					 * than 64 (MLX4_INLINE_ALIGN).
+					 * Bounds and other conditions are checked in current
+					 * function and earlier in mlx4_en_xmit()
+					 */);
 			inl = (void *) (inl + 1) + spc;
 			skb_copy_from_linear_data_offset(skb, spc, inl + 1,
 							 hlen - spc);
#3 lore raw
Quoted 48 lines · Tariq Toukan
Quoted 42 lines · Kees Cook
Quoted 35 lines · Josef Oskera

This call "skb_copy_from_linear_data(skb, inl + 1, spc)" triggers FORTIFY memcpy() warning on ppc64 platform.

In function ‘fortify_memcpy_chk’,
    inlined from ‘skb_copy_from_linear_data’ at ./include/linux/skbuff.h:4029:2,
    inlined from ‘build_inline_wqe’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:722:4,
    inlined from ‘mlx4_en_xmit’ at drivers/net/ethernet/mellanox/mlx4/en_tx.c:1066:3:
./include/linux/fortify-string.h:513:25: error: call to ‘__write_overflow_field’ declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning] 513 |                         __write_overflow_field(p_size_field, size);
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same behaviour on x86 you can get if you use "__always_inline" instead of "inline" for skb_copy_from_linear_data() in skbuff.h

The call here copies data into inlined tx destricptor, which has 104 bytes (MAX_INLINE) space for data payload. In this case "spc" is known in compile-time but the destination is used with hidden knowledge (real structure of destination is different from that the compiler can see). That cause the fortify warning because compiler can check bounds, but the real bounds are different. "spc" can't be bigger than 64 bytes (MLX4_INLINE_ALIGN), so the data can always fit into inlined tx descriptor.
The fact that "inl" points into inlined tx descriptor is determined earlier in mlx4_en_xmit().

Fixes f68f2ff91512c1 fortify: Detect struct member overflows in memcpy() at compile-time

---
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 11 ++++++++++-

1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
index c5758637b7bed6..f30ca9fe90e5b4 100644

drivers/net/ethernet/mellanox/mlx4/en_tx.c static void build_inline_wqe(struct · line 721

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +0 -0
@@ -719,7 +719,16 @@ static void build_inline_wqe(struct 
 mlx4_en_tx_desc *tx_desc,
             inl = (void *) (inl + 1) + spc;
             memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);

Using "unsafe" isn't the right solution here. What needs fixing is the "inl + 1" pattern which lacks any sense from the compilet's perspective. The struct of inl needs to end with a flex array, and it should be used for all the accesses. i.e. replace all the "inl + 1" instances with "inl->data". This makes it more readable for humans too. :)

Quoted 2 lines

I can send a patch...

Quoted 7 lines

Although expanding the mlx4_wqe_inline_seg struct with a flex array sounds valid, I wouldn't go that way as it requires a larger change, touching common and RDMA code as well, for a driver in it's end-of-life stage.

We already have such unsafe_memcpy usage in mlx5 driver, so I can accept it here as well.

Let's keep the change as contained as possible.

Kees posted a contained alternative solution.
Let's go with that one. Thanks.