Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

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")
Signed-off-by: Tanner Love <tannerlove@google.com>
Acked-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

tannerlove and committed by
David S. Miller
8027bc03 07a86b01

+8 -2
+8 -2
tools/testing/selftests/net/timestamping.c
··· 313 313 int val; 314 314 socklen_t len; 315 315 struct timeval next; 316 + size_t if_len; 316 317 317 318 if (argc < 2) 318 319 usage(0); 319 320 interface = argv[1]; 321 + if_len = strlen(interface); 322 + if (if_len >= IFNAMSIZ) { 323 + printf("interface name exceeds IFNAMSIZ\n"); 324 + exit(1); 325 + } 320 326 321 327 for (i = 2; i < argc; i++) { 322 328 if (!strcasecmp(argv[i], "SO_TIMESTAMP")) ··· 356 350 bail("socket"); 357 351 358 352 memset(&device, 0, sizeof(device)); 359 - strncpy(device.ifr_name, interface, sizeof(device.ifr_name)); 353 + memcpy(device.ifr_name, interface, if_len + 1); 360 354 if (ioctl(sock, SIOCGIFADDR, &device) < 0) 361 355 bail("getting interface IP address"); 362 356 363 357 memset(&hwtstamp, 0, sizeof(hwtstamp)); 364 - strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name)); 358 + memcpy(hwtstamp.ifr_name, interface, if_len + 1); 365 359 hwtstamp.ifr_data = (void *)&hwconfig; 366 360 memset(&hwconfig, 0, sizeof(hwconfig)); 367 361 hwconfig.tx_type =