kernel_optimize_test/drivers/nvdimm
Dan Williams a80e9eee50 libnvdimm/dimm: Avoid race between probe and available_slots_show()
commit 7018c897c2f243d4b5f1b94bc6b4831a7eab80fb 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: 4d88a97aa9 ("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>
2021-02-10 09:29:17 +01:00
..
badrange.c mm/memremap_pages: convert to 'struct range' 2020-10-13 18:38:28 -07:00
blk.c nvdimm: simplify revalidate_disk handling 2020-09-02 08:00:07 -06:00
btt_devs.c
btt.c bdi: remove BDI_CAP_SYNCHRONOUS_IO 2020-09-24 13:43:39 -06:00
btt.h
bus.c nvdimm: simplify revalidate_disk handling 2020-09-02 08:00:07 -06:00
claim.c mm/memremap_pages: convert to 'struct range' 2020-10-13 18:38:28 -07:00
core.c
dax_devs.c
dimm_devs.c libnvdimm/dimm: Avoid race between probe and available_slots_show() 2021-02-10 09:29:17 +01:00
dimm.c
e820.c
Kconfig
label.c libnvdimm/namespace: Fix reaping of invalidated block-window-namespace labels 2020-12-30 11:54:27 +01:00
label.h
Makefile
namespace_devs.c libnvdimm/namespace: Fix visibility of namespace resource attribute 2021-02-10 09:29:16 +01:00
nd_virtio.c
nd-core.h
nd.h mm/memremap_pages: convert to 'struct range' 2020-10-13 18:38:28 -07:00
of_pmem.c
pfn_devs.c mm/memremap_pages: support multiple ranges per invocation 2020-10-13 18:38:28 -07:00
pfn.h
pmem.c mm/memremap_pages: support multiple ranges per invocation 2020-10-13 18:38:28 -07:00
pmem.h
region_devs.c libnvdimm for 5.9 2020-08-11 10:59:19 -07:00
region.c mm/memremap_pages: convert to 'struct range' 2020-10-13 18:38:28 -07:00
security.c libnvdimm for 5.9 2020-08-11 10:59:19 -07:00
virtio_pmem.c
virtio_pmem.h