Skip to content

Commit

Permalink
pps: Fix a use-after free bug when unregistering a source.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Cc: stable <[email protected]>
Acked-by: Rodolfo Giometti <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
George Spelvin authored and gregkh committed Feb 13, 2013
1 parent 03a7ffe commit d953e0e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions drivers/pps/pps.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,15 @@ static int pps_cdev_open(struct inode *inode, struct file *file)
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;
}

Expand All @@ -274,8 +277,10 @@ static void pps_device_destruct(struct device *dev)
{
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);
Expand Down Expand Up @@ -332,6 +337,7 @@ int pps_register_cdev(struct pps_device *pps)
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,
Expand All @@ -352,9 +358,9 @@ int pps_register_cdev(struct pps_device *pps)

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);
}

/*
Expand Down

0 comments on commit d953e0e

Please sign in to comment.