Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1905170pxp; Thu, 10 Mar 2022 14:45:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwg8RBVLeK5h6lShFUHijTnV07eBeinUKMNLHWOq+iwzE7Xp8BZ+623JcpchUMKVvjhQVfQ X-Received: by 2002:a17:906:4c83:b0:6b7:b321:5d54 with SMTP id q3-20020a1709064c8300b006b7b3215d54mr6185118eju.676.1646952336321; Thu, 10 Mar 2022 14:45:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646952336; cv=none; d=google.com; s=arc-20160816; b=M6BMz7/L4MjDk3K+g1JpxiH9v4ajU3KfS8zJX5LA4aDgiDZ9eG3f2G2r7uit8vol3r VjcWQbz0rVsL/uztY73wgKTqlOB9HIjBvkbcabfo5d0wj+sO+3RBBswsdvpVHKJf6J+d piqE88Odyv0puZgJ8Q7gPPRrxyOJqfWXCb9KrUGLNCkfP1Hxr0gA7ae2UyC+vug5QIID r2E/giSV6GjBzU/cVqWsjl+U/hRC5tyoF9yFxHjcAcsKQ9QjhU70lmDLaGLCpv2GL2hJ BjhsbL7Px2SZ6yHJzd2uC6wEwTLN4il7dF0r/+wGmQ8wydXnAWs10sK45bQnZ/H3Otfi NEsg== 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=ztCaBoz1dsdDX3+4WK4s5TkhqOk9PXIqZlc8DaATlDs=; b=FdSFh2NqMjvz6I1vQfdrs65PHQY3jl/NwpvH40lPFV4uwXVGX+WK23/+742MvQsh7e clr6TVnQGvqO09DUMJ3xwJtFY/hjU+xX87+3OFvHC71AFl7Ej/HpHIeZjW/bPu887Kda 35OKHca7MCDHBjnccsYfFOnaSr+d/f/wcUXQbon9orhMjUs5LBxn8EHjcJVCaMyvroCF 5ELP8xPhFCzWKfpVMB5XrTUYzBM52HF5L7RnUeE0UI7k0s/ZPvpd2q+V9GjGsD1/KN2D 44uoPkfbRZZRkmpVT8bXxPx8mIy/8bKF7sfJl63rZAKDEWxMIKmHMYMvAVfmXCHwGvpe ealg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UFdYgh6F; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn42-20020a1709070d2a00b006db3accad43si4258501ejc.519.2022.03.10.14.45.13; Thu, 10 Mar 2022 14:45:36 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=UFdYgh6F; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243300AbiCJOYv (ORCPT + 99 others); Thu, 10 Mar 2022 09:24:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244160AbiCJOS7 (ORCPT ); Thu, 10 Mar 2022 09:18:59 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A671B251A; Thu, 10 Mar 2022 06:15:39 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 5BA2261C67; Thu, 10 Mar 2022 14:15:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EFDDC340EB; Thu, 10 Mar 2022 14:15:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646921730; bh=0SlhBY9FVbky4lNAtCvq+QSM2Rt7IiNUXbD6lBysKLc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UFdYgh6FsYyGQ7q7XfxNsSGATpZUv/rCFqabsEK7FGXO2merKw+w/O3ZXHFDFAEDZ aHhTa3QxPMH93gLRz/8YzcqWyEZjqjIeb7KpeZDfgl8cW4xiaDSOXt+dYfWW65lupV Alb7/h4JzYqaKX/+3p1XR6YxyG4H4+Ea+f+/qLbo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Demi Marie Obenour , Juergen Gross , Jan Beulich Subject: [PATCH 4.9 35/38] xen/gntalloc: dont use gnttab_query_foreign_access() Date: Thu, 10 Mar 2022 15:13:48 +0100 Message-Id: <20220310140809.157981695@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220310140808.136149678@linuxfoundation.org> References: <20220310140808.136149678@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=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 From: Juergen Gross Commit d3b6372c5881cb54925212abb62c521df8ba4809 upstream. Using gnttab_query_foreign_access() is unsafe, as it is racy by design. The use case in the gntalloc driver is not needed at all. While at it replace the call of gnttab_end_foreign_access_ref() with a call of gnttab_end_foreign_access(), which is what is really wanted there. In case the grant wasn't used due to an allocation failure, just free the grant via gnttab_free_grant_reference(). This is CVE-2022-23039 / part of XSA-396. Reported-by: Demi Marie Obenour Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich Signed-off-by: Greg Kroah-Hartman --- drivers/xen/gntalloc.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) --- a/drivers/xen/gntalloc.c +++ b/drivers/xen/gntalloc.c @@ -166,20 +166,14 @@ undo: __del_gref(gref); } - /* It's possible for the target domain to map the just-allocated grant - * references by blindly guessing their IDs; if this is done, then - * __del_gref will leave them in the queue_gref list. They need to be - * added to the global list so that we can free them when they are no - * longer referenced. - */ - if (unlikely(!list_empty(&queue_gref))) - list_splice_tail(&queue_gref, &gref_list); mutex_unlock(&gref_mutex); return rc; } static void __del_gref(struct gntalloc_gref *gref) { + unsigned long addr; + if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { uint8_t *tmp = kmap(gref->page); tmp[gref->notify.pgoff] = 0; @@ -193,21 +187,16 @@ static void __del_gref(struct gntalloc_g gref->notify.flags = 0; if (gref->gref_id) { - if (gnttab_query_foreign_access(gref->gref_id)) - return; - - if (!gnttab_end_foreign_access_ref(gref->gref_id, 0)) - return; - - gnttab_free_grant_reference(gref->gref_id); + if (gref->page) { + addr = (unsigned long)page_to_virt(gref->page); + gnttab_end_foreign_access(gref->gref_id, 0, addr); + } else + gnttab_free_grant_reference(gref->gref_id); } gref_size--; list_del(&gref->next_gref); - if (gref->page) - __free_page(gref->page); - kfree(gref); }