From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:59279 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750977AbdCNWmb (ORCPT ); Tue, 14 Mar 2017 18:42:31 -0400 Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v2EMcZtw036569 for ; Tue, 14 Mar 2017 18:42:30 -0400 Received: from e28smtp03.in.ibm.com (e28smtp03.in.ibm.com [125.16.236.3]) by mx0a-001b2d01.pphosted.com with ESMTP id 296nfvjh9g-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 14 Mar 2017 18:42:29 -0400 Received: from localhost by e28smtp03.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 15 Mar 2017 04:12:27 +0530 Subject: Re: [tpmdd-devel] [PATCH] vTPM: Fix missing NULL check From: "Hon Ching(Vicky) Lo" To: Jason Gunthorpe Cc: tpmdd-devel@lists.sourceforge.net, Ashley Lai , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Peter Huewe In-Reply-To: <20170308205236.GA28643@obsidianresearch.com> References: <1488839535-11822-1-git-send-email-honclo@linux.vnet.ibm.com> <20170306231946.GA23953@obsidianresearch.com> <1488946363.15595.17.camel@vtpm2014.ibm.com> <20170308171729.GC16709@obsidianresearch.com> <1489004891.15595.37.camel@vtpm2014.ibm.com> <20170308205236.GA28643@obsidianresearch.com> Content-Type: text/plain; charset="UTF-8" Date: Tue, 14 Mar 2017 18:42:20 -0400 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Message-Id: <1489531340.7013.13.camel@vtpm2014.ibm.com> Sender: stable-owner@vger.kernel.org List-ID: On Wed, 2017-03-08 at 13:52 -0700, Jason Gunthorpe wrote: > On Wed, Mar 08, 2017 at 03:28:11PM -0500, Hon Ching(Vicky) Lo wrote: > > On Wed, 2017-03-08 at 10:17 -0700, Jason Gunthorpe wrote: > > > On Tue, Mar 07, 2017 at 11:12:43PM -0500, Hon Ching(Vicky) Lo wrote: > > > > On Mon, 2017-03-06 at 16:19 -0700, Jason Gunthorpe wrote: > > > > > > > > Also, how does locking work here? Does the vio core prevent > > > > > tpm_ibmvtpm_get_desired_dma and tpm_ibmvtpm_remove from running > > > > > concurrently? > > > > > > > > No, vio core doesn't prevent tpm_ibmvtpm_get_desired_dma and tpm_ibmvtpm_remove > > > > from running concurrently. > > > > > > > > vio_bus_probe calls vio_cmo_bus_probe which calls tpm_ibmvtpm_get_desired_dma. > > > > tpm_ibmvtpm_get_desired_dma is called before the code enters critical section. > > > > > > > > There is no locking mechanism around tpm_ibmvtpm_remove in vio_bus_remove. > > > > > > > > What's the concern here? > > > > > > tpm_ibmvtpm_remove makes the pointer that tpm_ibmvtpm_get_desired_dma > > > is accessing invalid, so some kind of locking is technically required > > > so that the two things do not create a use after free race: > > > > I don't think we need to worry about locking in this specific case. > > tpm_ibmvtpm_get_desired_dma was designed to return a default value > > in the case when the chip is not available. > > You have to worry about it to prevent a use after free race: > > CPU0 CPU1 > tpm_ibmvtpm_remove() tpm_ibmvtpm_get_desired_dma() > > chip = dev_get_drvdata(dev); > dev_set_drvdata(&vdev->dev, NULL); > if (chip) > ibmvtpm = dev_get_drvdata(&chip->dev); > kfree(ibmvtpm); > // *ibmvtpm is now a use-after-free > > Jason > I have dug further up along the call stack of tpm_ibmvtpm_get_desired_dma() and found that there is a locking mechanism in place at the bus probe level. 'probe' and 'remove' callbacks are both surrounded by mutex_lock and mutex_unlock on the device. The code is in the really_probe() and device_release_driver_internal() accordingly. Thanks for pointing this out! Vicky