Commit 777d90fa authored by Neel Chauhan's avatar Neel Chauhan Committed by Nick Mathewson
Browse files

Check for leading zeros in tor_inet_aton()

parent 1b63eea6
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -15,3 +15,4 @@ lib/string/*.h
lib/subsys/*.h
lib/testsupport/*.h
lib/malloc/*.h
lib/smartlist_core/*.h
+21 −0
Original line number Diff line number Diff line
@@ -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;
+25 −6
Original line number Diff line number Diff line
@@ -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
};