From patchwork Thu Feb 23 21:19:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 9589001 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 00BF96051E for ; Thu, 23 Feb 2017 21:19:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E05C52891C for ; Thu, 23 Feb 2017 21:19:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D4BFC28922; Thu, 23 Feb 2017 21:19:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4D2802891C for ; Thu, 23 Feb 2017 21:19:28 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ch0nM-0003dL-W0; Thu, 23 Feb 2017 21:19:24 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ch0nL-0003dG-SS for tpmdd-devel@lists.sourceforge.net; Thu, 23 Feb 2017 21:19:24 +0000 Received-SPF: pass (sog-mx-1.v43.ch3.sourceforge.com: domain of obsidianresearch.com designates 184.70.90.242 as permitted sender) client-ip=184.70.90.242; envelope-from=jgunthorpe@obsidianresearch.com; helo=quartz.orcorp.ca; Received: from quartz.orcorp.ca ([184.70.90.242]) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1ch0nJ-0003By-EY for tpmdd-devel@lists.sourceforge.net; Thu, 23 Feb 2017 21:19:23 +0000 Received: from [10.0.0.156] (helo=jggl.edm.orcorp.ca) by quartz.orcorp.ca with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1ch0nC-0007LC-Js; Thu, 23 Feb 2017 14:19:14 -0700 Received: from jgg by jggl.edm.orcorp.ca with local (Exim 4.86_2) (envelope-from ) id 1ch0nC-0003QJ-Dg; Thu, 23 Feb 2017 14:19:14 -0700 Date: Thu, 23 Feb 2017 14:19:14 -0700 From: Jason Gunthorpe To: Jarkko Sakkinen Message-ID: <20170223211914.GA12752@obsidianresearch.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Broken-Reverse-DNS: no host name found for IP address 10.0.0.156 X-Headers-End: 1ch0nJ-0003By-EY Cc: linux-security-module@vger.kernel.org, tpmdd-devel@lists.sourceforge.net Subject: [tpmdd-devel] [PATCH] tpm: Use the right clean up after cdev_add completes X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: tpmdd-devel-bounces@lists.sourceforge.net X-Virus-Scanned: ClamAV using ClamSMTP Once cdev_add is done the device node is visible to user space and could have been opened. Thus we have to go through the locking process in tpm_del_char_device if device_add fails. Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()") Signed-off-by: Jason Gunthorpe Reviewed-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-chip.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) static function moved to avoid a prototype This was noticed while reviewing the cdev patchset from Logan diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index a77262d31911a1..c82acf0a6e7353 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -226,6 +226,26 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev, } EXPORT_SYMBOL_GPL(tpmm_chip_alloc); +static void tpm_del_char_device(struct tpm_chip *chip, bool with_device) +{ + cdev_del(&chip->cdev); + if (with_device) { + device_del(&chip->dev); + + /* Make the chip unavailable. */ + mutex_lock(&idr_lock); + idr_replace(&dev_nums_idr, NULL, chip->dev_num); + mutex_unlock(&idr_lock); + } + + /* Make the driver uncallable. */ + down_write(&chip->ops_sem); + if (chip->flags & TPM_CHIP_FLAG_TPM2) + tpm2_shutdown(chip, TPM2_SU_CLEAR); + chip->ops = NULL; + up_write(&chip->ops_sem); +} + static int tpm_add_char_device(struct tpm_chip *chip) { int rc; @@ -246,8 +266,7 @@ static int tpm_add_char_device(struct tpm_chip *chip) "unable to device_register() %s, major %d, minor %d, err=%d\n", dev_name(&chip->dev), MAJOR(chip->dev.devt), MINOR(chip->dev.devt), rc); - - cdev_del(&chip->cdev); + tpm_del_char_device(chip, false); return rc; } @@ -259,24 +278,6 @@ static int tpm_add_char_device(struct tpm_chip *chip) return rc; } -static void tpm_del_char_device(struct tpm_chip *chip) -{ - cdev_del(&chip->cdev); - device_del(&chip->dev); - - /* Make the chip unavailable. */ - mutex_lock(&idr_lock); - idr_replace(&dev_nums_idr, NULL, chip->dev_num); - mutex_unlock(&idr_lock); - - /* Make the driver uncallable. */ - down_write(&chip->ops_sem); - if (chip->flags & TPM_CHIP_FLAG_TPM2) - tpm2_shutdown(chip, TPM2_SU_CLEAR); - chip->ops = NULL; - up_write(&chip->ops_sem); -} - static void tpm_del_legacy_sysfs(struct tpm_chip *chip) { struct attribute **i; @@ -384,6 +385,6 @@ void tpm_chip_unregister(struct tpm_chip *chip) { tpm_del_legacy_sysfs(chip); tpm_bios_log_teardown(chip); - tpm_del_char_device(chip); + tpm_del_char_device(chip, true); } EXPORT_SYMBOL_GPL(tpm_chip_unregister);