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, Greg Hackmann <ghackmann@google.com>
Subject: [PATCH 4.9 063/107] staging: android: ion: fix ION_IOC_{MAP,SHARE} use-after-free
Date: Mon,  3 Sep 2018 18:56:27 +0200	[thread overview]
Message-ID: <20180903165640.363207389@linuxfoundation.org> (raw)
In-Reply-To: <20180903165637.293735109@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Greg Hackmann <ghackmann@android.com>

This patch is 4.9.y only.  Kernels 4.12 and later are unaffected, since
all the underlying ion_handle infrastructure has been ripped out.

The ION_IOC_{MAP,SHARE} ioctls drop and reacquire client->lock several
times while operating on one of the client's ion_handles.  This creates
windows where userspace can call ION_IOC_FREE on the same client with
the same handle, and effectively make the kernel drop its own reference.
For example:

- thread A: ION_IOC_ALLOC creates an ion_handle with refcount 1
- thread A: starts ION_IOC_MAP and increments the refcount to 2
- thread B: ION_IOC_FREE decrements the refcount to 1
- thread B: ION_IOC_FREE decrements the refcount to 0 and frees the
            handle
- thread A: continues ION_IOC_MAP with a dangling ion_handle * to
            freed memory

Fix this by holding client->lock for the duration of
ION_IOC_{MAP,SHARE}, preventing the concurrent ION_IOC_FREE.  Also
remove ion_handle_get_by_id(), since there's literally no way to use it
safely.

Cc: stable@vger.kernel.org # v4.11-
Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/staging/android/ion/ion-ioctl.c |   12 +++++---
 drivers/staging/android/ion/ion.c       |   48 +++++++++++++++++++-------------
 drivers/staging/android/ion/ion_priv.h  |    6 ++--
 3 files changed, 40 insertions(+), 26 deletions(-)

--- a/drivers/staging/android/ion/ion-ioctl.c
+++ b/drivers/staging/android/ion/ion-ioctl.c
@@ -128,11 +128,15 @@ long ion_ioctl(struct file *filp, unsign
 	{
 		struct ion_handle *handle;
 
-		handle = ion_handle_get_by_id(client, data.handle.handle);
-		if (IS_ERR(handle))
+		mutex_lock(&client->lock);
+		handle = ion_handle_get_by_id_nolock(client, data.handle.handle);
+		if (IS_ERR(handle)) {
+			mutex_unlock(&client->lock);
 			return PTR_ERR(handle);
-		data.fd.fd = ion_share_dma_buf_fd(client, handle);
-		ion_handle_put(handle);
+		}
+		data.fd.fd = ion_share_dma_buf_fd_nolock(client, handle);
+		ion_handle_put_nolock(handle);
+		mutex_unlock(&client->lock);
 		if (data.fd.fd < 0)
 			ret = data.fd.fd;
 		break;
--- a/drivers/staging/android/ion/ion.c
+++ b/drivers/staging/android/ion/ion.c
@@ -352,18 +352,6 @@ struct ion_handle *ion_handle_get_by_id_
 	return handle ? handle : ERR_PTR(-EINVAL);
 }
 
-struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
-					       int id)
-{
-	struct ion_handle *handle;
-
-	mutex_lock(&client->lock);
-	handle = ion_handle_get_by_id_nolock(client, id);
-	mutex_unlock(&client->lock);
-
-	return handle;
-}
-
 static bool ion_handle_validate(struct ion_client *client,
 				struct ion_handle *handle)
 {
@@ -1029,24 +1017,28 @@ static struct dma_buf_ops dma_buf_ops =
 	.kunmap = ion_dma_buf_kunmap,
 };
 
-struct dma_buf *ion_share_dma_buf(struct ion_client *client,
-				  struct ion_handle *handle)
+static struct dma_buf *__ion_share_dma_buf(struct ion_client *client,
+					   struct ion_handle *handle,
+					   bool lock_client)
 {
 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
 	struct ion_buffer *buffer;
 	struct dma_buf *dmabuf;
 	bool valid_handle;
 
-	mutex_lock(&client->lock);
+	if (lock_client)
+		mutex_lock(&client->lock);
 	valid_handle = ion_handle_validate(client, handle);
 	if (!valid_handle) {
 		WARN(1, "%s: invalid handle passed to share.\n", __func__);
-		mutex_unlock(&client->lock);
+		if (lock_client)
+			mutex_unlock(&client->lock);
 		return ERR_PTR(-EINVAL);
 	}
 	buffer = handle->buffer;
 	ion_buffer_get(buffer);
-	mutex_unlock(&client->lock);
+	if (lock_client)
+		mutex_unlock(&client->lock);
 
 	exp_info.ops = &dma_buf_ops;
 	exp_info.size = buffer->size;
@@ -1061,14 +1053,21 @@ struct dma_buf *ion_share_dma_buf(struct
 
 	return dmabuf;
 }
+
+struct dma_buf *ion_share_dma_buf(struct ion_client *client,
+				  struct ion_handle *handle)
+{
+	return __ion_share_dma_buf(client, handle, true);
+}
 EXPORT_SYMBOL(ion_share_dma_buf);
 
-int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle)
+static int __ion_share_dma_buf_fd(struct ion_client *client,
+				  struct ion_handle *handle, bool lock_client)
 {
 	struct dma_buf *dmabuf;
 	int fd;
 
-	dmabuf = ion_share_dma_buf(client, handle);
+	dmabuf = __ion_share_dma_buf(client, handle, lock_client);
 	if (IS_ERR(dmabuf))
 		return PTR_ERR(dmabuf);
 
@@ -1078,8 +1077,19 @@ int ion_share_dma_buf_fd(struct ion_clie
 
 	return fd;
 }
+
+int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle)
+{
+	return __ion_share_dma_buf_fd(client, handle, true);
+}
 EXPORT_SYMBOL(ion_share_dma_buf_fd);
 
+int ion_share_dma_buf_fd_nolock(struct ion_client *client,
+				struct ion_handle *handle)
+{
+	return __ion_share_dma_buf_fd(client, handle, false);
+}
+
 struct ion_handle *ion_import_dma_buf(struct ion_client *client,
 				      struct dma_buf *dmabuf)
 {
--- a/drivers/staging/android/ion/ion_priv.h
+++ b/drivers/staging/android/ion/ion_priv.h
@@ -463,11 +463,11 @@ void ion_free_nolock(struct ion_client *
 
 int ion_handle_put_nolock(struct ion_handle *handle);
 
-struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
-						int id);
-
 int ion_handle_put(struct ion_handle *handle);
 
 int ion_query_heaps(struct ion_client *client, struct ion_heap_query *query);
 
+int ion_share_dma_buf_fd_nolock(struct ion_client *client,
+				struct ion_handle *handle);
+
 #endif /* _ION_PRIV_H */

  parent reply	other threads:[~2018-09-03 16:56 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-03 16:55 [PATCH 4.9 000/107] 4.9.125-stable review Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 001/107] vti6: fix PMTU caching and reporting on xmit Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 002/107] xfrm: fix missing dst_release() after policy blocking lbcast and multicast Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 003/107] xfrm: free skb if nlsk pointer is NULL Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 004/107] mac80211: add stations tied to AP_VLANs during hw reconfig Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 005/107] nl80211: Add a missing break in parse_station_flags Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 006/107] drm/bridge: adv7511: Reset registers on hotplug Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 007/107] scsi: libiscsi: fix possible NULL pointer dereference in case of TMF Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 008/107] drm/imx: imx-ldb: disable LDB on driver bind Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 009/107] drm/imx: imx-ldb: check if channel is enabled before printing warning Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 010/107] usb: gadget: r8a66597: Fix two possible sleep-in-atomic-context bugs in init_controller() Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 011/107] usb: gadget: r8a66597: Fix a possible sleep-in-atomic-context bugs in r8a66597_queue() Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 012/107] usb/phy: fix PPC64 build errors in phy-fsl-usb.c Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 014/107] usb: gadget: f_uac2: fix endianness of struct cntrl_*_lay3 Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 015/107] bpf, ppc64: fix unexpected r0=0 exit path inside bpf_xadd Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 016/107] tools/power turbostat: fix -S on UP systems Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 017/107] net: caif: Add a missing rcu_read_unlock() in caif_flow_cb Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 018/107] qed: Fix possible race for the link state value Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 019/107] qed: Correct Multicast API to reflect existence of 256 approximate buckets Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 020/107] atl1c: reserve min skb headroom Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 021/107] net: prevent ISA drivers from building on PPC32 Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 022/107] can: mpc5xxx_can: check of_iomap return before use Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 023/107] i2c: davinci: Avoid zero value of CLKH Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 024/107] perf/x86/amd/ibs: Dont access non-started event Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 025/107] media: staging: omap4iss: Include asm/cacheflush.h after generic includes Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 026/107] bnx2x: Fix invalid memory access in rss hash config path Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 028/107] net: axienet: Fix double deregister of mdio Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 029/107] x86/boot: Fix if_changed build flip/flop bug Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 030/107] fscache: Allow cancelled operations to be enqueued Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 031/107] cachefiles: Fix refcounting bug in backing-file read monitoring Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 032/107] cachefiles: Wait rather than BUGing on "Unexpected object collision" Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 033/107] selftests/ftrace: Add snapshot and tracing_on test case Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 034/107] zswap: re-check zswap_is_full() after do zswap_shrink() Greg Kroah-Hartman
2018-09-03 16:55 ` [PATCH 4.9 035/107] tools/power turbostat: Read extended processor family from CPUID Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 037/107] enic: handle mtu change for vf properly Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 038/107] arc: [plat-eznps] fix data type errors in platform headers Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 039/107] arc: fix build errors in arc/include/asm/delay.h Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 040/107] arc: fix type warnings in arc/mm/cache.c Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 041/107] squashfs metadata 2: electric boogaloo Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 043/107] drivers: net: lmc: fix case value for target abort error Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 044/107] memcg: remove memcg_cgroup::id from IDR on mem_cgroup_css_alloc() failure Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 045/107] scsi: fcoe: drop frames in ELS LOGO error path Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 046/107] scsi: fcoe: clear FC_RP_STARTED flags when receiving a LOGO Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 047/107] scsi: vmw_pvscsi: Return DID_RESET for status SAM_STAT_COMMAND_TERMINATED Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 048/107] mm/memory.c: check return value of ioremap_prot Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 049/107] sched/sysctl: Check user input value of sysctl_sched_time_avg Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 050/107] Cipso: cipso_v4_optptr enter infinite loop Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 051/107] mei: dont update offset in write Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 052/107] cifs: add missing debug entries for kconfig options Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 053/107] cifs: check kmalloc before use Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 054/107] smb3: enumerating snapshots was leaving part of the data off end Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 055/107] smb3: Do not send SMB3 SET_INFO if nothing changed Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 056/107] smb3: dont request leases in symlink creation and query Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 057/107] kprobes/arm64: Fix %p uses in error messages Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 058/107] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 059/107] s390/kvm: fix deadlock when killed by oom Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 060/107] ext4: check for NUL characters in extended attributes name Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 061/107] ext4: sysfs: print ext4_super_block fields as little-endian Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 062/107] ext4: reset error code in ext4_find_entry in fallback Greg Kroah-Hartman
2018-09-03 16:56 ` Greg Kroah-Hartman [this message]
2018-09-03 16:56 ` [PATCH 4.9 064/107] KVM: arm/arm64: Skip updating PTE entry if no change Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 065/107] KVM: arm/arm64: Skip updating PMD " Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 067/107] x86/speculation/l1tf: Fix overflow in l1tf_pfn_limit() on 32bit Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 068/107] x86/speculation/l1tf: Fix off-by-one error when warning that system has too much RAM Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 069/107] x86/speculation/l1tf: Suggest what to do on systems with " Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 070/107] x86/process: Re-export start_thread() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 072/107] x86/kvm/vmx: Remove duplicate l1d flush definitions Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 073/107] fuse: Dont access pipe->buffers without pipe_lock() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 074/107] fuse: fix initial parallel dirops Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 075/107] fuse: fix double request_end() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 076/107] fuse: fix unlocked access to processing queue Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 077/107] fuse: umount should wait for all requests Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 078/107] fuse: Fix oops at process_init_reply() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 079/107] fuse: Add missed unlock_page() to fuse_readpages_fill() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 080/107] udl-kms: change down_interruptible to down Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 081/107] udl-kms: handle allocation failure Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 082/107] udl-kms: fix crash due to uninitialized memory Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 083/107] b43legacy/leds: Ensure NUL-termination of LED name string Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 084/107] b43/leds: " Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 085/107] ASoC: dpcm: dont merge format from invalid codec dai Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 086/107] ASoC: sirf: Fix potential NULL pointer dereference Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 088/107] x86/irqflags: Mark native_restore_fl extern inline Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 089/107] x86/spectre: Add missing family 6 check to microcode check Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 090/107] x86/speculation/l1tf: Increase l1tf memory limit for Nehalem+ Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 091/107] x86/entry/64: Wipe KASAN stack shadow before rewind_stack_do_exit() Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 092/107] s390: fix br_r1_trampoline for machines without exrl Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 093/107] s390/qdio: reset old sbal_state flags Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 094/107] s390/numa: move initial setup of node_to_cpumask_map Greg Kroah-Hartman
2018-09-03 16:56 ` [PATCH 4.9 095/107] s390/pci: fix out of bounds access during irq setup Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 096/107] kprobes: Make list and blacklist root user read only Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 097/107] MIPS: Correct the 64-bit DSP accumulator register size Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 098/107] MIPS: lib: Provide MIPS64r6 __multi3() for GCC < 7 Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 099/107] scsi: sysfs: Introduce sysfs_{un,}break_active_protection() Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 100/107] scsi: core: Avoid that SCSI device removal through sysfs triggers a deadlock Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 101/107] iscsi target: fix session creation failure handling Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 102/107] clk: rockchip: fix clk_i2sout parent selection bits on rk3399 Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 103/107] PM / clk: signedness bug in of_pm_clk_add_clks() Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 104/107] power: generic-adc-battery: fix out-of-bounds write when copying channel properties Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 105/107] power: generic-adc-battery: check for duplicate properties copied from iio channels Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 106/107] cdrom: Fix info leak/OOB read in cdrom_ioctl_drive_status Greg Kroah-Hartman
2018-09-03 16:57 ` [PATCH 4.9 107/107] staging: android: ion: check for kref overflow Greg Kroah-Hartman
2018-09-04  8:22 ` [PATCH 4.9 000/107] 4.9.125-stable review Naresh Kamboju
2018-09-04 19:33   ` Greg Kroah-Hartman
2018-09-04 19:29 ` Shuah Khan
2018-09-04 22:51 ` Guenter Roeck

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=20180903165640.363207389@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ghackmann@google.com \
    --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).