Skip to content

Commit 7018c89

Browse files
committed
libnvdimm/dimm: Avoid race between probe and available_slots_show()
Richard reports that the following test: (while true; do cat /sys/bus/nd/devices/nmem*/available_slots 2>&1 > /dev/null done) & while true; do for i in $(seq 0 4); do echo nmem$i > /sys/bus/nd/drivers/nvdimm/bind done for i in $(seq 0 4); do echo nmem$i > /sys/bus/nd/drivers/nvdimm/unbind done done ...fails with a crash signature like: divide error: 0000 [#1] SMP KASAN PTI RIP: 0010:nd_label_nfree+0x134/0x1a0 [libnvdimm] [..] Call Trace: available_slots_show+0x4e/0x120 [libnvdimm] dev_attr_show+0x42/0x80 ? memset+0x20/0x40 sysfs_kf_seq_show+0x218/0x410 The root cause is that available_slots_show() consults driver-data, but fails to synchronize against device-unbind setting up a TOCTOU race to access uninitialized memory. Validate driver-data under the device-lock. Fixes: 4d88a97 ("libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver infrastructure") Cc: <[email protected]> Cc: Vishal Verma <[email protected]> Cc: Dave Jiang <[email protected]> Cc: Ira Weiny <[email protected]> Cc: Coly Li <[email protected]> Reported-by: Richard Palethorpe <[email protected]> Acked-by: Richard Palethorpe <[email protected]> Signed-off-by: Dan Williams <[email protected]>
1 parent 50f558a commit 7018c89

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

drivers/nvdimm/dimm_devs.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,16 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
335335
}
336336
static DEVICE_ATTR_RO(state);
337337

338-
static ssize_t available_slots_show(struct device *dev,
339-
struct device_attribute *attr, char *buf)
338+
static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)
340339
{
341-
struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
340+
struct device *dev;
342341
ssize_t rc;
343342
u32 nfree;
344343

345344
if (!ndd)
346345
return -ENXIO;
347346

347+
dev = ndd->dev;
348348
nvdimm_bus_lock(dev);
349349
nfree = nd_label_nfree(ndd);
350350
if (nfree - 1 > nfree) {
@@ -356,6 +356,18 @@ static ssize_t available_slots_show(struct device *dev,
356356
nvdimm_bus_unlock(dev);
357357
return rc;
358358
}
359+
360+
static ssize_t available_slots_show(struct device *dev,
361+
struct device_attribute *attr, char *buf)
362+
{
363+
ssize_t rc;
364+
365+
nd_device_lock(dev);
366+
rc = __available_slots_show(dev_get_drvdata(dev), buf);
367+
nd_device_unlock(dev);
368+
369+
return rc;
370+
}
359371
static DEVICE_ATTR_RO(available_slots);
360372

361373
__weak ssize_t security_show(struct device *dev,

0 commit comments

Comments
 (0)