Skip to contentSkip to conversation

kernel/resource: Fix return code check in __request_free_mem_region

PATCH Applied Alistair Popple 2 participants 3 messages May 12, 2021 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/resource: Fix return code check in __request_free_mem_region

Splitting an earlier version of a patch that allowed calling __request_region() while holding the resource lock into a series of patches required changing the return code for the newly introduced __request_region_locked().

Unfortunately this change was not carried through to a subsequent commit 56fd94919b8b ("kernel/resource: fix locking in request_free_mem_region") in the series. This resulted in a use-after-free due to freeing the struct resource without properly releasing it. Fix this by correcting the return code check so that the struct is not freed if the request to add it was successful.

Fixes 56fd94919b8b ("kernel/resource: fix locking in request_free_mem_region")
---
 kernel/resource.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/resource.c +1 -1
index 028a5ab18818..ca9f5198a01f 100644
@@ -1805,7 +1805,7 @@ static struct resource *__request_free_mem_region(struct device *dev,
 				REGION_DISJOINT)
 			continue;
 
-		if (!__request_region_locked(res, &iomem_resource, addr, size,
+		if (__request_region_locked(res, &iomem_resource, addr, size,
 						name, 0))
 			break;
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Alistair Popple

Splitting an earlier version of a patch that allowed calling __request_region() while holding the resource lock into a series of patches required changing the return code for the newly introduced __request_region_locked().

Unfortunately this change was not carried through to a subsequent commit 56fd94919b8b ("kernel/resource: fix locking in request_free_mem_region") in the series. This resulted in a use-after-free due to freeing the struct resource without properly releasing it. Fix this by correcting the return code check so that the struct is not freed if the request to add it was successful.

Fixes 56fd94919b8b ("kernel/resource: fix locking in request_free_mem_region")
---
  kernel/resource.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/resource.c +0 -0
index 028a5ab18818..ca9f5198a01f 100644

kernel/resource.c static struct resource *__request_free_mem_region(struct device *dev, · line 1811

Quoted 8 lines
kernel/resource.c +1 -1
@@ -1805,7 +1805,7 @@ static struct resource *__request_free_mem_region(struct device *dev,
  				REGION_DISJOINT)
  			continue;
 
-		if (!__request_region_locked(res, &iomem_resource, addr, size,
+		if (__request_region_locked(res, &iomem_resource, addr, size,
  						name, 0))
  			break;
 

Ouch, missed that, would have expected this pops up right away when testing.

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
Quoted 13 lines · AEST David Hildenbrand
Quoted 12 lines · Alistair Popple

Splitting an earlier version of a patch that allowed calling __request_region() while holding the resource lock into a series of patches required changing the return code for the newly introduced __request_region_locked().

Unfortunately this change was not carried through to a subsequent commit 56fd94919b8b ("kernel/resource: fix locking in request_free_mem_region") in the series. This resulted in a use-after-free due to freeing the struct resource without properly releasing it. Fix this by correcting the return code check so that the struct is not freed if the request to add it was successful.

Quoted 2 lines
Quoted 2 lines
Fixes 56fd94919b8b ("kernel/resource: fix locking in

request_free_mem_region")

Quoted 10 lines

kernel/resource.c static struct resource

Quoted 10 lines
---
  kernel/resource.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/resource.c +0 -0
index 028a5ab18818..ca9f5198a01f 100644
@@ -1805,7 +1805,7 @@ static struct resource 

*__request_free_mem_region(struct device *dev,

Quoted 4 lines

kernel/resource.c static struct resource *__request_free_mem_region(struct device *dev,

Quoted 4 lines
kernel/resource.c +0 -1
                              REGION_DISJOINT)
                      continue;
 
-             if (!__request_region_locked(res, &iomem_resource, addr, 

size,

Quoted 1 line

kernel/resource.c static struct resource · line 1808

Quoted 1 line
kernel/resource.c +1 -0
+             if (__request_region_locked(res, &iomem_resource, addr, 

size,

Quoted 6 lines

kernel/resource.c static struct resource *__request_free_mem_region(struct device *dev, · line 1811

Quoted 4 lines
kernel/resource.c +0 -0
                                              name, 0))
                      break;
 

Ouch, missed that, would have expected this pops up right away when testing.

Yes, ouch indeed. I am still trying to figure out why I didn't catch this right away as well. I retested locally and the HMM tests do complete without causing an oops although they don't all pass. I guess I must have been in a rush and didn't check the test output properly to see if they actually passed.

Quoted 1 line

Thanks, and sorry for the extra noise.

Quoted 5 lines

--
Thanks,

David / dhildenb