Skip to contentSkip to conversation

cxl/mbox: Clamp mailbox output allocation to the payload size

PATCH Applied Richard Cheng 2 participants 3 messages Jun 24 – Jun 29 CXL

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cxl/mbox: Clamp mailbox output allocation to the payload size

CXL_MEM_SEND_COMMAND bounds the user's in.size to the mailbox payload size but leaves out.size unbounded, then cxl_mbox_cmd_ctor() calls kvzalloc(out.size). A large out.size drives a huge allocation, above INT_MAX it WARNs and taints, and with panic_on_warn=1 it panics.

The transport __cxl_pci_mbox_send_cmd() already clamps the response copy to min(out.size, payload_size, device len), so the output buffer is never written beyond payload_size. Clamp the allocation to payload_size too, matching the RAW path.

Fixes 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")

---
Changelog:

v1 -> v2:
- Correct the Fixes tag to 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")
- Drop the reproducer and sent it as a regression test in ndctl

Best regards,
Richard Cheng.

---
 drivers/cxl/core/mbox.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/cxl/core/mbox.c +1 -5
index 7c6c5b7450a5..d9cb02c9f72c 100644
@@ -380,11 +380,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd,
 		}
 	}
 
-	/* Prepare to handle a full payload for variable sized output */
-	if (out_size == CXL_VARIABLE_PAYLOAD)
-		mbox_cmd->size_out = cxl_mbox->payload_size;
-	else
-		mbox_cmd->size_out = out_size;
+	mbox_cmd->size_out = min_t(size_t, out_size, cxl_mbox->payload_size);
 
 	if (mbox_cmd->size_out) {
 		mbox_cmd->payload_out = kvzalloc(mbox_cmd->size_out, GFP_KERNEL);

base-commit: ef0c9f75a19532d7675384708fc8621e10850104

Signature · 1 line
2.43.0
#1 lore raw
Quoted 48 lines · Richard Cheng

CXL_MEM_SEND_COMMAND bounds the user's in.size to the mailbox payload size but leaves out.size unbounded, then cxl_mbox_cmd_ctor() calls kvzalloc(out.size). A large out.size drives a huge allocation, above INT_MAX it WARNs and taints, and with panic_on_warn=1 it panics.

The transport __cxl_pci_mbox_send_cmd() already clamps the response copy to min(out.size, payload_size, device len), so the output buffer is never written beyond payload_size. Clamp the allocation to payload_size too, matching the RAW path.

Fixes 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")

---
Changelog:

v1 -> v2:
- Correct the Fixes tag to 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")
- Drop the reproducer and sent it as a regression test in ndctl

Best regards,
Richard Cheng.

---
 drivers/cxl/core/mbox.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/cxl/core/mbox.c +1 -5
index 7c6c5b7450a5..d9cb02c9f72c 100644
@@ -380,11 +380,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd,
 		}
 	}
 
-	/* Prepare to handle a full payload for variable sized output */
-	if (out_size == CXL_VARIABLE_PAYLOAD)
-		mbox_cmd->size_out = cxl_mbox->payload_size;
-	else
-		mbox_cmd->size_out = out_size;
+	mbox_cmd->size_out = min_t(size_t, out_size, cxl_mbox->payload_size);
 
 	if (mbox_cmd->size_out) {
 		mbox_cmd->payload_out = kvzalloc(mbox_cmd->size_out, GFP_KERNEL);
Quoted 4 lines

base-commit: ef0c9f75a19532d7675384708fc8621e10850104

Signature · 1 line
2.43.0

Hi folks,

Just a gentle ping on this one, I've tweak the commit message and send ndctl patch for it.

Please take a look while you're available, thanks.

Best regards,
Richard Cheng

maintainer Applied
#2 lore raw
In reply to Richard Cheng
Quoted 10 lines · Richard Cheng

CXL_MEM_SEND_COMMAND bounds the user's in.size to the mailbox payload size but leaves out.size unbounded, then cxl_mbox_cmd_ctor() calls kvzalloc(out.size). A large out.size drives a huge allocation, above INT_MAX it WARNs and taints, and with panic_on_warn=1 it panics.

The transport __cxl_pci_mbox_send_cmd() already clamps the response copy to min(out.size, payload_size, device len), so the output buffer is never written beyond payload_size. Clamp the allocation to payload_size too, matching the RAW path.

Quoted 6 lines
Fixes 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")

Applied to next
8a13db9f899d

Quoted 33 lines

---
Changelog:

v1 -> v2:
- Correct the Fixes tag to 583fa5e71cae ("cxl/mem: Add basic IOCTL interface")
- Drop the reproducer and sent it as a regression test in ndctl

Best regards,
Richard Cheng.

---
 drivers/cxl/core/mbox.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/cxl/core/mbox.c +1 -5
index 7c6c5b7450a5..d9cb02c9f72c 100644
@@ -380,11 +380,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd,
 		}
 	}
 
-	/* Prepare to handle a full payload for variable sized output */
-	if (out_size == CXL_VARIABLE_PAYLOAD)
-		mbox_cmd->size_out = cxl_mbox->payload_size;
-	else
-		mbox_cmd->size_out = out_size;
+	mbox_cmd->size_out = min_t(size_t, out_size, cxl_mbox->payload_size);
 
 	if (mbox_cmd->size_out) {
 		mbox_cmd->payload_out = kvzalloc(mbox_cmd->size_out, GFP_KERNEL);

base-commit: ef0c9f75a19532d7675384708fc8621e10850104