Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp376042lqh; Fri, 31 May 2024 04:19:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVCAnMqTYuyHQyamdV7GfhZdIqzeCTlVj+ThKG28CPtxbGd9b91FQ9sQ10yzCU8606Em+yWNocmSda+vFlcFDAhcedoiyPYmL8CSt+mEw== X-Google-Smtp-Source: AGHT+IGXvNYuLDVYF3+ybOuIE33J0ZL7w3v+MIS+276dG9x9b4krnYVHbdCP90PUFCPPJ6bRn9bH X-Received: by 2002:a05:6a00:847:b0:6f0:c79f:cd7e with SMTP id d2e1a72fcca58-70247663dbfmr1961910b3a.0.1717154366851; Fri, 31 May 2024 04:19:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717154366; cv=pass; d=google.com; s=arc-20160816; b=JfBODUDC/Jjeavshtm3vmCGDpuWHeVRgaFIoHBwN50uK3LB2FE+QH68736f73BtmDg vgv1bnEA+P6AZLByuhLDWnq72QaPc72B+EA53XyJ5FQ45XVNT1NA+MDzTuyP77SiMJw5 TZDOslddK9TCforvywrORmL3aJXQ4uUDoUYFqKGMWFyCySRjcIvsiIgFyS9dgylQJ6Wa r3h9iOJQWuTH6C6ArHVC8P+BZlBxDQVbsUtrLSQJkYQNHxSowKRIEVVgIEXWOHV2Sext vMeA2APxMlj5h6geXeirz3/eSrxxAlI5sdFp8w/GMPqRU9bKvHksO+atytDcbyVDRklW lWvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=FhOFxtuZRW6McJk35fxGo8aJBPM+ac6u4rfcns9YsC0=; fh=yiS3t11htnSE0T7fStY1IwmKdoy56qHrxfshV6dSphM=; b=h+r3u5yJwAYeVhUtav2s515jVHxPHxFy6MHhNFpEbKDA4fMkZ6w+Le4aDWR54JM05r Z8Y+shd1xFoqNoDCBiTdJsgy3mGca0zWc67MDZ4WEqvvc92pyHyMXwUPNfiHRh377i9y j3jvpmggim5Z+ggvWH51xrSnvy3bA7xSVrE8ZdjevZxw9rmKP2lM69aO3X6bCJvF7Cww PFHcMoMFBeG7zjIc0Sd6ijOPWrxWaTPusAweZPR50azt9MkVWmLDgnHIhhW3dLAd2Yhk 3qDR9oFWgrcv19PRa9g1bYMaC/Y+jjzTK7zNtxS/qmJtX987Y01j2y+FArumDdJfMyRF 8OVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=I+tCTTr4; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=mumtj6s+; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-196725-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6c35b40c8d0si1364091a12.564.2024.05.31.04.19.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 04:19:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196725-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=I+tCTTr4; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=mumtj6s+; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-196725-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7A1B1282CF5 for ; Fri, 31 May 2024 11:19:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1EAC015748F; Fri, 31 May 2024 11:19:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="I+tCTTr4"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mumtj6s+" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 DFA56155A53; Fri, 31 May 2024 11:19:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717154352; cv=none; b=OM1f7VXOKqHvy/QRUoFDkJTsisoz5QFjuZnNS5CzjQD2myXhs39POKD64236FO1wAGml/tP7wjR8mFtOK4y8DOa6eq9iqezpSzw0lZRKY5iy3rUzw0Br12+8tz6lGIYOoZ1W6N0Byf4zzNKhC1PbhpCuGVZJT83neu1GrDX8ckk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717154352; c=relaxed/simple; bh=pSNqZXR3ptCr47hJi4y3sx1XE5Az6hVVaKrTUaPrsvY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LIO4L6mkzFXf2517fGBDR3ieBzZlrBEATGJ/+nuvBoK2tlX9jjtjdCB0TjNgE6bjciq6p1JPGiJJDvtszDPmJGTpbwubaiSsWqygO6tikvYq5Hn/CykjHddq7lxNRdFLXlnt4uQFjNoEAczLSeAuc0XFnKUyPgNJCueyEvuhyWQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=I+tCTTr4; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mumtj6s+; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Nam Cao DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1717154349; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FhOFxtuZRW6McJk35fxGo8aJBPM+ac6u4rfcns9YsC0=; b=I+tCTTr4HQPtlV84niLSe6Dp9NKksAbEBTfLcnh0VKoOXbIIcXbByC52ItsfVS0GzjunjK xtumrU9+05MR7A4jKoJc2Og9s6Vwyx6kEI9B9ujOJS6rhHvqxmO7SZoo/OGbyjdXkCdEAB 4li/rPTQrYLJIkqkolYM7cxWgPKlfeTQKk5FqrxKQSibdrQs7kmaS/8pfij8kr3x5ZJuhf RNrk7Tk0+NOmV/dpjDe5UQUOSemE8BzHBX5Etj3E2RmAUV0g2Dr0UV+expUF0rm704JYsN XeKxyIWJJkP52w0b+TAoB8Ll+0LK558yZ2EWzS1Pl+d4nu5B6fo94pxEa3FkAw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1717154349; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FhOFxtuZRW6McJk35fxGo8aJBPM+ac6u4rfcns9YsC0=; b=mumtj6s+40M2wsO/GPb2nw1ItxedxrOOkOmRMLcFyGCYym89akvhLxB60gPcApMNnrKi3M i/iNFp0OIA6rWcAA== To: Bjorn Helgaas , Rob Herring , Lizhi Hou , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Lukas Wunner , Nam Cao , stable@vger.kernel.org Subject: [PATCH 1/2] PCI: of_property: Fix NULL pointer defererence in of_pci_prop_bus_range() Date: Fri, 31 May 2024 13:18:59 +0200 Message-Id: <18ae685b655c6c9334cb80bb32e6b4a1b423b0ae.1717154083.git.namcao@linutronix.de> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The subordinate pointer can be null if we are out of bus number. The function of_pci_prop_bus_range() deferences this pointer without checking and may crashes the kernel. This crash can be reproduced by starting a QEMU instance: qemu-system-riscv64 -machine virt \ -kernel ../build-pci-riscv/arch/riscv/boot/Image \ -append "console=ttyS0 root=/dev/vda" \ -nographic \ -drive "file=root.img,format=raw,id=hd0" \ -device virtio-blk-device,drive=hd0 \ -device pcie-root-port,bus=pcie.0,slot=1,id=rp1,bus-reserve=0 \ -device pcie-pci-bridge,id=br1,bus=rp1 Then hot-add a bridge with device_add pci-bridge,id=br2,bus=br1,chassis_nr=1,addr=1 Then the kernel crashes: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000088 [snip] [] of_pci_add_properties+0x34c/0x3c6 [] of_pci_make_dev_node+0xb6/0x116 [] pci_bus_add_device+0xa8/0xaa [] pci_bus_add_devices+0x30/0x6a [] shpchp_configure_device+0xa0/0x112 [] board_added+0xce/0x354 [] shpchp_enable_slot+0xda/0x30c [] shpchp_pushbutton_thread+0x84/0xa0 NULL check this pointer first before proceeding. Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") Signed-off-by: Nam Cao Cc: stable@vger.kernel.org --- drivers/pci/of_property.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index c2c7334152bc..5fb516807ba2 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -91,6 +91,9 @@ static int of_pci_prop_bus_range(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np) { + if (!pdev->subordinate) + return 0; + u32 bus_range[] = { pdev->subordinate->busn_res.start, pdev->subordinate->busn_res.end }; -- 2.39.2