futex: Prevent attaching to kernel threads

We happily allow userspace to declare a random kernel thread to be the
owner of a user space PI futex.

Found while analysing the fallout of Dave Jones syscall fuzzer.

We also should validate the thread group for private futexes and find
some fast way to validate whether the "alleged" owner has RW access on
the file which backs the SHM, but that's a separate issue.

Change-Id: Id43144232efa7c3ae7c630cffa69f55ff83d8fd9
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Dave Jones <davej@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Darren Hart <darren@dvhart.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Clark Williams <williams@redhat.com>
Cc: Paul McKenney <paulmck@linux.vnet.ibm.com>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
Cc: Roland McGrath <roland@hack.frob.com>
Cc: Carlos ODonell <carlos@redhat.com>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: http://lkml.kernel.org/r/20140512201701.194824402@linutronix.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
futex: Add another early deadlock detection check

Dave Jones trinity syscall fuzzer exposed an issue in the deadlock
detection code of rtmutex:
  http://lkml.kernel.org/r/20140429151655.GA14277@redhat.com

That underlying issue has been fixed with a patch to the rtmutex code,
but the futex code must not call into rtmutex in that case because
    - it can detect that issue early
    - it avoids a different and more complex fixup for backing out

If the user space variable got manipulated to 0x80000000 which means
no lock holder, but the waiters bit set and an active pi_state in the
kernel is found we can figure out the recursive locking issue by
looking at the pi_state owner. If that is the current task, then we
can safely return -EDEADLK.

The check should have been added in commit 59fa62451 (futex: Handle
futex_pi OWNER_DIED take over correctly) already, but I did not see
the above issue caused by user space manipulation back then.

Change-Id: I038ce8d61ee0d427574939b048035877149a2981
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Dave Jones <davej@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Darren Hart <darren@dvhart.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Clark Williams <williams@redhat.com>
Cc: Paul McKenney <paulmck@linux.vnet.ibm.com>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
Cc: Roland McGrath <roland@hack.frob.com>
Cc: Carlos ODonell <carlos@redhat.com>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: http://lkml.kernel.org/r/20140512201701.097349971@linutronix.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
futex-prevent-requeue-pi-on-same-futex.patch futex: Forbid uaddr == uaddr2 in futex_requeue(..., requeue_pi=1)

If uaddr == uaddr2, then we have broken the rule of only requeueing
from a non-pi futex to a pi futex with this call. If we attempt this,
then dangling pointers may be left for rt_waiter resulting in an
exploitable condition.

This change brings futex_requeue() into line with
futex_wait_requeue_pi() which performs the same check as per commit
6f7b0a2a5 (futex: Forbid uaddr == uaddr2 in futex_wait_requeue_pi())

[ tglx: Compare the resulting keys as well, as uaddrs might be
  	different depending on the mapping ]

Fixes CVE-2014-3153.

Change-Id: I6bfa0943ee54d6331e6faa7779d621f9cf19fe52
Reported-by: Pinkie Pie
Signed-off-by: Will Drewry <wad@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
futex: Validate atomic acquisition in futex_lock_pi_atomic()

We need to protect the atomic acquisition in the kernel against rogue
user space which sets the user space futex to 0, so the kernel side
acquisition succeeds while there is existing state in the kernel
associated to the real owner.

Verify whether the futex has waiters associated with kernel state. If
it has, return -EINVAL. The state is corrupted already, so no point in
cleaning it up. Subsequent calls will fail as well. Not our problem.

[ tglx: Use futex_top_waiter() and explain why we do not need to try
  	restoring the already corrupted user space state. ]

Change-Id: Ifca31cf91f32a408171a01b8db1e49c14411a776
Signed-off-by: Darren Hart <dvhart@linux.intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Drewry <wad@chromium.org>
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
futex: Always cleanup owner tid in unlock_pi

If the owner died bit is set at futex_unlock_pi, we currently do not
cleanup the user space futex. So the owner TID of the current owner
(the unlocker) persists. That's observable inconsistant state,
especially when the ownership of the pi state got transferred.

Clean it up unconditionally.

Change-Id: I8708a8c38c82ac4565634421fce8f916a6da873b
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Drewry <wad@chromium.org>
Cc: Darren Hart <dvhart@linux.intel.com>
Cc: stable@vger.kernel.org
futex: Make lookup_pi_state more robust

The current implementation of lookup_pi_state has ambigous handling of
the TID value 0 in the user space futex. We can get into the kernel
even if the TID value is 0, because either there is a stale waiters
bit or the owner died bit is set or we are called from the requeue_pi
path or from user space just for fun.

The current code avoids an explicit sanity check for pid = 0 in case
that kernel internal state (waiters) are found for the user space
address. This can lead to state leakage and worse under some
circumstances.

Handle the cases explicit:

     Waiter | pi_state | pi->owner | uTID      | uODIED | ?

[1]  NULL   | ---      | ---       | 0         | 0/1    | Valid
[2]  NULL   | ---      | ---       | >0        | 0/1    | Valid

[3]  Found  | NULL     | --        | Any       | 0/1    | Invalid

[4]  Found  | Found    | NULL      | 0         | 1      | Valid
[5]  Found  | Found    | NULL      | >0        | 1      | Invalid

[6]  Found  | Found    | task      | 0         | 1      | Valid

[7]  Found  | Found    | NULL      | Any       | 0      | Invalid

[8]  Found  | Found    | task      | ==taskTID | 0/1    | Valid
[9]  Found  | Found    | task      | 0         | 0      | Invalid
[10] Found  | Found    | task      | !=taskTID | 0/1    | Invalid

[1]  Indicates that the kernel can acquire the futex atomically. We
     came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.

[2]  Valid, if TID does not belong to a kernel thread. If no matching
     thread is found then it indicates that the owner TID has died.

[3]  Invalid. The waiter is queued on a non PI futex

[4]  Valid state after exit_robust_list(), which sets the user space
     value to FUTEX_WAITERS | FUTEX_OWNER_DIED.

[5]  The user space value got manipulated between exit_robust_list()
     and exit_pi_state_list()

[6]  Valid state after exit_pi_state_list() which sets the new owner in
     the pi_state but cannot access the user space value.

[7]  pi_state->owner can only be NULL when the OWNER_DIED bit is set.

[8]  Owner and user space value match

[9]  There is no transient state which sets the user space TID to 0
     except exit_robust_list(), but this is indicated by the
     FUTEX_OWNER_DIED bit. See [4]

[10] There is no transient state which leaves owner and user space
     TID out of sync.

Backport to 3.13
  conflicts: kernel/futex.c

Change-Id: Ibec82fe154f4ed702332d2d259ec88317b6fede1
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Johansen <john.johansen@canonical.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Drewry <wad@chromium.org>
Cc: Darren Hart <dvhart@linux.intel.com>
Cc: stable@vger.kernel.org
uio: msm_sharedmem: Add new device driver for sharedmemory access

Add new device driver to provide a standard interface for its
clients (like RemoteFS and RFSA) to be able to memory map their
respective allotted shared memory address in the client's address space.
The shared memory (a transport buffer) address is unique for each
individual client and is made available to the driver via device tree.

This driver uses the existing UIO framework to facilitate the clients
mmap requirements. Each individual client will be able to use a unique
UIO device for this purpose.

CRs-Fixed: 477427
Change-Id: If07b88086b5f1b87845962818094644575629fcf
Signed-off-by: Pratibhasagar V <pratibha@codeaurora.org>
Signed-off-by: Venkat Gopalakrishnan <venkatg@codeaurora.org>
uio: msm_shared: Add support for platform data

Update the driver to support backward compatibility for devices
relying on board files for platform data.

CRs-Fixed: 592586
Change-Id: I04d4195a7fd4b926e52b0cbb2bb33cfcc054689f
Signed-off-by: Pratibhasagar V <pratibha@codeaurora.org>
msm: mach: include: add missing lines
Updated iommu/msm_bus

Conflicts:
	arch/arm/mach-msm/include/mach/iommu.h
	arch/arm/mach-msm/include/mach/msm_bus.h
	arch/arm/mach-msm/iommu_domains.c
	arch/arm/mach-msm/msm_bus/Makefile
	arch/arm/mach-msm/msm_bus/msm_bus_arb.c
	arch/arm/mach-msm/msm_bus/msm_bus_bimc.c
	arch/arm/mach-msm/msm_bus/msm_bus_board_8064.c
	arch/arm/mach-msm/msm_bus/msm_bus_board_8960.c
	arch/arm/mach-msm/msm_bus/msm_bus_core.h
	arch/arm/mach-msm/msm_bus/msm_bus_rpm.c
	drivers/iommu/iommu.c
	drivers/iommu/msm_iommu.c
msm-3.4 (commit 35cca8ba3ee0e6a2085dbcac48fb2ccbaa72ba98) video/gpu/iommu .. and all the hacks that goes with that

Conflicts:
	arch/arm/mach-msm/devices-iommu.c
	arch/arm/mach-msm/include/mach/board.h
	arch/arm/mach-msm/include/mach/socinfo.h
	arch/arm/mach-msm/iommu_domains.c
	arch/arm/mach-msm/socinfo.c
	drivers/gpu/ion/Makefile
	drivers/gpu/ion/ion.c
	drivers/gpu/ion/ion_carveout_heap.c
	drivers/gpu/ion/ion_cma_heap.c
	drivers/gpu/ion/ion_cp_heap.c
	drivers/gpu/ion/ion_heap.c
	drivers/gpu/ion/ion_iommu_heap.c
	drivers/gpu/ion/ion_priv.h
	drivers/gpu/ion/ion_system_heap.c
	drivers/gpu/ion/msm/msm_ion.c
	drivers/gpu/msm/Makefile
	drivers/gpu/msm/a3xx_reg.h
	drivers/gpu/msm/adreno.c
	drivers/gpu/msm/adreno.h
	drivers/gpu/msm/adreno_a2xx.c
	drivers/gpu/msm/adreno_a2xx_snapshot.c
	drivers/gpu/msm/adreno_a3xx.c
	drivers/gpu/msm/adreno_a3xx_snapshot.c
	drivers/gpu/msm/adreno_debugfs.c
	drivers/gpu/msm/adreno_drawctxt.c
	drivers/gpu/msm/adreno_drawctxt.h
	drivers/gpu/msm/adreno_pm4types.h
	drivers/gpu/msm/adreno_postmortem.h
	drivers/gpu/msm/adreno_ringbuffer.c
	drivers/gpu/msm/adreno_snapshot.c
	drivers/gpu/msm/kgsl.c
	drivers/gpu/msm/kgsl.h
	drivers/gpu/msm/kgsl_debugfs.c
	drivers/gpu/msm/kgsl_device.h
	drivers/gpu/msm/kgsl_events.c
	drivers/gpu/msm/kgsl_gpummu.c
	drivers/gpu/msm/kgsl_iommu.c
	drivers/gpu/msm/kgsl_iommu.h
	drivers/gpu/msm/kgsl_mmu.c
	drivers/gpu/msm/kgsl_mmu.h
	drivers/gpu/msm/kgsl_pwrctrl.c
	drivers/gpu/msm/kgsl_pwrctrl.h
	drivers/gpu/msm/kgsl_pwrscale_trustzone.c
	drivers/gpu/msm/kgsl_sharedmem.c
	drivers/gpu/msm/kgsl_sharedmem.h
	drivers/gpu/msm/kgsl_snapshot.c
	drivers/gpu/msm/kgsl_snapshot.h
	drivers/gpu/msm/kgsl_sync.c
	drivers/gpu/msm/kgsl_trace.h
	drivers/gpu/msm/z180.c
	drivers/gpu/msm/z180.h
	drivers/gpu/msm2/adreno_a2xx_trace.c
	drivers/gpu/msm2/kgsl_cffdump.c
	drivers/gpu/msm2/kgsl_drm.c
	drivers/iommu/iommu.c
	drivers/video/msm/Kconfig
	drivers/video/msm/external_common.c
	drivers/video/msm/external_common.h
	drivers/video/msm/hdmi_msm.c
	drivers/video/msm/hdmi_msm.h
	drivers/video/msm/mddi_quickvx.c
	drivers/video/msm/mdp.c
	drivers/video/msm/mdp.h
	drivers/video/msm/mdp4.h
	drivers/video/msm/mdp4_overlay.c
	drivers/video/msm/mdp4_overlay_dsi_cmd.c
	drivers/video/msm/mdp4_overlay_dsi_video.c
	drivers/video/msm/mdp4_overlay_dtv.c
	drivers/video/msm/mdp4_overlay_lcdc.c
	drivers/video/msm/mdp4_overlay_mddi.c
	drivers/video/msm/mdp4_overlay_writeback.c
	drivers/video/msm/mdp4_util.c
	drivers/video/msm/mdp_cursor.c
	drivers/video/msm/mdp_debugfs.c
	drivers/video/msm/mdp_dma.c
	drivers/video/msm/mdp_dma_dsi_video.c
	drivers/video/msm/mdp_dma_lcdc.c
	drivers/video/msm/mdp_hw_init.c
	drivers/video/msm/mdp_ppp.c
	drivers/video/msm/mdp_ppp_v20.c
	drivers/video/msm/mdss/Makefile
	drivers/video/msm/mdss/mdss.h
	drivers/video/msm/mdss/mdss_dsi.c
	drivers/video/msm/mdss/mdss_dsi.h
	drivers/video/msm/mdss/mdss_dsi_host.c
	drivers/video/msm/mdss/mdss_dsi_panel.c
	drivers/video/msm/mdss/mdss_fb.c
	drivers/video/msm/mdss/mdss_fb.h
	drivers/video/msm/mdss/mdss_hdmi_mhl.h
	drivers/video/msm/mdss/mdss_mdp.c
	drivers/video/msm/mdss/mdss_mdp.h
	drivers/video/msm/mdss/mdss_mdp_ctl.c
	drivers/video/msm/mdss/mdss_mdp_formats.h
	drivers/video/msm/mdss/mdss_mdp_hwio.h
	drivers/video/msm/mdss/mdss_mdp_intf_video.c
	drivers/video/msm/mdss/mdss_mdp_intf_writeback.c
	drivers/video/msm/mdss/mdss_mdp_overlay.c
	drivers/video/msm/mdss/mdss_mdp_pipe.c
	drivers/video/msm/mdss/mdss_mdp_pp.c
	drivers/video/msm/mdss/mdss_mdp_rotator.c
	drivers/video/msm/mdss/mdss_mdp_rotator.h
	drivers/video/msm/mdss/mdss_mdp_util.c
	drivers/video/msm/mdss/mdss_mdp_wb.c
	drivers/video/msm/mdss/mdss_panel.h
	drivers/video/msm/mdss/mdss_wb.c
	drivers/video/msm/mdss/msm_mdss_io_8974.c
	drivers/video/msm/mhl/mhl_8334.c
	drivers/video/msm/mipi_dsi.c
	drivers/video/msm/mipi_dsi.h
	drivers/video/msm/mipi_dsi_host.c
	drivers/video/msm/mipi_novatek.c
	drivers/video/msm/mipi_novatek_cmd_qhd_pt.c
	drivers/video/msm/mipi_tc358764_dsi2lvds.c
	drivers/video/msm/mipi_toshiba.c
	drivers/video/msm/mipi_truly_tft540960_1_e.c
	drivers/video/msm/msm_dss_io_8960.c
	drivers/video/msm/msm_fb.c
	drivers/video/msm/msm_fb.h
	drivers/video/msm/msm_fb_panel.h
	drivers/video/msm/tvout_msm.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_errors.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_helper.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_interrupt_handler.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_properties.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_shared_mem.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_shared_mem.h
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_utils.c
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_utils.h
	drivers/video/msm/vidc/1080p/ddl/vcd_ddl_vidc.c
	drivers/video/msm/vidc/1080p/ddl/vidc_pix_cache.c
	drivers/video/msm/vidc/1080p/resource_tracker/vcd_res_tracker.c
	drivers/video/msm/vidc/720p/ddl/vcd_ddl.h
	drivers/video/msm/vidc/720p/ddl/vcd_ddl_utils.c
	drivers/video/msm/vidc/720p/resource_tracker/vcd_res_tracker.c
	drivers/video/msm/vidc/common/dec/vdec.c
	drivers/video/msm/vidc/common/enc/venc.c
	drivers/video/msm/vidc/common/enc/venc_internal.c
	drivers/video/msm/vidc/common/vcd/vcd.h
	drivers/video/msm/vidc/common/vcd/vcd_api.c
	drivers/video/msm/vidc/common/vcd/vcd_client_sm.c
	drivers/video/msm/vidc/common/vcd/vcd_device_sm.c
	drivers/video/msm/vidc/common/vcd/vcd_scheduler.c
	drivers/video/msm/vidc/common/vcd/vcd_sub.c
	drivers/video/msm/vidc/common/vcd/vcd_util.h
	fs/proc/meminfo.c
	include/linux/iommu.h
	include/linux/ion.h
	include/linux/msm_ion.h
	include/linux/msm_kgsl.h
	include/linux/msm_mdp.h
	include/linux/msm_vidc_dec.h
	include/linux/msm_vidc_enc.h
	include/media/msm/vcd_api.h
	include/media/msm/vcd_property.h
	mm/oom_kill.c