Blame SOURCES/edk2-CryptoPkg-Crt-import-inet_pton.c-CVE-2019-14553.patch

63d87e
From 3c9574af677c24b969c3baa6a527dabaf97f11a2 Mon Sep 17 00:00:00 2001
63d87e
From: Laszlo Ersek <lersek@redhat.com>
63d87e
Date: Mon, 2 Dec 2019 12:31:53 +0100
63d87e
Subject: [PATCH 5/9] CryptoPkg/Crt: import "inet_pton.c" (CVE-2019-14553)
63d87e
MIME-Version: 1.0
63d87e
Content-Type: text/plain; charset=UTF-8
63d87e
Content-Transfer-Encoding: 8bit
63d87e
63d87e
RH-Author: Laszlo Ersek <lersek@redhat.com>
63d87e
Message-id: <20191117220052.15700-6-lersek@redhat.com>
63d87e
Patchwork-id: 92461
63d87e
O-Subject: [RHEL-8.2.0 edk2 PATCH 5/9] CryptoPkg/Crt: import "inet_pton.c" (CVE-2019-14553)
63d87e
Bugzilla: 1536624
63d87e
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
63d87e
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
63d87e
63d87e
For TianoCore BZ#1734, StdLib has been moved from the edk2 project to the
63d87e
edk2-libc project, in commit 964f432b9b0a ("edk2: Remove AppPkg, StdLib,
63d87e
StdLibPrivateInternalFiles", 2019-04-29).
63d87e
63d87e
We'd like to use the inet_pton() function in CryptoPkg. Resurrect the
63d87e
"inet_pton.c" file from just before the StdLib removal, as follows:
63d87e
63d87e
  $ git show \
63d87e
      964f432b9b0a^:StdLib/BsdSocketLib/inet_pton.c \
63d87e
      > CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c
63d87e
63d87e
The inet_pton() function is only intended for the DXE phase at this time,
63d87e
therefore only the "BaseCryptLib" instance INF file receives the new file.
63d87e
63d87e
Cc: David Woodhouse <dwmw2@infradead.org>
63d87e
Cc: Jian J Wang <jian.j.wang@intel.com>
63d87e
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
63d87e
Cc: Sivaraman Nainar <sivaramann@amiindia.co.in>
63d87e
Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
63d87e
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=960
63d87e
CVE: CVE-2019-14553
63d87e
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
63d87e
Reviewed-by: Jian J Wang <jian.j.wang@intel.com>
63d87e
Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>
63d87e
(cherry picked from commit 8d16ef8269b2ff373d8da674e59992adfdc032d3)
63d87e
---
63d87e
 CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf    |   1 +
63d87e
 CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c | 257 +++++++++++++++++++++
63d87e
 CryptoPkg/Library/Include/CrtLibSupport.h          |   1 +
63d87e
 3 files changed, 259 insertions(+)
63d87e
 create mode 100644 CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c
63d87e
63d87e
diff --git a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
63d87e
index 8d4988e..b5cfd8b 100644
63d87e
--- a/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
63d87e
+++ b/CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
63d87e
@@ -58,6 +58,7 @@
63d87e
   SysCall/CrtWrapper.c
63d87e
   SysCall/TimerWrapper.c
63d87e
   SysCall/BaseMemAllocation.c
63d87e
+  SysCall/inet_pton.c
63d87e
 
63d87e
 [Sources.Ia32]
63d87e
   Rand/CryptRandTsc.c
63d87e
diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c b/CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c
63d87e
new file mode 100644
63d87e
index 0000000..32e1ab8
63d87e
--- /dev/null
63d87e
+++ b/CryptoPkg/Library/BaseCryptLib/SysCall/inet_pton.c
63d87e
@@ -0,0 +1,257 @@
63d87e
+/* Copyright (c) 1996 by Internet Software Consortium.
63d87e
+ *
63d87e
+ * Permission to use, copy, modify, and distribute this software for any
63d87e
+ * purpose with or without fee is hereby granted, provided that the above
63d87e
+ * copyright notice and this permission notice appear in all copies.
63d87e
+ *
63d87e
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
63d87e
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
63d87e
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
63d87e
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
63d87e
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
63d87e
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63d87e
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
63d87e
+ * SOFTWARE.
63d87e
+ */
63d87e
+
63d87e
+/*
63d87e
+ * Portions copyright (c) 1999, 2000
63d87e
+ * Intel Corporation.
63d87e
+ * All rights reserved.
63d87e
+ * 
63d87e
+ * Redistribution and use in source and binary forms, with or without
63d87e
+ * modification, are permitted provided that the following conditions
63d87e
+ * are met:
63d87e
+ * 
63d87e
+ * 1. Redistributions of source code must retain the above copyright
63d87e
+ *    notice, this list of conditions and the following disclaimer.
63d87e
+ * 
63d87e
+ * 2. Redistributions in binary form must reproduce the above copyright
63d87e
+ *    notice, this list of conditions and the following disclaimer in the
63d87e
+ *    documentation and/or other materials provided with the distribution.
63d87e
+ * 
63d87e
+ * 3. All advertising materials mentioning features or use of this software
63d87e
+ *    must display the following acknowledgement:
63d87e
+ * 
63d87e
+ *    This product includes software developed by Intel Corporation and
63d87e
+ *    its contributors.
63d87e
+ * 
63d87e
+ * 4. Neither the name of Intel Corporation or its contributors may be
63d87e
+ *    used to endorse or promote products derived from this software
63d87e
+ *    without specific prior written permission.
63d87e
+ * 
63d87e
+ * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
63d87e
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63d87e
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63d87e
+ * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
63d87e
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63d87e
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63d87e
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63d87e
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63d87e
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63d87e
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
63d87e
+ * THE POSSIBILITY OF SUCH DAMAGE.
63d87e
+ * 
63d87e
+ */
63d87e
+
63d87e
+#if defined(LIBC_SCCS) && !defined(lint)
63d87e
+static char rcsid[] = "$Id: inet_pton.c,v 1.1.1.1 2003/11/19 01:51:30 kyu3 Exp $";
63d87e
+#endif /* LIBC_SCCS and not lint */
63d87e
+
63d87e
+#include <sys/param.h>
63d87e
+#include <sys/types.h>
63d87e
+#include <sys/socket.h>
63d87e
+#include <netinet/in.h>
63d87e
+#include <arpa/inet.h>
63d87e
+#include <arpa/nameser.h>
63d87e
+#include <string.h>
63d87e
+#include <errno.h>
63d87e
+
63d87e
+/*
63d87e
+ * WARNING: Don't even consider trying to compile this on a system where
63d87e
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
63d87e
+ */
63d87e
+
63d87e
+static int	inet_pton4 (const char *src, u_char *dst);
63d87e
+static int	inet_pton6 (const char *src, u_char *dst);
63d87e
+
63d87e
+/* int
63d87e
+ * inet_pton(af, src, dst)
63d87e
+ *	convert from presentation format (which usually means ASCII printable)
63d87e
+ *	to network format (which is usually some kind of binary format).
63d87e
+ * return:
63d87e
+ *	1 if the address was valid for the specified address family
63d87e
+ *	0 if the address wasn't valid (`dst' is untouched in this case)
63d87e
+ *	-1 if some other error occurred (`dst' is untouched in this case, too)
63d87e
+ * author:
63d87e
+ *	Paul Vixie, 1996.
63d87e
+ */
63d87e
+int
63d87e
+inet_pton(
63d87e
+	int af,
63d87e
+	const char *src,
63d87e
+	void *dst
63d87e
+	)
63d87e
+{
63d87e
+	switch (af) {
63d87e
+	case AF_INET:
63d87e
+		return (inet_pton4(src, dst));
63d87e
+	case AF_INET6:
63d87e
+		return (inet_pton6(src, dst));
63d87e
+	default:
63d87e
+		errno = EAFNOSUPPORT;
63d87e
+		return (-1);
63d87e
+	}
63d87e
+	/* NOTREACHED */
63d87e
+}
63d87e
+
63d87e
+/* int
63d87e
+ * inet_pton4(src, dst)
63d87e
+ *	like inet_aton() but without all the hexadecimal and shorthand.
63d87e
+ * return:
63d87e
+ *	1 if `src' is a valid dotted quad, else 0.
63d87e
+ * notice:
63d87e
+ *	does not touch `dst' unless it's returning 1.
63d87e
+ * author:
63d87e
+ *	Paul Vixie, 1996.
63d87e
+ */
63d87e
+static int
63d87e
+inet_pton4(
63d87e
+	const char *src,
63d87e
+	u_char *dst
63d87e
+	)
63d87e
+{
63d87e
+	static const char digits[] = "0123456789";
63d87e
+	int saw_digit, octets, ch;
63d87e
+	u_char tmp[NS_INADDRSZ], *tp;
63d87e
+
63d87e
+	saw_digit = 0;
63d87e
+	octets = 0;
63d87e
+	*(tp = tmp) = 0;
63d87e
+	while ((ch = *src++) != '\0') {
63d87e
+		const char *pch;
63d87e
+
63d87e
+		if ((pch = strchr(digits, ch)) != NULL) {
63d87e
+			u_int new = *tp * 10 + (u_int)(pch - digits);
63d87e
+
63d87e
+			if (new > 255)
63d87e
+				return (0);
63d87e
+			*tp = (u_char)new;
63d87e
+			if (! saw_digit) {
63d87e
+				if (++octets > 4)
63d87e
+					return (0);
63d87e
+				saw_digit = 1;
63d87e
+			}
63d87e
+		} else if (ch == '.' && saw_digit) {
63d87e
+			if (octets == 4)
63d87e
+				return (0);
63d87e
+			*++tp = 0;
63d87e
+			saw_digit = 0;
63d87e
+		} else
63d87e
+			return (0);
63d87e
+	}
63d87e
+	if (octets < 4)
63d87e
+		return (0);
63d87e
+
63d87e
+	memcpy(dst, tmp, NS_INADDRSZ);
63d87e
+	return (1);
63d87e
+}
63d87e
+
63d87e
+/* int
63d87e
+ * inet_pton6(src, dst)
63d87e
+ *	convert presentation level address to network order binary form.
63d87e
+ * return:
63d87e
+ *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
63d87e
+ * notice:
63d87e
+ *	(1) does not touch `dst' unless it's returning 1.
63d87e
+ *	(2) :: in a full address is silently ignored.
63d87e
+ * credit:
63d87e
+ *	inspired by Mark Andrews.
63d87e
+ * author:
63d87e
+ *	Paul Vixie, 1996.
63d87e
+ */
63d87e
+static int
63d87e
+inet_pton6(
63d87e
+	const char *src,
63d87e
+	u_char *dst
63d87e
+	)
63d87e
+{
63d87e
+	static const char xdigits_l[] = "0123456789abcdef",
63d87e
+			  xdigits_u[] = "0123456789ABCDEF";
63d87e
+	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
63d87e
+	const char *xdigits, *curtok;
63d87e
+	int ch, saw_xdigit;
63d87e
+	u_int val;
63d87e
+
63d87e
+	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
63d87e
+	endp = tp + NS_IN6ADDRSZ;
63d87e
+	colonp = NULL;
63d87e
+	/* Leading :: requires some special handling. */
63d87e
+	if (*src == ':')
63d87e
+		if (*++src != ':')
63d87e
+			return (0);
63d87e
+	curtok = src;
63d87e
+	saw_xdigit = 0;
63d87e
+	val = 0;
63d87e
+	while ((ch = *src++) != '\0') {
63d87e
+		const char *pch;
63d87e
+
63d87e
+		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
63d87e
+			pch = strchr((xdigits = xdigits_u), ch);
63d87e
+		if (pch != NULL) {
63d87e
+			val <<= 4;
63d87e
+			val |= (pch - xdigits);
63d87e
+			if (val > 0xffff)
63d87e
+				return (0);
63d87e
+			saw_xdigit = 1;
63d87e
+			continue;
63d87e
+		}
63d87e
+		if (ch == ':') {
63d87e
+			curtok = src;
63d87e
+			if (!saw_xdigit) {
63d87e
+				if (colonp)
63d87e
+					return (0);
63d87e
+				colonp = tp;
63d87e
+				continue;
63d87e
+			}
63d87e
+			if (tp + NS_INT16SZ > endp)
63d87e
+				return (0);
63d87e
+			*tp++ = (u_char) (val >> 8) & 0xff;
63d87e
+			*tp++ = (u_char) val & 0xff;
63d87e
+			saw_xdigit = 0;
63d87e
+			val = 0;
63d87e
+			continue;
63d87e
+		}
63d87e
+		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
63d87e
+		    inet_pton4(curtok, tp) > 0) {
63d87e
+			tp += NS_INADDRSZ;
63d87e
+			saw_xdigit = 0;
63d87e
+			break;	/* '\0' was seen by inet_pton4(). */
63d87e
+		}
63d87e
+		return (0);
63d87e
+	}
63d87e
+	if (saw_xdigit) {
63d87e
+		if (tp + NS_INT16SZ > endp)
63d87e
+			return (0);
63d87e
+		*tp++ = (u_char) (val >> 8) & 0xff;
63d87e
+		*tp++ = (u_char) val & 0xff;
63d87e
+	}
63d87e
+	if (colonp != NULL) {
63d87e
+		/*
63d87e
+		 * Since some memmove()'s erroneously fail to handle
63d87e
+		 * overlapping regions, we'll do the shift by hand.
63d87e
+		 */
63d87e
+		const int n = (int)(tp - colonp);
63d87e
+		int i;
63d87e
+
63d87e
+		for (i = 1; i <= n; i++) {
63d87e
+			endp[- i] = colonp[n - i];
63d87e
+			colonp[n - i] = 0;
63d87e
+		}
63d87e
+		tp = endp;
63d87e
+	}
63d87e
+	if (tp != endp)
63d87e
+		return (0);
63d87e
+	memcpy(dst, tmp, NS_IN6ADDRSZ);
63d87e
+	return (1);
63d87e
+}
63d87e
diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h b/CryptoPkg/Library/Include/CrtLibSupport.h
63d87e
index e603fad..5a20ba6 100644
63d87e
--- a/CryptoPkg/Library/Include/CrtLibSupport.h
63d87e
+++ b/CryptoPkg/Library/Include/CrtLibSupport.h
63d87e
@@ -192,6 +192,7 @@ void           abort       (void) __attribute__((__noreturn__));
63d87e
 #else
63d87e
 void           abort       (void);
63d87e
 #endif
63d87e
+int            inet_pton   (int, const char *, void *);
63d87e
 
63d87e
 //
63d87e
 // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
63d87e
-- 
63d87e
1.8.3.1
63d87e