From: Wolfram Sang <wsa+renesas@sang-engineering.com>
To: Tejas Mutalikdesai <tejasmutalikdesai@gmail.com>
Cc: andi.shyti@kernel.org, wsa@kernel.org,
vladimir_zapolskiy@mentor.com, linux-i2c@vger.kernel.org,
linux-kernel@vger.kernel.org,
syzbot+c0291c8c9aaa473c7721@syzkaller.appspotmail.com
Subject: Re: [PATCH] i2c: core: move idr_remove() in i2c_del_adapter() before device_unregister()
Date: Thu, 18 Jun 2026 22:52:10 +0200 [thread overview]
Message-ID: <ajRaej6d2npPBeca@shikoro> (raw)
In-Reply-To: <20260618184610.6536-1-tejasmutalikdesai@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2162 bytes --]
On Fri, Jun 19, 2026 at 12:16:10AM +0530, Tejas Mutalikdesai wrote:
> There is a race between i2c_del_adapter() and i2c_get_adapter() that
> can trigger a "refcount_t: addition on 0; use-after-free" warning.
>
> The sequence is:
>
> 1. i2c_del_adapter() calls device_unregister(), which drops the
> device refcount to zero (the adapter's release callback fires
> and signals dev_released).
>
> 2. The adapter is still in i2c_adapter_idr because idr_remove()
> hasn't been called yet.
>
> 3. A concurrent i2c_get_adapter() calls idr_find() under core_lock
> and finds the adapter. try_module_get() succeeds because the
> owning module is still MODULE_STATE_LIVE (this is a hot-remove
> path, not a module unload).
>
> 4. get_device() is called on a kobject whose refcount is already
> zero, triggering refcount_warn_saturate() with REFCOUNT_ADD_UAF.
>
> Fixing this by moving the idr_remove() call to before device_unregister().
> Once the adapter is removed from the IDR, any concurrent
> i2c_get_adapter() will get NULL from idr_find() and return -ENODEV.
> Callers that already hold a device reference are unaffected:
> wait_for_completion() correctly waits for them to release it via
> i2c_put_adapter().
>
> REPRODUCTION AND VALIDATION: Tested by inserting a msleep(500) after wait_for_completion() to widen
> the race window, and using a kernel module that spawns two kthreads: one
> calling i2c_del_adapter() and another calling i2c_get_adapter()
> concurrently while the module remains MODULE_STATE_LIVE. Without the
> fix, the WARNING fires reliably. With the fix, i2c_get_adapter()
> returns NULL and no WARNING is observed.
>
> Reported-by: syzbot+c0291c8c9aaa473c7721@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c0291c8c9aaa473c7721
> Fixes: 611e12ea0f12 ("i2c: core: manage i2c bus device refcount in i2c_[get|put]_adapter")
> Signed-off-by: Tejas Mutalikdesai <tejasmutalikdesai@gmail.com>
I'd think this is fixed with b1a58ed9eab1 ("i2c: core: fix adapter
deregistration race") which went upstream this merge window.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
prev parent reply other threads:[~2026-06-18 20:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-18 18:46 [PATCH] i2c: core: move idr_remove() in i2c_del_adapter() before device_unregister() Tejas Mutalikdesai
2026-06-18 20:52 ` Wolfram Sang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ajRaej6d2npPBeca@shikoro \
--to=wsa+renesas@sang-engineering.com \
--cc=andi.shyti@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=syzbot+c0291c8c9aaa473c7721@syzkaller.appspotmail.com \
--cc=tejasmutalikdesai@gmail.com \
--cc=vladimir_zapolskiy@mentor.com \
--cc=wsa@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox