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: 9589003 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 B91B86051E for ; Thu, 23 Feb 2017 21:20:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A4E7F28922 for ; Thu, 23 Feb 2017 21:20:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 977DC28925; Thu, 23 Feb 2017 21:20:19 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 093C428922 for ; Thu, 23 Feb 2017 21:20:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751201AbdBWVUS (ORCPT ); Thu, 23 Feb 2017 16:20:18 -0500 Received: from quartz.orcorp.ca ([184.70.90.242]:57650 "EHLO quartz.orcorp.ca" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751199AbdBWVUR (ORCPT ); Thu, 23 Feb 2017 16:20:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=obsidianresearch.com; s=rsa1; h=Content-Type:MIME-Version:Message-ID:Subject:Cc:To:From:Date; bh=klCGKcSAWd3u5RONHlMI5n226ZpT2/HcWRsFulWfF1U=; b=hCZqB88hfykPoUr87NnPa/ddi0LViPo/1H2khl8t/o+B0CdRMAxQ7O9lMOuOUBr7AM4w+ZRQr895agvZDzoE9sY/1lDE2tkFtWBonBMAda69Zq+n9mkH7DM+CzFUbSDVzN62hNxNR/MKopqe2yKxjJyMEZ6MPvqyNSLGbDf+FIM=; 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 Cc: tpmdd-devel@lists.sourceforge.net, linux-security-module@vger.kernel.org Subject: [PATCH] tpm: Use the right clean up after cdev_add completes 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 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: 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);