1 /*	$NetBSD: isakmp_frag.c,v 1.10 2018/10/05 20:12:37 christos Exp $	*/
2 
3 /* Id: isakmp_frag.c,v 1.4 2004/11/13 17:31:36 manubsd Exp */
4 
5 /*
6  * Copyright (C) 2004 Emmanuel Dreyfus
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "config.h"
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
40 
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 
44 #include <openssl/md5.h>
45 
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <errno.h>
51 #if TIME_WITH_SYS_TIME
52 # include <sys/time.h>
53 # include <time.h>
54 #else
55 # if HAVE_SYS_TIME_H
56 #  include <sys/time.h>
57 # else
58 #  include <time.h>
59 # endif
60 #endif
61 #include <netdb.h>
62 #ifdef HAVE_UNISTD_H
63 #include <unistd.h>
64 #endif
65 #include <ctype.h>
66 
67 #include "var.h"
68 #include "misc.h"
69 #include "vmbuf.h"
70 #include "plog.h"
71 #include "sockmisc.h"
72 #include "schedule.h"
73 #include "debug.h"
74 
75 #include "isakmp_var.h"
76 #include "isakmp.h"
77 #include "handler.h"
78 #include "isakmp_frag.h"
79 #include "strnames.h"
80 
81 int
isakmp_sendfrags(iph1,buf)82 isakmp_sendfrags(iph1, buf)
83 	struct ph1handle *iph1;
84 	vchar_t *buf;
85 {
86 	struct isakmp *hdr;
87 	struct isakmp_frag *fraghdr;
88 	caddr_t data;
89 	caddr_t sdata;
90 	size_t datalen;
91 	size_t max_datalen;
92 	size_t fraglen;
93 	vchar_t *frag;
94 	unsigned int trailer;
95 	unsigned int fragnum = 0;
96 	size_t len;
97 	int etype;
98 
99 	/*
100 	 * Catch the exchange type for later: the fragments and the
101 	 * fragmented packet must have the same exchange type.
102 	 */
103 	hdr = (struct isakmp *)buf->v;
104 	etype = hdr->etype;
105 
106 	/*
107 	 * We want to send a a packet smaller than ISAKMP_FRAG_MAXLEN
108 	 * First compute the maximum data length that will fit in it
109 	 */
110 	max_datalen = ISAKMP_FRAG_MAXLEN -
111 	    (sizeof(*hdr) + sizeof(*fraghdr) + sizeof(trailer));
112 
113 	sdata = buf->v;
114 	len = buf->l;
115 
116 	while (len > 0) {
117 		fragnum++;
118 
119 		if (len > max_datalen)
120 			datalen = max_datalen;
121 		else
122 			datalen = len;
123 
124 		fraglen = sizeof(*hdr)
125 			+ sizeof(*fraghdr)
126 			+ datalen;
127 
128 		if ((frag = vmalloc(fraglen)) == NULL) {
129 			plog(LLV_ERROR, LOCATION, NULL,
130 			    "Cannot allocate memory\n");
131 			return -1;
132 		}
133 
134 		set_isakmp_header1(frag, iph1, ISAKMP_NPTYPE_FRAG);
135 		hdr = (struct isakmp *)frag->v;
136 		hdr->etype = etype;
137 
138 		fraghdr = (struct isakmp_frag *)(hdr + 1);
139 		fraghdr->unknown0 = htons(0);
140 		fraghdr->len = htons(fraglen - sizeof(*hdr));
141 		fraghdr->unknown1 = htons(1);
142 		fraghdr->index = fragnum;
143 		if (len == datalen)
144 			fraghdr->flags = ISAKMP_FRAG_LAST;
145 		else
146 			fraghdr->flags = 0;
147 
148 		data = (caddr_t)(fraghdr + 1);
149 		memcpy(data, sdata, datalen);
150 
151 		if (isakmp_send(iph1, frag) < 0) {
152 			plog(LLV_ERROR, LOCATION, NULL, "isakmp_send failed\n");
153 			return -1;
154 		}
155 
156 		vfree(frag);
157 
158 		len -= datalen;
159 		sdata += datalen;
160 	}
161 
162 	return fragnum;
163 }
164 
165 unsigned int
vendorid_frag_cap(gen)166 vendorid_frag_cap(gen)
167 	struct isakmp_gen *gen;
168 {
169 	int *hp;
170 
171 	hp = (int *)(gen + 1);
172 
173 	return ntohl(hp[MD5_DIGEST_LENGTH / sizeof(*hp)]);
174 }
175 
176 static int
isakmp_frag_insert(struct ph1handle * iph1,struct isakmp_frag_item * item)177 isakmp_frag_insert(struct ph1handle *iph1, struct isakmp_frag_item *item)
178 {
179 	struct isakmp_frag_item *pitem = NULL;
180 	struct isakmp_frag_item *citem = iph1->frag_chain;
181 
182 	/* no frag yet, just insert at beginning of list */
183 	if (iph1->frag_chain == NULL) {
184 		iph1->frag_chain = item;
185 		return 0;
186 	}
187 
188 	do {
189 		/* duplicate fragment number, abort (CVE-2016-10396) */
190 		if (citem->frag_num == item->frag_num)
191 			return -1;
192 
193 		/* need to insert before current item */
194 		if (citem->frag_num > item->frag_num) {
195 			if (pitem != NULL)
196 				pitem->frag_next = item;
197 			else
198 				/* insert at the beginning of the list  */
199 				iph1->frag_chain = item;
200 			item->frag_next = citem;
201 			return 0;
202 		}
203 
204 		pitem = citem;
205 		citem = citem->frag_next;
206 	} while (citem != NULL);
207 
208 	/* we reached the end of the list, insert */
209 	pitem->frag_next = item;
210 	return 0;
211 }
212 
213 int
isakmp_frag_extract(iph1,msg)214 isakmp_frag_extract(iph1, msg)
215 	struct ph1handle *iph1;
216 	vchar_t *msg;
217 {
218 	struct isakmp *isakmp;
219 	struct isakmp_frag *frag;
220 	struct isakmp_frag_item *item;
221 	vchar_t *buf;
222 	const char *m;
223 	char *data;
224 	int i;
225 
226  	if (iph1->frag_chain == NULL) {
227 		plog(LLV_DEBUG, LOCATION, NULL,
228 		     "fragmented IKE phase 1 message payload detected\n");
229 	}
230 
231 	if (msg->l < sizeof(*isakmp) + sizeof(*frag)) {
232 		plog(LLV_ERROR, LOCATION, NULL, "Message too short\n");
233 		return -1;
234 	}
235 
236 	isakmp = (struct isakmp *)msg->v;
237 	frag = (struct isakmp_frag *)(isakmp + 1);
238 
239 	/*
240 	 * frag->len is the frag payload data plus the frag payload header,
241 	 * whose size is sizeof(*frag)
242 	 */
243 	if (msg->l < sizeof(*isakmp) + ntohs(frag->len) ||
244 	    ntohs(frag->len) < sizeof(*frag) + 1) {
245 		plog(LLV_ERROR, LOCATION, NULL, "Fragment too short\n");
246 		return -1;
247 	}
248 
249 	if ((buf = vmalloc(ntohs(frag->len) - sizeof(*frag))) == NULL) {
250 		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
251 		return -1;
252 	}
253 
254 	if ((item = racoon_malloc(sizeof(*item))) == NULL) {
255 		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
256 		vfree(buf);
257 		return -1;
258 	}
259 
260 	data = (char *)(frag + 1);
261 	memcpy(buf->v, data, buf->l);
262 
263 	item->frag_num = frag->index;
264 	item->frag_last = (frag->flags & ISAKMP_FRAG_LAST);
265 	item->frag_next = NULL;
266 	item->frag_packet = buf;
267 
268 
269 	/* Perform required last frag checks before inserting the new item in
270 	   the chain */
271 	if (iph1->frag_last_index != 0) {
272 		/* Only one fragment payload allowed with last frag flag set */
273 		if (item->frag_last) {
274 			m = "Message has multiple tail fragments\n";
275 			goto out;
276 		}
277 
278 		/* Fragment payload with fragment number greater than the
279 		   fragment number of the last fragment is not allowed*/
280 		if (item->frag_num > iph1->frag_last_index) {
281 			m = "Fragment number greater than tail fragment number\n";
282 			goto out;
283 		}
284 	}
285 
286 	/* insert fragment into chain */
287 	if (isakmp_frag_insert(iph1, item) == -1) {
288 		m = "Duplicate fragment number\n";
289 		goto out;
290 	}
291 
292 	plog(LLV_DEBUG, LOCATION, NULL,
293 	     "fragment payload #%d queued\n", item->frag_num);
294 
295 	/* remember last frag after insertion into fragment chain */
296 	if (item->frag_last)
297 		iph1->frag_last_index = item->frag_num;
298 
299 	/* If we saw the last frag, check if the chain is complete
300 	 * we have a sorted list now, so just walk through */
301  	if (iph1->frag_last_index != 0) {
302 		item = iph1->frag_chain;
303 		for (i = 1; i <= iph1->frag_last_index; i++) {
304 			if (item == NULL ||
305 			    item->frag_num != i) {
306 				plog(LLV_DEBUG, LOCATION, NULL,
307 				     "fragment payload #%d still missing\n",
308 				     i);
309  				break;
310 			}
311 			item = item->frag_next;
312 		}
313 
314 		if (i > iph1->frag_last_index) {/* It is complete */
315 			plog(LLV_DEBUG, LOCATION, NULL,
316 			     "fragment #%d completed IKE phase 1 message payload.\n",
317 			     frag->index);
318  			return 1;
319 		}
320 	}
321 
322 	return 0;
323 out:
324 	plog(LLV_ERROR, LOCATION, NULL, m);
325 	racoon_free(item);
326 	vfree(buf);
327 	return -1;
328 }
329 
330 vchar_t *
isakmp_frag_reassembly(iph1)331 isakmp_frag_reassembly(iph1)
332 	struct ph1handle *iph1;
333 {
334 	struct isakmp_frag_item *item;
335 	size_t len = 0;
336 	vchar_t *buf = NULL;
337 	int frag_count = 0;
338 	int i;
339 	char *data;
340 
341 	if ((item = iph1->frag_chain) == NULL) {
342 		plog(LLV_ERROR, LOCATION, NULL, "No fragment to reassemble\n");
343 		goto out;
344 	}
345 
346 	do {
347 		frag_count++;
348 		len += item->frag_packet->l;
349 		item = item->frag_next;
350 	} while (item != NULL);
351 
352 	if ((buf = vmalloc(len)) == NULL) {
353 		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate memory\n");
354 		goto out;
355 	}
356 	data = buf->v;
357 
358 	item = iph1->frag_chain;
359 	for (i = 1; i <= frag_count; i++) {
360 		if (item->frag_num != i) {
361 			plog(LLV_ERROR, LOCATION, NULL,
362 			    "Missing fragment #%d\n", i);
363 			vfree(buf);
364 			buf = NULL;
365 			goto out;
366 		}
367 		memcpy(data, item->frag_packet->v, item->frag_packet->l);
368 		data += item->frag_packet->l;
369 		item = item->frag_next;
370 	}
371 
372 out:
373 	item = iph1->frag_chain;
374 	do {
375 		struct isakmp_frag_item *next_item;
376 
377 		next_item = item->frag_next;
378 
379 		vfree(item->frag_packet);
380 		racoon_free(item);
381 
382 		item = next_item;
383 	} while (item != NULL);
384 
385 	iph1->frag_chain = NULL;
386 	iph1->frag_last_index = 0;
387 
388 	return buf;
389 }
390 
391 vchar_t *
isakmp_frag_addcap(buf,cap)392 isakmp_frag_addcap(buf, cap)
393 	vchar_t *buf;
394 	int cap;
395 {
396 	int *capp;
397 	size_t len;
398 
399 	/* If the capability has not been added, add room now */
400 	len = buf->l;
401 	if (len == MD5_DIGEST_LENGTH) {
402 		if ((buf = vrealloc(buf, len + sizeof(cap))) == NULL) {
403 			plog(LLV_ERROR, LOCATION, NULL,
404 			    "Cannot allocate memory\n");
405 			return NULL;
406 		}
407 		capp = (int *)(buf->v + len);
408 		*capp = htonl(0);
409 	}
410 
411 	capp = (int *)(buf->v + MD5_DIGEST_LENGTH);
412 	*capp |= htonl(cap);
413 
414 	return buf;
415 }
416 
417