From patchwork Tue Feb 21 19:09:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 9585339 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 A24CB6042F for ; Tue, 21 Feb 2017 19:09:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9A26D28607 for ; Tue, 21 Feb 2017 19:09:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C1E028611; Tue, 21 Feb 2017 19:09:24 +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=-1.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=no version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2A14228607 for ; Tue, 21 Feb 2017 19:09:23 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 9B56582224; Tue, 21 Feb 2017 11:09:23 -0800 (PST) X-Original-To: linux-nvdimm@ml01.01.org Delivered-To: linux-nvdimm@ml01.01.org Received: from quartz.orcorp.ca (quartz.orcorp.ca [184.70.90.242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0495C82223 for ; Tue, 21 Feb 2017 11:09:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=obsidianresearch.com; s=rsa1; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=x4Uqx/0GzOyPhlvcwE4S7WQmi4qyRPm1tbx/8/q/jVQ=; b=1h2YVif1m8wFeM5/n73ilVqMl8Ok5t4F7OmqZc8ukFRQaTTmAxNwaiMEQgQVBlEstD9VuVlyZL5hfzvqaVI8lLV6MufQskHNUmqRnYVKTSSA88a1TeJJ2huSHy0pa1OnYx8E/YudNZt94w80YOujo5OJTZXBXr3bT2W5xg0KsqE=; 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 1cgFoA-0007th-1A; Tue, 21 Feb 2017 12:09:06 -0700 Received: from jgg by jggl.edm.orcorp.ca with local (Exim 4.86_2) (envelope-from ) id 1cgFo9-0000yh-Tb; Tue, 21 Feb 2017 12:09:05 -0700 Date: Tue, 21 Feb 2017 12:09:05 -0700 From: Jason Gunthorpe To: Logan Gunthorpe Subject: Re: [PATCH 07/14] infiniband: utilize new device_add_cdev helper function Message-ID: <20170221190905.GD13138@obsidianresearch.com> References: <1487653253-11497-1-git-send-email-logang@deltatee.com> <1487653253-11497-8-git-send-email-logang@deltatee.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1487653253-11497-8-git-send-email-logang@deltatee.com> 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-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alessandro Zummo , Jan Kara , linux-iio@vger.kernel.org, linux-pci@vger.kernel.org, Linus Walleij , Jarkko Sakkinen , Alexandre Bounine , Alexandre Belloni , Peter Meerwald-Stadler , linux-scsi@vger.kernel.org, Sean Hefty , Parav Pandit , Peter Huewe , Alexandre Courbot , Lars-Peter Clausen , "James E.J. Bottomley" , rtc-linux@googlegroups.com, Leon Romanovsky , linux-rdma@vger.kernel.org, Richard Weinberger , Marek Vasut , Doug Ledford , tpmdd-devel@lists.sourceforge.net, Hans Verkuil , Matt Porter , Hal Rosenstock , linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, Boaz Harrosh , Arnd Bergmann , Boris Brezillon , Sajjan Vikas C , linux-input@vger.kernel.org, Marcel Selhorst , Vladimir Zapolskiy , Joe Perches , Cyrille Pitchen , Benny Halevy , Alexander Viro , Bjorn Helgaas , Mauro Carvalho Chehab , Dmitry Vyukov , linux-nvdimm@ml01.01.org, Haggai Eran , Lorenzo Stoakes , "Martin K. Petersen" , Artem Bityutskiy , Greg Kroah-Hartman , linux-gpio@vger.kernel.org, Dmitry Torokhov , linux-kernel@vger.kernel.org, Stephen Bates , Hartmut Knaack , Olof Johansson , linux-mtd@lists.infradead.org, Andrew Morton , Brian Norris , David Woodhouse , Jonathan Cameron , osd-dev@open-osd.org Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP On Mon, Feb 20, 2017 at 10:00:46PM -0700, Logan Gunthorpe wrote: > This patch updates core/ucm.c which didn't originally use the > cdev.kobj.parent with it's parent device. I did not look heavily into > whether this was a bug or not, but it seems likely to me there would > be a use before free. Hum, is probably safe - ib_ucm_remove_one can only happen on module unload and the cdev holds the module lock while open. Even so device_add_cdev should be used anyhow. > I also took a look at core/uverbs_main.c, core/user_mad.c and > hw/hfi1/device.c which utilize cdev.kobj.parent but because the > infiniband core seems to use kobjs internally instead of struct devices > they could not be converted to use the new helper API and still > directly manipulate the internals of the kobj. Yes, and hfi1 had the same use-afte-free bug when it was first submitted as well. IHMO cdev should have a helper entry for this style of use case as well. > diff --git a/drivers/infiniband/core/ucm.c b/drivers/infiniband/core/ucm.c > index e0a995b..38ea316 100644 > +++ b/drivers/infiniband/core/ucm.c > @@ -1283,18 +1283,20 @@ static void ib_ucm_add_one(struct ib_device *device) > set_bit(devnum, dev_map); > } > > + device_initialize(&ucm_dev->dev); Ah, this needs more help. Once device_initialize is called put_device should be the error-unwind, can you use something more like this? From ac7a35ea98066c9a9e3f3e54557c0ccda44b0ffc Mon Sep 17 00:00:00 2001 From: Jason Gunthorpe Date: Tue, 21 Feb 2017 12:07:55 -0700 Subject: [PATCH] infiniband: utilize new device_add_cdev helper The use after free is not triggerable here because the cdev holds the module lock and the only device_unregister is only triggered by module ouload, however make the change for consistency. To make this work the cdev_del needs to move out of the struct device release function. Signed-off-by: Jason Gunthorpe --- drivers/infiniband/core/ucm.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/drivers/infiniband/core/ucm.c b/drivers/infiniband/core/ucm.c index 7713ef089c3ccc..acda8d941381f3 100644 --- a/drivers/infiniband/core/ucm.c +++ b/drivers/infiniband/core/ucm.c @@ -1202,12 +1202,16 @@ static void ib_ucm_release_dev(struct device *dev) struct ib_ucm_device *ucm_dev; ucm_dev = container_of(dev, struct ib_ucm_device, dev); + kfree(ucm_dev); +} + +static void ib_ucm_cdev_del(struct ib_ucm_device *ucm_dev) +{ cdev_del(&ucm_dev->cdev); if (ucm_dev->devnum < IB_UCM_MAX_DEVICES) clear_bit(ucm_dev->devnum, dev_map); else clear_bit(ucm_dev->devnum - IB_UCM_MAX_DEVICES, overflow_map); - kfree(ucm_dev); } static const struct file_operations ucm_fops = { @@ -1263,6 +1267,7 @@ static void ib_ucm_add_one(struct ib_device *device) if (!ucm_dev) return; + device_initialize(&ucm_dev->dev); ucm_dev->ib_dev = device; devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES); @@ -1280,18 +1285,19 @@ static void ib_ucm_add_one(struct ib_device *device) set_bit(devnum, dev_map); } + ucm_dev->dev.devt = base; + ucm_dev->dev.release = ib_ucm_release_dev; + cdev_init(&ucm_dev->cdev, &ucm_fops); ucm_dev->cdev.owner = THIS_MODULE; kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum); - if (cdev_add(&ucm_dev->cdev, base, 1)) + if (device_add_cdev(&ucm_dev->dev, &ucm_dev->cdev)) goto err; ucm_dev->dev.class = &cm_class; ucm_dev->dev.parent = device->dma_device; - ucm_dev->dev.devt = ucm_dev->cdev.dev; - ucm_dev->dev.release = ib_ucm_release_dev; dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum); - if (device_register(&ucm_dev->dev)) + if (device_add(&ucm_dev->dev)) goto err_cdev; if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev)) @@ -1303,13 +1309,9 @@ static void ib_ucm_add_one(struct ib_device *device) err_dev: device_unregister(&ucm_dev->dev); err_cdev: - cdev_del(&ucm_dev->cdev); - if (ucm_dev->devnum < IB_UCM_MAX_DEVICES) - clear_bit(devnum, dev_map); - else - clear_bit(devnum, overflow_map); + ib_ucm_cdev_del(ucm_dev); err: - kfree(ucm_dev); + put_device(&ucm_dev->dev); return; } @@ -1320,6 +1322,7 @@ static void ib_ucm_remove_one(struct ib_device *device, void *client_data) if (!ucm_dev) return; + ib_ucm_cdev_del(ucm_dev); device_unregister(&ucm_dev->dev); }