190885
From c62e1cf68a70de8ac4cc8aebae760784009777c5 Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Fri, 1 Feb 2019 12:20:54 -0800
190885
Subject: [PATCH] x86-64 memrchr: Properly handle the length parameter [BZ
190885
 #24097]
190885
190885
On x32, the size_t parameter may be passed in the lower 32 bits of a
190885
64-bit register with the non-zero upper 32 bits.  The string/memory
190885
functions written in assembly can only use the lower 32 bits of a
190885
64-bit register as length or must clear the upper 32 bits before using
190885
the full 64-bit register for length.
190885
190885
This pach fixes memrchr for x32.  Tested on x86-64 and x32.  On x86-64,
190885
libc.so is the same with and withou the fix.
190885
190885
	[BZ #24097]
190885
	CVE-2019-6488
190885
	* sysdeps/x86_64/memrchr.S: Use RDX_LP for length.
190885
	* sysdeps/x86_64/multiarch/memrchr-avx2.S: Likewise.
190885
	* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-memrchr.
190885
	* sysdeps/x86_64/x32/tst-size_t-memrchr.c: New file.
190885
190885
(cherry picked from commit ecd8b842cf37ea112e59cd9085ff1f1b6e208ae0)
190885
---
190885
 sysdeps/x86_64/memrchr.S                |  4 +-
190885
 sysdeps/x86_64/multiarch/memrchr-avx2.S |  4 +-
190885
 sysdeps/x86_64/x32/Makefile             |  3 +-
190885
 sysdeps/x86_64/x32/tst-size_t-memrchr.c | 57 +++++++++++++++++++++++++
190885
 4 files changed, 63 insertions(+), 5 deletions(-)
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-memrchr.c
190885
190885
diff --git a/sysdeps/x86_64/memrchr.S b/sysdeps/x86_64/memrchr.S
190885
index b8e3fa1d..dc82f8f7 100644
190885
--- a/sysdeps/x86_64/memrchr.S
190885
+++ b/sysdeps/x86_64/memrchr.S
190885
@@ -24,13 +24,13 @@
190885
 ENTRY (__memrchr)
190885
 	movd	%esi, %xmm1
190885
 
190885
-	sub	$16, %rdx
190885
+	sub	$16, %RDX_LP
190885
 	jbe	L(length_less16)
190885
 
190885
 	punpcklbw	%xmm1, %xmm1
190885
 	punpcklbw	%xmm1, %xmm1
190885
 
190885
-	add	%rdx, %rdi
190885
+	add	%RDX_LP, %RDI_LP
190885
 	pshufd	$0, %xmm1, %xmm1
190885
 
190885
 	movdqu	(%rdi), %xmm0
190885
diff --git a/sysdeps/x86_64/multiarch/memrchr-avx2.S b/sysdeps/x86_64/multiarch/memrchr-avx2.S
190885
index b41a58bc..ce488dd9 100644
190885
--- a/sysdeps/x86_64/multiarch/memrchr-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/memrchr-avx2.S
190885
@@ -32,10 +32,10 @@ ENTRY (__memrchr_avx2)
190885
 	vmovd	%esi, %xmm0
190885
 	vpbroadcastb %xmm0, %ymm0
190885
 
190885
-	subq	$VEC_SIZE, %rdx
190885
+	sub	$VEC_SIZE, %RDX_LP
190885
 	jbe	L(last_vec_or_less)
190885
 
190885
-	addq	%rdx, %rdi
190885
+	add	%RDX_LP, %RDI_LP
190885
 
190885
 	/* Check the last VEC_SIZE bytes.  */
190885
 	vpcmpeqb (%rdi), %ymm0, %ymm1
190885
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
190885
index 2fe1e5ac..e99dbd7c 100644
190885
--- a/sysdeps/x86_64/x32/Makefile
190885
+++ b/sysdeps/x86_64/x32/Makefile
190885
@@ -6,7 +6,8 @@ CFLAGS-s_llround.c += -fno-builtin-lround
190885
 endif
190885
 
190885
 ifeq ($(subdir),string)
190885
-tests += tst-size_t-memchr tst-size_t-memcmp tst-size_t-memcpy
190885
+tests += tst-size_t-memchr tst-size_t-memcmp tst-size_t-memcpy \
190885
+	 tst-size_t-memrchr
190885
 endif
190885
 
190885
 ifeq ($(subdir),wcsmbs)
190885
diff --git a/sysdeps/x86_64/x32/tst-size_t-memrchr.c b/sysdeps/x86_64/x32/tst-size_t-memrchr.c
190885
new file mode 100644
190885
index 00000000..c83699c0
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-memrchr.c
190885
@@ -0,0 +1,57 @@
190885
+/* Test memrchr 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_NAME "memrchr"
190885
+#include "test-size_t.h"
190885
+
190885
+IMPL (memchr, 1)
190885
+
190885
+typedef void * (*proto_t) (const void *, int, size_t);
190885
+
190885
+static void *
190885
+__attribute__ ((noinline, noclone))
190885
+do_memrchr (parameter_t a, parameter_t b)
190885
+{
190885
+  return CALL (&b, a.p, (uintptr_t) b.p, a.len);
190885
+}
190885
+
190885
+static int
190885
+test_main (void)
190885
+{
190885
+  test_init ();
190885
+
190885
+  parameter_t src = { { page_size }, buf2 };
190885
+  parameter_t c = { { 0 }, (void *) (uintptr_t) 0x12 };
190885
+
190885
+  int ret = 0;
190885
+  FOR_EACH_IMPL (impl, 0)
190885
+    {
190885
+      c.fn = impl->fn;
190885
+      void * res = do_memrchr (src, c);
190885
+      if (res)
190885
+	{
190885
+	  error (0, 0, "Wrong result in function %s: %p != NULL",
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