of/address: Fix NULL bus dereference in of_pci_range_parser_one()
Subsystems (2)
PATCH v3 1/1 of/address: Fix NULL bus dereference in of_pci_range_parser_one()
The bus matching rework made of_match_bus() return NULL for nodes with ranges/dma-ranges but no local #address-cells. parser_init() stored that NULL bus, and the range iterator later dereferenced it.
Reject such nodes in parser_init(), leaving an explicit empty iterator for callers that ignore the init return, and make of_dma_get_max_cpu_address() honour the init failure so a rejected node cannot clamp the DMA limit.
---
Changes in v3:
- Drop the of_range_count() hunk in of_dma_get_max_cpu_address(): it guarded a pre-existing corner, not this regression (Rob)
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Validate na/pna/ns in parser_init() with OF_CHECK_COUNTS() / OF_CHECK_ADDR_COUNT()
- Link to v1: https://lore.kernel.org/r/[email protected]
--- drivers/of/address.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/of/address.c +16 -6
@@ -753,6 +753,7 @@ EXPORT_SYMBOL(of_property_read_reg);static int parser_init (struct of_pci_range_parser *parser,struct device_node *node,const char *name) { +const __be32 *range;int rlen; parser->node = node;
@@ -761,12 +762,20 @@ static int parser_init(struct of_pci_range_parser *parser, parser->ns =of_bus_n_size_cells (node); parser->dma = !strcmp (name,"dma-ranges" ); parser->bus =of_match_bus (node); + parser->range =NULL ; + parser->end =NULL ; - parser->range =of_get_property (node, name, &rlen); -if (parser->range ==NULL ) + range =of_get_property (node, name, &rlen); +if (!range)return -ENOENT ; - parser->end = parser->range + rlen /sizeof (__be32); +if (!parser->bus || + !OF_CHECK_COUNTS (parser->na, parser->ns) || + !OF_CHECK_ADDR_COUNT (parser->pna)) +return -EINVAL ; + + parser->range = range; + parser->end = range + rlen /sizeof (__be32);return 0 ; }
@@ -792,7 +801,7 @@ struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,int na = parser->na;int ns = parser->ns;int np = parser->pna + na + ns; -int busflag_na = parser->bus->flag_cells; +int busflag_na;if (!range)return NULL ;
@@ -800,6 +809,8 @@ struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,if (!parser->range || parser->range + np > parser->end)return NULL ; + busflag_na = parser->bus->flag_cells; + range->flags = parser->bus->get_flags (parser->range); range->bus_addr =of_read_number (parser->range + busflag_na, na - busflag_na);
@@ -976,8 +987,7 @@ phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np) np = of_root; ranges =of_get_property (np,"dma-ranges" , &len); -if (ranges && len) { -of_dma_range_parser_init (&parser, np); +if (ranges && len && !of_dma_range_parser_init (&parser, np)) {for_each_of_range (&parser, &range)if (range.cpu_addr + range.size > cpu_end) cpu_end = range.cpu_addr + range.size -1 ;
---
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
change-id: 20260727-of-range-parser-null-bus-9f87bbf440ee
Best regards,
Signature · 1 line
Carlo Caione <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] An integer overflow in the calculation of np (np = parser->pna + na + ns) can be triggered by a maliciously crafted Device Tree with a large positive #size-cells value (e.g., 0x7FFFFFFE). This results in a negative np which bypasses the bounds check parser->range + np > parser->end. --
--- Patch [1]: [PATCH v3] of/address: Fix NULL bus dereference in of_pci_range_parser_one() --- commit 958bc6596a6b1a98e35e89a326c87639d08d1a2b
Author: Carlo Caione <[email protected]>
of/address: Fix NULL bus dereference in of_pci_range_parser_one()
The bus matching rework made of_match_bus() return NULL for nodes with ranges/dma-ranges but no local #address-cells. parser_init() stored that NULL bus, and the range iterator later dereferenced it.
Quoted 4 lines
drivers/of/address.c +0
-0
[ ... ]
drivers/of/address.c struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, · line 810
Quoted 12 lines
drivers/of/address.c +1
-1@@ -792,7 +801,7 @@ struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,int na = parser->na;int ns = parser->ns;int np = parser->pna + na + ns; -int busflag_na = parser->bus->flag_cells; +int busflag_na;if (!range)return NULL ;@@ -800,6 +809,8 @@ struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,if (!parser->range || parser->range + np > parser->end)return NULL ;
[Severity: High]
This isn't a bug introduced by this patch, but can this integer calculation overflow?
If a maliciously crafted Device Tree with an extremely large positive #size-cells property (e.g., 0x7FFFFFFE) is parsed, the calculation np = parser->pna + na + ns could overflow a 32-bit signed integer.
This would result in a negative np value, bypassing the bounds check parser->range + np > parser->end.
Because np is negative, parser->range + np performs negative pointer arithmetic. Does this then cause an out-of-bounds memory read when calling of_read_number(..., ns), running a loop repeatedly reading memory until it causes a page fault?
Quoted 5 lines
drivers/of/address.c +2
-0+ busflag_na = parser->bus->flag_cells; + range->flags = parser->bus->get_flags (parser->range); range->bus_addr =of_read_number (parser->range + busflag_na, na - busflag_na);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] An integer overflow in the calculation ofnp(np = parser->pna + na + ns) can be triggered by a maliciously crafted Device Tree with a large positive#size-cellsvalue (e.g.,0x7FFFFFFE). This results in a negativenpwhich bypasses the bounds checkparser->range + np > parser->end.
[...]
Quoted 3 lines
[Severity: High]
This isn't a bug introduced by this patch, but can this integer calculation overflow?
Not going to endless-ly fix pre-existing issues.
--
Carlo Caione
Rob Herring
maintainerQuoted 7 lines · Carlo Caione
Quoted 4 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] An integer overflow in the calculation ofnp(np = parser->pna + na + ns) can be triggered by a maliciously crafted Device Tree with a large positive#size-cellsvalue (e.g.,0x7FFFFFFE). This results in a negativenpwhich bypasses the bounds checkparser->range + np > parser->end.
Quoted 6 lines
[...]
Quoted 3 lines
[Severity: High]
This isn't a bug introduced by this patch, but can this integer calculation overflow?Not going to endless-ly fix pre-existing issues.
Agreed. I'm not sure this one is worth "fixing".
Rob
Quoted 18 lines · Carlo Caione
The bus matching rework made
of_match_bus()return NULL for nodes with ranges/dma-ranges but no local #address-cells.parser_init()stored that NULL bus, and the range iterator later dereferenced it.Reject such nodes in
parser_init(), leaving an explicit empty iterator for callers that ignore the init return, and makeof_dma_get_max_cpu_address()honour the init failure so a rejected node cannot clamp the DMA limit.Fixes 64ee3cf096ac ("of/address: Rework bus matching to avoid warnings")Signed-off-by Carlo Caione <[email protected]>---
Changes in v3:
- Drop theof_range_count()hunk inof_dma_get_max_cpu_address(): it guarded a pre-existing corner, not this regression (Rob)
- Link to v2: https://lore.kernel.org/r/[email protected]
Quoted 8 lines
Changes in v2:
- Validate na/pna/ns inparser_init()withOF_CHECK_COUNTS()/OF_CHECK_ADDR_COUNT()
- Link to v1: https://lore.kernel.org/r/[email protected]--- drivers/of/address.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
Applied, thanks!