Skip to content

Commit 2623c7a

Browse files
takondrahtejun
authored andcommitted
libata: add refcounting to ata_host
After commit 9a6d6a2 ("ata: make ata port as parent device of scsi host") manual driver unbind/remove causes use-after-free. Unbind unconditionally invokes devres_release_all() which calls ata_host_release() and frees ata_host/ata_port memory while it is still being referenced as a parent of SCSI host. When SCSI host is finally released scsi_host_dev_release() calls put_device(parent) and accesses freed ata_port memory. Add reference counting to make sure that ata_host lives long enough. Bug report: https://lkml.org/lkml/2017/11/1/945 Fixes: 9a6d6a2 ("ata: make ata port as parent device of scsi host") Cc: Tejun Heo <tj@kernel.org> Cc: Lin Ming <minggr@gmail.com> Cc: linux-ide@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Taras Kondratiuk <takondra@cisco.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent a80ea4c commit 2623c7a

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

drivers/ata/libata-core.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6005,7 +6005,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
60056005
return ap;
60066006
}
60076007

6008-
static void ata_host_release(struct device *gendev, void *res)
6008+
static void ata_devres_release(struct device *gendev, void *res)
60096009
{
60106010
struct ata_host *host = dev_get_drvdata(gendev);
60116011
int i;
@@ -6019,13 +6019,36 @@ static void ata_host_release(struct device *gendev, void *res)
60196019
if (ap->scsi_host)
60206020
scsi_host_put(ap->scsi_host);
60216021

6022+
}
6023+
6024+
dev_set_drvdata(gendev, NULL);
6025+
ata_host_put(host);
6026+
}
6027+
6028+
static void ata_host_release(struct kref *kref)
6029+
{
6030+
struct ata_host *host = container_of(kref, struct ata_host, kref);
6031+
int i;
6032+
6033+
for (i = 0; i < host->n_ports; i++) {
6034+
struct ata_port *ap = host->ports[i];
6035+
60226036
kfree(ap->pmp_link);
60236037
kfree(ap->slave_link);
60246038
kfree(ap);
60256039
host->ports[i] = NULL;
60266040
}
6041+
kfree(host);
6042+
}
60276043

6028-
dev_set_drvdata(gendev, NULL);
6044+
void ata_host_get(struct ata_host *host)
6045+
{
6046+
kref_get(&host->kref);
6047+
}
6048+
6049+
void ata_host_put(struct ata_host *host)
6050+
{
6051+
kref_put(&host->kref, ata_host_release);
60296052
}
60306053

60316054
/**
@@ -6053,26 +6076,31 @@ struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
60536076
struct ata_host *host;
60546077
size_t sz;
60556078
int i;
6079+
void *dr;
60566080

60576081
DPRINTK("ENTER\n");
60586082

6059-
if (!devres_open_group(dev, NULL, GFP_KERNEL))
6060-
return NULL;
6061-
60626083
/* alloc a container for our list of ATA ports (buses) */
60636084
sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6064-
/* alloc a container for our list of ATA ports (buses) */
6065-
host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6085+
host = kzalloc(sz, GFP_KERNEL);
60666086
if (!host)
6087+
return NULL;
6088+
6089+
if (!devres_open_group(dev, NULL, GFP_KERNEL))
6090+
return NULL;
6091+
6092+
dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
6093+
if (!dr)
60676094
goto err_out;
60686095

6069-
devres_add(dev, host);
6096+
devres_add(dev, dr);
60706097
dev_set_drvdata(dev, host);
60716098

60726099
spin_lock_init(&host->lock);
60736100
mutex_init(&host->eh_mutex);
60746101
host->dev = dev;
60756102
host->n_ports = max_ports;
6103+
kref_init(&host->kref);
60766104

60776105
/* allocate ports bound to this host */
60786106
for (i = 0; i < max_ports; i++) {

drivers/ata/libata-transport.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ static DECLARE_TRANSPORT_CLASS(ata_port_class,
224224

225225
static void ata_tport_release(struct device *dev)
226226
{
227+
struct ata_port *ap = tdev_to_port(dev);
228+
ata_host_put(ap->host);
227229
}
228230

229231
/**
@@ -284,6 +286,7 @@ int ata_tport_add(struct device *parent,
284286
dev->type = &ata_port_type;
285287

286288
dev->parent = parent;
289+
ata_host_get(ap->host);
287290
dev->release = ata_tport_release;
288291
dev_set_name(dev, "ata%d", ap->print_id);
289292
transport_setup_device(dev);
@@ -314,6 +317,7 @@ int ata_tport_add(struct device *parent,
314317
tport_err:
315318
transport_destroy_device(dev);
316319
put_device(dev);
320+
ata_host_put(ap->host);
317321
return error;
318322
}
319323

drivers/ata/libata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ extern int ata_port_probe(struct ata_port *ap);
100100
extern void __ata_port_probe(struct ata_port *ap);
101101
extern unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
102102
u8 page, void *buf, unsigned int sectors);
103+
extern void ata_host_get(struct ata_host *host);
104+
extern void ata_host_put(struct ata_host *host);
103105

104106
#define to_ata_port(d) container_of(d, struct ata_port, tdev)
105107

include/linux/libata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ struct ata_host {
617617
void *private_data;
618618
struct ata_port_operations *ops;
619619
unsigned long flags;
620+
struct kref kref;
620621

621622
struct mutex eh_mutex;
622623
struct task_struct *eh_owner;

0 commit comments

Comments
 (0)