Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2197340iof; Tue, 7 Jun 2022 22:56:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydDpvBA7LA9XXIAy1+U3TjT3wi+0FkXqP56WLoWNLIkdFlq4qUDno3qV6O0WP6VDt6KFhV X-Received: by 2002:a63:9752:0:b0:3c6:5a7a:5bd6 with SMTP id d18-20020a639752000000b003c65a7a5bd6mr29123816pgo.390.1654667775765; Tue, 07 Jun 2022 22:56:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667775; cv=none; d=google.com; s=arc-20160816; b=xMXrq+DNNl8Bb5g80Wv6bVN0Yd4pVLsF5cMkRsXICKlW3Kqyum7/LBXY9WB000RkuL uDvIjMBCIW98Vj9bgyOX2bxaTuxkCb+8z2xXbCjAi4qp2Ybs05FYrs/oi4AXI+TI4A03 gE7ZC0H43OKXEXpLGpN7O2AyhCsYPWAID34B0qW159KbQgjxllgw6DENm4AFBFaZ2LtE /yQGvcC+CggHuIwfugoUpqIS1grXNkscN1wxswqq4uFU4LkITbV/uDY+YXtYyVz2kYLH Annsn5tp+twQHDkmazIV74BVPlzd4LuDxxBLhwykF6eaQXJp0G8txDCP1gg2e9msTyDJ 3DuA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hPnEHoeMSImQVrkr/r43oOIL7tAWrLmUBU/kxyn05P0=; b=Lqh6Psr/edcHa0qMPkh6YDuwv6mWqo96D61lFGVxFpkn3lphMzK/RSFhgg//sxAOa7 ilDiwC2U8nWcalhC1pbgixWABqbIbDjNXb7HTiss1pnzGdAgLFRSqQtyM5oxcwAbn+3O AfglNKnKPidLYlYn6qdoq8bes9OU577eUs3WuVQmndxWiATHojItDEuRih8csvc4jwO1 9Y6GhtECu1y8oXoRAaN7WxhuZ3IuZmHYsTFSM7r1Ectz682taTwWS/PibLdvVX9tSZHW Qnce6r9KpKy/CD8KBi0u05OJKO+GfRtgqI/FdsUauSQzBbyDCHUcqUOoI2VN2TSEI1mQ wGMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HTzPwQhS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bg12-20020a056a02010c00b003fb9808054bsi24444442pgb.359.2022.06.07.22.56.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:56:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HTzPwQhS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EF23824A6A9; Tue, 7 Jun 2022 22:22:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443614AbiFHBAk (ORCPT + 99 others); Tue, 7 Jun 2022 21:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383158AbiFGWFS (ORCPT ); Tue, 7 Jun 2022 18:05:18 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A1D419594A; Tue, 7 Jun 2022 12:16:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 15E2BB823CA; Tue, 7 Jun 2022 19:16:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73593C385A2; Tue, 7 Jun 2022 19:16:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629388; bh=b1g1fws0lCsOl10pEsDk3as04zJl0512CLXA21q35DY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HTzPwQhSa5ZElVoIp24FjoKQmbQ+trwyFQRuf0KriS0fSKpyU4S2fzl89TgxIHuKn gei99Px6/g6gTsyuFKy1MzCOmLQUQire33KT3Fhex1xZnvcQzOXYQ2CDDENoiVg57F pWA3buCRbkNSH6l4/x3sT4gs3g5Ce/KGzPMSYU1I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jean-Philippe Brucker , Zhangfei Gao , Will Deacon , Sasha Levin Subject: [PATCH 5.18 671/879] iommu/arm-smmu-v3-sva: Fix mm use-after-free Date: Tue, 7 Jun 2022 19:03:09 +0200 Message-Id: <20220607165022.326261425@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Jean-Philippe Brucker [ Upstream commit cbd23144f7662b00bcde32a938c4a4057e476d68 ] We currently call arm64_mm_context_put() without holding a reference to the mm, which can result in use-after-free. Call mmgrab()/mmdrop() to ensure the mm only gets freed after we unpinned the ASID. Fixes: 32784a9562fb ("iommu/arm-smmu-v3: Implement iommu_sva_bind/unbind()") Signed-off-by: Jean-Philippe Brucker Tested-by: Zhangfei Gao Link: https://lore.kernel.org/r/20220426130444.300556-1-jean-philippe@linaro.org Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index c623dae1e115..1ef7bbb4acf3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "arm-smmu-v3.h" @@ -96,9 +97,14 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) struct arm_smmu_ctx_desc *cd; struct arm_smmu_ctx_desc *ret = NULL; + /* Don't free the mm until we release the ASID */ + mmgrab(mm); + asid = arm64_mm_context_get(mm); - if (!asid) - return ERR_PTR(-ESRCH); + if (!asid) { + err = -ESRCH; + goto out_drop_mm; + } cd = kzalloc(sizeof(*cd), GFP_KERNEL); if (!cd) { @@ -165,6 +171,8 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) kfree(cd); out_put_context: arm64_mm_context_put(mm); +out_drop_mm: + mmdrop(mm); return err < 0 ? ERR_PTR(err) : ret; } @@ -173,6 +181,7 @@ static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) if (arm_smmu_free_asid(cd)) { /* Unpin ASID */ arm64_mm_context_put(cd->mm); + mmdrop(cd->mm); kfree(cd); } } -- 2.35.1