From: Sergey Bugaev <bugaevc@gmail.com>
To: libc-alpha@sourceware.org
Cc: bug-hurd@gnu.org, Samuel Thibault <samuel.thibault@gnu.org>,
Luca <luca@orpolo.org>, Sergey Bugaev <bugaevc@gmail.com>
Subject: [PATCH 1/2] hurd: Don't migrate reply port into __init1_tcbhead
Date: Thu, 20 Apr 2023 21:42:19 +0300 [thread overview]
Message-ID: <20230420184220.300862-1-bugaevc@gmail.com> (raw)
Properly differentiate between setting up the real TLS with
TLS_INIT_TP, and setting up the early TLS (__init1_tcbhead) in static
builds. In the latter case, don't yet migrate the reply port into the
TCB, and don't yet set __libc_tls_initialized to 1.
This also lets us move the __init1_desc assignment inside
_hurd_tls_init ().
Fixes cd019ddd892e182277fadd6aedccc57fa3923c8d
"hurd: Don't leak __hurd_reply_port0"
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
sysdeps/mach/hurd/i386/tls.h | 18 ++++++++++++------
sysdeps/mach/hurd/x86/init-first.c | 7 +------
sysdeps/mach/hurd/x86_64/tls.h | 11 ++++++-----
3 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h
index 0ac8917a..e124fb10 100644
--- a/sysdeps/mach/hurd/i386/tls.h
+++ b/sysdeps/mach/hurd/i386/tls.h
@@ -136,7 +136,7 @@ __LIBC_NO_TLS (void)
special attention since 'errno' is not yet available and if the
operation can cause a failure 'errno' must not be touched. */
static inline bool __attribute__ ((unused))
-_hurd_tls_init (tcbhead_t *tcb)
+_hurd_tls_init (tcbhead_t *tcb, bool full)
{
HURD_TLS_DESC_DECL (desc, tcb);
thread_t self = __mach_thread_self ();
@@ -148,8 +148,9 @@ _hurd_tls_init (tcbhead_t *tcb)
tcb->tcb = tcb;
/* We always at least start the sigthread anyway. */
tcb->multiple_threads = 1;
- /* Take over the reply port we've been using. */
- tcb->reply_port = __hurd_reply_port0;
+ if (full)
+ /* Take over the reply port we've been using. */
+ tcb->reply_port = __hurd_reply_port0;
/* Get the first available selector. */
int sel = -1;
@@ -175,15 +176,20 @@ _hurd_tls_init (tcbhead_t *tcb)
/* Now install the new selector. */
asm volatile ("mov %w0, %%gs" :: "q" (sel));
- /* This port is now owned by the TCB. */
- __hurd_reply_port0 = MACH_PORT_NULL;
+ if (full)
+ /* This port is now owned by the TCB. */
+ __hurd_reply_port0 = MACH_PORT_NULL;
+#ifndef SHARED
+ else
+ __init1_desc = sel;
+#endif
out:
__mach_port_deallocate (__mach_task_self (), self);
return success;
}
-# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr))
+# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr), 1)
#else /* defined (SHARED) && !IS_IN (rtld) */
# define __LIBC_NO_TLS() 0
#endif
diff --git a/sysdeps/mach/hurd/x86/init-first.c b/sysdeps/mach/hurd/x86/init-first.c
index 89a5f44c..d74a3c86 100644
--- a/sysdeps/mach/hurd/x86/init-first.c
+++ b/sysdeps/mach/hurd/x86/init-first.c
@@ -156,12 +156,7 @@ first_init (void)
#ifndef SHARED
/* In the static case, we need to set up TLS early so that the stack
protection guard can be read at gs:0x14 by the gcc-generated snippets. */
- _hurd_tls_init (&__init1_tcbhead);
-
- /* Make sure __LIBC_NO_TLS () keeps evaluating to 1. */
-# ifndef __x86_64__
- asm ("movw %%gs,%w0" : "=m" (__init1_desc));
-# endif
+ _hurd_tls_init (&__init1_tcbhead, 0);
#endif
RUN_RELHOOK (_hurd_preinit_hook, ());
diff --git a/sysdeps/mach/hurd/x86_64/tls.h b/sysdeps/mach/hurd/x86_64/tls.h
index 2b7135f6..1274723a 100644
--- a/sysdeps/mach/hurd/x86_64/tls.h
+++ b/sysdeps/mach/hurd/x86_64/tls.h
@@ -173,7 +173,7 @@ extern unsigned char __libc_tls_initialized;
# define __LIBC_NO_TLS() __builtin_expect (!__libc_tls_initialized, 0)
static inline bool __attribute__ ((unused))
-_hurd_tls_init (tcbhead_t *tcb)
+_hurd_tls_init (tcbhead_t *tcb, bool full)
{
error_t err;
thread_t self = __mach_thread_self ();
@@ -181,11 +181,12 @@ _hurd_tls_init (tcbhead_t *tcb)
/* We always at least start the sigthread anyway. */
tcb->multiple_threads = 1;
- /* Take over the reply port we've been using. */
- tcb->reply_port = __hurd_reply_port0;
+ if (full)
+ /* Take over the reply port we've been using. */
+ tcb->reply_port = __hurd_reply_port0;
err = _hurd_tls_new (self, tcb);
- if (err == 0)
+ if (err == 0 && full)
{
__libc_tls_initialized = 1;
/* This port is now owned by the TCB. */
@@ -195,7 +196,7 @@ _hurd_tls_init (tcbhead_t *tcb)
return err == 0;
}
-# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr))
+# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr), 1)
# else /* defined (SHARED) && !IS_IN (rtld) */
# define __LIBC_NO_TLS() 0
# endif
--
2.40.0
next reply other threads:[~2023-04-20 18:42 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-20 18:42 Sergey Bugaev [this message]
2023-04-20 18:42 ` [VERY RFC PATCH 2/2] hurd: Make it possible to call memcpy very early Sergey Bugaev
2023-04-20 20:25 ` H.J. Lu
2023-04-20 20:38 ` Adhemerval Zanella Netto
2023-04-21 9:27 ` Sergey Bugaev
2023-04-21 11:50 ` Adhemerval Zanella Netto
2023-04-21 12:38 ` Sergey Bugaev
2023-04-21 12:58 ` Adhemerval Zanella Netto
2023-04-21 13:56 ` Sergey Bugaev
2023-04-21 14:28 ` Adhemerval Zanella Netto
2023-04-22 11:59 ` Samuel Thibault
2023-04-22 12:18 ` Sergey Bugaev
2023-04-22 16:20 ` Samuel Thibault
2023-04-22 17:04 ` Sergey Bugaev
2023-04-21 11:51 ` Sergey Bugaev
2023-04-21 1:02 ` [PATCH 1/2] hurd: Don't migrate reply port into __init1_tcbhead Samuel Thibault
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230420184220.300862-1-bugaevc@gmail.com \
--to=bugaevc@gmail.com \
--cc=bug-hurd@gnu.org \
--cc=libc-alpha@sourceware.org \
--cc=luca@orpolo.org \
--cc=samuel.thibault@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).