Skip to contentSkip to conversation

acpi: nfit: vmalloc-out-of-bounds Read in acpi_nfit_ctl

PATCH Superseded Suraj Sonawane 2 participants 5 messages Nov 12, 2024 – Nov 13, 2024

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 acpi: nfit: vmalloc-out-of-bounds Read in acpi_nfit_ctl

Fix an issue detected by syzbot with KASAN:

BUG: KASAN: vmalloc-out-of-bounds in cmd_to_func drivers/acpi/nfit/
core.c:416 [inline]
BUG: KASAN: vmalloc-out-of-bounds in acpi_nfit_ctl+0x20e8/0x24a0

drivers/acpi/nfit/core.c:459

The issue occurs in cmd_to_func when the call_pkg->nd_reserved2 array is accessed without verifying that call_pkg points to a buffer that is sized appropriately as a struct nd_cmd_pkg. This could lead to out-of-bounds access and undefined behavior if the buffer does not have sufficient space.

To address this issue, a check was added in acpi_nfit_ctl() to ensure that buf is not NULL and buf_len is greater than or equal to sizeof(struct nd_cmd_pkg) before casting buf to struct nd_cmd_pkg *. This ensures safe access to the members of call_pkg, including the nd_reserved2 array.

This change preventing out-of-bounds reads.

---
V1: https://lore.kernel.org/lkml/[email protected]/
V2: Initialized out_obj to NULL in acpi_nfit_ctl() to prevent potential uninitialized variable usage if condition is true.

 drivers/acpi/nfit/core.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/acpi/nfit/core.c +9 -3
index 5429ec9ef..d0e655a9c 100644
@@ -439,7 +439,7 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
 {
 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
 	struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
-	union acpi_object in_obj, in_buf, *out_obj;
+	union acpi_object in_obj, in_buf, *out_obj = NULL;
 	const struct nd_cmd_desc *desc = NULL;
 	struct device *dev = acpi_desc->dev;
 	struct nd_cmd_pkg *call_pkg = NULL;
@@ -454,8 +454,14 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
 	if (cmd_rc)
 		*cmd_rc = -EINVAL;
 
-	if (cmd == ND_CMD_CALL)
-		call_pkg = buf;
+	if (cmd == ND_CMD_CALL) {
+		if (buf == NULL || buf_len < sizeof(struct nd_cmd_pkg)) {
+			rc = -EINVAL;
+			goto out;
+		}
+		call_pkg = (struct nd_cmd_pkg *)buf;
+	}
+
 	func = cmd_to_func(nfit_mem, cmd, call_pkg, &family);
 	if (func < 0)
 		return func;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 13 lines · Suraj Sonawane

Fix an issue detected by syzbot with KASAN:

BUG: KASAN: vmalloc-out-of-bounds in cmd_to_func drivers/acpi/nfit/
core.c:416 [inline]
BUG: KASAN: vmalloc-out-of-bounds in acpi_nfit_ctl+0x20e8/0x24a0

drivers/acpi/nfit/core.c:459

The issue occurs in cmd_to_func when the call_pkg->nd_reserved2 array is accessed without verifying that call_pkg points to a buffer that is sized appropriately as a struct nd_cmd_pkg. This could lead to out-of-bounds access and undefined behavior if the buffer does not have sufficient space.

Quoted 5 lines

To address this issue, a check was added in acpi_nfit_ctl() to ensure that buf is not NULL and buf_len is greater than or equal to sizeof(struct nd_cmd_pkg) before casting buf to struct nd_cmd_pkg *. This ensures safe access to the members of call_pkg, including the nd_reserved2 array.

That all sounds good! A couple of coding conventions fixups suggested below -

snip

Quoted 10 lines
drivers/acpi/nfit/core.c +1 -1
@@ -439,7 +439,7 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
 {
 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
 	struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
-	union acpi_object in_obj, in_buf, *out_obj;
+	union acpi_object in_obj, in_buf, *out_obj = NULL;
 	const struct nd_cmd_desc *desc = NULL;
 	struct device *dev = acpi_desc->dev;
 	struct nd_cmd_pkg *call_pkg = NULL;

drivers/acpi/nfit/core.c int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm, · line 458

Quoted 7 lines
drivers/acpi/nfit/core.c +2 -2
@@ -454,8 +454,14 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
 	if (cmd_rc)
 		*cmd_rc = -EINVAL;
 
-	if (cmd == ND_CMD_CALL)
-		call_pkg = buf;
+	if (cmd == ND_CMD_CALL) {
+		if (buf == NULL || buf_len < sizeof(struct nd_cmd_pkg)) {

Comparison to NULL and sizeof() usage preferred like this:

	if (!buf || buf_len < sizeof(*call_pkg))

-snip

Quoted 2 lines
#2 lore raw
Quoted 44 lines · Alison Schofield
Quoted 13 lines · Suraj Sonawane

Fix an issue detected by syzbot with KASAN:

BUG: KASAN: vmalloc-out-of-bounds in cmd_to_func drivers/acpi/nfit/
core.c:416 [inline]
BUG: KASAN: vmalloc-out-of-bounds in acpi_nfit_ctl+0x20e8/0x24a0

drivers/acpi/nfit/core.c:459

The issue occurs in cmd_to_func when the call_pkg->nd_reserved2 array is accessed without verifying that call_pkg points to a buffer that is sized appropriately as a struct nd_cmd_pkg. This could lead to out-of-bounds access and undefined behavior if the buffer does not have sufficient space.

Quoted 5 lines

To address this issue, a check was added in acpi_nfit_ctl() to ensure that buf is not NULL and buf_len is greater than or equal to sizeof(struct nd_cmd_pkg) before casting buf to struct nd_cmd_pkg *. This ensures safe access to the members of call_pkg, including the nd_reserved2 array.

That all sounds good! A couple of coding conventions fixups suggested below -

snip

Quoted 10 lines
drivers/acpi/nfit/core.c +1 -1
@@ -439,7 +439,7 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
  {
  	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  	struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
-	union acpi_object in_obj, in_buf, *out_obj;
+	union acpi_object in_obj, in_buf, *out_obj = NULL;
  	const struct nd_cmd_desc *desc = NULL;
  	struct device *dev = acpi_desc->dev;
  	struct nd_cmd_pkg *call_pkg = NULL;

drivers/acpi/nfit/core.c int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm, · line 458

Quoted 7 lines
drivers/acpi/nfit/core.c +2 -2
@@ -454,8 +454,14 @@ int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
  	if (cmd_rc)
  		*cmd_rc = -EINVAL;
 
-	if (cmd == ND_CMD_CALL)
-		call_pkg = buf;
+	if (cmd == ND_CMD_CALL) {
+		if (buf == NULL || buf_len < sizeof(struct nd_cmd_pkg)) {

Comparison to NULL and sizeof() usage preferred like this:

Quoted 6 lines
	if (!buf || buf_len < sizeof(*call_pkg))

-snip

Quoted 2 lines

Thank you for the feedback and your time.

I appreciate your review and coding convention insights. I have studied the suggested change and updated the patch.

I will test the updated patch with syzbot and submit a new version very shortly.

Best regards,
Suraj Sonawane

maintainer
#3 lore raw
In reply to Suraj Sonawane
Quoted 19 lines · Suraj Sonawane

Fix an issue detected by syzbot with KASAN:

BUG: KASAN: vmalloc-out-of-bounds in cmd_to_func drivers/acpi/nfit/
core.c:416 [inline]
BUG: KASAN: vmalloc-out-of-bounds in acpi_nfit_ctl+0x20e8/0x24a0

drivers/acpi/nfit/core.c:459

The issue occurs in cmd_to_func when the call_pkg->nd_reserved2 array is accessed without verifying that call_pkg points to a buffer that is sized appropriately as a struct nd_cmd_pkg. This could lead to out-of-bounds access and undefined behavior if the buffer does not have sufficient space.

To address this issue, a check was added in acpi_nfit_ctl() to ensure that buf is not NULL and buf_len is greater than or equal to sizeof(struct nd_cmd_pkg) before casting buf to struct nd_cmd_pkg *. This ensures safe access to the members of call_pkg, including the nd_reserved2 array.

Quoted 7 lines

This change preventing out-of-bounds reads.

Suraj,

The fixes tag needs to be where the issue originated, not where you discovered it (which I'm guessing was using 6.12-rc7).

Here's how I find the tag:

$ git blame drivers/acpi/nfit/core.c | grep call_pkg | grep buf
ebe9f6f19d80d drivers/acpi/nfit/core.c (Dan Williams       2019-02-07 14:56:50 -0800  458) 		call_pkg = buf;
$ git log -1 --pretty=fixes ebe9f6f19d80d
Fixes: ebe9f6f19d80 ("acpi/nfit: Fix bus command validation")

I think ^ should be your Fixes tag.

snip

Quoted 1 line
#4 lore raw
Quoted 28 lines · Alison Schofield
Quoted 19 lines · Suraj Sonawane

Fix an issue detected by syzbot with KASAN:

BUG: KASAN: vmalloc-out-of-bounds in cmd_to_func drivers/acpi/nfit/
core.c:416 [inline]
BUG: KASAN: vmalloc-out-of-bounds in acpi_nfit_ctl+0x20e8/0x24a0

drivers/acpi/nfit/core.c:459

The issue occurs in cmd_to_func when the call_pkg->nd_reserved2 array is accessed without verifying that call_pkg points to a buffer that is sized appropriately as a struct nd_cmd_pkg. This could lead to out-of-bounds access and undefined behavior if the buffer does not have sufficient space.

To address this issue, a check was added in acpi_nfit_ctl() to ensure that buf is not NULL and buf_len is greater than or equal to sizeof(struct nd_cmd_pkg) before casting buf to struct nd_cmd_pkg *. This ensures safe access to the members of call_pkg, including the nd_reserved2 array.

Quoted 7 lines

This change preventing out-of-bounds reads.

Quoted 5 lines

Suraj,

The fixes tag needs to be where the issue originated, not where you discovered it (which I'm guessing was using 6.12-rc7).

Thank you for your feedback.

Quoted 6 lines

Here's how I find the tag:

$ git blame drivers/acpi/nfit/core.c | grep call_pkg | grep buf
ebe9f6f19d80d drivers/acpi/nfit/core.c (Dan Williams       2019-02-07 14:56:50 -0800  458) 		call_pkg = buf;
$ git log -1 --pretty=fixes ebe9f6f19d80d

Thank you for this detailed explaination.

Quoted 3 lines
Fixes ebe9f6f19d80 ("acpi/nfit: Fix bus command validation")

I think ^ should be your Fixes tag.

Yes, I ran the provided steps to verify the commit ID ebe9f6f19d80d and verified it.

Quoted 5 lines

snip

Quoted 1 line

I'll update the Fixes tag in the next version of the patch. Additionally, I will re-test with syzbot and submit the revised patch shortly.

Thank you for your time and feedback!

Best,
Suraj Sonawane