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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4669DC636CC for ; Wed, 8 Feb 2023 02:01:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229563AbjBHCBQ (ORCPT ); Tue, 7 Feb 2023 21:01:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjBHCBN (ORCPT ); Tue, 7 Feb 2023 21:01:13 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEEAB41B74; Tue, 7 Feb 2023 18:01:11 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 5931761460; Wed, 8 Feb 2023 02:01:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE63AC433EF; Wed, 8 Feb 2023 02:01:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675821670; bh=mgwYtaT9A8LtpqMbidmBxeGCLHmzFF3ZFWIdQRS2Nk8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BGqCotlvesXZ0LPeAGM0zmKUJsjU6gJOHm24l9+gpzlv2KLyiHmF49T7wutqVI2oP l6sLTB4WG7GQPYPU3s307E1MT7TwSfkIClr2mLPgF0UOQqrBOlytPmb/CwxO0O++Qm uBmQYWwrc6mUJZVNm1EPK937pywK8oq1MZB5Hk4SO1XUeFi4HWuLS5OvuO6e4NQLy/ +zGJckybNjqHsX2WWeirNEgEg5XXA9ZBwK4EPDWuDBB6dflgBtufkdfhjPZ8nSA1XP D8Z5XY2PBQmef0dfCvr8NZutvHIdmn7HTgposiMoMfE6JTujK+2XTNx/3q/x9+HX9O NofTWgiEBOaew== Date: Wed, 8 Feb 2023 04:01:05 +0200 From: Jarkko Sakkinen To: Jakob Koschel Cc: Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, Pietro Borrello , Cristiano Giuffrida , "Bos, H.J." Subject: Re: [PATCH] x86/sgx: Avoid using iterator after loop in sgx_mmu_notifier_release() Message-ID: References: <20230206-sgx-use-after-iter-v1-1-c09fb5300b5e@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230206-sgx-use-after-iter-v1-1-c09fb5300b5e@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org On Mon, Feb 06, 2023 at 11:39:02AM +0100, Jakob Koschel wrote: > If &encl_mm->encl->mm_list does not contain the searched 'encl_mm', > 'tmp' will not point to a valid sgx_encl_mm struct. Please explain. Perhaps, extend with something like ", and can be turned into transient gadget." > > Since the code within the guarded block is just called when the element > is found, it can simply be moved into the list iterator. > Within the list iterator 'tmp' is guaranteed to point to a valid > element. > > Signed-off-by: Jakob Koschel > --- > Linus proposed to avoid any use of the list iterator variable after the > loop, in the attempt to move the list iterator variable declaration into > the marcro to avoid any potential misuse after the loop. > Using it in a pointer comparision after the loop is undefined behavior > and should be omitted if possible [1]. > > Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] I would move all this to the commit message. It is useful information. > --- > arch/x86/kernel/cpu/sgx/encl.c | 10 ++++------ > 1 file changed, 4 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c > index 2a0e90fe2abc..db585b780141 100644 > --- a/arch/x86/kernel/cpu/sgx/encl.c > +++ b/arch/x86/kernel/cpu/sgx/encl.c > @@ -764,15 +764,13 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn, > list_for_each_entry(tmp, &encl_mm->encl->mm_list, list) { > if (tmp == encl_mm) { > list_del_rcu(&encl_mm->list); > - break; > + spin_unlock(&encl_mm->encl->mm_lock); > + synchronize_srcu(&encl_mm->encl->srcu); > + mmu_notifier_put(mn); > + return; > } > } > spin_unlock(&encl_mm->encl->mm_lock); > - > - if (tmp == encl_mm) { > - synchronize_srcu(&encl_mm->encl->srcu); > - mmu_notifier_put(mn); > - } > } > > static void sgx_mmu_notifier_free(struct mmu_notifier *mn) > > --- > base-commit: d2d11f342b179f1894a901f143ec7c008caba43e > change-id: 20230206-sgx-use-after-iter-f584c1d64c87 > > Best regards, > -- > Jakob Koschel > BR, Jarkko