From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D57ABC5479D for ; Mon, 9 Jan 2023 15:39:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=t4MIYItwQYMmLt/3P7yoGBd2peAwQWtI8wH9RbNpZ0I=; b=S+6JkRwN/2bPlI mZp9oNbfZ1yo/Pyn8tsJKeDUucP/rBS3sI+2uJjwY4z6XcEJqex8ChXUsbkkBQmgr7Ne2d7qqRAJI 3ogKndmq+kVrm5x/xf7MaoRsqNRG9Yl4PlIXLVCjkqkZvXxMj0A9bqqEj0l1HczNKHETFWOyO492E EJXDy6iDzwqmtpdYjywnWVI+EDT8bs2JSRsoipNWtKRRN/RehhVNA8oYA80cY9+rbUbkp8Yd2fJpS Zr2+E9BpgtylUxc9cm7zLTzqLjt8zDxIk0uql3iZufpx6/Y6f+fu83AG9KmP9oPZm+oMbhbR7YhwZ YKzxXvTBVg9blgEt6vTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pEuEG-002KAy-Hw; Mon, 09 Jan 2023 15:38:29 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pEuAZ-002I4C-RC for linux-arm-kernel@lists.infradead.org; Mon, 09 Jan 2023 15:34:41 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2C117B80D3D; Mon, 9 Jan 2023 15:34:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28AE4C433EF; Mon, 9 Jan 2023 15:34:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673278477; bh=WmKtUssUUKemnbA+tR6zwPKSfUAvCj2jLE4fMoIwAFM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jtouydVp5xhN24JtyRtIg8BJ5XW5fbu1rNn5cCswFTxTVMTG2xWEkOJrJTZ+AT6Rm +sU9pp0bsTa5/JdJoCZfvdAtapxceEuGG/DDqsGU6TtMUSZIrUGyq1u0aVok0q3+z6 nmxJOX8l937vLPxHn16O95d+NKBRVxNAqbYaFTzsHGljJ35jrYNJRP+w3thH1uFLrS XmYoZUc2rE3x2A2/FoXmoV80u1xXbnzn5Kd8CaAh1+xCoU3FYsmslWpcvbwRimrxII NUCiLo5kGMdpdUhS5N9Do6ZNo/LikrttAhV1p0XbBmun+6i4e06l2ygq78HmHuIzkE 2CNMECIQOUEvQ== Date: Mon, 9 Jan 2023 15:34:32 +0000 From: Will Deacon To: Ard Biesheuvel Cc: linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, catalin.marinas@arm.com, stable@vger.kernel.org, Peter Jones , Jarkko Sakkinen , Matthew Garrett , Nathan Chancellor Subject: Re: [PATCH] efi: tpm: Avoid READ_ONCE() for accessing the event log Message-ID: <20230109153431.GB7817@willie-the-truck> References: <20230109095948.2471205-1-ardb@kernel.org> <20230109151054.GA7817@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230109_073440_249966_56D47CBA X-CRM114-Status: GOOD ( 42.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Jan 09, 2023 at 04:20:34PM +0100, Ard Biesheuvel wrote: > On Mon, 9 Jan 2023 at 16:11, Will Deacon wrote: > > > > On Mon, Jan 09, 2023 at 10:59:48AM +0100, Ard Biesheuvel wrote: > > > Nathan reports that recent kernels built with LTO will crash when doing > > > EFI boot using Fedora's GRUB and SHIM. The culprit turns out to be a > > > misaligned load from the TPM event log, which is annotated with > > > READ_ONCE(), and under LTO, this gets translated into a LDAR instruction > > > which does not tolerate misaligned accesses. > > > > Interesting, that's a funny change in behaviour. READ_ONCE() of an unaligned > > address is pretty sketchy, but if this ends up tripping lots of folks up > > then I suppose we could use a plain load and a DMB LD as an alternative. > > It's likely to be more expensive in the LDAPR case, though. > > > > Yeah, I am not suggesting that we change READ_ONCE(), but this case > was definitely not taken into account at the time. Indeed, and it looks like the architecture added SCTLR_ELx.nAA to toggle this behaviour, although it was only added in 8.4 with FEAT_LSE2. > > > Interestingly, this does not happen when booting the same kernel > > > straight from the UEFI shell, and so the fact that the event log may > > > appear misaligned in memory may be caused by a bug in GRUB or SHIM. > > > > > > However, using READ_ONCE() to access firmware tables is slightly unusual > > > in any case, and here, we only need to ensure that 'event' is not > > > dereferenced again after it gets unmapped, so a compiler barrier should > > > be sufficient, and works around the reported issue. > > > > > > Cc: > > > Cc: Peter Jones > > > Cc: Jarkko Sakkinen > > > Cc: Matthew Garrett > > > Reported-by: Nathan Chancellor > > > Link: https://github.com/ClangBuiltLinux/linux/issues/1782 > > > Signed-off-by: Ard Biesheuvel > > > --- > > > include/linux/tpm_eventlog.h | 6 ++++-- > > > 1 file changed, 4 insertions(+), 2 deletions(-) > > > > > > diff --git a/include/linux/tpm_eventlog.h b/include/linux/tpm_eventlog.h > > > index 20c0ff54b7a0d313..0abcc85904cba874 100644 > > > --- a/include/linux/tpm_eventlog.h > > > +++ b/include/linux/tpm_eventlog.h > > > @@ -198,8 +198,10 @@ static __always_inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *ev > > > * The loop below will unmap these fields if the log is larger than > > > * one page, so save them here for reference: > > > */ > > > - count = READ_ONCE(event->count); > > > - event_type = READ_ONCE(event->event_type); > > > + count = event->count; > > > + event_type = event->event_type; > > > + > > > + barrier(); > > > > It would be handy to have a comment here, but when I started thinking about > > what that would say, it occurred to me that the unmap operation should > > already have a barrier inside it due to the TLB invalidation, so I'm not > > sure why this is needed at all. > > > > This is purely to prevent the compiler from accessing count or > event_type by reloading it from the event pointer, in case it runs out > of registers. But that reload would only be a problem if the event has been unmapped, no? Given that the unmapping code has a barrier() and the unmapped page is not explicitly referenced, then I don't see the issue. Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel