render / rpms / edk2

Forked from rpms/edk2 3 months ago
Clone

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

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