stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org,
	Rustam Subkhankulov <subkhankulov@ispras.ru>,
	Juergen Gross <jgross@suse.com>, Jan Beulich <jbeulich@suse.com>,
	Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Subject: [PATCH 5.10 75/86] xen/privcmd: fix error exit of privcmd_ioctl_dm_op()
Date: Mon, 29 Aug 2022 12:59:41 +0200	[thread overview]
Message-ID: <20220829105759.574557229@linuxfoundation.org> (raw)
In-Reply-To: <20220829105756.500128871@linuxfoundation.org>

From: Juergen Gross <jgross@suse.com>

commit c5deb27895e017a0267de0a20d140ad5fcc55a54 upstream.

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: <stable@vger.kernel.org>
Fixes: ab520be8cd5d ("xen/privcmd: Add IOCTL_PRIVCMD_DM_OP")
Reported-by: Rustam Subkhankulov <subkhankulov@ispras.ru>
Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Link: https://lore.kernel.org/r/20220825141918.3581-1-jgross@suse.com
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/xen/privcmd.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

--- 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 f
 	}
 
 	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 f
 	xen_preemptible_hcall_end();
 
 out:
-	unlock_pages(pages, nr_pages);
+	unlock_pages(pages, pinned);
 	kfree(xbufs);
 	kfree(pages);
 	kfree(kbufs);



  parent reply	other threads:[~2022-08-29 12:16 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29 10:58 [PATCH 5.10 00/86] 5.10.140-rc1 review Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 01/86] audit: fix potential double free on error path from fsnotify_add_inode_mark Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 02/86] parisc: Fix exception handler for fldw and fstw instructions Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 03/86] kernel/sys_ni: add compat entry for fadvise64_64 Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 04/86] pinctrl: amd: Dont save/restore interrupt status and wake status bits Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 05/86] xfs: prevent a WARN_ONCE() in xfs_ioc_attr_list() Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 06/86] xfs: reject crazy array sizes being fed to XFS_IOC_GETBMAP* Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 07/86] fs: remove __sync_filesystem Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 08/86] vfs: make sync_filesystem return errors from ->sync_fs Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 09/86] xfs: return errors in xfs_fs_sync_fs Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 10/86] xfs: only bother with sync_filesystem during readonly remount Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 11/86] kernel/sched: Remove dl_boosted flag comment Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 12/86] xfrm: fix refcount leak in __xfrm_policy_check() Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 13/86] xfrm: clone missing x->lastused in xfrm_do_migrate Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 14/86] af_key: Do not call xfrm_probe_algs in parallel Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 15/86] xfrm: policy: fix metadata dst->dev xmit null pointer dereference Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 16/86] NFS: Dont allocate nfs_fattr on the stack in __nfs42_ssc_open() Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 17/86] NFSv4.2 fix problems with __nfs42_ssc_open Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 18/86] SUNRPC: RPC level errors should set task->tk_rpc_status Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 19/86] mm/huge_memory.c: use helper function migration_entry_to_page() Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 20/86] mm/smaps: dont access young/dirty bit if pte unpresent Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 21/86] rose: check NULL rose_loopback_neigh->loopback Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 22/86] nfc: pn533: Fix use-after-free bugs caused by pn532_cmd_timeout Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 23/86] ice: xsk: Force rings to be sized to power of 2 Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 24/86] ice: xsk: prohibit usage of non-balanced queue id Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 25/86] net/mlx5e: Properly disable vlan strip on non-UL reps Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 26/86] net: ipa: dont assume SMEM is page-aligned Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 27/86] net: moxa: get rid of asymmetry in DMA mapping/unmapping Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 28/86] bonding: 802.3ad: fix no transmission of LACPDUs Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 29/86] net: ipvtap - add __init/__exit annotations to module init/exit funcs Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 30/86] netfilter: ebtables: reject blobs that dont provide all entry points Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 31/86] bnxt_en: fix NQ resource accounting during vf creation on 57500 chips Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 32/86] netfilter: nft_payload: report ERANGE for too long offset and length Greg Kroah-Hartman
2022-08-29 10:58 ` [PATCH 5.10 33/86] netfilter: nft_payload: do not truncate csum_offset and csum_type Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 34/86] netfilter: nf_tables: do not leave chain stats enabled on error Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 35/86] netfilter: nft_osf: restrict osf to ipv4, ipv6 and inet families Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 36/86] netfilter: nft_tunnel: restrict it to netdev family Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 37/86] netfilter: nftables: remove redundant assignment of variable err Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 38/86] netfilter: nf_tables: consolidate rule verdict trace call Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 39/86] netfilter: nft_cmp: optimize comparison for 16-bytes Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 40/86] netfilter: bitwise: improve error goto labels Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 41/86] netfilter: nf_tables: upfront validation of data via nft_data_init() Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 42/86] netfilter: nf_tables: disallow jump to implicit chain from set element Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 43/86] netfilter: nf_tables: disallow binding to already bound chain Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 44/86] tcp: tweak len/truesize ratio for coalesce candidates Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 45/86] net: Fix data-races around sysctl_[rw]mem(_offset)? Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 46/86] net: Fix data-races around sysctl_[rw]mem_(max|default) Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 47/86] net: Fix data-races around weight_p and dev_weight_[rt]x_bias Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 48/86] net: Fix data-races around netdev_max_backlog Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 49/86] net: Fix data-races around netdev_tstamp_prequeue Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 50/86] ratelimit: Fix data-races in ___ratelimit() Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 51/86] bpf: Folding omem_charge() into sk_storage_charge() Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 52/86] net: Fix data-races around sysctl_optmem_max Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 53/86] net: Fix a data-race around sysctl_tstamp_allow_data Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 54/86] net: Fix a data-race around sysctl_net_busy_poll Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 55/86] net: Fix a data-race around sysctl_net_busy_read Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 56/86] net: Fix a data-race around netdev_budget Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 57/86] net: Fix a data-race around netdev_budget_usecs Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 58/86] net: Fix data-races around sysctl_fb_tunnels_only_for_init_net Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 59/86] net: Fix data-races around sysctl_devconf_inherit_init_net Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 60/86] net: Fix a data-race around sysctl_somaxconn Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 61/86] ixgbe: stop resetting SYSTIME in ixgbe_ptp_start_cyclecounter Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 62/86] rxrpc: Fix locking in rxrpcs sendmsg Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 63/86] ionic: fix up issues with handling EAGAIN on FW cmds Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 64/86] btrfs: fix silent failure when deleting root reference Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 65/86] btrfs: replace: drop assert for suspended replace Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 66/86] btrfs: add info when mount fails due to stale replace target Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 67/86] btrfs: check if root is readonly while setting security xattr Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 68/86] perf/x86/lbr: Enable the branch type for the Arch LBR by default Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 69/86] x86/unwind/orc: Unwind ftrace trampolines with correct ORC entry Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 70/86] x86/bugs: Add "unknown" reporting for MMIO Stale Data Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 71/86] loop: Check for overflow while configuring loop Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 72/86] asm-generic: sections: refactor memory_intersects Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 73/86] s390: fix double free of GS and RI CBs on fork() failure Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 74/86] ACPI: processor: Remove freq Qos request for all CPUs Greg Kroah-Hartman
2022-08-29 10:59 ` Greg Kroah-Hartman [this message]
2022-08-29 10:59 ` [PATCH 5.10 76/86] mm/hugetlb: fix hugetlb not supporting softdirty tracking Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 77/86] Revert "md-raid: destroy the bitmap after destroying the thread" Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 78/86] md: call __md_stop_writes in md_stop Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 79/86] arm64: Fix match_list for erratum 1286807 on Arm Cortex-A76 Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 80/86] Documentation/ABI: Mention retbleed vulnerability info file for sysfs Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 81/86] blk-mq: fix io hung due to missing commit_rqs Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 82/86] perf python: Fix build when PYTHON_CONFIG is user supplied Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 83/86] perf/x86/intel/uncore: Fix broken read_counter() for SNB IMC PMU Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 84/86] scsi: ufs: core: Enable link lost interrupt Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 85/86] scsi: storvsc: Remove WQ_MEM_RECLAIM from storvsc_error_wq Greg Kroah-Hartman
2022-08-29 10:59 ` [PATCH 5.10 86/86] bpf: Dont use tnum_range on array range checking for poke descriptors Greg Kroah-Hartman
2022-08-29 17:19 ` [PATCH 5.10 00/86] 5.10.140-rc1 review Florian Fainelli
2022-08-29 17:56 ` Slade Watkins
2022-08-29 18:41 ` Pavel Machek
2022-08-29 22:19 ` Shuah Khan
2022-08-30  0:47 ` Guenter Roeck
2022-08-30  2:16 ` Daniel Díaz
2022-08-30 10:21 ` Jon Hunter
2022-08-30 10:41 ` Sudip Mukherjee (Codethink)
2022-08-30 11:56 ` Rudi Heitbaum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220829105759.574557229@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=jbeulich@suse.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=stable@vger.kernel.org \
    --cc=subkhankulov@ispras.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).