Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1571124iob; Fri, 29 Apr 2022 08:08:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEiP62rzZMTS4WCPsCQ3P0vapPE3uYuTXfkOroFPPXrAgf8jbqju00H9jH6GtPILypjF3P X-Received: by 2002:a05:6122:d98:b0:331:47bf:b437 with SMTP id bc24-20020a0561220d9800b0033147bfb437mr12435736vkb.29.1651244912102; Fri, 29 Apr 2022 08:08:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651244912; cv=none; d=google.com; s=arc-20160816; b=toPqih8rIAeePoKEQehecJ6lhTusoTYhYsnKWAwDebmhXj0Po296NsMaISKOaRvjIY DUvNHhnzpjohXqZYSNFGq3Y9fyPXa70W88H1MRXJkLt9Y09ucaL7C7uSEOW4FeQ9NH70 6oKhZMoVQ9aIwzH68Xp+xSNmukmIoiWla3eeLnS29u9ahuFfgC9xWYrKdSoRUXHbGx/e HNGkgUYaBz++LNyDME3Opcn8ftSMrA6jrj6K3S1f+Pc08xNjGdZPyE9Z54LOD98+kdKx vvSz0z08RthRwtT42iEsHLsg7j3V9qTR6wHUyFRSS4FVwwGhElVWF/jf6oF0+HFP3nNW Nwag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject:dkim-signature; bh=rhA4aZUdRaNgOqnDVV3QzhoT8n++/dGWzUM62LHJQpE=; b=lY/nEtVe9WRV6inwJjmItgQFAQcUyss7SQ1+7jOG5MJ9TbjKG0cLYyUDRHPHO6xn68 rPldyAY6BvNXu4M+4SgrXXq9N+UoWO/uc2THVwM967H/58peG17xyMlWBqgYSTYdugER VnFfR8/gpvmyn8oQ/W9D5ikZl6qQsJEp3xZqWiJKXnEDXU+tl+YR/0eAv18w+CUcCfTq 8vDy+bkZ75rfaF9gd/3meWUk4/gGgU3cRcQj8cHWQ3OEM8U9uvXmksUd7wLd2cIJnvw7 xdQRMnU94RSPO5SXYB8+iaR0th/WXlDKTNNRapmSzbuxG9yxOkHcjlwLrKJNw06wI1AP iA9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YpVVxkFO; 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=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q143-20020a1f2a95000000b0034d55c47bcesi670101vkq.162.2022.04.29.08.08.12; Fri, 29 Apr 2022 08:08:32 -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=@linaro.org header.s=google header.b=YpVVxkFO; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353986AbiD2Bmj (ORCPT + 99 others); Thu, 28 Apr 2022 21:42:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235461AbiD2Bmi (ORCPT ); Thu, 28 Apr 2022 21:42:38 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E19CBF330 for ; Thu, 28 Apr 2022 18:39:21 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id t13so5384989pgn.8 for ; Thu, 28 Apr 2022 18:39:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=rhA4aZUdRaNgOqnDVV3QzhoT8n++/dGWzUM62LHJQpE=; b=YpVVxkFOvaBfZTwyNXGDo+Gr0q/zc/izFhnZ0kSp0xp8DXzHB1Vhz7HEg5yEGk9Lup YqVJ4Jji5VQN5ToQuduIiSWpyfgRp6q35jcBRVqCZY3MH2VDrToKXurUtMP4CYo+4RYp tClatmIUqEuTphALX0jOQfN2TYYniHEJgKhchMNvxSVy/i8/gaRrMRVsXsm2pKmFYLWm fEUru6dQJKSiDwjpDi2qICmSJQVmhUptUFoTMpv7QIYHzgiIGIiOJESnm0N2ZmWP3Q7U x9ZXWGbJYbb/xEYE+LAaSN88EKwKXGtQ94UV5TszGa4g/ZGZzb1+wInB5Ik/W5FmGjqS rluQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=rhA4aZUdRaNgOqnDVV3QzhoT8n++/dGWzUM62LHJQpE=; b=bGTExf0sIgFU1oi5PJmtIW68TbIItrTlE1k8CGrxq7ynSVOi1JrLxoKkz3kaj9kQoo 1IvG+ND4fQmOYEG2FMsuVCVG3X8UmX66cLYIdghe16iQPRHXHDRZjEKIFOsLxTo3CVqE lJCyViC7K/ea35H4nQGIBycYiKC6h1iTtURGYXa8wjHlYSV4fxNUPt3Kvzw19LwoNcGx KgK+Y62rtoGJD6t0NmdWPHngMQmc1IyLt0hJROn6F70/FcY1J5Lq4hJOrJUdU7YP1Yat C4tognJz6dDDVmsvDxYoQlbhnmNx0icFiLCje18nlxAYpIvaHIv0JQIA3QcX0I//j+8A rpJA== X-Gm-Message-State: AOAM531jQfmHkGQMRsp38Jktma+0yOLLeIH5B9crpQoMFnW7NehBK/Hy 5Fx02rX/9FMow1iL0pRKEzNPdQ== X-Received: by 2002:a63:d145:0:b0:3c1:4ba0:d890 with SMTP id c5-20020a63d145000000b003c14ba0d890mr8385077pgj.607.1651196361096; Thu, 28 Apr 2022 18:39:21 -0700 (PDT) Received: from [10.52.0.6] ([94.177.118.121]) by smtp.gmail.com with ESMTPSA id k17-20020a628e11000000b0050d8d373331sm1021922pfe.214.2022.04.28.18.39.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 28 Apr 2022 18:39:20 -0700 (PDT) Subject: Re: [PATCH v2] iommu/sva: Fix PASID use-after-free issue To: Fenghua Yu , Dave Hansen , Thomas Gleixner , Jean-Philippe Brucker , Borislav Petkov , Ingo Molnar , Zhangfei Gao , Will Deacon , Robin Murphy , Tony Luck , Jacob Pan , Ravi V Shankar , Peter Zijlstra , Andy Lutomirski , x86 , linux-kernel , iommu@lists.linux-foundation.org References: <20220428180041.806809-1-fenghua.yu@intel.com> From: Zhangfei Gao Message-ID: <8f50c673-fe92-3c42-993d-43e65fc7235c@linaro.org> Date: Fri, 29 Apr 2022 09:39:09 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20220428180041.806809-1-fenghua.yu@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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 On 2022/4/29 上午2:00, Fenghua Yu wrote: > The PASID is being freed too early. It needs to stay around until after > device drivers that might be using it have had a chance to clear it out > of the hardware. > > As a reminder: > > mmget() /mmput() refcount the mm's address space > mmgrab()/mmdrop() refcount the mm itself > > The PASID is currently tied to the life of the mm's address space and > freed in __mmput(). This makes logical sense because the PASID can't be > used once the address space is gone. > > But, this misses an important point: even after the address space is > gone, the PASID will still be programmed into a device. Device drivers > might, for instance, still need to flush operations that are outstanding > and need to use that PASID. They do this at file->release() time. > > Device drivers call the IOMMU driver to hold a reference on the mm itself > and drop it at file->release() time. But, the IOMMU driver holds a > reference on the mm itself, not the address space. The address space > (and the PASID) is long gone by the time the driver tries to clean up. > This is effectively a use-after-free bug on the PASID. > > To fix this, move the PASID free operation from __mmput() to __mmdrop(). > This ensures that the IOMMU driver's existing mmgrab() keeps the PASID > allocated until it drops its mm reference. > > Fixes: 701fac40384f ("iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit") > > Reported-by: Zhangfei Gao > Tested-by: Zhangfei Gao Tested-by: Zhangfei Gao Use the formal email, thanks > Suggested-by: Jean-Philippe Brucker > Suggested-by: Jacob Pan > Reviewed-by: Jean-Philippe Brucker > Signed-off-by: Fenghua Yu > --- > > v2: > - Dave Hansen rewrites the change log. > - Add Tested-by: Zhangfei Gao > - Add Reviewed-by: Jean-Philippe Brucker > > The original patch was posted and discussed in: > https://lore.kernel.org/lkml/YmdzFFx7fN586jcf@fyu1.sc.intel.com/ > > kernel/fork.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/fork.c b/kernel/fork.c > index 9796897560ab..35a3beff140b 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -792,6 +792,7 @@ void __mmdrop(struct mm_struct *mm) > mmu_notifier_subscriptions_destroy(mm); > check_mm(mm); > put_user_ns(mm->user_ns); > + mm_pasid_drop(mm); > free_mm(mm); > } > EXPORT_SYMBOL_GPL(__mmdrop); > @@ -1190,7 +1191,6 @@ static inline void __mmput(struct mm_struct *mm) > } > if (mm->binfmt) > module_put(mm->binfmt->module); > - mm_pasid_drop(mm); > mmdrop(mm); > } >