netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Saeed Mahameed <saeed@kernel.org>
To: "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Eric Dumazet <edumazet@google.com>
Cc: Saeed Mahameed <saeedm@nvidia.com>,
	netdev@vger.kernel.org, Tariq Toukan <tariqt@nvidia.com>,
	Rahul Rameshbabu <rrameshbabu@nvidia.com>,
	Dragos Tatulea <dtatulea@nvidia.com>
Subject: [net-next V2 12/15] net/mlx5e: Check return value of snprintf writing to fw_version buffer
Date: Fri, 20 Oct 2023 23:46:17 -0700	[thread overview]
Message-ID: <20231021064620.87397-13-saeed@kernel.org> (raw)
In-Reply-To: <20231021064620.87397-1-saeed@kernel.org>

From: Rahul Rameshbabu <rrameshbabu@nvidia.com>

Treat the operation as an error case when the return value is equivalent to
the size of the name buffer. Failed to write null terminator to the name
buffer, making the string malformed and should not be used. Provide a
string with only the firmware version when forming the string with the
board id fails.

Without check, will trigger -Wformat-truncation with W=1.

    drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c: In function 'mlx5e_ethtool_get_drvinfo':
    drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c:49:31: warning: '%.16s' directive output may be truncated writing up to 16 bytes into a region of size between 13 and 22 [-Wformat-truncation=]
      49 |                  "%d.%d.%04d (%.16s)",
         |                               ^~~~~
    drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c:48:9: note: 'snprintf' output between 12 and 37 bytes into a destination of size 32
      48 |         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      49 |                  "%d.%d.%04d (%.16s)",
         |                  ~~~~~~~~~~~~~~~~~~~~~
      50 |                  fw_rev_maj(mdev), fw_rev_min(mdev), fw_rev_sub(mdev),
         |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      51 |                  mdev->board_id);
         |                  ~~~~~~~~~~~~~~~

Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6d4ab2e97dcfbcd748ae71761a9d8e5e41cc732c
Signed-off-by: Rahul Rameshbabu <rrameshbabu@nvidia.com>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
---
 .../net/ethernet/mellanox/mlx5/core/en_ethtool.c    | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
index 215261a69255..792a0ea544cd 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
@@ -43,12 +43,17 @@ void mlx5e_ethtool_get_drvinfo(struct mlx5e_priv *priv,
 			       struct ethtool_drvinfo *drvinfo)
 {
 	struct mlx5_core_dev *mdev = priv->mdev;
+	int count;
 
 	strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
-	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);
+	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))
+		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
+			 "%d.%d.%04d", fw_rev_maj(mdev),
+			 fw_rev_min(mdev), fw_rev_sub(mdev));
+
 	strscpy(drvinfo->bus_info, dev_name(mdev->device),
 		sizeof(drvinfo->bus_info));
 }
-- 
2.41.0


  parent reply	other threads:[~2023-10-21  6:46 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-21  6:46 [pull request][net-next V2 00/15] mlx5 updates 2023-10-19 Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 01/15] xfrm: generalize xdo_dev_state_update_curlft to allow statistics update Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 02/15] xfrm: get global statistics from the offloaded device Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 03/15] net/mlx5e: Honor user choice of IPsec replay window size Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 04/15] net/mlx5e: Ensure that IPsec sequence packet number starts from 1 Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 05/15] net/mlx5e: Unify esw and normal IPsec status table creation/destruction Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 06/15] net/mlx5e: Remove exposure of IPsec RX flow steering struct Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 07/15] net/mlx5e: Add IPsec and ASO syndromes check in HW Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 08/15] net/mlx5e: Connect mlx5 IPsec statistics with XFRM core Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 09/15] net/mlx5e: Delete obsolete IPsec code Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 10/15] net/mlx5: Increase size of irq name buffer Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 11/15] net/mlx5e: Reduce the size of icosq_str Saeed Mahameed
2023-10-21  6:46 ` Saeed Mahameed [this message]
2023-10-21  6:46 ` [net-next V2 13/15] net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 14/15] net/mlx5: print change on SW reset semaphore returns busy Saeed Mahameed
2023-10-21  6:46 ` [net-next V2 15/15] net/mlx5: Allow sync reset flow when BF MGT interface device is present Saeed Mahameed
2023-10-25  1:02 ` [pull request][net-next V2 00/15] mlx5 updates 2023-10-19 Jakub Kicinski
2023-10-25  8:52   ` Leon Romanovsky
2023-10-26  1:25     ` Jakub Kicinski
2023-10-26  7:29       ` Leon Romanovsky
2023-10-26 22:26   ` Saeed Mahameed
2023-10-26 22:46     ` Jakub Kicinski
2023-10-27  0:44       ` Saeed Mahameed
2023-10-27 10:08         ` Leon Romanovsky
2023-10-27 22:02 ` Saeed Mahameed
2023-10-29  7:44   ` Leon Romanovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231021064620.87397-13-saeed@kernel.org \
    --to=saeed@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dtatulea@nvidia.com \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rrameshbabu@nvidia.com \
    --cc=saeedm@nvidia.com \
    --cc=tariqt@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).