From patchwork Fri Nov 29 21:03:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 36385 Received: (qmail 72392 invoked by alias); 29 Nov 2019 21:03:44 -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 72334 invoked by uid 89); 29 Nov 2019 21:03:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=sk:sparc64 X-HELO: mail-qk1-f195.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=uZ7ljY/tPkpsfNmkSLtnbeMdTkZ8/CcTaqA/754S96I=; b=T/qG1jGaDm1rpRD6rX9fMr0FRuCwXewIEnSnanmw8jJe0qXWmA+Pt1LnQoyL2a87on 8nIgEIOFJiTTffWkDKTc/DhTWC/hUCUK0QPossAMKK35zlTViLVTZfAnwXFKsIkDEz1e vTm/HLX9BR2LtLwXGBMXhOhR7OjzVOv5iOw7sEMsPK+R++vXa7xEcZx2BOBRKN/nF4Qo 7h3LCRH9Wja1EFDlLLacb07q8VzV4XHROkdwPs/V3MumG8LQnitc9Rfj/7UnApZ+eGiN 4DoJ0VmNbdo/5UXumSvakSnq5LkFHhxa/bAhA2IKdDfAgZBFWpdc6XxwldEIF6VLofED PU4A== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 5/7] elf: Enable relro for static build Date: Fri, 29 Nov 2019 18:03:25 -0300 Message-Id: <20191129210327.26434-5-adhemerval.zanella@linaro.org> In-Reply-To: <20191129210327.26434-1-adhemerval.zanella@linaro.org> References: <20191129210327.26434-1-adhemerval.zanella@linaro.org> The code is similar to the one at rtld.c, where its check for the PT_GNU_RELRO header values from program headers and call _dl_protected_relro with the updated l_relro_{addr,size} values. Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu, aarch64-linux-gnu, s390x-linux-gnu, and sparc64-linux-gnu. --- elf/dl-support.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/elf/dl-support.c b/elf/dl-support.c index 5526d5ee6e..bdb5c2ae91 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -367,14 +367,24 @@ _dl_non_dynamic_init (void) if (_dl_platform != NULL) _dl_platformlen = strlen (_dl_platform); - /* Scan for a program header telling us the stack is nonexecutable. */ if (_dl_phdr != NULL) - for (uint_fast16_t i = 0; i < _dl_phnum; ++i) - if (_dl_phdr[i].p_type == PT_GNU_STACK) + for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph) + switch (ph->p_type) { - _dl_stack_flags = _dl_phdr[i].p_flags; + /* Check if the stack is nonexecutable. */ + case PT_GNU_STACK: + _dl_stack_flags = ph->p_flags; + break; + + case PT_GNU_RELRO: + _dl_main_map.l_relro_addr = ph->p_vaddr; + _dl_main_map.l_relro_size = ph->p_memsz; break; } + + /* Setup relro on the binary itself. */ + if (_dl_main_map.l_relro_size) + _dl_protect_relro (&_dl_main_map); } #ifdef DL_SYSINFO_IMPLEMENTATION