190885
From 3cef261bb97c00473cb41ae4d7401a46e3d96ce7 Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Mon, 4 Feb 2019 08:55:52 -0800
190885
Subject: [PATCH] x86-64 memcmp: Use unsigned Jcc instructions on size [BZ
190885
 #24155]
190885
190885
Since the size argument is unsigned. we should use unsigned Jcc
190885
instructions, instead of signed, to check size.
190885
190885
Tested on x86-64 and x32, with and without --disable-multi-arch.
190885
190885
	[BZ #24155]
190885
	CVE-2019-7309
190885
	* NEWS: Updated for CVE-2019-7309.
190885
	* sysdeps/x86_64/memcmp.S: Use RDX_LP for size.  Clear the
190885
	upper 32 bits of RDX register for x32.  Use unsigned Jcc
190885
	instructions, instead of signed.
190885
	* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-memcmp-2.
190885
	* sysdeps/x86_64/x32/tst-size_t-memcmp-2.c: New test.
190885
190885
(cherry picked from commit 3f635fb43389b54f682fc9ed2acc0b2aaf4a923d)
190885
---
190885
 sysdeps/x86_64/memcmp.S                  | 20 +++---
190885
 sysdeps/x86_64/x32/Makefile              |  3 +-
190885
 sysdeps/x86_64/x32/tst-size_t-memcmp-2.c | 79 ++++++++++++++++++++++++
190885
 3 files changed, 93 insertions(+), 9 deletions(-)
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-memcmp-2.c
190885
190885
diff --git a/sysdeps/x86_64/memcmp.S b/sysdeps/x86_64/memcmp.S
190885
index bcb4a2e8..45918d37 100644
190885
--- a/sysdeps/x86_64/memcmp.S
190885
+++ b/sysdeps/x86_64/memcmp.S
190885
@@ -21,14 +21,18 @@
190885
 
190885
 	.text
190885
 ENTRY (memcmp)
190885
-	test	%rdx, %rdx
190885
+#ifdef __ILP32__
190885
+	/* Clear the upper 32 bits.  */
190885
+	movl	%edx, %edx
190885
+#endif
190885
+	test	%RDX_LP, %RDX_LP
190885
 	jz	L(finz)
190885
 	cmpq	$1, %rdx
190885
-	jle	L(finr1b)
190885
+	jbe	L(finr1b)
190885
 	subq	%rdi, %rsi
190885
 	movq	%rdx, %r10
190885
 	cmpq	$32, %r10
190885
-	jge	L(gt32)
190885
+	jae	L(gt32)
190885
 	/* Handle small chunks and last block of less than 32 bytes.  */
190885
 L(small):
190885
 	testq	$1, %r10
190885
@@ -156,7 +160,7 @@ L(A32):
190885
 	movq	%r11, %r10
190885
 	andq	$-32, %r10
190885
 	cmpq	%r10, %rdi
190885
-        jge	L(mt16)
190885
+        jae	L(mt16)
190885
 	/* Pre-unroll to be ready for unrolled 64B loop.  */
190885
 	testq	$32, %rdi
190885
 	jz	L(A64)
190885
@@ -178,7 +182,7 @@ L(A64):
190885
 	movq	%r11, %r10
190885
 	andq	$-64, %r10
190885
 	cmpq	%r10, %rdi
190885
-        jge	L(mt32)
190885
+        jae	L(mt32)
190885
 
190885
 L(A64main):
190885
 	movdqu    (%rdi,%rsi), %xmm0
190885
@@ -216,7 +220,7 @@ L(mt32):
190885
 	movq	%r11, %r10
190885
 	andq	$-32, %r10
190885
 	cmpq	%r10, %rdi
190885
-        jge	L(mt16)
190885
+        jae	L(mt16)
190885
 
190885
 L(A32main):
190885
 	movdqu    (%rdi,%rsi), %xmm0
190885
@@ -254,7 +258,7 @@ L(ATR):
190885
 	movq	%r11, %r10
190885
 	andq	$-32, %r10
190885
 	cmpq	%r10, %rdi
190885
-        jge	L(mt16)
190885
+        jae	L(mt16)
190885
 	testq	$16, %rdi
190885
 	jz	L(ATR32)
190885
 
190885
@@ -325,7 +329,7 @@ L(ATR64main):
190885
 	movq	%r11, %r10
190885
 	andq	$-32, %r10
190885
 	cmpq	%r10, %rdi
190885
-        jge	L(mt16)
190885
+        jae	L(mt16)
190885
 
190885
 L(ATR32res):
190885
 	movdqa    (%rdi,%rsi), %xmm0
190885
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
190885
index 1557724b..87489565 100644
190885
--- a/sysdeps/x86_64/x32/Makefile
190885
+++ b/sysdeps/x86_64/x32/Makefile
190885
@@ -8,7 +8,8 @@ endif
190885
 ifeq ($(subdir),string)
190885
 tests += tst-size_t-memchr tst-size_t-memcmp tst-size_t-memcpy \
190885
 	 tst-size_t-memrchr tst-size_t-memset tst-size_t-strncasecmp \
190885
-	 tst-size_t-strncmp tst-size_t-strncpy tst-size_t-strnlen
190885
+	 tst-size_t-strncmp tst-size_t-strncpy tst-size_t-strnlen \
190885
+	 tst-size_t-memcmp-2
190885
 endif
190885
 
190885
 ifeq ($(subdir),wcsmbs)
190885
diff --git a/sysdeps/x86_64/x32/tst-size_t-memcmp-2.c b/sysdeps/x86_64/x32/tst-size_t-memcmp-2.c
190885
new file mode 100644
190885
index 00000000..d8ae1a08
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-memcmp-2.c
190885
@@ -0,0 +1,79 @@
190885
+/* Test memcmp with size_t in the lower 32 bits of 64-bit register.
190885
+   Copyright (C) 2019 Free Software Foundation, Inc.
190885
+   This file is part of the GNU C Library.
190885
+
190885
+   The GNU C Library is free software; you can redistribute it and/or
190885
+   modify it under the terms of the GNU Lesser General Public
190885
+   License as published by the Free Software Foundation; either
190885
+   version 2.1 of the License, or (at your option) any later version.
190885
+
190885
+   The GNU C Library is distributed in the hope that it will be useful,
190885
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
190885
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
190885
+   Lesser General Public License for more details.
190885
+
190885
+   You should have received a copy of the GNU Lesser General Public
190885
+   License along with the GNU C Library; if not, see
190885
+   <http://www.gnu.org/licenses/>.  */
190885
+
190885
+#define TEST_MAIN
190885
+#ifdef WIDE
190885
+# define TEST_NAME "wmemcmp"
190885
+#else
190885
+# define TEST_NAME "memcmp"
190885
+#endif
190885
+
190885
+#include "test-size_t.h"
190885
+
190885
+#ifdef WIDE
190885
+# include <inttypes.h>
190885
+# include <wchar.h>
190885
+
190885
+# define MEMCMP wmemcmp
190885
+# define CHAR wchar_t
190885
+#else
190885
+# define MEMCMP memcmp
190885
+# define CHAR char
190885
+#endif
190885
+
190885
+IMPL (MEMCMP, 1)
190885
+
190885
+typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
190885
+
190885
+static int
190885
+__attribute__ ((noinline, noclone))
190885
+do_memcmp (parameter_t a, parameter_t b)
190885
+{
190885
+  return CALL (&b, a.p, b.p, a.len);
190885
+}
190885
+
190885
+static int
190885
+test_main (void)
190885
+{
190885
+  test_init ();
190885
+
190885
+  parameter_t dest = { { page_size / sizeof (CHAR) }, buf1 };
190885
+  parameter_t src = { { 0 }, buf2 };
190885
+
190885
+  memcpy (buf1, buf2, page_size);
190885
+
190885
+  CHAR *p = (CHAR *) buf1;
190885
+  p[page_size / sizeof (CHAR) - 1] = (CHAR) 1;
190885
+
190885
+  int ret = 0;
190885
+  FOR_EACH_IMPL (impl, 0)
190885
+    {
190885
+      src.fn = impl->fn;
190885
+      int res = do_memcmp (dest, src);
190885
+      if (res >= 0)
190885
+	{
190885
+	  error (0, 0, "Wrong result in function %s: %i >= 0",
190885
+		 impl->name, res);
190885
+	  ret = 1;
190885
+	}
190885
+    }
190885
+
190885
+  return ret ? EXIT_FAILURE : EXIT_SUCCESS;
190885
+}
190885
+
190885
+#include <support/test-driver.c>
190885
-- 
190885
GitLab
190885