mac_pton: Don't access memory over expected length
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 8 Nov 2022 14:11:08 +0000 (16:11 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 10 Nov 2022 03:28:02 +0000 (19:28 -0800)
The strlen() may go too far when estimating the length of
the given string. In some cases it may go over the boundary
and crash the system which is the case according to the commit
13a55372b64e ("ARM: orion5x: Revert commit 4904dbda41c8.").

Rectify this by switching to strnlen() for the expected
maximum length of the string.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20221108141108.62974-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
lib/net_utils.c

index af525353395d679001d98f73d1411e2d0dbbecda..c17201df3d08142bfc0e64b5903d0838afa710bf 100644 (file)
@@ -6,10 +6,11 @@
 
 bool mac_pton(const char *s, u8 *mac)
 {
+       size_t maxlen = 3 * ETH_ALEN - 1;
        int i;
 
        /* XX:XX:XX:XX:XX:XX */
-       if (strlen(s) < 3 * ETH_ALEN - 1)
+       if (strnlen(s, maxlen) < maxlen)
                return false;
 
        /* Don't dirty result unless string is valid MAC. */