From patchwork Thu Oct 13 15:33:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerry Snitselaar X-Patchwork-Id: 2079 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp344912wrs; Thu, 13 Oct 2022 08:40:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6gQ8iEsqZT410Yjdg5iGIIYrSi8IhgylMIMrkMMWRBq1ZuF9Ta/yAbrJP2Su/XJBnrGeCV X-Received: by 2002:a05:6402:292f:b0:458:e447:5c with SMTP id ee47-20020a056402292f00b00458e447005cmr331614edb.286.1665675657360; Thu, 13 Oct 2022 08:40:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665675657; cv=none; d=google.com; s=arc-20160816; b=HhkKYaf26Zf0TihKKEYDLOJOxcBc51k6Z75QWp6r427Tpx00MlyaZChLtgXJoXXLxJ 3+iGNk92iqy0E/iFi/ZBgRRKC4pMjG1t4N0B23AurfGRh0qvj+R3aQSyshuzyuaXaQnz +NFj/4p+3H0seow2nts/k3aWFK/mNcJ6cSovKD6xh5R2Su/xOZVMNZXgax42u6nxv6Z4 Bl+1r2kuKQtjdrPJZb9r8MxTSbGiCql/u+3+LJ+pYPLIGot2M19O8kUhhWpg6juMKkhA cdNhiPIoQqs0O6biHs1T/UepQTV89mnkXEDQakQCDEvt+RWXoDpGCW1flgXXySD9EvUy f69g== 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=b9+V6R7tFlbeiwac+o//rSY61pCcgVEQM77YrZbvp4g=; b=FLbqzNKNpskXLLd6NLhiHWWCwIqHdcLE1oGK2ZBfFV+RXg3E/YPcaREQ8DTGckZ4+P rs9MiaD9P0uz/WM7Ea8+cFnMqCZN/Qz0lmIPQ88p1ZYdsi/hm+ERvf18KmlrwM9hiv3R iBrB4xD/uZ92ceNTc5Y5zvBJ4sIoSO3/hPk+lvfmYUrrWLqkMLKAuqTZbF6SqTpb2eB0 lfjyt6rsRZuL39+OVgboTL9IbcJf8BgxX1BkZI+iVPRCaWdEZjO0aJOMTeWKaIn5s/As Tnjz5/qhicRtJ2BCMfDQpYsv48N9tS8AlGvBfijtHXAwVJcHcqJ9+Rurs5bjulVs2xTk GmOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R9MrUHYa; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020a056402511100b0045938ab7129si52042edd.330.2022.10.13.08.40.29; Thu, 13 Oct 2022 08:40:57 -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=@redhat.com header.s=mimecast20190719 header.b=R9MrUHYa; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbiJMPeM (ORCPT + 99 others); Thu, 13 Oct 2022 11:34:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229495AbiJMPeL (ORCPT ); Thu, 13 Oct 2022 11:34:11 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 345A089CF1 for ; Thu, 13 Oct 2022 08:34:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665675248; 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; bh=b9+V6R7tFlbeiwac+o//rSY61pCcgVEQM77YrZbvp4g=; b=R9MrUHYaCTR94E9Rc3uLa4tg9GNUD37kRxdv8huKsqVHz0G0lUbHd7qH1qSfGuiaDp2L/b Kq6ImK7Hg1v2ybfvm2zFnOANC3uTEK+Q4v6p+WqoPp8CsEZayay7ePuW/YF0b8kyJ5rEvN JCwzR1VSv6YSCb84XFMiFp+nq/lN0d4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-658-mhXDiGpFPbyJlSVFSPWONA-1; Thu, 13 Oct 2022 11:34:07 -0400 X-MC-Unique: mhXDiGpFPbyJlSVFSPWONA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 58D8C2A5955A; Thu, 13 Oct 2022 15:34:06 +0000 (UTC) Received: from cantor.redhat.com (unknown [10.2.16.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id CAD6140C8461; Thu, 13 Oct 2022 15:33:57 +0000 (UTC) From: Jerry Snitselaar To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Will Deacon , Robin Murphy , Lu Baolu Subject: [RFC PATCH] iommu/vt-d: Add sanity check to iommu_sva_bind_device() Date: Thu, 13 Oct 2022 08:33:55 -0700 Message-Id: <20221013153355.2365865-1-jsnitsel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746587518455198610?= X-GMAIL-MSGID: =?utf-8?q?1746587518455198610?= iommu_sva_bind_device() should only be called if iommu_dev_enable_feature() succeeded. There has been one case already where that hasn't been the case, which resulted in a null pointer deref in dev_iommu_ops(). To avoid that happening in the future if another driver makes that mistake, sanity check dev->iommu and dev->iommu->iommu_dev prior to calling dev_iommu_ops(). Cc: Joerg Roedel Cc: Will Deacon Cc: Robin Murphy Cc: Lu Baolu Signed-off-by: Jerry Snitselaar --- drivers/iommu/iommu.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 4893c2429ca5..20ec75667529 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2746,7 +2746,15 @@ iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) { struct iommu_group *group; struct iommu_sva *handle = ERR_PTR(-EINVAL); - const struct iommu_ops *ops = dev_iommu_ops(dev); + const struct iommu_ops *ops; + + if (!dev->iommu || !dev->iommu->iommu_dev) { + dev_warn(dev, "%s called without checking succes of iommu_dev_enable_feature?\n", + __func__); + return ERR_PTR(-ENODEV); + } + + ops = dev_iommu_ops(dev); if (!ops->sva_bind) return ERR_PTR(-ENODEV);