Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1943528pxp; Thu, 10 Mar 2022 15:40:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzayy5PDo37ojxOIt8YOgWxHDLPP9yoLnD+MHcVwYkMIuwqgtcQ0sK1qvKjFZPIoJRPcX8G X-Received: by 2002:aa7:9199:0:b0:4e1:9243:b625 with SMTP id x25-20020aa79199000000b004e19243b625mr7446556pfa.21.1646955647961; Thu, 10 Mar 2022 15:40:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646955647; cv=none; d=google.com; s=arc-20160816; b=WsTehv40siNsQ6Lgvhed80Oj+IF04iZxR0Zd+RNeodSU/kmEH0C5vq6pnuwLWdWGmm HFL5Eyc4P7nl5hgwnIByfsmEbSs6C2FFlWCAEg2FVyYcje4f/xXLGi1yt3rxTkgTecYN TgRMjNVM+DVKn7my8JVb3ubf5IP77YjF+ywmr1L+JjfFF38nlK302U6d5U96tl7lOJ1i joicCJToL5YNDdMzhVixWL2mdWTLObDbEe3Wa+W4TfEpvh4ifwzrPTKPNskewCI9zEn7 Xzoho6W87KZ4t26ZrAVllnU+4CYZcxus0XlMWzW7yA96vxoIBD0103ZI/fAF/u3h/Itb pUlg== 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=Wt8PYM/Kbo+x5nRMM+3Z2u97qNUJqIdKAozbH8TUFvg=; b=NT6xw9PlPqG0idiPz/XwuRd2vTOEJ0vFDrZUeyZnZXdhne2Bm4RZ/7DCOijfGfYXFe ewNqquU2bMJlN94g2ns3ld56UhCNUN+yfFmplaFJgltO2u7pq6KYlqmBtSRODsgI5e0u w+i6Mn/pB4EXfq/cCJILkjgbFFK3aeGQcBQT5lLIpZwnhbo/MuHj/hVTCrDc26TK3hP0 M8tKRrh5r+RY7yaPBQc/pjv9ccHHogOy8OvKdvRMxeoN1KwXh/SirrGeXu3yvKBUTbAP 0cGnJ/IUsVYniLLAbIrdggi7a7mS/e2xLPwEjP1gITo38QEkrIfoyrXM3jSIYbXQZIPR HFsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qh1+6HPJ; 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 z5-20020a170902ccc500b0014ff95b3920si6460937ple.420.2022.03.10.15.40.31; Thu, 10 Mar 2022 15:40:47 -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=qh1+6HPJ; 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 S243764AbiCJO1L (ORCPT + 99 others); Thu, 10 Mar 2022 09:27:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243746AbiCJOXN (ORCPT ); Thu, 10 Mar 2022 09:23:13 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D1D6158794; Thu, 10 Mar 2022 06:21:14 -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 ams.source.kernel.org (Postfix) with ESMTPS id 4C674B825A7; Thu, 10 Mar 2022 14:21:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EAE7C340F5; Thu, 10 Mar 2022 14:21:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646922071; bh=T/mHRVpyB/klvNBKNmoZm1usKzPz4/NT8r60/gXuNsc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qh1+6HPJAwpLhZerrbYE5m4qhqcGDTakEcjsqBkHe0q82f0z1fq6BLoKBZhoFEjS8 yLjkqWNQX++iiTK6BGcsJurTEWUDHKexZufLWE8LQ8K+cs9d6bzs3hkTpY1asyo7yR aVYj+A5V41h76buxqJEeIgwviYXsNjos5rNceGtk= 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.14 27/31] xen/gntalloc: dont use gnttab_query_foreign_access() Date: Thu, 10 Mar 2022 15:18:40 +0100 Message-Id: <20220310140808.332010211@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220310140807.524313448@linuxfoundation.org> References: <20220310140807.524313448@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 @@ -169,20 +169,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; @@ -196,21 +190,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); }