Skip to contentSkip to conversation

qede: Prevent possible snprintf() truncation by bounding %s string format

PATCH In review Baran Tuna 3 participants 4 messages Jul 1

Subsystems (3)
lore
Share & tools
lore

Baran Tuna

author
#0 lore raw

PATCH 1/1 qede: Prevent possible snprintf() truncation by bounding %s string format

GCC warning shows that formatted strings may
exceed the fixed-size destination buffers.

Bounding the %s string format
so the maximum formatted output always fits.

This eliminates the -Wformat-truncation warning.

---
 drivers/net/ethernet/qlogic/qede/qede_ethtool.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +4 -4
index 647f30a16a94..5428f53150a0 100644
@@ -618,10 +618,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
 	if ((strlen(storm) + strlen("[storm]")) <
 	    sizeof(info->version))
 		snprintf(info->version, sizeof(info->version),
-			 "[storm %s]", storm);
+			 "[storm %.16s]", storm);
 	else
 		snprintf(info->version, sizeof(info->version),
-			 "%s", storm);
+			 "%.16s", storm);
 
 	if (edev->dev_info.common.mbi_version) {
 		snprintf(mbi, ETHTOOL_FWVERS_LEN, "%d.%d.%d",
@@ -632,10 +632,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
 			 (edev->dev_info.common.mbi_version &
 			  QED_MBI_VERSION_0_MASK) >> QED_MBI_VERSION_0_OFFSET);
 		snprintf(info->fw_version, sizeof(info->fw_version),
-			 "mbi %s [mfw %s]", mbi, mfw);
+			 "mbi %.10s [mfw %.10s]", mbi, mfw);
 	} else {
 		snprintf(info->fw_version, sizeof(info->fw_version),
-			 "mfw %s", mfw);
+			 "mfw %.16s", mfw);
 	}
 
 	strscpy(info->bus_info, pci_name(edev->pdev), sizeof(info->bus_info));
Signature · 1 line
2.54.0
#1 lore raw
Quoted 18 lines · Baran Tuna

GCC warning shows that formatted strings may
exceed the fixed-size destination buffers.

Bounding the %s string format
so the maximum formatted output always fits.

This eliminates the -Wformat-truncation warning.

---
 drivers/net/ethernet/qlogic/qede/qede_ethtool.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +0 -0
index 647f30a16a94..5428f53150a0 100644

drivers/net/ethernet/qlogic/qede/qede_ethtool.c static void qede_get_drvinfo(struct net_device *ndev, · line 621

Quoted 5 lines
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +1 -1
@@ -618,10 +618,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
 	if ((strlen(storm) + strlen("[storm]")) <
 	    sizeof(info->version))
 		snprintf(info->version, sizeof(info->version),
-			 "[storm %s]", storm);
+			 "[storm %.16s]", storm);

Where is this 16 coming from?

Also, isn't the if above checking for no overflow? I.e,
we got here only if strlen(storm) + strlen("[storm]") < sizeof(info->version))

For whoever else is reviwewing this, this the buffers:

	#define ETHTOOL_FWVERS_LEN      32
        char    version[32];
	char storm[ETHTOOL_FWVERS_LEN];
author
#2 lore raw

The current solution is pretty arbitrary.

Numbers are coming from a simple calculation, to make sure output always fits.

I will take a further look and send a patch if there is a more generalized solution.

Quoted 35 lines · Breno Leitao
Quoted 18 lines · Baran Tuna

GCC warning shows that formatted strings may
exceed the fixed-size destination buffers.

Bounding the %s string format
so the maximum formatted output always fits.

This eliminates the -Wformat-truncation warning.

---
  drivers/net/ethernet/qlogic/qede/qede_ethtool.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +0 -0
index 647f30a16a94..5428f53150a0 100644

drivers/net/ethernet/qlogic/qede/qede_ethtool.c static void qede_get_drvinfo(struct net_device *ndev, · line 621

Quoted 5 lines
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +1 -1
@@ -618,10 +618,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
  	if ((strlen(storm) + strlen("[storm]")) <
  	    sizeof(info->version))
  		snprintf(info->version, sizeof(info->version),
-			 "[storm %s]", storm);
+			 "[storm %.16s]", storm);

Where is this 16 coming from?

Also, isn't the if above checking for no overflow? I.e,
we got here only if strlen(storm) + strlen("[storm]") < sizeof(info->version))

For whoever else is reviwewing this, this the buffers:

	#define ETHTOOL_FWVERS_LEN      32
         char    version[32];
	char storm[ETHTOOL_FWVERS_LEN];
#3 lore raw
In reply to Baran Tuna
Quoted 18 lines · On Wed, 1 Jul 2026 17:47:11 +0300 Baran Tuna <[email protected]> wrote:

GCC warning shows that formatted strings may
exceed the fixed-size destination buffers.

Bounding the %s string format
so the maximum formatted output always fits.

This eliminates the -Wformat-truncation warning.

---
 drivers/net/ethernet/qlogic/qede/qede_ethtool.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +0 -0
index 647f30a16a94..5428f53150a0 100644

drivers/net/ethernet/qlogic/qede/qede_ethtool.c static void qede_get_drvinfo(struct net_device *ndev, · line 624

Quoted 9 lines
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +2 -2
@@ -618,10 +618,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
 	if ((strlen(storm) + strlen("[storm]")) <
 	    sizeof(info->version))
 		snprintf(info->version, sizeof(info->version),
-			 "[storm %s]", storm);
+			 "[storm %.16s]", storm);
 	else
 		snprintf(info->version, sizeof(info->version),
-			 "%s", storm);
+			 "%.16s", storm);

That looks wrong.
The code is using two different formats based on the length of 'storm' but you are truncating it to the same length in both cases.
I think this will work:

	if (snprintf(info->version, sizeof(info->version),
		     "[storm %s]", storm) >= sizeof(info->strorm))
		strscpy(info->version, storm);

-- David

Quoted 16 lines
drivers/net/ethernet/qlogic/qede/qede_ethtool.c +2 -2
 	if (edev->dev_info.common.mbi_version) {
 		snprintf(mbi, ETHTOOL_FWVERS_LEN, "%d.%d.%d",
@@ -632,10 +632,10 @@ static void qede_get_drvinfo(struct net_device *ndev,
 			 (edev->dev_info.common.mbi_version &
 			  QED_MBI_VERSION_0_MASK) >> QED_MBI_VERSION_0_OFFSET);
 		snprintf(info->fw_version, sizeof(info->fw_version),
-			 "mbi %s [mfw %s]", mbi, mfw);
+			 "mbi %.10s [mfw %.10s]", mbi, mfw);
 	} else {
 		snprintf(info->fw_version, sizeof(info->fw_version),
-			 "mfw %s", mfw);
+			 "mfw %.16s", mfw);
 	}
 
 	strscpy(info->bus_info, pci_name(edev->pdev), sizeof(info->bus_info));