From patchwork Wed Mar 24 10:16:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shradha Todi X-Patchwork-Id: 12166121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B4D6C433E0 for ; Fri, 26 Mar 2021 09:37:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3540F61A51 for ; Fri, 26 Mar 2021 09:37:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230317AbhCZJhM (ORCPT ); Fri, 26 Mar 2021 05:37:12 -0400 Received: from mailout2.samsung.com ([203.254.224.25]:59983 "EHLO mailout2.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230130AbhCZJhD (ORCPT ); Fri, 26 Mar 2021 05:37:03 -0400 Received: from epcas5p3.samsung.com (unknown [182.195.41.41]) by mailout2.samsung.com (KnoxPortal) with ESMTP id 20210326093701epoutp0252e5ad648c170c4b381eecc2f71b9f57~v2toUdKKY2100121001epoutp02p for ; Fri, 26 Mar 2021 09:37:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.samsung.com 20210326093701epoutp0252e5ad648c170c4b381eecc2f71b9f57~v2toUdKKY2100121001epoutp02p DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1616751421; bh=hxHXp6PeFmfwJxhC51xr7jOS0CvxIZzcuhvqoRIdIoY=; h=From:To:Cc:Subject:Date:References:From; b=NzjKLmCfwxm5+swbdcj36hMuiiRJODvGQlye6vmgT1Y0o+YXs4FtwDzRgTxHsWaLG sRdNWNLjlV2K4eJTcGkOu6q8JEp8p68LRGnpphja7qsFt90eW/goRfDu1F9SFy2z5x 3/tHveYG6A/clraEdsAoiL7EM8m/cT9RKb7MVVd0= Received: from epsmges5p3new.samsung.com (unknown [182.195.42.75]) by epcas5p3.samsung.com (KnoxPortal) with ESMTP id 20210326093659epcas5p318af0671c516bc2e17b4a202fa47783a~v2tmyN0O52571125711epcas5p3v; Fri, 26 Mar 2021 09:36:59 +0000 (GMT) Received: from epcas5p1.samsung.com ( [182.195.41.39]) by epsmges5p3new.samsung.com (Symantec Messaging Gateway) with SMTP id 93.CD.41008.B3BAD506; Fri, 26 Mar 2021 18:36:59 +0900 (KST) Received: from epsmtrp1.samsung.com (unknown [182.195.40.13]) by epcas5p1.samsung.com (KnoxPortal) with ESMTPA id 20210324101555epcas5p1b5c8ff4d99b045b94f820c2151800127~vP9B0vIAg1046710467epcas5p1g; Wed, 24 Mar 2021 10:15:55 +0000 (GMT) Received: from epsmgms1p1new.samsung.com (unknown [182.195.42.41]) by epsmtrp1.samsung.com (KnoxPortal) with ESMTP id 20210324101555epsmtrp1347a89b34131b9c8ca40757871367bea~vP9Bz2m9F1863418634epsmtrp1M; Wed, 24 Mar 2021 10:15:55 +0000 (GMT) X-AuditID: b6c32a4b-661ff7000001a030-fb-605dab3bca2b Received: from epsmtip1.samsung.com ( [182.195.34.30]) by epsmgms1p1new.samsung.com (Symantec Messaging Gateway) with SMTP id 2C.DD.33967.B511B506; Wed, 24 Mar 2021 19:15:55 +0900 (KST) Received: from Jaguar.sa.corp.samsungelectronics.net (unknown [107.108.73.139]) by epsmtip1.samsung.com (KnoxPortal) with ESMTPA id 20210324101553epsmtip14886026cd630a4b503948d646d4664fd~vP9AIC2nm1129011290epsmtip1y; Wed, 24 Mar 2021 10:15:53 +0000 (GMT) From: Shradha Todi To: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Cc: p.rajanbabu@samsung.com, lorenzo.pieralisi@arm.com, kishon@ti.com, bhelgaas@google.com, niyas.ahmed@samsung.com, hari.tv@samsung.com, l.mehra@samsung.com, pankaj.dubey@samsung.com, Shradha Todi , Sriram Dash Subject: [PATCH v5] PCI: endpoint: Fix NULL pointer dereference for ->get_features() Date: Wed, 24 Mar 2021 15:46:09 +0530 Message-Id: <20210324101609.79278-1-shradha.t@samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrPIsWRmVeSWpSXmKPExsWy7bCmuq716tgEg0fHNS2WNGVYLLu0mdHi 47SVTBYXnvawWdx5foPR4vKuOWwWZ+cdZ7N48/sFu8WTKY9YLY5uDLZYtPULu0Xv4VoHHo81 89YweuycdZfdY8GmUo++LasYPY7f2M7k8XmTXABbFJdNSmpOZllqkb5dAlfGjimv2QoOiFYs OuTcwLhZqIuRk0NCwETi+qy/7F2MXBxCArsZJW7//8MM4XxilLjXsJERwvnGKHF+zz0ghwOs ZepbX4j4XkaJzfsPMUE4LUwSD76uYwKZyyagJdH4tYsZxBYRsJY43L6FDcRmFuhgktizIATE FhYIk3h25D0jiM0ioCoxdfcmdpAFvAJWEo9vpUOcJy+xesMBsIskBG6xS5xeOJMNIuEicerw PxYIW1ji1fEt7BC2lMTL/jYoO19i6oWnLBBHV0gs76mDCNtLHLgyByzMLKApsX6XPkRYVmLq KYjrmQX4JHp/P2GCiPNK7JgHYytLfPm7B2qrpMS8Y5dZIWwPiY8zP4JtFRKIlXh68g3TBEbZ WQgbFjAyrmKUTC0ozk1PLTYtMM5LLdcrTswtLs1L10vOz93ECE4TWt47GB89+KB3iJGJg/EQ owQHs5IIb5JvTIIQb0piZVVqUX58U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mTFNRzty3PvfeTWyJlT+bDa4mG rflv5yfvt+pxU73xKjNt2bsOgfnLjFKnM62VlH0qOs1ZTWHXy/Vmzky7Cq57e6wSOnoxXuZ6 7535Cw5MfOf5882lTbrFtyV0r/gtM5g384ro2uvVmv3u31NCT6287LTSecZX9Q+lEpwzE2+c Xuxacyl2nuh/i3v/NXlldv4UOLj7uMSlGYpaxezXp50I5Lff2KC/0zfz/s77GXdXvL09uSd1 5kwTs+9rm6e4R5382qT0qmvpxb+Pt67hLeVVYinOSDTUYi4qTgQAMF9lZqoCAAA= X-CMS-MailID: 20210324101555epcas5p1b5c8ff4d99b045b94f820c2151800127 X-Msg-Generator: CA X-Sendblock-Type: REQ_APPROVE CMS-TYPE: 105P X-CMS-RootMailID: 20210324101555epcas5p1b5c8ff4d99b045b94f820c2151800127 References: Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org get_features ops of pci_epc_ops may return NULL, causing NULL pointer dereference in pci_epf_test_alloc_space function. Let us add a check for pci_epc_feature pointer in pci_epf_test_bind before we access it to avoid any such NULL pointer dereference and return -ENOTSUPP in case pci_epc_feature is not found. When the patch is not applied and EPC features is not implemented in the platform driver, we see the following dump due to kernel NULL pointer dereference. Call trace: pci_epf_test_bind+0xf4/0x388 pci_epf_bind+0x3c/0x80 pci_epc_epf_link+0xa8/0xcc configfs_symlink+0x1a4/0x48c vfs_symlink+0x104/0x184 do_symlinkat+0x80/0xd4 __arm64_sys_symlinkat+0x1c/0x24 el0_svc_common.constprop.3+0xb8/0x170 el0_svc_handler+0x70/0x88 el0_svc+0x8/0x640 Code: d2800581 b9403ab9 f9404ebb 8b394f60 (f9400400) ---[ end trace a438e3c5a24f9df0 ]--- Fixes: 2c04c5b8eef79 ("PCI: pci-epf-test: Use pci_epc_get_features() to get EPC features") Reviewed-by: Pankaj Dubey Reviewed-by: Kishon Vijay Abraham I Signed-off-by: Sriram Dash Signed-off-by: Shradha Todi --- Changes w.r.t. v4: https://lkml.org/lkml/2021/1/12/815 - Changed commit message to remove time stamp as suggested by Lorenzo - Since deference in not actually happening in bind function, mentioned the same in the commit message as suggested by Leon. drivers/pci/endpoint/functions/pci-epf-test.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c index c0ac4e9cbe72..bc35b3566be6 100644 --- a/drivers/pci/endpoint/functions/pci-epf-test.c +++ b/drivers/pci/endpoint/functions/pci-epf-test.c @@ -833,15 +833,18 @@ static int pci_epf_test_bind(struct pci_epf *epf) return -EINVAL; epc_features = pci_epc_get_features(epc, epf->func_no); - if (epc_features) { - linkup_notifier = epc_features->linkup_notifier; - core_init_notifier = epc_features->core_init_notifier; - test_reg_bar = pci_epc_get_first_free_bar(epc_features); - if (test_reg_bar < 0) - return -EINVAL; - pci_epf_configure_bar(epf, epc_features); + if (!epc_features) { + dev_err(&epf->dev, "epc_features not implemented\n"); + return -EOPNOTSUPP; } + linkup_notifier = epc_features->linkup_notifier; + core_init_notifier = epc_features->core_init_notifier; + test_reg_bar = pci_epc_get_first_free_bar(epc_features); + if (test_reg_bar < 0) + return -EINVAL; + pci_epf_configure_bar(epf, epc_features); + epf_test->test_reg_bar = test_reg_bar; epf_test->epc_features = epc_features;