* [PATCH] net: ethernet: broadcom: replace strcpy with strscpy
@ 2025-11-13 8:25 Shi Hao
2025-11-13 19:22 ` David Laight
0 siblings, 1 reply; 3+ messages in thread
From: Shi Hao @ 2025-11-13 8:25 UTC (permalink / raw)
To: pavan.chebbi
Cc: mchan, andrew+netdev, davem, edumazet, kuba, pabeni, netdev,
linux-kernel, Shi Hao
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] net: ethernet: broadcom: replace strcpy with strscpy
2025-11-13 8:25 [PATCH] net: ethernet: broadcom: replace strcpy with strscpy Shi Hao
@ 2025-11-13 19:22 ` David Laight
2025-11-14 9:07 ` ShiHao
0 siblings, 1 reply; 3+ messages in thread
From: David Laight @ 2025-11-13 19:22 UTC (permalink / raw)
To: Shi Hao
Cc: pavan.chebbi, mchan, andrew+netdev, davem, edumazet, kuba, pabeni,
netdev, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] net: ethernet: broadcom: replace strcpy with strscpy
2025-11-13 19:22 ` David Laight
@ 2025-11-14 9:07 ` ShiHao
0 siblings, 0 replies; 3+ messages in thread
From: ShiHao @ 2025-11-14 9:07 UTC (permalink / raw)
To: David Laight
Cc: andrew+netdev, davem, edumazet, i.shihao.999, kuba, linux-kernel,
mchan, netdev, pabeni, pavan.chebbi
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-14 9:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-13 8:25 [PATCH] net: ethernet: broadcom: replace strcpy with strscpy Shi Hao
2025-11-13 19:22 ` David Laight
2025-11-14 9:07 ` ShiHao
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).