Skip to content

Commit f3cdbe3

Browse files
Mike ChristieNicholas Bellinger
authored andcommitted
tcmu: fix crash during device removal
We currently do tcmu_free_device ->tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE) -> uio_unregister_device -> kfree(tcmu_dev). The problem is that the kernel does not wait for userspace to do the close() on the uio device before freeing the tcmu_dev. We can then hit a race where the kernel frees the tcmu_dev before userspace does close() and so when close() -> release -> tcmu_release is done, we try to access a freed tcmu_dev. This patch made over the target-pending master branch moves the freeing of the tcmu_dev to when the last reference has been dropped. This also fixes a leak where if tcmu_configure_device was not called on a device we did not free udev->name which was allocated at tcmu_alloc_device time. Signed-off-by: Mike Christie <mchristi@redhat.com> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 4ff83da commit f3cdbe3

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

‎drivers/target/target_core_user.c‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct tcmu_hba {
9797

9898
struct tcmu_dev {
9999
struct list_head node;
100-
100+
struct kref kref;
101101
struct se_device se_dev;
102102

103103
char *name;
@@ -969,6 +969,7 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
969969
udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
970970
if (!udev)
971971
return NULL;
972+
kref_init(&udev->kref);
972973

973974
udev->name = kstrdup(name, GFP_KERNEL);
974975
if (!udev->name) {
@@ -1145,14 +1146,33 @@ static int tcmu_open(struct uio_info *info, struct inode *inode)
11451146
return 0;
11461147
}
11471148

1149+
static void tcmu_dev_call_rcu(struct rcu_head *p)
1150+
{
1151+
struct se_device *dev = container_of(p, struct se_device, rcu_head);
1152+
struct tcmu_dev *udev = TCMU_DEV(dev);
1153+
1154+
kfree(udev->uio_info.name);
1155+
kfree(udev->name);
1156+
kfree(udev);
1157+
}
1158+
1159+
static void tcmu_dev_kref_release(struct kref *kref)
1160+
{
1161+
struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1162+
struct se_device *dev = &udev->se_dev;
1163+
1164+
call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1165+
}
1166+
11481167
static int tcmu_release(struct uio_info *info, struct inode *inode)
11491168
{
11501169
struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
11511170

11521171
clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
11531172

11541173
pr_debug("close\n");
1155-
1174+
/* release ref from configure */
1175+
kref_put(&udev->kref, tcmu_dev_kref_release);
11561176
return 0;
11571177
}
11581178

@@ -1272,6 +1292,12 @@ static int tcmu_configure_device(struct se_device *dev)
12721292
dev->dev_attrib.hw_max_sectors = 128;
12731293
dev->dev_attrib.hw_queue_depth = 128;
12741294

1295+
/*
1296+
* Get a ref incase userspace does a close on the uio device before
1297+
* LIO has initiated tcmu_free_device.
1298+
*/
1299+
kref_get(&udev->kref);
1300+
12751301
ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
12761302
udev->uio_info.uio_dev->minor);
12771303
if (ret)
@@ -1284,11 +1310,13 @@ static int tcmu_configure_device(struct se_device *dev)
12841310
return 0;
12851311

12861312
err_netlink:
1313+
kref_put(&udev->kref, tcmu_dev_kref_release);
12871314
uio_unregister_device(&udev->uio_info);
12881315
err_register:
12891316
vfree(udev->mb_addr);
12901317
err_vzalloc:
12911318
kfree(info->name);
1319+
info->name = NULL;
12921320

12931321
return ret;
12941322
}
@@ -1302,14 +1330,6 @@ static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
13021330
return -EINVAL;
13031331
}
13041332

1305-
static void tcmu_dev_call_rcu(struct rcu_head *p)
1306-
{
1307-
struct se_device *dev = container_of(p, struct se_device, rcu_head);
1308-
struct tcmu_dev *udev = TCMU_DEV(dev);
1309-
1310-
kfree(udev);
1311-
}
1312-
13131333
static bool tcmu_dev_configured(struct tcmu_dev *udev)
13141334
{
13151335
return udev->uio_info.uio_dev ? true : false;
@@ -1364,10 +1384,10 @@ static void tcmu_free_device(struct se_device *dev)
13641384
udev->uio_info.uio_dev->minor);
13651385

13661386
uio_unregister_device(&udev->uio_info);
1367-
kfree(udev->uio_info.name);
1368-
kfree(udev->name);
13691387
}
1370-
call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1388+
1389+
/* release ref from init */
1390+
kref_put(&udev->kref, tcmu_dev_kref_release);
13711391
}
13721392

13731393
enum {

0 commit comments

Comments
 (0)