`__if_nametoindex()` can leak an FD if its argument is too long

Message ID 162965eb-f84c-ca03-2cc1-dd895fbadcd6@126.com
State Not applicable
Headers
Series `__if_nametoindex()` can leak an FD if its argument is too long |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

LIU Hao Feb. 7, 2023, 1:11 p.m. UTC
  Greetings, libc maintainers.

It looks like `if_nametoindex()` for Hurd can leak a socket descriptor if its argument string is too 
long. Patch attached.


-- 
Best regards,
LIU Hao
  

Patch

From 6ba2bc2fa7d01999a9c92953ca7d84146fe6c741 Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mouse@126.com>
Date: Tue, 7 Feb 2023 21:05:50 +0800
Subject: [PATCH] hurd: Don't lean the socket FD if argument to
 `__if_nametoindex()` is too long

---
 sysdeps/mach/hurd/if_index.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/sysdeps/mach/hurd/if_index.c b/sysdeps/mach/hurd/if_index.c
index a4472269b7..9b598a279d 100644
--- a/sysdeps/mach/hurd/if_index.c
+++ b/sysdeps/mach/hurd/if_index.c
@@ -32,10 +32,7 @@  unsigned int
 __if_nametoindex (const char *ifname)
 {
   struct ifreq ifr;
-  int fd = __socket (AF_INET, SOCK_DGRAM, 0);
-
-  if (fd < 0)
-    return 0;
+  int fd;
 
   if (strlen (ifname) >= IFNAMSIZ)
     {
@@ -43,6 +40,10 @@  __if_nametoindex (const char *ifname)
       return 0;
     }
 
+  fd = __socket (AF_INET, SOCK_DGRAM, 0);
+  if (fd < 0)
+    return 0;
+
   strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
   if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
     {
-- 
2.34.1