From patchwork Thu Apr 20 18:42:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68089 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BAAED3857705 for ; Thu, 20 Apr 2023 18:43:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAAED3857705 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682016193; bh=Zb6BD4STXaO2v8RknRj5erWdyh9BVQK7meMH7HBBpU8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=g3ii7IwqI0Xr7kCOu2/+FzVOzJrdLlNWgdtU8QnPh6vyXkuCarBZyWdaQhurLEvYo UQ5OgCyLqSY6koP1D94hGW2x2b/lp0tWXhk6dQjrbwgWBZ27bFhCFVrCogk1u5GF4Z POTnRiWI5yTpjWeaEy5Hetel3X33eH+YdrKKlINY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 278E33858D37 for ; Thu, 20 Apr 2023 18:42:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 278E33858D37 Received: by mail-lj1-x236.google.com with SMTP id c3so3870739ljf.7 for ; Thu, 20 Apr 2023 11:42:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682016167; x=1684608167; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zb6BD4STXaO2v8RknRj5erWdyh9BVQK7meMH7HBBpU8=; b=finy6EAB/83ehmuetBSIZ4P7ykzNx6tqTUkntGpYTVMHbRTRdGvtqmCWgdwrGxU7mC m0YQ/mPh8yfxMO+1ru+a7J3sj77GXuKSwsP0d5jWTU3WDGQyTuqxK3iHPMI10+Epw86E vYRy6LkXYWID28Cn+ynELmi3436JDb1YBCf9jCS4riNO/MXLfAP+Xb5/3wgnKIz7IqdT L87z9WCVI98NqLJR71lwUT+eONQPXcQmqzYKPjMhJqHjumh4AG/i1UNQF/WtGUfA5nqC PyAOpavnRt3HTJR3T9+eNvYBWUtcTp3GuiGYcYPmiJ+ZgJzInY2YFrqzOYWfzstlVV6U O4+Q== X-Gm-Message-State: AAQBX9dy+tsivFDrH62OJNFOjBxgGnhdEsGItzSsf6XiIIqoYS4wMopJ 3ZQm2F7mLJK8SCXpseTs+yGDsWCMw+w= X-Google-Smtp-Source: AKy350aDmK5tfEdv+VE6lY88BUX3NZRDIxQ6UCgPM8Z9jYtsIzkGllue5PutXQOVzn7JzOntM5zU4w== X-Received: by 2002:a2e:8947:0:b0:2a8:c1a8:7760 with SMTP id b7-20020a2e8947000000b002a8c1a87760mr718793ljk.30.1682016167079; Thu, 20 Apr 2023 11:42:47 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id w4-20020ac25d44000000b004eb0c51780bsm304675lfd.29.2023.04.20.11.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 11:42:46 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault , Luca , Sergey Bugaev Subject: [PATCH 1/2] hurd: Don't migrate reply port into __init1_tcbhead Date: Thu, 20 Apr 2023 21:42:19 +0300 Message-Id: <20230420184220.300862-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 --- 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 From patchwork Thu Apr 20 18:42:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 68090 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 743353857017 for ; Thu, 20 Apr 2023 18:43:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 743353857017 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682016211; bh=N447C5S64S98YoX/xw4Hdv/uxiDQlZA/ZJmzLDqjDYM=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=bQcnlHMcUW9LAryOhyYBPweuJqsE8cGzWyqqgH5d3g0DNIpo61YcGgFLQgv287HT8 hvK6BG8JDjeiHQm3GmVtNjJHwuzy/aX77crHGN/5KSKK6kavj9/M8QWT+gsXdEStuG s75+KROPmF+Acr7OORyH6PNSO77Fe3cEhuTP44/g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id DB6B33858C74 for ; Thu, 20 Apr 2023 18:43:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB6B33858C74 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2a8ba5f1d6bso7056161fa.2 for ; Thu, 20 Apr 2023 11:43:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682016186; x=1684608186; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N447C5S64S98YoX/xw4Hdv/uxiDQlZA/ZJmzLDqjDYM=; b=CqwknDViJ4mm4gBXno7gJfw+KQArDOsUh1cWrveHJp5KPsR5xvyBymlf+YhWJ7zQWk gOLQM6vV5cM3HMTTISSXyg6AltITQjzy2MlJ5XiWknX6yXwG2XBz31wVIhyNIpTyVL/m fohZE5Q4xXO3ahkW0/3ajvAA/ojKBnlZYRUSlGmmPxWcxz28EoRrr1f9YmH6xuMPvl8a 3HaMBhn/vm85SEg56db8BhBUU7epkZy51xbUAxW+yyqRqDwMXpiTWQveq4Otb67RUtLu qFPlE/d89cxHnvyFZIeTYZJxPxFGYmdM0qfQO8GHrwCX5Wrbuem/tmNZZxCzys6PwYsq jghw== X-Gm-Message-State: AAQBX9eUeCuoYO0K51lZUWWRAlmBLZj+61SdycjJf7c1SskSG/m44ppD m4COhFcIc7Ar2nAsJzmKtA5kxIhXaTTcFA== X-Google-Smtp-Source: AKy350ad0Ur00mAMhwR7ALh3wJmiRzp+lat7Tx4SVmn1hRqXPIqJLcxOL5JQAGVzfDCXJEDt5+FXHQ== X-Received: by 2002:a19:ad47:0:b0:4de:ca63:b2d2 with SMTP id s7-20020a19ad47000000b004deca63b2d2mr643508lfd.26.1682016186391; Thu, 20 Apr 2023 11:43:06 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id w4-20020ac25d44000000b004eb0c51780bsm304675lfd.29.2023.04.20.11.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 11:43:05 -0700 (PDT) To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault , Luca , Sergey Bugaev , "H . J . Lu" Subject: [VERY RFC PATCH 2/2] hurd: Make it possible to call memcpy very early Date: Thu, 20 Apr 2023 21:42:20 +0300 Message-Id: <20230420184220.300862-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230420184220.300862-1-bugaevc@gmail.com> References: <20230420184220.300862-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Normally, in static builds, the first code that runs is _start, in e.g. sysdeps/x86_64/start.S, which quickly calls __libc_start_main, passing it the argv etc. Among the first things __libc_start_main does is initializing the tunables (based on env), then CPU features, and then calls _dl_relocate_static_pie (). Specifically, this runs ifunc resolvers to pick, based on the CPU features discovered earlier, the most suitable implementation of "string" functions such as memcpy. Before that point, calling memcpy (or other ifunc-resolved functions) will not work. In the Hurd port, things are more complex. In order to get argv/env for our process, glibc normally needs to do an RPC to the exec server, unless our args/env are already located on the stack (which is what happens to bootstrap processes spawned by GNU Mach). Fetching our argv/env from the exec server has to be done before the call to __libc_start_main, since we need to know what our argv/env are to pass them to __libc_start_main. On the other hand, the implementation of the RPC (and other initial setup needed on the Hurd before __libc_start_main can be run) is not very trivial. In particular, it may (and on x86_64, will) use memcpy. But as described above, calling memcpy before __libc_start_main can not work, since the GOT entry for it is not yet initialized at that point. Work around this by pre-filling the GOT entry with the baseline version of memcpy, __memcpy_sse2_unaligned. This makes it possible for early calls to memcpy to just work. Once _dl_relocate_static_pie () is called, the baseline version will get replaced with the most suitable one, and that's what subsequent calls of memcpy are going to call. Also, apply the same treatment to __stpncpy, which can also be used by the RPCs (see mig_strncpy.c), and is an ifunc-resolved function on both x86_64 and i386. Tested on x86_64-gnu (!). Signed-off-by: Sergey Bugaev --- Please tell me: * if the approach is at all sane * if there's a better way to do this without hardcoding "__memcpy_sse2_unaligned" * are the GOT entries for indirect functions supposed to be statically initialized to anything (in the binary)? if yes, why? if not, why is PROGBITS and not NOBITS? * am I doing all this _GLOBAL_OFFSET_TABLE_, @GOT, @GOTOFF, @GOTPCREL correctly? * should there be a !PIC version as well? does the GOT exist under !PIC (to access indirect functions), and if it does then how do I access it? it would seem gcc just generates a direct $function even for indirect functions in this case. sysdeps/mach/hurd/i386/static-start.S | 7 +++++++ sysdeps/mach/hurd/x86_64/static-start.S | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/sysdeps/mach/hurd/i386/static-start.S b/sysdeps/mach/hurd/i386/static-start.S index c5d12645..1b1ae559 100644 --- a/sysdeps/mach/hurd/i386/static-start.S +++ b/sysdeps/mach/hurd/i386/static-start.S @@ -19,6 +19,13 @@ .text .globl _start _start: +#ifdef PIC + call __x86.get_pc_thunk.bx + addl $_GLOBAL_OFFSET_TABLE_, %ebx + leal __stpncpy_ia32@GOTOFF(%ebx), %eax + movl %eax, __stpncpy@GOT(%ebx) +#endif + call _hurd_stack_setup xorl %edx, %edx jmp _start1 diff --git a/sysdeps/mach/hurd/x86_64/static-start.S b/sysdeps/mach/hurd/x86_64/static-start.S index 982d3d52..81b3c0ac 100644 --- a/sysdeps/mach/hurd/x86_64/static-start.S +++ b/sysdeps/mach/hurd/x86_64/static-start.S @@ -19,6 +19,14 @@ .text .globl _start _start: + +#ifdef PIC + leaq __memcpy_sse2_unaligned(%rip), %rax + movq %rax, memcpy@GOTPCREL(%rip) + leaq __stpncpy_sse2_unaligned(%rip), %rax + movq %rax, __stpncpy@GOTPCREL(%rip) +#endif + call _hurd_stack_setup xorq %rdx, %rdx jmp _start1