Skip to contentSkip to conversation

s390: cmf: fix use-after-free in enable_cmf

PATCH In review Qinglang Miao 2 participants 2 messages Nov 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390: cmf: fix use-after-free in enable_cmf

kfree(cdev) is called in put_device in the error branch. So that device_unlock(&cdev->dev) would raise a use-after-free bug. In fact, there's no need to call device_unlock after put_device.

Fix it by adding simply return after put_device.

Fixes a6ef15652d26 ("s390/cio: fix use after free in cmb processing")
---
 drivers/s390/cio/cmf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/s390/cio/cmf.c +4 -1
index 72dd2471e..e95ca476f 100644
@@ -1149,9 +1149,12 @@ int enable_cmf(struct ccw_device *cdev)
 		sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
 		cmbops->free(cdev);
 	}
+
 out:
-	if (ret)
+	if (ret) {
 		put_device(&cdev->dev);
+		return ret;
+	}
 out_unlock:
 	device_unlock(&cdev->dev);
 	return ret;
Signature · 1 line
2.23.0
#1 lore raw
Quoted 18 lines · On Fri, 20 Nov 2020 15:48:50 +0800 Qinglang Miao <[email protected]>…

kfree(cdev) is called in put_device in the error branch. So that device_unlock(&cdev->dev) would raise a use-after-free bug. In fact, there's no need to call device_unlock after put_device.

Fix it by adding simply return after put_device.

Fixes a6ef15652d26 ("s390/cio: fix use after free in cmb processing")
---
 drivers/s390/cio/cmf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/s390/cio/cmf.c +0 -0
index 72dd2471e..e95ca476f 100644

drivers/s390/cio/cmf.c int enable_cmf(struct ccw_device *cdev) · line 1155

Quoted 8 lines
drivers/s390/cio/cmf.c +2 -1
@@ -1149,9 +1149,12 @@ int enable_cmf(struct ccw_device *cdev)
 		sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
 		cmbops->free(cdev);
 	}
+
 out:
-	if (ret)
+	if (ret) {
 		put_device(&cdev->dev);

The put_device() here undoes a get_device() further up in the function. There is at least one more reference remaining, held by the caller of enable_cmf(). Returning here would actually introduce a bug (missing unlock).

Quoted 5 lines
drivers/s390/cio/cmf.c +2 -0
+		return ret;
+	}
 out_unlock:
 	device_unlock(&cdev->dev);
 	return ret;