[PATCH] net: ethernet: broadcom: replace strcpy with strscpy

Shi Hao posted 1 patch 2 months, 3 weeks ago
drivers/net/ethernet/broadcom/tg3.c | 36 ++++++++++++++---------------
1 file changed, 18 insertions(+), 18 deletions(-)
[PATCH] net: ethernet: broadcom: replace strcpy with strscpy
Posted by Shi Hao 2 months, 3 weeks ago
Replace strcpy function calls with strscpy to ensure bounds checking
in the destination buffer, preventing buffer overflows and improving
security. This change aligns with current kernel coding guidelines
and best practices.

Signed-off-by: Shi Hao <i.shihao.999@gmail.com>
---
 drivers/net/ethernet/broadcom/tg3.c | 36 ++++++++++++++---------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/tg3.c b/drivers/net/ethernet/broadcom/tg3.c
index d78cafdb2094..1a1673842a35 100644
--- a/drivers/net/ethernet/broadcom/tg3.c
+++ b/drivers/net/ethernet/broadcom/tg3.c
@@ -15765,53 +15765,53 @@ static void tg3_read_vpd(struct tg3 *tp)
 	if (tg3_asic_rev(tp) == ASIC_REV_5717) {
 		if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
 		    tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
-			strcpy(tp->board_part_number, "BCM5717");
+			strscpy(tp->board_part_number, "BCM5717", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
-			strcpy(tp->board_part_number, "BCM5718");
+			strscpy(tp->board_part_number, "BCM5718", TG3_BPN_SIZE);
 		else
 			goto nomatch;
 	} else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
 		if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
-			strcpy(tp->board_part_number, "BCM57780");
+			strscpy(tp->board_part_number, "BCM57780", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
-			strcpy(tp->board_part_number, "BCM57760");
+			strscpy(tp->board_part_number, "BCM57760", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
-			strcpy(tp->board_part_number, "BCM57790");
+			strscpy(tp->board_part_number, "BCM57790", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
-			strcpy(tp->board_part_number, "BCM57788");
+			strscpy(tp->board_part_number, "BCM57788", TG3_BPN_SIZE);
 		else
 			goto nomatch;
 	} else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
 		if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
-			strcpy(tp->board_part_number, "BCM57761");
+			strscpy(tp->board_part_number, "BCM57761", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
-			strcpy(tp->board_part_number, "BCM57765");
+			strscpy(tp->board_part_number, "BCM57765", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
-			strcpy(tp->board_part_number, "BCM57781");
+			strscpy(tp->board_part_number, "BCM57781", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
-			strcpy(tp->board_part_number, "BCM57785");
+			strscpy(tp->board_part_number, "BCM57785", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
-			strcpy(tp->board_part_number, "BCM57791");
+			strscpy(tp->board_part_number, "BCM57791", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
-			strcpy(tp->board_part_number, "BCM57795");
+			strscpy(tp->board_part_number, "BCM57795", TG3_BPN_SIZE);
 		else
 			goto nomatch;
 	} else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
 		if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
-			strcpy(tp->board_part_number, "BCM57762");
+			strscpy(tp->board_part_number, "BCM57762", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
-			strcpy(tp->board_part_number, "BCM57766");
+			strscpy(tp->board_part_number, "BCM57766", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
-			strcpy(tp->board_part_number, "BCM57782");
+			strscpy(tp->board_part_number, "BCM57782", TG3_BPN_SIZE);
 		else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
-			strcpy(tp->board_part_number, "BCM57786");
+			strscpy(tp->board_part_number, "BCM57786", TG3_BPN_SIZE);
 		else
 			goto nomatch;
 	} else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
-		strcpy(tp->board_part_number, "BCM95906");
+		strscpy(tp->board_part_number, "BCM95906", TG3_BPN_SIZE);
 	} else {
 nomatch:
-		strcpy(tp->board_part_number, "none");
+		strscpy(tp->board_part_number, "none", TG3_BPN_SIZE);
 	}
 }

--
2.51.0
Re: [PATCH] net: ethernet: broadcom: replace strcpy with strscpy
Posted by David Laight 2 months, 3 weeks ago
On Thu, 13 Nov 2025 13:55:17 +0530
Shi Hao <i.shihao.999@gmail.com> wrote:

> Replace strcpy function calls with strscpy to ensure bounds checking
> in the destination buffer, preventing buffer overflows and improving
> security. This change aligns with current kernel coding guidelines
> and best practices.
> 
...
> -			strcpy(tp->board_part_number, "BCM5717");
> +			strscpy(tp->board_part_number, "BCM5717", TG3_BPN_SIZE);

No one really knows that TG3_BPN_SIZE is in any way related to the destination.
So this doesn't actually make the code that much better at all.

Since tp->board_part_number is an array and "BCM5717" a constant I suspect
there is already a compile-time check that the string fits.
The strcpy() will also be converted to a memcpy().

So all, in all, this makes the code worse on several fronts.

	David
Re: [PATCH] net: ethernet: broadcom: replace strcpy with strscpy
Posted by ShiHao 2 months, 3 weeks ago
On Thu, Nov 13, 2025 at 07:22:18PM +0000, David Laight wrote:

> No one really knows that TG3_BPN_SIZE is in any way related to the destination.
> So this doesn't actually make the code that much better at all.
>
> Since tp->board_part_number is an array and "BCM5717" a constant I suspect
> there is already a compile-time check that the string fits.
> The strcpy() will also be converted to a memcpy().
>
> So all, in all, this makes the code worse on several fronts.
>
> 	Davidi


Okay. I got it thanks for your time david.