1 #include "libcflat.h" 2 #include "x86/desc.h" 3 #include "x86/processor.h" 4 #include "x86/vm.h" 5 6 volatile int pf_count = 0; 7 volatile int save; 8 volatile unsigned test; 9 10 void do_pf_tss(unsigned long error_code); 11 12 // When doing ring 3 tests, page fault handlers will always run on a 13 // separate stack (the ring 0 stack). Seems easier to use the alt_stack 14 // mechanism for both ring 0 and ring 3. 15 16 void do_pf_tss(unsigned long error_code) 17 { 18 pf_count++; 19 save = test; 20 21 #ifndef __x86_64__ 22 tss.eflags |= X86_EFLAGS_AC; 23 #endif 24 } 25 26 extern void pf_tss(void); 27 asm ("pf_tss:\n" 28 #ifdef __x86_64__ 29 // no task on x86_64, save/restore caller-save regs 30 "push %rax; push %rcx; push %rdx; push %rsi; push %rdi\n" 31 "push %r8; push %r9; push %r10; push %r11\n" 32 "mov 9*8(%rsp),%rdi\n" 33 #endif 34 "call do_pf_tss\n" 35 #ifdef __x86_64__ 36 "pop %r11; pop %r10; pop %r9; pop %r8\n" 37 "pop %rdi; pop %rsi; pop %rdx; pop %rcx; pop %rax\n" 38 #endif 39 "add $"S", %"R "sp\n" 40 #ifdef __x86_64__ 41 "orl $" xstr(X86_EFLAGS_AC) ", 2*"S"(%"R "sp)\n" // set EFLAGS.AC and retry 42 #endif 43 "iret"W" \n\t" 44 "jmp pf_tss\n\t"); 45 46 47 #define USER_BASE (1 << 24) 48 #define USER_VAR(v) (*((__typeof__(&(v))) (((unsigned long)&v) + USER_BASE))) 49 #define USER_ADDR(v) ((void *)((unsigned long)(&v) + USER_BASE)) 50 51 static void init_test(int i) 52 { 53 pf_count = 0; 54 if (i) { 55 invlpg(&test); 56 invlpg(&USER_VAR(test)); 57 } 58 } 59 60 static void check_smap_nowp(void) 61 { 62 test = 0x99; 63 64 *get_pte(phys_to_virt(read_cr3()), USER_ADDR(test)) &= ~PT_WRITABLE_MASK; 65 66 write_cr4(read_cr4() & ~X86_CR4_SMAP); 67 write_cr0(read_cr0() & ~X86_CR0_WP); 68 clac(); 69 write_cr3(read_cr3()); 70 71 init_test(0); 72 USER_VAR(test) = 0x99; 73 report("write from user page with SMAP=0, AC=0, WP=0, PTE.U=1 && PTE.W=0", pf_count == 0); 74 75 write_cr4(read_cr4() | X86_CR4_SMAP); 76 write_cr3(read_cr3()); 77 78 init_test(0); 79 (void)USER_VAR(test); 80 report("read from user page with SMAP=1, AC=0, WP=0, PTE.U=1 && PTE.W=0", pf_count == 1 && save == 0x99); 81 82 /* Undo changes */ 83 *get_pte(phys_to_virt(read_cr3()), USER_ADDR(test)) |= PT_WRITABLE_MASK; 84 85 write_cr0(read_cr0() | X86_CR0_WP); 86 write_cr3(read_cr3()); 87 } 88 89 int main(int ac, char **av) 90 { 91 unsigned long i; 92 93 if (!this_cpu_has(X86_FEATURE_SMAP)) { 94 printf("SMAP not enabled\n"); 95 return report_summary(); 96 } 97 98 setup_vm(); 99 setup_alt_stack(); 100 set_intr_alt_stack(14, pf_tss); 101 102 // Map first 16MB as supervisor pages 103 for (i = 0; i < USER_BASE; i += PAGE_SIZE) { 104 *get_pte(phys_to_virt(read_cr3()), phys_to_virt(i)) &= ~PT_USER_MASK; 105 invlpg((void *)i); 106 } 107 108 // Present the same 16MB as user pages in the 16MB-32MB range 109 for (i = USER_BASE; i < 2 * USER_BASE; i += PAGE_SIZE) { 110 *get_pte(phys_to_virt(read_cr3()), phys_to_virt(i)) &= ~USER_BASE; 111 invlpg((void *)i); 112 } 113 114 clac(); 115 write_cr4(read_cr4() | X86_CR4_SMAP); 116 write_cr3(read_cr3()); 117 118 for (i = 0; i < 2; i++) { 119 if (i) 120 printf("testing with INVLPG\n"); 121 else 122 printf("testing without INVLPG\n"); 123 124 init_test(i); 125 clac(); 126 test = 42; 127 report("write to supervisor page", pf_count == 0 && test == 42); 128 129 init_test(i); 130 stac(); 131 (void)USER_VAR(test); 132 report("read from user page with AC=1", pf_count == 0); 133 134 init_test(i); 135 clac(); 136 (void)USER_VAR(test); 137 report("read from user page with AC=0", pf_count == 1 && save == 42); 138 139 init_test(i); 140 stac(); 141 save = 0; 142 USER_VAR(test) = 43; 143 report("write to user page with AC=1", pf_count == 0 && test == 43); 144 145 init_test(i); 146 clac(); 147 USER_VAR(test) = 44; 148 report("read from user page with AC=0", pf_count == 1 && test == 44 && save == 43); 149 150 init_test(i); 151 stac(); 152 test = -1; 153 asm("or $(" xstr(USER_BASE) "), %"R "sp \n" 154 "push $44 \n " 155 "decl test\n" 156 "and $~(" xstr(USER_BASE) "), %"R "sp \n" 157 "pop %"R "ax\n" 158 "movl %eax, test"); 159 report("write to user stack with AC=1", pf_count == 0 && test == 44); 160 161 init_test(i); 162 clac(); 163 test = -1; 164 asm("or $(" xstr(USER_BASE) "), %"R "sp \n" 165 "push $45 \n " 166 "decl test\n" 167 "and $~(" xstr(USER_BASE) "), %"R "sp \n" 168 "pop %"R "ax\n" 169 "movl %eax, test"); 170 report("write to user stack with AC=0", pf_count == 1 && test == 45 && save == -1); 171 172 /* This would be trapped by SMEP */ 173 init_test(i); 174 clac(); 175 asm("jmp 1f + "xstr(USER_BASE)" \n" 176 "1: jmp 2f - "xstr(USER_BASE)" \n" 177 "2:"); 178 report("executing on user page with AC=0", pf_count == 0); 179 } 180 181 check_smap_nowp(); 182 183 // TODO: implicit kernel access from ring 3 (e.g. int) 184 185 return report_summary(); 186 } 187