Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp613739pxa; Tue, 11 Aug 2020 10:38:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyf3P8x4xMIchX80xBxck9CIXBq0sGQEPePA/4JLYGPLFEckgLWnj4bv7Iy1ZlaRIlabhWj X-Received: by 2002:a17:906:7fc9:: with SMTP id r9mr26687436ejs.407.1597167486884; Tue, 11 Aug 2020 10:38:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597167486; cv=none; d=google.com; s=arc-20160816; b=LefVaeqxNKNBd8xupSsOkmBWI6RK0gTK2nwlP1UlKp415gP+Z+7Gr1o8wdznFjQ2Ou Y6/uO43L7IhnJzFeft0BNSKefIQUR2tKsTBjG4c6f6JwzWJbinqmKhsl2mhAfT5P2ryf uDOU7i6bIiVkYNeaC8CEBqsn2Lq+vVlpQn7Nbmepdismp3Xf0W67lAplK+b3VNfVZP6h StgFLaaVLHze3YtotKlWVWStkOCOq+hseGQ8thEVS2HiC49H1NC1N37yKJ73KWpt+/DB yRlTIoTzmOhENa6wGC45HjVr5IyIDbnmUGm7xzJ2dCvo6QaS3bx1Fq4kX+w57+RxezB3 amKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2HnMUolNEq5cXPKXr7ZP+lVltLaJ656X7idvYxtYJ7A=; b=RTV1Mq5oFJTivQMaN5oxEM9ZhP03907eqY9MPY/NT5jxr6k9W3P2/tZHMN+K//GPKB eFZXdk8tfWyc97cSsAk/SLhFprqT+J/XsXFW/ssu1IYQVmRZfbiXFioPgOBHxHUX2pm4 TjmZZIcf38DBjmzICgyZ2KMtDdmMQR2yDJpxxWCbbIacs5Gz/CfW2Jnku/MRveriCcFi PyPhRoFUiFHoscoKy0DN87EyrIXsUVgd/Sjmnwawnxul1ZJuLz0fov6az52+KSv4lwU9 H1BNgbpGgfdj3GD7b8inE2poV+D+brpQUEhNlVsFPCJez2Pe70pxNHqbAD5pKpiZnQoU FEhA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gu8si12714154ejb.317.2020.08.11.10.37.44; Tue, 11 Aug 2020 10:38:06 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729167AbgHKRg7 (ORCPT + 99 others); Tue, 11 Aug 2020 13:36:59 -0400 Received: from mail-qv1-f66.google.com ([209.85.219.66]:34621 "EHLO mail-qv1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729046AbgHKRg6 (ORCPT ); Tue, 11 Aug 2020 13:36:58 -0400 Received: by mail-qv1-f66.google.com with SMTP id t6so6366609qvw.1; Tue, 11 Aug 2020 10:36:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2HnMUolNEq5cXPKXr7ZP+lVltLaJ656X7idvYxtYJ7A=; b=Pl5B0myGCA0wsbETlDHMgxKZY1rdOqLnNI6Z+rZ6JwYT+IzTA5EUbTDw7nRIPE6Idd 6g+HSRPpqkp9xP36/s9m6IIGbLAA4tugxfXe1k+OFXGok5kKrDe4ub7n9uOEX8u6aYel Hwu87YfpzMWJ85KAFhJF+vhixmNdf2+W2LtSqHXI3jZVyVBnk7HN+PbjqTc9mR19ACMt FxUYD/n9ExGJfrSh3/WFxEzXDungU3Nt//mBmHiJZpgbcNIjV1CMZzeyTLKqpEynm9l5 sILdED892AK+DvMz3RQgiWU6mkYrjXv7mbw6Wmt3RyNU02Z7I3BpM3BD31b9/2lqGqzA +F5A== X-Gm-Message-State: AOAM5338qWjUIMIPw9EdbqKcjWNrFv4LaRgjVi++bxEknsX9XwdE/EGq EKXOjjNyxmab9ri3YfYtVKI= X-Received: by 2002:ad4:450e:: with SMTP id k14mr2315086qvu.211.1597167416620; Tue, 11 Aug 2020 10:36:56 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id q68sm17305993qke.123.2020.08.11.10.36.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 10:36:56 -0700 (PDT) From: Arvind Sankar To: Nick Desaulniers Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Fangrui Song , clang-built-linux , e5ten.arch@gmail.com, "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" , Masahiro Yamada , Ard Biesheuvel , Kees Cook , LKML , stable@vger.kernel.org Subject: [PATCH] x86/boot/compressed: Disable relocation relaxation for non-pie link Date: Tue, 11 Aug 2020 13:36:55 -0400 Message-Id: <20200811173655.1162093-1-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The x86-64 psABI [0] specifies special relocation types (R_X86_64_[REX_]GOTPCRELX) for indirection through the Global Offset Table, semantically equivalent to R_X86_64_GOTPCREL, which the linker can take advantage of for optimization (relaxation) at link time. This is supported by LLD and binutils versions 2.26 onwards. The compressed kernel is position-independent code, however, when using LLD or binutils versions before 2.27, it must be linked without the -pie option. In this case, the linker may optimize certain instructions into a non-position-independent form, by converting foo@GOTPCREL(%rip) to $foo. This potential issue has been present with LLD and binutils-2.26 for a long time, but it has never manifested itself before now: - LLD and binutils-2.26 only relax movq foo@GOTPCREL(%rip), %reg to leaq foo(%rip), %reg which is still position-independent, rather than mov $foo, %reg which is permitted by the psABI when -pie is not enabled. - gcc happens to only generate GOTPCREL relocations on mov instructions. - clang does generate GOTPCREL relocations on non-mov instructions, but when building the compressed kernel, it uses its integrated assembler (due to the redefinition of KBUILD_CFLAGS dropping -no-integrated-as), which has so far defaulted to not generating the GOTPCRELX relocations. Nick Desaulniers reports [1,2]: A recent change [3] to a default value of configuration variable (ENABLE_X86_RELAX_RELOCATIONS OFF -> ON) in LLVM now causes Clang's integrated assembler to emit R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX relocations. LLD will relax instructions with these relocations based on whether the image is being linked as position independent or not. When not, then LLD will relax these instructions to use absolute addressing mode (R_RELAX_GOT_PC_NOPIC). This causes kernels built with Clang and linked with LLD to fail to boot. Patch series [4] is a solution to allow the compressed kernel to be linked with -pie unconditionally, but even if merged is unlikely to be backported. As a simple solution that can be applied to stable as well, prevent the assembler from generating the relaxed relocation types using the -mrelax-relocations=no option. [0] https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/linker-optimization.tex#L65 [1] https://lore.kernel.org/lkml/20200807194100.3570838-1-ndesaulniers@google.com/ [2] https://github.com/ClangBuiltLinux/linux/issues/1121 [3] https://reviews.llvm.org/rGc41a18cf61790fc898dcda1055c3efbf442c14c0 [4] https://lore.kernel.org/lkml/20200731202738.2577854-1-nivedita@alum.mit.edu/ Signed-off-by: Arvind Sankar Reported-by: Nick Desaulniers Reviewed-by: Nick Desaulniers Tested-by: Nick Desaulniers Cc: stable@vger.kernel.org # 4.19.x --- arch/x86/boot/compressed/Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 3962f592633d..c5449bea58ec 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -62,6 +62,12 @@ KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \ endif LDFLAGS_vmlinux := -T +# Disable relocation relaxation if not linking as PIE +ifeq ($(filter -pie,$(KBUILD_LDFLAGS)),) +KBUILD_CFLAGS += $(call as-option, -Wa$(comma)-mrelax-relocations=no) +KBUILD_AFLAGS += $(call as-option, -Wa$(comma)-mrelax-relocations=no) +endif + hostprogs := mkpiggy HOST_EXTRACFLAGS += -I$(srctree)/tools/include -- 2.26.2