cxl/test: Zero out LSA backing memory to avoid leaking to user
Dave Jiang
maintainerPATCH 1/1 cxl/test: Zero out LSA backing memory to avoid leaking to user
Memory through vmalloc() is not zeroed out. When this memory is copied into output payload, it leaks memory content to user. Use vzalloc() instead to zero out the memory.
--- tools/testing/cxl/test/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/cxl/test/mem.c +1 -1
@@ -1724,7 +1724,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)return -ENOMEM ;dev_set_drvdata (dev, mdata); - mdata->lsa =vmalloc (LSA_SIZE ); + mdata->lsa =vzalloc (LSA_SIZE );if (!mdata->lsa)return -ENOMEM ; mdata->fw =vmalloc (FW_SIZE );
base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8
prerequisite-patch-id: a09fa1bd37ae9ee663117e196f1b8c49c55365e4
prerequisite-patch-id: 75202ea281daa3b091090369ada6eabcaefc6671
Signature · 1 line
2.54.0
[email protected]
AI reviewThank 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:
- [High] Integer overflow in mock mailbox command bounds checking allows out-of-bounds memory reads and writes.
- [High] Uninitialized vmalloc memory in mdata->fw is processed by SHA-256, causing a KMSAN use-of-uninitialized-value bug and theoretical info-leak.
- [High] Race condition between character device exposure and probe completion leads to Use-After-Free in delayed work.
--
commit 8a5efeb3136cc5d1dd28b13e54923a6be8c62677
Author: Dave Jiang <[email protected]>
cxl/test: Zero out LSA backing memory to avoid leaking to user
Memory through vmalloc() is not zeroed out. When this memory is copied into output payload, it leaks memory content to user. Use vzalloc() instead to zero out the memory.
tools/testing/cxl/test/mem.c static int cxl_mock_mem_probe(struct platform_device *pdev) · line 1709
Quoted 13 lines
tools/testing/cxl/test/mem.c +1
-1@@ -1703,7 +1703,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)return -ENOMEM ;dev_set_drvdata (dev, mdata); - mdata->lsa =vmalloc (LSA_SIZE ); + mdata->lsa =vzalloc (LSA_SIZE );if (!mdata->lsa)return -ENOMEM ; mdata->fw =vmalloc (FW_SIZE );
[Severity: High]
This is a pre-existing issue, but since mdata->lsa is being updated to use vzalloc() here, should mdata->fw also be updated?
If a user issues a CXL_MBOX_OP_TRANSFER_FW command with a large offset but no payload, it can artificially inflate mdata->fw_size without initializing the memory.
When reading the fw_buf_checksum sysfs attribute:
fw_buf_checksum_show()
sha256(mdata->fw, mdata->fw_size, hash);
Does this process uninitialized memory and trigger a KMSAN use-of-uninitialized-value, acting as a side-channel information leak?
[Severity: High]
This isn't a bug introduced by this patch, but could the 32-bit addition in the mock mailbox command bounds checking overflow?
In mock_get_lsa() and mock_set_lsa():
if (offset + length > LSA_SIZE)
If a large offset is provided alongside a small length, will the 32-bit math wrap around and bypass the bounds check?
On 64-bit systems, the 32-bit offset is zero-extended during memcpy(). Can this allow an attacker with mailbox access to read or write out-of-bounds kernel memory at offsets ahead of the target buffer?
Similarly, in mock_get_log():
if (offset + length > sizeof(mock_cel))
Could this also overflow and bypass the limit?
[Severity: High]
This is a pre-existing issue, but is there a race condition between character device exposure and probe completion in cxl_mock_mem_probe()?
devm_cxl_add_memdev() registers the character device and exposes it to userspace before all probe setup steps are finished:
cxl_mock_mem_probe() {
...
cxlmd = devm_cxl_add_memdev(cxlds, NULL);
if (IS_ERR(cxlmd))
return PTR_ERR(cxlmd);
rc = devm_cxl_setup_fw_upload(&pdev->dev, mds);
...
}If userspace immediately opens the device and issues a CXL_MBOX_OP_SANITIZE command, it schedules the mds->security.poll_dwork delayed work.
If a subsequent probe step like devm_cxl_setup_fw_upload() fails, devres automatically destroys the mds state.
Since devm_cxl_sanitize_setup_notifier() either hasn't executed or failed, the teardown notifier that cancels poll_dwork is never registered.
Will the pending work execute later, accessing the freed mds structure and causing a use-after-free?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · Dave Jiang
Memory through
vmalloc()is not zeroed out. When this memory is copied into output payload, it leaks memory content to user. Usevzalloc()instead to zero out the memory.
I had wondered if the mdata->fw allocation was a similar risk, and since it is not, this LGTM.
Quoted 28 lines
Suggested-by sashiko-botFixes 7d3eb23c4ccf ("tools/testing/cxl: Introduce a mock memory device + driver")Signed-off-by Dave Jiang <[email protected]>--- tools/testing/cxl/test/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/cxl/test/mem.c +1
-1@@ -1724,7 +1724,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)return -ENOMEM ;dev_set_drvdata (dev, mdata); - mdata->lsa =vmalloc (LSA_SIZE ); + mdata->lsa =vzalloc (LSA_SIZE );if (!mdata->lsa)return -ENOMEM ; mdata->fw =vmalloc (FW_SIZE );base-commit:
e43ffb69e0438cddd72aaa30898b4dc446f664f8
prerequisite-patch-id:a09fa1bd37ae9ee663117e196f1b8c49c55365e4
prerequisite-patch-id:75202ea281daa3b091090369ada6eabcaefc6671Signature · 1 line
2.54.0
Quoted 8 lines · Dave Jiang
Memory through
vmalloc()is not zeroed out. When this memory is copied into output payload, it leaks memory content to user. Usevzalloc()instead to zero out the memory.Suggested-by sashiko-botFixes 7d3eb23c4ccf ("tools/testing/cxl: Introduce a mock memory device + driver")Signed-off-by Dave Jiang <[email protected]>
Applied to cxl/nextb6d9860c11b8f3331c08f650ccf5ff24c61745e2
Quoted 21 lines
--- tools/testing/cxl/test/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/cxl/test/mem.c +1
-1@@ -1724,7 +1724,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)return -ENOMEM ;dev_set_drvdata (dev, mdata); - mdata->lsa =vmalloc (LSA_SIZE ); + mdata->lsa =vzalloc (LSA_SIZE );if (!mdata->lsa)return -ENOMEM ; mdata->fw =vmalloc (FW_SIZE );base-commit:
e43ffb69e0438cddd72aaa30898b4dc446f664f8
prerequisite-patch-id:a09fa1bd37ae9ee663117e196f1b8c49c55365e4
prerequisite-patch-id:75202ea281daa3b091090369ada6eabcaefc6671