190885
From ea31ff23cc0f5577d6947a717a2d733d7963f5c6 Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Fri, 1 Feb 2019 12:17:09 -0800
190885
Subject: [PATCH] x86-64 memchr/wmemchr: Properly handle the length parameter
190885
 [BZ #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 memchr/wmemchr for x32.  Tested on x86-64 and x32.  On
190885
x86-64, libc.so is the same with and withou the fix.
190885
190885
	[BZ #24097]
190885
	CVE-2019-6488
190885
	* sysdeps/x86_64/memchr.S: Use RDX_LP for length.  Clear the
190885
	upper 32 bits of RDX register.
190885
	* sysdeps/x86_64/multiarch/memchr-avx2.S: Likewise.
190885
	* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-memchr and
190885
	tst-size_t-wmemchr.
190885
	* sysdeps/x86_64/x32/test-size_t.h: New file.
190885
	* sysdeps/x86_64/x32/tst-size_t-memchr.c: Likewise.
190885
	* sysdeps/x86_64/x32/tst-size_t-wmemchr.c: Likewise.
190885
190885
(cherry picked from commit 97700a34f36721b11a754cf37a1cc40695ece1fd)
190885
---
190885
 sysdeps/x86_64/memchr.S                 | 10 ++--
190885
 sysdeps/x86_64/multiarch/memchr-avx2.S  |  8 ++-
190885
 sysdeps/x86_64/x32/Makefile             |  8 +++
190885
 sysdeps/x86_64/x32/test-size_t.h        | 35 ++++++++++++
190885
 sysdeps/x86_64/x32/tst-size_t-memchr.c  | 72 +++++++++++++++++++++++++
190885
 sysdeps/x86_64/x32/tst-size_t-wmemchr.c | 20 +++++++
190885
 6 files changed, 148 insertions(+), 5 deletions(-)
190885
 create mode 100644 sysdeps/x86_64/x32/test-size_t.h
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-memchr.c
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-wmemchr.c
190885
190885
diff --git a/sysdeps/x86_64/memchr.S b/sysdeps/x86_64/memchr.S
190885
index feef5d4f..cb320257 100644
190885
--- a/sysdeps/x86_64/memchr.S
190885
+++ b/sysdeps/x86_64/memchr.S
190885
@@ -34,12 +34,16 @@ ENTRY(MEMCHR)
190885
 	mov	%edi, %ecx
190885
 
190885
 #ifdef USE_AS_WMEMCHR
190885
-	test	%rdx, %rdx
190885
+	test	%RDX_LP, %RDX_LP
190885
 	jz	L(return_null)
190885
-	shl	$2, %rdx
190885
+	shl	$2, %RDX_LP
190885
 #else
190885
+# ifdef __ILP32__
190885
+	/* Clear the upper 32 bits.  */
190885
+	movl	%edx, %edx
190885
+# endif
190885
 	punpcklbw %xmm1, %xmm1
190885
-	test	%rdx, %rdx
190885
+	test	%RDX_LP, %RDX_LP
190885
 	jz	L(return_null)
190885
 	punpcklbw %xmm1, %xmm1
190885
 #endif
190885
diff --git a/sysdeps/x86_64/multiarch/memchr-avx2.S b/sysdeps/x86_64/multiarch/memchr-avx2.S
190885
index 5f5e7725..c81da19b 100644
190885
--- a/sysdeps/x86_64/multiarch/memchr-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/memchr-avx2.S
190885
@@ -40,16 +40,20 @@
190885
 ENTRY (MEMCHR)
190885
 # ifndef USE_AS_RAWMEMCHR
190885
 	/* Check for zero length.  */
190885
-	testq	%rdx, %rdx
190885
+	test	%RDX_LP, %RDX_LP
190885
 	jz	L(null)
190885
 # endif
190885
 	movl	%edi, %ecx
190885
 	/* Broadcast CHAR to YMM0.  */
190885
 	vmovd	%esi, %xmm0
190885
 # ifdef USE_AS_WMEMCHR
190885
-	shl	$2, %rdx
190885
+	shl	$2, %RDX_LP
190885
 	vpbroadcastd %xmm0, %ymm0
190885
 # else
190885
+#  ifdef __ILP32__
190885
+	/* Clear the upper 32 bits.  */
190885
+	movl	%edx, %edx
190885
+#  endif
190885
 	vpbroadcastb %xmm0, %ymm0
190885
 # endif
190885
 	/* Check if we may cross page boundary with one vector load.  */
190885
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
190885
index f2ebc24f..7d528889 100644
190885
--- a/sysdeps/x86_64/x32/Makefile
190885
+++ b/sysdeps/x86_64/x32/Makefile
190885
@@ -4,3 +4,11 @@ ifeq ($(subdir),math)
190885
 # 64-bit llround.  Add -fno-builtin-lround to silence the compiler.
190885
 CFLAGS-s_llround.c += -fno-builtin-lround
190885
 endif
190885
+
190885
+ifeq ($(subdir),string)
190885
+tests += tst-size_t-memchr
190885
+endif
190885
+
190885
+ifeq ($(subdir),wcsmbs)
190885
+tests += tst-size_t-wmemchr
190885
+endif
190885
diff --git a/sysdeps/x86_64/x32/test-size_t.h b/sysdeps/x86_64/x32/test-size_t.h
190885
new file mode 100644
190885
index 00000000..78a94086
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/test-size_t.h
190885
@@ -0,0 +1,35 @@
190885
+/* Test string/memory functions with size_t in the lower 32 bits of
190885
+   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
+#include <string/test-string.h>
190885
+
190885
+/* On x32, parameter_t may be passed in a 64-bit register with the LEN
190885
+   field in the lower 32 bits.  When the LEN field of 64-bit register
190885
+   is passed to string/memory function as the size_t parameter, only
190885
+   the lower 32 bits can be used.  */
190885
+typedef struct
190885
+{
190885
+  union
190885
+    {
190885
+      size_t len;
190885
+      void (*fn) (void);
190885
+    };
190885
+  void *p;
190885
+} parameter_t;
190885
diff --git a/sysdeps/x86_64/x32/tst-size_t-memchr.c b/sysdeps/x86_64/x32/tst-size_t-memchr.c
190885
new file mode 100644
190885
index 00000000..29a3daf1
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-memchr.c
190885
@@ -0,0 +1,72 @@
190885
+/* Test memchr 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
+#ifndef WIDE
190885
+# define TEST_NAME "memchr"
190885
+#else
190885
+# define TEST_NAME "wmemchr"
190885
+#endif /* WIDE */
190885
+#include "test-size_t.h"
190885
+
190885
+#ifndef WIDE
190885
+# define MEMCHR memchr
190885
+# define CHAR char
190885
+# define UCHAR unsigned char
190885
+#else
190885
+# include <wchar.h>
190885
+# define MEMCHR wmemchr
190885
+# define CHAR wchar_t
190885
+# define UCHAR wchar_t
190885
+#endif /* WIDE */
190885
+
190885
+IMPL (MEMCHR, 1)
190885
+
190885
+typedef CHAR * (*proto_t) (const CHAR*, int, size_t);
190885
+
190885
+static CHAR *
190885
+__attribute__ ((noinline, noclone))
190885
+do_memchr (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 / sizeof (CHAR) }, 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
+      CHAR *res = do_memchr (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
diff --git a/sysdeps/x86_64/x32/tst-size_t-wmemchr.c b/sysdeps/x86_64/x32/tst-size_t-wmemchr.c
190885
new file mode 100644
190885
index 00000000..877801d6
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-wmemchr.c
190885
@@ -0,0 +1,20 @@
190885
+/* Test wmemchr 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 WIDE 1
190885
+#include "tst-size_t-memchr.c"
190885
-- 
190885
GitLab
190885