Received: by 2002:a05:7412:8d09:b0:fa:4c10:6cad with SMTP id bj9csp459590rdb; Tue, 16 Jan 2024 05:59:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHHYU0e5kJJKCQcem5UN+YU202oCqtZ+sWMcPW0NitnMZ95rjeEe4fWxLda6Oz1w9PQlxLx X-Received: by 2002:a05:6214:626:b0:681:62c8:edd0 with SMTP id a6-20020a056214062600b0068162c8edd0mr2818148qvx.52.1705413546803; Tue, 16 Jan 2024 05:59:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705413546; cv=none; d=google.com; s=arc-20160816; b=prYUlZEWhIYCzCOi8pr45xkwSudcMetSOv1skI5lr9xH+KovTJ72FhYPo0YeBz3pZ/ ErWfARXADbUqfx8bkkisxZELgjW/Pj1CKrRnLOlbIg9cevHOcY/ylP+aXnNAy4QHtcdM MPb+5xukJCEamB3GIvxGrqnBI+5SjRchpbxvn2yAq77GNEkqjl/ZR8Ea2/wF011APZM+ ESsKWmFa/bezyTz0KiVMUQGLpeQZOKQb+El3FGmL3VPhTe+nTJZNehTpLagueC7QdI4W Fdlzju3UyLHvNXmHkM6ThVMrSGg01I5Jdsu5/Spe8bi4vx2TdaxEQ2MNyM6GIZ3Jo+XI MxSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=ziJCWr1stBawzylcMCAO4gfLdEekI8dgDB8JZQjy/mc=; fh=n20MorCtWsYdrFFZRh9knjHu6oORm29Ywf9a4+SBvUE=; b=dBT2rU7Fx27SWFXWyabuUQBg+4+cSJARIhyJm1EL7dRTZB6xRC62R65+jwnUBMwPFN kXapkN10AAGJxgNh/GH8jj7jFV4ksAZH89ES9ad6EGQruR1C6gJHvwto5BkyKcnniF2B wM4ixMgQugkzclY39QbnOpubAByBn3rj1cHK68MW8HTIUwzVmu2FMWZbQM4NNGJCS9Tg 5vq9ukUodHeyAaygt+Y0/M4UF9+FfUSIRkU/LJGRJTrcLkStJWMBd15SiteopMIkVp5j X2F5Ol0wAK5XIVm1a46sdOmS0kDS5HiC8NgsdS39IgO5MBwbcyfrDlpvrRsZiwHMSQAm suxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oAi4cj2x; spf=pass (google.com: domain of linux-kernel+bounces-27440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f3-20020a0ccc83000000b00680f70d3983si9828189qvl.117.2024.01.16.05.59.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 05:59:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-27440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oAi4cj2x; spf=pass (google.com: domain of linux-kernel+bounces-27440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8074B1C234E5 for ; Tue, 16 Jan 2024 13:59:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F23E1BDE1; Tue, 16 Jan 2024 13:58:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oAi4cj2x" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1DB01BDD3; Tue, 16 Jan 2024 13:58:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76BA5C433C7; Tue, 16 Jan 2024 13:58:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705413538; bh=LejeWozr8nvQaPAoZ2ulzd0nzo1lH5SPcl0UDydAhps=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=oAi4cj2xVyWm5GpKzblaEsXW8HwyYlErFZAXhFFpx4ZvAL88G08XD+oOo3UKb3XHL aAsmBufJbNbEer64s7D8XMAPh7MvOTkyZ8M0KXIbXSnKLaTC73zulI6QI7pZTbyhvS JZDkPINbcXsSSd14NOxJhblyNR+/l23urpk2wEXJ441TQfc7MUQn/CI50oM77skDHX T6t2MS8IvB5bsUyr8wT3BUnZWkMqzAr1Bd1IF9Y/RjUGvM7gLie9j6E17wdB1iVsCT DdRJfWefM4F7noNtZRr157nj9XeaPAHWgFy0SR5jqz6NTdx9allhUo7W0gvjmhHImw XuJ7Eva8jDahg== Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-50e80d14404so9396091e87.1; Tue, 16 Jan 2024 05:58:58 -0800 (PST) X-Gm-Message-State: AOJu0YzJXcBNmsSSjTWgeDGH0nbvbnq4+G/xKVRKf5LUZa0uM+Bazzf6 jDJ8cKjUFc6ceZny90UKMerBDaK0ldOUYq/mK5w= X-Received: by 2002:ac2:5b02:0:b0:50e:7d25:bd88 with SMTP id v2-20020ac25b02000000b0050e7d25bd88mr4193804lfn.18.1705413536593; Tue, 16 Jan 2024 05:58:56 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <56b52d4e-9dc0-400c-a141-7e70f5c72afa@siemens.com> <578aae7c-4069-4071-ba4b-cc86d3b516c1@siemens.com> <5c077064-bdab-4796-9ed6-8f884669b73f@siemens.com> In-Reply-To: From: Ard Biesheuvel Date: Tue, 16 Jan 2024 14:58:45 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] riscv/efistub: Ensure GP-relative addressing is not used To: Jan Kiszka Cc: Palmer Dabbelt , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" On Tue, 16 Jan 2024 at 14:56, Jan Kiszka wrote: > > On 16.01.24 14:47, Ard Biesheuvel wrote: > > On Tue, 16 Jan 2024 at 14:44, Jan Kiszka wrote: > >> > >> On 16.01.24 09:36, Ard Biesheuvel wrote: > >>> On Tue, 16 Jan 2024 at 06:21, Jan Kiszka wrote: > >>>> > >>>> On 15.01.24 18:34, Ard Biesheuvel wrote: > >>>>> On Sat, 13 Jan 2024 at 11:35, Jan Kiszka wrote: > >>>>>> > >>>>>> On 12.01.24 19:56, Palmer Dabbelt wrote: > >>>>>>> On Fri, 12 Jan 2024 10:51:16 PST (-0800), Ard Biesheuvel wrote: > >>>>>>>> Hi Jan, > >>>>>>>> > >>>>>>>> On Fri, 12 Jan 2024 at 19:37, Jan Kiszka wrote: > >>>>>>>>> > >>>>>>>>> From: Jan Kiszka > >>>>>>>>> > >>>>>>>>> The cflags for the RISC-V efistub were missing -mno-relax, thus were > >>>>>>>>> under the risk that the compiler could use GP-relative addressing. That > >>>>>>>>> happened for _edata with binutils-2.41 and kernel 6.1, causing the > >>>>>>>>> relocation to fail due to an invalid kernel_size in handle_kernel_image. > >>>>>>>>> It was not yet observed with newer versions, but that may just be luck. > >>>>>>>>> > >>>>>>>>> Signed-off-by: Jan Kiszka > >>>>>>>>> --- > >>>>>>>>> > >>>>>>>>> Something like this should go to stable as well, but we will need > >>>>>>>>> rebased patches. > >>>>>>>>> > >>>>>>>>> drivers/firmware/efi/libstub/Makefile | 2 +- > >>>>>>>>> 1 file changed, 1 insertion(+), 1 deletion(-) > >>>>>>>>> > >>>>>>>>> diff --git a/drivers/firmware/efi/libstub/Makefile > >>>>>>>>> b/drivers/firmware/efi/libstub/Makefile > >>>>>>>>> index 06964a3c130f..d561d7de46a9 100644 > >>>>>>>>> --- a/drivers/firmware/efi/libstub/Makefile > >>>>>>>>> +++ b/drivers/firmware/efi/libstub/Makefile > >>>>>>>>> @@ -28,7 +28,7 @@ cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN > >>>>>>>>> -DEFI_HAVE_STRNLEN \ > >>>>>>>>> -DEFI_HAVE_MEMCHR > >>>>>>>>> -DEFI_HAVE_STRRCHR \ > >>>>>>>>> -DEFI_HAVE_STRCMP -fno-builtin > >>>>>>>>> -fpic \ > >>>>>>>>> $(call > >>>>>>>>> cc-option,-mno-single-pic-base) > >>>>>>>>> -cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE > >>>>>>>>> +cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax > >>>>>>>> > >>>>>>>> Can we detect the presence of these references (via the relocation > >>>>>>>> type)? We already do something similar for ordinary absolute > >>>>>>>> references too. > >>>>>>> > >>>>>>> If there's no `__global_pointer$` symbol then the linker won't make > >>>>>>> GP-relative relaxations (because it doesn't know where GP is). We > >>>>>>> usually define that symbol in the linker script, but I'm not entierly > >>>>>>> sure how libstub gets its linker script... > >>>>>>> > >>>>>> > >>>>>> The stub seems to be linked together with the rest of the kernel, thus > >>>>>> the regular arch/riscv/kernel/vmlinux.lds.S is used. > >>>>>> > >>>>> > >>>>> Indeed - the EFI stub is part of the same executable as vmlinux, we > >>>>> just mangle the symbol names to ensure that only code that can be > >>>>> safely called from the EFI stub can be linked to it. > >>>>> > >>>>> If the effect of -mno-relax is to stop emitting R_RISCV_RELAX > >>>>> relocations, we should perhaps add those to the STUBCOPY_RELOC-y > >>>>> Makefile variable? (in the same file). BTW R_RISCV_HI20 doesn't seem > >>>>> like the right value there to begin with: the idea of that is to > >>>>> disallow ELF relocations that evaluate to expressions that can only be > >>>>> known at runtime (like absolute addresses for global pointer > >>>>> variables) > >>>> > >>>> How to do that best? Simply replace R_RISCV_HI20 with R_RISCV_RELAX? > >>>> > >>> > >>> We'll need to keep the HI20, in fact - I got confused between HI20 and > >>> PCREL_HI20, and the former is actually used for 32-bit absolute > >>> addresses in 32-bit code. > >>> > >>> This seems to do the trick: it disallows relaxation relocations and > >>> native word sizes absolute references. AFAICT, those are the only ones > >>> we should care about. > >>> > >>> STUBCOPY_RELOC-$(CONFIG_RISCV) := -E > >>> R_RISCV_HI20\|R_RISCV_$(BITS)\|R_RISCV_RELAX > >> > >> I would suggest to do that on top of this patch. Want me to write such a > >> patch, or will you? You can probably more fluently explain why > >> R_RISCV_32/64 is important, I would first have to understand what that > >> is exactly. :) > >> > > > > Sure, I can take care of that. > > > > Perfect. > > > For your patch, > > > > Reviewed-by: Ard Biesheuvel > > > > I'll queue this up as a EFI fix. > > Thanks. Will you take care of stable, or should I once the commit was > merged? > I'll add the cc:stable so we'll both get informed once the -stable maintainers [attempt to] queue this up.