netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments
@ 2023-10-11 21:29 Justin Stitt
  2023-10-11 21:34 ` Kees Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Justin Stitt @ 2023-10-11 21:29 UTC (permalink / raw)
  To: Saeed Mahameed, Leon Romanovsky, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-rdma, linux-kernel, linux-hardening, Kees Cook,
	Justin Stitt

In total, just assigning this version string takes:
(1) strncpy()'s
(5) strlen()'s
(3) strncat()'s
(1) snprintf()'s
(4) max_t()'s

Moreover, `strncpy` is deprecated [1] and `strncat` really shouldn't be
used either [2]. With this in mind, let's simply use a single
`snprintf`.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://elixir.bootlin.com/linux/v6.6-rc5/source/include/linux/fortify-string.h#L448 [2]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/net/ethernet/mellanox/mlx5/core/main.c | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/main.c b/drivers/net/ethernet/mellanox/mlx5/core/main.c
index 15561965d2af..0c829b6d2b49 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/main.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/main.c
@@ -218,7 +218,6 @@ static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
 	int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
 					      driver_version);
 	u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {};
-	int remaining_size = driver_ver_sz;
 	char *string;
 
 	if (!MLX5_CAP_GEN(dev, driver_version))
@@ -226,22 +225,9 @@ static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
 
 	string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
 
-	strncpy(string, "Linux", remaining_size);
-
-	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
-	strncat(string, ",", remaining_size);
-
-	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
-	strncat(string, KBUILD_MODNAME, remaining_size);
-
-	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
-	strncat(string, ",", remaining_size);
-
-	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
-
-	snprintf(string + strlen(string), remaining_size, "%u.%u.%u",
-		LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL,
-		LINUX_VERSION_SUBLEVEL);
+	snprintf(string, driver_ver_sz, "Linux,%s,%u.%u.%u",
+		 KBUILD_MODNAME, LINUX_VERSION_MAJOR,
+		 LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL);
 
 	/*Send the command*/
 	MLX5_SET(set_driver_version_in, in, opcode,

---
base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2
change-id: 20231011-strncpy-drivers-net-ethernet-mellanox-mlx5-core-main-c-2dd9a3fa22e0

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments
  2023-10-11 21:29 [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments Justin Stitt
@ 2023-10-11 21:34 ` Kees Cook
  2023-10-12 19:46   ` Saeed Mahameed
  0 siblings, 1 reply; 3+ messages in thread
From: Kees Cook @ 2023-10-11 21:34 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Saeed Mahameed, Leon Romanovsky, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, netdev, linux-rdma, linux-kernel,
	linux-hardening

On Wed, Oct 11, 2023 at 09:29:57PM +0000, Justin Stitt wrote:
> In total, just assigning this version string takes:
> (1) strncpy()'s
> (5) strlen()'s
> (3) strncat()'s
> (1) snprintf()'s
> (4) max_t()'s
> 
> Moreover, `strncpy` is deprecated [1] and `strncat` really shouldn't be
> used either [2]. With this in mind, let's simply use a single
> `snprintf`.

Yes, please! readability++

> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://elixir.bootlin.com/linux/v6.6-rc5/source/include/linux/fortify-string.h#L448 [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments
  2023-10-11 21:34 ` Kees Cook
@ 2023-10-12 19:46   ` Saeed Mahameed
  0 siblings, 0 replies; 3+ messages in thread
From: Saeed Mahameed @ 2023-10-12 19:46 UTC (permalink / raw)
  To: Kees Cook
  Cc: Justin Stitt, Saeed Mahameed, Leon Romanovsky, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-rdma,
	linux-kernel, linux-hardening

On 11 Oct 14:34, Kees Cook wrote:
>On Wed, Oct 11, 2023 at 09:29:57PM +0000, Justin Stitt wrote:
>> In total, just assigning this version string takes:
>> (1) strncpy()'s
>> (5) strlen()'s
>> (3) strncat()'s
>> (1) snprintf()'s
>> (4) max_t()'s
>>
>> Moreover, `strncpy` is deprecated [1] and `strncat` really shouldn't be
>> used either [2]. With this in mind, let's simply use a single
>> `snprintf`.
>
>Yes, please! readability++
>
>>
>> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
>> Link: https://elixir.bootlin.com/linux/v6.6-rc5/source/include/linux/fortify-string.h#L448 [2]
>> Link: https://github.com/KSPP/linux/issues/90
>> Cc: linux-hardening@vger.kernel.org
>> Cc: Kees Cook <keescook@chromium.org>
>> Signed-off-by: Justin Stitt <justinstitt@google.com>
>
>Reviewed-by: Kees Cook <keescook@chromium.org>

Applied to net-next-mlx5.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-12 19:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-11 21:29 [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments Justin Stitt
2023-10-11 21:34 ` Kees Cook
2023-10-12 19:46   ` Saeed Mahameed

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