190885
From 323c698927f67a153e278cc99870998a9c7c87af Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Fri, 1 Feb 2019 12:22:33 -0800
190885
Subject: [PATCH] x86-64 strncmp family: 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 the strncmp family for x32.  Tested on x86-64 and x32.
190885
On x86-64, libc.so is the same with and withou the fix.
190885
190885
	[BZ #24097]
190885
	CVE-2019-6488
190885
	* sysdeps/x86_64/multiarch/strcmp-avx2.S: Use RDX_LP for length.
190885
	* sysdeps/x86_64/multiarch/strcmp-sse42.S: Likewise.
190885
	* sysdeps/x86_64/strcmp.S: Likewise.
190885
	* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-strncasecmp,
190885
	tst-size_t-strncmp and tst-size_t-wcsncmp.
190885
	* sysdeps/x86_64/x32/tst-size_t-strncasecmp.c: New file.
190885
	* sysdeps/x86_64/x32/tst-size_t-strncmp.c: Likewise.
190885
	* sysdeps/x86_64/x32/tst-size_t-wcsncmp.c: Likewise.
190885
190885
(cherry picked from commit ee915088a0231cd421054dbd8abab7aadf331153)
190885
---
190885
 sysdeps/x86_64/multiarch/strcmp-avx2.S      |  6 +-
190885
 sysdeps/x86_64/multiarch/strcmp-sse42.S     |  6 +-
190885
 sysdeps/x86_64/strcmp.S                     |  6 +-
190885
 sysdeps/x86_64/x32/Makefile                 |  6 +-
190885
 sysdeps/x86_64/x32/tst-size_t-strncasecmp.c | 59 ++++++++++++++++
190885
 sysdeps/x86_64/x32/tst-size_t-strncmp.c     | 78 +++++++++++++++++++++
190885
 sysdeps/x86_64/x32/tst-size_t-wcsncmp.c     | 20 ++++++
190885
 7 files changed, 170 insertions(+), 11 deletions(-)
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-strncasecmp.c
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-strncmp.c
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-wcsncmp.c
190885
190885
diff --git a/sysdeps/x86_64/multiarch/strcmp-avx2.S b/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
index 327e3d87..156c1949 100644
190885
--- a/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
@@ -79,15 +79,15 @@
190885
 ENTRY (STRCMP)
190885
 # ifdef USE_AS_STRNCMP
190885
 	/* Check for simple cases (0 or 1) in offset.  */
190885
-	cmp	$1, %rdx
190885
+	cmp	$1, %RDX_LP
190885
 	je	L(char0)
190885
 	jb	L(zero)
190885
 #  ifdef USE_AS_WCSCMP
190885
 	/* Convert units: from wide to byte char.  */
190885
-	shl	$2, %rdx
190885
+	shl	$2, %RDX_LP
190885
 #  endif
190885
 	/* Register %r11 tracks the maximum offset.  */
190885
-	movq	%rdx, %r11
190885
+	mov	%RDX_LP, %R11_LP
190885
 # endif
190885
 	movl	%edi, %eax
190885
 	xorl	%edx, %edx
190885
diff --git a/sysdeps/x86_64/multiarch/strcmp-sse42.S b/sysdeps/x86_64/multiarch/strcmp-sse42.S
190885
index d3c07bd2..a1ebea46 100644
190885
--- a/sysdeps/x86_64/multiarch/strcmp-sse42.S
190885
+++ b/sysdeps/x86_64/multiarch/strcmp-sse42.S
190885
@@ -156,11 +156,11 @@ STRCMP_SSE42:
190885
 #endif
190885
 
190885
 #if defined USE_AS_STRNCMP || defined USE_AS_STRNCASECMP_L
190885
-	test	%rdx, %rdx
190885
+	test	%RDX_LP, %RDX_LP
190885
 	je	LABEL(strcmp_exitz)
190885
-	cmp	$1, %rdx
190885
+	cmp	$1, %RDX_LP
190885
 	je	LABEL(Byte0)
190885
-	mov	%rdx, %r11
190885
+	mov	%RDX_LP, %R11_LP
190885
 #endif
190885
 	mov	%esi, %ecx
190885
 	mov	%edi, %eax
190885
diff --git a/sysdeps/x86_64/strcmp.S b/sysdeps/x86_64/strcmp.S
190885
index e16945b9..f47c8ad4 100644
190885
--- a/sysdeps/x86_64/strcmp.S
190885
+++ b/sysdeps/x86_64/strcmp.S
190885
@@ -135,11 +135,11 @@ ENTRY (STRCMP)
190885
  * This implementation uses SSE to compare up to 16 bytes at a time.
190885
  */
190885
 #if defined USE_AS_STRNCMP || defined USE_AS_STRNCASECMP_L
190885
-	test	%rdx, %rdx
190885
+	test	%RDX_LP, %RDX_LP
190885
 	je	LABEL(strcmp_exitz)
190885
-	cmp	$1, %rdx
190885
+	cmp	$1, %RDX_LP
190885
 	je	LABEL(Byte0)
190885
-	mov	%rdx, %r11
190885
+	mov	%RDX_LP, %R11_LP
190885
 #endif
190885
 	mov	%esi, %ecx
190885
 	mov	%edi, %eax
190885
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
190885
index 98bd9ae9..db302839 100644
190885
--- a/sysdeps/x86_64/x32/Makefile
190885
+++ b/sysdeps/x86_64/x32/Makefile
190885
@@ -7,9 +7,11 @@ endif
190885
 
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
190885
+	 tst-size_t-memrchr tst-size_t-memset tst-size_t-strncasecmp \
190885
+	 tst-size_t-strncmp
190885
 endif
190885
 
190885
 ifeq ($(subdir),wcsmbs)
190885
-tests += tst-size_t-wmemchr tst-size_t-wmemcmp tst-size_t-wmemset
190885
+tests += tst-size_t-wmemchr tst-size_t-wmemcmp tst-size_t-wmemset \
190885
+	 tst-size_t-wcsncmp
190885
 endif
190885
diff --git a/sysdeps/x86_64/x32/tst-size_t-strncasecmp.c b/sysdeps/x86_64/x32/tst-size_t-strncasecmp.c
190885
new file mode 100644
190885
index 00000000..86233593
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-strncasecmp.c
190885
@@ -0,0 +1,59 @@
190885
+/* Test strncaecmp 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 "strncasecmp"
190885
+#include "test-size_t.h"
190885
+
190885
+IMPL (strncasecmp, 1)
190885
+
190885
+typedef int (*proto_t) (const char *, const char *, size_t);
190885
+
190885
+static int
190885
+__attribute__ ((noinline, noclone))
190885
+do_strncasecmp (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 }, buf1 };
190885
+  parameter_t src = { { 0 }, buf2 };
190885
+
190885
+  strncpy ((char *) buf1, (const char *) buf2, page_size);
190885
+
190885
+  int ret = 0;
190885
+  FOR_EACH_IMPL (impl, 0)
190885
+    {
190885
+      src.fn = impl->fn;
190885
+      int res = do_strncasecmp (dest, src);
190885
+      if (res)
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
diff --git a/sysdeps/x86_64/x32/tst-size_t-strncmp.c b/sysdeps/x86_64/x32/tst-size_t-strncmp.c
190885
new file mode 100644
190885
index 00000000..54e6bd83
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-strncmp.c
190885
@@ -0,0 +1,78 @@
190885
+/* Test strncmp 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
+#ifdef WIDE
190885
+# define TEST_NAME "wcsncmp"
190885
+#else
190885
+# define TEST_NAME "strncmp"
190885
+#endif
190885
+
190885
+#include "test-size_t.h"
190885
+
190885
+#ifdef WIDE
190885
+# include <wchar.h>
190885
+
190885
+# define STRNCMP wcsncmp
190885
+# define STRNCPY wcsncpy
190885
+# define CHAR wchar_t
190885
+#else
190885
+# define STRNCMP strncmp
190885
+# define STRNCPY strncpy
190885
+# define CHAR char
190885
+#endif
190885
+
190885
+IMPL (STRNCMP, 1)
190885
+
190885
+typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
190885
+
190885
+
190885
+static int
190885
+__attribute__ ((noinline, noclone))
190885
+do_strncmp (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
+  size_t size = page_size / sizeof (CHAR);
190885
+  parameter_t dest = { { size }, buf1 };
190885
+  parameter_t src = { { 0 }, buf2 };
190885
+
190885
+  STRNCPY ((CHAR *) buf1, (const CHAR *) buf2, size);
190885
+
190885
+  int ret = 0;
190885
+  FOR_EACH_IMPL (impl, 0)
190885
+    {
190885
+      src.fn = impl->fn;
190885
+      int res = do_strncmp (dest, src);
190885
+      if (res)
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
diff --git a/sysdeps/x86_64/x32/tst-size_t-wcsncmp.c b/sysdeps/x86_64/x32/tst-size_t-wcsncmp.c
190885
new file mode 100644
190885
index 00000000..4829647c
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-wcsncmp.c
190885
@@ -0,0 +1,20 @@
190885
+/* Test wcsncmp 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-strncmp.c"
190885
-- 
190885
GitLab
190885