From patchwork Wed Feb 12 19:00:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 864475 Delivered-To: patch@linaro.org Received: by 2002:a5d:47a6:0:b0:38f:210b:807b with SMTP id 6csp80625wrb; Wed, 12 Feb 2025 11:02:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXLp1IhpGElVYwwsm1BzxRgAmvTW8OghDoazXvRs2ScLk7h9vLK61t5GlFQtqBbPUb9RPAEeA==@linaro.org X-Google-Smtp-Source: AGHT+IFpJ+ijpt0aiVsYLRDliWKjJ8HjsdCped1BRJnri0FVa6rTMv87uVIbiNxRPYCtgsoafyiR X-Received: by 2002:a05:6102:3751:b0:4bb:e2a1:183e with SMTP id ada2fe7eead31-4bbf22df22dmr4753819137.18.1739386945258; Wed, 12 Feb 2025 11:02:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1739386945; cv=pass; d=google.com; s=arc-20240605; b=Hmn1EOl6evI9ffFgZThVxeuSEq49woVY4PVhbitbp7/aUOuTRZS1gCecU3MeUkxK5Q y+8YqtujWerUCRVYCwdFn7goAh4Odt6jcZZWOGm9BLBmSOCENG17f2AIaBisYt14UKxC dCTdEu5O5HFS1ZmlCU+U/7ygK6CwIpiaZtxAKX4zSfYW0vbSWsTd7O7e7W8F1JUxmhgh vvh36yy+S2up4Op9iQ3+Bf7kppgRbUNmEUcqSPH1UdtUOS11nV5OcUyXyWP1X/EAIN8F F5REHIlWeN0OaA3WWfdh5krVgiPkgWgQGDUcjyrUE715xin5Q/DMlT0oAzVCYiGSvGzm F1Sw== 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=bkdr6CcV300fn5th87sFH2Avn9Zue32d8m5Z/rTwYb0=; fh=rCa3ppVe5iCiIg2hlr/ZWmuMjHCO35lkvmJd+GFan0o=; b=Mwmm/4STjMlcPjvaw2yNHyPwTERMqI4+u5a4Pliw/OT+9oRSP2fy89UIKYVyuQPGJN I3bVEsge3+oIa0GCTmEwx8V02TguY9Qb6a2Oz8cF9es2aBxncwtChUqcBXUBk23TSWwv DNFh05qKWNCINNEhnpNwF+be2LkmJlNQdLrJo66p7wY1bcp8EnDZAsDKGmaqaYuHPBsZ 3ZqY2e5ux6oIafM1n+jjmeEe84cwuLCDn/YpttdxKq8dgloFWVLiJdevgAhEngOc/DBP ezQwrsdNqybRWGqo2euLQqDdSroxeyKFzEz9SEgqDNg3BkNI0idPq0TzK6Ed9nWAj4X9 jgFg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ELEu2OLO; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id ada2fe7eead31-4bbc29474a5si1888726137.354.2025.02.12.11.02.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 11:02:25 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ELEu2OLO; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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 1C6C9385840E for ; Wed, 12 Feb 2025 19:02:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C6C9385840E 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=ELEu2OLO X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc31.google.com (mail-oo1-xc31.google.com [IPv6:2607:f8b0:4864:20::c31]) by sourceware.org (Postfix) with ESMTPS id 34A173858C53 for ; Wed, 12 Feb 2025 19:02:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 34A173858C53 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 34A173858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739386922; cv=none; b=Mmpycn+qgUlAB9EIYQHFwkBjRIn6G20Nn5wV8OcgZA6CUaMuQvfwyhvanleZj25kZe6NaGZYHjKWytvEUyCujx0PTaX8flv/jXkmJt8bZCI7yG9BQ0RC25WnxDz8NxK8tq7hiGif75PHn2IK6hBMFACAzqGzcDsLT0lkMlQHp4o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739386922; c=relaxed/simple; bh=DnDdE7HztCFjw0y2Oahhu6VS18ZLS3NcllUltTmfYnA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nN1rHpf45ni1CCttyl4A7tg9TD/JoItuhbeIBSk0Jzl9TGEHsHlbC6kd5Iebl7jNAzG+ZD7ZXsiX+EUjwhFTm93LVO1IqKYfXkAcZC5leji+ANQL1g3Vqge4hxvuNMa0KkdDE0BEQuS0Q88m+GvF4jCRkw5rdvgp+a4gsPfKAKM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34A173858C53 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-5fc8a0cb812so56564eaf.0 for ; Wed, 12 Feb 2025 11:02:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1739386921; x=1739991721; 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=bkdr6CcV300fn5th87sFH2Avn9Zue32d8m5Z/rTwYb0=; b=ELEu2OLOMV4CtTGlwlou/aKrGjqTcbHG6PMhIN6+mDiN6QYSvs+I4lMjJOibUz31sI s0icVyl++OsAmWap8jv7yPjs0PM9NPifpKyGnZjR5qotUyr00ZFvhK4NBiKCzQFrlab4 56CXSlPJPXyPeQMMfO623k5/gh2o+FumbVwVUyxfdvaLVfftBnA4J51xYFqafAo5hhsV 1CaFqS2Ft3SGZ9JVXSiwM1GCiAFavmsLk/sESpHgvqeiwCR4ypOmBhF34kyqahTFEq56 cxXohiLERyBkaz97/RAC2z5qrFRt7TZ4Io1yNARaNYNZj1W7H3kkV2SJjMKONqIuQTXR Fa5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739386921; x=1739991721; 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=bkdr6CcV300fn5th87sFH2Avn9Zue32d8m5Z/rTwYb0=; b=UWrUaUMmCGoPBK5/Lua4U3QjV0MvL9OK70y6gdJ5GRssy4s4GriNG9bYMb6zcbE/XZ U98jFGpXlAqpHu7INZ4NsTLQtZtVv+9kQ4UHGNmCtlpsCVqRLaX+BzJrYNO6xMdFcES4 1YTOL7nM15+wALCDX2xkXZ/6Vt5TtrcCUU1hSSt6O/kXxLaf3WIXMzxgokGMlVdnbVVD WDCS9DyKaGrBhLwqIvWT4rBNNOVrHTwyK68Ieay+/ZKnBULy+VvgfdNkluuvqT4dGy+9 DkNvI0jTPRGC7tO7QssWRA+e2AGLNXth6FRE9r6OZNqU0ylPNgor1Vr4t9q59YB0i9NY 1Jjg== X-Gm-Message-State: AOJu0Ywn4ZYMQvF6ZPfs40+SZ1ukkNmH+qc2nuDBblKZmDt300VvV+y7 pVdVq/vjIBlv4cxhyzpVI8RcV53uEFu4IeTWuT2mokdIi4pfIA21u3cy2ZOhOZHNamo60tJC+77 5 X-Gm-Gg: ASbGncs4OSgFrrCHAHFJNBBRiZ32r65NQEHryOaA2nV2yQ3humQpxq3nH1wUR0dmGpN X0Ir0LFL1Kuxfk7+Tb4luhteC0lpB31GK0cADHtbjzq2OO/d3BLEgPKEkHs83Otph/oBufTexZ7 rOIiX6eHlxFFVjDZ6NGm6hVr4/gvRbGvwFWdRt3W2DGtX8N/i/9vbw8oui5t7xgZmqm3q14H4hz m71v/+GkrtUa65xv9oTAlzldwkRHOjKok+T2VZrt1PuEVlhMSEqCefHHXfW0Fn2T36+vdqao/rN g0Q/rnOxrBO0x0Tdcau2wcUe2/nq X-Received: by 2002:a05:6871:10e:b0:29e:63c3:3392 with SMTP id 586e51a60fabf-2b8d64ecc6cmr2509904fac.15.1739386920884; Wed, 12 Feb 2025 11:02:00 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c3:5d67:342c:89b2:7a6a:6c56]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-2b87e0cee6esm3612344fac.20.2025.02.12.11.01.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 11:02:00 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Sam James , Florian Weimer , Ethan Lee Subject: [PATCH v3] elf: Extend glibc.rtld.execstack tunable to force executable stack (BZ 32653) Date: Wed, 12 Feb 2025 16:00:30 -0300 Message-ID: <20250212190156.3544468-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 >From the bug report [1], multiple programs still require to dlopen shared libraries with either missing PT_GNU_STACK or with the executable bit set. Although, in some cases, it seems to be a hard-craft assembly source without the required .note.GNU-stack marking (so the static linker is forced to set the stack executable if the ABI requires it), other cases seem that the library uses trampolines [2]. Unfortunately, READ_IMPLIES_EXEC is not an option since on some ABIs (x86_64), the kernel clears the bit, making it unsupported. To avoid reinstating the broken code that changes stack permission on dlopen (0ca8785a28), this patch extends the glibc.rtld.execstack tunable to allow an option to force an executable stack at the program startup. The tunable is a security issue because it defeats the PT_GNU_STACK hardening. It has the slight advantage of making it explicit by the caller, and, as for other tunables, this is disabled for setuid binaries. A tunable also allows us to eventually remove it, but from previous experiences, it would require some time. Checked on x86_64-linux-gnu and i686-linux-gnu. [1] https://sourceware.org/bugzilla/show_bug.cgi?id=32653 [2] https://github.com/conda-forge/ctng-compiler-activation-feedstock/issues/143 --- Changes from v2: * Move exec stack handling earlier within the loader. * Make it generic and not Linux specific. --- elf/Makefile | 13 +++++++- elf/dl-execstack-tunable.c | 40 +++++++++++++++++++++++++ elf/dl-support.c | 4 +-- elf/dl-tunables.list | 2 +- elf/rtld.c | 6 ++-- elf/tst-execstack-prog-static-tunable.c | 1 + elf/tst-execstack-tunable.c | 1 + elf/tst-rtld-list-tunables.exp | 2 +- manual/tunables.texi | 10 +++++-- sysdeps/generic/ldsodefs.h | 13 ++++++++ sysdeps/unix/sysv/linux/dl-execstack.c | 1 + 11 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 elf/dl-execstack-tunable.c create mode 100644 elf/tst-execstack-prog-static-tunable.c create mode 100644 elf/tst-execstack-tunable.c diff --git a/elf/Makefile b/elf/Makefile index 5c833871d0..5c51f4cb2d 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -60,6 +60,7 @@ dl-routines = \ dl-deps \ dl-exception \ dl-execstack \ + dl-execstack-tunable \ dl-find_object \ dl-fini \ dl-init \ @@ -567,9 +568,11 @@ tests-execstack-yes = \ tst-execstack \ tst-execstack-needed \ tst-execstack-prog \ + tst-execstack-tunable \ # tests-execstack-yes tests-execstack-static-yes = \ - tst-execstack-prog-static + tst-execstack-prog-static \ + tst-execstack-prog-static-tunable \ # tests-execstack-static-yes ifeq (yes,$(run-built-tests)) tests-execstack-special-yes = \ @@ -1996,6 +1999,14 @@ LDFLAGS-tst-execstack-prog = -Wl,-z,execstack CFLAGS-tst-execstack-prog.c += -Wno-trampolines CFLAGS-tst-execstack-mod.c += -Wno-trampolines +# It expects loading a module with executable stack to work. +CFLAGS-tst-execstack-tunable.c += -DUSE_PTHREADS=0 -DDEFAULT_RWX_STACK=1 +$(objpfx)tst-execstack-tunable.out: $(objpfx)tst-execstack-mod.so +tst-execstack-tunable-ENV = GLIBC_TUNABLES=glibc.rtld.execstack=2 + +LDFLAGS-tst-execstack-prog-static-tunable = -Wl,-z,noexecstack +tst-execstack-prog-static-tunable-ENV = GLIBC_TUNABLES=glibc.rtld.execstack=2 + LDFLAGS-tst-execstack-prog-static = -Wl,-z,execstack CFLAGS-tst-execstack-prog-static.c += -Wno-trampolines diff --git a/elf/dl-execstack-tunable.c b/elf/dl-execstack-tunable.c new file mode 100644 index 0000000000..eeb0603abd --- /dev/null +++ b/elf/dl-execstack-tunable.c @@ -0,0 +1,40 @@ +/* Stack executability handling for GNU dynamic linker. + 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 + +void +_dl_handle_execstack_tunable (void) +{ + switch (TUNABLE_GET (glibc, rtld, execstack, int32_t, NULL)) + { + case stack_tunable_mode_disable: + if ((__glibc_unlikely (GL(dl_stack_flags)) & PF_X)) + _dl_fatal_printf ( +"Fatal glibc error: executable stack is not allowed\n"); + break; + + case stack_tunable_mode_force: + 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-support.c b/elf/dl-support.c index a7d5a5e8ab..0388e23448 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -332,9 +332,7 @@ _dl_non_dynamic_init (void) break; } - if ((__glibc_unlikely (GL(dl_stack_flags)) & PF_X) - && TUNABLE_GET (glibc, rtld, execstack, int32_t, NULL) == 0) - _dl_fatal_printf ("Fatal glibc error: executable stack is not allowed\n"); + _dl_handle_execstack_tunable (); call_function_static_weak (_dl_find_object_init); diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 0b6721bc51..c03c9967f0 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -138,7 +138,7 @@ glibc { execstack { type: INT_32 minval: 0 - maxval: 1 + maxval: 2 default: 1 } } diff --git a/elf/rtld.c b/elf/rtld.c index 115f1da37f..b9f69b007b 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1622,9 +1622,9 @@ dl_main (const ElfW(Phdr) *phdr, bool has_interp = rtld_setup_main_map (main_map); - if ((__glibc_unlikely (GL(dl_stack_flags)) & PF_X) - && TUNABLE_GET (glibc, rtld, execstack, int32_t, NULL) == 0) - _dl_fatal_printf ("Fatal glibc error: executable stack is not allowed\n"); + /* Handle this after PT_GNU_STACK parse, because it updates dl_stack_flags + if required. */ + _dl_handle_execstack_tunable (); /* If the current libname is different from the SONAME, add the latter as well. */ diff --git a/elf/tst-execstack-prog-static-tunable.c b/elf/tst-execstack-prog-static-tunable.c new file mode 100644 index 0000000000..88b0ca1263 --- /dev/null +++ b/elf/tst-execstack-prog-static-tunable.c @@ -0,0 +1 @@ +#include diff --git a/elf/tst-execstack-tunable.c b/elf/tst-execstack-tunable.c new file mode 100644 index 0000000000..9f03b0f7ca --- /dev/null +++ b/elf/tst-execstack-tunable.c @@ -0,0 +1 @@ +#include diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp index 9f5990f340..8df6f5906e 100644 --- a/elf/tst-rtld-list-tunables.exp +++ b/elf/tst-rtld-list-tunables.exp @@ -13,6 +13,6 @@ glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+) glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+) glibc.rtld.dynamic_sort: 2 (min: 1, max: 2) glibc.rtld.enable_secure: 0 (min: 0, max: 1) -glibc.rtld.execstack: 1 (min: 0, max: 1) +glibc.rtld.execstack: 1 (min: 0, max: 2) glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10) glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+) diff --git a/manual/tunables.texi b/manual/tunables.texi index 7f0246c789..5c96fae769 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -365,8 +365,11 @@ change the main stack permission if kernel starts with a non-executable stack. The @code{glibc.rtld.execstack} can be used to control whether an executable stack is allowed from the main program. Setting the value to @code{0} disables the ABI auto-negotiation (meaning no executable stacks even if the ABI or ELF -header requires it), while @code{1} enables auto-negotiation (although the -program might not need an executable stack). +header requires it), @code{1} enables auto-negotiation (although the program +might not need an executable stack), while @code{2} forces and executable +stack during initialization (this is provide for compatibility reasons, where +the program requires to dynamically load modules with executable stacks with +@code{dlopen}). When executable stacks are not allowed, and if the main program requires it, the loader will fail with an error message. @@ -380,7 +383,8 @@ of hardware capabilities and kernel configuration. @strong{NB:} Trying to load a dynamic shared library with @code{dlopen} or @code{dlmopen} that requires an executable stack will always fail if the main program does not require an executable stack at loading time. This -is enforced regardless of the tunable value. +can be disable by setting the tunable to @code{2}, where the stack is +always executable. @end deftp @node Elision Tunables diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 8465cbaa9b..182172ea24 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -707,6 +707,19 @@ extern const ElfW(Phdr) *_dl_phdr; extern size_t _dl_phnum; #endif +/* Possible values for the glibc.rtld.execstack tunable. */ +enum stack_tunable_mode + { + /* Do not allow executable stacks, even if program requires it. */ + stack_tunable_mode_disable = 0, + /* Follows either ABI requirement, or the PT_GNU_STACK value. */ + stack_tunable_mode_enable = 1, + /* Always enable an executable stack. */ + stack_tunable_mode_force = 2 + }; + +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. */ diff --git a/sysdeps/unix/sysv/linux/dl-execstack.c b/sysdeps/unix/sysv/linux/dl-execstack.c index 9791b339ca..338c642040 100644 --- a/sysdeps/unix/sysv/linux/dl-execstack.c +++ b/sysdeps/unix/sysv/linux/dl-execstack.c @@ -17,6 +17,7 @@ . */ #include +#include int _dl_make_stack_executable (void **stack_endp)