Skip to contentSkip to conversation

cxl/test: Fix integer overflow in mock LSA bounds checks

PATCH In review Dave Jiang 4 participants 4 messages Jun 5 – Jun 10 CXL

lore
Share & tools
lore

Dave Jiang

author maintainer
#0 lore raw

PATCH 1/1 cxl/test: Fix integer overflow in mock LSA bounds checks

Pre-existing issue discovered by sashiko-bot.

mock_get_lsa() and mock_set_lsa() validate the requested LSA range with "offset + length > LSA_SIZE". Both offset and length are u32 and, in mock_get_lsa(), both are taken directly from the user-supplied payload. The addition is evaluated modulo 2^32, so a large offset combined with a small length wraps around and passes the check.

Rewrite the checks to compare offset and length against LSA_SIZE without overflowing by subtracting offset from LSA_SIZE after bounding offset.

Suggested-by sashiko-bot
Fixes 7d3eb23c4ccf ("tools/testing/cxl: Introduce a mock memory device + driver")
Assisted-by Claude:claude-opus-4-8
---
 tools/testing/cxl/test/mem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/cxl/test/mem.c +2 -2
index 5d812b10fbc8..f9b11207ae60 100644
@@ -1063,7 +1063,7 @@ static int mock_get_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(get_lsa->offset);
 	length = le32_to_cpu(get_lsa->length);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 	if (length > cmd->size_out)
 		return -EINVAL;
@@ -1083,7 +1083,7 @@ static int mock_set_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(set_lsa->offset);
 	length = cmd->size_in - sizeof(*set_lsa);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 
 	memcpy(lsa + offset, &set_lsa->data[0], length);

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8
prerequisite-patch-id: 2230e8cb1f5da168992ad92776043f6632681665

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Integer underflow in mock_transfer_fw() allows massive out-of-bounds memory write and kernel panic.
- [High] Unpatched identical integer overflow in mock_get_log() bounds check allows out-of-bounds read and kernel crash.
- [High] Uninitialized memory leak via vmalloc() in mock_get_lsa().
--

commit b6be4918eaf54ec33c636f5e1951da3320c032b5
Author: Dave Jiang <[email protected]>
Subject: cxl/test: Fix integer overflow in mock LSA bounds checks

mock_get_lsa() and mock_set_lsa() validate the requested LSA range with "offset + length > LSA_SIZE". The addition is evaluated modulo 2^32, so a large offset combined with a small length wraps around and passes the check. This commit rewrites the checks to avoid overflow.

tools/testing/cxl/test/mem.c static int mock_get_lsa(struct cxl_mockmem_data *mdata, · line 1059

Quoted 13 lines
tools/testing/cxl/test/mem.c +1 -1
index 271c7ad8cc32c..f27a83da738fc 100644
@@ -1053,7 +1053,7 @@ static int mock_get_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(get_lsa->offset);
 	length = le32_to_cpu(get_lsa->length);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 	if (length > cmd->size_out)
 		return -EINVAL;

[Severity: High]
This isn't a bug introduced by this patch, but the lsa memory buffer returned shortly after this bounds check contains uninitialized kernel memory.

In cxl_mock_mem_probe(), the memory for the mock Label Storage Area (mdata->lsa) is allocated using vmalloc():

tools/testing/cxl/test/mem.c:cxl_mock_mem_probe() {
    ...
    mdata->lsa = vmalloc(LSA_SIZE);
    ...
}

Unlike vzalloc(), vmalloc() does not zero the allocated memory. When mock_get_lsa() uses memcpy() to copy this memory into the output payload, it leaks up to 128KB of whatever was previously stored in those physical pages by the kernel to userspace.

Does this code leak uninitialized memory to userspace? Should vmalloc() be changed to vzalloc()?

[Severity: High]
This is a pre-existing issue, but mock_get_log() has an identical unpatched integer overflow.

Both offset and length are user-controlled u32 values. The bounds check is vulnerable to 32-bit integer wrap-around:

tools/testing/cxl/test/mem.c:mock_get_log() {
    ...
    u32 offset = le32_to_cpu(gl->offset);
    u32 length = le32_to_cpu(gl->length);
    ...
    if (offset + length > sizeof(mock_cel))
        return -EINVAL;
    ...
    memcpy(cmd->payload_out, data + offset, length);
    ...
}

A large offset combined with a small length wraps around, bypassing the check and causing an out-of-bounds read during the memcpy().

Should mock_get_log() be updated with the same bounds checking logic?

tools/testing/cxl/test/mem.c static int mock_set_lsa(struct cxl_mockmem_data *mdata, · line 1079

Quoted 9 lines
tools/testing/cxl/test/mem.c +1 -1
@@ -1073,7 +1073,7 @@ static int mock_set_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(set_lsa->offset);
 	length = cmd->size_in - sizeof(*set_lsa);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 
 	memcpy(lsa + offset, &set_lsa->data[0], length);

[Severity: Critical]
This isn't a bug introduced by this patch, but mock_transfer_fw() suffers from a similar 64-bit integer wrap-around that can bypass bounds checks.

The length is calculated without verifying that cmd->size_in >= sizeof(*transfer):

tools/testing/cxl/test/mem.c:mock_transfer_fw() {
    ...
    size_t offset, length;

    offset = le32_to_cpu(transfer->offset) * CXL_FW_TRANSFER_ALIGNMENT;
    length = cmd->size_in - sizeof(*transfer);
    if (offset + length > FW_SIZE)
        return -EINVAL;
    ...
    memcpy(fw + offset, transfer->data, length);
    ...
}

If userspace triggers this with a small size_in (e.g., 8 bytes), the size_t (64-bit) length underflows to a massive value. If offset is carefully chosen, offset + length wraps around in 64-bit arithmetic to a small value, passing the bounds check. The function then attempts to memcpy() an extremely large number of bytes, causing a kernel panic.

Can this check be tightened to ensure cmd->size_in is large enough to prevent the underflow?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Dave Jiang
Quoted 10 lines · Dave Jiang

Pre-existing issue discovered by sashiko-bot.

mock_get_lsa() and mock_set_lsa() validate the requested LSA range with "offset + length > LSA_SIZE". Both offset and length are u32 and, in mock_get_lsa(), both are taken directly from the user-supplied payload. The addition is evaluated modulo 2^32, so a large offset combined with a small length wraps around and passes the check.

Rewrite the checks to compare offset and length against LSA_SIZE without overflowing by subtracting offset from LSA_SIZE after bounding offset.

Above is a mouthful, is it more simply...
Rewrite the checks to first bound offset, then compare length against the remaining LSA size.

With a commit msg update upon merge...

Quoted 38 lines
Suggested-by sashiko-bot
Fixes 7d3eb23c4ccf ("tools/testing/cxl: Introduce a mock memory device + driver")
Assisted-by Claude:claude-opus-4-8
---
 tools/testing/cxl/test/mem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/cxl/test/mem.c +2 -2
index 5d812b10fbc8..f9b11207ae60 100644
@@ -1063,7 +1063,7 @@ static int mock_get_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(get_lsa->offset);
 	length = le32_to_cpu(get_lsa->length);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 	if (length > cmd->size_out)
 		return -EINVAL;
@@ -1083,7 +1083,7 @@ static int mock_set_lsa(struct cxl_mockmem_data *mdata,
 		return -EINVAL;
 	offset = le32_to_cpu(set_lsa->offset);
 	length = cmd->size_in - sizeof(*set_lsa);
-	if (offset + length > LSA_SIZE)
+	if (offset > LSA_SIZE || length > LSA_SIZE - offset)
 		return -EINVAL;
 
 	memcpy(lsa + offset, &set_lsa->data[0], length);

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8
prerequisite-patch-id: 2230e8cb1f5da168992ad92776043f6632681665

Signature · 1 line
2.54.0
#3 lore raw
Quoted 13 lines · Alison Schofield
Quoted 10 lines · Dave Jiang

Pre-existing issue discovered by sashiko-bot.

mock_get_lsa() and mock_set_lsa() validate the requested LSA range with "offset + length > LSA_SIZE". Both offset and length are u32 and, in mock_get_lsa(), both are taken directly from the user-supplied payload. The addition is evaluated modulo 2^32, so a large offset combined with a small length wraps around and passes the check.

Rewrite the checks to compare offset and length against LSA_SIZE without overflowing by subtracting offset from LSA_SIZE after bounding offset.

Quoted 8 lines

Above is a mouthful, is it more simply...
Rewrite the checks to first bound offset, then compare length against the remaining LSA size.

With a commit msg update upon merge...

I am getting ready to travel and I will address this when I return in about two weeks. Thank you for understanding.