]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ether-addr-util: make ether_addr_from_string() stricter
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 4 May 2018 08:36:40 +0000 (17:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 9 May 2018 02:57:36 +0000 (11:57 +0900)
src/basic/ether-addr-util.c
src/basic/ether-addr-util.h
src/libsystemd-network/network-internal.c
src/network/test-networkd-conf.c

index e6ca7fa52690f57f407b01bdfa232989b05e2c72..7e08ba09f0a7c13811366f367c5e77ab48dc677f 100644 (file)
@@ -45,7 +45,7 @@ bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
                 a->ether_addr_octet[5] == b->ether_addr_octet[5];
 }
 
-int ether_addr_from_string(const char *s, struct ether_addr *ret, size_t *offset) {
+int ether_addr_from_string(const char *s, struct ether_addr *ret) {
         size_t pos = 0, n, field;
         char sep = '\0';
         const char *hex = HEXDIGITS, *hexoff;
@@ -84,31 +84,35 @@ int ether_addr_from_string(const char *s, struct ether_addr *ret, size_t *offset
         assert(s);
         assert(ret);
 
+        s += strspn(s, WHITESPACE);
         sep = s[strspn(s, hex)];
-        if (sep == '\n')
-                return -EINVAL;
-        if (!strchr(":.-", sep))
-                return -EINVAL;
 
         if (sep == '.') {
                 uint16_t shorts[3] = { 0 };
 
                 parse_fields(shorts);
 
+                if (s[pos] != '\0')
+                        return -EINVAL;
+
                 for (n = 0; n < ELEMENTSOF(shorts); n++) {
                         ret->ether_addr_octet[2*n] = ((shorts[n] & (uint16_t)0xff00) >> 8);
                         ret->ether_addr_octet[2*n + 1] = (shorts[n] & (uint16_t)0x00ff);
                 }
-        } else {
-                struct ether_addr out = { .ether_addr_octet = { 0 } };
+
+        } else if (IN_SET(sep, ':', '-')) {
+                struct ether_addr out = ETHER_ADDR_NULL;
 
                 parse_fields(out.ether_addr_octet);
 
+                if (s[pos] != '\0')
+                        return -EINVAL;
+
                 for (n = 0; n < ELEMENTSOF(out.ether_addr_octet); n++)
                         ret->ether_addr_octet[n] = out.ether_addr_octet[n];
-        }
 
-        if (offset)
-                *offset = pos;
+        } else
+                return -EINVAL;
+
         return 0;
 }
index 29d7f362944f1347c65e50ba02376d75a0fbc320..11ff72a2359e4c8e3e9ef6011b1f7c3c9d338476 100644 (file)
@@ -24,4 +24,4 @@ static inline bool ether_addr_is_null(const struct ether_addr *addr) {
         return ether_addr_equal(addr, &ETHER_ADDR_NULL);
 }
 
-int ether_addr_from_string(const char *s, struct ether_addr *ret, size_t *offset);
+int ether_addr_from_string(const char *s, struct ether_addr *ret);
index 620f7115456d8e0263ca956c1fb3c98a2837c7e8..9a11c0d975cc8e6cd0c661747f896fdb93024d41 100644 (file)
@@ -281,10 +281,9 @@ int config_parse_hwaddr(const char *unit,
                         const char *rvalue,
                         void *data,
                         void *userdata) {
+
+        _cleanup_free_ struct ether_addr *n = NULL;
         struct ether_addr **hwaddr = data;
-        struct ether_addr *n;
-        const char *start;
-        size_t offset;
         int r;
 
         assert(filename);
@@ -296,17 +295,13 @@ int config_parse_hwaddr(const char *unit,
         if (!n)
                 return log_oom();
 
-        start = rvalue + strspn(rvalue, WHITESPACE);
-        r = ether_addr_from_string(start, n, &offset);
-
-        if (r || (start[offset + strspn(start + offset, WHITESPACE)] != '\0')) {
-                log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
-                free(n);
+        r = ether_addr_from_string(rvalue, n);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
-        free(*hwaddr);
-        *hwaddr = n;
+        *hwaddr = TAKE_PTR(n);
 
         return 0;
 }
index b966fbd96092cacf911a0fb9bb3eec32712b6b9f..e24fea9c804cb1a4a127fdf25b328afc5114c408 100644 (file)
@@ -58,10 +58,10 @@ static void test_config_parse_hwaddr_one(const char *rvalue, int ret, const stru
         assert_se(ret == r);
         if (expected) {
                 assert_se(actual);
-                assert(ether_addr_equal(expected, actual));
-        } else {
+                assert_se(ether_addr_equal(expected, actual));
+        } else
                 assert_se(actual == NULL);
-        }
+
         free(actual);
 }
 
@@ -90,12 +90,13 @@ static void test_config_parse_hwaddr(void) {
                 { .ether_addr_octet = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff } },
                 { .ether_addr_octet = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } },
         };
+
         test_config_parse_hwaddr_one("", 0, NULL);
         test_config_parse_hwaddr_one("no:ta:ma:ca:dd:re", 0, NULL);
         test_config_parse_hwaddr_one("aa:bb:cc:dd:ee:fx", 0, NULL);
         test_config_parse_hwaddr_one("aa:bb:cc:dd:ee:ff", 0, &t[0]);
         test_config_parse_hwaddr_one(" aa:bb:cc:dd:ee:ff", 0, &t[0]);
-        test_config_parse_hwaddr_one("aa:bb:cc:dd:ee:ff \t\n", 0, &t[0]);
+        test_config_parse_hwaddr_one("aa:bb:cc:dd:ee:ff \t\n", 0, NULL);
         test_config_parse_hwaddr_one("aa:bb:cc:dd:ee:ff \t\nxxx", 0, NULL);
         test_config_parse_hwaddr_one("aa:bb:cc: dd:ee:ff", 0, NULL);
         test_config_parse_hwaddr_one("aa:bb:cc:d d:ee:ff", 0, NULL);