Skip to contentSkip to conversation

tools/testing/nvdimm: Stop read past end of global handle array

PATCH In review Alison Schofield 3 participants 4 messages Oct 30, 2025 – Oct 31, 2025

Subsystems (2)
lore
Share & tools
lore

Alison Schofield

author maintainer
#0 lore raw

PATCH 1/1 tools/testing/nvdimm: Stop read past end of global handle array

KASAN reports a global-out-of-bounds access when running these nfit
tests: clear.sh, pmem-errors, pfn-meta-errors.sh, btt-errors.sh, daxdev-errors.sh, and inject-error.sh.

[] BUG: KASAN: global-out-of-bounds in nfit_test_ctl+0x769f/0x7840 [nfit_test] [] Read of size 4 at addr ffffffffc03ea01c by task ndctl/1215
[] The buggy address belongs to the variable:
[] handle+0x1c/0x1df4 [nfit_test]

The nfit_test mock platform defines a static table of 7 NFIT DIMM handles, but nfit_test.0 builds 8 mock DIMMs total (5 DCR + 3 PM). When the final DIMM (id == 7) is selected, this code:

    spa->devices[0].nfit_device_handle = handle[nvdimm->id];

indexes past the end of the 7-entry table, triggering KASAN.

Fix this by adding an eighth entry to the handle[] table and a defensive bounds check so the test fails cleanly instead of dereferencing out-of-bounds memory.

To generate a unique handle, the new entry sets the 'imc' field rather than the 'chan' field. This matches the pattern of earlier entries and avoids introducing a non-zero 'chan' which is never used in the table. Computing the new handle shows no collision.

Notes from spelunkering for a Fixes Tag:

Commit 209851649dc4 ("acpi: nfit: Add support for hot-add") increased the mock DIMMs to eight yet kept the handle[] array at seven.

Commit 10246dc84dfc ("acpi nfit: nfit_test supports translate SPA") began using the last mock DIMM, triggering the KASAN.

Commit af31b04b67f4 ("tools/testing/nvdimm: Fix the array size for dimm devices.") addressed a related KASAN warning but not the actual handle array length.

Fixes 209851649dc4 ("acpi: nfit: Add support for hot-add")
---
 tools/testing/nvdimm/test/nfit.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/nvdimm/test/nfit.c +8 -0
index cfd4378e2129..cdbf9e8ee80a 100644
@@ -129,6 +129,7 @@ static u32 handle[] = {
 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
+	[7] = NFIT_DIMM_HANDLE(1, 0, 1, 0, 1),
 };
 
 static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
@@ -688,6 +689,13 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
 	nvdimm = nd_mapping->nvdimm;
 
+	if (WARN_ON_ONCE(nvdimm->id >= ARRAY_SIZE(handle))) {
+		dev_err(&bus->dev,
+			"invalid nvdimm->id %u >= handle array size %zu\n",
+			nvdimm->id, ARRAY_SIZE(handle));
+		return -EINVAL;
+	}
+
 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
 	spa->num_nvdimms = 1;
 	spa->devices[0].dpa = dpa;

base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada

Signature · 1 line
2.37.3
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Alison Schofield

KASAN reports a global-out-of-bounds access when running these nfit
tests: clear.sh, pmem-errors, pfn-meta-errors.sh, btt-errors.sh, daxdev-errors.sh, and inject-error.sh.

[] BUG: KASAN: global-out-of-bounds in nfit_test_ctl+0x769f/0x7840 [nfit_test] [] Read of size 4 at addr ffffffffc03ea01c by task ndctl/1215
[] The buggy address belongs to the variable:
[] handle+0x1c/0x1df4 [nfit_test]

The nfit_test mock platform defines a static table of 7 NFIT DIMM handles, but nfit_test.0 builds 8 mock DIMMs total (5 DCR + 3 PM). When the final DIMM (id == 7) is selected, this code:

    spa->devices[0].nfit_device_handle = handle[nvdimm->id];

indexes past the end of the 7-entry table, triggering KASAN.

Fix this by adding an eighth entry to the handle[] table and a defensive bounds check so the test fails cleanly instead of dereferencing out-of-bounds memory.

To generate a unique handle, the new entry sets the 'imc' field rather than the 'chan' field. This matches the pattern of earlier entries and avoids introducing a non-zero 'chan' which is never used in the table. Computing the new handle shows no collision.

Notes from spelunkering for a Fixes Tag:

Commit 209851649dc4 ("acpi: nfit: Add support for hot-add") increased the mock DIMMs to eight yet kept the handle[] array at seven.

Commit 10246dc84dfc ("acpi nfit: nfit_test supports translate SPA") began using the last mock DIMM, triggering the KASAN.

Quoted 7 lines

Commit af31b04b67f4 ("tools/testing/nvdimm: Fix the array size for dimm devices.") addressed a related KASAN warning but not the actual handle array length.

Fixes 209851649dc4 ("acpi: nfit: Add support for hot-add")
Quoted 31 lines
 tools/testing/nvdimm/test/nfit.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/nvdimm/test/nfit.c +8 -0
index cfd4378e2129..cdbf9e8ee80a 100644
@@ -129,6 +129,7 @@ static u32 handle[] = {
 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
+	[7] = NFIT_DIMM_HANDLE(1, 0, 1, 0, 1),
 };
 
 static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
@@ -688,6 +689,13 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
 	nvdimm = nd_mapping->nvdimm;
 
+	if (WARN_ON_ONCE(nvdimm->id >= ARRAY_SIZE(handle))) {
+		dev_err(&bus->dev,
+			"invalid nvdimm->id %u >= handle array size %zu\n",
+			nvdimm->id, ARRAY_SIZE(handle));
+		return -EINVAL;
+	}
+
 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
 	spa->num_nvdimms = 1;
 	spa->devices[0].dpa = dpa;

base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada

#2 lore raw
In reply to Alison Schofield
Quoted 11 lines · Alison Schofield

KASAN reports a global-out-of-bounds access when running these nfit
tests: clear.sh, pmem-errors, pfn-meta-errors.sh, btt-errors.sh, daxdev-errors.sh, and inject-error.sh.

[] BUG: KASAN: global-out-of-bounds in nfit_test_ctl+0x769f/0x7840 [nfit_test] [] Read of size 4 at addr ffffffffc03ea01c by task ndctl/1215
[] The buggy address belongs to the variable:
[] handle+0x1c/0x1df4 [nfit_test]

The nfit_test mock platform defines a static table of 7 NFIT DIMM handles, but nfit_test.0 builds 8 mock DIMMs total (5 DCR + 3 PM).

That does not sound right. NUM_PM is not adding DIMM devices.

Quoted 13 lines
tools/testing/nvdimm/test/nfit.c +1 -0
index cfd4378e2129..cdbf9e8ee80a 100644
@@ -129,6 +129,7 @@ static u32 handle[] = {
 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
+	[7] = NFIT_DIMM_HANDLE(1, 0, 1, 0, 1),
 };
 
 static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];

tools/testing/nvdimm/test/nfit.c static int nfit_test_search_spa(struct nvdimm_bus *bus, · line 697

Quoted 9 lines
tools/testing/nvdimm/test/nfit.c +6 -0
@@ -688,6 +689,13 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
 	nvdimm = nd_mapping->nvdimm;
 
+	if (WARN_ON_ONCE(nvdimm->id >= ARRAY_SIZE(handle))) {
+		dev_err(&bus->dev,
+			"invalid nvdimm->id %u >= handle array size %zu\n",
+			nvdimm->id, ARRAY_SIZE(handle));
+		return -EINVAL;
+	}

No, I think the bug is assuming that the nvdimm device id is the handle lookup.

I.e. this looks wrong to me:

	spa->devices[0].nfit_device_handle = handle[nvdimm->id];

...does something like this also fix the warning? (UNTESTED, not even compiled!):

tools/testing/nvdimm/test/nfit.c +2 -1
index cfd4378e2129..5456f67b7e43 100644
@@ -688,7 +688,8 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
        nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
        nvdimm = nd_mapping->nvdimm;
 
-       spa->devices[0].nfit_device_handle = handle[nvdimm->id];
+       nfit_mem = nvdimm_provider_data(nvdimm);
+       spa->devices[0].nfit_device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
        spa->num_nvdimms = 1;
        spa->devices[0].dpa = dpa;
 
author maintainer
#3 lore raw
Quoted 41 lines · Dan Williams
Quoted 11 lines · Alison Schofield

KASAN reports a global-out-of-bounds access when running these nfit
tests: clear.sh, pmem-errors, pfn-meta-errors.sh, btt-errors.sh, daxdev-errors.sh, and inject-error.sh.

[] BUG: KASAN: global-out-of-bounds in nfit_test_ctl+0x769f/0x7840 [nfit_test] [] Read of size 4 at addr ffffffffc03ea01c by task ndctl/1215
[] The buggy address belongs to the variable:
[] handle+0x1c/0x1df4 [nfit_test]

The nfit_test mock platform defines a static table of 7 NFIT DIMM handles, but nfit_test.0 builds 8 mock DIMMs total (5 DCR + 3 PM).

That does not sound right. NUM_PM is not adding DIMM devices.

Quoted 13 lines
tools/testing/nvdimm/test/nfit.c +1 -0
index cfd4378e2129..cdbf9e8ee80a 100644
@@ -129,6 +129,7 @@ static u32 handle[] = {
 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
+	[7] = NFIT_DIMM_HANDLE(1, 0, 1, 0, 1),
 };
 
 static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];

tools/testing/nvdimm/test/nfit.c static int nfit_test_search_spa(struct nvdimm_bus *bus, · line 697

Quoted 9 lines
tools/testing/nvdimm/test/nfit.c +6 -0
@@ -688,6 +689,13 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
 	nvdimm = nd_mapping->nvdimm;
 
+	if (WARN_ON_ONCE(nvdimm->id >= ARRAY_SIZE(handle))) {
+		dev_err(&bus->dev,
+			"invalid nvdimm->id %u >= handle array size %zu\n",
+			nvdimm->id, ARRAY_SIZE(handle));
+		return -EINVAL;
+	}

No, I think the bug is assuming that the nvdimm device id is the handle lookup.

Quoted 6 lines

I.e. this looks wrong to me:

	spa->devices[0].nfit_device_handle = handle[nvdimm->id];

...does something like this also fix the warning? (UNTESTED, not even compiled!):

Dan - This does fix it...and I basically get it. v2'ing. Thanks!

Quoted 15 lines
tools/testing/nvdimm/test/nfit.c +2 -1
index cfd4378e2129..5456f67b7e43 100644
@@ -688,7 +688,8 @@ static int nfit_test_search_spa(struct nvdimm_bus *bus,
        nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
        nvdimm = nd_mapping->nvdimm;
 
-       spa->devices[0].nfit_device_handle = handle[nvdimm->id];
+       nfit_mem = nvdimm_provider_data(nvdimm);
+       spa->devices[0].nfit_device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
        spa->num_nvdimms = 1;
        spa->devices[0].dpa = dpa;