Jesse Keating 7a32965
--- a/arch/x86/include/asm/desc.h
Jesse Keating 7a32965
+++ b/arch/x86/include/asm/desc.h
Jesse Keating 7a32965
@@ -5,6 +5,7 @@
Jesse Keating 7a32965
 #include <asm/ldt.h>
Jesse Keating 7a32965
 #include <asm/mmu.h>
Jesse Keating 7a32965
 #include <linux/smp.h>
Jesse Keating 7a32965
+#include <linux/mm_types.h>
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 static inline void fill_ldt(struct desc_struct *desc,
Jesse Keating 7a32965
 			    const struct user_desc *info)
Jesse Keating 7a32965
@@ -93,6 +94,9 @@ static inline int desc_empty(const void *ptr)
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 #define load_TLS(t, cpu) native_load_tls(t, cpu)
Jesse Keating 7a32965
 #define set_ldt native_set_ldt
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+#define load_user_cs_desc native_load_user_cs_desc
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 #define write_ldt_entry(dt, entry, desc)	\
Jesse Keating 7a32965
 	native_write_ldt_entry(dt, entry, desc)
Jesse Keating 7a32965
@@ -392,4 +396,25 @@ static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
Jesse Keating 7a32965
 	_set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+static inline void set_user_cs(struct desc_struct *desc, unsigned long limit)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	limit = (limit - 1) / PAGE_SIZE;
Jesse Keating 7a32965
+	desc->a = limit & 0xffff;
Jesse Keating 7a32965
+	desc->b = (limit & 0xf0000) | 0x00c0fb00;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static inline void native_load_user_cs_desc(int cpu, struct mm_struct *mm)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	get_cpu_gdt_table(cpu)[GDT_ENTRY_DEFAULT_USER_CS] = (mm)->context.user_cs;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#define arch_add_exec_range arch_add_exec_range
Jesse Keating 7a32965
+#define arch_remove_exec_range arch_remove_exec_range
Jesse Keating 7a32965
+#define arch_flush_exec_range arch_flush_exec_range
Jesse Keating 7a32965
+extern void arch_add_exec_range(struct mm_struct *mm, unsigned long limit);
Jesse Keating 7a32965
+extern void arch_remove_exec_range(struct mm_struct *mm, unsigned long limit);
Jesse Keating 7a32965
+extern void arch_flush_exec_range(struct mm_struct *mm);
Jesse Keating 7a32965
+#endif /* CONFIG_X86_32 */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #endif /* _ASM_X86_DESC_H */
Jesse Keating 7a32965
--- a/arch/x86/include/asm/mmu.h
Jesse Keating 7a32965
+++ b/arch/x86/include/asm/mmu.h
Jesse Keating 7a32965
@@ -7,12 +7,19 @@
Jesse Keating 7a32965
 /*
Jesse Keating 7a32965
  * The x86 doesn't have a mmu context, but
Jesse Keating 7a32965
  * we put the segment information here.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * exec_limit is used to track the range PROT_EXEC
Jesse Keating 7a32965
+ * mappings span.
Jesse Keating 7a32965
  */
Jesse Keating 7a32965
 typedef struct {
Jesse Keating 7a32965
 	void *ldt;
Jesse Keating 7a32965
 	int size;
Jesse Keating 7a32965
 	struct mutex lock;
Jesse Keating 7a32965
 	void *vdso;
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	struct desc_struct user_cs;
Jesse Keating 7a32965
+	unsigned long exec_limit;
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
 } mm_context_t;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 #ifdef CONFIG_SMP
Jesse Keating 7a32965
--- a/arch/x86/include/asm/paravirt.h
Jesse Keating 7a32965
+++ b/arch/x86/include/asm/paravirt.h
Jesse Keating 7a32965
@@ -289,6 +289,12 @@ static inline void set_ldt(const void *addr, unsigned entries)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+static inline void load_user_cs_desc(unsigned int cpu, struct mm_struct *mm)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	PVOP_VCALL2(pv_cpu_ops.load_user_cs_desc, cpu, mm);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
 static inline void store_gdt(struct desc_ptr *dtr)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
Jesse Keating 7a32965
--- a/arch/x86/include/asm/paravirt_types.h
Jesse Keating 7a32965
+++ b/arch/x86/include/asm/paravirt_types.h
Jesse Keating 7a32965
@@ -118,6 +118,9 @@ struct pv_cpu_ops {
Jesse Keating 7a32965
 	void (*store_gdt)(struct desc_ptr *);
Jesse Keating 7a32965
 	void (*store_idt)(struct desc_ptr *);
Jesse Keating 7a32965
 	void (*set_ldt)(const void *desc, unsigned entries);
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	void (*load_user_cs_desc)(int cpu, struct mm_struct *mm);
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
 	unsigned long (*store_tr)(void);
Jesse Keating 7a32965
 	void (*load_tls)(struct thread_struct *t, unsigned int cpu);
Jesse Keating 7a32965
 #ifdef CONFIG_X86_64
Jesse Keating 7a32965
--- a/arch/x86/kernel/cpu/common.c
Jesse Keating 7a32965
+++ b/arch/x86/kernel/cpu/common.c
Jesse Keating 7a32965
@@ -802,6 +802,22 @@ static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
Jesse Keating 7a32965
 	/* Filter out anything that depends on CPUID levels we don't have */
Jesse Keating 7a32965
 	filter_cpuid_features(c, true);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	/*
Jesse Keating 7a32965
+	 *  emulation of NX with segment limits unfortunately means
Jesse Keating 7a32965
+	 *  we have to disable the fast system calls, due to the way that
Jesse Keating 7a32965
+	 *  sysexit clears the segment limits on return.
Jesse Keating 7a32965
+	 *  If we have either disabled exec-shield on the boot command line,
Jesse Keating 7a32965
+	 *  or we have NX, then we don't need to do this.
Jesse Keating 7a32965
+	 */
Jesse Keating 7a32965
+	if (exec_shield != 0) {
Jesse Keating 7a32965
+#ifdef CONFIG_X86_PAE
Jesse Keating 7a32965
+		if (!test_cpu_cap(c, X86_FEATURE_NX))
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+			clear_cpu_cap(c, X86_FEATURE_SEP);
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	/* If the model name is still unset, do table lookup. */
Jesse Keating 7a32965
 	if (!c->x86_model_id[0]) {
Jesse Keating 7a32965
 		const char *p;
Jesse Keating 7a32965
--- a/arch/x86/kernel/paravirt.c
Jesse Keating 7a32965
+++ b/arch/x86/kernel/paravirt.c
Jesse Keating 7a32965
@@ -345,6 +345,9 @@ struct pv_cpu_ops pv_cpu_ops = {
Jesse Keating 7a32965
 	.read_tscp = native_read_tscp,
Jesse Keating 7a32965
 	.load_tr_desc = native_load_tr_desc,
Jesse Keating 7a32965
 	.set_ldt = native_set_ldt,
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	.load_user_cs_desc = native_load_user_cs_desc,
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
 	.load_gdt = native_load_gdt,
Jesse Keating 7a32965
 	.load_idt = native_load_idt,
Jesse Keating 7a32965
 	.store_gdt = native_store_gdt,
Jesse Keating 7a32965
--- a/arch/x86/kernel/process_32.c
Jesse Keating 7a32965
+++ b/arch/x86/kernel/process_32.c
Jesse Keating 7a32965
@@ -243,7 +243,10 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
Jesse Keating 7a32965
 void
Jesse Keating 7a32965
 start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
+	int cpu;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	set_user_gs(regs, 0);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	regs->fs		= 0;
Jesse Keating 7a32965
 	set_fs(USER_DS);
Jesse Keating 7a32965
 	regs->ds		= __USER_DS;
Jesse Keating 7a32965
@@ -252,6 +255,11 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
Jesse Keating 7a32965
 	regs->cs		= __USER_CS;
Jesse Keating 7a32965
 	regs->ip		= new_ip;
Jesse Keating 7a32965
 	regs->sp		= new_sp;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	cpu = get_cpu();
Jesse Keating 7a32965
+	load_user_cs_desc(cpu, current->mm);
Jesse Keating 7a32965
+	put_cpu();
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	/*
Jesse Keating 7a32965
 	 * Free the old FP and other extended state
Jesse Keating 7a32965
 	 */
Jesse Keating 7a32965
@@ -311,6 +319,9 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Jesse Keating 7a32965
 	if (preload_fpu)
Jesse Keating 7a32965
 		prefetch(next->fpu.state);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+	if (next_p->mm)
Jesse Keating 7a32965
+		load_user_cs_desc(cpu, next_p->mm);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	/*
Jesse Keating 7a32965
 	 * Reload esp0.
Jesse Keating 7a32965
 	 */
Jesse Keating 7a32965
@@ -404,3 +415,40 @@ unsigned long get_wchan(struct task_struct *p)
Jesse Keating 7a32965
 	return 0;
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+static void modify_cs(struct mm_struct *mm, unsigned long limit)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	mm->context.exec_limit = limit;
Jesse Keating 7a32965
+	set_user_cs(&mm->context.user_cs, limit);
Jesse Keating 7a32965
+	if (mm == current->mm) {
Jesse Keating 7a32965
+		int cpu;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+		cpu = get_cpu();
Jesse Keating 7a32965
+		load_user_cs_desc(cpu, mm);
Jesse Keating 7a32965
+		put_cpu();
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+void arch_add_exec_range(struct mm_struct *mm, unsigned long limit)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	if (limit > mm->context.exec_limit)
Jesse Keating 7a32965
+		modify_cs(mm, limit);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+void arch_remove_exec_range(struct mm_struct *mm, unsigned long old_end)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	struct vm_area_struct *vma;
Jesse Keating 7a32965
+	unsigned long limit = PAGE_SIZE;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (old_end == mm->context.exec_limit) {
Jesse Keating 7a32965
+		for (vma = mm->mmap; vma; vma = vma->vm_next)
Jesse Keating 7a32965
+			if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
Jesse Keating 7a32965
+				limit = vma->vm_end;
Jesse Keating 7a32965
+		modify_cs(mm, limit);
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+void arch_flush_exec_range(struct mm_struct *mm)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	mm->context.exec_limit = 0;
Jesse Keating 7a32965
+	set_user_cs(&mm->context.user_cs, 0);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
--- a/arch/x86/kernel/traps.c
Jesse Keating 7a32965
+++ b/arch/x86/kernel/traps.c
Jesse Keating 7a32965
@@ -109,6 +109,78 @@ static inline void preempt_conditional_cli(struct pt_regs *regs)
Jesse Keating 7a32965
 	dec_preempt_count();
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+static inline int
Jesse Keating 7a32965
+__compare_user_cs_desc(const struct desc_struct *desc1,
Jesse Keating 7a32965
+	const struct desc_struct *desc2)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	return ((desc1->limit0 != desc2->limit0) ||
Jesse Keating 7a32965
+		(desc1->limit != desc2->limit) ||
Jesse Keating 7a32965
+		(desc1->base0 != desc2->base0) ||
Jesse Keating 7a32965
+		(desc1->base1 != desc2->base1) ||
Jesse Keating 7a32965
+		(desc1->base2 != desc2->base2));
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ * lazy-check for CS validity on exec-shield binaries:
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * the original non-exec stack patch was written by
Jesse Keating 7a32965
+ * Solar Designer <solar at openwall.com>. Thanks!
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+static int
Jesse Keating 7a32965
+check_lazy_exec_limit(int cpu, struct pt_regs *regs, long error_code)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	struct desc_struct *desc1, *desc2;
Jesse Keating 7a32965
+	struct vm_area_struct *vma;
Jesse Keating 7a32965
+	unsigned long limit;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (current->mm == NULL)
Jesse Keating 7a32965
+		return 0;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	limit = -1UL;
Jesse Keating 7a32965
+	if (current->mm->context.exec_limit != -1UL) {
Jesse Keating 7a32965
+		limit = PAGE_SIZE;
Jesse Keating 7a32965
+		spin_lock(&current->mm->page_table_lock);
Jesse Keating 7a32965
+		for (vma = current->mm->mmap; vma; vma = vma->vm_next)
Jesse Keating 7a32965
+			if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
Jesse Keating 7a32965
+				limit = vma->vm_end;
Jesse Keating 7a32965
+		vma = get_gate_vma(current);
Jesse Keating 7a32965
+		if (vma && (vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
Jesse Keating 7a32965
+			limit = vma->vm_end;
Jesse Keating 7a32965
+		spin_unlock(&current->mm->page_table_lock);
Jesse Keating 7a32965
+		if (limit >= TASK_SIZE)
Jesse Keating 7a32965
+			limit = -1UL;
Jesse Keating 7a32965
+		current->mm->context.exec_limit = limit;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+	set_user_cs(&current->mm->context.user_cs, limit);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	desc1 = &current->mm->context.user_cs;
Jesse Keating 7a32965
+	desc2 = get_cpu_gdt_table(cpu) + GDT_ENTRY_DEFAULT_USER_CS;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (__compare_user_cs_desc(desc1, desc2)) {
Jesse Keating 7a32965
+		/*
Jesse Keating 7a32965
+		 * The CS was not in sync - reload it and retry the
Jesse Keating 7a32965
+		 * instruction. If the instruction still faults then
Jesse Keating 7a32965
+		 * we won't hit this branch next time around.
Jesse Keating 7a32965
+		 */
Jesse Keating 7a32965
+		if (print_fatal_signals >= 2) {
Jesse Keating 7a32965
+			printk(KERN_ERR "#GPF fixup (%ld[seg:%lx]) at %08lx, CPU#%d.\n",
Jesse Keating 7a32965
+				error_code, error_code/8, regs->ip,
Jesse Keating 7a32965
+				smp_processor_id());
Jesse Keating 7a32965
+			printk(KERN_ERR "exec_limit: %08lx, user_cs: %08x/%08x, CPU_cs: %08x/%08x.\n",
Jesse Keating 7a32965
+				current->mm->context.exec_limit,
Jesse Keating 7a32965
+				desc1->a, desc1->b, desc2->a, desc2->b);
Jesse Keating 7a32965
+		}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+		load_user_cs_desc(cpu, current->mm);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+		return 1;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	return 0;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 static void __kprobes
Jesse Keating 7a32965
 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
Jesse Keating 7a32965
 	long error_code, siginfo_t *info)
Jesse Keating 7a32965
@@ -265,6 +337,29 @@ do_general_protection(struct pt_regs *regs, long error_code)
Jesse Keating 7a32965
 	if (!user_mode(regs))
Jesse Keating 7a32965
 		goto gp_in_kernel;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	int cpu;
Jesse Keating 7a32965
+	int ok;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	cpu = get_cpu();
Jesse Keating 7a32965
+	ok = check_lazy_exec_limit(cpu, regs, error_code);
Jesse Keating 7a32965
+	put_cpu();
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (ok)
Jesse Keating 7a32965
+		return;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (print_fatal_signals) {
Jesse Keating 7a32965
+		printk(KERN_ERR "#GPF(%ld[seg:%lx]) at %08lx, CPU#%d.\n",
Jesse Keating 7a32965
+			error_code, error_code/8, regs->ip, smp_processor_id());
Jesse Keating 7a32965
+		printk(KERN_ERR "exec_limit: %08lx, user_cs: %08x/%08x.\n",
Jesse Keating 7a32965
+			current->mm->context.exec_limit,
Jesse Keating 7a32965
+			current->mm->context.user_cs.a,
Jesse Keating 7a32965
+			current->mm->context.user_cs.b);
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	tsk->thread.error_code = error_code;
Jesse Keating 7a32965
 	tsk->thread.trap_no = 13;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
@@ -792,19 +887,37 @@ do_device_not_available(struct pt_regs *regs, long error_code)
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 #ifdef CONFIG_X86_32
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ * The fixup code for errors in iret jumps to here (iret_exc). It loses
Jesse Keating 7a32965
+ * the original trap number and erorr code. The bogus trap 32 and error
Jesse Keating 7a32965
+ * code 0 are what the vanilla kernel delivers via:
Jesse Keating 7a32965
+ * DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0, 1)
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * NOTE: Because of the final "1" in the macro we need to enable interrupts.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * In case of a general protection fault in the iret instruction, we
Jesse Keating 7a32965
+ * need to check for a lazy CS update for exec-shield.
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
 dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
-	siginfo_t info;
Jesse Keating 7a32965
+	int ok;
Jesse Keating 7a32965
+	int cpu;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	local_irq_enable();
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	info.si_signo = SIGILL;
Jesse Keating 7a32965
-	info.si_errno = 0;
Jesse Keating 7a32965
-	info.si_code = ILL_BADSTK;
Jesse Keating 7a32965
-	info.si_addr = NULL;
Jesse Keating 7a32965
-	if (notify_die(DIE_TRAP, "iret exception",
Jesse Keating 7a32965
-			regs, error_code, 32, SIGILL) == NOTIFY_STOP)
Jesse Keating 7a32965
-		return;
Jesse Keating 7a32965
-	do_trap(32, SIGILL, "iret exception", regs, error_code, &info;;
Jesse Keating 7a32965
+	cpu = get_cpu();
Jesse Keating 7a32965
+	ok = check_lazy_exec_limit(cpu, regs, error_code);
Jesse Keating 7a32965
+	put_cpu();
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (!ok && notify_die(DIE_TRAP, "iret exception", regs,
Jesse Keating 7a32965
+		error_code, 32, SIGSEGV) != NOTIFY_STOP) {
Jesse Keating 7a32965
+			siginfo_t info;
Jesse Keating 7a32965
+			info.si_signo = SIGSEGV;
Jesse Keating 7a32965
+			info.si_errno = 0;
Jesse Keating 7a32965
+			info.si_code = ILL_BADSTK;
Jesse Keating 7a32965
+			info.si_addr = 0;
Jesse Keating 7a32965
+			do_trap(32, SIGSEGV, "iret exception", regs, error_code, &info;;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 #endif
Jesse Keating 7a32965
 
Jesse Keating 7a32965
--- a/arch/x86/mm/setup_nx.c
Jesse Keating 7a32965
+++ b/arch/x86/mm/setup_nx.c
Jesse Keating 7a32965
@@ -1,3 +1,4 @@
Jesse Keating 7a32965
+#include <linux/sched.h>
Jesse Keating 7a32965
 #include <linux/spinlock.h>
Jesse Keating 7a32965
 #include <linux/errno.h>
Jesse Keating 7a32965
 #include <linux/init.h>
Jesse Keating 7a32965
@@ -23,6 +24,7 @@ static int __init noexec_setup(char *str)
Jesse Keating 7a32965
 		disable_nx = 0;
Jesse Keating 7a32965
 	} else if (!strncmp(str, "off", 3)) {
Jesse Keating 7a32965
 		disable_nx = 1;
Jesse Keating 7a32965
+		exec_shield = 0;
Jesse Keating 7a32965
 	}
Jesse Keating 7a32965
 	x86_configure_nx();
Jesse Keating 7a32965
 	return 0;
Jesse Keating 7a32965
@@ -40,6 +42,10 @@ void __cpuinit x86_configure_nx(void)
Jesse Keating 7a32965
 void __init x86_report_nx(void)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	if (!cpu_has_nx) {
Jesse Keating 7a32965
+		if (exec_shield)
Jesse Keating 7a32965
+			printk(KERN_INFO "Using x86 segment limits to approximate NX protection\n");
Jesse Keating 7a32965
+		else
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 		printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
Jesse Keating 7a32965
 		       "missing in CPU or disabled in BIOS!\n");
Jesse Keating 7a32965
 	} else {
Jesse Keating 7a32965
--- a/arch/x86/mm/tlb.c
Jesse Keating 7a32965
+++ b/arch/x86/mm/tlb.c
Jesse Keating 7a32965
@@ -6,6 +6,7 @@
Jesse Keating 7a32965
 #include <linux/interrupt.h>
Jesse Keating 7a32965
 #include <linux/module.h>
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#include <asm/desc.h>
Jesse Keating 7a32965
 #include <asm/tlbflush.h>
Jesse Keating 7a32965
 #include <asm/mmu_context.h>
Jesse Keating 7a32965
 #include <asm/cache.h>
Jesse Keating 7a32965
@@ -131,6 +132,12 @@ void smp_invalidate_interrupt(struct pt_regs *regs)
Jesse Keating 7a32965
 	union smp_flush_state *f;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	cpu = smp_processor_id();
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	if (current->active_mm)
Jesse Keating 7a32965
+		load_user_cs_desc(cpu, current->active_mm);
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	/*
Jesse Keating 7a32965
 	 * orig_rax contains the negated interrupt vector.
Jesse Keating 7a32965
 	 * Use that to determine where the sender put the data.
Jesse Keating 7a32965
--- a/arch/x86/xen/enlighten.c
Jesse Keating 7a32965
+++ b/arch/x86/xen/enlighten.c
Jesse Keating 7a32965
@@ -335,6 +335,24 @@ static void xen_set_ldt(const void *addr, unsigned entries)
Jesse Keating 7a32965
 	xen_mc_issue(PARAVIRT_LAZY_CPU);
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+static void xen_load_user_cs_desc(int cpu, struct mm_struct *mm)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	void *gdt;
Jesse Keating 7a32965
+	xmaddr_t mgdt;
Jesse Keating 7a32965
+	u64 descriptor;
Jesse Keating 7a32965
+	struct desc_struct user_cs;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	gdt = &get_cpu_gdt_table(cpu)[GDT_ENTRY_DEFAULT_USER_CS];
Jesse Keating 7a32965
+	mgdt = virt_to_machine(gdt);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	user_cs = mm->context.user_cs;
Jesse Keating 7a32965
+	descriptor = (u64) user_cs.a | ((u64) user_cs.b) << 32;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	HYPERVISOR_update_descriptor(mgdt.maddr, descriptor);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 static void xen_load_gdt(const struct desc_ptr *dtr)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	unsigned long va = dtr->address;
Jesse Keating 7a32965
@@ -961,6 +979,9 @@ static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	.load_tr_desc = paravirt_nop,
Jesse Keating 7a32965
 	.set_ldt = xen_set_ldt,
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	.load_user_cs_desc = xen_load_user_cs_desc,
Jesse Keating 7a32965
+#endif /*CONFIG_X86_32*/
Jesse Keating 7a32965
 	.load_gdt = xen_load_gdt,
Jesse Keating 7a32965
 	.load_idt = xen_load_idt,
Jesse Keating 7a32965
 	.load_tls = xen_load_tls,
Jesse Keating 7a32965
--- a/fs/binfmt_elf.c
Jesse Keating 7a32965
+++ b/fs/binfmt_elf.c
Jesse Keating 7a32965
@@ -717,6 +722,15 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
Jesse Keating 7a32965
 	if (retval)
Jesse Keating 7a32965
 		goto out_free_dentry;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	/*
Jesse Keating 7a32965
+	 * Turn off the CS limit completely if exec-shield disabled or
Jesse Keating 7a32965
+	 * NX active:
Jesse Keating 7a32965
+	 */
Jesse Keating 7a32965
+	if (!exec_shield || executable_stack != EXSTACK_DISABLE_X || (__supported_pte_mask & _PAGE_NX))
Jesse Keating 7a32965
+		arch_add_exec_range(current->mm, -1);
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	/* OK, This is the point of no return */
Jesse Keating 7a32965
 	current->flags &= ~PF_FORKNOEXEC;
Jesse Keating 7a32965
 	current->mm->def_flags = def_flags;
Jesse Keating 7a32965
--- a/include/linux/sched.h
Jesse Keating 7a32965
+++ b/include/linux/sched.h
Jesse Keating 7a32965
@@ -101,6 +101,9 @@ struct bio_list;
Jesse Keating 7a32965
 struct fs_struct;
Jesse Keating 7a32965
 struct perf_event_context;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+extern int exec_shield;
Jesse Keating 7a32965
+extern int print_fatal_signals;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 /*
Jesse Keating 7a32965
  * List of flags we want to share for kernel threads,
Jesse Keating 7a32965
  * if only because they are not used by them anyway.
Jesse Keating 7a32965
--- a/kernel/sysctl.c
Jesse Keating 7a32965
+++ b/kernel/sysctl.c
Jesse Keating 7a32965
@@ -101,6 +101,17 @@ extern int sysctl_nr_open_min, sysctl_nr_open_max;
Jesse Keating 7a32965
 #ifndef CONFIG_MMU
Jesse Keating 7a32965
 extern int sysctl_nr_trim_pages;
Jesse Keating 7a32965
 #endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+int exec_shield = 1;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static int __init setup_exec_shield(char *str)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	get_option(&str, &exec_shield);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	return 1;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+__setup("exec-shield=", setup_exec_shield);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #ifdef CONFIG_BLOCK
Jesse Keating 7a32965
 extern int blk_iopoll_enabled;
Jesse Keating 7a32965
 #endif
Jesse Keating 7a32965
@@ -428,6 +448,16 @@ static struct ctl_table kern_table[] = {
Jesse Keating 7a32965
 		.mode		= 0644,
Jesse Keating 7a32965
 		.proc_handler	= proc_dointvec,
Jesse Keating 7a32965
 	},
Jesse Keating 7a32965
+#ifdef CONFIG_X86_32
Jesse Keating 7a32965
+	{
Jesse Keating 7a32965
+		.procname	= "exec-shield",
Jesse Keating 7a32965
+		.data		= &exec_shield,
Jesse Keating 7a32965
+		.maxlen		= sizeof(int),
Jesse Keating 7a32965
+		.mode		= 0644,
Jesse Keating 7a32965
+		.proc_handler	= &proc_dointvec,
Jesse Keating 7a32965
+	},
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #ifdef CONFIG_PROC_SYSCTL
Jesse Keating 7a32965
 	{
Jesse Keating 7a32965
 		.procname	= "tainted",
Jesse Keating 7a32965
--- b/mm/mmap.c
Jesse Keating 7a32965
+++ b/mm/mmap.c
Jesse Keating 7a32965
@@ -44,6 +45,18 @@
Jesse Keating 7a32965
 #define arch_rebalance_pgtables(addr, len)		(addr)
Jesse Keating 7a32965
 #endif
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+/* No sane architecture will #define these to anything else */
Jesse Keating 7a32965
+#ifndef arch_add_exec_range
Jesse Keating 7a32965
+#define arch_add_exec_range(mm, limit)	do { ; } while (0)
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+#ifndef arch_flush_exec_range
Jesse Keating 7a32965
+#define arch_flush_exec_range(mm)	do { ; } while (0)
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+#ifndef arch_remove_exec_range
Jesse Keating 7a32965
+#define arch_remove_exec_range(mm, limit)	do { ; } while (0)
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 static void unmap_region(struct mm_struct *mm,
Jesse Keating 7a32965
 		struct vm_area_struct *vma, struct vm_area_struct *prev,
Jesse Keating 7a32965
 		unsigned long start, unsigned long end);
Jesse Keating 7a32965
@@ -388,6 +401,8 @@
Jesse Keating 7a32965
 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
Jesse Keating 7a32965
 		struct vm_area_struct *prev, struct rb_node *rb_parent)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
+	if (vma->vm_flags & VM_EXEC)
Jesse Keating 7a32965
+		arch_add_exec_range(mm, vma->vm_end);
Jesse Keating 7a32965
 	if (prev) {
Jesse Keating 7a32965
 		vma->vm_next = prev->vm_next;
Jesse Keating 7a32965
 		prev->vm_next = vma;
Jesse Keating 7a32965
@@ -489,6 +504,8 @@
Jesse Keating 7a32965
 	rb_erase(&vma->vm_rb, &mm->mm_rb);
Jesse Keating 7a32965
 	if (mm->mmap_cache == vma)
Jesse Keating 7a32965
 		mm->mmap_cache = prev;
Jesse Keating 7a32965
+	if (vma->vm_flags & VM_EXEC)
Jesse Keating 7a32965
+		arch_remove_exec_range(mm, vma->vm_end);
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 /*
Jesse Keating 7a32965
@@ -790,6 +807,8 @@
Jesse Keating 7a32965
 		} else					/* cases 2, 5, 7 */
Jesse Keating 7a32965
 			err = vma_adjust(prev, prev->vm_start,
Jesse Keating 7a32965
 				end, prev->vm_pgoff, NULL);
Jesse Keating 7a32965
+		if (prev->vm_flags & VM_EXEC)
Jesse Keating 7a32965
+			arch_add_exec_range(mm, prev->vm_end);
Jesse Keating 7a32965
 		if (err)
Jesse Keating 7a32965
 			return NULL;
Jesse Keating 7a32965
 		return prev;
Jesse Keating 7a32965
@@ -1966,10 +2075,14 @@
Jesse Keating 7a32965
 	if (new->vm_ops && new->vm_ops->open)
Jesse Keating 7a32965
 		new->vm_ops->open(new);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	if (new_below)
Jesse Keating 7a32965
+	if (new_below) {
Jesse Keating 7a32965
+		unsigned long old_end = vma->vm_end;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 		err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Jesse Keating 7a32965
 			((addr - new->vm_start) >> PAGE_SHIFT), new);
Jesse Keating 7a32965
-	else
Jesse Keating 7a32965
+		if (vma->vm_flags & VM_EXEC)
Jesse Keating 7a32965
+			arch_remove_exec_range(mm, old_end);
Jesse Keating 7a32965
+	} else
Jesse Keating 7a32965
 		err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	/* Success. */
Jesse Keating 7a32965
@@ -2254,6 +2367,7 @@
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
Jesse Keating 7a32965
 	tlb_finish_mmu(tlb, 0, end);
Jesse Keating 7a32965
+	arch_flush_exec_range(mm);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	/*
Jesse Keating 7a32965
 	 * Walk the list again, actually closing and freeing it,
Jesse Keating 7a32965
--- a/mm/mprotect.c
Jesse Keating 7a32965
+++ b/mm/mprotect.c
Jesse Keating 7a32965
@@ -25,9 +25,14 @@
Jesse Keating 7a32965
 #include <linux/perf_event.h>
Jesse Keating 7a32965
 #include <asm/uaccess.h>
Jesse Keating 7a32965
 #include <asm/pgtable.h>
Jesse Keating 7a32965
+#include <asm/pgalloc.h>
Jesse Keating 7a32965
 #include <asm/cacheflush.h>
Jesse Keating 7a32965
 #include <asm/tlbflush.h>
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#ifndef arch_remove_exec_range
Jesse Keating 7a32965
+#define arch_remove_exec_range(mm, limit)      do { ; } while (0)
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #ifndef pgprot_modify
Jesse Keating 7a32965
 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
@@ -138,7 +143,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
Jesse Keating 7a32965
 	struct mm_struct *mm = vma->vm_mm;
Jesse Keating 7a32965
 	unsigned long oldflags = vma->vm_flags;
Jesse Keating 7a32965
 	long nrpages = (end - start) >> PAGE_SHIFT;
Jesse Keating 7a32965
-	unsigned long charged = 0;
Jesse Keating 7a32965
+	unsigned long charged = 0, old_end = vma->vm_end;
Jesse Keating 7a32965
 	pgoff_t pgoff;
Jesse Keating 7a32965
 	int error;
Jesse Keating 7a32965
 	int dirty_accountable = 0;
Jesse Keating 7a32965
@@ -203,6 +208,9 @@ success:
Jesse Keating 7a32965
 		dirty_accountable = 1;
Jesse Keating 7a32965
 	}
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+	if (oldflags & VM_EXEC)
Jesse Keating 7a32965
+		arch_remove_exec_range(current->mm, old_end);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 	mmu_notifier_invalidate_range_start(mm, start, end);
Jesse Keating 7a32965
 	if (is_vm_hugetlb_page(vma))
Jesse Keating 7a32965
 		hugetlb_change_protection(vma, start, end, vma->vm_page_prot);