Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752267AbdCNWmd (ORCPT ); Tue, 14 Mar 2017 18:42:33 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:39179 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751143AbdCNWmb (ORCPT ); Tue, 14 Mar 2017 18:42:31 -0400 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 X-Mailer: Evolution 2.32.3 (2.32.3-34.el6) Content-Transfer-Encoding: 7bit X-TM-AS-MML: disable x-cbid: 17031422-0008-0000-0000-0000053F1178 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17031422-0009-0000-0000-0000134A11C5 Message-Id: <1489531340.7013.13.camel@vtpm2014.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-03-14_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=2 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703140174 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2259 Lines: 55 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