Skip to content

Commit 0a4ce3f

Browse files
djbwksacilotto
authored andcommitted
libnvdimm/dimm: Avoid race between probe and available_slots_show()
BugLink: https://bugs.launchpad.net/bugs/1916066 commit 7018c89 upstream. 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: <stable@vger.kernel.org> Cc: Vishal Verma <vishal.l.verma@intel.com> Cc: Dave Jiang <dave.jiang@intel.com> Cc: Ira Weiny <ira.weiny@intel.com> Cc: Coly Li <colyli@suse.com> Reported-by: Richard Palethorpe <rpalethorpe@suse.com> Acked-by: Richard Palethorpe <rpalethorpe@suse.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 9125258 commit 0a4ce3f

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
@@ -344,16 +344,16 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr,
344344
}
345345
static DEVICE_ATTR_RO(state);
346346

347-
static ssize_t available_slots_show(struct device *dev,
348-
struct device_attribute *attr, char *buf)
347+
static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)
349348
{
350-
struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
349+
struct device *dev;
351350
ssize_t rc;
352351
u32 nfree;
353352

354353
if (!ndd)
355354
return -ENXIO;
356355

356+
dev = ndd->dev;
357357
nvdimm_bus_lock(dev);
358358
nfree = nd_label_nfree(ndd);
359359
if (nfree - 1 > nfree) {
@@ -365,6 +365,18 @@ static ssize_t available_slots_show(struct device *dev,
365365
nvdimm_bus_unlock(dev);
366366
return rc;
367367
}
368+
369+
static ssize_t available_slots_show(struct device *dev,
370+
struct device_attribute *attr, char *buf)
371+
{
372+
ssize_t rc;
373+
374+
nd_device_lock(dev);
375+
rc = __available_slots_show(dev_get_drvdata(dev), buf);
376+
nd_device_unlock(dev);
377+
378+
return rc;
379+
}
368380
static DEVICE_ATTR_RO(available_slots);
369381

370382
__weak ssize_t security_show(struct device *dev,

0 commit comments

Comments
 (0)