linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Alex Williamson <alex.williamson@redhat.com>,
	Sasha Levin <sashal@kernel.org>,
	kvm@vger.kernel.org
Subject: [PATCH AUTOSEL 4.19 32/52] vfio/type1: Limit DMA mappings per container
Date: Wed, 24 Apr 2019 10:38:50 -0400	[thread overview]
Message-ID: <20190424143911.28890-32-sashal@kernel.org> (raw)
In-Reply-To: <20190424143911.28890-1-sashal@kernel.org>

From: Alex Williamson <alex.williamson@redhat.com>

[ Upstream commit 492855939bdb59c6f947b0b5b44af9ad82b7e38c ]

Memory backed DMA mappings are accounted against a user's locked
memory limit, including multiple mappings of the same memory.  This
accounting bounds the number of such mappings that a user can create.
However, DMA mappings that are not backed by memory, such as DMA
mappings of device MMIO via mmaps, do not make use of page pinning
and therefore do not count against the user's locked memory limit.
These mappings still consume memory, but the memory is not well
associated to the process for the purpose of oom killing a task.

To add bounding on this use case, we introduce a limit to the total
number of concurrent DMA mappings that a user is allowed to create.
This limit is exposed as a tunable module option where the default
value of 64K is expected to be well in excess of any reasonable use
case (a large virtual machine configuration would typically only make
use of tens of concurrent mappings).

This fixes CVE-2019-3882.

Reviewed-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/vfio/vfio_iommu_type1.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 64cbc2d007c9..c36275754086 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -58,12 +58,18 @@ module_param_named(disable_hugepages,
 MODULE_PARM_DESC(disable_hugepages,
 		 "Disable VFIO IOMMU support for IOMMU hugepages.");
 
+static unsigned int dma_entry_limit __read_mostly = U16_MAX;
+module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644);
+MODULE_PARM_DESC(dma_entry_limit,
+		 "Maximum number of user DMA mappings per container (65535).");
+
 struct vfio_iommu {
 	struct list_head	domain_list;
 	struct vfio_domain	*external_domain; /* domain for external user */
 	struct mutex		lock;
 	struct rb_root		dma_list;
 	struct blocking_notifier_head notifier;
+	unsigned int		dma_avail;
 	bool			v2;
 	bool			nesting;
 };
@@ -836,6 +842,7 @@ static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
 	vfio_unlink_dma(iommu, dma);
 	put_task_struct(dma->task);
 	kfree(dma);
+	iommu->dma_avail++;
 }
 
 static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)
@@ -1110,12 +1117,18 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 		goto out_unlock;
 	}
 
+	if (!iommu->dma_avail) {
+		ret = -ENOSPC;
+		goto out_unlock;
+	}
+
 	dma = kzalloc(sizeof(*dma), GFP_KERNEL);
 	if (!dma) {
 		ret = -ENOMEM;
 		goto out_unlock;
 	}
 
+	iommu->dma_avail--;
 	dma->iova = iova;
 	dma->vaddr = vaddr;
 	dma->prot = prot;
@@ -1612,6 +1625,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)
 
 	INIT_LIST_HEAD(&iommu->domain_list);
 	iommu->dma_list = RB_ROOT;
+	iommu->dma_avail = dma_entry_limit;
 	mutex_init(&iommu->lock);
 	BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier);
 
-- 
2.19.1


  parent reply	other threads:[~2019-04-24 14:42 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-24 14:38 [PATCH AUTOSEL 4.19 01/52] arm64: dts: rockchip: fix rk3328-roc-cc gmac2io tx/rx_delay Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 02/52] HID: logitech: check the return value of create_singlethread_workqueue Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 03/52] HID: debug: fix race condition with between rdesc_show() and device removal Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 04/52] rtc: cros-ec: Fail suspend/resume if wake IRQ can't be configured Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 05/52] rtc: sh: Fix invalid alarm warning for non-enabled alarm Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 06/52] batman-adv: Reduce claim hash refcnt only for removed entry Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 07/52] batman-adv: Reduce tt_local " Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 08/52] batman-adv: Reduce tt_global " Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 09/52] batman-adv: fix warning in function batadv_v_elp_get_throughput Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 10/52] ARM: dts: rockchip: Fix gpu opp node names for rk3288 Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 11/52] reset: meson-audio-arb: Fix missing .owner setting of reset_controller_dev Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 12/52] igb: Fix WARN_ONCE on runtime suspend Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 13/52] fm10k: Fix a potential NULL pointer dereference Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 14/52] riscv: fix accessing 8-byte variable from RV32 Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 15/52] HID: quirks: Fix keyboard + touchpad on Lenovo Miix 630 Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 16/52] net: hns3: fix compile error Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 17/52] net/mlx5: E-Switch, Fix esw manager vport indication for more vport commands Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 18/52] bonding: show full hw address in sysfs for slave entries Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 19/52] net: stmmac: use correct DMA buffer size in the RX descriptor Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 20/52] net: stmmac: ratelimit RX error logs Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 21/52] net: stmmac: don't stop NAPI processing when dropping a packet Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 22/52] net: stmmac: don't overwrite discard_frame status Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 23/52] net: stmmac: fix dropping of multi-descriptor RX frames Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 24/52] net: stmmac: don't log oversized frames Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 25/52] jffs2: fix use-after-free on symlink traversal Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 26/52] debugfs: " Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 27/52] mfd: twl-core: Disable IRQ while suspended Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 28/52] block: use blk_free_flush_queue() to free hctx->fq in blk_mq_init_hctx Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 29/52] rtc: da9063: set uie_unsupported when relevant Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 30/52] HID: input: add mapping for Assistant key Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 31/52] vfio/pci: use correct format characters Sasha Levin
2019-04-24 14:38 ` Sasha Levin [this message]
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 33/52] scsi: core: add new RDAC LENOVO/DE_Series device Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 34/52] scsi: storvsc: Fix calculation of sub-channel count Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 35/52] arm/mach-at91/pm : fix possible object reference leak Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 36/52] arm64: fix wrong check of on_sdei_stack in nmi context Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 37/52] net: hns: fix KASAN: use-after-free in hns_nic_net_xmit_hw() Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 38/52] net: hns: Use NAPI_POLL_WEIGHT for hns driver Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 39/52] net: hns: Fix probabilistic memory overwrite when HNS driver initialized Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 40/52] net: hns: fix ICMP6 neighbor solicitation messages discard problem Sasha Levin
2019-04-24 14:38 ` [PATCH AUTOSEL 4.19 41/52] net: hns: Fix WARNING when remove HNS driver with SMMU enabled Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 42/52] libcxgb: fix incorrect ppmax calculation Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 43/52] KVM: SVM: prevent DBG_DECRYPT and DBG_ENCRYPT overflow Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 44/52] kmemleak: powerpc: skip scanning holes in the .bss section Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 45/52] hugetlbfs: fix memory leak for resv_map Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 46/52] sh: fix multiple function definition build errors Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 47/52] kernel/sysctl.c: fix out-of-bounds access when setting file-max Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 48/52] xsysace: Fix error handling in ace_setup Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 49/52] fs: stream_open - opener for stream-like files so that read and write can run simultaneously without deadlock Sasha Levin
2019-04-24 16:34   ` Greg Kroah-Hartman
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 50/52] ARM: orion: don't use using 64-bit DMA masks Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 51/52] ARM: iop: " Sasha Levin
2019-04-24 14:39 ` [PATCH AUTOSEL 4.19 52/52] aio: fold lookup_kiocb() into its sole caller Sasha Levin

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=20190424143911.28890-32-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=alex.williamson@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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).