Skip to contentSkip to conversation

mfd: ls2kbmc: mfd: ls2kbmc: Fix iomem pointer handling in video mode parsing

PATCH In review Binbin Zhou 3 participants 3 messages Jul 6

Subsystems (4)
lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

PATCH v3 1/1 mfd: ls2kbmc: mfd: ls2kbmc: Fix iomem pointer handling in video mode parsing

Use pointers annotated with the __iomem marker for all iomem map calls, and creates a local copy of the mapped IO memory for future access in the code. memcpy_fromio() and memcpy_toio() are used to read/write data from/to mapped IO memory

---
V3:
- Reduce the range of ioremap from SZ_16M to SZ_64;
- Define a new variable pos to iterate through the string;
- Add failure handling for strncmp().

Link to V2:
https://lore.kernel.org/all/[email protected]/

V2:
- Add the missing memcpy_fromio();
- Drop the unnecessary buf variable.

Link to V1:
https://lore.kernel.org/all/[email protected]/

 drivers/mfd/ls2k-bmc-core.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
drivers/mfd/ls2k-bmc-core.c +22 -7
index 408056bfb2fe..a2aee2131529 100644
@@ -66,6 +66,9 @@
 /* Maximum time to wait for U-Boot and DDR to be ready with ms. */
 #define LS2K_BMC_RESET_WAIT_TIME	10000
 
+/* The length of the LS2K BMC display resolution string stored in PCI BAR0 */
+#define LS2K_RESOLUTION_STR_LEN		SZ_64
+
 /* It's an experience value */
 #define LS7A_BAR0_CHECK_MAX_TIMES	2000
 
@@ -427,27 +430,39 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	char *mode __free(kfree) = NULL;
+	void __iomem *base;
+	char *pos = NULL;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
+	base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
+			    LS2K_RESOLUTION_STR_LEN);
+	if (!base)
+		return -ENOMEM;
+
+	mode = kmalloc(LS2K_RESOLUTION_STR_LEN, GFP_KERNEL);
 	if (!mode)
 		return -ENOMEM;
 
+	memcpy_fromio(mode, base, LS2K_RESOLUTION_STR_LEN);
+
 	/* The resolution field starts with the flag "video=". */
-	if (!strncmp(mode, "video=", 6))
-		mode = mode + 6;
+	if (strncmp(mode, "video=", 6)) {
+		dev_err(&pdev->dev, "Simpledrm resolution missing or corrupt!\n");
+		return -EINVAL;
+	}
 
-	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+	pos = mode + 6;
+	ret = kstrtoint(strsep(&pos, "x"), 10, &pd->width);
 	if (ret)
 		return ret;
 
-	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+	ret = kstrtoint(strsep(&pos, "-"), 10, &pd->height);
 	if (ret)
 		return ret;
 
-	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
+	ret = kstrtoint(strsep(&pos, "@"), 10, &depth);
 	if (ret)
 		return ret;
 

base-commit: d5d2d7a8d8be18681a0864f58e3875f1c639e11c

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

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

New issues:
- [High] Out-of-bounds read and write due to missing null termination on kmalloc buffer.
- [Medium] Implicit breakage of backwards compatibility by strictly requiring 'video=' prefix.

Pre-existing issues:
- [High] NULL pointer dereference in resolution string parsing.
- [High] 10-second busy-wait inside stop_machine() callback.
- [High] Use-after-free of ddata on probe failure or device unbind.
- [High] Shared interrupt handler unconditionally returns IRQ_HANDLED. --

commit 6f5ec8b16462761d3de782394ef96b6a09a36913
Author: Binbin Zhou <[email protected]>

mfd: ls2kbmc: mfd: ls2kbmc: Fix iomem pointer handling in video mode parsing

This commit fixes iomem pointer handling in the video mode parsing logic. It uses __iomem annotated pointers for all iomem map calls and creates a local copy of the mapped IO memory using memcpy_fromio() and memcpy_toio().

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index 408056bfb2fe7..a2aee21315292 100644

[ ... ]

Quoted 20 lines
drivers/mfd/ls2k-bmc-core.c +9 -2
@@ -427,27 +430,39 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	char *mode __free(kfree) = NULL;
+	void __iomem *base;
+	char *pos = NULL;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
+	base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
+			    LS2K_RESOLUTION_STR_LEN);
+	if (!base)
+		return -ENOMEM;
+
+	mode = kmalloc(LS2K_RESOLUTION_STR_LEN, GFP_KERNEL);
 	if (!mode)
 		return -ENOMEM;

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 454

Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +6 -2
 
+	memcpy_fromio(mode, base, LS2K_RESOLUTION_STR_LEN);
+
 	/* The resolution field starts with the flag "video=". */
-	if (!strncmp(mode, "video=", 6))
-		mode = mode + 6;
+	if (strncmp(mode, "video=", 6)) {
+		dev_err(&pdev->dev, "Simpledrm resolution missing or corrupt!\n");
+		return -EINVAL;
+	}

[Severity: Medium]
Does this explicit failure break compatibility with older firmware?

Previously, the code bypassed advancing the pointer but didn't return -EINVAL if the raw resolution string was provided without the "video=" prefix.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 457

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +2 -1
-	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+	pos = mode + 6;
+	ret = kstrtoint(strsep(&pos, "x"), 10, &pd->width);

[Severity: High]
Since mode is allocated via kmalloc() and not zeroed, and memcpy_fromio() doesn't guarantee a null terminator, could strsep() scan past the end of the allocation if the hardware string lacks delimiters?

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 461

Quoted 5 lines
drivers/mfd/ls2k-bmc-core.c +1 -1
 	if (ret)
 		return ret;
 
-	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+	ret = kstrtoint(strsep(&pos, "-"), 10, &pd->height);

[Severity: High]
This is a pre-existing issue, but if the resolution string lacks an expected delimiter (like "x"), strsep() will return the remaining string and set pos to NULL.

Will the subsequent call to strsep(&pos, "-") then return NULL, causing kstrtoint() to unconditionally dereference a NULL pointer?

I also noticed a few other pre-existing issues in this driver that might need attention:

[Severity: High]
This isn't a bug introduced by this patch, but is it safe to execute a 10 second mdelay() in ls2k_bmc_recover_pci_data()?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_recover_pci_data() {
    ...
    /* Waiting for U-Boot and DDR ready */
    mdelay(LS2K_BMC_RESET_WAIT_TIME);
    if (!ls2k_bmc_bar0_addr_is_set(parent))
        return false;
    ...
}

Because this is called from a stop_machine() callback, does this freeze the entire system for 10 seconds with interrupts globally disabled on all CPUs, potentially triggering hardware watchdogs and soft lockups?

[Severity: High]
This is a pre-existing issue, but does probe failure or device unbind leave a use-after-free window for the ddata structure?

In ls2k_bmc_probe(), if ls2k_bmc_parse_mode() fails after ls2k_bmc_init() has already requested the IRQ:

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_probe() {
    ...
    ret = ls2k_bmc_init(ddata);
    if (ret)
        return ret;

    ret = ls2k_bmc_parse_mode(dev, &pd);
    if (ret)
        return ret;
    ...
}

If the IRQ fires and schedules bmc_reset_work, devres will free the IRQ and the ddata structure on failure, but the workqueue is not canceled.

Will ls2k_bmc_events_fn() then execute and access the freed ddata?

[Severity: High]
This isn't a bug introduced by this patch, but does the shared interrupt handler mask spurious interrupts or break other devices sharing the same PCI IRQ line?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_interrupt() {
    ...
    /* Skip interrupt in LS2K_BMC_INT_INTERVAL */
    if (time_after(jiffies, last_jiffies + LS2K_BMC_INT_INTERVAL)) {
        schedule_work(&ddata->bmc_reset_work);
        last_jiffies = jiffies;
    }

    return IRQ_HANDLED;
}

Since this handler is registered with IRQF_SHARED, unconditionally returning IRQ_HANDLED without checking hardware registers tricks the kernel into assuming the interrupt was successfully handled.

Does this deprive other devices on the shared line from processing their interrupts?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Binbin Zhou

Hi, Binbin,

Quoted 47 lines · Binbin Zhou

Use pointers annotated with the __iomem marker for all iomem map calls, and creates a local copy of the mapped IO memory for future access in the code. memcpy_fromio() and memcpy_toio() are used to read/write data from/to mapped IO memory

---
V3:
- Reduce the range of ioremap from SZ_16M to SZ_64;
- Define a new variable pos to iterate through the string;
- Add failure handling for strncmp().

Link to V2:
https://lore.kernel.org/all/[email protected]/

V2:
- Add the missing memcpy_fromio();
- Drop the unnecessary buf variable.

Link to V1:
https://lore.kernel.org/all/[email protected]/

 drivers/mfd/ls2k-bmc-core.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
drivers/mfd/ls2k-bmc-core.c +3 -0
index 408056bfb2fe..a2aee2131529 100644
@@ -66,6 +66,9 @@
 /* Maximum time to wait for U-Boot and DDR to be ready with ms. */
 #define LS2K_BMC_RESET_WAIT_TIME       10000
 
+/* The length of the LS2K BMC display resolution string stored in PCI BAR0 */
+#define LS2K_RESOLUTION_STR_LEN                SZ_64
+
 /* It's an experience value */
 #define LS7A_BAR0_CHECK_MAX_TIMES      2000
 

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 435

Quoted 7 lines
drivers/mfd/ls2k-bmc-core.c +3 -1
@@ -427,27 +430,39 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-       char *mode;
+       char *mode __free(kfree) = NULL;
+       void __iomem *base;
+       char *pos = NULL;

Don't need to initialize, and you can define it in the same line as mode.

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 440

Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +2 -1
        int depth, ret;
 
        /* The last 16M of PCI BAR0 is used to store the resolution string. */
-       mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
+       base = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M,
+                           LS2K_RESOLUTION_STR_LEN);

IOREMAP is page based, so just use PAGE_SIZE rather than LS2K_RESOLUTION_STR_LEN.

Huacai

Quoted 38 lines
drivers/mfd/ls2k-bmc-core.c +14 -5
+       if (!base)
+               return -ENOMEM;
+
+       mode = kmalloc(LS2K_RESOLUTION_STR_LEN, GFP_KERNEL);
        if (!mode)
                return -ENOMEM;
 
+       memcpy_fromio(mode, base, LS2K_RESOLUTION_STR_LEN);
+
        /* The resolution field starts with the flag "video=". */
-       if (!strncmp(mode, "video=", 6))
-               mode = mode + 6;
+       if (strncmp(mode, "video=", 6)) {
+               dev_err(&pdev->dev, "Simpledrm resolution missing or corrupt!\n");
+               return -EINVAL;
+       }
 
-       ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+       pos = mode + 6;
+       ret = kstrtoint(strsep(&pos, "x"), 10, &pd->width);
        if (ret)
                return ret;
 
-       ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+       ret = kstrtoint(strsep(&pos, "-"), 10, &pd->height);
        if (ret)
                return ret;
 
-       ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
+       ret = kstrtoint(strsep(&pos, "@"), 10, &depth);
        if (ret)
                return ret;
 

base-commit: d5d2d7a8d8be18681a0864f58e3875f1c639e11c --
2.52.0