net: ethernet: broadcom: replace strcpy with strscpy
Subsystems (3)
PATCH 1/1 net: ethernet: broadcom: replace strcpy with strscpy
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.
--- drivers/net/ethernet/broadcom/tg3.c | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/net/ethernet/broadcom/tg3.c +18 -18
@@ -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
Quoted 5 lines · On Thu, 13 Nov 2025 13:55:17 +0530 Shi Hao <[email protected]> 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.
...
drivers/net/ethernet/broadcom/tg3.c static void tg3_read_vpd(struct tg3 *tp) · line 15768
Quoted 2 lines
drivers/net/ethernet/broadcom/tg3.c +1
-1-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
Quoted 10 lines · David Laight
No one really knows that
TG3_BPN_SIZEis in any way related to the destination. So this doesn't actually make the code that much better at all.Since
tp->board_part_numberis an array and "BCM5717" a constant I suspect there is already a compile-time check that the string fits. Thestrcpy()will also be converted to amemcpy().So all, in all, this makes the code worse on several fronts.
Davidi
Okay. I got it thanks for your time david.