of: address: Unify resource bounds overflow checking
PATCH 1/1 of: address: Unify resource bounds overflow checking
The members "start" and "end" of struct resource are of type "resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.
Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)
drivers/of/address.c +26 -19
@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{ +u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ; + + r->start = start; + r->end = end; + +return 0 ; +} +/* * of_pci_range_to_resource - Create a resource from an of_pci_range * @range: the PCI range that describes the resource
@@ -216,6 +235,7 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,int of_pci_range_to_resource (struct of_pci_range *range,struct device_node *np,struct resource *res) { +u64 start;int err; res->flags = range->flags; res->parent = res->child = res->sibling =NULL ;
@@ -232,18 +252,11 @@ int of_pci_range_to_resource(struct of_pci_range *range, err = -EINVAL ; goto invalid_range; } - res->start = port; + start = port; }else { -if ((sizeof (resource_size_t) <8 ) && -upper_32_bits (range->cpu_addr)) { - err = -EINVAL ; - goto invalid_range; - } - - res->start = range->cpu_addr; + start = range->cpu_addr; } - res->end = res->start + range->size -1 ; -return 0 ; +return __of_address_resource_bounds (res, start, range->size); invalid_range: res->start = (resource_size_t )OF_BAD_ADDR ;
@@ -259,8 +272,8 @@ EXPORT_SYMBOL(of_pci_range_to_resource);* @res: pointer to a valid resource that will be updated to * reflect the values contained in the range. * -* Returns ENOENT if the entry is not found or EINVAL if the range cannot be -* converted to resource. +* Returns -ENOENT if the entry is not found or -EOVERFLOW if the range +* cannot be converted to resource. */ int of_range_to_resource (struct device_node *np,int index,struct resource *res) {
@@ -1062,16 +1075,10 @@ static int __of_address_to_resource(struct device_node *dev, int index, int bar_if (of_mmio_is_nonposted (dev)) flags |=IORESOURCE_MEM_NONPOSTED ; -if (overflows_type (taddr, r->start)) -return -EOVERFLOW ; - r->start = taddr; -if (overflows_type (taddr + size -1 , r->end)) -return -EOVERFLOW ; - r->end = taddr + size -1 ; r->flags = flags; r->name = name ? name : dev->full_name; -return 0 ; +return __of_address_resource_bounds (r, taddr, size); }/**
---
base-commit: 20e5e74b91e99d57059d783e910d08a65573458f
change-id: 20240906-of-address-overflow-06b16d785ba7
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Quoted 4 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.
Quoted 8 lines
Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)
Applied, thanks!
Quoted 22 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{
drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, · line 212
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;
This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
The diff below fixes it for me.
It leaves r.end == r.start, which is fine in my case, because the code only uses r.start.
And it seems more sane than the old code which would return end = start - 1, for zero sized resources.
cheers
drivers/of/address.c +1 -3
@@ -204,9 +204,7 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)if (overflows_type (start, r->start))return -EOVERFLOW ; -if (size ==0 ) -return -EOVERFLOW ; -if (check_add_overflow (end, size -1 , &end)) +if (size >0 &&check_add_overflow (end, size -1 , &end))return -EOVERFLOW ;if (overflows_type (end, r->end))return -EOVERFLOW ;
Rob Herring
maintainerQuoted 39 lines · Michael Ellerman
Quoted 22 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, · line 212
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
Quoted 1 line
The diff below fixes it for me.
I fixed it up with your change.
Quoted 25 lines
It leaves r.end == r.start, which is fine in my case, because the code only uses r.start.
And it seems more sane than the old code which would return end = start - 1, for zero sized resources.
cheers
drivers/of/address.c +1
-3@@ -204,9 +204,7 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)if (overflows_type (start, r->start))return -EOVERFLOW ; -if (size ==0 ) -return -EOVERFLOW ; -if (check_add_overflow (end, size -1 , &end)) +if (size >0 &&check_add_overflow (end, size -1 , &end))return -EOVERFLOW ;if (overflows_type (end, r->end))return -EOVERFLOW ;
Quoted 39 lines · Rob Herring
Quoted 38 lines · Michael Ellerman
Quoted 22 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, · line 212
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
Quoted 3 lines
Quoted 1 line
The diff below fixes it for me.
I fixed it up with your change.
Thanks.
cheers
Hi,
Quoted 38 lines
Quoted 38 lines
Quoted 22 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, · line 212
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
Quoted 3 lines
Quoted 1 line
The diff below fixes it for me.
I fixed it up with your change.
This commit is breaking Ethernet functionality on the TI AM57xx platform due to zero byte SRAM block size allocation during initialization. Prior to this patch, zero byte block sizes were handled properly.
The issue is with the following line of code:
if (size && check_add_overflow(end, size - 1, &end)) // check_add_overflow not called when size is zero
We feel check_add_overflow() should be invoked even when the size is zero to ensure correct block size allocation.
Thanks & Best Regards,
Basharath
Rob Herring
maintainerQuoted 41 lines · Basharath Hussain Khaja
Hi,
Quoted 38 lines
Quoted 38 lines
Quoted 22 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,#endif /* CONFIG_PCI */ +static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, · line 212
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
The diff below fixes it for me.
I fixed it up with your change.
This commit is breaking Ethernet functionality on the TI AM57xx platform due to zero byte SRAM block size allocation during initialization. Prior to this patch, zero byte block sizes were handled properly.
What driver and where exactly?
Rob
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 42 lines
Quoted 24 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, #endif /* CONFIG_PCI */ +static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 · line 214
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
The diff below fixes it for me.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
I fixed it up with your change.
This commit is breaking Ethernet functionality on the TI AM57xx platform due to zero byte SRAM block size allocation during initialization. Prior to this patch, zero byte block sizes were handled properly.
What driver and where exactly?
We found an issue while developing the driver [1] and more specifically in [2] (lines 313-327), but it looks like this is a generic issue which can block 1 byte of memory, when a zero size request has been initiated for the reserved region.
static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
{
u64 end = start;
if (overflows_type(start, r->start))
return -EOVERFLOW;
if (size && check_add_overflow(end, size - 1, &end))
return -EOVERFLOW;
if (overflows_type(end, r->end))
return -EOVERFLOW;
r->start = start;
r->end = end;
return 0;
}Though we have the start address handling already in place above, we do see an issue with the end address, because there is an unconditional +1 afterwards in resource_size() API below which is responsible for reserving the extra byte
static inline resource_size_t resource_size(const struct resource *res)
{
return res->end - res->start + 1;
}We have 4 ways of fixing it.
Option 1: Modify the function to handle the size zero case
drivers/of/address.c +6 -0
@@ -204,6 +204,12 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)if (overflows_type (start, r->start))return -EOVERFLOW ; +if (!size) { + r->start = start; + r->end = end -1 ; + +return 0 ; + }if (size &&check_add_overflow (end, size -1 , &end))return -EOVERFLOW ;if (overflows_type (end, r->end))
This seems to be the simplest solution.
Option 2: Handle in resource_size().
static inline resource_size_t resource_size(const struct resource *res)
{
return (res->end == res->start) ? 0 : (res->end - res->start + 1);
}There are plenty of places where we are using this API and there is an assumption that the end address should always be start + size -1. We are a bit unsure about the side effects of this change.
Option 3: Handle in sram_reserve_region().
We can avoid calling the resource_size() API and handle size zero as a special case. We are a bit unsure about the side effects of this change as well.
Option 4: Handle this in dts [2] with non zero size. Estimate the approximate size and update that value in dts file with extra buffer. However, as indicated in [2] in lines 313-327, the size is not known apriori and the actual size is only known in runtime. So if we set some size for this buffer, then this will always be blocked and may or may not be used subsequently.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/torvalds/linux/blob/master/arch/arm/boot/dts/ti/omap/dra7.dtsi
Thanks & Best Regards,
Basharath
Quoted 78 lines · Basharath Hussain Khaja
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 24 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, #endif /* CONFIG_PCI */ +static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 · line 214
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
The diff below fixes it for me.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
I fixed it up with your change.
This commit is breaking Ethernet functionality on the TI AM57xx platform due to zero byte SRAM block size allocation during initialization. Prior to this patch, zero byte block sizes were handled properly.
What driver and where exactly?
We found an issue while developing the driver [1] and more specifically in [2] (lines 313-327), but it looks like this is a generic issue which can block 1 byte of memory, when a zero size request has been initiated for the reserved region.
static int
__of_address_resource_bounds(struct resource *r, u64 start, u64 size){ u64 end = start; if (overflows_type(start, r->start)) return -EOVERFLOW; if (size && check_add_overflow(end, size - 1, &end)) return -EOVERFLOW; if (overflows_type(end, r->end)) return -EOVERFLOW; r->start = start; r->end = end; return 0; }Though we have the start address handling already in place above, we do see an issue with the end address, because there is an unconditional +1 afterwards in
resource_size()API below which is responsible for reserving the extra byte
Quoted 4 lines
static inline
resource_size_tresource_size(const struct resource *res){ return res->end - res->start + 1; }
Now the report makes more sense.
Quoted 22 lines
We have 4 ways of fixing it.
Option 1: Modify the function to handle the size zero case
drivers/of/address.c +6
-0@@ -204,6 +204,12 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)if (overflows_type (start, r->start))return -EOVERFLOW ; +if (!size) { + r->start = start; + r->end = end -1 ; + +return 0 ; + }if (size &&check_add_overflow (end, size -1 , &end))return -EOVERFLOW ;if (overflows_type (end, r->end))
Quoted 1 line
This seems to be the simplest solution.
Fixing it in __of_address_resource_bounds() looks correct to me. The proposed solution doesn't look as clean as I'd like though, this is highly subjective, though.
What about the following (untested)?
static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
{
if (overflows_type(start, r->start))
return -EOVERFLOW;
r->start = start;
r->end = start;
if (!size)
r->end -= 1; /* May underflow for empty resources. */
else if (check_add_overflow(r->end, size - 1, &r->end))
return -EOVERFLOW;
return 0;
}A kunit test looks to be in order in any case, to make sure all the edgecases are handled.
Quoted 18 lines
Option 2: Handle in
resource_size().
static inlineresource_size_tresource_size(const struct resource *res){ return (res->end == res->start) ? 0 : (res->end - res->start + 1); }There are plenty of places where we are using this API and there is an assumption that the end address should always be start + size -1. We are a bit unsure about the side effects of this change.
Option 3: Handle in
sram_reserve_region().
We can avoid calling theresource_size()API and handle size zero as a special case. We are a bit unsure about the side effects of this change as well.Option 4: Handle this in dts [2] with non zero size. Estimate the approximate size and update that value in dts file with extra buffer. However, as indicated in [2] in lines 313-327, the size is not known apriori and the actual size is only known in runtime. So if we set some size for this buffer, then this will always be blocked and may or may not be used subsequently.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/torvalds/linux/blob/master/arch/arm/boot/dts/ti/omap/dra7.dtsiThanks & Best Regards,
Basharath
Quoted 134 lines · Basharath Hussain Khaja
Quoted 78 lines
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 24 lines · Thomas Weißschuh
The members "start" and "end" of struct resource are of type "
resource_size_t" which can be 32bit wide.
Values read from OF however are always 64bit wide.Refactor the diff overflow checks into a helper function. Also extend the checks to validate each calculation step.
Signed-off-by Thomas Weißschuh <[email protected]>--- drivers/of/address.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)drivers/of/address.c +2
-0@@ -198,6 +198,25 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, #endif /* CONFIG_PCI */ +static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) +{drivers/of/address.c static u64 of_bus_pci_map(__be32 *addr, const __be32 · line 214
Quoted 10 lines
drivers/of/address.c +10
-0+u64 end = start; + +if (overflows_type (start, r->start)) +return -EOVERFLOW ; +if (size ==0 ) +return -EOVERFLOW ; +if (check_add_overflow (end, size -1 , &end)) +return -EOVERFLOW ; +if (overflows_type (end, r->end)) +return -EOVERFLOW ;This breaks PCI on powerpc qemu. Part of the PCI probe reads a resource that's zero sized, which used to succeed but now fails due to the size check above.
The diff below fixes it for me.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
I fixed it up with your change.
This commit is breaking Ethernet functionality on the TI AM57xx platform due to zero byte SRAM block size allocation during initialization. Prior to this patch, zero byte block sizes were handled properly.
What driver and where exactly?
We found an issue while developing the driver [1] and more specifically in [2] (lines 313-327), but it looks like this is a generic issue which can block 1 byte of memory, when a zero size request has been initiated for the reserved region.
static int
__of_address_resource_bounds(struct resource *r, u64 start, u64 size){ u64 end = start; if (overflows_type(start, r->start)) return -EOVERFLOW; if (size && check_add_overflow(end, size - 1, &end)) return -EOVERFLOW; if (overflows_type(end, r->end)) return -EOVERFLOW; r->start = start; r->end = end; return 0; }Though we have the start address handling already in place above, we do see an issue with the end address, because there is an unconditional +1 afterwards in
resource_size()API below which is responsible for reserving the extra byteQuoted 4 lines
static inline
resource_size_tresource_size(const struct resource *res){ return res->end - res->start + 1; }Now the report makes more sense.
Quoted 23 lines
We have 4 ways of fixing it.
Option 1: Modify the function to handle the size zero case
drivers/of/address.c +6
-0@@ -204,6 +204,12 @@ static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size) if (overflows_type(start, r->start)) return -EOVERFLOW; +if (!size) { + r->start = start; + r->end = end -1 ; + +return 0 ; + }if (size &&check_add_overflow (end, size -1 , &end))return -EOVERFLOW ;if (overflows_type(end, r->end))Quoted 1 line
This seems to be the simplest solution.
Fixing it in
__of_address_resource_bounds()looks correct to me. The proposed solution doesn't look as clean as I'd like though, this is highly subjective, though.What about the following (untested)?
static int
__of_address_resource_bounds(struct resource *r, u64 start, u64 size){ if (overflows_type(start, r->start)) return -EOVERFLOW; r->start = start; r->end = start; if (!size) r->end -= 1; /* May underflow for empty resources. */ else if (check_add_overflow(r->end, size - 1, &r->end)) return -EOVERFLOW; return 0; }
Quoted 3 lines
A kunit test looks to be in order in any case, to make sure all the edgecases are handled.
We have tested with your suggested changes as below for our functionality it is working as expected. To be on safe side we ran through patch verification tools, we found no issues.
drivers/of/address.c +6 -7
@@ -200,17 +200,16 @@ static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,static int __of_address_resource_bounds (struct resource *r,u64 start,u64 size) { -u64 end = start; -if (overflows_type (start, r->start))return -EOVERFLOW ; -if (size &&check_add_overflow (end, size -1 , &end)) -return -EOVERFLOW ; -if (overflows_type (end, r->end)) -return -EOVERFLOW ; r->start = start; - r->end = end; + r->end = start; + +if (!size) + r->end -=1 ;/* May underflow for empty resources. */ +else if (check_add_overflow (r->end, size -1 , &r->end)) +return -EOVERFLOW ;return 0 ; }
Thanks & Best Regards,
Basharath