From nobody Tue May 21 13:24:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1661437207; cv=none; d=zohomail.com; s=zohoarc; b=NbVG4KoiR3zKKqPjTT+O3/wzoEa2oA+U3agDqhkQKKALK8LQOSG6ZOQ3SPHRHvGgnYwLPMPNJG68gzFOvpSLhEfcKUs611QtFRo2fxWdga52s8hNoCjfm67EXTuPzuiuz7rJjQHQBc8EIrXul4dPuBIWBGqY6a4t16bZhzcibtc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661437207; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=sNKQlGDAZqQferZnNmp0WSqatXflduAzkdFeb31EiP0=; b=f2TAVtR0BrfxwzJTqC0im69GHzCahcudkz5aCPrtyMn3vdbIJpuzeoUVwjOdTZVvg0ssC66DAFVTK851JfOEt5/mdocWRhWf7KMV1d1oxmlkhmysK1lZXBUDGN8Nsr/KPFb47SAh7DM9WDuPevTENImW6ZuYVnicZ86kpoUr5JU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1661437207803473.52807149025716; Thu, 25 Aug 2022 07:20:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.393435.632384 (Exim 4.92) (envelope-from ) id 1oRDhc-0002ib-Vc; Thu, 25 Aug 2022 14:19:24 +0000 Received: by outflank-mailman (output) from mailman id 393435.632384; Thu, 25 Aug 2022 14:19:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oRDhc-0002iU-T3; Thu, 25 Aug 2022 14:19:24 +0000 Received: by outflank-mailman (input) for mailman id 393435; Thu, 25 Aug 2022 14:19:23 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oRDhb-0002iO-3W for xen-devel@lists.xenproject.org; Thu, 25 Aug 2022 14:19:23 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e0e6d7e5-2480-11ed-9250-1f966e50362f; Thu, 25 Aug 2022 16:19:06 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C8D465BE56; Thu, 25 Aug 2022 14:19:20 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 8E14413517; Thu, 25 Aug 2022 14:19:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id XCoxIeiEB2M8SAAAMHmgww (envelope-from ); Thu, 25 Aug 2022 14:19:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e0e6d7e5-2480-11ed-9250-1f966e50362f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1661437160; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=sNKQlGDAZqQferZnNmp0WSqatXflduAzkdFeb31EiP0=; b=sZhw/MifAZ9FmVNdpEIeJr63FRoVAiv5dCw037wAAehLMI2JBd1YOH7DY7jAQvDEFppZS5 Zutm6Gs1TXuG7MekvCt8ADUq28/EbOU5OjEgnfDVPV583ehUMykLGYu14gmz/0EDjqm8kR R7km1sxINwFnA7KxTi+0X0MlcG9X2l8= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko , stable@vger.kernel.org, Rustam Subkhankulov Subject: [PATCH v4] xen/privcmd: fix error exit of privcmd_ioctl_dm_op() Date: Thu, 25 Aug 2022 16:19:18 +0200 Message-Id: <20220825141918.3581-1-jgross@suse.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1661437210463100001 Content-Type: text/plain; charset="utf-8" The error exit of privcmd_ioctl_dm_op() is calling unlock_pages() potentially with pages being NULL, leading to a NULL dereference. Additionally lock_pages() doesn't check for pin_user_pages_fast() having been completely successful, resulting in potentially not locking all pages into memory. This could result in sporadic failures when using the related memory in user mode. Fix all of that by calling unlock_pages() always with the real number of pinned pages, which will be zero in case pages being NULL, and by checking the number of pages pinned by pin_user_pages_fast() matching the expected number of pages. Cc: Fixes: ab520be8cd5d ("xen/privcmd: Add IOCTL_PRIVCMD_DM_OP") Reported-by: Rustam Subkhankulov Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich Reviewed-by: Oleksandr Tyshchenko --- V2: - use "pinned" as parameter for unlock_pages() (Jan Beulich) - drop label "unlock" again (Jan Beulich) - add check for complete success of pin_user_pages_fast() V3: - continue after partial success of pin_user_pages_fast() (Jan Beulich) V4: - fix case of multiple partial successes for one buffer (Jan Beulich) --- drivers/xen/privcmd.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c index 3369734108af..e88e8f6f0a33 100644 --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -581,27 +581,30 @@ static int lock_pages( struct privcmd_dm_op_buf kbufs[], unsigned int num, struct page *pages[], unsigned int nr_pages, unsigned int *pinned) { - unsigned int i; + unsigned int i, off =3D 0; =20 - for (i =3D 0; i < num; i++) { + for (i =3D 0; i < num; ) { unsigned int requested; int page_count; =20 requested =3D DIV_ROUND_UP( offset_in_page(kbufs[i].uptr) + kbufs[i].size, - PAGE_SIZE); + PAGE_SIZE) - off; if (requested > nr_pages) return -ENOSPC; =20 page_count =3D pin_user_pages_fast( - (unsigned long) kbufs[i].uptr, + (unsigned long)kbufs[i].uptr + off * PAGE_SIZE, requested, FOLL_WRITE, pages); - if (page_count < 0) - return page_count; + if (page_count <=3D 0) + return page_count ? : -EFAULT; =20 *pinned +=3D page_count; nr_pages -=3D page_count; pages +=3D page_count; + + off =3D (requested =3D=3D page_count) ? 0 : off + page_count; + i +=3D !off; } =20 return 0; @@ -677,10 +680,8 @@ static long privcmd_ioctl_dm_op(struct file *file, voi= d __user *udata) } =20 rc =3D lock_pages(kbufs, kdata.num, pages, nr_pages, &pinned); - if (rc < 0) { - nr_pages =3D pinned; + if (rc < 0) goto out; - } =20 for (i =3D 0; i < kdata.num; i++) { set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr); @@ -692,7 +693,7 @@ static long privcmd_ioctl_dm_op(struct file *file, void= __user *udata) xen_preemptible_hcall_end(); =20 out: - unlock_pages(pages, nr_pages); + unlock_pages(pages, pinned); kfree(xbufs); kfree(pages); kfree(kbufs); --=20 2.35.3