mfd: ls2kbmc: mfd: ls2kbmc: Fix iomem pointer handling in video mode parsing
Subsystems (3)
Binbin Zhou
maintainerPATCH v2 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
---
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 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/mfd/ls2k-bmc-core.c +8 -2
@@ -427,14 +427,20 @@ 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;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 ,SZ_16M ); +if (!base) +return -ENOMEM ; + + mode =kmalloc (SZ_16M ,GFP_KERNEL );if (!mode)return -ENOMEM ; +memcpy_fromio (mode, base,SZ_16M );/* The resolution field starts with the flag "video=". */ if (!strncmp (mode,"video=" ,6 )) mode = mode +6 ;
base-commit: c454531af72e0df811600601413bb8d3d039ed08
Signature · 1 line
2.52.0
Quoted 54 lines · Binbin Zhou
Use pointers annotated with the
__iomemmarker for all iomem map calls, and creates a local copy of the mapped IO memory for future access in the code.memcpy_fromio()andmemcpy_toio()are used to read/write data from/to mapped IO memoryFixes 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")Reported-by kernel test robot <[email protected]>Signed-off-by Binbin Zhou <[email protected]>---
V2:
- Add the missingmemcpy_fromio();
- Drop the unnecessarybufvariable.Link to V1:
https://lore.kernel.org/all/[email protected]/drivers/mfd/ls2k-bmc-core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/mfd/ls2k-bmc-core.c +8
-2@@ -427,14 +427,20 @@ 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;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 ,SZ_16M ); +if (!base) +return -ENOMEM ; + + mode =kmalloc (SZ_16M ,GFP_KERNEL );if (!mode)return -ENOMEM ; +memcpy_fromio (mode, base,SZ_16M );/* The resolution field starts with the flag "video=". */ if (!strncmp (mode,"video=" ,6 )) mode = mode +6 ;base-commit:
c454531af72e0df811600601413bb8d3d039ed08--
2.52.0
Lee Jones
maintainerPlease consider these:
/* Sashiko Automation: Issues Found (4 Findings) */
Quoted 31 lines · Binbin Zhou
Use pointers annotated with the
__iomemmarker for all iomem map calls, and creates a local copy of the mapped IO memory for future access in the code.memcpy_fromio()andmemcpy_toio()are used to read/write data from/to mapped IO memoryFixes 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")Reported-by kernel test robot <[email protected]>Signed-off-by Binbin Zhou <[email protected]>---
V2:
- Add the missingmemcpy_fromio();
- Drop the unnecessarybufvariable.Link to V1:
https://lore.kernel.org/all/[email protected]/drivers/mfd/ls2k-bmc-core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/mfd/ls2k-bmc-core.c +1
-1@@ -427,14 +427,20 @@ 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;
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 439
Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +6
-1+void __iomem *base;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 ,SZ_16M ); +if (!base) +return -ENOMEM ; + + mode =kmalloc (SZ_16M ,GFP_KERNEL );
[Severity: High]
Will this 16MB allocation unconditionally fail on systems where KMALLOC_MAX_SIZE is smaller, such as 4MB with 4K pages?
Even if successful, is it necessary to synchronously copy 16MB of device I/O memory just to read a short resolution string?
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 443
Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +1
-0if (!mode)return -ENOMEM ; +memcpy_fromio (mode, base,SZ_16M );
[Severity: High]
This is a pre-existing issue, but since the buffer is now allocated on the heap via kmalloc() and copied directly from device memory, will string functions like strncmp() and strsep() read out-of-bounds if the 16MB region lacks a null terminator byte?
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 446
Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0
-0/* The resolution field starts with the flag "video=". */ if (!strncmp (mode,"video=" ,6 )) mode = mode +6 ;
[Severity: High]
Since mode was declared with __free(kfree), will advancing the pointer here cause kfree() to be called with a mid-buffer pointer and crash the allocator when ls2k_bmc_parse_mode() returns?
Quoted 5 lines
base-commit:
c454531af72e0df811600601413bb8d3d039ed08Signature · 1 line
2.52.0
Signature · 1 line
Lee Jones
Hi Lee:
Quoted 53 lines · Lee Jones
Please consider these:
/* Sashiko Automation: Issues Found (4 Findings) */
Quoted 31 lines · Binbin Zhou
Use pointers annotated with the
__iomemmarker for all iomem map calls, and creates a local copy of the mapped IO memory for future access in the code.memcpy_fromio()andmemcpy_toio()are used to read/write data from/to mapped IO memoryFixes 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")Reported-by kernel test robot <[email protected]>Signed-off-by Binbin Zhou <[email protected]>---
V2:
- Add the missingmemcpy_fromio();
- Drop the unnecessarybufvariable.Link to V1:
https://lore.kernel.org/all/[email protected]/drivers/mfd/ls2k-bmc-core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)drivers/mfd/ls2k-bmc-core.c +1
-1@@ -427,14 +427,20 @@ 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;drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 439
Quoted 10 lines
drivers/mfd/ls2k-bmc-core.c +6
-1+void __iomem *base;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 ,SZ_16M ); +if (!base) +return -ENOMEM ; + + mode =kmalloc (SZ_16M ,GFP_KERNEL );[Severity: High]
Will this 16MB allocation unconditionally fail on systems whereKMALLOC_MAX_SIZEis smaller, such as 4MB with 4K pages?
Quoted 2 lines
Even if successful, is it necessary to synchronously copy 16MB of device I/O memory just to read a short resolution string?
Emm, indeed, SZ_64 should be enough. I will also define the following macro:
/* The LS2K BMC display resolution string length store in PCI BAR0 */
#define LS2K_RESOLUTION_STR_LEN SZ_64
Quoted 14 lines
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 443
Quoted 6 lines
drivers/mfd/ls2k-bmc-core.c +1
-0if (!mode)return -ENOMEM ; +memcpy_fromio (mode, base,SZ_16M );[Severity: High]
This is a pre-existing issue, but since the buffer is now allocated on the heap viakmalloc()and copied directly from device memory, will string functions likestrncmp()andstrsep()read out-of-bounds if the 16MB region lacks a null terminator byte?
Quoted 8 lines
drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 446
Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0
-0/* The resolution field starts with the flag "video=". */ if (!strncmp (mode,"video=" ,6 )) mode = mode +6 ;[Severity: High]
Since mode was declared with__free(kfree), will advancing the pointer here causekfree()to be called with a mid-buffer pointer and crash the allocator whenls2k_bmc_parse_mode()returns?
Yes, I'll add a new variable pos and check the return value of strncmp(), as follows:
/* The resolution field starts with the flag "video=". */
if (strncmp(mode, "video=", 6)) {
dev_err(&pdev->dev, "Simpledrm resolution missing or
corrupt!\n");
return -EINVAL;
}
pos = mode + 6;
ret = kstrtoint(strsep(&pos, "x"), 10, &pd->width);........
Quoted 10 lines
Quoted 7 lines
base-commit:
c454531af72e0df811600601413bb8d3d039ed08--
2.52.0--
Lee Jones
--
Thanks.
Binbin