stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, "M. Vefa Bicakci" <m.v.b@runbox.com>,
	Demi Marie Obenour <demi@invisiblethingslab.com>,
	Juergen Gross <jgross@suse.com>
Subject: [PATCH 4.19 46/78] xen/gntdev: Prevent leaking grants
Date: Wed,  2 Nov 2022 03:34:31 +0100	[thread overview]
Message-ID: <20221102022054.343934004@linuxfoundation.org> (raw)
In-Reply-To: <20221102022052.895556444@linuxfoundation.org>

From: M. Vefa Bicakci <m.v.b@runbox.com>

commit 0991028cd49567d7016d1b224fe0117c35059f86 upstream.

Prior to this commit, if a grant mapping operation failed partially,
some of the entries in the map_ops array would be invalid, whereas all
of the entries in the kmap_ops array would be valid. This in turn would
cause the following logic in gntdev_map_grant_pages to become invalid:

  for (i = 0; i < map->count; i++) {
    if (map->map_ops[i].status == GNTST_okay) {
      map->unmap_ops[i].handle = map->map_ops[i].handle;
      if (!use_ptemod)
        alloced++;
    }
    if (use_ptemod) {
      if (map->kmap_ops[i].status == GNTST_okay) {
        if (map->map_ops[i].status == GNTST_okay)
          alloced++;
        map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
      }
    }
  }
  ...
  atomic_add(alloced, &map->live_grants);

Assume that use_ptemod is true (i.e., the domain mapping the granted
pages is a paravirtualized domain). In the code excerpt above, note that
the "alloced" variable is only incremented when both kmap_ops[i].status
and map_ops[i].status are set to GNTST_okay (i.e., both mapping
operations are successful).  However, as also noted above, there are
cases where a grant mapping operation fails partially, breaking the
assumption of the code excerpt above.

The aforementioned causes map->live_grants to be incorrectly set. In
some cases, all of the map_ops mappings fail, but all of the kmap_ops
mappings succeed, meaning that live_grants may remain zero. This in turn
makes it impossible to unmap the successfully grant-mapped pages pointed
to by kmap_ops, because unmap_grant_pages has the following snippet of
code at its beginning:

  if (atomic_read(&map->live_grants) == 0)
    return; /* Nothing to do */

In other cases where only some of the map_ops mappings fail but all
kmap_ops mappings succeed, live_grants is made positive, but when the
user requests unmapping the grant-mapped pages, __unmap_grant_pages_done
will then make map->live_grants negative, because the latter function
does not check if all of the pages that were requested to be unmapped
were actually unmapped, and the same function unconditionally subtracts
"data->count" (i.e., a value that can be greater than map->live_grants)
from map->live_grants. The side effects of a negative live_grants value
have not been studied.

The net effect of all of this is that grant references are leaked in one
of the above conditions. In Qubes OS v4.1 (which uses Xen's grant
mechanism extensively for X11 GUI isolation), this issue manifests
itself with warning messages like the following to be printed out by the
Linux kernel in the VM that had granted pages (that contain X11 GUI
window data) to dom0: "g.e. 0x1234 still pending", especially after the
user rapidly resizes GUI VM windows (causing some grant-mapping
operations to partially or completely fail, due to the fact that the VM
unshares some of the pages as part of the window resizing, making the
pages impossible to grant-map from dom0).

The fix for this issue involves counting all successful map_ops and
kmap_ops mappings separately, and then adding the sum to live_grants.
During unmapping, only the number of successfully unmapped grants is
subtracted from live_grants. The code is also modified to check for
negative live_grants values after the subtraction and warn the user.

Link: https://github.com/QubesOS/qubes-issues/issues/7631
Fixes: dbe97cff7dd9 ("xen/gntdev: Avoid blocking in unmap_grant_pages()")
Cc: stable@vger.kernel.org
Signed-off-by: M. Vefa Bicakci <m.v.b@runbox.com>
Acked-by: Demi Marie Obenour <demi@invisiblethingslab.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Link: https://lore.kernel.org/r/20221002222006.2077-2-m.v.b@runbox.com
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/xen/gntdev.c |   22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -387,8 +387,7 @@ int gntdev_map_grant_pages(struct gntdev
 	for (i = 0; i < map->count; i++) {
 		if (map->map_ops[i].status == GNTST_okay) {
 			map->unmap_ops[i].handle = map->map_ops[i].handle;
-			if (!use_ptemod)
-				alloced++;
+			alloced++;
 		} else if (!err)
 			err = -EINVAL;
 
@@ -397,8 +396,7 @@ int gntdev_map_grant_pages(struct gntdev
 
 		if (use_ptemod) {
 			if (map->kmap_ops[i].status == GNTST_okay) {
-				if (map->map_ops[i].status == GNTST_okay)
-					alloced++;
+				alloced++;
 				map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
 			} else if (!err)
 				err = -EINVAL;
@@ -414,8 +412,14 @@ static void __unmap_grant_pages_done(int
 	unsigned int i;
 	struct gntdev_grant_map *map = data->data;
 	unsigned int offset = data->unmap_ops - map->unmap_ops;
+	int successful_unmaps = 0;
+	int live_grants;
 
 	for (i = 0; i < data->count; i++) {
+		if (map->unmap_ops[offset + i].status == GNTST_okay &&
+		    map->unmap_ops[offset + i].handle != -1)
+			successful_unmaps++;
+
 		WARN_ON(map->unmap_ops[offset+i].status &&
 			map->unmap_ops[offset+i].handle != -1);
 		pr_debug("unmap handle=%d st=%d\n",
@@ -423,6 +427,10 @@ static void __unmap_grant_pages_done(int
 			map->unmap_ops[offset+i].status);
 		map->unmap_ops[offset+i].handle = -1;
 		if (use_ptemod) {
+			if (map->kunmap_ops[offset + i].status == GNTST_okay &&
+			    map->kunmap_ops[offset + i].handle != -1)
+				successful_unmaps++;
+
 			WARN_ON(map->kunmap_ops[offset+i].status &&
 				map->kunmap_ops[offset+i].handle != -1);
 			pr_debug("kunmap handle=%u st=%d\n",
@@ -431,11 +439,15 @@ static void __unmap_grant_pages_done(int
 			map->kunmap_ops[offset+i].handle = -1;
 		}
 	}
+
 	/*
 	 * Decrease the live-grant counter.  This must happen after the loop to
 	 * prevent premature reuse of the grants by gnttab_mmap().
 	 */
-	atomic_sub(data->count, &map->live_grants);
+	live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
+	if (WARN_ON(live_grants < 0))
+		pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
+		       __func__, live_grants, successful_unmaps);
 
 	/* Release reference taken by __unmap_grant_pages */
 	gntdev_put_map(NULL, map);



  parent reply	other threads:[~2022-11-02  3:34 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02  2:33 [PATCH 4.19 00/78] 4.19.264-rc1 review Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 01/78] ocfs2: clear dinode links count in case of error Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 02/78] ocfs2: fix BUG when iput after ocfs2_mknod fails Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 03/78] x86/microcode/AMD: Apply the patch early on every logical thread Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 04/78] hwmon/coretemp: Handle large core ID value Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 05/78] ata: ahci-imx: Fix MODULE_ALIAS Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 06/78] ata: ahci: Match EM_MAX_SLOTS with SATA_PMP_MAX_PORTS Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 07/78] KVM: arm64: vgic: Fix exit condition in scan_its_table() Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 08/78] media: venus: dec: Handle the case where find_format fails Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 09/78] arm64: errata: Remove AES hwcap for COMPAT tasks Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 10/78] r8152: add PID for the Lenovo OneLink+ Dock Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 11/78] btrfs: fix processing of delayed data refs during backref walking Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 12/78] btrfs: fix processing of delayed tree block " Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 13/78] ACPI: extlog: Handle multiple records Greg Kroah-Hartman
2022-11-02  2:33 ` [PATCH 4.19 14/78] tipc: Fix recognition of trial period Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 15/78] tipc: fix an information leak in tipc_topsrv_kern_subscr Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 16/78] HID: magicmouse: Do not set BTN_MOUSE on double report Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 17/78] net/atm: fix proc_mpc_write incorrect return value Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 18/78] net: sched: cake: fix null pointer access issue when cake_init() fails Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 19/78] net: hns: fix possible memory leak in hnae_ae_register() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 20/78] iommu/vt-d: Clean up si_domain in the init_dmars() error path Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 21/78] media: v4l2-mem2mem: Apply DST_QUEUE_OFF_BASE on MMAP buffers across ioctls Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 22/78] [PATCH v3] ACPI: video: Force backlight native for more TongFang devices Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 23/78] Makefile.debug: re-enable debug info for .S files Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 24/78] hv_netvsc: Fix race between VF offering and VF association message from host Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 25/78] mm: /proc/pid/smaps_rollup: fix no vmas null-deref Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 26/78] can: kvaser_usb: Fix possible completions during init_completion Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 27/78] ALSA: Use del_timer_sync() before freeing timer Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 28/78] ALSA: au88x0: use explicitly signed char Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 29/78] USB: add RESET_RESUME quirk for NVIDIA Jetson devices in RCM Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 30/78] usb: dwc3: gadget: Stop processing more requests on IMI Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 31/78] usb: dwc3: gadget: Dont set IMI for no_interrupt Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 32/78] usb: bdc: change state when port disconnected Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 33/78] usb: xhci: add XHCI_SPURIOUS_SUCCESS to ASM1042 despite being a V0.96 controller Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 34/78] xhci: Remove device endpoints from bandwidth list when freeing the device Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 35/78] tools: iio: iio_utils: fix digit calculation Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 36/78] iio: light: tsl2583: Fix module unloading Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 37/78] fbdev: smscufx: Fix several use-after-free bugs Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 38/78] mac802154: Fix LQI recording Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 39/78] drm/msm/dsi: fix memory corruption with too many bridges Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 40/78] drm/msm/hdmi: " Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 41/78] mmc: core: Fix kernel panic when remove non-standard SDIO card Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 42/78] kernfs: fix use-after-free in __kernfs_remove Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 43/78] perf auxtrace: Fix address filter symbol name match for modules Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 44/78] s390/futex: add missing EX_TABLE entry to __futex_atomic_op() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 45/78] Xen/gntdev: dont ignore kernel unmapping error Greg Kroah-Hartman
2022-11-02  2:34 ` Greg Kroah-Hartman [this message]
2022-11-02  2:34 ` [PATCH 4.19 47/78] mm,hugetlb: take hugetlb_lock before decrementing h->resv_huge_pages Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 48/78] net: ieee802154: fix error return code in dgram_bind() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 49/78] drm/msm: Fix return type of mdp4_lvds_connector_mode_valid Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 50/78] arc: iounmap() arg is volatile Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 51/78] ALSA: ac97: fix possible memory leak in snd_ac97_dev_register() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 52/78] tipc: fix a null-ptr-deref in tipc_topsrv_accept Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 53/78] net: netsec: fix error handling in netsec_register_mdio() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 54/78] x86/unwind/orc: Fix unreliable stack dump with gcov Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 55/78] amd-xgbe: fix the SFP compliance codes check for DAC cables Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 56/78] amd-xgbe: add the bit rate quirk for Molex cables Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 57/78] kcm: annotate data-races around kcm->rx_psock Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 58/78] kcm: annotate data-races around kcm->rx_wait Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 59/78] net: fix UAF issue in nfqnl_nf_hook_drop() when ops_init() failed Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 60/78] net: lantiq_etop: dont free skb when returning NETDEV_TX_BUSY Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 61/78] tcp: fix indefinite deferral of RTO with SACK reneging Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 62/78] can: mscan: mpc5xxx: mpc5xxx_can_probe(): add missing put_clock() in error path Greg Kroah-Hartman
2022-11-04 17:28   ` Pavel Machek
2022-11-02  2:34 ` [PATCH 4.19 63/78] PM: hibernate: Allow hybrid sleep to work with s2idle Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 64/78] media: vivid: s_fbuf: add more sanity checks Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 65/78] media: vivid: dev->bitmap_cap wasnt freed in all cases Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 66/78] media: v4l2-dv-timings: add sanity checks for blanking values Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 67/78] media: videodev2.h: V4L2_DV_BT_BLANKING_HEIGHT should check interlaced Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 68/78] i40e: Fix ethtool rx-flow-hash setting for X722 Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 69/78] i40e: Fix VF hang when reset is triggered on another VF Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 70/78] i40e: Fix flow-type by setting GL_HASH_INSET registers Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 71/78] net: ksz884x: fix missing pci_disable_device() on error in pcidev_init() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 72/78] PM: domains: Fix handling of unavailable/disabled idle states Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 73/78] ALSA: aoa: i2sbus: fix possible memory leak in i2sbus_add_dev() Greg Kroah-Hartman
2022-11-02  2:34 ` [PATCH 4.19 74/78] ALSA: aoa: Fix I2S device accounting Greg Kroah-Hartman
2022-11-02  2:35 ` [PATCH 4.19 75/78] openvswitch: switch from WARN to pr_warn Greg Kroah-Hartman
2022-11-02  2:35 ` [PATCH 4.19 76/78] net: ehea: fix possible memory leak in ehea_register_port() Greg Kroah-Hartman
2022-11-02  2:35 ` [PATCH 4.19 77/78] net/mlx5e: Do not increment ESN when updating IPsec ESN state Greg Kroah-Hartman
2022-11-02  2:35 ` [PATCH 4.19 78/78] can: rcar_canfd: rcar_canfd_handle_global_receive(): fix IRQ storm on global FIFO receive Greg Kroah-Hartman
2022-11-02 17:22 ` [PATCH 4.19 00/78] 4.19.264-rc1 review Pavel Machek
2022-11-02 20:46 ` Guenter Roeck
2022-11-03 10:18 ` Naresh Kamboju
2022-11-03 12:22 ` Sudip Mukherjee
2022-11-04 15:17 ` zhouzhixiu

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=20221102022054.343934004@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=demi@invisiblethingslab.com \
    --cc=jgross@suse.com \
    --cc=m.v.b@runbox.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /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).