From 434eca873f14f618d6c2279b54fb809fb56f2c50 Mon Sep 17 00:00:00 2001
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Mon, 6 Nov 2023 17:25:40 -0300
Subject: [PATCH] elf: Fix _dl_debug_vdprintf to work before self-relocation

The strlen might trigger and invalid GOT entry if it used before
the process is self-relocated (for instance on dl-tunables if any
error occurs).

For i386, _dl_writev with PIE requires to use the old 'int $0x80'
syscall mode because the calling the TLS register (gs) is not yet
initialized.

Reference:https://sourceware.org/git/?p=glibc.git;a=commit;h=434eca873f14f618d6c2279b54fb809fb56f2c50

Checked on x86_64-linux-gnu.
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
 elf/dl-misc.c                            | 15 +++++++++++++--
 stdio-common/Makefile                    |  5 +++++
 stdio-common/_itoa.c                     |  5 +++++
 sysdeps/unix/sysv/linux/i386/dl-writev.h | 24 ++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/i386/dl-writev.h

diff --git a/elf/dl-misc.c b/elf/dl-misc.c
index bba2e714..5b637c7e 100644
--- a/elf/dl-misc.c
+++ b/elf/dl-misc.c
@@ -16,6 +16,10 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <string.h>
+#if BUILD_PIE_DEFAULT
+# pragma GCC visibility push(hidden)
+#endif
 #include <assert.h>
 #include <fcntl.h>
 #include <ldsodefs.h>
@@ -23,7 +27,6 @@
 #include <link.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 #include <stdint.h>
 #include <sys/mman.h>
@@ -71,6 +74,14 @@ _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
   return result;
 }
 
+/* The function might be called before the process is self-relocated.  */
+static size_t
+_dl_debug_strlen (const char *s)
+{
+  const char *p = s;
+  for (; *s != '\0'; s++);
+  return s - p;
+}
 
 /* Bare-bones printf implementation.  This function only knows about
    the formats and flags needed and can handle only up to 64 stripes in
@@ -235,7 +246,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
 	    case 's':
 	      /* Get the string argument.  */
 	      iov[niov].iov_base = va_arg (arg, char *);
-	      iov[niov].iov_len = strlen (iov[niov].iov_base);
+	      iov[niov].iov_len = _dl_debug_strlen (iov[niov].iov_base);
 	      if (prec != -1)
 		iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
 	      ++niov;
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 803f16da..f9763c27 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -181,6 +181,11 @@ CFLAGS-isoc99_scanf.c += -fexceptions
 CFLAGS-errlist.c += $(fno-unit-at-a-time)
 CFLAGS-siglist.c += $(fno-unit-at-a-time)
 
+# Called during static library initialization, so turn stack-protection
+# off for non-shared builds.
+CFLAGS-_itoa.o = $(no-stack-protector)
+CFLAGS-_itoa.op = $(no-stack-protector)
+
 # scanf14a.c and scanf16a.c test a deprecated extension which is no
 # longer visible under most conformance levels; see the source files
 # for more detail.
diff --git a/stdio-common/_itoa.c b/stdio-common/_itoa.c
index c37d1c35..20c2e384 100644
--- a/stdio-common/_itoa.c
+++ b/stdio-common/_itoa.c
@@ -18,6 +18,11 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+/* Mark symbols hidden in static PIE for early self relocation to work.
+   Note: string.h may have ifuncs which cannot be hidden on i686.  */
+#if BUILD_PIE_DEFAULT
+# pragma GCC visibility push(hidden)
+#endif
 #include <gmp-mparam.h>
 #include <gmp.h>
 #include <limits.h>
diff --git a/sysdeps/unix/sysv/linux/i386/dl-writev.h b/sysdeps/unix/sysv/linux/i386/dl-writev.h
new file mode 100644
index 00000000..624d0e46
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/i386/dl-writev.h
@@ -0,0 +1,24 @@
+/* Message-writing for the dynamic linker.  Linux/i386 version.
+   Copyright (C) 2013-2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if BUILD_PIE_DEFAULT
+/* Can't use "call *%gs:SYSINFO_OFFSET" during startup in static PIE.  */
+# define I386_USE_SYSENTER 0
+#endif
+
+#include <sysdeps/unix/sysv/linux/dl-writev.h>
-- 
2.33.0

