Skip to content

Commit

Permalink
selftests/net: in timestamping, strncpy needs to preserve null byte
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
tannerlove authored and davem330 committed Jun 9, 2020
1 parent 07a86b0 commit 8027bc0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tools/testing/selftests/net/timestamping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 8027bc0

Please sign in to comment.