Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1627524ybk; Thu, 21 May 2020 11:16:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLxZedCkDlGPtUTkIjVIVzw0HskP/Q/xx6m4t2mBC3zmTQSSdEwgYz+rPUTAdZz4L4wFRH X-Received: by 2002:a17:906:1ed3:: with SMTP id m19mr4968680ejj.429.1590084960586; Thu, 21 May 2020 11:16:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590084960; cv=none; d=google.com; s=arc-20160816; b=DHWsv7wWQaqJSjt8misPiiRIEXVUWwI1OazGc0B8p47yRkr4hwx8LWkgoQ/1ZYJkrw 5nK9XTgCH5ocfqHSnfq2gDlst+eontEdS7U4NNW8J7xH1P38US4EhYHl5GmcgcHMnqYg 97Aly7yMRB24O67JQUK5ghRBPe/bZ3jg7sKFsiMJk+e5ibsvgVfzIdNE7lWRfn/89Tli +b45ygddzBydnxrRNErrrLlQfEMQFsdbIZiiZe/mkmUgWqeHtZ+AJ7pRFB8jq2N4T+Bh 769ZjqNdPKcu2d1WlCuyf7WV5L5E37eEO6BqKwnLzsAjoyG+0Iwkg1/KVs/SCGV9xKWl gv3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=QnbqNy9EV06MW5CdU8p7rG6H8xARB4MCiMEyZtqlPZY=; b=DvE8Zue4pUCFBLCPHZbjrQygMikhHs7wGT2efM5QOzBuX0cEnpjLStUEOgzC3nb0h7 Bgs3xxoKqu6rNR3E+NSFueCh4mX29mH0YYXFygDGnh0QKaS1A/kYWvB3BL8klYWfXcnh +20sE/WpXK9MF4NZtVnQ/E1IqHSzf+S2D/T5Vs+3HCejPDatR47E/Fj/SU70BNpw9Lwf A3M8HKwMiUl2tT06bdWHfJXTD5HerM8P6Fp2EYVCjxQuWLo1wvtMG29ehQyFRLJ6CQUW Dyo6AZ/2qMo8EKOif3jzuQsPgAxmDztdNt/O2+jWbKDqq8p0TJtacACPlDDrBnfanlWA Tp/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="g/kstoEF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l16si3870528edj.436.2020.05.21.11.15.37; Thu, 21 May 2020 11:16:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="g/kstoEF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729474AbgEUSLY (ORCPT + 99 others); Thu, 21 May 2020 14:11:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729086AbgEUSLW (ORCPT ); Thu, 21 May 2020 14:11:22 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBE52C061A0E for ; Thu, 21 May 2020 11:11:21 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id x18so2047617pll.6 for ; Thu, 21 May 2020 11:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QnbqNy9EV06MW5CdU8p7rG6H8xARB4MCiMEyZtqlPZY=; b=g/kstoEF7jBH50MDuXScB/Wy07PeKyWkq6ndhA3tjBPgWJ0fihAAPJUK0c+ShkSt/7 wNJiwUfFT3bKmn3+xX8i8/Aqrw/2AHceGXIuENBN9r3NnYy7oCRDmrpBRbLHh6v6yWO8 bVxXeURBoZhPWu0Orftx1teKFHlJomMID6Lbg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QnbqNy9EV06MW5CdU8p7rG6H8xARB4MCiMEyZtqlPZY=; b=pD3oSmh7lZsxtxzlKh4Aq3K+vI+1+CnfflJQFEO9Arujs7QwYCLh+DPGf0cMhSAreX /AgKxo2UCknpiaL7UcBDuex/tqR6ojEbyNNJvKlhSY0tdIoTSEGUh2CN1j0rN4CtN4OG i0lAvplamK+w1hglyGaccqQE00tLCqmzxi6womW0Var+6Tf2I+Pv7kIogZuXRvNtWJye Ir2QhFOyjVSKrmbX7Q8pt879BvnzC/WxWhhtk0iR00eEAvCTUtInl375PcHrzyugTyUA Mgx4NUKBDcIVFkg5IGpNUpTU2HrAjfISyRGoQAB/ln/KCiyGvgntBB51y+RRynthsANv +jpw== X-Gm-Message-State: AOAM532zs5viEFvF6WJO63tzWr/AQValrJxTyITXrGJ7/oS5+1nyOLk8 Pw286S71pdJ/BIKPY6vV39K9cA== X-Received: by 2002:a17:902:bf43:: with SMTP id u3mr10554884pls.240.1590084681259; Thu, 21 May 2020 11:11:21 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id v1sm4969026pjn.9.2020.05.21.11.11.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 May 2020 11:11:20 -0700 (PDT) Date: Thu, 21 May 2020 11:11:18 -0700 From: Kees Cook To: Kristen Carlson Accardi Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, "H. Peter Anvin" , Arnd Bergmann , arjan@linux.intel.com, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, rick.p.edgecombe@intel.com, Tony Luck , linux-arch@vger.kernel.org Subject: Re: [PATCH v2 5/9] x86: Make sure _etext includes function sections Message-ID: <202005211109.4EE4DCBA3@keescook> References: <20200521165641.15940-1-kristen@linux.intel.com> <20200521165641.15940-6-kristen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200521165641.15940-6-kristen@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 21, 2020 at 09:56:36AM -0700, Kristen Carlson Accardi wrote: > When using -ffunction-sections to place each function in > it's own text section so it can be randomized at load time, the > linker considers these .text.* sections "orphaned sections", and > will place them after the first similar section (.text). In order > to accurately represent the end of the text section and the > orphaned sections, _etext must be moved so that it is after both > .text and .text.* The text size must also be calculated to > include .text AND .text.* > > Signed-off-by: Kristen Carlson Accardi > Reviewed-by: Tony Luck > Tested-by: Tony Luck > --- > arch/x86/kernel/vmlinux.lds.S | 18 +++++++++++++++++- > include/asm-generic/vmlinux.lds.h | 2 +- > 2 files changed, 18 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S > index 1bf7e312361f..044f7528a2f0 100644 > --- a/arch/x86/kernel/vmlinux.lds.S > +++ b/arch/x86/kernel/vmlinux.lds.S > @@ -147,8 +147,24 @@ SECTIONS > #endif > } :text =0xcccc > > - /* End of text section, which should occupy whole number of pages */ > +#ifdef CONFIG_FG_KASLR > + /* > + * -ffunction-sections creates .text.* sections, which are considered > + * "orphan sections" and added after the first similar section (.text). > + * Adding this ALIGN statement causes the address of _etext > + * to be below that of all the .text.* orphaned sections > + */ > + . = ALIGN(PAGE_SIZE); > +#endif > _etext = .; > + > + /* > + * the size of the .text section is used to calculate the address > + * range for orc lookups. If we just use SIZEOF(.text), we will > + * miss all the .text.* sections. Calculate the size using _etext > + * and _stext and save the value for later. > + */ > + text_size = _etext - _stext; > . = ALIGN(PAGE_SIZE); I don't think there's any need for this #ifdef (nor the trailing ALIGN). I think leave the comment to explain why the ALIGN is being moved before the _etext. A repeated ALIGN won't move the position: --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -147,9 +147,11 @@ SECTIONS #endif } :text =0xcccc + . = ALIGN(PAGE_SIZE); /* End of text section, which should occupy whole number of * pages */ _etext = .; . = ALIGN(PAGE_SIZE); + _ktext = .; X86_ALIGN_RODATA_BEGIN RO_DATA(PAGE_SIZE) $ nm vmlinux | grep '_[ek]text' ffffffff81e02000 R _etext ffffffff81e02000 R _ktext -- Kees Cook