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