Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756993Ab3CDDoa (ORCPT ); Sun, 3 Mar 2013 22:44:30 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:33048 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756900Ab3CDDoX (ORCPT ); Sun, 3 Mar 2013 22:44:23 -0500 Message-Id: <20130304033713.050717345@decadent.org.uk> User-Agent: quilt/0.60-1 Date: Mon, 04 Mar 2013 03:38:02 +0000 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: akpm@linux-foundation.org, George Spelvin , Rodolfo Giometti , Greg Kroah-Hartman Subject: [ 055/153] pps: Fix a use-after free bug when unregistering a source. In-Reply-To: <20130304033707.648729212@decadent.org.uk> X-SA-Exim-Connect-IP: 2001:470:1f08:1539:a11:96ff:fec6:70c4 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2618 Lines: 86 3.2-stable review patch. If anyone has any objections, please let me know. ------------------ From: George Spelvin commit d953e0e837e65ecc1ddaa4f9560f7925878a0de6 upstream. Remove the cdev from the system (with cdev_del) *before* deallocating it (in pps_device_destruct, called via kobject_put from device_destroy). Also prevent deallocating a device with open file handles. A better long-term fix is probably to remove the cdev from the pps_device entirely, and instead have all devices reference one global cdev. Then the deallocation ordering becomes simpler. But that's more complex and invasive change, so we leave that for later. Signed-off-by: George Spelvin Acked-by: Rodolfo Giometti Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- drivers/pps/pps.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) --- a/drivers/pps/pps.c +++ b/drivers/pps/pps.c @@ -247,12 +247,15 @@ static int pps_cdev_open(struct inode *i struct pps_device *pps = container_of(inode->i_cdev, struct pps_device, cdev); file->private_data = pps; - + kobject_get(&pps->dev->kobj); return 0; } static int pps_cdev_release(struct inode *inode, struct file *file) { + struct pps_device *pps = container_of(inode->i_cdev, + struct pps_device, cdev); + kobject_put(&pps->dev->kobj); return 0; } @@ -274,8 +277,10 @@ static void pps_device_destruct(struct d { struct pps_device *pps = dev_get_drvdata(dev); - /* release id here to protect others from using it while it's - * still in use */ + cdev_del(&pps->cdev); + + /* Now we can release the ID for re-use */ + pr_debug("deallocating pps%d\n", pps->id); mutex_lock(&pps_idr_lock); idr_remove(&pps_idr, pps->id); mutex_unlock(&pps_idr_lock); @@ -330,6 +335,7 @@ int pps_register_cdev(struct pps_device if (IS_ERR(pps->dev)) goto del_cdev; + /* Override the release function with our own */ pps->dev->release = pps_device_destruct; pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, @@ -350,9 +356,9 @@ free_idr: void pps_unregister_cdev(struct pps_device *pps) { + pr_debug("unregistering pps%d\n", pps->id); pps->lookup_cookie = NULL; device_destroy(pps_class, pps->dev->devt); - cdev_del(&pps->cdev); } /* -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/