Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp717266rwe; Thu, 25 Aug 2022 08:04:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR71zCk1w8XCOYo9tler20IvHqRN1fY+2418j5cuXCDg8G3pI8Z+AIs1XKo1oPru1a4KEJhv X-Received: by 2002:a17:90b:1c0c:b0:1f5:494a:304b with SMTP id oc12-20020a17090b1c0c00b001f5494a304bmr5234174pjb.157.1661439875659; Thu, 25 Aug 2022 08:04:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661439875; cv=none; d=google.com; s=arc-20160816; b=WGSThNkerAedgjTg7t+Z9f+Z7Y8itdoXgk7V0iHot2yKkBVG/0Tah27RDrGgUX9UZ7 xEapVTwGU05rbpuN4D/Kxt22ut+MrnlJuzvJviqLAye2XFGcwYKzGDkso4SLk3X28BPq kmMi8E8MguHMlmrbKsgKi0tOUDBVzPNBxTAUap2YZk7MqGetaUz0gxVsegDU1kVxsPWF ukBNU8dwKqvKwp69igmyt6173AFxq56xOT56q/FduEC1wx6NR5q68Q5bFIzFcHnc1dZB iyiMbc9VwbzZ2KT9lovD9c6sQT2DrhRdjnQ2m6oaVVEhbnFnQRGySKmgoKyIniFV58Wm QIvw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=sNKQlGDAZqQferZnNmp0WSqatXflduAzkdFeb31EiP0=; b=AgdtMPawqExwI5AxXzVtXf23qV6nvPUkplYis9ApK4wSp94Aed+B5Gh+6+x/xKvszG UhRRoO7OqyApsQkfF0G7hDbZh80EFMzoOEEh5vulfNKA+ABRJt/VVrCjns1wqn/oIrpQ frIObVGXv/g2PhIQdUJwfKVFv+2fFGTKsWik0jm5+oyZbAdRrEjZnfu6aWnfg1PJRbSo o8Rv63wNG37ncyihdELogixh09SprS7m5eMO/pTBgeR44o+o3xZKkyGfkTsw0lwqjtP4 oo6DXRX5PUjKnwH6E32vFHAFySHUbRZnYCkg2LcnR4rgvgmff+8roe3hTYuE4RzoDoJp mOnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="sZhw/Mif"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t10-20020a17090aba8a00b001fae6054453si3852182pjr.80.2022.08.25.08.04.22; Thu, 25 Aug 2022 08:04:35 -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=@suse.com header.s=susede1 header.b="sZhw/Mif"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240803AbiHYOUP (ORCPT + 99 others); Thu, 25 Aug 2022 10:20:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237432AbiHYOTX (ORCPT ); Thu, 25 Aug 2022 10:19:23 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B3A1AA4EF; Thu, 25 Aug 2022 07:19:22 -0700 (PDT) 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) 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= 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 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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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 --- 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 = 0; - for (i = 0; i < num; i++) { + for (i = 0; i < num; ) { unsigned int requested; int page_count; requested = DIV_ROUND_UP( offset_in_page(kbufs[i].uptr) + kbufs[i].size, - PAGE_SIZE); + PAGE_SIZE) - off; if (requested > nr_pages) return -ENOSPC; page_count = 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 <= 0) + return page_count ? : -EFAULT; *pinned += page_count; nr_pages -= page_count; pages += page_count; + + off = (requested == page_count) ? 0 : off + page_count; + i += !off; } return 0; @@ -677,10 +680,8 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata) } rc = lock_pages(kbufs, kdata.num, pages, nr_pages, &pinned); - if (rc < 0) { - nr_pages = pinned; + if (rc < 0) goto out; - } for (i = 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(); out: - unlock_pages(pages, nr_pages); + unlock_pages(pages, pinned); kfree(xbufs); kfree(pages); kfree(kbufs); -- 2.35.3