|
|
190885 |
From 3e2556a4407e551a9c4086e67f46ef8d235cfe0d Mon Sep 17 00:00:00 2001
|
|
|
190885 |
From: "H.J. Lu" <hjl.tools@gmail.com>
|
|
|
190885 |
Date: Fri, 1 Feb 2019 12:24:08 -0800
|
|
|
190885 |
Subject: [PATCH] x86-64 strnlen/wcsnlen: 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 strnlen/wcsnlen 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/multiarch/strlen-avx2.S: Use RSI_LP for length.
|
|
|
190885 |
Clear the upper 32 bits of RSI register.
|
|
|
190885 |
* sysdeps/x86_64/strlen.S: Use RSI_LP for length.
|
|
|
190885 |
* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-strnlen
|
|
|
190885 |
and tst-size_t-wcsnlen.
|
|
|
190885 |
* sysdeps/x86_64/x32/tst-size_t-strnlen.c: New file.
|
|
|
190885 |
* sysdeps/x86_64/x32/tst-size_t-wcsnlen.c: Likewise.
|
|
|
190885 |
|
|
|
190885 |
(cherry picked from commit 5165de69c0908e28a380cbd4bb054e55ea4abc95)
|
|
|
190885 |
---
|
|
|
190885 |
sysdeps/x86_64/multiarch/strlen-avx2.S | 9 ++--
|
|
|
190885 |
sysdeps/x86_64/strlen.S | 12 ++---
|
|
|
190885 |
sysdeps/x86_64/x32/Makefile | 4 +-
|
|
|
190885 |
sysdeps/x86_64/x32/tst-size_t-strnlen.c | 72 +++++++++++++++++++++++++
|
|
|
190885 |
sysdeps/x86_64/x32/tst-size_t-wcsnlen.c | 20 +++++++
|
|
|
190885 |
5 files changed, 106 insertions(+), 11 deletions(-)
|
|
|
190885 |
create mode 100644 sysdeps/x86_64/x32/tst-size_t-strnlen.c
|
|
|
190885 |
create mode 100644 sysdeps/x86_64/x32/tst-size_t-wcsnlen.c
|
|
|
190885 |
|
|
|
190885 |
diff --git a/sysdeps/x86_64/multiarch/strlen-avx2.S b/sysdeps/x86_64/multiarch/strlen-avx2.S
|
|
|
190885 |
index fb2418cd..645e0446 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/multiarch/strlen-avx2.S
|
|
|
190885 |
+++ b/sysdeps/x86_64/multiarch/strlen-avx2.S
|
|
|
190885 |
@@ -42,12 +42,15 @@
|
|
|
190885 |
ENTRY (STRLEN)
|
|
|
190885 |
# ifdef USE_AS_STRNLEN
|
|
|
190885 |
/* Check for zero length. */
|
|
|
190885 |
- testq %rsi, %rsi
|
|
|
190885 |
+ test %RSI_LP, %RSI_LP
|
|
|
190885 |
jz L(zero)
|
|
|
190885 |
# ifdef USE_AS_WCSLEN
|
|
|
190885 |
- shl $2, %rsi
|
|
|
190885 |
+ shl $2, %RSI_LP
|
|
|
190885 |
+# elif defined __ILP32__
|
|
|
190885 |
+ /* Clear the upper 32 bits. */
|
|
|
190885 |
+ movl %esi, %esi
|
|
|
190885 |
# endif
|
|
|
190885 |
- movq %rsi, %r8
|
|
|
190885 |
+ mov %RSI_LP, %R8_LP
|
|
|
190885 |
# endif
|
|
|
190885 |
movl %edi, %ecx
|
|
|
190885 |
movq %rdi, %rdx
|
|
|
190885 |
diff --git a/sysdeps/x86_64/strlen.S b/sysdeps/x86_64/strlen.S
|
|
|
190885 |
index 01cb5fa8..f845f3d4 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/strlen.S
|
|
|
190885 |
+++ b/sysdeps/x86_64/strlen.S
|
|
|
190885 |
@@ -59,21 +59,21 @@ ENTRY(strlen)
|
|
|
190885 |
|
|
|
190885 |
#ifdef AS_STRNLEN
|
|
|
190885 |
/* Do not read anything when n==0. */
|
|
|
190885 |
- test %rsi, %rsi
|
|
|
190885 |
+ test %RSI_LP, %RSI_LP
|
|
|
190885 |
jne L(n_nonzero)
|
|
|
190885 |
xor %rax, %rax
|
|
|
190885 |
ret
|
|
|
190885 |
L(n_nonzero):
|
|
|
190885 |
# ifdef AS_WCSLEN
|
|
|
190885 |
- shlq $2, %rsi
|
|
|
190885 |
+ shl $2, %RSI_LP
|
|
|
190885 |
# endif
|
|
|
190885 |
|
|
|
190885 |
/* Initialize long lived registers. */
|
|
|
190885 |
|
|
|
190885 |
- add %rdi, %rsi
|
|
|
190885 |
- mov %rsi, %r10
|
|
|
190885 |
- and $-64, %r10
|
|
|
190885 |
- mov %rsi, %r11
|
|
|
190885 |
+ add %RDI_LP, %RSI_LP
|
|
|
190885 |
+ mov %RSI_LP, %R10_LP
|
|
|
190885 |
+ and $-64, %R10_LP
|
|
|
190885 |
+ mov %RSI_LP, %R11_LP
|
|
|
190885 |
#endif
|
|
|
190885 |
|
|
|
190885 |
pxor %xmm0, %xmm0
|
|
|
190885 |
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
index 2a9e20a9..1557724b 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
@@ -8,10 +8,10 @@ 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
|
|
|
190885 |
+ tst-size_t-strncmp tst-size_t-strncpy tst-size_t-strnlen
|
|
|
190885 |
endif
|
|
|
190885 |
|
|
|
190885 |
ifeq ($(subdir),wcsmbs)
|
|
|
190885 |
tests += tst-size_t-wmemchr tst-size_t-wmemcmp tst-size_t-wmemset \
|
|
|
190885 |
- tst-size_t-wcsncmp
|
|
|
190885 |
+ tst-size_t-wcsncmp tst-size_t-wcsnlen
|
|
|
190885 |
endif
|
|
|
190885 |
diff --git a/sysdeps/x86_64/x32/tst-size_t-strnlen.c b/sysdeps/x86_64/x32/tst-size_t-strnlen.c
|
|
|
190885 |
new file mode 100644
|
|
|
190885 |
index 00000000..690a4a8a
|
|
|
190885 |
--- /dev/null
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/tst-size_t-strnlen.c
|
|
|
190885 |
@@ -0,0 +1,72 @@
|
|
|
190885 |
+/* Test strnlen 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 "wcsnlen"
|
|
|
190885 |
+#else
|
|
|
190885 |
+# define TEST_NAME "strnlen"
|
|
|
190885 |
+#endif /* WIDE */
|
|
|
190885 |
+
|
|
|
190885 |
+#include "test-size_t.h"
|
|
|
190885 |
+
|
|
|
190885 |
+#ifdef WIDE
|
|
|
190885 |
+# include <wchar.h>
|
|
|
190885 |
+# define STRNLEN wcsnlen
|
|
|
190885 |
+# define CHAR wchar_t
|
|
|
190885 |
+#else
|
|
|
190885 |
+# define STRNLEN strnlen
|
|
|
190885 |
+# define CHAR char
|
|
|
190885 |
+#endif /* WIDE */
|
|
|
190885 |
+
|
|
|
190885 |
+IMPL (STRNLEN, 1)
|
|
|
190885 |
+
|
|
|
190885 |
+typedef size_t (*proto_t) (const CHAR *, size_t);
|
|
|
190885 |
+
|
|
|
190885 |
+static size_t
|
|
|
190885 |
+__attribute__ ((noinline, noclone))
|
|
|
190885 |
+do_strnlen (parameter_t a, parameter_t b)
|
|
|
190885 |
+{
|
|
|
190885 |
+ return CALL (&a, a.p, b.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 src = { { 0 }, buf2 };
|
|
|
190885 |
+ parameter_t c = { { size }, (void *) (uintptr_t) 'a' };
|
|
|
190885 |
+
|
|
|
190885 |
+ int ret = 0;
|
|
|
190885 |
+ FOR_EACH_IMPL (impl, 0)
|
|
|
190885 |
+ {
|
|
|
190885 |
+ src.fn = impl->fn;
|
|
|
190885 |
+ size_t res = do_strnlen (src, c);
|
|
|
190885 |
+ if (res != size)
|
|
|
190885 |
+ {
|
|
|
190885 |
+ error (0, 0, "Wrong result in function %s: 0x%x != 0x%x",
|
|
|
190885 |
+ impl->name, res, size);
|
|
|
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-wcsnlen.c b/sysdeps/x86_64/x32/tst-size_t-wcsnlen.c
|
|
|
190885 |
new file mode 100644
|
|
|
190885 |
index 00000000..093b4bbe
|
|
|
190885 |
--- /dev/null
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/tst-size_t-wcsnlen.c
|
|
|
190885 |
@@ -0,0 +1,20 @@
|
|
|
190885 |
+/* Test wcsnlen 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-strnlen.c"
|
|
|
190885 |
--
|
|
|
190885 |
GitLab
|
|
|
190885 |
|