Skip to contentSkip to conversation

net/mlx5e: Correct snprintf truncation handling for fw_version buffer

PATCH In review Rahul Rameshbabu 2 participants 4 messages Nov 21, 2023 – Nov 24, 2023

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

PATCH 1/2 net/mlx5e: Correct snprintf truncation handling for fw_version buffer

snprintf returns the length of the formatted string, excluding the trailing null, without accounting for truncation. This means that is the return value is greater than or equal to the size parameter, the fw_version string was truncated.

Fixes 41e63c2baa11 ("net/mlx5e: Check return value of snprintf writing to fw_version buffer")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c +1 -1
index 792a0ea544cd..c7c1b667b105 100644
@@ -49,7 +49,7 @@ void mlx5e_ethtool_get_drvinfo(struct mlx5e_priv *priv,
 	count = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d (%.16s)", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev), mdev->board_id);
-	if (count == sizeof(drvinfo->fw_version))
+	if (count >= sizeof(drvinfo->fw_version))
 		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev));
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/2 net/mlx5e: Correct snprintf truncation handling for fw_version buffer used by representors

snprintf returns the length of the formatted string, excluding the trailing null, without accounting for truncation. This means that is the return value is greater than or equal to the size parameter, the fw_version string was truncated.

Fixes 1b2bd0c0264f ("net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_rep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rep.c +1 -1
index 3ab682bbcf86..8d6cca6e7755 100644
@@ -78,7 +78,7 @@ static void mlx5e_rep_get_drvinfo(struct net_device *dev,
 	count = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d (%.16s)", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev), mdev->board_id);
-	if (count == sizeof(drvinfo->fw_version))
+	if (count >= sizeof(drvinfo->fw_version))
 		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev));
Signature · 1 line
2.40.1
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH net v1 2/2] net/mlx5e: Correct snprintf truncation handling for fw_version buffer used by representors

Quoted 8 lines · Rahul Rameshbabu

snprintf returns the length of the formatted string, excluding the trailing null, without accounting for truncation. This means that is the return value is greater than or equal to the size parameter, the fw_version string was truncated.

Fixes 1b2bd0c0264f ("net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors")
reviewer Reviewed-by on v1 1/2
#3 lore raw
In reply to Rahul Rameshbabu
Quoted 10 lines · Rahul Rameshbabu

snprintf returns the length of the formatted string, excluding the trailing null, without accounting for truncation. This means that is the return value is greater than or equal to the size parameter, the fw_version string was truncated.

Fixes 41e63c2baa11 ("net/mlx5e: Check return value of snprintf writing to fw_version buffer")