mmu_audit.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * mmu_audit.c:
  3. *
  4. * Audit code for KVM MMU
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affilates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@qumranet.com>
  12. * Marcelo Tosatti <mtosatti@redhat.com>
  13. * Xiao Guangrong <xiaoguangrong@cn.fujitsu.com>
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2. See
  16. * the COPYING file in the top-level directory.
  17. *
  18. */
  19. static const char *audit_msg;
  20. typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level);
  21. static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  22. inspect_spte_fn fn, int level)
  23. {
  24. int i;
  25. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  26. u64 *ent = sp->spt;
  27. fn(vcpu, ent + i, level);
  28. if (is_shadow_present_pte(ent[i]) &&
  29. !is_last_spte(ent[i], level)) {
  30. struct kvm_mmu_page *child;
  31. child = page_header(ent[i] & PT64_BASE_ADDR_MASK);
  32. __mmu_spte_walk(vcpu, child, fn, level - 1);
  33. }
  34. }
  35. }
  36. static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
  37. {
  38. int i;
  39. struct kvm_mmu_page *sp;
  40. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  41. return;
  42. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  43. hpa_t root = vcpu->arch.mmu.root_hpa;
  44. sp = page_header(root);
  45. __mmu_spte_walk(vcpu, sp, fn, PT64_ROOT_LEVEL);
  46. return;
  47. }
  48. for (i = 0; i < 4; ++i) {
  49. hpa_t root = vcpu->arch.mmu.pae_root[i];
  50. if (root && VALID_PAGE(root)) {
  51. root &= PT64_BASE_ADDR_MASK;
  52. sp = page_header(root);
  53. __mmu_spte_walk(vcpu, sp, fn, 2);
  54. }
  55. }
  56. return;
  57. }
  58. typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp);
  59. static void walk_all_active_sps(struct kvm *kvm, sp_handler fn)
  60. {
  61. struct kvm_mmu_page *sp;
  62. list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link)
  63. fn(kvm, sp);
  64. }
  65. static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
  66. {
  67. struct kvm_mmu_page *sp;
  68. gfn_t gfn;
  69. pfn_t pfn;
  70. hpa_t hpa;
  71. sp = page_header(__pa(sptep));
  72. if (sp->unsync) {
  73. if (level != PT_PAGE_TABLE_LEVEL) {
  74. printk(KERN_ERR "audit: (%s) error: unsync sp: %p level = %d\n",
  75. audit_msg, sp, level);
  76. return;
  77. }
  78. if (*sptep == shadow_notrap_nonpresent_pte) {
  79. printk(KERN_ERR "audit: (%s) error: notrap spte in unsync sp: %p\n",
  80. audit_msg, sp);
  81. return;
  82. }
  83. }
  84. if (sp->role.direct && *sptep == shadow_notrap_nonpresent_pte) {
  85. printk(KERN_ERR "audit: (%s) error: notrap spte in direct sp: %p\n",
  86. audit_msg, sp);
  87. return;
  88. }
  89. if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level))
  90. return;
  91. gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
  92. pfn = gfn_to_pfn_atomic(vcpu->kvm, gfn);
  93. if (is_error_pfn(pfn)) {
  94. kvm_release_pfn_clean(pfn);
  95. return;
  96. }
  97. hpa = pfn << PAGE_SHIFT;
  98. if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
  99. printk(KERN_ERR "xx audit error: (%s) levels %d"
  100. "pfn %llx hpa %llx ent %llxn",
  101. audit_msg, vcpu->arch.mmu.root_level,
  102. pfn, hpa, *sptep);
  103. }
  104. static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
  105. {
  106. unsigned long *rmapp;
  107. struct kvm_mmu_page *rev_sp;
  108. gfn_t gfn;
  109. rev_sp = page_header(__pa(sptep));
  110. gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
  111. if (!gfn_to_memslot(kvm, gfn)) {
  112. if (!printk_ratelimit())
  113. return;
  114. printk(KERN_ERR "%s: no memslot for gfn %llx\n",
  115. audit_msg, gfn);
  116. printk(KERN_ERR "%s: index %ld of sp (gfn=%llx)\n",
  117. audit_msg, (long int)(sptep - rev_sp->spt),
  118. rev_sp->gfn);
  119. dump_stack();
  120. return;
  121. }
  122. rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
  123. if (!*rmapp) {
  124. if (!printk_ratelimit())
  125. return;
  126. printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
  127. audit_msg, *sptep);
  128. dump_stack();
  129. }
  130. }
  131. static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level)
  132. {
  133. if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level))
  134. inspect_spte_has_rmap(vcpu->kvm, sptep);
  135. }
  136. static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
  137. {
  138. int i;
  139. if (sp->role.level != PT_PAGE_TABLE_LEVEL)
  140. return;
  141. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  142. if (!is_rmap_spte(sp->spt[i]))
  143. continue;
  144. inspect_spte_has_rmap(kvm, sp->spt + i);
  145. }
  146. }
  147. void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
  148. {
  149. struct kvm_memory_slot *slot;
  150. unsigned long *rmapp;
  151. u64 *spte;
  152. if (sp->role.direct || sp->unsync || sp->role.invalid)
  153. return;
  154. slot = gfn_to_memslot(kvm, sp->gfn);
  155. rmapp = &slot->rmap[sp->gfn - slot->base_gfn];
  156. spte = rmap_next(kvm, rmapp, NULL);
  157. while (spte) {
  158. if (is_writable_pte(*spte))
  159. printk(KERN_ERR "%s: (%s) shadow page has "
  160. "writable mappings: gfn %llx role %x\n",
  161. __func__, audit_msg, sp->gfn,
  162. sp->role.word);
  163. spte = rmap_next(kvm, rmapp, spte);
  164. }
  165. }
  166. static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
  167. {
  168. check_mappings_rmap(kvm, sp);
  169. audit_write_protection(kvm, sp);
  170. }
  171. static void audit_all_active_sps(struct kvm *kvm)
  172. {
  173. walk_all_active_sps(kvm, audit_sp);
  174. }
  175. static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level)
  176. {
  177. audit_sptes_have_rmaps(vcpu, sptep, level);
  178. audit_mappings(vcpu, sptep, level);
  179. }
  180. static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
  181. {
  182. mmu_spte_walk(vcpu, audit_spte);
  183. }
  184. static void kvm_mmu_audit(void *ignore, struct kvm_vcpu *vcpu, int audit_point)
  185. {
  186. audit_msg = audit_point_name[audit_point];
  187. audit_all_active_sps(vcpu->kvm);
  188. audit_vcpu_spte(vcpu);
  189. }
  190. static bool mmu_audit;
  191. static void mmu_audit_enable(void)
  192. {
  193. int ret;
  194. if (mmu_audit)
  195. return;
  196. ret = register_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
  197. WARN_ON(ret);
  198. mmu_audit = true;
  199. }
  200. static void mmu_audit_disable(void)
  201. {
  202. if (!mmu_audit)
  203. return;
  204. unregister_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
  205. tracepoint_synchronize_unregister();
  206. mmu_audit = false;
  207. }
  208. static int mmu_audit_set(const char *val, const struct kernel_param *kp)
  209. {
  210. int ret;
  211. unsigned long enable;
  212. ret = strict_strtoul(val, 10, &enable);
  213. if (ret < 0)
  214. return -EINVAL;
  215. switch (enable) {
  216. case 0:
  217. mmu_audit_disable();
  218. break;
  219. case 1:
  220. mmu_audit_enable();
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. return 0;
  226. }
  227. static struct kernel_param_ops audit_param_ops = {
  228. .set = mmu_audit_set,
  229. .get = param_get_bool,
  230. };
  231. module_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644);