Thread (4 messages) flat view 4 messages, 3 authors, 2025-08-12

Re: [PATCH] net: tun: replace strcpy with strscpy for ifr_name

From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Date: 2025-08-11 14:27:50
Also in: lkml

[PATCH net-next]

Miguel García wrote:
Replace the strcpy() calls that copy the device name into ifr->ifr_name
with strscpy() to avoid potential overflows and guarantee NUL termination.
NULL
quoted hunk ↗ jump to hunk
Destination is ifr->ifr_name (size IFNAMSIZ).

Tested in QEMU (BusyBox rootfs):
 - Created TUN devices via TUNSETIFF helper
 - Set addresses and brought links up
 - Verified long interface names are safely truncated (IFNAMSIZ-1)

Signed-off-by: Miguel García <redacted>
---
 drivers/net/tun.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index f8c5e2fd04df..e4c6c1118acb 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -2800,13 +2800,13 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 	if (netif_running(tun->dev))
 		netif_tx_wake_all_queues(tun->dev);
 
-	strcpy(ifr->ifr_name, tun->dev->name);
+	strscpy(ifr->ifr_name, tun->dev->name, IFNAMSIZ);
Since both dst and src are arrays of IFNAMSIZ, can drop the third
argument. Then it is inferred from the field sizes, which is more
robust.
 	return 0;
 }
 
 static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
 {
-	strcpy(ifr->ifr_name, tun->dev->name);
+	strscpy(ifr->ifr_name, tun->dev->name, IFNAMSIZ);
 
 	ifr->ifr_flags = tun_flags(tun);
 
-- 
2.34.1
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help