]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Check for leading zeros in tor_inet_aton()
authorNeel Chauhan <neel@neelc.org>
Tue, 7 Jan 2020 04:12:11 +0000 (20:12 -0800)
committerNick Mathewson <nickm@torproject.org>
Tue, 11 Feb 2020 13:47:13 +0000 (08:47 -0500)
src/lib/net/.may_include
src/lib/net/inaddr.c
src/test/test_addr.c

index e4368f799bc08b560a9bf30bbff3d487c3fc7bf2..6e9af9737a35f67944ee42befaa125045ff14afc 100644 (file)
@@ -14,4 +14,5 @@ lib/net/*.h
 lib/string/*.h
 lib/subsys/*.h
 lib/testsupport/*.h
-lib/malloc/*.h
\ No newline at end of file
+lib/malloc/*.h
+lib/smartlist_core/*.h
index a655ca6ad82da681fc50ec18726f7b10a074c0cb..7ae376b1193358227c21edd5cc6720ace3b476e5 100644 (file)
@@ -11,7 +11,9 @@
 #include "lib/net/inaddr.h"
 
 #include "lib/cc/torint.h"
+#include "lib/container/smartlist.h"
 #include "lib/log/util_bug.h"
+#include "lib/malloc/malloc.h"
 #include "lib/net/inaddr_st.h"
 #include "lib/string/compat_ctype.h"
 #include "lib/string/compat_string.h"
@@ -39,8 +41,27 @@ tor_inet_aton(const char *str, struct in_addr *addr)
 {
   unsigned a,b,c,d;
   char more;
+  bool is_octal = false;
+  smartlist_t *sl = NULL;
+
   if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a, &b, &c, &d, &more) != 4)
     return 0;
+
+  /* Parse the octets and check them for leading zeros. */
+  sl = smartlist_new();
+  smartlist_split_string(sl, str, ".", 0, 0);
+  SMARTLIST_FOREACH(sl, const char *, octet, {
+    is_octal = (strlen(octet) > 1 && octet[0] == '0');
+    if (is_octal) {
+        break;
+    }
+  });
+  SMARTLIST_FOREACH(sl, char *, octet, tor_free(octet));
+  smartlist_free(sl);
+
+  if (is_octal)
+    return 0;
+
   if (a > 255) return 0;
   if (b > 255) return 0;
   if (c > 255) return 0;
index 04380d1ccf6019cc8beac045a1070145dea9ed51..3ca5d7986263d1cc52427c8bf848fc9ca79491a1 100644 (file)
@@ -659,12 +659,7 @@ test_addr_ip6_helpers(void *arg)
   tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
   tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010202);
   r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
-  tt_int_op(r, OP_EQ, AF_INET);
-  tt_int_op(mask,OP_EQ,32);
-  tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
-  tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x03041020);
-  tt_uint_op(port1, OP_EQ, 1);
-  tt_uint_op(port2, OP_EQ, 2);
+  tt_int_op(r, OP_EQ, -1);
   r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
   tt_int_op(r, OP_EQ, AF_INET);
   tt_int_op(mask,OP_EQ,17);
@@ -1653,6 +1648,29 @@ test_addr_rfc6598(void *arg)
   ;
 }
 
+#define TEST_ADDR_ATON(a, rv) STMT_BEGIN \
+    struct in_addr addr; \
+    tt_int_op(tor_inet_aton(a, &addr), OP_EQ, rv); \
+  STMT_END;
+
+static void
+test_addr_octal(void *arg)
+{
+  (void)arg;
+
+  /* Test non-octal IP addresses. */
+  TEST_ADDR_ATON("0.1.2.3", 1);
+  TEST_ADDR_ATON("1.0.2.3", 1);
+  TEST_ADDR_ATON("1.2.3.0", 1);
+
+  /* Test octal IP addresses. */
+  TEST_ADDR_ATON("01.1.2.3", 0);
+  TEST_ADDR_ATON("1.02.3.4", 0);
+  TEST_ADDR_ATON("1.2.3.04", 0);
+ done:
+  ;
+}
+
 #ifndef COCCI
 #define ADDR_LEGACY(name)                                               \
   { #name, test_addr_ ## name , 0, NULL, NULL }
@@ -1671,5 +1689,6 @@ struct testcase_t addr_tests[] = {
   { "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
   { "make_null", test_addr_make_null, 0, NULL, NULL },
   { "rfc6598", test_addr_rfc6598, 0, NULL, NULL },
+  { "octal", test_addr_octal, 0, NULL, NULL },
   END_OF_TESTCASES
 };