Skip to content

Commit c030175

Browse files
oberparMartin Schwidefsky
authored andcommitted
[S390] cio: fix ccwgroup unregistration race condition
A race condition exists in the ccwgroup device unregistration code which can cause a kernel panic due to a use-after-free bug. This race condition might be triggered when all ccw devices associated with a ccwgroup device are removed at the same time (e.g. because the corresponding channel path becomes no longer available). Fix this race condition by clearing the references from the associated ccw devices to the ccw group device during unregistration of the ccw group device. Signed-off-by: Peter Oberparleiter <peter.oberparleiter@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent f602be6 commit c030175

1 file changed

Lines changed: 40 additions & 38 deletions

File tree

‎drivers/s390/cio/ccwgroup.c‎

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
6666

6767
}
6868

69+
/*
70+
* Remove references from ccw devices to ccw group device and from
71+
* ccw group device to ccw devices.
72+
*/
73+
static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
74+
{
75+
struct ccw_device *cdev;
76+
int i;
77+
78+
for (i = 0; i < gdev->count; i++) {
79+
cdev = gdev->cdev[i];
80+
if (!cdev)
81+
continue;
82+
spin_lock_irq(cdev->ccwlock);
83+
dev_set_drvdata(&cdev->dev, NULL);
84+
spin_unlock_irq(cdev->ccwlock);
85+
gdev->cdev[i] = NULL;
86+
put_device(&cdev->dev);
87+
}
88+
}
89+
6990
/*
7091
* Provide an 'ungroup' attribute so the user can remove group devices no
7192
* longer needed or accidentially created. Saves memory :)
@@ -78,6 +99,7 @@ static void ccwgroup_ungroup_callback(struct device *dev)
7899
if (device_is_registered(&gdev->dev)) {
79100
__ccwgroup_remove_symlinks(gdev);
80101
device_unregister(dev);
102+
__ccwgroup_remove_cdev_refs(gdev);
81103
}
82104
mutex_unlock(&gdev->reg_mutex);
83105
}
@@ -116,21 +138,7 @@ static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
116138
static void
117139
ccwgroup_release (struct device *dev)
118140
{
119-
struct ccwgroup_device *gdev;
120-
int i;
121-
122-
gdev = to_ccwgroupdev(dev);
123-
124-
for (i = 0; i < gdev->count; i++) {
125-
if (gdev->cdev[i]) {
126-
spin_lock_irq(gdev->cdev[i]->ccwlock);
127-
if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
128-
dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
129-
spin_unlock_irq(gdev->cdev[i]->ccwlock);
130-
put_device(&gdev->cdev[i]->dev);
131-
}
132-
}
133-
kfree(gdev);
141+
kfree(to_ccwgroupdev(dev));
134142
}
135143

136144
static int
@@ -639,6 +647,7 @@ void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
639647
mutex_lock(&gdev->reg_mutex);
640648
__ccwgroup_remove_symlinks(gdev);
641649
device_unregister(dev);
650+
__ccwgroup_remove_cdev_refs(gdev);
642651
mutex_unlock(&gdev->reg_mutex);
643652
put_device(dev);
644653
}
@@ -660,25 +669,6 @@ int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
660669
return 0;
661670
}
662671

663-
static struct ccwgroup_device *
664-
__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
665-
{
666-
struct ccwgroup_device *gdev;
667-
668-
gdev = dev_get_drvdata(&cdev->dev);
669-
if (gdev) {
670-
if (get_device(&gdev->dev)) {
671-
mutex_lock(&gdev->reg_mutex);
672-
if (device_is_registered(&gdev->dev))
673-
return gdev;
674-
mutex_unlock(&gdev->reg_mutex);
675-
put_device(&gdev->dev);
676-
}
677-
return NULL;
678-
}
679-
return NULL;
680-
}
681-
682672
/**
683673
* ccwgroup_remove_ccwdev() - remove function for slave devices
684674
* @cdev: ccw device to be removed
@@ -694,13 +684,25 @@ void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
694684
/* Ignore offlining errors, device is gone anyway. */
695685
ccw_device_set_offline(cdev);
696686
/* If one of its devices is gone, the whole group is done for. */
697-
gdev = __ccwgroup_get_gdev_by_cdev(cdev);
698-
if (gdev) {
687+
spin_lock_irq(cdev->ccwlock);
688+
gdev = dev_get_drvdata(&cdev->dev);
689+
if (!gdev) {
690+
spin_unlock_irq(cdev->ccwlock);
691+
return;
692+
}
693+
/* Get ccwgroup device reference for local processing. */
694+
get_device(&gdev->dev);
695+
spin_unlock_irq(cdev->ccwlock);
696+
/* Unregister group device. */
697+
mutex_lock(&gdev->reg_mutex);
698+
if (device_is_registered(&gdev->dev)) {
699699
__ccwgroup_remove_symlinks(gdev);
700700
device_unregister(&gdev->dev);
701-
mutex_unlock(&gdev->reg_mutex);
702-
put_device(&gdev->dev);
701+
__ccwgroup_remove_cdev_refs(gdev);
703702
}
703+
mutex_unlock(&gdev->reg_mutex);
704+
/* Release ccwgroup device reference for local processing. */
705+
put_device(&gdev->dev);
704706
}
705707

706708
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)