Skip to contentSkip to conversation

resource: harden resource_contains

PATCH In review [email protected] 3 participants 5 messages Nov 29, 2024 – Dec 8, 2024 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 resource: harden resource_contains

From: Alejandro Lucero <[email protected]>

While resource_contains checks for IORESOURCE_UNSET flag for the resources given, if r1 was initialized with 0 size, the function returns a false positive. This is so because resource start and end fields are unsigned with end initialised to size - 1 by current resource macros.

Make the function to check for the resource size for both resources since r2 with size 0 should not be considered as valid for the function purpose.

---
 include/linux/ioport.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ioport.h +2 -0
index 6e9fb667a1c5..6cb8a8494508 100644
@@ -264,6 +264,8 @@ static inline unsigned long resource_ext_type(const struct resource *res)
 /* True iff r1 completely contains r2 */
 static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
 {
+	if (!resource_size(r1) || !resource_size(r2))
+		return false;
 	if (resource_type(r1) != resource_type(r2))
 		return false;
 	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw

+ linux-cxl

Quoted 8 lines · On Fri, Nov 29, 2024 at 09:15:12AM +0000, [email protected] wrote:

From: Alejandro Lucero <[email protected]>

While resource_contains checks for IORESOURCE_UNSET flag for the resources given, if r1 was initialized with 0 size, the function returns a false positive. This is so because resource start and end fields are unsigned with end initialised to size - 1 by current resource macros.

Quoted 4 lines

Make the function to check for the resource size for both resources since r2 with size 0 should not be considered as valid for the function purpose.

Hi Alejandro,

Can this patch be included in the CXL Type-2 patchset, as a replacement for:

[PATCH v6 10/28] cxl: harden resource_contains checks to handle zero size resources
https://lore.kernel.org/[email protected]/T/#u

Keeping it in that set also keeps the discussion history.

DaveJ may be able to take it through the CXL tree with Bjorn's ACK.

--Alison

Quoted 22 lines
---
 include/linux/ioport.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ioport.h +2 -0
index 6e9fb667a1c5..6cb8a8494508 100644
@@ -264,6 +264,8 @@ static inline unsigned long resource_ext_type(const struct resource *res)
 /* True iff r1 completely contains r2 */
 static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
 {
+	if (!resource_size(r1) || !resource_size(r2))
+		return false;
 	if (resource_type(r1) != resource_type(r2))
 		return false;
 	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
Signature · 1 line
2.17.1
#2 lore raw
Quoted 17 lines · Alison Schofield

+ linux-cxl

Quoted 8 lines · On Fri, Nov 29, 2024 at 09:15:12AM +0000, [email protected] wrote:

From: Alejandro Lucero <[email protected]>

While resource_contains checks for IORESOURCE_UNSET flag for the resources given, if r1 was initialized with 0 size, the function returns a false positive. This is so because resource start and end fields are unsigned with end initialised to size - 1 by current resource macros.

Quoted 4 lines

Make the function to check for the resource size for both resources since r2 with size 0 should not be considered as valid for the function purpose.

Hi Alejandro,

Quoted 8 lines

Can this patch be included in the CXL Type-2 patchset, as a replacement for:

[PATCH v6 10/28] cxl: harden resource_contains checks to handle zero size resources
https://lore.kernel.org/[email protected]/T/#u

Keeping it in that set also keeps the discussion history.

DaveJ may be able to take it through the CXL tree with Bjorn's ACK.

Sure. I'll do so in the almost ready v7.

Quoted 5 lines

--Alison

Thank you

Quoted 23 lines
Quoted 23 lines
---
  include/linux/ioport.h | 2 ++
  1 file changed, 2 insertions(+)
include/linux/ioport.h +2 -0
index 6e9fb667a1c5..6cb8a8494508 100644
@@ -264,6 +264,8 @@ static inline unsigned long resource_ext_type(const struct resource *res)
  /* True iff r1 completely contains r2 */
  static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
  {
+	if (!resource_size(r1) || !resource_size(r2))
+		return false;
  	if (resource_type(r1) != resource_type(r2))
  		return false;
  	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 8 lines · On Fri, Nov 29, 2024 at 09:15:12AM +0000, [email protected] wrote:

From: Alejandro Lucero <[email protected]>

While resource_contains checks for IORESOURCE_UNSET flag for the resources given, if r1 was initialized with 0 size, the function returns a false positive. This is so because resource start and end fields are unsigned with end initialised to size - 1 by current resource macros.

Quoted 6 lines

Make the function to check for the resource size for both resources since r2 with size 0 should not be considered as valid for the function purpose.

Seems reasonable to me. FWIW,

Quoted 20 lines
---
 include/linux/ioport.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ioport.h +2 -0
index 6e9fb667a1c5..6cb8a8494508 100644
@@ -264,6 +264,8 @@ static inline unsigned long resource_ext_type(const struct resource *res)
 /* True iff r1 completely contains r2 */
 static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
 {
+	if (!resource_size(r1) || !resource_size(r2))
+		return false;
 	if (resource_type(r1) != resource_type(r2))
 		return false;
 	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
Signature · 1 line
2.17.1
#4 lore raw
Quoted 13 lines · Bjorn Helgaas
Quoted 12 lines · On Fri, Nov 29, 2024 at 09:15:12AM +0000, [email protected] wrote:

From: Alejandro Lucero <[email protected]>

While resource_contains checks for IORESOURCE_UNSET flag for the resources given, if r1 was initialized with 0 size, the function returns a false positive. This is so because resource start and end fields are unsigned with end initialised to size - 1 by current resource macros.

Make the function to check for the resource size for both resources since r2 with size 0 should not be considered as valid for the function purpose.

Quoted 5 lines
Quoted 2 lines

Seems reasonable to me. FWIW,

Thanks!

Quoted 20 lines
Quoted 20 lines
---
  include/linux/ioport.h | 2 ++
  1 file changed, 2 insertions(+)
include/linux/ioport.h +2 -0
index 6e9fb667a1c5..6cb8a8494508 100644
@@ -264,6 +264,8 @@ static inline unsigned long resource_ext_type(const struct resource *res)
  /* True iff r1 completely contains r2 */
  static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
  {
+	if (!resource_size(r1) || !resource_size(r2))
+		return false;
  	if (resource_type(r1) != resource_type(r2))
  		return false;
  	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
Signature · 1 line
2.17.1