|
|
190885 |
From bd6061191c63fb24025f3bb4cfc88519024f2b7a Mon Sep 17 00:00:00 2001
|
|
|
190885 |
From: "H.J. Lu" <hjl.tools@gmail.com>
|
|
|
190885 |
Date: Wed, 2 Mar 2022 14:03:51 -0800
|
|
|
190885 |
Subject: [PATCH] x86-64 memset/wmemset: 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 memset/wmemset 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/memset-avx512-no-vzeroupper.S: Use
|
|
|
190885 |
RDX_LP for length. Clear the upper 32 bits of RDX register.
|
|
|
190885 |
* sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S: Likewise.
|
|
|
190885 |
* sysdeps/x86_64/x32/Makefile (tests): Add tst-size_t-wmemset.
|
|
|
190885 |
* sysdeps/x86_64/x32/tst-size_t-memset.c: New file.
|
|
|
190885 |
* sysdeps/x86_64/x32/tst-size_t-wmemset.c: Likewise.
|
|
|
190885 |
|
|
|
190885 |
(cherry picked from commit 82d0b4a4d76db554eb6757acb790fcea30b19965)
|
|
|
190885 |
---
|
|
|
190885 |
.../multiarch/memset-avx512-no-vzeroupper.S | 6 +-
|
|
|
190885 |
.../multiarch/memset-vec-unaligned-erms.S | 34 +++++----
|
|
|
190885 |
sysdeps/x86_64/x32/Makefile | 4 +-
|
|
|
190885 |
sysdeps/x86_64/x32/tst-size_t-memset.c | 73 +++++++++++++++++++
|
|
|
190885 |
sysdeps/x86_64/x32/tst-size_t-wmemset.c | 20 +++++
|
|
|
190885 |
5 files changed, 121 insertions(+), 16 deletions(-)
|
|
|
190885 |
create mode 100644 sysdeps/x86_64/x32/tst-size_t-memset.c
|
|
|
190885 |
create mode 100644 sysdeps/x86_64/x32/tst-size_t-wmemset.c
|
|
|
190885 |
|
|
|
190885 |
diff --git a/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S b/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S
|
|
|
190885 |
index 689cc119..99e25519 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S
|
|
|
190885 |
+++ b/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S
|
|
|
190885 |
@@ -29,12 +29,16 @@
|
|
|
190885 |
.section .text.avx512,"ax",@progbits
|
|
|
190885 |
#if defined PIC
|
|
|
190885 |
ENTRY (MEMSET_CHK)
|
|
|
190885 |
- cmpq %rdx, %rcx
|
|
|
190885 |
+ cmp %RDX_LP, %RCX_LP
|
|
|
190885 |
jb HIDDEN_JUMPTARGET (__chk_fail)
|
|
|
190885 |
END (MEMSET_CHK)
|
|
|
190885 |
#endif
|
|
|
190885 |
|
|
|
190885 |
ENTRY (MEMSET)
|
|
|
190885 |
+# ifdef __ILP32__
|
|
|
190885 |
+ /* Clear the upper 32 bits. */
|
|
|
190885 |
+ mov %edx, %edx
|
|
|
190885 |
+# endif
|
|
|
190885 |
vpxor %xmm0, %xmm0, %xmm0
|
|
|
190885 |
vmovd %esi, %xmm1
|
|
|
190885 |
lea (%rdi, %rdx), %rsi
|
|
|
190885 |
diff --git a/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S b/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
|
|
|
190885 |
index 270a1d49..9a0fd818 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
|
|
|
190885 |
+++ b/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
|
|
|
190885 |
@@ -65,8 +65,8 @@
|
|
|
190885 |
.section SECTION(.text),"ax",@progbits
|
|
|
190885 |
#if VEC_SIZE == 16 && IS_IN (libc)
|
|
|
190885 |
ENTRY (__bzero)
|
|
|
190885 |
- movq %rdi, %rax /* Set return value. */
|
|
|
190885 |
- movq %rsi, %rdx /* Set n. */
|
|
|
190885 |
+ mov %RDI_LP, %RAX_LP /* Set return value. */
|
|
|
190885 |
+ mov %RSI_LP, %RDX_LP /* Set n. */
|
|
|
190885 |
pxor %xmm0, %xmm0
|
|
|
190885 |
jmp L(entry_from_bzero)
|
|
|
190885 |
END (__bzero)
|
|
|
190885 |
@@ -76,13 +76,13 @@ weak_alias (__bzero, bzero)
|
|
|
190885 |
#if IS_IN (libc)
|
|
|
190885 |
# if defined SHARED
|
|
|
190885 |
ENTRY_CHK (WMEMSET_CHK_SYMBOL (__wmemset_chk, unaligned))
|
|
|
190885 |
- cmpq %rdx, %rcx
|
|
|
190885 |
+ cmp %RDX_LP, %RCX_LP
|
|
|
190885 |
jb HIDDEN_JUMPTARGET (__chk_fail)
|
|
|
190885 |
END_CHK (WMEMSET_CHK_SYMBOL (__wmemset_chk, unaligned))
|
|
|
190885 |
# endif
|
|
|
190885 |
|
|
|
190885 |
ENTRY (WMEMSET_SYMBOL (__wmemset, unaligned))
|
|
|
190885 |
- shlq $2, %rdx
|
|
|
190885 |
+ shl $2, %RDX_LP
|
|
|
190885 |
WMEMSET_VDUP_TO_VEC0_AND_SET_RETURN (%esi, %rdi)
|
|
|
190885 |
jmp L(entry_from_bzero)
|
|
|
190885 |
END (WMEMSET_SYMBOL (__wmemset, unaligned))
|
|
|
190885 |
@@ -90,13 +90,17 @@ END (WMEMSET_SYMBOL (__wmemset, unaligned))
|
|
|
190885 |
|
|
|
190885 |
#if defined SHARED && IS_IN (libc)
|
|
|
190885 |
ENTRY_CHK (MEMSET_CHK_SYMBOL (__memset_chk, unaligned))
|
|
|
190885 |
- cmpq %rdx, %rcx
|
|
|
190885 |
+ cmp %RDX_LP, %RCX_LP
|
|
|
190885 |
jb HIDDEN_JUMPTARGET (__chk_fail)
|
|
|
190885 |
END_CHK (MEMSET_CHK_SYMBOL (__memset_chk, unaligned))
|
|
|
190885 |
#endif
|
|
|
190885 |
|
|
|
190885 |
ENTRY (MEMSET_SYMBOL (__memset, unaligned))
|
|
|
190885 |
MEMSET_VDUP_TO_VEC0_AND_SET_RETURN (%esi, %rdi)
|
|
|
190885 |
+# ifdef __ILP32__
|
|
|
190885 |
+ /* Clear the upper 32 bits. */
|
|
|
190885 |
+ mov %edx, %edx
|
|
|
190885 |
+# endif
|
|
|
190885 |
L(entry_from_bzero):
|
|
|
190885 |
cmpq $VEC_SIZE, %rdx
|
|
|
190885 |
jb L(less_vec)
|
|
|
190885 |
@@ -112,14 +116,14 @@ END (MEMSET_SYMBOL (__memset, unaligned))
|
|
|
190885 |
|
|
|
190885 |
# if VEC_SIZE == 16
|
|
|
190885 |
ENTRY (__memset_chk_erms)
|
|
|
190885 |
- cmpq %rdx, %rcx
|
|
|
190885 |
+ cmp %RDX_LP, %RCX_LP
|
|
|
190885 |
jb HIDDEN_JUMPTARGET (__chk_fail)
|
|
|
190885 |
END (__memset_chk_erms)
|
|
|
190885 |
|
|
|
190885 |
/* Only used to measure performance of REP STOSB. */
|
|
|
190885 |
ENTRY (__memset_erms)
|
|
|
190885 |
/* Skip zero length. */
|
|
|
190885 |
- testq %rdx, %rdx
|
|
|
190885 |
+ test %RDX_LP, %RDX_LP
|
|
|
190885 |
jnz L(stosb)
|
|
|
190885 |
movq %rdi, %rax
|
|
|
190885 |
ret
|
|
|
190885 |
@@ -131,11 +135,11 @@ ENTRY (MEMSET_SYMBOL (__memset, erms))
|
|
|
190885 |
L(stosb):
|
|
|
190885 |
/* Issue vzeroupper before rep stosb. */
|
|
|
190885 |
VZEROUPPER
|
|
|
190885 |
- movq %rdx, %rcx
|
|
|
190885 |
+ mov %RDX_LP, %RCX_LP
|
|
|
190885 |
movzbl %sil, %eax
|
|
|
190885 |
- movq %rdi, %rdx
|
|
|
190885 |
+ mov %RDI_LP, %RDX_LP
|
|
|
190885 |
rep stosb
|
|
|
190885 |
- movq %rdx, %rax
|
|
|
190885 |
+ mov %RDX_LP, %RAX_LP
|
|
|
190885 |
ret
|
|
|
190885 |
# if VEC_SIZE == 16
|
|
|
190885 |
END (__memset_erms)
|
|
|
190885 |
@@ -145,16 +149,20 @@ END (MEMSET_SYMBOL (__memset, erms))
|
|
|
190885 |
|
|
|
190885 |
# if defined SHARED && IS_IN (libc)
|
|
|
190885 |
ENTRY_CHK (MEMSET_CHK_SYMBOL (__memset_chk, unaligned_erms))
|
|
|
190885 |
- cmpq %rdx, %rcx
|
|
|
190885 |
+ cmp %RDX_LP, %RCX_LP
|
|
|
190885 |
jb HIDDEN_JUMPTARGET (__chk_fail)
|
|
|
190885 |
END_CHK (MEMSET_CHK_SYMBOL (__memset_chk, unaligned_erms))
|
|
|
190885 |
# endif
|
|
|
190885 |
|
|
|
190885 |
ENTRY (MEMSET_SYMBOL (__memset, unaligned_erms))
|
|
|
190885 |
MEMSET_VDUP_TO_VEC0_AND_SET_RETURN (%esi, %rdi)
|
|
|
190885 |
- cmpq $VEC_SIZE, %rdx
|
|
|
190885 |
+# ifdef __ILP32__
|
|
|
190885 |
+ /* Clear the upper 32 bits. */
|
|
|
190885 |
+ mov %edx, %edx
|
|
|
190885 |
+# endif
|
|
|
190885 |
+ cmp $VEC_SIZE, %RDX_LP
|
|
|
190885 |
jb L(less_vec)
|
|
|
190885 |
- cmpq $(VEC_SIZE * 2), %rdx
|
|
|
190885 |
+ cmp $(VEC_SIZE * 2), %RDX_LP
|
|
|
190885 |
ja L(stosb_more_2x_vec)
|
|
|
190885 |
/* From VEC and to 2 * VEC. No branch when size == VEC_SIZE. */
|
|
|
190885 |
VMOVU %VEC(0), -VEC_SIZE(%rdi,%rdx)
|
|
|
190885 |
diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
index e99dbd7c..98bd9ae9 100644
|
|
|
190885 |
--- a/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/Makefile
|
|
|
190885 |
@@ -7,9 +7,9 @@ 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
|
|
|
190885 |
+ tst-size_t-memrchr tst-size_t-memset
|
|
|
190885 |
endif
|
|
|
190885 |
|
|
|
190885 |
ifeq ($(subdir),wcsmbs)
|
|
|
190885 |
-tests += tst-size_t-wmemchr tst-size_t-wmemcmp
|
|
|
190885 |
+tests += tst-size_t-wmemchr tst-size_t-wmemcmp tst-size_t-wmemset
|
|
|
190885 |
endif
|
|
|
190885 |
diff --git a/sysdeps/x86_64/x32/tst-size_t-memset.c b/sysdeps/x86_64/x32/tst-size_t-memset.c
|
|
|
190885 |
new file mode 100644
|
|
|
190885 |
index 00000000..2c367af6
|
|
|
190885 |
--- /dev/null
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/tst-size_t-memset.c
|
|
|
190885 |
@@ -0,0 +1,73 @@
|
|
|
190885 |
+/* Test memset 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 "wmemset"
|
|
|
190885 |
+#else
|
|
|
190885 |
+# define TEST_NAME "memset"
|
|
|
190885 |
+#endif /* WIDE */
|
|
|
190885 |
+
|
|
|
190885 |
+#include "test-size_t.h"
|
|
|
190885 |
+
|
|
|
190885 |
+#ifdef WIDE
|
|
|
190885 |
+# include <wchar.h>
|
|
|
190885 |
+# define MEMSET wmemset
|
|
|
190885 |
+# define CHAR wchar_t
|
|
|
190885 |
+#else
|
|
|
190885 |
+# define MEMSET memset
|
|
|
190885 |
+# define CHAR char
|
|
|
190885 |
+#endif /* WIDE */
|
|
|
190885 |
+
|
|
|
190885 |
+IMPL (MEMSET, 1)
|
|
|
190885 |
+
|
|
|
190885 |
+typedef CHAR *(*proto_t) (CHAR *, int, size_t);
|
|
|
190885 |
+
|
|
|
190885 |
+static void *
|
|
|
190885 |
+__attribute__ ((noinline, noclone))
|
|
|
190885 |
+do_memset (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 |
+ CHAR ch = 0x23;
|
|
|
190885 |
+ parameter_t src = { { page_size / sizeof (CHAR) }, buf2 };
|
|
|
190885 |
+ parameter_t c = { { 0 }, (void *) (uintptr_t) ch };
|
|
|
190885 |
+
|
|
|
190885 |
+ int ret = 0;
|
|
|
190885 |
+ FOR_EACH_IMPL (impl, 0)
|
|
|
190885 |
+ {
|
|
|
190885 |
+ c.fn = impl->fn;
|
|
|
190885 |
+ CHAR *p = (CHAR *) do_memset (src, c);
|
|
|
190885 |
+ size_t i;
|
|
|
190885 |
+ for (i = 0; i < src.len; i++)
|
|
|
190885 |
+ if (p[i] != ch)
|
|
|
190885 |
+ {
|
|
|
190885 |
+ error (0, 0, "Wrong result in function %s", impl->name);
|
|
|
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-wmemset.c b/sysdeps/x86_64/x32/tst-size_t-wmemset.c
|
|
|
190885 |
new file mode 100644
|
|
|
190885 |
index 00000000..955eb488
|
|
|
190885 |
--- /dev/null
|
|
|
190885 |
+++ b/sysdeps/x86_64/x32/tst-size_t-wmemset.c
|
|
|
190885 |
@@ -0,0 +1,20 @@
|
|
|
190885 |
+/* Test wmemset 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-memset.c"
|
|
|
190885 |
--
|
|
|
190885 |
GitLab
|
|
|
190885 |
|