Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp228687rdh; Thu, 23 Nov 2023 02:15:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGo/IztkKLfS94aytDtDxUvKLWwfW2B/bcOHequQU93KVrDKrLGIcOvYlEtudK8SSoP9Xnd X-Received: by 2002:a17:903:2448:b0:1ca:29f:4b06 with SMTP id l8-20020a170903244800b001ca029f4b06mr5718164pls.2.1700734516628; Thu, 23 Nov 2023 02:15:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700734516; cv=none; d=google.com; s=arc-20160816; b=D3lImHpiS/NcpWOm768kqlG5xV7gl+T8lb3C5fEVlZXL3RaiubDcxqfsw9PwO7YvBU eZWpwZO34+VFHjkAIgP+OGYcxILgB7yUQ0LJQSzbz+P/2Qrgj1hfpsYnN6Xk3xyJqIg8 Aq51RpCd+ofvZK9V8f+vuDp9eluddcqOSw+ch1QKmntfRl79qV4d8MCkykn5sj/kQC0E 3GBZ23GOD1E8k5iA36WIv5hcMsshciTD5BZpAxYRuhHwFnegnZjEcyHS9VhhQiEAgh4v 3hPVKEdulMAcOdQ7et3xKVOAKzHYiJOco0mtKlCKdpgvvvkUyacxrFYiFsJiotiTmhiM B2DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MQREW3baGndx1wdkULhwnnvD72bo94XUdZuHEKqF35U=; fh=owMMBtWKJWYt0u/4B8yYJeVKTMACwxCiOTIkcMo23Kc=; b=Ld7K4A3LzvREjrHu3PKUw+cPPvNJMWHXJLbvXQgiCtzmn3sOpYAMJsAsbMdFw0y/qa NQMyXnWqfxhaZgYHzLdgMubQ1jULL7tMeJQYOEa3qHGMVoSPIm2mm7BqLCl+oL22ep1p GtIg+z2qHlmIQGK0HwVQPzaDbE3A4mNNc4U5Np351e/bKXb0bcuSyBwvATbPzI2CoQPZ EUoz32u7L6mpLMnJKbY8oCGn8MWzVT82xqRybSiU9rymlu8ECsQ0MoW4TZk4wse/F6MO YgAZNWnIUfnXXSmUHLqu2ynuF5rJ8lEC9X6pqbdxICM2G89oLIXKKd/lNWl4nVUzkRns brSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ncz0K9C0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id e6-20020a170902d38600b001cdf0388455si854476pld.111.2023.11.23.02.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 02:15:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ncz0K9C0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 551428085F8C; Thu, 23 Nov 2023 02:15:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234607AbjKWKOv (ORCPT + 99 others); Thu, 23 Nov 2023 05:14:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbjKWKOu (ORCPT ); Thu, 23 Nov 2023 05:14:50 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ABC39F for ; Thu, 23 Nov 2023 02:14:56 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77CB8C433C7; Thu, 23 Nov 2023 10:14:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1700734496; bh=olc2VOouxGYvM14Tj0AO4e9V39Dc2KatskZsfmaluQs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Ncz0K9C0NVYZKPiSoS2Cb1mcs27qWL9MTuKhZ5hVaCnrtqPcVJuyqhtail7oRAK+J 69ys7CjhlQh3ADDDZMlFn/wt0TSFNbYY44Ark43nOJhF3rWaVS6gm1ALQNO3ACPnUv wG5qeGRiIOzh2aTGIy84SQCfo6W+j1wEEYxzf7vo= Date: Thu, 23 Nov 2023 10:14:52 +0000 From: Greg Kroah-Hartman To: "Dae R. Jeong" Cc: Yewon Choi , Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers , Arnd Bergmann , linux-kernel@vger.kernel.org Subject: Re: [PATCH] vmci_host: use smp_load_acquire/smp_store_release when accessing vmci_host_dev->ct_type Message-ID: <2023112352-congested-puzzling-f76d@gregkh> References: <20231122122005.GA4661@libra05> <2023112257-polymer-banknote-4869@gregkh> <20231123074920.GA10480@libra05> <2023112331-wise-regain-72dc@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 23 Nov 2023 02:15:09 -0800 (PST) On Thu, Nov 23, 2023 at 07:06:52PM +0900, Dae R. Jeong wrote: > On Thu, Nov 23, 2023 at 08:44:46AM +0000, Greg Kroah-Hartman wrote: > > On Thu, Nov 23, 2023 at 04:49:22PM +0900, Yewon Choi wrote: > > > On Wed, Nov 22, 2023 at 02:34:55PM +0000, Greg Kroah-Hartman wrote: > > > > On Wed, Nov 22, 2023 at 09:20:08PM +0900, Yewon Choi wrote: > > > > > In vmci_host.c, missing memory barrier between vmci_host_dev->ct_type > > > > > and vmci_host_dev->context may cause uninitialized data access. > > > > > > > > > > One of possible execution flows is as follows: > > > > > > > > > > CPU 1 (vmci_host_do_init_context) > > > > > ===== > > > > > vmci_host_dev->context = vmci_ctx_create(...) // 1 > > > > > vmci_host_dev->ct_type = VMCIOBJ_CONTEXT; // 2 > > > > > > > > > > CPU 2 (vmci_host_poll) > > > > > ===== > > > > > if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) { // 3 > > > > > context = vmci_host_dev->context; // 4 > > > > > poll_wait(..., &context->host_context.wait_queue, ...); > > > > > > > > > > While ct_type serves as a flag indicating that context is initialized, > > > > > there is no memory barrier which prevents reordering between > > > > > 1,2 and 3, 4. So it is possible that 4 reads uninitialized > > > > > vmci_host_dev->context. > > > > > In this case, the null dereference occurs in poll_wait(). > > > > > > > > > > In order to prevent this kind of reordering, we change plain accesses > > > > > to ct_type into smp_load_acquire() and smp_store_release(). > > > > > > > > > > Signed-off-by: Yewon Choi > > > > > --- > > > > > drivers/misc/vmw_vmci/vmci_host.c | 40 ++++++++++++++++++------------- > > > > > 1 file changed, 23 insertions(+), 17 deletions(-) > > > > > > > > > > diff --git a/drivers/misc/vmw_vmci/vmci_host.c b/drivers/misc/vmw_vmci/vmci_host.c > > > > > index abe79f6fd2a7..e83b6e0fe55b 100644 > > > > > --- a/drivers/misc/vmw_vmci/vmci_host.c > > > > > +++ b/drivers/misc/vmw_vmci/vmci_host.c > > > > > @@ -139,7 +139,7 @@ static int vmci_host_close(struct inode *inode, struct file *filp) > > > > > { > > > > > struct vmci_host_dev *vmci_host_dev = filp->private_data; > > > > > > > > > > - if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) { > > > > > + if (smp_load_acquire(&vmci_host_dev->ct_type) == VMCIOBJ_CONTEXT) { > > > > > > > > This is getting tricky, why not use a normal lock to ensure that all is > > > > safe? close isn't on a "fast path", so this shouldn't be a speed issue, > > > > right? > > > > > > > > > > I think using locks can be considered orthogonal to correcting memory ordering. > > > > But they ensure proper memory ordering. > > Yes, using a lock obviously ensures memory ordering. > > > > If the lock is needed, we will need to add locks in all of them. I cannot be > > > sure which is better. Besides that, it seems to be a separate issue. > > > > Nope, I think it's the same issue :) > > > > > On the other hand, the current implementation doesn't guarantee memory ordering > > > which leads to wrong behavior. > > > This patch fixes this issue by adding primitives. > > > > But it's still wrong, again, what keeps the value from changing right > > after checking it? > > > > thanks, > > > > greg k-h > > It seems that VMCI assumes that vmci_host_dev->context is not NULL if > vmci_host_dev->ct_type == VMCIOBJ_CONTEXT (because all readers of > vmci_host_dev->context check whether vmci_host_dev->ct_type is > VMCIOBJ_CONTEXT or not, and access vmci_host_dev->context without > checking whether is it NULL or not). So I think this patch clarifies > this assumption. > > As you said, we need to ensure that vmci_host_dev->context is not > changed after checking vmci_host_dev->ct_type. But > (1) the only place that changes vmci_host_dev->context is > vmci_host_close() and Then why is it even checked in close()? > (2) (I think) vmci_host_close() do not concurrently run with readers > of vmci_host_dev->context. IIUC, all readers of vmci_host_dev->context > are system calls (eg, ioctl handlers or the poll handler). So I think > the ref count of the file saves us here. (Otherwise, Syzkaller will > tell us the truth maybe?) Ok, then why is this needed to be checked then at all? > At least, this patch introduces no change of the logic but the > guarantees of the memory ordering, so I think this patch is safe? I think the logic is incorrect, don't try to paper over it thinking that the issue to be solved is "memory ordering" please. Solve the root issue here. thanks, greg k-h