From 4601c685e7fd19c3724d5e499c69b8d3ec49933e Mon Sep 17 00:00:00 2001
From: Guy Harris <guy@alum.mit.edu>
Date: Tue, 21 Mar 2017 21:49:45 -0700
Subject: [PATCH] CVE-2017-13019: Clean up PGM option processing.

Add #defines for option lengths or the lengths of the fixed-length part
of the option.  Sometimes those #defines differ from what was there
before; what was there before was wrong, probably because the option
lengths given in RFC 3208 were sometimes wrong - some lengths included
the length of the option header, some lengths didn't.

Don't use "sizeof(uintXX_t)" for sizes in the packet, just use the
number of bytes directly.

For the options that include an IPv4 or IPv6 address, check the option
length against the length of what precedes the address before fetching
any of that data.

This fixes a buffer over-read discovered by Bhargava Shastry,
SecT/TU Berlin.

Add a test using the capture file supplied by the reporter(s), modified
so the capture file won't be rejected as an invalid capture.
---
 print-pgm.c                | 210 +++++++++++++++++++++++--------------
 tests/TESTLIST             |   1 +
 tests/pgm_opts_asan_2.out  |   2 +
 tests/pgm_opts_asan_2.pcap | Bin 0 -> 135 bytes
 4 files changed, 133 insertions(+), 80 deletions(-)
 create mode 100644 tests/pgm_opts_asan_2.out
 create mode 100644 tests/pgm_opts_asan_2.pcap

Index: tcpdump-3.9.8/print-pgm.c
===================================================================
--- tcpdump-3.9.8.orig/print-pgm.c
+++ tcpdump-3.9.8/print-pgm.c
@@ -515,94 +515,110 @@ pgm_print(register const u_char *bp, reg
 
 		switch (opt_type & PGM_OPT_MASK) {
 		case PGM_OPT_LENGTH:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len);
+#define PGM_OPT_LENGTH_LEN	(2+2)
+		    if (opt_len != PGM_OPT_LENGTH_LEN) {
+			(void)printf("[Bad OPT_LENGTH option, length %u != %u]",
+				     opt_len, PGM_OPT_LENGTH_LEN);
 			return;
 		    }
 		    (void)printf(" OPTS LEN (extra?) %d", EXTRACT_16BITS(bp));
-		    bp += sizeof(u_int16_t);
-		    opts_len -= 4;
+		    bp += 2;
+		    opts_len -= PGM_OPT_LENGTH_LEN;
 		    break;
 
 		case PGM_OPT_FRAGMENT:
-		    if (opt_len != 16) {
-			(void)printf("[Bad OPT_FRAGMENT option, length %u != 16]", opt_len);
+#define PGM_OPT_FRAGMENT_LEN	(2+2+4+4+4)
+		    if (opt_len != PGM_OPT_FRAGMENT_LEN) {
+			(void)printf("[Bad OPT_FRAGMENT option, length %u != %u]",
+				     opt_len, PGM_OPT_FRAGMENT_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    seq = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    offset = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    len = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" FRAG seq %u off %u len %u", seq, offset, len);
-		    opts_len -= 16;
+		    opts_len -= PGM_OPT_FRAGMENT_LEN;
 		    break;
 
 		case PGM_OPT_NAK_LIST:
 		    flags1 = *bp++;
 		    flags2 = *bp++;
-		    opt_len -= sizeof(u_int32_t);	/* option header */
+		    opt_len -= 4;	/* option header */
 		    (void)printf(" NAK LIST");
 		    while (opt_len) {
-			if (opt_len < sizeof(u_int32_t)) {
+			if (opt_len < 4) {
 			    (void)printf("[Option length not a multiple of 4]");
 			    return;
 			}
-			TCHECK2(*bp, sizeof(u_int32_t));
+			TCHECK2(*bp, 4);
 			(void)printf(" %u", EXTRACT_32BITS(bp));
-			bp += sizeof(u_int32_t);
-			opt_len -= sizeof(u_int32_t);
-			opts_len -= sizeof(u_int32_t);
+			bp += 4;
+			opt_len -= 4;
+			opts_len -= 4;
 		    }
 		    break;
 
 		case PGM_OPT_JOIN:
-		    if (opt_len != 8) {
-			(void)printf("[Bad OPT_JOIN option, length %u != 8]", opt_len);
+#define PGM_OPT_JOIN_LEN	(2+2+4)
+		    if (opt_len != PGM_OPT_JOIN_LEN) {
+			(void)printf("[Bad OPT_JOIN option, length %u != %u]",
+				     opt_len, PGM_OPT_JOIN_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    seq = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" JOIN %u", seq);
-		    opts_len -= 8;
+		    opts_len -= PGM_OPT_JOIN_LEN;
 		    break;
 
 		case PGM_OPT_NAK_BO_IVL:
-		    if (opt_len != 12) {
-			(void)printf("[Bad OPT_NAK_BO_IVL option, length %u != 12]", opt_len);
+#define PGM_OPT_NAK_BO_IVL_LEN	(2+2+4+4)
+		    if (opt_len != PGM_OPT_NAK_BO_IVL_LEN) {
+			(void)printf("[Bad OPT_NAK_BO_IVL option, length %u != %u]",
+				     opt_len, PGM_OPT_NAK_BO_IVL_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    offset = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    seq = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" BACKOFF ivl %u ivlseq %u", offset, seq);
-		    opts_len -= 12;
+		    opts_len -= PGM_OPT_NAK_BO_IVL_LEN;
 		    break;
 
 		case PGM_OPT_NAK_BO_RNG:
-		    if (opt_len != 12) {
-			(void)printf("[Bad OPT_NAK_BO_RNG option, length %u != 12]", opt_len);
+#define PGM_OPT_NAK_BO_RNG_LEN	(2+2+4+4)
+		    if (opt_len != PGM_OPT_NAK_BO_RNG_LEN) {
+			(void)printf("[Bad OPT_NAK_BO_RNG option, length %u != %u]",
+				     opt_len, PGM_OPT_NAK_BO_RNG_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    offset = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    seq = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" BACKOFF max %u min %u", offset, seq);
-		    opts_len -= 12;
+		    opts_len -= PGM_OPT_NAK_BO_RNG_LEN;
 		    break;
 
 		case PGM_OPT_REDIRECT:
+#define PGM_OPT_REDIRECT_FIXED_LEN	(2+2+2+2)
+		    if (opt_len < PGM_OPT_REDIRECT_FIXED_LEN) {
+			printf("[Bad OPT_REDIRECT option, length %u < %u]",
+			       opt_len, PGM_OPT_REDIRECT_FIXED_LEN);
+			return;
+		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    switch (EXTRACT_16BITS(bp)) {
@@ -620,9 +636,10 @@ pgm_print(register const u_char *bp, reg
 			goto trunc;
 			break;
 		    }
-		    bp += (2 * sizeof(u_int16_t));
+		    bp += 2+2;
 		    if (opt_len != 4 + addr_size) {
-			(void)printf("[Bad OPT_REDIRECT option, length %u != 4 + address size]", opt_len);
+			(void)printf("[Bad OPT_REDIRECT option, length %u != %u + address size]",
+				     opt_len, PGM_OPT_REDIRECT_FIXED_LEN);
 			return;
 		    }
 		    TCHECK2(*bp, addr_size);
@@ -631,57 +648,65 @@ pgm_print(register const u_char *bp, reg
 
 		    inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
 		    (void)printf(" REDIRECT %s",  (char *)nla);
-		    opts_len -= 4 + addr_size;
+		    opts_len -= PGM_OPT_REDIRECT_FIXED_LEN + addr_size;
 		    break;
 
 		case PGM_OPT_PARITY_PRM:
-		    if (opt_len != 8) {
-			(void)printf("[Bad OPT_PARITY_PRM option, length %u != 8]", opt_len);
+#define PGM_OPT_PARITY_PRM_LEN	(2+2+4)
+		    if (opt_len != PGM_OPT_PARITY_PRM_LEN) {
+			(void)printf("[Bad OPT_PARITY_PRM option, length %u != %u]",
+				     opt_len, PGM_OPT_PARITY_PRM_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    len = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" PARITY MAXTGS %u", len);
-		    opts_len -= 8;
+		    opts_len -= PGM_OPT_PARITY_PRM_LEN;
 		    break;
 
 		case PGM_OPT_PARITY_GRP:
-		    if (opt_len != 8) {
-			(void)printf("[Bad OPT_PARITY_GRP option, length %u != 8]", opt_len);
+#define PGM_OPT_PARITY_GRP_LEN	(2+2+4)
+		    if (opt_len != PGM_OPT_PARITY_GRP_LEN) {
+			(void)printf("[Bad OPT_PARITY_GRP option, length %u != %u]",
+				     opt_len, PGM_OPT_PARITY_GRP_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    seq = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" PARITY GROUP %u", seq);
-		    opts_len -= 8;
+		    opts_len -= PGM_OPT_PARITY_GRP_LEN;
 		    break;
 
 		case PGM_OPT_CURR_TGSIZE:
-		    if (opt_len != 8) {
-			(void)printf("[Bad OPT_CURR_TGSIZE option, length %u != 8]", opt_len);
+#define PGM_OPT_CURR_TGSIZE_LEN	(2+2+4)
+		    if (opt_len != PGM_OPT_CURR_TGSIZE_LEN) {
+			(void)printf("[Bad OPT_CURR_TGSIZE option, length %u != %u]",
+				     opt_len, PGM_OPT_CURR_TGSIZE_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    len = EXTRACT_32BITS(bp);
-		    bp += sizeof(u_int32_t);
+		    bp += 4;
 		    (void)printf(" PARITY ATGS %u", len);
-		    opts_len -= 8;
+		    opts_len -= PGM_OPT_CURR_TGSIZE_LEN;
 		    break;
 
 		case PGM_OPT_NBR_UNREACH:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_NBR_UNREACH option, length %u != 4]", opt_len);
+#define PGM_OPT_NBR_UNREACH_LEN	(2+2)
+		    if (opt_len != PGM_OPT_NBR_UNREACH_LEN) {
+			(void)printf("[Bad OPT_NBR_UNREACH option, length %u != %u]",
+				     opt_len, PGM_OPT_NBR_UNREACH_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    (void)printf(" NBR_UNREACH");
-		    opts_len -= 4;
+		    opts_len -= PGM_OPT_NBR_UNREACH_LEN;
 		    break;
 
 		case PGM_OPT_PATH_NLA:
@@ -691,36 +716,42 @@ pgm_print(register const u_char *bp, reg
 		    break;
 
 		case PGM_OPT_SYN:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_SYN option, length %u != 4]", opt_len);
+#define PGM_OPT_SYN_LEN	(2+2)
+		    if (opt_len != PGM_OPT_SYN_LEN) {
+			(void)printf("[Bad OPT_SYN option, length %u != %u]",
+				     opt_len, PGM_OPT_SYN_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    (void)printf(" SYN");
-		    opts_len -= 4;
+		    opts_len -= PGM_OPT_SYN_LEN;
 		    break;
 
 		case PGM_OPT_FIN:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_FIN option, length %u != 4]", opt_len);
+#define PGM_OPT_FIN_LEN	(2+2)
+		    if (opt_len != PGM_OPT_FIN_LEN) {
+			    (void)printf("[Bad OPT_FIN option, length %u != %u]",
+					 opt_len, PGM_OPT_FIN_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    (void)printf(" FIN");
-		    opts_len -= 4;
+		    opts_len -= PGM_OPT_FIN_LEN;
 		    break;
 
 		case PGM_OPT_RST:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_RST option, length %u != 4]", opt_len);
+#define PGM_OPT_RST_LEN	(2+2)
+		    if (opt_len != PGM_OPT_RST_LEN) {
+			(void)printf("[Bad OPT_RST option, length %u != %u]",
+				     opt_len, PGM_OPT_RST_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    (void)printf(" RST");
-		    opts_len -= 4;
+		    opts_len -= PGM_OPT_RST_LEN;
 		    break;
 
 		case PGM_OPT_CR:
@@ -730,14 +761,16 @@ pgm_print(register const u_char *bp, reg
 		    break;
 
 		case PGM_OPT_CRQST:
-		    if (opt_len != 4) {
-			(void)printf("[Bad OPT_CRQST option, length %u != 4]", opt_len);
+#define PGM_OPT_CRQST_LEN	(2+2)
+		    if (opt_len != PGM_OPT_CRQST_LEN) {
+			(void)printf("[Bad OPT_CRQST option, length %u != %u]",
+				     opt_len, PGM_OPT_CRQST_LEN);
 			return;
 		    }
 		    flags1 = *bp++;
 		    flags2 = *bp++;
 		    (void)printf(" CRQST");
-		    opts_len -= 4;
+		    opts_len -= PGM_OPT_CRQST_LEN;
 		    break;
 
 		default:
