Skip to contentSkip to conversation

ethtool.c:fix buffer overflow when devname is too long

PATCH In review [email protected] 2 participants 2 messages Aug 30, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ethtool.c:fix buffer overflow when devname is too long

As the length of field ifr_name of struct ifreq is IFNAMSIZ(16) in header file /usr/include/net/if.h. It will result in buffer overflow when devname is too long. Modified strcpy to strncpy for only copying IFNAMSIZ bytes into struct ifreq. Also, by adding a section into parse_cmdline to detect if the length of devname is invalid.

ethtool.c +4 -1
@@ -626,6 +626,9 @@ static void parse_cmdline(int argc, char
 
 	if (devname == NULL) {
 		show_usage(1);
+	} else if (strlen(devname) > IFNAMSIZ) {
+		fprintf(stderr, "Device name is too long. Should be less than %d!\n", IFNAMSIZ);
+		show_usage(1);
 	}
 }
 
@@ -1139,7 +1142,7 @@ static int doit(void)
 
 	/* Setup our control structures. */
 	memset(&ifr, 0, sizeof(ifr));
-	strcpy(ifr.ifr_name, devname);
+	strncpy(ifr.ifr_name, devname, IFNAMSIZ);
 
 	/* Open control socket. */
 	fd = socket(AF_INET, SOCK_DGRAM, 0);
#1 lore raw
Quoted 3 lines · [email protected] wrote:

As the length of field ifr_name of struct ifreq is IFNAMSIZ(16) in header file /usr/include/net/if.h. It will result in buffer overflow when devname is too long. Modified strcpy to strncpy for only copying IFNAMSIZ bytes into struct ifreq. Also, by adding a section into parse_cmdline to detect if the length of devname is invalid.

There's already a patch checked into ethtool.git for this...

Jeff