From nobody Tue Sep 29 05:34:51 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 192E12BD587 for ; Wed, 12 Aug 2026 02:47:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786502845; cv=none; b=VMEoVKDaqhCENzU/akxJ7wuUGoo+yMANETuvHtpqznahGR64TNRg4bdMqbazvc1KwAhVGhpTsu6P+BZVhvzHJYRFZ4pl8PIUGlpDmGpCyxAfTm7+GxGf5UV1+KIem2RKaRSllW1uynrsKm2M4HsVCHS5WuBVuTqBH/YyfY12ZH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786502845; c=relaxed/simple; bh=ALDw7ALpx5kPUnaKuRpbZ57cF4Us+cKtY0dL1YT+XOo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=rGg1MkTufUemuwX+nse04odB0LVJDHr8iYpcMAdmzspGf5YNiKXYpnfuCcyucRkREjExX7VUpEQ5jOFgrIOP6cXUUb+PYogUbWHi24OJRT9UHs4LGaX77urn+x+IBXkqFsBt2ggqyLJdEqM2khbwNl3NFpFd2BR9v3FgcHefcUg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K1zqhm9u; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K1zqhm9u" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c9cf07d2df6so281928a12.2 for ; Tue, 11 Aug 2026 19:47:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786502842; x=1787107642; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=aAuLpf/tdgMm6ZPBoW/USWZ/wPE2gVwN/uVbw8U/1z4=; b=K1zqhm9u0HxVChqxW7X/q5+D39PK3smi375aNo+dyp3hg0/blUiUFQhGno5rVfIEaK b2hvw3NSPcbKG7sSPOjhI/ITP7AVJMGA+NnenV6d5V4LC/IyFF58eOMLk4ZKUFJ6/rXO TDh8JyhFH6YNefKJqzS+Z2GJ4BkIVJlwHBUj36bRQq8jnWmAlZMvASj37faRSUS1n6pC rV4bsEipn4NQI/9xS9AnknPhGRHH6nIzGRDjQePjBjpS0VFaAH8uBtyosh/n+QTooEOP z3CjvNpYO2ACD19nauQsVLOPThiPAD1qi7tIfHhRE4erLM0KYQRkm+0+TXRAT5vXuhgy 4PWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786502842; x=1787107642; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aAuLpf/tdgMm6ZPBoW/USWZ/wPE2gVwN/uVbw8U/1z4=; b=fnEk/Gum4vQJ8GwW0GoGAdLU1jt2SOCXGtWkwqkWK47deCSlCoytjEHrrqWmJF5yDx R1hVi15e8KA7xsUchM3+soH++o9zVXbAqijIaAE4WoomTlUENHGUt2oRJoMaeNEBAkuS eXEh+aoRmGI7sSXtsTsCKH2EenbvLVSVnT2OCAZZMibrw6NBWPRPb+BXvAkt0hNNjBYy TEEieTLY5HgsT4T8cvd2lOAjFD0IerA8/AKxPPuBtNY7iDqi8HCz0sYiLKOUIe85kxHW EWJhS5Fj7WnAsxDos0b70AnEIHPPk8oCevVbf9zyo3OwN+HLGBu2zvUpmoOqa+CNQhwl +2pQ== X-Forwarded-Encrypted: i=1; AHgh+Rp2Fpi1YTn2ZhbAsdyf7xW6dBvL5cDi2CkEJZ2xrdbCgO4WvrohjpM5gOavF8Zg7CYq08vD5g5BOs3j1Yo=@vger.kernel.org X-Gm-Message-State: AOJu0YwcthAlgrFpOfobqpKSSecQBVnNGR7vdLX06TECCSfKtkW4r20A 2lZCalyViqC+HmmDUPphgOZVGT7kIi3Jm71WEJpOC1A7wYqvENyKlNzr0eEjvVyE X-Gm-Gg: AR+sD13nUmUvTrCBT4sQaOU6lluTsgJrtNq58J9kclaP7+JMhwW2ZdgwHZ9oX3DAl++ 7/CQ++QsR9AQQZBehpCI7b83rwS90XJAQXMRo4bFseR2VT5s0bEW1AgWrUgZUXDXjuIDPVBXerR UD5utRqG+YDTDUa/jv4/za92/5jSf/57NvQ0QmFmQTFqZWTioBiFgT3ozSTWxV9rGyt9yNDUw4d F91BLFOqRxzRKC+u3W69VPlTe+kycKI3PH5wOR5p515Fh1hjkgDArwXG+9aWBuXNc1L40F+24wV TaAHlqJgbx802skuTtBSc+vvGNiDbjHc0hxGJE5PGKd0yETZSJmQP2lQ3Nsh3Nq/MUBdGQhTS7J ZPkj4lvqYIQ+5Xb9stAl0A9oBuIpeC4zDRg2wz5g4V+dXiWDnB4AB2+EzzLn05Q5Pim3fL1qY9S Cz8cwRDLibpMAX0dOlnWYcuVIq55ervACmTT0xcJu/aytQ/1d0Ftj13LqdOQwnwclv3evDpodp4 prfpD4meg== X-Received: by 2002:a05:6a21:350f:b0:3c4:2b8b:e88f with SMTP id adf61e73a8af0-3cc3f520e14mr1854571637.5.1786502842272; Tue, 11 Aug 2026 19:47:22 -0700 (PDT) Received: from kernel ([103.219.206.122]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31cf67912d0sm6965188eec.24.2026.08.11.19.47.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2026 19:47:21 -0700 (PDT) From: Mohamad Raizudeen To: bhelgaas@google.com, gregkh@linuxfoundation.org Cc: skhan@linuxfoundation.org, jkoolstra@xs4all.nl, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Mohamad Raizudeen Subject: [PATCH] PCI: Fix use-after-free race in pci_find_bus() Date: Wed, 12 Aug 2026 08:17:13 +0530 Message-ID: <20260812024713.4958-1-raizudeen.kerneldev@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pci_find_bus() iterates over the list of PCI root buses using pci_find_next_bus(). This helper acquires pci_bus_sem, retrieves the next bus and drops the lock before returning the pointer to the caller. pci_find_bus() then uses this pointer to check the domain and traverses the child buses via pci_do_find_bus() without holding the pci_bus_sem lock. If a PCI bus is concurrently removed for example via hotplug between loop iterations, the from pointer passed back into pci_find_next_bus() becomes stale, leading to a user-after-free when dereferencing from->node.next. Additionally, traversing the bus tree without holding the lock is a race condition. Fix this by iterating pci_root_buses list directly using list_for_each_entry() inside pci_find_bus() while holding the pci_bus_sem read lock for the entire duration of the search. This ensures the list and tree structures cannot change while being traversed, eliminating the use-after-free. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Mohamad Raizudeen --- drivers/pci/search.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/pci/search.c b/drivers/pci/search.c index e3d3177fce54..f50e83061b76 100644 --- a/drivers/pci/search.c +++ b/drivers/pci/search.c @@ -142,17 +142,19 @@ static struct pci_bus *pci_do_find_bus(struct pci_bus= *bus, unsigned char busnr) */ struct pci_bus *pci_find_bus(int domain, int busnr) { - struct pci_bus *bus =3D NULL; - struct pci_bus *tmp_bus; + struct pci_bus *bus; + struct pci_bus *tmp_bus =3D NULL; =20 - while ((bus =3D pci_find_next_bus(bus)) !=3D NULL) { - if (pci_domain_nr(bus) !=3D domain) - continue; - tmp_bus =3D pci_do_find_bus(bus, busnr); - if (tmp_bus) - return tmp_bus; + down_read(&pci_bus_sem); + list_for_each_entry(bus, &pci_root_buses, node) { + if (pci_domain_nr(bus) =3D=3D domain) { + tmp_bus =3D pci_do_find_bus(bus, busnr); + if (tmp_bus) + break; + } } - return NULL; + up_read(&pci_bus_sem); + return tmp_bus; } EXPORT_SYMBOL(pci_find_bus); =20 --=20 2.53.0