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: 730720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 ozlabs.org (Postfix) with ESMTPS id 3vSVT72ghlz9s7M for ; Wed, 22 Feb 2017 06:10:51 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cgFpm-0007dk-2O; Tue, 21 Feb 2017 19:10:46 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cgFpl-0007dS-AF for tpmdd-devel@lists.sourceforge.net; Tue, 21 Feb 2017 19:10:45 +0000 Received-SPF: pass (sog-mx-2.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-2.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1cgFpk-0000or-2c for tpmdd-devel@lists.sourceforge.net; Tue, 21 Feb 2017 19:10:45 +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 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 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-Spam-Score: -1.6 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1cgFpk-0000or-2c Cc: Alessandro Zummo , Jan Kara , linux-iio@vger.kernel.org, linux-pci@vger.kernel.org, Linus Walleij , Alexandre Bounine , Alexandre Belloni , Peter Meerwald-Stadler , linux-scsi@vger.kernel.org, Sean Hefty , Parav Pandit , Alexandre Courbot , Lars-Peter Clausen , rtc-linux@googlegroups.com, Leon Romanovsky , linux-rdma@vger.kernel.org, Richard Weinberger , Marek Vasut , Doug Ledford , tpmdd-devel@lists.sourceforge.net, Hans Verkuil , Johannes Thumshirn , Matt Porter , Hal Rosenstock , linux-media@vger.kernel.org, Boaz Harrosh , Arnd Bergmann , Boris Brezillon , Sajjan Vikas C , linux-input@vger.kernel.org, Vladimir Zapolskiy , Joe Perches , Cyrille Pitchen , Benny Halevy , Alexander Viro , Bjorn Helgaas , Dan Williams , Mauro Carvalho Chehab , Dmitry Vyukov , linux-nvdimm@ml01.01.org, Haggai Eran , linux-fsdevel@vger.kernel.org, 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 Subject: Re: [tpmdd-devel] [PATCH 07/14] infiniband: utilize new device_add_cdev helper function 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 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); }