Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp584761rwe; Thu, 1 Sep 2022 04:50:53 -0700 (PDT) X-Google-Smtp-Source: AA6agR7mdRSZBm3BedFqEQLF4p/gLdL6g1dNBiQXgxIkyGBS9azs/1rVm1ebG5yq4kuDDGmObUxH X-Received: by 2002:a63:e306:0:b0:42c:414a:95e9 with SMTP id f6-20020a63e306000000b0042c414a95e9mr14940910pgh.139.1662033053254; Thu, 01 Sep 2022 04:50:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662033053; cv=none; d=google.com; s=arc-20160816; b=n8elvLfuWOH+rWRdLi/UVjPpo06psMovwjm1EDAwLt8lNeHe3MXxu06xWJbga7GLb5 jOfe3em/BDM78V/pi3n9XJLIP9OEVju3NK9HP9uurn+Q8WLYcCZJxKUx3IxDreN6HnVD 7fZUFQ6ZJt9ba2Jc98Ep1ds34FVvIDy5M3dHiaEX25k7UD0zJwnjeP9coajMNdFW/I0h 2sUIu7SSAT+FPQuIvJHCV3T3iIRQSIdHZRT4Z6GILZnd8fNNagbXsMhepzB2PQz3XbFF z65/2EUsdSvhj9z/e0qnf4EIFbkxgssKUbXCv5Iub58qehS8RhhB9feSgg0iXifHU8MG tHxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8cQV8tQDyiwu/A2S/YEu7h9Kz2DFYKKPPS/j4ZQgWXg=; b=03xs8uRBpnb3DqawWD6y2f7kfqqwJrdAFlbPDFudtKOOWTLykq2eubAcnWfxTOoa6P 0qGKKABgvhxjTsLfr04/nC3sIvKoUZnt9PQWQE2Y17lfyaylZXeKh0e4MBl/YqQh6niD ZZKsLH/FEuf7Y0VF2L0tGF0XiGSSwh8PQ0g9Al9u6iCh1PKPdaDPSS4pWf4CLtgdKWN2 gVInPIHTht4uprqlkM7FPy0KtO/P7eS4xPgnOyKYyakMIuQJjAU1Wmh7+ZRcEAdqzJyD IpLlG2YBxG+veAv/gHbw1enHvt9t8L44/wNh0GGR/pOsdipbUTdldKARyedRbeXCwgff 1z9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DiVw3ZkU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a4-20020a056a000c8400b0053815aa4c51si13463741pfv.195.2022.09.01.04.50.42; Thu, 01 Sep 2022 04:50:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DiVw3ZkU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232539AbiIALd5 (ORCPT + 99 others); Thu, 1 Sep 2022 07:33:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232357AbiIALdz (ORCPT ); Thu, 1 Sep 2022 07:33:55 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DABB115189; Thu, 1 Sep 2022 04:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662032035; x=1693568035; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=E+0XHZ48Mbb5Asg5RDA8s1aKri51xsKNicl5KUhjpiI=; b=DiVw3ZkUpCufsg7se79Krwf10pdY7qeYLcDAb4KeFp8+i1BldmEq5K58 07B+76/cWh27nFi994CSXG4YK+H689/VC//tP59+BAJHRUX1cTpDZMPz+ qBv9JujKS1ZfciW76DYfy7XRoL2CnDLcsNroFzUKHQQyukvZ5oas5QwWG VKYIfbN9IGqEnSe2CWDYvXRpTTbqgti/1NOtC2v3Pt3aH7meiKTGzpNTI XK6N5B5iY+/AJOezAplAm4mir7djfD3XGqOrcoMBzRpsTHNqcYoqCKERl OrOWdgutfcOQplclDcycpF9MlG4qY+Bbj6EOmVS8XiZO6OCZs2AOFIUmw Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10456"; a="295697443" X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="295697443" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 04:33:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="673809731" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 01 Sep 2022 04:33:53 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 11AEE1C3; Thu, 1 Sep 2022 14:34:07 +0300 (EEST) From: Andy Shevchenko To: Andy Shevchenko , Hans de Goede , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Mark Gross , kernel test robot , Mika Westerberg Subject: [PATCH v1 1/1] platform/x86: p2sb: Fix UAF when caller uses resource name Date: Thu, 1 Sep 2022 14:34:06 +0300 Message-Id: <20220901113406.65876-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have to copy only selected fields from the original resource. Because a PCI device will be removed immediately after getting its resources, we may not use any allocated data, hence we may not copy any pointers. Consider the following scenario: 1/ a caller of p2sb_bar() gets the resource; 2/ the resource has been copied by platform_device_add_data() in order to create a platform device; 3/ the platform device creation will call for the device driver's ->probe() as soon as a match found; 4/ the ->probe() takes given resources (see 2/) and tries to access one of its field, i.e. 'name', in the __devm_ioremap_resource() to create a pretty looking output; 5/ but the 'name' is a dangling pointer because p2sb_bar() removed a PCI device, which 'name' had been copied to the caller's memory. 6/ UAF (Use-After-Free) as a result. Kudos to Mika for the initial analisys of the issue. Fixes: 9745fb07474f ("platform/x86/intel: Add Primary to Sideband (P2SB) bridge support") Reported-by: kernel test robot Suggested-by: Mika Westerberg Link: https://lore.kernel.org/linux-i2c/YvPCbnKqDiL2XEKp@xsang-OptiPlex-9020/ Link: https://lore.kernel.org/linux-i2c/YtjAswDKfiuDfWYs@xsang-OptiPlex-9020/ Signed-off-by: Andy Shevchenko --- drivers/platform/x86/p2sb.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/platform/x86/p2sb.c b/drivers/platform/x86/p2sb.c index fb2e141f3eb8..384d0962ae93 100644 --- a/drivers/platform/x86/p2sb.c +++ b/drivers/platform/x86/p2sb.c @@ -42,10 +42,24 @@ static int p2sb_get_devfn(unsigned int *devfn) return 0; } +/* Copy resource from the first BAR of the device in question */ static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem) { - /* Copy resource from the first BAR of the device in question */ - *mem = pdev->resource[0]; + struct resource *bar0 = &pdev->resource[0]; + + /* Make sure we have no dangling pointers in the output */ + memset(mem, 0, sizeof(*mem)); + + /* + * We copy only selected fields from the original resource. + * Because a PCI device will be removed soon, we may not use + * any allocated data, hence we may not copy any pointers. + */ + mem->start = bar0->start; + mem->end = bar0->end; + mem->flags = bar0->flags; + mem->desc = bar0->desc; + return 0; } -- 2.35.1