All of lore.kernel.org
 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, Shakeel Butt <shakeelb@google.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Dave Hansen <dave.hansen@intel.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Michael Ellermen <mpe@ellerman.id.au>,
	Peter Zijlstra <peterz@infradead.org>,
	Ram Pai <linuxram@us.ibm.com>, Shuah Khan <shuah@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	linux-mm@kvack.org, Ingo Molnar <mingo@kernel.org>
Subject: [PATCH 4.14 32/95] x86/pkeys: Override pkey when moving away from PROT_EXEC
Date: Mon, 21 May 2018 23:11:22 +0200	[thread overview]
Message-ID: <20180521210454.684335787@linuxfoundation.org> (raw)
In-Reply-To: <20180521210447.219380974@linuxfoundation.org>

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

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

From: Dave Hansen <dave.hansen@linux.intel.com>

commit 0a0b152083cfc44ec1bb599b57b7aab41327f998 upstream.

I got a bug report that the following code (roughly) was
causing a SIGSEGV:

	mprotect(ptr, size, PROT_EXEC);
	mprotect(ptr, size, PROT_NONE);
	mprotect(ptr, size, PROT_READ);
	*ptr = 100;

The problem is hit when the mprotect(PROT_EXEC)
is implicitly assigned a protection key to the VMA, and made
that key ACCESS_DENY|WRITE_DENY.  The PROT_NONE mprotect()
failed to remove the protection key, and the PROT_NONE->
PROT_READ left the PTE usable, but the pkey still in place
and left the memory inaccessible.

To fix this, we ensure that we always "override" the pkee
at mprotect() if the VMA does not have execute-only
permissions, but the VMA has the execute-only pkey.

We had a check for PROT_READ/WRITE, but it did not work
for PROT_NONE.  This entirely removes the PROT_* checks,
which ensures that PROT_NONE now works.

Reported-by: Shakeel Butt <shakeelb@google.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michael Ellermen <mpe@ellerman.id.au>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-mm@kvack.org
Cc: stable@vger.kernel.org
Fixes: 62b5f7d013f ("mm/core, x86/mm/pkeys: Add execute-only protection keys support")
Link: http://lkml.kernel.org/r/20180509171351.084C5A71@viggo.jf.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/x86/include/asm/pkeys.h |   12 +++++++++++-
 arch/x86/mm/pkeys.c          |   21 +++++++++++----------
 2 files changed, 22 insertions(+), 11 deletions(-)

--- a/arch/x86/include/asm/pkeys.h
+++ b/arch/x86/include/asm/pkeys.h
@@ -2,6 +2,8 @@
 #ifndef _ASM_X86_PKEYS_H
 #define _ASM_X86_PKEYS_H
 
+#define ARCH_DEFAULT_PKEY	0
+
 #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
 
 extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
@@ -15,7 +17,7 @@ extern int __execute_only_pkey(struct mm
 static inline int execute_only_pkey(struct mm_struct *mm)
 {
 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
-		return 0;
+		return ARCH_DEFAULT_PKEY;
 
 	return __execute_only_pkey(mm);
 }
@@ -56,6 +58,14 @@ bool mm_pkey_is_allocated(struct mm_stru
 		return false;
 	if (pkey >= arch_max_pkey())
 		return false;
+	/*
+	 * The exec-only pkey is set in the allocation map, but
+	 * is not available to any of the user interfaces like
+	 * mprotect_pkey().
+	 */
+	if (pkey == mm->context.execute_only_pkey)
+		return false;
+
 	return mm_pkey_allocation_map(mm) & (1U << pkey);
 }
 
--- a/arch/x86/mm/pkeys.c
+++ b/arch/x86/mm/pkeys.c
@@ -94,26 +94,27 @@ int __arch_override_mprotect_pkey(struct
 	 */
 	if (pkey != -1)
 		return pkey;
-	/*
-	 * Look for a protection-key-drive execute-only mapping
-	 * which is now being given permissions that are not
-	 * execute-only.  Move it back to the default pkey.
-	 */
-	if (vma_is_pkey_exec_only(vma) &&
-	    (prot & (PROT_READ|PROT_WRITE))) {
-		return 0;
-	}
+
 	/*
 	 * The mapping is execute-only.  Go try to get the
 	 * execute-only protection key.  If we fail to do that,
 	 * fall through as if we do not have execute-only
-	 * support.
+	 * support in this mm.
 	 */
 	if (prot == PROT_EXEC) {
 		pkey = execute_only_pkey(vma->vm_mm);
 		if (pkey > 0)
 			return pkey;
+	} else if (vma_is_pkey_exec_only(vma)) {
+		/*
+		 * Protections are *not* PROT_EXEC, but the mapping
+		 * is using the exec-only pkey.  This mapping was
+		 * PROT_EXEC and will no longer be.  Move back to
+		 * the default pkey.
+		 */
+		return ARCH_DEFAULT_PKEY;
 	}
+
 	/*
 	 * This is a vanilla, non-pkey mprotect (or we failed to
 	 * setup execute-only), inherit the pkey from the VMA we

  parent reply	other threads:[~2018-05-21 21:11 UTC|newest]

Thread overview: 103+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-21 21:10 [PATCH 4.14 00/95] 4.14.43-stable review Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 01/95] usbip: usbip_host: refine probe and disconnect debug msgs to be useful Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 02/95] usbip: usbip_host: delete device from busid_table after rebind Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 03/95] usbip: usbip_host: run rebind from exit when module is removed Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 04/95] usbip: usbip_host: fix NULL-ptr deref and use-after-free errors Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 05/95] usbip: usbip_host: fix bad unlock balance during stub_probe() Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 06/95] ALSA: usb: mixer: volume quirk for CM102-A+/102S+ Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 07/95] ALSA: hda: Add Lenovo C50 All in one to the power_save blacklist Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 08/95] ALSA: control: fix a redundant-copy issue Greg Kroah-Hartman
2018-05-21 21:10 ` [PATCH 4.14 09/95] spi: pxa2xx: Allow 64-bit DMA Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 10/95] spi: bcm-qspi: Avoid setting MSPI_CDRAM_PCS for spi-nor master Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 11/95] spi: bcm-qspi: Always read and set BSPI_MAST_N_BOOT_CTRL Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 12/95] KVM: arm/arm64: VGIC/ITS save/restore: protect kvm_read_guest() calls Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 13/95] KVM: arm/arm64: VGIC/ITS: protect kvm_read_guest() calls with SRCU lock Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 14/95] powerpc: Dont preempt_disable() in show_cpuinfo() Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 15/95] vfio: ccw: fix cleanup if cp_prefetch fails Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 16/95] tracing/x86/xen: Remove zero data size trace events trace_xen_mmu_flush_tlb{_all} Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 17/95] tee: shm: fix use-after-free via temporarily dropped reference Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 18/95] netfilter: nf_tables: free set name in error path Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 19/95] netfilter: nf_tables: cant fail after linking rule into active rule list Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 20/95] netfilter: nf_socket: Fix out of bounds access in nf_sk_lookup_slow_v{4,6} Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 21/95] i2c: designware: fix poll-after-enable regression Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 22/95] powerpc/powernv: Fix NVRAM sleep in invalid context when crashing Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 23/95] drm: Match sysfs name in link removal to link creation Greg Kroah-Hartman
2018-05-21 21:11   ` Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 24/95] lib/test_bitmap.c: fix bitmap optimisation tests to report errors correctly Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 25/95] radix tree: fix multi-order iteration race Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 26/95] mm: dont allow deferred pages with NEED_PER_CPU_KM Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 27/95] drm/i915/gen9: Add WaClearHIZ_WM_CHICKEN3 for bxt and glk Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 28/95] s390/qdio: fix access to uninitialized qdio_q fields Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 29/95] s390/cpum_sf: ensure sample frequency of perf event attributes is non-zero Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 30/95] s390/qdio: dont release memory in qdio_setup_irq() Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 31/95] s390: remove indirect branch from do_softirq_own_stack Greg Kroah-Hartman
2018-05-21 21:11 ` Greg Kroah-Hartman [this message]
2018-05-21 21:11 ` [PATCH 4.14 33/95] x86/pkeys: Do not special case protection key 0 Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 34/95] efi: Avoid potential crashes, fix the struct efi_pci_io_protocol_32 definition for mixed mode Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 35/95] ARM: 8771/1: kprobes: Prohibit kprobes on do_undefinstr Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 36/95] x86/mm: Drop TS_COMPAT on 64-bit exec() syscall Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 37/95] tick/broadcast: Use for_each_cpu() specially on UP kernels Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 38/95] ARM: 8769/1: kprobes: Fix to use get_kprobe_ctlblk after irq-disabed Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 39/95] ARM: 8770/1: kprobes: Prohibit probing on optimized_callback Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 40/95] ARM: 8772/1: kprobes: Prohibit kprobes on get_user functions Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 41/95] Btrfs: fix xattr loss after power failure Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 42/95] Btrfs: send, fix invalid access to commit roots due to concurrent snapshotting Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 43/95] btrfs: property: Set incompat flag if lzo/zstd compression is set Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 44/95] btrfs: fix crash when trying to resume balance without the resume flag Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 45/95] btrfs: Split btrfs_del_delalloc_inode into 2 functions Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 46/95] btrfs: Fix delalloc inodes invalidation during transaction abort Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 47/95] btrfs: fix reading stale metadata blocks after degraded raid1 mounts Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 48/95] x86/nospec: Simplify alternative_msr_write() Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 49/95] x86/bugs: Concentrate bug detection into a separate function Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 50/95] x86/bugs: Concentrate bug reporting " Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 51/95] x86/bugs: Read SPEC_CTRL MSR during boot and re-use reserved bits Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 52/95] x86/bugs, KVM: Support the combination of guest and host IBRS Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 53/95] x86/bugs: Expose /sys/../spec_store_bypass Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 54/95] x86/cpufeatures: Add X86_FEATURE_RDS Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 55/95] x86/bugs: Provide boot parameters for the spec_store_bypass_disable mitigation Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 56/95] x86/bugs/intel: Set proper CPU features and setup RDS Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 57/95] x86/bugs: Whitelist allowed SPEC_CTRL MSR values Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 58/95] x86/bugs/AMD: Add support to disable RDS on Fam[15,16,17]h if requested Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 59/95] x86/KVM/VMX: Expose SPEC_CTRL Bit(2) to the guest Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 60/95] x86/speculation: Create spec-ctrl.h to avoid include hell Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 61/95] prctl: Add speculation control prctls Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 62/95] x86/process: Allow runtime control of Speculative Store Bypass Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 63/95] x86/speculation: Add prctl for Speculative Store Bypass mitigation Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 64/95] nospec: Allow getting/setting on non-current task Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 65/95] proc: Provide details on speculation flaw mitigations Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 66/95] seccomp: Enable " Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 67/95] x86/bugs: Make boot modes __ro_after_init Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 68/95] prctl: Add force disable speculation Greg Kroah-Hartman
2018-05-21 21:11 ` [PATCH 4.14 69/95] seccomp: Use PR_SPEC_FORCE_DISABLE Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 70/95] seccomp: Add filter flag to opt-out of SSB mitigation Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 71/95] seccomp: Move speculation migitation control to arch code Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 72/95] x86/speculation: Make "seccomp" the default mode for Speculative Store Bypass Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 73/95] x86/bugs: Rename _RDS to _SSBD Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 74/95] proc: Use underscores for SSBD in status Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 75/95] Documentation/spec_ctrl: Do some minor cleanups Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 76/95] x86/bugs: Fix __ssb_select_mitigation() return type Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 77/95] x86/bugs: Make cpu_show_common() static Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 78/95] x86/bugs: Fix the parameters alignment and missing void Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 79/95] x86/cpu: Make alternative_msr_write work for 32-bit code Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 80/95] KVM: SVM: Move spec control call after restore of GS Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 81/95] x86/speculation: Use synthetic bits for IBRS/IBPB/STIBP Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 82/95] x86/cpufeatures: Disentangle MSR_SPEC_CTRL enumeration from IBRS Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 83/95] x86/cpufeatures: Disentangle SSBD enumeration Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 84/95] x86/cpufeatures: Add FEATURE_ZEN Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 85/95] x86/speculation: Handle HT correctly on AMD Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 86/95] x86/bugs, KVM: Extend speculation control for VIRT_SPEC_CTRL Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 87/95] x86/speculation: Add virtualized speculative store bypass disable support Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 88/95] x86/speculation: Rework speculative_store_bypass_update() Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 89/95] x86/bugs: Unify x86_spec_ctrl_{set_guest,restore_host} Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 90/95] x86/bugs: Expose x86_spec_ctrl_base directly Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 91/95] x86/bugs: Remove x86_spec_ctrl_set() Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 92/95] x86/bugs: Rework spec_ctrl base and mask logic Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 93/95] x86/speculation, KVM: Implement support for VIRT_SPEC_CTRL/LS_CFG Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 94/95] KVM: SVM: Implement VIRT_SPEC_CTRL support for SSBD Greg Kroah-Hartman
2018-05-21 21:12 ` [PATCH 4.14 95/95] x86/bugs: Rename SSBD_NO to SSB_NO Greg Kroah-Hartman
2018-05-22  2:26 ` [PATCH 4.14 00/95] 4.14.43-stable review kernelci.org bot
2018-05-22 11:03 ` Naresh Kamboju
     [not found] ` <CA+res+SgOtr-k80FOBNCczL4adhsqAocFKExoR1-J4-Cq95vQg@mail.gmail.com>
2018-05-22 13:21   ` Jinpu Wang
2018-05-22 14:02     ` Greg Kroah-Hartman
2018-05-22 13:29 ` Guenter Roeck
2018-05-22 20:44 ` Shuah Khan

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=20180521210454.684335787@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linuxram@us.ibm.com \
    --cc=mingo@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=peterz@infradead.org \
    --cc=shakeelb@google.com \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.