Skip to contentSkip to conversation

regulator: core: use-after-free of rdev->bdev when a probe fails

DISCUSSION Carlo Szelinsky 1 participant 1 message Nov 28

lore
Share & tools
lore
#0 lore raw

Hi,

since 304f5784e972 ("regulator: core: reresolve unresolved supplies when available"), unregistering a regulator that had an unresolved supply when it registered gives a "does not have a release() function" WARNING. If the unregister comes soon after the register, it is also a use-after-free. I see it on net-next f89726ffabef with PSE patches on top, none of which touch the regulator core. drivers/regulator/core.c is the same in mainline (7cd8495c0f16).

How it happens:

1. regulator_register() cannot resolve the supply, so it adds rdev->bdev on regulator_bus. The only driver on that bus prefers asynchronous probing, so device_add() queues __device_attach_async_helper(), which holds a reference on the bdev.
2. The driver's probe then fails, for example with -EPROBE_DEFER, and devres calls regulator_unregister().
3. device_unregister(&rdev->bdev) drops the bdev's references on its

    parent rdev->dev, but the bdev is still alive through the async
    helper. device_unregister(&rdev->dev) then frees rdev in
    regulator_dev_release(), and the bdev is part of rdev.

4. The async helper then reads the freed bdev, and its last put_device() reaches device_release(), which WARNs because the bdev has no release function. Without the async race, the WARNING comes from regulator_unregister() itself. From the code, the bdev is only removed in regulator_unregister(), so a regulator whose supply was resolved later should WARN there too. I have not tested that.

So any driver that registers a regulator with an unresolved supply and then fails its probe can hit this. I hit it while testing a PSE controller patch: the PI regulators registered while their supply was not ready yet (not registered, or its device still probing), then the PSE driver deferred its probe. In QEMU (arm64, KASAN) one boot gave 37 of these WARNINGs and this report:

  BUG: KASAN: slab-use-after-free in __device_attach_async_helper+0x260/0x298
  Read of size 8 at addr ffff00000535c4f8 by task kworker/u4:2/30
  Call trace:
   __device_attach_async_helper+0x260/0x298
   async_run_entry_fn+0x80/0x264
   process_one_work+0x850/0x1844
   worker_thread+0x73c/0xca4
  Allocated by task 1:
   regulator_register+0x220/0x1d10
   devm_regulator_register+0x58/0xbc
   pse_controller_register+0xa84/0x1aa4
  Freed by task 1:
   kfree+0x1f0/0x4bc
   regulator_dev_release+0x74/0x98
   device_release+0xfc/0x204
   kobject_put+0x138/0x234
   device_unregister+0x20/0x2c
   regulator_unregister+0x228/0x2e8
   devm_rdev_release+0x24/0x40
   ...
   really_probe+0x3b4/0x59c

Device 'regulator.6.bdev' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.

  WARNING: drivers/base/core.c:2642 at device_release+0x184/0x204
  Call trace:
   device_release+0x184/0x204
   kobject_put+0x138/0x234
   device_unregister+0x20/0x2c
   regulator_unregister+0x25c/0x2e8
   devm_rdev_release+0x24/0x40
   ...
   really_probe+0x3b4/0x59c

The reproducer is an out-of-tree test driver, so I have not seen it with an in-tree driver yet.

One way to fix it would be to let the bdev hold a reference on rdev->dev: take it before device_add(&rdev->bdev) and drop it in a new release function for the bdev. Then rdev stays alive until the last bdev reference is gone. I have not written or tested that, so I leave the fix to you.

I found and analysed this with help from an LLM assistant.

Thanks,
Carlo