Skip to contentSkip to conversation

selftests/net: in timestamping, strncpy needs to preserve null byte

PATCH Applied Tanner Love 2 participants 2 messages Jun 8, 2020 – Jun 9, 2020

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

PATCH 1/1 selftests/net: in timestamping, strncpy needs to preserve null byte

From: tannerlove <[email protected]>

If user passed an interface option longer than 15 characters, then device.ifr_name and hwtstamp.ifr_name became non-null-terminated strings. The compiler warned about this:

timestamping.c:353:2: warning: ‘strncpy’ specified bound 16 equals \
destination size [-Wstringop-truncation]
353 | strncpy(device.ifr_name, interface, sizeof(device.ifr_name));

Fixes cb9eff097831 ("net: new user space API for time stamping of incoming and outgoing packets")
---
 tools/testing/selftests/net/timestamping.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/net/timestamping.c +8 -2
index aca3491174a1..f4bb4fef0f39 100644
@@ -313,10 +313,16 @@ int main(int argc, char **argv)
 	int val;
 	socklen_t len;
 	struct timeval next;
+	size_t if_len;
 
 	if (argc < 2)
 		usage(0);
 	interface = argv[1];
+	if_len = strlen(interface);
+	if (if_len >= IFNAMSIZ) {
+		printf("interface name exceeds IFNAMSIZ\n");
+		exit(1);
+	}
 
 	for (i = 2; i < argc; i++) {
 		if (!strcasecmp(argv[i], "SO_TIMESTAMP"))
@@ -350,12 +356,12 @@ int main(int argc, char **argv)
 		bail("socket");
 
 	memset(&device, 0, sizeof(device));
-	strncpy(device.ifr_name, interface, sizeof(device.ifr_name));
+	memcpy(device.ifr_name, interface, if_len + 1);
 	if (ioctl(sock, SIOCGIFADDR, &device) < 0)
 		bail("getting interface IP address");
 
 	memset(&hwtstamp, 0, sizeof(hwtstamp));
-	strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));
+	memcpy(hwtstamp.ifr_name, interface, if_len + 1);
 	hwtstamp.ifr_data = (void *)&hwconfig;
 	memset(&hwconfig, 0, sizeof(hwconfig));
 	hwconfig.tx_type =
Signature · 1 line
2.27.0.278.ge193c7cf3a9-goog
maintainer Applied
#1 lore raw

From: Tanner Love <[email protected]>
Date: Mon, 8 Jun 2020 15:37:15 -0400

Quoted 6 lines

From: tannerlove <[email protected]>

If user passed an interface option longer than 15 characters, then device.ifr_name and hwtstamp.ifr_name became non-null-terminated strings. The compiler warned about this:

Quoted 7 lines

timestamping.c:353:2: warning: ‘strncpy’ specified bound 16 equals \
destination size [-Wstringop-truncation]
353 | strncpy(device.ifr_name, interface, sizeof(device.ifr_name));

Fixes cb9eff097831 ("net: new user space API for time stamping of incoming and outgoing packets")

Applied, thank you.