From nobody Tue May 21 13:45:27 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=1661419603; cv=none; d=zohomail.com; s=zohoarc; b=MGEfqFq8PoLnWfmRSMbpxrliWPB2LvFPtnml+zJs9k3/O9+N3LcDF+gDvM+3bq0dm++sqqCpXoBSmBZktmarxpB7wxN6IwWwe7RkMOsharh4nZQ9RfxfN4FNKC9Tvee2KlxSHrRT6xVlj92pXMLoMOR6J2ZEQHs+nNMZ63pUMzg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661419603; 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=BYfTZFURvyLCFYVU3ltFgIRHLc50Lkg36j+xzInhtWI=; b=atxL5xGTnqUtzKNfW47pDccRrITm0i1TG3Wu8C02C43RN7vjNBLDc6aVF5JEnbt7llaPQ7rEiW1YRUIRSlaZi/60WeaYWGzyWxu2yhlYfwXls+2+lSPT+kaiEJgVMUXglbxezGxQ+uCjs7P5qduC0hXHG+SyB41V6E8PRxWimiU= 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 1661419603681449.4206555604768; Thu, 25 Aug 2022 02:26:43 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.393189.631976 (Exim 4.92) (envelope-from ) id 1oR97q-0004fY-6C; Thu, 25 Aug 2022 09:26:10 +0000 Received: by outflank-mailman (output) from mailman id 393189.631976; Thu, 25 Aug 2022 09:26:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oR97q-0004fR-3I; Thu, 25 Aug 2022 09:26:10 +0000 Received: by outflank-mailman (input) for mailman id 393189; Thu, 25 Aug 2022 09:26:08 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oR97o-0004fL-Lk for xen-devel@lists.xenproject.org; Thu, 25 Aug 2022 09:26:08 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f0989845-2457-11ed-bd2e-47488cf2e6aa; Thu, 25 Aug 2022 11:26:05 +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 4081E5BCEF; Thu, 25 Aug 2022 09:26:03 +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 059DC13517; Thu, 25 Aug 2022 09:26:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id luA+OypAB2M1RgAAMHmgww (envelope-from ); Thu, 25 Aug 2022 09:26:02 +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: f0989845-2457-11ed-bd2e-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1661419563; 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=BYfTZFURvyLCFYVU3ltFgIRHLc50Lkg36j+xzInhtWI=; b=qrtmchGnpdx4Li2CexVjMJHXy+6yN4kZSUZagt6K/wEDzX6fbtPxLO/TYsa0ADZmPk6Dkt K0+ZYizhPYlyPeCuLO6HdEAkq3cpYHG7QUUP+BVo8f0nR91Gcn3E9LNVZPgqPttUFJTOUk uepJ+vC6hOLcHab0OG+L32V3qNARRgs= 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 v2] xen/privcmd: fix error exit of privcmd_ioctl_dm_op() Date: Thu, 25 Aug 2022 11:26:00 +0200 Message-Id: <20220825092600.7188-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: 1661419605871100001 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 patches 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 --- 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() --- drivers/xen/privcmd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c index 3369734108af..7dc62510635e 100644 --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -602,6 +602,10 @@ static int lock_pages( *pinned +=3D page_count; nr_pages -=3D page_count; pages +=3D page_count; + + /* Exact reason isn't known, EFAULT is one possibility. */ + if (page_count < requested) + return -EFAULT; } =20 return 0; @@ -677,10 +681,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 +694,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