Skip to content

Commit eaf9347

Browse files
committed
Fix segmentation fault when receiving a port scan
1 parent 64f1859 commit eaf9347

12 files changed

Lines changed: 62 additions & 49 deletions

File tree

oor/cmdline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C" {
3131

3232
#ifndef CMDLINE_PARSER_VERSION
3333
/** @brief the program version */
34-
#define CMDLINE_PARSER_VERSION "1.1"
34+
#define CMDLINE_PARSER_VERSION "1.1.1"
3535
#endif
3636

3737
enum enum_debug { debug__NULL = -1, debug_arg_0 = 0, debug_arg_1, debug_arg_2, debug_arg_3 };

oor/control/control-data-plane/tun/cdp_tun.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ tun_control_dp_recv_msg(sock_t *sl)
155155
return (BAD);
156156
}
157157

158+
if (lbuf_size(b) < 4){
159+
OOR_LOG(LDBG_3, "Received a non LISP message in the "
160+
"control port! Discarding packet!");
161+
return (BAD);
162+
}
163+
158164
lbuf_reset_lisp(b);
159165
OOR_LOG(LDBG_1, "Received %s, IP: %s -> %s, UDP: %d -> %d",
160166
lisp_msg_hdr_to_char(b), lisp_addr_to_char(&uc.ra),

oor/control/control-data-plane/vpnapi/cdp_vpnapi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ vpnapi_control_dp_recv_msg(sock_t *sl)
142142
lbuf_del(b);
143143
return (BAD);
144144
}
145+
if (lbuf_size(b) < 4){
146+
OOR_LOG(LDBG_3, "Received a non LISP message in the "
147+
"control port! Discarding packet!");
148+
return (BAD);
149+
}
145150

146151
lbuf_reset_lisp(b);
147152
OOR_LOG(LDBG_1, "Received %s, IP: %s -> %s, UDP: %d -> %d",

oor/data-plane/tun/tun_input.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ tun_read_and_decap_pkt(int sock, lbuf_t *b, uint32_t *iid)
5959
}
6060

6161
udph = pkt_pull_udp(b);
62+
if (ntohs(udplen(udph)) < 16){//8 udp header + 8 lisp header
63+
return (ERR_NOT_ENCAP);
64+
}
6265

6366
/* FILTER UDP: with input RAW UDP sockets, we receive all UDP packets,
6467
* we only want LISP data ones */
65-
switch (ntohs(udph->dest)){
68+
switch (ntohs(udpdport(udph))){
6669
case LISP_DATA_PORT:
6770
lisph = lisp_data_pull_hdr(b);
6871
if (LDHDR_LSB_BIT(lisph)){

oor/data-plane/vpnapi/vpnapi_input.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ vpnapi_read_and_decap_pkt(int sock, lbuf_t *b, uint32_t *iid)
5050
if (sock_data_recv(sock, b, &afi, &ttl, &tos) != GOOD) {
5151
return(BAD);
5252
}
53-
53+
if (lbuf_size(b) < 8){ // 8-> At least LISP header size
54+
return (ERR_NOT_ENCAP);
55+
}
5456

5557
switch (data->encap_type){
5658
case ENCP_LISP:

oor/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ typedef struct htable_nonces_ htable_nonces_t;
8888
*/
8989

9090
#define EVER ;;
91-
#define OOR_VERSION "v1.1"
91+
#define OOR_VERSION "v1.1.1"
9292
#define OOR "oor"
9393
#define PID_FILE "/var/run/oor.pid"
9494

oor/lib/mem_util.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@
6464
#define ARRAY_SIZE(x) ((sizeof x) / (sizeof *x))
6565

6666

67-
/* names for where the udp checksum goes */
68-
#ifdef BSD
69-
#define udpsum(x) x->uh_sum
70-
#else
71-
#define udpsum(x) x->check
72-
#endif
73-
7467

7568
/* compile attributes */
7669
#define NO_RETURN __attribute__((__noreturn__))

oor/lib/packets.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,11 @@ pkt_push_udp(lbuf_t *b, uint16_t sp, uint16_t dp)
9696
udp_len = sizeof(struct udphdr) + lbuf_size(b);
9797
uh = lbuf_push_uninit(b, sizeof(struct udphdr));
9898

99-
#ifdef BSD
100-
uh->uh_sport = htons(port_from);
101-
uh->uh_dport = htons(port_dest);
102-
uh->uh_ulen = htons(udp_payload_len);
103-
uh->uh_sum = 0;
104-
#else
105-
uh->source = htons(sp);
106-
uh->dest = htons(dp);
107-
uh->len = htons(udp_len);
108-
uh->check = 0; /* to be filled in after IP is pushed */
109-
#endif
99+
udpsport(uh) = htons(sp);
100+
udpdport(uh) = htons(dp);
101+
udplen(uh) = htons(udp_len);
102+
udpsum(uh) = 0; /* to be filled in after IP is pushed */
103+
110104
return(uh);
111105
}
112106

@@ -199,7 +193,7 @@ pkt_push_udp_and_ip(lbuf_t *b, uint16_t sp, uint16_t dp, ip_addr_t *sip,
199193
lbuf_reset_ip(b);
200194

201195
uh = lbuf_udp(b);
202-
udpsum = udp_checksum(uh, ntohs(uh->len), lbuf_ip(b), ip_addr_afi(sip));
196+
udpsum = udp_checksum(uh, ntohs(udplen(uh)), lbuf_ip(b), ip_addr_afi(sip));
203197
if (udpsum == -1) {
204198
OOR_LOG(LDBG_1, "Failed UDP checksum! Discarding");
205199
return (BAD);
@@ -246,12 +240,12 @@ pkt_parse_5_tuple(lbuf_t *b, packet_tuple_t *tuple)
246240

247241
if (tuple->protocol == IPPROTO_UDP) {
248242
udp = lbuf_data(&packet);
249-
tuple->src_port = ntohs(udp->source);
250-
tuple->dst_port = ntohs(udp->dest);
243+
tuple->src_port = ntohs(udpsport(udp));
244+
tuple->dst_port = ntohs(udpdport(udp));
251245
} else if (tuple->protocol == IPPROTO_TCP) {
252246
tcp = lbuf_data(&packet);
253-
tuple->src_port = ntohs(tcp->source);
254-
tuple->dst_port = ntohs(tcp->dest);
247+
tuple->src_port = ntohs(tcpsport(tcp));
248+
tuple->dst_port = ntohs(tcpdport(tcp));
255249
} else {
256250
/* If protocol is not TCP or UDP, ports of the tuple set to 0 */
257251
tuple->src_port = 0;
@@ -560,18 +554,11 @@ build_ip_udp_pcket(uint8_t *orig_pkt, int orig_pkt_len,lisp_addr_t *addr_from,
560554
}
561555

562556
/* UDP header */
557+
udpsport(udph_ptr) = htons(port_from);
558+
udpdport(udph_ptr) = htons(port_dest);
559+
udplen(udph_ptr) = htons(udp_hdr_and_payload_len);
560+
udpsum(udph_ptr) = 0;
563561

564-
#ifdef BSD
565-
udph_ptr->uh_sport = htons(port_from);
566-
udph_ptr->uh_dport = htons(port_dest);
567-
udph_ptr->uh_ulen = htons(udp_payload_len);
568-
udph_ptr->uh_sum = 0;
569-
#else
570-
udph_ptr->source = htons(port_from);
571-
udph_ptr->dest = htons(port_dest);
572-
udph_ptr->len = htons(udp_hdr_and_payload_len);
573-
udph_ptr->check = 0;
574-
#endif
575562

576563
/* Copy original packet after the headers */
577564
memcpy(CO(udph_ptr, udp_hdr_len), orig_pkt, orig_pkt_len);

oor/lib/packets.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@
3939
#define MAX_IP_HDR_LEN 40 /* without options or IPv6 hdr extensions */
4040
#define UDP_HDR_LEN 8
4141

42+
#ifdef BSD
43+
#define udpsport(x) x->uh_sport
44+
#define udpdport(x) x->uh_dport
45+
#define udplen(x) x->uh_ulen
46+
#define udpsum(x) x->uh_sum
47+
#else
48+
#define udpsport(x) x->source
49+
#define udpdport(x) x->dest
50+
#define udplen(x) x->len
51+
#define udpsum(x) x->check
52+
#endif
53+
54+
#ifdef BSD
55+
#define tcpsport(x) x->th_sport
56+
#define tcpdport(x) x->th_dport
57+
#else
58+
#define tcpsport(x) x->source
59+
#define tcpdport(x) x->dest
60+
#endif
61+
62+
63+
4264
/* shared between data and control */
4365
typedef struct packet_tuple {
4466
lisp_addr_t src_addr;
@@ -50,6 +72,7 @@ typedef struct packet_tuple {
5072
} packet_tuple_t;
5173

5274

75+
5376
/*
5477
* Generate IP header. Returns the poninter to the transport header
5578
*/

oor/liblisp/liblisp.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ lisp_msg_ecm_decap(lbuf_t *pkt, uint16_t *src_port)
6767

6868
/* This should overwrite the external port (dst_port in map-reply =
6969
* inner src_port in encap map-request) */
70-
*src_port = ntohs(udph->source);
71-
72-
#ifdef BSD
73-
udp_len = ntohs(udph->uh_ulen);
74-
#else
75-
udp_len = ntohs(udph->len);
76-
#endif
70+
*src_port = ntohs(udpsport(udph));
71+
udp_len = ntohs(udplen(udph));
7772

7873
/* Verify the checksums. */
7974
if (iph->ip_v == IPVERSION) {
@@ -86,7 +81,7 @@ lisp_msg_ecm_decap(lbuf_t *pkt, uint16_t *src_port)
8681

8782
/* Verify UDP checksum only if different from 0.
8883
* This means we ACCEPT UDP checksum 0! */
89-
if (udph->check != 0) {
84+
if (udpsum(udph) != 0) {
9085
udpsum = udp_checksum(udph, udp_len, iph,
9186
ip_version_to_sock_afi(iph->ip_v));
9287
if (udpsum != 0) {
@@ -99,7 +94,7 @@ lisp_msg_ecm_decap(lbuf_t *pkt, uint16_t *src_port)
9994
lisp_msg_hdr_to_char(pkt),
10095
ip_to_char(&iph->ip_src, ip_version_to_sock_afi(iph->ip_v)),
10196
ip_to_char(&iph->ip_dst, ip_version_to_sock_afi(iph->ip_v)),
102-
ntohs(udph->source), ntohs(udph->dest));
97+
ntohs(udpsport(udph)), ntohs(udpdport(udph)));
10398

10499
return (GOOD);
105100
}

0 commit comments

Comments
 (0)