Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7-0]: src/sys/arch Pull up following revision(s) (requested by ma...



details:   https://anonhg.NetBSD.org/src/rev/c865e43e85e7
branches:  netbsd-7-0
changeset: 801420:c865e43e85e7
user:      snj <snj%NetBSD.org@localhost>
date:      Mon Jan 22 19:40:25 2018 +0000

description:
Pull up following revision(s) (requested by maxv in ticket #1550):
        sys/arch/amd64/amd64/machdep.c: revision 1.280 via patch
        sys/arch/amd64/include/segments.h: revision 1.34 via patch
        sys/arch/i386/i386/machdep.c: revision 1.800 via patch
        sys/arch/i386/include/segments.h: revision 1.64 via patch
        sys/arch/x86/x86/vm_machdep.c: revision 1.30 via patch
Fix a huge privilege separation vulnerability in Xen-amd64.
On amd64 the kernel runs in ring3, like userland, and therefore SEL_KPL
equals SEL_UPL. While Xen can make a distinction between usermode and
kernelmode in %cs, it can't when it comes to iopl. Since we set SEL_KPL
in iopl, Xen sees SEL_UPL, and allows (unprivileged) userland processes
to read and write to the CPU ports.
It is easy, then, to completely escalate privileges; by reprogramming the
PIC, by reading the ATA disks, by intercepting the keyboard interrupts
(keylogger), etc.
Declare IOPL_KPL, set to 1 on Xen-amd64, which allows the kernel to use
the ports but not userland. I didn't test this change on i386, but it
seems fine enough.

diffstat:

 sys/arch/amd64/amd64/machdep.c    |  6 +++---
 sys/arch/amd64/include/segments.h |  8 +++++++-
 sys/arch/i386/i386/machdep.c      |  6 +++---
 sys/arch/i386/include/segments.h  |  5 ++++-
 sys/arch/x86/x86/vm_machdep.c     |  6 +++---
 5 files changed, 20 insertions(+), 11 deletions(-)

diffs (122 lines):

diff -r 8a0c8222d04b -r c865e43e85e7 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Wed Jan 03 21:53:50 2018 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Mon Jan 22 19:40:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.211.6.1 2017/04/26 14:50:51 martin Exp $ */
+/*     $NetBSD: machdep.c,v 1.211.6.2 2018/01/22 19:40:25 snj Exp $    */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.211.6.1 2017/04/26 14:50:51 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.211.6.2 2018/01/22 19:40:25 snj Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -468,7 +468,7 @@
        pcb->pcb_fs = 0;
        pcb->pcb_gs = 0;
        pcb->pcb_rsp0 = (uvm_lwp_getuarea(l) + USPACE - 16) & ~0xf;
-       pcb->pcb_iopl = SEL_KPL;
+       pcb->pcb_iopl = IOPL_KPL;
 
        pmap_kernel()->pm_ldt_sel = GSYSSEL(GLDT_SEL, SEL_KPL);
        pcb->pcb_cr0 = rcr0() & ~CR0_TS;
diff -r 8a0c8222d04b -r c865e43e85e7 sys/arch/amd64/include/segments.h
--- a/sys/arch/amd64/include/segments.h Wed Jan 03 21:53:50 2018 +0000
+++ b/sys/arch/amd64/include/segments.h Mon Jan 22 19:40:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: segments.h,v 1.24 2013/01/07 17:03:06 chs Exp $        */
+/*     $NetBSD: segments.h,v 1.24.16.1 2018/01/22 19:40:25 snj Exp $   */
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -107,6 +107,12 @@
 #define        ISLDT(s)        ((s) & SEL_LDT) /* is it local or global */
 #define        SEL_LDT         4               /* local descriptor table */    
 
+#ifdef XEN
+#define IOPL_KPL       1
+#else
+#define IOPL_KPL       SEL_KPL
+#endif
+
 /* Dynamically allocated TSSs and LDTs start (byte offset) */
 #define SYSSEL_START   (NGDT_MEM << 3)
 #define DYNSEL_START   (SYSSEL_START + (NGDT_SYS << 4))
diff -r 8a0c8222d04b -r c865e43e85e7 sys/arch/i386/i386/machdep.c
--- a/sys/arch/i386/i386/machdep.c      Wed Jan 03 21:53:50 2018 +0000
+++ b/sys/arch/i386/i386/machdep.c      Mon Jan 22 19:40:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.752.8.1 2017/07/20 01:42:39 snj Exp $    */
+/*     $NetBSD: machdep.c,v 1.752.8.2 2018/01/22 19:40:25 snj Exp $    */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.752.8.1 2017/07/20 01:42:39 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.752.8.2 2018/01/22 19:40:25 snj Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -502,7 +502,7 @@
        pmap_kernel()->pm_ldt_sel = GSEL(GLDT_SEL, SEL_KPL);
        pcb->pcb_cr0 = rcr0() & ~CR0_TS;
        pcb->pcb_esp0 = uvm_lwp_getuarea(l) + USPACE - 16;
-       pcb->pcb_iopl = SEL_KPL;
+       pcb->pcb_iopl = IOPL_KPL;
        l->l_md.md_regs = (struct trapframe *)pcb->pcb_esp0 - 1;
        memcpy(&pcb->pcb_fsd, &gdt[GUDATA_SEL], sizeof(pcb->pcb_fsd));
        memcpy(&pcb->pcb_gsd, &gdt[GUDATA_SEL], sizeof(pcb->pcb_gsd));
diff -r 8a0c8222d04b -r c865e43e85e7 sys/arch/i386/include/segments.h
--- a/sys/arch/i386/include/segments.h  Wed Jan 03 21:53:50 2018 +0000
+++ b/sys/arch/i386/include/segments.h  Mon Jan 22 19:40:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: segments.h,v 1.54 2011/04/26 15:51:23 joerg Exp $      */
+/*     $NetBSD: segments.h,v 1.54.34.1 2018/01/22 19:40:25 snj Exp $   */
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -104,6 +104,9 @@
 #endif /* XEN */
 #define        ISLDT(s)        ((s) & SEL_LDT) /* is it local or global */
 #define        SEL_LDT         4               /* local descriptor table */
+
+#define IOPL_KPL       SEL_KPL
+
 #define        IDXSEL(s)       (((s) >> 3) & 0x1fff)           /* index of selector */
 #define        IDXSELN(s)      (((s) >> 3))                    /* index of selector */
 #define        GSEL(s,r)       (((s) << 3) | r)                /* a global selector */
diff -r 8a0c8222d04b -r c865e43e85e7 sys/arch/x86/x86/vm_machdep.c
--- a/sys/arch/x86/x86/vm_machdep.c     Wed Jan 03 21:53:50 2018 +0000
+++ b/sys/arch/x86/x86/vm_machdep.c     Mon Jan 22 19:40:25 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vm_machdep.c,v 1.25.8.1 2016/12/18 07:02:59 snj Exp $  */
+/*     $NetBSD: vm_machdep.c,v 1.25.8.2 2018/01/22 19:40:25 snj Exp $  */
 
 /*-
  * Copyright (c) 1982, 1986 The Regents of the University of California.
@@ -80,7 +80,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.25.8.1 2016/12/18 07:02:59 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.25.8.2 2018/01/22 19:40:25 snj Exp $");
 
 #include "opt_mtrr.h"
 
@@ -158,7 +158,7 @@
        fpu_save_area_fork(pcb2, pcb1);
 
 #if defined(XEN)
-       pcb2->pcb_iopl = SEL_KPL;
+       pcb2->pcb_iopl = IOPL_KPL;
 #endif
 
        /*



Home | Main Index | Thread Index | Old Index