[hurd,commited] hurd: Make __if_nametoindex return ENODEV if ifname is too long

Message ID 20180403211458.12717-1-samuel.thibault@ens-lyon.org
State New
Headers show
Series
  • [hurd,commited] hurd: Make __if_nametoindex return ENODEV if ifname is too long
Related show

Commit Message

Samuel Thibault April 3, 2018, 9:14 p.m.
rather than truncating it.

	* sysdeps/mach/hurd/if_index.c (__if_nametoindex): Return ENODEV if
	ifname is too long.
---
 ChangeLog                    | 4 ++--
 sysdeps/mach/hurd/if_index.c | 8 ++++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Andreas Schwab April 3, 2018, 9:24 p.m. | #1
On Apr 03 2018, Samuel Thibault <samuel.thibault@ens-lyon.org> wrote:

> +  strncpy (ifr.ifr_name, ifname, IFNAMESIZ - 1);

That leaves ifr.ifr_name[IFNAMESIZ - 1] uninitialized.

Andreas.
Samuel Thibault April 3, 2018, 10:03 p.m. | #2
Andreas Schwab, on mar. 03 avril 2018 23:24:54 +0200, wrote:
> On Apr 03 2018, Samuel Thibault <samuel.thibault@ens-lyon.org> wrote:
> > +  strncpy (ifr.ifr_name, ifname, IFNAMESIZ - 1);
> 
> That leaves ifr.ifr_name[IFNAMESIZ - 1] uninitialized.

D'oh...

If I wasn't seemingly all alone actually working on patches for the Hurd
port, I'd tend to have more time to take care...

Thanks,
Samuel

Patch

diff --git a/ChangeLog b/ChangeLog
index dd78bf691a..cc9613239d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -35,8 +35,8 @@ 
 
 2018-04-03  Samuel Thibault  <samuel.thibault@ens-lyon.org>
 
-	* sysdeps/mach/hurd/if_index.c (__if_nametoindex): Always end
-	ifr.fr_name with a NUL caracter.
+	* sysdeps/mach/hurd/if_index.c (__if_nametoindex): Return ENODEV if
+	ifname is too long.
 
 2018-04-03  Wilco Dijkstra  <wdijkstr@arm.com>
 
diff --git a/sysdeps/mach/hurd/if_index.c b/sysdeps/mach/hurd/if_index.c
index 7f647b7036..b56bfc7ecd 100644
--- a/sysdeps/mach/hurd/if_index.c
+++ b/sysdeps/mach/hurd/if_index.c
@@ -37,9 +37,13 @@  __if_nametoindex (const char *ifname)
   if (fd < 0)
     return 0;
 
-  strncpy (ifr.ifr_name, ifname, IFNAMSIZ - 1);
-  ifr.ifr_name[IFNAMESIZ - 1] = '\0';
+  if (strlen (ifname) >= IFNAMSIZ)
+    {
+      __set_errno (ENODEV);
+      return 0;
+    }
 
+  strncpy (ifr.ifr_name, ifname, IFNAMESIZ - 1);
   if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
     {
       int saved_errno = errno;