__release_resource() dereferences the parent of the resource without
checking whether the resource is in a resource tree at all. Releasing
a resource that is not in the tree or has already been removed from it
results in a NULL pointer dereference. Callers cannot check the parent
beforehand without racing as release_resource() takes the resource lock
only internally.

Looping a BAR resize through sysfs against a rescan of the parent bus
on a QEMU guest released a PCI bridge window that had already been
released:

  BUG: kernel NULL pointer dereference, address: 0000000000000038
  Oops: Oops: 0000 [#1] SMP
  CPU: 0 UID: 0 PID: 49 Comm: init Not tainted 7.4.0-rc1-00001-g4aa3acaaeb08 #1 
PREEMPTLAZY
  RIP: 0010:__release_resource+0x8/0x70
  Call Trace:
   release_resource+0x22/0x40
   pci_release_resource+0x54/0x90
   pci_do_resource_release_and_resize+0x1f3/0x3d0
   __resource_resize_store+0xf0/0x180
   kernfs_fop_write_iter+0xfe/0x150
   vfs_write+0xf7/0x160
   ksys_write+0x6c/0xa0
   do_syscall_64+0x3b/0x1b0
   entry_SYSCALL_64_after_hwframe+0x4b/0x53

The double release was due to a race in the PCI bridge window sizing
and resource assignment, which is fixed separately, but the resource
tree code should have prevented the NULL pointer dereference too.

Return -EINVAL if the resource has no parent.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Ilpo Järvinen <[email protected]>
Cc: [email protected]
---

The PCI race fixes series mentioned in the changelog is here:

  
https://lore.kernel.org/linux-pci/[email protected]/

 kernel/resource.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/resource.c b/kernel/resource.c
index 0c315c917fbb..68843852f342 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -218,6 +218,9 @@ static int __release_resource(struct resource *old, bool 
release_child)
 {
        struct resource *tmp, **p, *chd;
 
+       if (!old->parent)
+               return -EINVAL;
+
        p = &old->parent->child;
        for (;;) {
                tmp = *p;
-- 
2.47.3

Reply via email to