From patchwork Thu Jun 27 21:50:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dragan Mladjenovic X-Patchwork-Id: 33466 Received: (qmail 88506 invoked by alias); 27 Jun 2019 21:50:10 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 88494 invoked by uid 89); 27 Jun 2019 21:50:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_WEB, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: NAM01-BN3-obe.outbound.protection.outlook.com ARC-Seal: i=1; a=rsa-sha256; s=testarcselector01; d=microsoft.com; cv=none; b=gpcSuKECv7J023Mj58T7q3M6PHgzo3ffFqrkx9IudaMyUZ8I8aoh32++9O3BrYSReGbZSX3K5aU6EHkpDPX0pKx+gL6+P6CE7OsAVW/C8fYcV8ao5MYD7koabgDbDovedYx7aIrxEGEAH58+pXRo2d7D2SBRw4HTT6WN2C0qWgk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=testarcselector01; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=A0kdEVvBMDSs91HFrTBkXETh3lUd3vsvmDSBsggSQms=; b=iczGq3rr32mO0/AUH3IboI3c4eoNxPGoH8pIAitmq8oWJdgOaV55O0Ln352ORd+puE0hE8saXdrxgwotUIqt5lzr959TLj+dAhH+Xz7N8nLuHua0aa8068nGJW81yjNZ6yn4miKXz4SU4f547kJqfCbAa2qm8aRBjYgLYkK053Y= ARC-Authentication-Results: i=1; test.office365.com 1;spf=none;dmarc=none;dkim=none;arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wavesemi.onmicrosoft.com; s=selector1-wavesemi-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=A0kdEVvBMDSs91HFrTBkXETh3lUd3vsvmDSBsggSQms=; b=Na/B/8z7j4lLxV13UWx7pT9l8wPsAffRAIDnoAggxBePJLKHY0OjKnVWsvhFubFH9PCvq1KsrRlCmYKH/F0X5BoatGnC5xTGgi5LDKT6wdR1vOV8TW8nPg0JFUiOI0O19AoSwu3ZKMJ1+u3qgdSwbSm93w+DlezlewV9Q/D2KhA= From: Dragan Mladjenovic To: "libc-alpha@sourceware.org" CC: Joseph Myers , Carlos O'Donell , "Maciej W. Rozycki" , Faraz Shahbazker , Dragan Mladjenovic Subject: [PATCH 1/3] [ELF] Allow the machine to override stack permissions via USE_DL_EXEC_STACK_OVERRIDE. Date: Thu, 27 Jun 2019 21:50:04 +0000 Message-ID: <1561672142-5907-2-git-send-email-dmladjenovic@wavecomp.com> References: <1561672142-5907-1-git-send-email-dmladjenovic@wavecomp.com> In-Reply-To: <1561672142-5907-1-git-send-email-dmladjenovic@wavecomp.com> authentication-results: spf=none (sender IP is ) smtp.mailfrom=dmladjenovic@wavecomp.com; x-ms-oob-tlc-oobclassifiers: OLM:10000; received-spf: None (protection.outlook.com: wavecomp.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 MIME-Version: 1.0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: dmladjenovic@wavecomp.com This patch allows the machine-dependent code to override non-executable stack permissions by defining USE_DL_EXEC_STACK_OVERRIDE and implementing _dl_exec_stack_override. It is called early during the static startup after the os version check and during the load of every shared library or main executable if ld.so is invoked explicitly. * elf/dl-load.c (_dl_map_object_from_fd): Call '_dl_exec_stack_override'. * elf/dl-support.c (_dl_non_dynamic_init): Likewise. * sysdeps/generic/ldsodefs.h (_dl_exec_stack_override): New prototype. --- elf/dl-load.c | 10 ++++++++++ elf/dl-support.c | 8 +++++++- sysdeps/generic/ldsodefs.h | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) -- 1.9.1 diff --git a/elf/dl-load.c b/elf/dl-load.c index 5abeb86..9155b74 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1242,6 +1242,16 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* Adjust the PT_PHDR value by the runtime load address. */ l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr); +#ifdef USE_DL_EXEC_STACK_OVERRIDE + /* Program requests a non-executable stack, but architecture does + not support it. */ + if (__glibc_unlikely (_dl_exec_stack_override (&stack_flags) != 0)) + { + errstring = N_("cannot override stack memory protections"); + goto call_lose_errno; + } +#endif + if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X)) { /* The stack is presently not executable, but this module diff --git a/elf/dl-support.c b/elf/dl-support.c index 0a8b636..dd99d58 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -179,7 +179,6 @@ ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS; It returns an errno code or zero on success. */ int (*_dl_make_stack_executable_hook) (void **) = _dl_make_stack_executable; - /* Function in libpthread to wait for termination of lookups. */ void (*_dl_wait_lookup_done) (void); @@ -375,6 +374,13 @@ _dl_non_dynamic_init (void) _dl_stack_flags = _dl_phdr[i].p_flags; break; } + +#ifdef USE_DL_EXEC_STACK_OVERRIDE + if (__glibc_unlikely (_dl_exec_stack_override (&_dl_stack_flags) != 0)) + { + _dl_fatal_printf ("cannot override stack memory protections\n"); + } +#endif } #ifdef DL_SYSINFO_IMPLEMENTATION diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index b1fc5c3..4e1f0f1 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -642,6 +642,10 @@ extern size_t _dl_phnum; extern int _dl_make_stack_executable (void **stack_endp); rtld_hidden_proto (_dl_make_stack_executable) +#ifdef USE_DL_EXEC_STACK_OVERRIDE +extern int _dl_exec_stack_override (void *); +rtld_hidden_proto (_dl_exec_stack_override) +#endif /* 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 might use the variable which results in copy relocations on some