Skip to content

Commit

Permalink
net: use scnprintf() to avoid potential buffer overflow
Browse files Browse the repository at this point in the history
strlcpy() returns the total length of the string they tried to create, so
we should not use its return value without any check. scnprintf() returns
the number of characters written into @buf not including the trailing '\0',
so use it instead here.

Signed-off-by: Changli Gao <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
xiaosuo authored and davem330 committed Aug 26, 2010
1 parent 145ce50 commit 53f91dc
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions net/ethernet/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count)
EXPORT_SYMBOL(alloc_etherdev_mq);

static size_t _format_mac_addr(char *buf, int buflen,
const unsigned char *addr, int len)
const unsigned char *addr, int len)
{
int i;
char *cp = buf;
Expand All @@ -376,7 +376,7 @@ static size_t _format_mac_addr(char *buf, int buflen,
cp += scnprintf(cp, buflen - (cp - buf), "%02x", addr[i]);
if (i == len - 1)
break;
cp += strlcpy(cp, ":", buflen - (cp - buf));
cp += scnprintf(cp, buflen - (cp - buf), ":");
}
return cp - buf;
}
Expand All @@ -386,7 +386,7 @@ ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)
size_t l;

l = _format_mac_addr(buf, PAGE_SIZE, addr, len);
l += strlcpy(buf + l, "\n", PAGE_SIZE - l);
l += scnprintf(buf + l, PAGE_SIZE - l, "\n");
return ((ssize_t) l);
}
EXPORT_SYMBOL(sysfs_format_mac);

0 comments on commit 53f91dc

Please sign in to comment.