From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F556414A1C; Tue, 21 Jul 2026 21:56:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784671004; cv=none; b=NZYotuQ4Wq6CAdOeKqAmxJr3ZM0tknV73swsUSYlYmsmEAZ3q9HhjN5PrLQndeDhuIXD2cr8nIe3EN0JmGBw8o/i/IoYTnppvJx7gmTsRsJN/oIv22x1S52QrNRLj1d2CnqdwiUQg08h55PYq9GuVzUaZwdaDo78pBi6JIooTEg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784671004; c=relaxed/simple; bh=lQ7bR0seMZ0xa1eWMYbQQ4FbCoA9N32OH/QbH9Jt1h0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RFeTTY6gstMIz0GFt7oJxNlLRMfDxcBTbl+RIMleC4AUlc6krcjdNuiFv4h3OLOo2QXeQQqCXpBkZyf6jjiB7+kx5XgZXs10JqqlbZaVHT5egyrWHMAAekDbQ1n9dFT0SO1ZoqQn7al8Z8MTcf7A2zR0oTcgqMf5BRO7ZBATr5c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=DIw39hAz; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="DIw39hAz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C69521F000E9; Tue, 21 Jul 2026 21:56:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784671003; bh=6/iV6YwKVlLQBdiQDHswQS98EYdzdQwQGinWcZZ3ACk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DIw39hAzOhb1YSucM6LiUTMx9TbSkRNsAv1TBDdxmsdd6ldG8J5M0cLSLwT2c8ej4 2iwh2+PazIJskrwvIaW/ARfuTF9GOkgYZlzVRq1IdaR94MnjPUSc34txX8PScDBWbT fH0PNfq11zlOOsskf/yUWEFu30ivEbGVccEXCdww= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Johan Hovold , Wolfram Sang , Sasha Levin Subject: [PATCH 5.15 039/843] i2c: core: fix adapter registration race Date: Tue, 21 Jul 2026 17:14:34 +0200 Message-ID: <20260721152406.855138664@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152405.946368001@linuxfoundation.org> References: <20260721152405.946368001@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Johan Hovold [ Upstream commit ba14d7cf2fe7284610a29854bdff22b2537d3ce6 ] Adapters can be looked up based on their id using i2c_get_adapter() which takes a reference to the embedded struct device. Make sure that the adapter (including its struct device) has been initialised before adding it to the IDR to avoid accessing uninitialised data which could, for example, lead to NULL-pointer dereferences or use-after-free. Note that the i2c-dev chardev, which is registered from a bus notifier, currently uses i2c_get_adapter() so the adapter needs to be added to the IDR before registration. Fixes: 6e13e6418418 ("i2c: Add i2c_add_numbered_adapter()") Cc: stable@vger.kernel.org # 2.6.22 Signed-off-by: Johan Hovold Signed-off-by: Wolfram Sang Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/i2c-core-base.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -1559,6 +1559,10 @@ static int i2c_register_adapter(struct i adap->debugfs = debugfs_create_dir(dev_name(&adap->dev), i2c_debugfs_root); + mutex_lock(&core_lock); + idr_replace(&i2c_adapter_idr, adap, adap->nr); + mutex_unlock(&core_lock); + res = device_add(&adap->dev); if (res) { pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); @@ -1631,7 +1635,7 @@ static int __i2c_add_numbered_adapter(st int id; mutex_lock(&core_lock); - id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); + id = idr_alloc(&i2c_adapter_idr, NULL, adap->nr, adap->nr + 1, GFP_KERNEL); mutex_unlock(&core_lock); if (WARN(id < 0, "couldn't get idr")) return id == -ENOSPC ? -EBUSY : id; @@ -1667,7 +1671,7 @@ int i2c_add_adapter(struct i2c_adapter * } mutex_lock(&core_lock); - id = idr_alloc(&i2c_adapter_idr, adapter, + id = idr_alloc(&i2c_adapter_idr, NULL, __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); mutex_unlock(&core_lock); if (WARN(id < 0, "couldn't get idr"))