From patchwork Thu Apr 24 15:27:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 883944 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp3497641wrs; Thu, 24 Apr 2025 08:29:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrgkmhCQih9hHyjBYs/AUBn7CyTgr4/543jPoLOBGrmEPqlFRr7YSjZVrLZksL8q73DY9zzg==@linaro.org X-Google-Smtp-Source: AGHT+IF5eMp2mwDhNmWwurABeOgLrmTKuK0NHY+gRB3tqrK6465GANOI9nuTmKJ7I8xakcQZOLxq X-Received: by 2002:ac8:5aca:0:b0:472:bbb:1bab with SMTP id d75a77b69052e-47eb32af21amr41809461cf.24.1745508575192; Thu, 24 Apr 2025 08:29:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745508575; cv=pass; d=google.com; s=arc-20240605; b=OWU/lCAWSC4sg4epevC2AugiBhUeBNEGFQP0IB/9uAx18Oe0wrNZYWqug1hU9vC/KP MKfsGla6LSX49RxTzY0IIZtEheOsMDzWzOYYjSHhvfWL8Znm170Ve1UBSJ7RbkgClmHW TDtoa6TR00VjQY1ijE1VL2sY+DU9AVm7q69ppZ+PdoOVnct/meMr5hqZRm52bIS3BnQV Tq+/yFqEE/yM8gLNn6e1IR2Xd1+a+O7SsQ5dbfw/nrphOiWgib2wMqCvuK08wu/Ty8Wh bdfQYV/58EmDaD/LB/iTfN1Ltreay9cL9Ar0u9wvalH04XXxhW9wS2IBzXsPGXaC04PB HNng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=x1WiXcymm51LK+iKOd15FSJOe3GBukIbkD9DAyexYn4=; fh=GI6CHjEUvXwGhZ1zGVSYTklPCnv63kRiNJMpMKP8x34=; b=F+UvVf9sjf/kY8ryKk9mwg/NvB82g4qX4qs+9uSFZvyANJjb8ViYSLZe98ODoQllMU A22f7NylZy6Z2OrPzjSIBWJYyCM4PPZP5TDI3mrDoAWvtUZe1wPHFZJGOrFicvTyd6Mg zxCnLODXITq/vIzuGhcp8vu4qz5dxHRtGEQIcWmkO3e5mg2YCDq1k1P23rvJ+enfS50A yhyj3jCddjT+j8Sxv/lFqTrJzh6Wt8MF/sU69R12at4+Z8ODtrAQ60mwvDOG+KLyI747 siBk4UmH9lGhUMp62uo/s3TxpPfUwBYUWXcPUdDwsu53WCEjI0Y2n16MyJvPmH3iCorJ rL9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=z5YeY2tl; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d75a77b69052e-47e9f3c0030si19099841cf.133.2025.04.24.08.29.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 08:29:35 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=z5YeY2tl; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BECEB3858D29 for ; Thu, 24 Apr 2025 15:29:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BECEB3858D29 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=z5YeY2tl X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id DE9F13858D35 for ; Thu, 24 Apr 2025 15:28:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE9F13858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DE9F13858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745508523; cv=none; b=IPGOUwt8DFcQqyvkdrKJ38BqdsClGTeiDNvCWjDAD/3aWWrdOAsVBLis3pWOCrpZI1OgugN4Rkmddhf/i9IYy5zSCDoG/Yhj+AVp6cuAhro7IcZkaPAj1wfyrAGIOy/KQ+mPmmQviTxJ/G83vMX685/BkOH43FYwf0Tw0GUVuoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745508523; c=relaxed/simple; bh=nLIFH6mzOswSkuijwZnCmSQy4ztMbnANJdaYWhNyrwo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ASikubPO0lf61E0YpQztVdn+dUjPRiCmgTRFfHNiSXNXZfO9DBydc0VTN9gcX3na5ErZqG0yeaGZMJQ/vi9xXrsiEKnXjNw59nveSUR/d5JhNjpkQ/4bs1YBfm9D2vRPmFJQF2C7/qlU9rZUJ1lMXeiuUVlzC9knTz1y12eLJac= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE9F13858D35 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-224341bbc1dso15151165ad.3 for ; Thu, 24 Apr 2025 08:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745508521; x=1746113321; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=x1WiXcymm51LK+iKOd15FSJOe3GBukIbkD9DAyexYn4=; b=z5YeY2tlBojjUIVyPXy74EAt7gfhghuvC/SJMUYxGN4ZmBvUoyIMLzfDTzwv1anGhz YQiMUGl7ckAfiOfyG+IFMOefYd4FgcbC+cFu9LwlS23fV/0YsaYyZ5/M4vG5iKHu3J4V q7rkDgeYrTCDWItiR7x1SiSJMJJy8sechTqLuFEXcIoB4vaFEHrKALDVugpgFzB8PLUu xAxtk9c5twdyqblmemNeCbn/h8+foVtwf+EA2c9YoFEJk10NDIxGV+DNaazVgAp77Cw1 qNMaRPrUFrghwjUrixZD6J1GatSnvhyebEWjb1YlliUpMuZPbPB7mp4p9HQtHnWRcT8v Qqig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745508521; x=1746113321; 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=x1WiXcymm51LK+iKOd15FSJOe3GBukIbkD9DAyexYn4=; b=W+tXq4m/CRryI1iZtBZoRNUVk/SKpecU16D1fnBlT/kRajm/uIf+tNGARXqTL9Kt0t C/jiL+xAiPRLJXEIDYbKiTDG6ANqY3qzyG6efoHOFdZkDxnHCFl9deOoD0Fv9kQO8S6P t99LqgiQcEVBYhc99zVNvVTWIpoJFKONvnfeQR5pxl/BMfzOB7sgjARmWhdwnaCCR55R jDEYYaP1vb7gpPtLgMZlhv5Qj9JDdaBqsNEpbOGT/pDJJJOba/cCfhdnvO+v9DCw1nrO +u9YLZJk0zg/qh9QYYiuLithUBsvGzbImM+lYi5NSacjNuu9+tWXjwDM6bkp1UlSYiuM igwg== X-Gm-Message-State: AOJu0YwS10mlg3Cl6ykbh5A4tYaTj7E7I6kgOAxkUWMe1Av3s9uhqoW8 qTyZSnViEgA2gmfG0m8yKuaH6PaUP7MqNQLHj+HWLMpTJWYcFIhRd3xqOCwb6U5OsrZgX9pAZlY p X-Gm-Gg: ASbGncvpqYtIiVZXLwh1E7tlyHgipHuT08S35csvL8KNINooRox+8Qux+QsEbK5OTqZ NKTBs04lpwej2kn8QY/ApI04ROirI4bVVszsFfsCU0Ny5x+pPufrKfwGwwT6pdzG6R3mh4Y20Dw UrSezKppxFmrfVkQmR8LGv894glIulArvznri1FD3y9fle+S1U82lJIKTO95QGI/5KRbhSU1LJj O2XPQeHt1jQjGah+FxBRdD0Ja8+loMFRCf2re2QZsOvWnCSmWjNFykm1nlOfooXfTHlEvrsK9YV DB1ejbpUaYY4jGjj2BqOdcmttxNdqWo0WFdzi4ZAPYM40yIJHZ44 X-Received: by 2002:a17:902:e747:b0:22c:35c5:e30e with SMTP id d9443c01a7336-22db3bdb237mr40385605ad.13.1745508521325; Thu, 24 Apr 2025 08:28:41 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:8a45:813:35d1:41d5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db4dbc0d1sm14858455ad.72.2025.04.24.08.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 08:28:40 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Sam James , Luke Drummond Subject: [PATCH v2] nptl: Fix pthread_getattr_np when modules with execstack are allowed (BZ 32897) Date: Thu, 24 Apr 2025 12:27:44 -0300 Message-ID: <20250424152837.2542322-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org The BZ 32653 fix (12a497c716f0a06be5946cabb8c3ec22a079771e) kept the stack pointer zeroing from make_main_stack_executable on _dl_make_stack_executable. However, previously the 'stack_endp' pointed to temporary variable created before the call of _dl_map_object_from_fd; while now we use the __libc_stack_end directly. Since pthread_getattr_np relies on correct __libc_stack_end, if _dl_make_stack_executable is called (for instance, when glibc.rtld.execstack=2 is set) __libc_stack_end will be set to zero, and the call will always fail. The __libc_stack_end zero was used a mitigation hardening, but since 52a01100ad011293197637e42b5be1a479a2f4ae it is used solely on pthread_getattr_np code. So there is no point in zeroing anymore. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Sam James --- Changes from v1: * Better description on when this bug happens on commit message. * Improve tst-stack2.c test coverage. --- elf/dl-execstack-tunable.c | 2 +- elf/dl-execstack.c | 2 +- sysdeps/generic/ldsodefs.h | 2 +- sysdeps/mach/hurd/dl-execstack.c | 5 ++- sysdeps/pthread/Makefile | 9 +++++ sysdeps/pthread/tst-stack2-mod.c | 39 +++++++++++++++++++++ sysdeps/pthread/tst-stack2.c | 47 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/dl-execstack.c | 7 ++-- 8 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 sysdeps/pthread/tst-stack2-mod.c create mode 100644 sysdeps/pthread/tst-stack2.c diff --git a/elf/dl-execstack-tunable.c b/elf/dl-execstack-tunable.c index 6cef1a3036..e3b638aeaa 100644 --- a/elf/dl-execstack-tunable.c +++ b/elf/dl-execstack-tunable.c @@ -31,7 +31,7 @@ _dl_handle_execstack_tunable (void) break; case stack_tunable_mode_force: - if (_dl_make_stack_executable (&__libc_stack_end) != 0) + if (_dl_make_stack_executable (__libc_stack_end) != 0) _dl_fatal_printf ( "Fatal glibc error: cannot enable executable stack as tunable requires"); break; diff --git a/elf/dl-execstack.c b/elf/dl-execstack.c index e4d7dbe7f8..ceec5b2def 100644 --- a/elf/dl-execstack.c +++ b/elf/dl-execstack.c @@ -23,7 +23,7 @@ so as to mprotect it. */ int -_dl_make_stack_executable (void **stack_endp) +_dl_make_stack_executable (const void *stack_endp) { return ENOSYS; } diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index b5d5b3106c..fc4a3de767 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -733,7 +733,7 @@ void _dl_handle_execstack_tunable (void) attribute_hidden; /* This function changes the permission of the memory region pointed by STACK_ENDP to executable and update the internal memory protection flags for future thread stack creation. */ -int _dl_make_stack_executable (void **stack_endp) attribute_hidden; +int _dl_make_stack_executable (const void *stack_endp) attribute_hidden; /* Variable pointing to the end of the stack (or close to it). This value must be constant over the runtime of the application. Some programs diff --git a/sysdeps/mach/hurd/dl-execstack.c b/sysdeps/mach/hurd/dl-execstack.c index 0617d3a161..dc4719bd38 100644 --- a/sysdeps/mach/hurd/dl-execstack.c +++ b/sysdeps/mach/hurd/dl-execstack.c @@ -26,12 +26,11 @@ extern struct hurd_startup_data *_dl_hurd_data attribute_hidden; so as to mprotect it. */ int -_dl_make_stack_executable (void **stack_endp) +_dl_make_stack_executable (const void *stack_endp) { /* Challenge the caller. */ - if (__builtin_expect (*stack_endp != __libc_stack_end, 0)) + if (__glibc_unlikely (stack_endp != __libc_stack_end)) return EPERM; - *stack_endp = NULL; #if IS_IN (rtld) if (__mprotect ((void *)_dl_hurd_data->stack_base, _dl_hurd_data->stack_size, diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index d4869c624b..5acf505a90 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -273,6 +273,7 @@ tests += \ tst-spin4 \ tst-spin5 \ tst-stack1 \ + tst-stack2 \ tst-stdio1 \ tst-stdio2 \ tst-thrd-detach \ @@ -368,6 +369,7 @@ modules-names += \ tst-atfork4mod \ tst-create1mod \ tst-fini1mod \ + tst-stack2-mod \ tst-tls4moda \ tst-tls4modb \ # modules-names @@ -541,4 +543,11 @@ LDFLAGS-tst-create1 = -Wl,-export-dynamic $(objpfx)tst-create1: $(shared-thread-library) $(objpfx)tst-create1.out: $(objpfx)tst-create1mod.so +$(objpfx)tst-stack2.out: $(objpfx)tst-stack2-mod.so +LDFLAGS-tst-stack2-mod.so = -Wl,-z,execstack +ifeq ($(have-no-error-execstack),yes) +LDFLAGS-tst-stack2-mod.so += -Wl,--no-error-execstack +endif +tst-stack2-ENV = GLIBC_TUNABLES=glibc.rtld.execstack=2 + endif diff --git a/sysdeps/pthread/tst-stack2-mod.c b/sysdeps/pthread/tst-stack2-mod.c new file mode 100644 index 0000000000..806fdbcd8d --- /dev/null +++ b/sysdeps/pthread/tst-stack2-mod.c @@ -0,0 +1,39 @@ +/* Check if pthread_getattr_np works within modules with non-exectuble + stacks (BZ 32897). + Copyright (C) 2025 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 + . */ + +#include + +bool init_result; + +void +__attribute__ ((constructor)) +init (void) +{ + pthread_t me = pthread_self (); + pthread_attr_t attr; + init_result = pthread_getattr_np (me, &attr) == 0; +} + +int +mod_func (void) +{ + pthread_t me = pthread_self (); + pthread_attr_t attr; + return pthread_getattr_np (me, &attr); +} diff --git a/sysdeps/pthread/tst-stack2.c b/sysdeps/pthread/tst-stack2.c new file mode 100644 index 0000000000..20ab5af166 --- /dev/null +++ b/sysdeps/pthread/tst-stack2.c @@ -0,0 +1,47 @@ +/* Check if pthread_getattr_np works within modules with non-exectuble + stacks (BZ 32897). + Copyright (C) 2025 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 + . */ + +#include +#include +#include +#include + +static int +do_test (void) +{ + { + pthread_t me = pthread_self (); + pthread_attr_t attr; + TEST_COMPARE (pthread_getattr_np (me, &attr), 0); + } + + void *h = xdlopen ("tst-stack2-mod.so", RTLD_NOW); + + bool *init_result = xdlsym (h, "init_result"); + TEST_COMPARE (*init_result, true); + + int (*mod_func)(void) = xdlsym (h, "mod_func"); + TEST_COMPARE (mod_func (), 0); + + xdlclose (h); + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/dl-execstack.c b/sysdeps/unix/sysv/linux/dl-execstack.c index 9791b339ca..6db9601656 100644 --- a/sysdeps/unix/sysv/linux/dl-execstack.c +++ b/sysdeps/unix/sysv/linux/dl-execstack.c @@ -19,10 +19,10 @@ #include int -_dl_make_stack_executable (void **stack_endp) +_dl_make_stack_executable (const void *stack_endp) { /* This gives us the highest/lowest page that needs to be changed. */ - uintptr_t page = ((uintptr_t) *stack_endp + uintptr_t page = ((uintptr_t) stack_endp & -(intptr_t) GLRO(dl_pagesize)); if (__mprotect ((void *) page, GLRO(dl_pagesize), @@ -35,9 +35,6 @@ _dl_make_stack_executable (void **stack_endp) ) != 0) return errno; - /* Clear the address. */ - *stack_endp = NULL; - /* Remember that we changed the permission. */ GL(dl_stack_flags) |= PF_X;