Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp640960ybs; Sun, 24 May 2020 16:42:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxT8zREVIhRntzAHHidhPrwq+j5E8HFeBFlFOV0fnhsZZyiyPMqfvRK0ZX0GNjKv0z6vtn1 X-Received: by 2002:a17:906:34c7:: with SMTP id h7mr16434360ejb.300.1590363764419; Sun, 24 May 2020 16:42:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590363764; cv=none; d=google.com; s=arc-20160816; b=syMAPCUE8/59epDuqHbMVZPqk2ybguMbZB3WoWAPDxU6urBH3Plhn2WmJnXSGNvHh7 bcfFW4ej74WB6nJi5d52wqgzVRCjDgcFYO9y7mTSVPiMBNTqocXX4NEh41TJSx9KYm+H RR9kKUbAVNKWH8aKNsoWK5dGLTgAMWRvxwB1hynjhoc1W3AMJdWxCaMN46TACcCqYyD3 Otnn1CYGbg1wivTPTIb/op3J2vKnPUV4HZ/g6XU050yiUdsnnyM6S+E/tmUagL9bcrsb 6gTXcZtSbRtRVkw5Ped6TJCNnrx/CrmbokaxHYeV/e/p/dqaXQLu0g39UyusP+JpxrtO ENTQ== 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=/xSfk8UNIM4h1pIcoF/9EHCGXYkBOmPVW3Mq1rqJ1Bw=; b=pIC1hNWsNZQOLo/gB3V53y81XJwMjqPmxilh2Jz4ZDm71OpnrQrrGFBGfmqy9tQdYD Eha5LJYb8zonu+aEu+RYjihNt7g8B1rbDWEWCCUNNz8GXxZkwpmlFtrgvUl/CRQbXGvf Q0Zzcv4HJ/aoRv3nVxvgYmyDVd7sv4/EMSXqFOvsm6G8m5YKnC+H7N3gnQgJIKJCrDW3 uzd4DzeY1XKFqUVoDinDAeaFe5gz4POvq9KgpnrEhOiOAQl7k4+r4haGQ53eyp3qrO6C /ESN8ZcfxmXgmj/kfNXfyfSuwQSHloxKyQ/segp+GNjHYYjybZ9mtYDRQbBFakE+5PjH oU8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QeeaUABZ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be13si9108611edb.146.2020.05.24.16.42.22; Sun, 24 May 2020 16:42:44 -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=@google.com header.s=20161025 header.b=QeeaUABZ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388581AbgEXXgN (ORCPT + 99 others); Sun, 24 May 2020 19:36:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387914AbgEXXgN (ORCPT ); Sun, 24 May 2020 19:36:13 -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 12A90C061A0E for ; Sun, 24 May 2020 16:36:13 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id bg4so1520272plb.3 for ; Sun, 24 May 2020 16:36:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=/xSfk8UNIM4h1pIcoF/9EHCGXYkBOmPVW3Mq1rqJ1Bw=; b=QeeaUABZgKIL3kaH31FoNGWufVvWNec0TZ5UsuZOCEnRk8+eS9pjWgzJoSn40kG1/X jcFJsZBiLmNaB6+b303tdFBI8ErFJV3+8eH04UQ09wr6+O2uNq4lMQLFLCDvQlMR32+0 RMQRBXbHx2kLpct8xh7/2/J1ysJavNbZXn/J9gcbrShTm+w9Oduh2gZDQR650KYJe8uH PWLitPBXKBd7t0Rvp93AHI1f4k6937DS2jmw8p0LFJzTEmA3wKtitNkgzH9YVclSyrVx diTra4psaw+vbpNgtDYUNQWP8nsVCZN5eqCJMfTCKxZi4fPf16rfpA7EWaP/n8lw4zZG UVkA== 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=/xSfk8UNIM4h1pIcoF/9EHCGXYkBOmPVW3Mq1rqJ1Bw=; b=gh9h/TzeNWvw6zPZs8Fo0bzFoq5P2k/hVTjDKWzzChrknFeBC6G2+vP+LWa91sgT2w wDDmj6Mebn27TFOScPHZiEXJYLQinlWuPcQwY4pgTXYcqMMPXa74btx4rRs6nOYf9sC4 9FK6A0FiK5Sod6+Q8QsTvQBHeyG3OLf7HApecod+I1m+I1pO6XPtAh4hVpwf0k4wO7+u /UVh7mxt6jyn4Bk522CgH9iEF+l0BbwVmllbkIEQ7IITLwuBVej+pIte0t82Z7mfQEe5 82zGFNmN6dxxv/LXlQ8J7oayY1iQRO9iEaTn1Q4Wb4coOhTdepDuFuBWqR15BUk9IUCv 8tDw== X-Gm-Message-State: AOAM531A9bMR1+rhZSiDdvN7zhYOsONjiR7uUdEQ/Ky7UD+RlwmcIlw/ KFqTgcQlxGp1smR11yBX35Zopg== X-Received: by 2002:a17:90a:64c6:: with SMTP id i6mr16719577pjm.14.1590363372259; Sun, 24 May 2020 16:36:12 -0700 (PDT) Received: from google.com ([2620:15c:2ce:0:9efe:9f1:9267:2b27]) by smtp.gmail.com with ESMTPSA id 2sm11652343pfz.39.2020.05.24.16.36.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 16:36:11 -0700 (PDT) Date: Sun, 24 May 2020 16:36:07 -0700 From: Fangrui Song To: Arvind Sankar Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Nick Desaulniers , Dmitry Golovin , clang-built-linux@googlegroups.com, Ard Biesheuvel , Masahiro Yamada , Daniel Kiper , Kees Cook , linux-kernel@vger.kernel.org Subject: Re: [PATCH 4/4] x86/boot: Check that there are no runtime relocations Message-ID: <20200524233607.cihnsmxuaqoy7xi4@google.com> References: <20200524212816.243139-5-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200524212816.243139-5-nivedita@alum.mit.edu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-05-24, Arvind Sankar wrote: >Add a linker script check that there are no runtime relocations, and >remove the old one that tries to check via looking for specially-named >sections in the object files. > >Drop the tests for -fPIE compiler option and -pie linker option, as they >are available in all supported gcc and binutils versions (as well as >clang and lld). > >Signed-off-by: Arvind Sankar >--- > arch/x86/boot/compressed/Makefile | 28 +++----------------------- > arch/x86/boot/compressed/vmlinux.lds.S | 11 ++++++++++ > 2 files changed, 14 insertions(+), 25 deletions(-) > >diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile >index d3e882e855ee..679a2b383bfe 100644 >--- a/arch/x86/boot/compressed/Makefile >+++ b/arch/x86/boot/compressed/Makefile >@@ -27,7 +27,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \ > vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4 > > KBUILD_CFLAGS := -m$(BITS) -O2 >-KBUILD_CFLAGS += -fno-strict-aliasing $(call cc-option, -fPIE, -fPIC) >+KBUILD_CFLAGS += -fno-strict-aliasing -fPIE > KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING > cflags-$(CONFIG_X86_32) := -march=i386 > cflags-$(CONFIG_X86_64) := -mcmodel=small >@@ -49,7 +49,7 @@ UBSAN_SANITIZE :=n > KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE) > # Compressed kernel should be built as PIE since it may be loaded at any > # address by the bootloader. >-KBUILD_LDFLAGS += $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker) >+KBUILD_LDFLAGS += -pie $(call ld-option, --no-dynamic-linker) > LDFLAGS_vmlinux := -T > > hostprogs := mkpiggy >@@ -84,30 +84,8 @@ vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o > vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a > vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o > >-# The compressed kernel is built with -fPIC/-fPIE so that a boot loader >-# can place it anywhere in memory and it will still run. However, since >-# it is executed as-is without any ELF relocation processing performed >-# (and has already had all relocation sections stripped from the binary), >-# none of the code can use data relocations (e.g. static assignments of >-# pointer values), since they will be meaningless at runtime. This check >-# will refuse to link the vmlinux if any of these relocations are found. >-quiet_cmd_check_data_rel = DATAREL $@ >-define cmd_check_data_rel >- for obj in $(filter %.o,$^); do \ >- $(READELF) -S $$obj | grep -qF .rel.local && { \ >- echo "error: $$obj has data relocations!" >&2; \ >- exit 1; \ >- } || true; \ >- done >-endef >- >-# We need to run two commands under "if_changed", so merge them into a >-# single invocation. >-quiet_cmd_check-and-link-vmlinux = LD $@ >- cmd_check-and-link-vmlinux = $(cmd_check_data_rel); $(cmd_ld) >- > $(obj)/vmlinux: $(vmlinux-objs-y) FORCE >- $(call if_changed,check-and-link-vmlinux) >+ $(call if_changed,ld) > > OBJCOPYFLAGS_vmlinux.bin := -R .comment -S > $(obj)/vmlinux.bin: vmlinux FORCE >diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S >index d826ab38a8f9..0ac14feacb24 100644 >--- a/arch/x86/boot/compressed/vmlinux.lds.S >+++ b/arch/x86/boot/compressed/vmlinux.lds.S >@@ -11,9 +11,15 @@ OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT) > #ifdef CONFIG_X86_64 > OUTPUT_ARCH(i386:x86-64) > ENTRY(startup_64) >+ >+#define REL .rela >+ > #else > OUTPUT_ARCH(i386) > ENTRY(startup_32) >+ >+#define REL .rel >+ > #endif > > SECTIONS >@@ -42,6 +48,9 @@ SECTIONS > *(.rodata.*) > _erodata = . ; > } >+ REL.dyn : { >+ *(REL.*) >+ } > .got : { > *(.got) > } >@@ -83,3 +92,5 @@ ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18, "Unexpected GOT/PLT en > #else > ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0xc, "Unexpected GOT/PLT entries detected!") > #endif >+ >+ASSERT(SIZEOF(REL.dyn) == 0, "Unexpected runtime relocations detected!") >-- >2.26.2 > `grep -qF .rel.local` from 98f78525371b55ccd1c480207ce10296c72fa340 may be incorrect.. None of these synthesized dynamic relocation sections is called *.rel.local* ... (it probably wanted to name .rel.data.rel.ro or .rel.data) Reviewed-by: Fangrui Song