From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id D5E13385828D for ; Sun, 19 Mar 2023 15:11:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5E13385828D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12f.google.com with SMTP id y15so12011515lfa.7 for ; Sun, 19 Mar 2023 08:11:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679238682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fbhKkyIUvwVhQIgWpcDaBhcFO5Q7BdDsWkFnV/fJdoo=; b=PlK+Z7bwhUioGEx/hxVA7YqV6jaCPj9+62i5Xu2CtkYCs0QKreI3o9GwqqxX/UFhnt lxcCy7ZTDT6jANvi3cpFxPWSbgY7VWadCL0bkjjS4+JUO1cSbVYYyBz0N7ooe+JLmDPT +RTw8OkskHKt6Tg/ox+OnbpN5UF0lkTassF0WTBej/84hVACkk0jSS/rP0oO+K/thLpo 5inROk5bh1IqI6T9LtvNDWXoj4oM8eROf4p5zFoiCfy7TVf+Fw/LFclbOYy0HV440LPe R1vGKF9hBNodTEB7pFBqZKunUf2ReBBJdJ2Us69JDz67vUtfzgASkK2E2EcCufbZLHIf cX6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679238682; 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=fbhKkyIUvwVhQIgWpcDaBhcFO5Q7BdDsWkFnV/fJdoo=; b=IsY679g+Rj+goBtlM3jYxcGmjbqYfooZnx5NZELduKXKNbXlUMFnMHd9ce9KzpWK2g wkdM4HmpJ94uUtGnDccq8wduUW3wnqkBzDPdc0k5NBP0xCkX8phvNKVwozmImST+0tSt iwBKeBCBqrel/Dp3hjxtEI8yFLltzMKu17AmY+87o2DrJ8afilr8Qdz4rcdcYI+EcwV4 lRK8n2+MQ3KHrFah9TtmsRBs2f0lsMAjHieztEw8uLXQFkYjzQVrQpnhczPqInG6GCkl fmIgD0iGstrbRPoyS+BoIs7lREL+iYgejKE1kgNYNvjiASl1F74uyhVl8z4BtD8vos/k V+2w== X-Gm-Message-State: AO0yUKUKIxdrzs1VOYZfK/QDkQHUwLk7BRRe3Nd/RKzbL657P4vK+c6V TRXVQbZoHok3RFbYzMD8EC4i0ZOcwKXqEQ== X-Google-Smtp-Source: AK7set9zzl8NVU2HKow4JsDDq6PzJU+EvkfknmcCIkMonH/vBoqB0OS1yITr6j7/Z1Od8rrSAwYnLg== X-Received: by 2002:ac2:4282:0:b0:4a9:39f4:579a with SMTP id m2-20020ac24282000000b004a939f4579amr6134351lfh.66.1679238682218; Sun, 19 Mar 2023 08:11:22 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:577a:76f4:df43:5e66]) by smtp.gmail.com with ESMTPSA id m19-20020ac24253000000b004e90dee5469sm1274089lfl.157.2023.03.19.08.11.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 08:11:21 -0700 (PDT) From: Sergey Bugaev To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Sergey Bugaev Subject: [RFC PATCH glibc 27/34] hurd: Don't leak __hurd_reply_port0 Date: Sun, 19 Mar 2023 18:10:10 +0300 Message-Id: <20230319151017.531737-28-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230319151017.531737-1-bugaevc@gmail.com> References: <20230319151017.531737-1-bugaevc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Previously, once we set up TLS, we would implicitly switch from using __hurd_reply_port0 to reply_port inside the TCB, leaving the former unused. But we never deallocated it, so it got leaked. Instead, migrate the port into the new TCB's reply_port slot. This avoids both the port leak and an extra syscall to create a new reply port for the TCB. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/i386/tls.h | 5 +++++ sysdeps/mach/hurd/x86_64/tls.h | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h index ee7b8004..0ac8917a 100644 --- a/sysdeps/mach/hurd/i386/tls.h +++ b/sysdeps/mach/hurd/i386/tls.h @@ -141,12 +141,15 @@ _hurd_tls_init (tcbhead_t *tcb) HURD_TLS_DESC_DECL (desc, tcb); thread_t self = __mach_thread_self (); bool success = true; + extern mach_port_t __hurd_reply_port0; /* This field is used by TLS accesses to get our "thread pointer" from the TLS point of view. */ 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; /* Get the first available selector. */ int sel = -1; @@ -172,6 +175,8 @@ _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; out: __mach_port_deallocate (__mach_task_self (), self); diff --git a/sysdeps/mach/hurd/x86_64/tls.h b/sysdeps/mach/hurd/x86_64/tls.h index da504d9c..2b7135f6 100644 --- a/sysdeps/mach/hurd/x86_64/tls.h +++ b/sysdeps/mach/hurd/x86_64/tls.h @@ -177,13 +177,20 @@ _hurd_tls_init (tcbhead_t *tcb) { error_t err; thread_t self = __mach_thread_self (); + extern mach_port_t __hurd_reply_port0; /* 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; err = _hurd_tls_new (self, tcb); if (err == 0) - __libc_tls_initialized = 1; + { + __libc_tls_initialized = 1; + /* This port is now owned by the TCB. */ + __hurd_reply_port0 = MACH_PORT_NULL; + } __mach_port_deallocate (__mach_task_self (), self); return err == 0; } -- 2.39.2