4318d63
From 32027e199368dad9508965aae8cd8de5b6ab5231 Mon Sep 17 00:00:00 2001
4318d63
From: Guy Harris <guy@alum.mit.edu>
4318d63
Date: Sat, 18 Apr 2020 14:04:59 -0700
4318d63
Subject: [PATCH] PPP: When un-escaping, don't allocate a too-large buffer.
4318d63
4318d63
The buffer should be big enough to hold the captured data, but it
4318d63
doesn't need to be big enough to hold the entire on-the-network packet,
4318d63
if we haven't captured all of it.
4318d63
4318d63
(backported from commit e4add0b010ed6f2180dcb05a13026242ed935334)
4318d63
---
4318d63
 print-ppp.c | 18 ++++++++++++++----
4318d63
 1 file changed, 14 insertions(+), 4 deletions(-)
4318d63
4318d63
diff --git a/print-ppp.c b/print-ppp.c
4318d63
index 891761728..33fb03412 100644
4318d63
--- a/print-ppp.c
4318d63
+++ b/print-ppp.c
4318d63
@@ -1367,19 +1367,29 @@ print_bacp_config_options(netdissect_options *ndo,
4318d63
 	return 0;
4318d63
 }
4318d63
 
4318d63
+/*
4318d63
+ * Un-escape RFC 1662 PPP in HDLC-like framing, with octet escapes.
4318d63
+ * The length argument is the on-the-wire length, not the captured
4318d63
+ * length; we can only un-escape the captured part.
4318d63
+ */
4318d63
 static void
4318d63
 ppp_hdlc(netdissect_options *ndo,
4318d63
          const u_char *p, int length)
4318d63
 {
4318d63
+	u_int caplen = ndo->ndo_snapend - p;
4318d63
 	u_char *b, *t, c;
4318d63
 	const u_char *s;
4318d63
-	int i, proto;
4318d63
+	u_int i;
4318d63
+	int proto;
4318d63
 	const void *se;
4318d63
 
4318d63
+	if (caplen == 0)
4318d63
+		return;
4318d63
+
4318d63
         if (length <= 0)
4318d63
                 return;
4318d63
 
4318d63
-	b = (u_char *)malloc(length);
4318d63
+	b = (u_char *)malloc(caplen);
4318d63
 	if (b == NULL)
4318d63
 		return;
4318d63
 
4318d63
@@ -1388,10 +1398,10 @@ ppp_hdlc(netdissect_options *ndo,
4318d63
 	 * Do this so that we dont overwrite the original packet
4318d63
 	 * contents.
4318d63
 	 */
4318d63
-	for (s = p, t = b, i = length; i > 0 && ND_TTEST(*s); i--) {
4318d63
+	for (s = p, t = b, i = caplen; i != 0; i--) {
4318d63
 		c = *s++;
4318d63
 		if (c == 0x7d) {
4318d63
-			if (i <= 1 || !ND_TTEST(*s))
4318d63
+			if (i <= 1)
4318d63
 				break;
4318d63
 			i--;
4318d63
 			c = *s++ ^ 0x20;