Skip to content

Commit 8027bc0

Browse files
tannerlovedavem330
authored andcommitted
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: cb9eff0 ("net: new user space API for time stamping of incoming and outgoing packets") Signed-off-by: Tanner Love <[email protected]> Acked-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 07a86b0 commit 8027bc0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

tools/testing/selftests/net/timestamping.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,16 @@ int main(int argc, char **argv)
313313
int val;
314314
socklen_t len;
315315
struct timeval next;
316+
size_t if_len;
316317

317318
if (argc < 2)
318319
usage(0);
319320
interface = argv[1];
321+
if_len = strlen(interface);
322+
if (if_len >= IFNAMSIZ) {
323+
printf("interface name exceeds IFNAMSIZ\n");
324+
exit(1);
325+
}
320326

321327
for (i = 2; i < argc; i++) {
322328
if (!strcasecmp(argv[i], "SO_TIMESTAMP"))
@@ -350,12 +356,12 @@ int main(int argc, char **argv)
350356
bail("socket");
351357

352358
memset(&device, 0, sizeof(device));
353-
strncpy(device.ifr_name, interface, sizeof(device.ifr_name));
359+
memcpy(device.ifr_name, interface, if_len + 1);
354360
if (ioctl(sock, SIOCGIFADDR, &device) < 0)
355361
bail("getting interface IP address");
356362

357363
memset(&hwtstamp, 0, sizeof(hwtstamp));
358-
strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));
364+
memcpy(hwtstamp.ifr_name, interface, if_len + 1);
359365
hwtstamp.ifr_data = (void *)&hwconfig;
360366
memset(&hwconfig, 0, sizeof(hwconfig));
361367
hwconfig.tx_type =

0 commit comments

Comments
 (0)