]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
revocable: Fix races in revocable_alloc() using RCU
authorTzung-Bi Shih <tzungbi@kernel.org>
Thu, 29 Jan 2026 14:37:30 +0000 (14:37 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 3 Feb 2026 11:30:43 +0000 (12:30 +0100)
commit4d7dc4d1a62dbb22b1178dddeeb7a22d0272df77
treea6f2d0ff8a60e1e97dab1cb7dcae3944b7eb4b0e
parent289b14592cefe95f7d0ef334873c12b96ce3824f
revocable: Fix races in revocable_alloc() using RCU

There are two race conditions when allocating a revocable instance:

1. After a struct revocable_provider is revoked, the caller might still
   hold a dangling pointer to it.  A subsequent call to
   revocable_alloc() can trigger a use-after-free.
2. If revocable_provider_release() runs concurrently with
   revocable_alloc(), the memory of struct revocable_provider can be
   accessed during or after kfree().

To fix these:
- Manage the lifetime of struct revocable_provider using RCU.  Annotate
  pointers to it with __rcu and use kfree_rcu() for deallocation.
- Update revocable_alloc() to safely acquire a reference using RCU
  primitives.
- Update revocable_provider_revoke() to take a double pointer (`**rp`).
  It atomically NULLs out the caller's pointer before starting
  revocation.  This prevents the caller from holding a dangling pointer.
- Drop devm_revocable_provider_alloc().  The devm-managed model cannot
  support the required double-pointer semantic for safe pointer nulling.

Reported-by: Johan Hovold <johan@kernel.org>
Closes: https://lore.kernel.org/all/aXdy-b3GOJkzGqYo@hovoldconsulting.com/
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
Link: https://patch.msgid.link/20260129143733.45618-2-tzungbi@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/driver-api/driver-model/revocable.rst
drivers/base/revocable.c
drivers/base/revocable_test.c
include/linux/revocable.h
tools/testing/selftests/drivers/base/revocable/test_modules/revocable_test.c