0
0
mirror of https://gitlab.torproject.org/tpo/core/tor.git synced 2024-09-20 12:22:14 +02:00

Check for leading zeros in tor_inet_aton()

This commit is contained in:
Neel Chauhan 2020-01-06 20:12:11 -08:00 committed by Nick Mathewson
parent 1b63eea66c
commit 777d90fa23
3 changed files with 48 additions and 7 deletions

View File

@ -14,4 +14,5 @@ lib/net/*.h
lib/string/*.h
lib/subsys/*.h
lib/testsupport/*.h
lib/malloc/*.h
lib/malloc/*.h
lib/smartlist_core/*.h

View 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;

View 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
};