190885
From 4f97e8708512462e87d2f1f1bdeb8cf55ce10e5a Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Fri, 1 Feb 2019 12:23:23 -0800
190885
Subject: [PATCH] x86-64 strncpy: 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 strncpy 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/multiarch/strcpy-sse2-unaligned.S: Use RDX_LP
190885
	for length.
190885
	* sysdeps/x86_64/multiarch/strcpy-ssse3.S: Likewise.
190885
	* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-strncpy.
190885
	* sysdeps/x86_64/x32/tst-size_t-strncpy.c: New file.
190885
190885
(cherry picked from commit c7c54f65b080affb87a1513dee449c8ad6143c8b)
190885
---
190885
 .../x86_64/multiarch/strcpy-sse2-unaligned.S  |  4 +-
190885
 sysdeps/x86_64/multiarch/strcpy-ssse3.S       |  6 +-
190885
 sysdeps/x86_64/x32/Makefile                   |  2 +-
190885
 sysdeps/x86_64/x32/tst-size_t-strncpy.c       | 58 +++++++++++++++++++
190885
 4 files changed, 64 insertions(+), 6 deletions(-)
190885
 create mode 100644 sysdeps/x86_64/x32/tst-size_t-strncpy.c
190885
190885
diff --git a/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S b/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S
190885
index 72bf7e85..50aca22d 100644
190885
--- a/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S
190885
+++ b/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S
190885
@@ -40,8 +40,8 @@
190885
 .text
190885
 ENTRY (STRCPY)
190885
 #  ifdef USE_AS_STRNCPY
190885
-	mov	%rdx, %r8
190885
-	test	%r8, %r8
190885
+	mov	%RDX_LP, %R8_LP
190885
+	test	%R8_LP, %R8_LP
190885
 	jz	L(ExitZero)
190885
 #  endif
190885
 	mov	%rsi, %rcx
190885
diff --git a/sysdeps/x86_64/multiarch/strcpy-ssse3.S b/sysdeps/x86_64/multiarch/strcpy-ssse3.S
190885
index 9858d0c4..0a62814a 100644
190885
--- a/sysdeps/x86_64/multiarch/strcpy-ssse3.S
190885
+++ b/sysdeps/x86_64/multiarch/strcpy-ssse3.S
190885
@@ -31,13 +31,13 @@ ENTRY (STRCPY)
190885
 
190885
 	mov	%rsi, %rcx
190885
 #  ifdef USE_AS_STRNCPY
190885
-	mov	%rdx, %r8
190885
+	mov	%RDX_LP, %R8_LP
190885
 #  endif
190885
 	mov	%rdi, %rdx
190885
 #  ifdef USE_AS_STRNCPY
190885
-	test	%r8, %r8
190885
+	test	%R8_LP, %R8_LP
190885
 	jz	L(Exit0)
190885
-	cmp	$8, %r8
190885
+	cmp	$8, %R8_LP
190885
 	jbe	L(StrncpyExit8Bytes)
190885
 # endif
190885
 	cmpb	$0, (%rcx)
190885
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
190885
index db302839..2a9e20a9 100644
190885
--- a/sysdeps/x86_64/x32/Makefile
190885
+++ b/sysdeps/x86_64/x32/Makefile
190885
@@ -8,7 +8,7 @@ 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
190885
+	 tst-size_t-strncmp tst-size_t-strncpy
190885
 endif
190885
 
190885
 ifeq ($(subdir),wcsmbs)
190885
diff --git a/sysdeps/x86_64/x32/tst-size_t-strncpy.c b/sysdeps/x86_64/x32/tst-size_t-strncpy.c
190885
new file mode 100644
190885
index 00000000..4dec71e6
190885
--- /dev/null
190885
+++ b/sysdeps/x86_64/x32/tst-size_t-strncpy.c
190885
@@ -0,0 +1,58 @@
190885
+/* Test strncpy 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 "strncpy"
190885
+#include "test-size_t.h"
190885
+
190885
+IMPL (strncpy, 1)
190885
+
190885
+typedef char *(*proto_t) (char *, const char*, size_t);
190885
+
190885
+static void *
190885
+__attribute__ ((noinline, noclone))
190885
+do_strncpy (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
+  int ret = 0;
190885
+  FOR_EACH_IMPL (impl, 0)
190885
+    {
190885
+      src.fn = impl->fn;
190885
+      do_strncpy (dest, src);
190885
+      int res = strncmp (dest.p, src.p, dest.len);
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
-- 
190885
GitLab
190885