Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp253451rdh; Thu, 23 Nov 2023 03:10:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IGoWdsHd1xMOZ7SFlb35nFwSuRUxJmhfht34Q/eQ7NYyH44y++Q+qfyuw9uKVFfsbJRe6bw X-Received: by 2002:a05:6e02:2187:b0:35a:b6ce:cedf with SMTP id j7-20020a056e02218700b0035ab6cecedfmr6638201ila.13.1700737831458; Thu, 23 Nov 2023 03:10:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700737831; cv=none; d=google.com; s=arc-20160816; b=dzA8T6vRwnbtE6639RKRpYv6kuejCY0QqvRD9luruzbDqYmx0qIkl6zJTUM15NtVOi JyAlo2t0Fd0hctGYtIY5YkX8Ckd2Hx+To4GWengV1IyIoJcVQU57b4qVU8kA0Xn8KnsL a7UiSE2qhALqYROhEP9LIYIjDghVmjQCP7AfIc4txudlLFdRRl7UU1aa/OxCTJ9aDN7W nHjRUJF7RUFes1DhGP6/dSP4ophrZ3xWNarZVbOcYgmyY1UqWFzV3n6erlxuN57TAcvA XU1RpTiDSlRtQ85gdKVuFiXBsnDD+UITNkjNpl+mUrU8ZBumdyW5+WYLcvMaECBcguEm Gp6w== 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=YnPIxcNUwxZIAQpLJFkXUu+mZlTcP31SPZMOLg6sxfA=; fh=duBqHXjrH/RbMx4hI9WkyuRbHmbcUhMMVcwFXwS3LzI=; b=lwDi5Ty5J3KrbC1MgF3zT/EMJpm3Mamor7xp1sTkYWJb5kNIssHQaI7LUZwciLrhsp 9QA3TnhvsNkDpaQzbSjQIDuYCk+mgvu+w5x7yOt5z6Az/+hpEti/3HnffXXfdL8XKp96 Ulez/VjhaLTYv4+1x2FuE33wu0zcHzmMB9dnrUynDonxyAbUtcfediVxTdn0x7+YdiKZ jPEqncGZfyv95fEW3boaC2kOmOmN+yWDa/XfN4jF0OtjUxWge35SaCMUyUvaNJKV2sf/ o3XSZUgJsgBay6DqN5GHaGp4mtpcJsND2qMKrD4dRnUHUND/33fgcI3K7cFebXaENGnW 4qAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XvaXi9Ib; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id q11-20020a056a00084b00b006c3b92140ebsi1055416pfk.151.2023.11.23.03.10.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 03:10:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XvaXi9Ib; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 4719E80758C9; Thu, 23 Nov 2023 03:10:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344907AbjKWLKH (ORCPT + 99 others); Thu, 23 Nov 2023 06:10:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229664AbjKWLKF (ORCPT ); Thu, 23 Nov 2023 06:10:05 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D541E1BD for ; Thu, 23 Nov 2023 03:10:11 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6cbe6d514cdso412388b3a.1 for ; Thu, 23 Nov 2023 03:10:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700737811; x=1701342611; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=YnPIxcNUwxZIAQpLJFkXUu+mZlTcP31SPZMOLg6sxfA=; b=XvaXi9IbgzCwbkNHxK32qLLu1yh+86yqCevUQTcAHmOoBEPosSkTuUvSAHq6bXWXdj YzS8SSheIM6kxbqxVoAEBCmjbqHUjvV/RM0+7B26PGMWsY+LHAjbAtxBm3bOEtn/oJ5q lWiF3gdABI+rsI8sn7uwnHimU3OlRHn9xLmA5+L7oHdqjb35xBSZe0mzTEWQoCC7aEku cAIZOtvCllDBBDCmnNul8odLXDpak1n/8m6uOU0d/Bk4zW5msx2tPE6iMx7nddXI5YNx 2QnLHBuo3iKua8XVJQz2YFN6SjOnRpnGYVhKWFPDFBQY9i08XentFNN6aPEG0FT2JCKJ gnSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700737811; x=1701342611; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=YnPIxcNUwxZIAQpLJFkXUu+mZlTcP31SPZMOLg6sxfA=; b=NBidRQ9wK2nQqVJHeLIZ5mSuqS1zURdlBhr3j1UYuIGsuykFSSOlXkMDuAZI38GV1n 4qAYd/OA5q7vhAcZS1dsOaP+04SCc4kyPXCfkuLL12AUBDWdL5MbEjJtUbNZ4yNJXxv2 6tFE48HiGPRQlkFKIvp7nk+Rg91exIqgleVsiVkD9oUVg8jCJuQy6CiZGCBH/NmmuMxq yD+r0eXza6cYlXcSDczTQ5tb8MoG+y5Be0RyQATtlg+baDd1tw8cv9NRQzWsJ+cfCU5f 0y283HAq/X9ZMAzpEL0T4p8WLuFniO2LQuZxaSDCUG1BpgYtiKzGLjC8Qh1Pd7k+nxHo haig== X-Gm-Message-State: AOJu0Ywyw5tpUPb+n47mrLVH9+E27/0J21vGq15OCUvprdqhrOlynogs DPHHlAY39Me55PFe/kITPWY= X-Received: by 2002:a05:6a00:4ac7:b0:6c2:cf23:3e21 with SMTP id ds7-20020a056a004ac700b006c2cf233e21mr5764471pfb.17.1700737811113; Thu, 23 Nov 2023 03:10:11 -0800 (PST) Received: from dragonet (dragonet.kaist.ac.kr. [143.248.133.220]) by smtp.gmail.com with ESMTPSA id a9-20020aa78e89000000b0068fb8e18971sm979405pfr.130.2023.11.23.03.10.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 03:10:10 -0800 (PST) Date: Thu, 23 Nov 2023 20:08:29 +0900 From: "Dae R. Jeong" To: Greg Kroah-Hartman 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: References: <20231122122005.GA4661@libra05> <2023112257-polymer-banknote-4869@gregkh> <20231123074920.GA10480@libra05> <2023112331-wise-regain-72dc@gregkh> <2023112352-congested-puzzling-f76d@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2023112352-congested-puzzling-f76d@gregkh> X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 morse.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 (morse.vger.email [0.0.0.0]); Thu, 23 Nov 2023 03:10:22 -0800 (PST) On Thu, Nov 23, 2023 at 10:14:52AM +0000, Greg Kroah-Hartman wrote: > 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()? It is because close() needs to destory vmci_host_dev->context if it is created. > > (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? It is because vmci_host_dev->context is created by ioctl(IOCTL_VMCI_INIT_CONTEXT). So it is possible that vmci_host_dev is created but vmci_host_dev->context is *not* created. All other places should be careful of this. > > 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. I don't exactly get the point what you think the root issue is. We can have a system call sequence like this: fd = open("/dev/vmci") ioctl(fd, VMCI_VERSION2, user_version) ioctl(fd, INIT_CONTEXT) // this somewhat depends on ioctl(VMCI_VERSION2) as it runs `context->user_version = user_version` Between open() and ioctl(INIT_CONTEXT), we have vmci_host_dev initialized but vmci_host_dev->context is not initialized. We need to check whether vmci_host_dev->context is initialized in other places. And I still think store_release/load_acquire can be used to declare that context is created and check whether context is created or not. Please excuse me if I'm wrong... Best regards, Dae R. Jeong