Skip to content

Commit 0f9024d

Browse files
committed
Fix overflow in test + add correct structures for encapsulation of
packets in ICMP
1 parent f98e46b commit 0f9024d

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

src/test/unit/unit_tests_dns_dhcp.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ START_TEST(test_icmp_input_dest_unreach_port_unreachable_closes_matching_tcp_soc
20392039
struct wolfIP s;
20402040
struct tsocket *ts;
20412041
struct wolfIP_icmp_dest_unreachable_packet icmp;
2042-
struct wolfIP_tcp_seg *orig;
2042+
struct wolfIP_tcp_wire_prefix *orig;
20432043
uint32_t frame_len;
20442044

20452045
wolfIP_init(&s);
@@ -2065,15 +2065,14 @@ START_TEST(test_icmp_input_dest_unreach_port_unreachable_closes_matching_tcp_soc
20652065
icmp.type = ICMP_DEST_UNREACH;
20662066
icmp.code = ICMP_PORT_UNREACH;
20672067

2068-
orig = (struct wolfIP_tcp_seg *)icmp.orig_packet;
2068+
orig = (struct wolfIP_tcp_wire_prefix *)icmp.orig_packet;
20692069
orig->ip.ver_ihl = 0x45;
20702070
orig->ip.proto = WI_IPPROTO_TCP;
20712071
orig->ip.src = ee32(ts->local_ip);
20722072
orig->ip.dst = ee32(ts->remote_ip);
2073-
orig->ip.len = ee16(IP_HEADER_LEN + TCP_HEADER_LEN);
2073+
orig->ip.len = ee16(IP_HEADER_LEN + 8U);
20742074
orig->src_port = ee16(ts->src_port);
20752075
orig->dst_port = ee16(ts->dst_port);
2076-
orig->hlen = TCP_HEADER_LEN << 2;
20772076

20782077
icmp.csum = ee16(icmp_checksum((struct wolfIP_icmp_packet *)&icmp,
20792078
ICMP_DEST_UNREACH_SIZE));
@@ -2090,7 +2089,7 @@ START_TEST(test_icmp_input_dest_unreach_frag_needed_reduces_tcp_peer_mss)
20902089
struct wolfIP s;
20912090
struct tsocket *ts;
20922091
struct wolfIP_icmp_dest_unreachable_packet icmp;
2093-
struct wolfIP_tcp_seg *orig;
2092+
struct wolfIP_tcp_wire_prefix *orig;
20942093
uint32_t frame_len;
20952094
uint16_t next_hop_mtu;
20962095

@@ -2116,19 +2115,18 @@ START_TEST(test_icmp_input_dest_unreach_frag_needed_reduces_tcp_peer_mss)
21162115
icmp.ip.proto = WI_IPPROTO_ICMP;
21172116
icmp.ip.len = ee16(IP_HEADER_LEN + ICMP_DEST_UNREACH_SIZE);
21182117
icmp.type = ICMP_DEST_UNREACH;
2119-
icmp.code = 4;
2118+
icmp.code = ICMP_FRAG_NEEDED;
21202119
next_hop_mtu = ee16(576U);
21212120
memcpy(&icmp.unused[2], &next_hop_mtu, sizeof(next_hop_mtu));
21222121

2123-
orig = (struct wolfIP_tcp_seg *)icmp.orig_packet;
2122+
orig = (struct wolfIP_tcp_wire_prefix *)icmp.orig_packet;
21242123
orig->ip.ver_ihl = 0x45;
21252124
orig->ip.proto = WI_IPPROTO_TCP;
21262125
orig->ip.src = ee32(ts->local_ip);
21272126
orig->ip.dst = ee32(ts->remote_ip);
2128-
orig->ip.len = ee16(IP_HEADER_LEN + TCP_HEADER_LEN);
2127+
orig->ip.len = ee16(IP_HEADER_LEN + 8U);
21292128
orig->src_port = ee16(ts->src_port);
21302129
orig->dst_port = ee16(ts->dst_port);
2131-
orig->hlen = TCP_HEADER_LEN << 2;
21322130

21332131
icmp.csum = ee16(icmp_checksum((struct wolfIP_icmp_packet *)&icmp,
21342132
ICMP_DEST_UNREACH_SIZE));

src/wolfip.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,16 @@ struct PACKED wolfIP_ip_packet {
655655
uint8_t data[0];
656656
};
657657

658+
/* ICMP quotes the original IP packet without the link-layer header. */
659+
struct PACKED wolfIP_ip_wire {
660+
uint8_t ver_ihl, tos;
661+
uint16_t len, id, flags_fo;
662+
uint8_t ttl, proto;
663+
uint16_t csum;
664+
ip4 src, dst;
665+
uint8_t data[0];
666+
};
667+
658668
/* Describe a TCP segment down to the datalink layer */
659669
struct PACKED wolfIP_tcp_seg {
660670
struct wolfIP_ip_packet ip;
@@ -665,6 +675,12 @@ struct PACKED wolfIP_tcp_seg {
665675
uint8_t data[0];
666676
};
667677

678+
struct PACKED wolfIP_tcp_wire_prefix {
679+
struct wolfIP_ip_wire ip;
680+
uint16_t src_port, dst_port;
681+
uint32_t seq;
682+
};
683+
668684
struct PACKED tcp_opt_ts {
669685
/* Timestamp option (10 extra bytes) */
670686
uint8_t opt, len;
@@ -1967,8 +1983,8 @@ static void icmp_try_recv(struct wolfIP *s, unsigned int if_idx,
19671983
static void icmp_try_deliver_tcp_error(struct wolfIP *s,
19681984
const struct wolfIP_icmp_packet *icmp)
19691985
{
1970-
const struct wolfIP_ip_packet *orig_ip;
1971-
const struct wolfIP_tcp_seg *orig_tcp;
1986+
const struct wolfIP_ip_wire *orig_ip;
1987+
const struct wolfIP_tcp_wire_prefix *orig_tcp;
19721988
uint32_t icmp_len;
19731989
uint32_t avail;
19741990
uint32_t orig_hlen;
@@ -1986,7 +2002,7 @@ static void icmp_try_deliver_tcp_error(struct wolfIP *s,
19862002
if (avail < IP_HEADER_LEN)
19872003
return;
19882004

1989-
orig_ip = (const struct wolfIP_ip_packet *)((const uint8_t *)icmp +
2005+
orig_ip = (const struct wolfIP_ip_wire *)((const uint8_t *)icmp +
19902006
sizeof(struct wolfIP_icmp_packet));
19912007
orig_hlen = (uint32_t)((orig_ip->ver_ihl & 0x0FU) << 2);
19922008
if (orig_hlen < IP_HEADER_LEN || orig_hlen > avail)
@@ -1996,7 +2012,7 @@ static void icmp_try_deliver_tcp_error(struct wolfIP *s,
19962012
if (avail < (orig_hlen + 8U))
19972013
return;
19982014

1999-
orig_tcp = (const struct wolfIP_tcp_seg *)orig_ip;
2015+
orig_tcp = (const struct wolfIP_tcp_wire_prefix *)orig_ip;
20002016
for (i = 0; i < MAX_TCPSOCKETS; i++) {
20012017
struct tsocket *t = &s->tcpsockets[i];
20022018

0 commit comments

Comments
 (0)