Restrict packet kinds that can be sent through onion path.

This commit is contained in:
Evgeny Kurnevsky 2018-04-15 09:27:17 +03:00
parent 09478f9957
commit 6b97acb773
No known key found for this signature in database
GPG Key ID: 8FF444E5412D750C
2 changed files with 33 additions and 9 deletions

View File

@ -48,12 +48,12 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui
{
Onion *onion = (Onion *)object;
if (memcmp(packet, "Install Gentoo", sizeof("Install Gentoo")) != 0) {
if (memcmp(packet, "\x83 Install Gentoo", sizeof("\x83 Install Gentoo")) != 0) {
return 1;
}
if (send_onion_response(onion->net, source, (const uint8_t *)"install gentoo", sizeof("install gentoo"),
packet + sizeof("Install Gentoo")) == -1) {
if (send_onion_response(onion->net, source, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo"),
packet + sizeof("\x84 install gentoo")) == -1) {
return 1;
}
@ -64,11 +64,11 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui
static int handled_test_2;
static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
{
if (length != sizeof("install Gentoo")) {
if (length != sizeof("\x84 install gentoo")) {
return 1;
}
if (memcmp(packet, (const uint8_t *)"install gentoo", sizeof("install gentoo")) != 0) {
if (memcmp(packet, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo")) != 0) {
return 1;
}
@ -170,7 +170,7 @@ START_TEST(test_basic)
Onion *onion1 = new_onion(new_DHT(log1, new_networking(log1, ip, 34567), true));
Onion *onion2 = new_onion(new_DHT(log2, new_networking(log2, ip, 34568), true));
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2);
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
IP_Port on1 = {ip, net_port(onion1->net)};
Node_format n1;
@ -189,8 +189,8 @@ START_TEST(test_basic)
nodes[3] = n2;
Onion_Path path;
create_onion_path(onion1->dht, &path, nodes);
int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, (const uint8_t *)"Install Gentoo",
sizeof("Install Gentoo"));
int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, (const uint8_t *)"\x83 Install Gentoo",
sizeof("\x83 Install Gentoo"));
ck_assert_msg(ret == 0, "Failed to create/send onion packet.");
handled_test_1 = 0;
@ -200,7 +200,7 @@ START_TEST(test_basic)
do_onion(onion2);
}
networking_registerhandler(onion1->net, 'i', &handle_test_2, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_2, onion1);
handled_test_2 = 0;
while (handled_test_2 == 0) {

View File

@ -471,6 +471,15 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
return 1;
}
if (len <= SIZE_IPPORT) {
return 1;
}
if (plain[SIZE_IPPORT] != NET_PACKET_ANNOUNCE_REQUEST &&
plain[SIZE_IPPORT] != NET_PACKET_ONION_DATA_REQUEST) {
return 1;
}
IP_Port send_to;
if (ipport_unpack(&send_to, plain, len, 0) == -1) {
@ -514,6 +523,11 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
return 1;
}
if (packet[1 + RETURN_3] != NET_PACKET_ANNOUNCE_RESPONSE &&
packet[1 + RETURN_3] != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}
change_symmetric_key(onion);
uint8_t plain[SIZE_IPPORT + RETURN_2];
@ -555,6 +569,11 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
return 1;
}
if (packet[1 + RETURN_2] != NET_PACKET_ANNOUNCE_RESPONSE &&
packet[1 + RETURN_2] != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}
change_symmetric_key(onion);
uint8_t plain[SIZE_IPPORT + RETURN_1];
@ -596,6 +615,11 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
return 1;
}
if (packet[1 + RETURN_1] != NET_PACKET_ANNOUNCE_RESPONSE &&
packet[1 + RETURN_1] != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}
change_symmetric_key(onion);
uint8_t plain[SIZE_IPPORT];