kmemcheck.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * kmemcheck - a heavyweight memory checker for the linux kernel
  3. * Copyright (C) 2007, 2008 Vegard Nossum <vegardno@ifi.uio.no>
  4. * (With a lot of help from Ingo Molnar and Pekka Enberg.)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2) as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kmemcheck.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/page-flags.h>
  18. #include <linux/percpu.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/kmemcheck.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/tlbflush.h>
  26. #include "error.h"
  27. #include "opcode.h"
  28. #include "pte.h"
  29. #include "selftest.h"
  30. #include "shadow.h"
  31. #ifdef CONFIG_KMEMCHECK_DISABLED_BY_DEFAULT
  32. # define KMEMCHECK_ENABLED 0
  33. #endif
  34. #ifdef CONFIG_KMEMCHECK_ENABLED_BY_DEFAULT
  35. # define KMEMCHECK_ENABLED 1
  36. #endif
  37. #ifdef CONFIG_KMEMCHECK_ONESHOT_BY_DEFAULT
  38. # define KMEMCHECK_ENABLED 2
  39. #endif
  40. int kmemcheck_enabled = KMEMCHECK_ENABLED;
  41. int __init kmemcheck_init(void)
  42. {
  43. #ifdef CONFIG_SMP
  44. /*
  45. * Limit SMP to use a single CPU. We rely on the fact that this code
  46. * runs before SMP is set up.
  47. */
  48. if (setup_max_cpus > 1) {
  49. printk(KERN_INFO
  50. "kmemcheck: Limiting number of CPUs to 1.\n");
  51. setup_max_cpus = 1;
  52. }
  53. #endif
  54. if (!kmemcheck_selftest()) {
  55. printk(KERN_INFO "kmemcheck: self-tests failed; disabling\n");
  56. kmemcheck_enabled = 0;
  57. return -EINVAL;
  58. }
  59. printk(KERN_INFO "kmemcheck: Initialized\n");
  60. return 0;
  61. }
  62. early_initcall(kmemcheck_init);
  63. /*
  64. * We need to parse the kmemcheck= option before any memory is allocated.
  65. */
  66. static int __init param_kmemcheck(char *str)
  67. {
  68. if (!str)
  69. return -EINVAL;
  70. sscanf(str, "%d", &kmemcheck_enabled);
  71. return 0;
  72. }
  73. early_param("kmemcheck", param_kmemcheck);
  74. int kmemcheck_show_addr(unsigned long address)
  75. {
  76. pte_t *pte;
  77. pte = kmemcheck_pte_lookup(address);
  78. if (!pte)
  79. return 0;
  80. set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
  81. __flush_tlb_one(address);
  82. return 1;
  83. }
  84. int kmemcheck_hide_addr(unsigned long address)
  85. {
  86. pte_t *pte;
  87. pte = kmemcheck_pte_lookup(address);
  88. if (!pte)
  89. return 0;
  90. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
  91. __flush_tlb_one(address);
  92. return 1;
  93. }
  94. struct kmemcheck_context {
  95. bool busy;
  96. int balance;
  97. /*
  98. * There can be at most two memory operands to an instruction, but
  99. * each address can cross a page boundary -- so we may need up to
  100. * four addresses that must be hidden/revealed for each fault.
  101. */
  102. unsigned long addr[4];
  103. unsigned long n_addrs;
  104. unsigned long flags;
  105. /* Data size of the instruction that caused a fault. */
  106. unsigned int size;
  107. };
  108. static DEFINE_PER_CPU(struct kmemcheck_context, kmemcheck_context);
  109. bool kmemcheck_active(struct pt_regs *regs)
  110. {
  111. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  112. return data->balance > 0;
  113. }
  114. /* Save an address that needs to be shown/hidden */
  115. static void kmemcheck_save_addr(unsigned long addr)
  116. {
  117. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  118. BUG_ON(data->n_addrs >= ARRAY_SIZE(data->addr));
  119. data->addr[data->n_addrs++] = addr;
  120. }
  121. static unsigned int kmemcheck_show_all(void)
  122. {
  123. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  124. unsigned int i;
  125. unsigned int n;
  126. n = 0;
  127. for (i = 0; i < data->n_addrs; ++i)
  128. n += kmemcheck_show_addr(data->addr[i]);
  129. return n;
  130. }
  131. static unsigned int kmemcheck_hide_all(void)
  132. {
  133. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  134. unsigned int i;
  135. unsigned int n;
  136. n = 0;
  137. for (i = 0; i < data->n_addrs; ++i)
  138. n += kmemcheck_hide_addr(data->addr[i]);
  139. return n;
  140. }
  141. /*
  142. * Called from the #PF handler.
  143. */
  144. void kmemcheck_show(struct pt_regs *regs)
  145. {
  146. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  147. BUG_ON(!irqs_disabled());
  148. if (unlikely(data->balance != 0)) {
  149. kmemcheck_show_all();
  150. kmemcheck_error_save_bug(regs);
  151. data->balance = 0;
  152. return;
  153. }
  154. /*
  155. * None of the addresses actually belonged to kmemcheck. Note that
  156. * this is not an error.
  157. */
  158. if (kmemcheck_show_all() == 0)
  159. return;
  160. ++data->balance;
  161. /*
  162. * The IF needs to be cleared as well, so that the faulting
  163. * instruction can run "uninterrupted". Otherwise, we might take
  164. * an interrupt and start executing that before we've had a chance
  165. * to hide the page again.
  166. *
  167. * NOTE: In the rare case of multiple faults, we must not override
  168. * the original flags:
  169. */
  170. if (!(regs->flags & X86_EFLAGS_TF))
  171. data->flags = regs->flags;
  172. regs->flags |= X86_EFLAGS_TF;
  173. regs->flags &= ~X86_EFLAGS_IF;
  174. }
  175. /*
  176. * Called from the #DB handler.
  177. */
  178. void kmemcheck_hide(struct pt_regs *regs)
  179. {
  180. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  181. int n;
  182. BUG_ON(!irqs_disabled());
  183. if (unlikely(data->balance != 1)) {
  184. kmemcheck_show_all();
  185. kmemcheck_error_save_bug(regs);
  186. data->n_addrs = 0;
  187. data->balance = 0;
  188. if (!(data->flags & X86_EFLAGS_TF))
  189. regs->flags &= ~X86_EFLAGS_TF;
  190. if (data->flags & X86_EFLAGS_IF)
  191. regs->flags |= X86_EFLAGS_IF;
  192. return;
  193. }
  194. if (kmemcheck_enabled)
  195. n = kmemcheck_hide_all();
  196. else
  197. n = kmemcheck_show_all();
  198. if (n == 0)
  199. return;
  200. --data->balance;
  201. data->n_addrs = 0;
  202. if (!(data->flags & X86_EFLAGS_TF))
  203. regs->flags &= ~X86_EFLAGS_TF;
  204. if (data->flags & X86_EFLAGS_IF)
  205. regs->flags |= X86_EFLAGS_IF;
  206. }
  207. void kmemcheck_show_pages(struct page *p, unsigned int n)
  208. {
  209. unsigned int i;
  210. for (i = 0; i < n; ++i) {
  211. unsigned long address;
  212. pte_t *pte;
  213. unsigned int level;
  214. address = (unsigned long) page_address(&p[i]);
  215. pte = lookup_address(address, &level);
  216. BUG_ON(!pte);
  217. BUG_ON(level != PG_LEVEL_4K);
  218. set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
  219. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_HIDDEN));
  220. __flush_tlb_one(address);
  221. }
  222. }
  223. bool kmemcheck_page_is_tracked(struct page *p)
  224. {
  225. /* This will also check the "hidden" flag of the PTE. */
  226. return kmemcheck_pte_lookup((unsigned long) page_address(p));
  227. }
  228. void kmemcheck_hide_pages(struct page *p, unsigned int n)
  229. {
  230. unsigned int i;
  231. for (i = 0; i < n; ++i) {
  232. unsigned long address;
  233. pte_t *pte;
  234. unsigned int level;
  235. address = (unsigned long) page_address(&p[i]);
  236. pte = lookup_address(address, &level);
  237. BUG_ON(!pte);
  238. BUG_ON(level != PG_LEVEL_4K);
  239. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
  240. set_pte(pte, __pte(pte_val(*pte) | _PAGE_HIDDEN));
  241. __flush_tlb_one(address);
  242. }
  243. }
  244. /* Access may NOT cross page boundary */
  245. static void kmemcheck_read_strict(struct pt_regs *regs,
  246. unsigned long addr, unsigned int size)
  247. {
  248. void *shadow;
  249. enum kmemcheck_shadow status;
  250. shadow = kmemcheck_shadow_lookup(addr);
  251. if (!shadow)
  252. return;
  253. kmemcheck_save_addr(addr);
  254. status = kmemcheck_shadow_test(shadow, size);
  255. if (status == KMEMCHECK_SHADOW_INITIALIZED)
  256. return;
  257. if (kmemcheck_enabled)
  258. kmemcheck_error_save(status, addr, size, regs);
  259. if (kmemcheck_enabled == 2)
  260. kmemcheck_enabled = 0;
  261. /* Don't warn about it again. */
  262. kmemcheck_shadow_set(shadow, size);
  263. }
  264. /* Access may cross page boundary */
  265. static void kmemcheck_read(struct pt_regs *regs,
  266. unsigned long addr, unsigned int size)
  267. {
  268. unsigned long page = addr & PAGE_MASK;
  269. unsigned long next_addr = addr + size - 1;
  270. unsigned long next_page = next_addr & PAGE_MASK;
  271. if (likely(page == next_page)) {
  272. kmemcheck_read_strict(regs, addr, size);
  273. return;
  274. }
  275. /*
  276. * What we do is basically to split the access across the
  277. * two pages and handle each part separately. Yes, this means
  278. * that we may now see reads that are 3 + 5 bytes, for
  279. * example (and if both are uninitialized, there will be two
  280. * reports), but it makes the code a lot simpler.
  281. */
  282. kmemcheck_read_strict(regs, addr, next_page - addr);
  283. kmemcheck_read_strict(regs, next_page, next_addr - next_page);
  284. }
  285. static void kmemcheck_write_strict(struct pt_regs *regs,
  286. unsigned long addr, unsigned int size)
  287. {
  288. void *shadow;
  289. shadow = kmemcheck_shadow_lookup(addr);
  290. if (!shadow)
  291. return;
  292. kmemcheck_save_addr(addr);
  293. kmemcheck_shadow_set(shadow, size);
  294. }
  295. static void kmemcheck_write(struct pt_regs *regs,
  296. unsigned long addr, unsigned int size)
  297. {
  298. unsigned long page = addr & PAGE_MASK;
  299. unsigned long next_addr = addr + size - 1;
  300. unsigned long next_page = next_addr & PAGE_MASK;
  301. if (likely(page == next_page)) {
  302. kmemcheck_write_strict(regs, addr, size);
  303. return;
  304. }
  305. /* See comment in kmemcheck_read(). */
  306. kmemcheck_write_strict(regs, addr, next_page - addr);
  307. kmemcheck_write_strict(regs, next_page, next_addr - next_page);
  308. }
  309. /*
  310. * Copying is hard. We have two addresses, each of which may be split across
  311. * a page (and each page will have different shadow addresses).
  312. */
  313. static void kmemcheck_copy(struct pt_regs *regs,
  314. unsigned long src_addr, unsigned long dst_addr, unsigned int size)
  315. {
  316. uint8_t shadow[8];
  317. enum kmemcheck_shadow status;
  318. unsigned long page;
  319. unsigned long next_addr;
  320. unsigned long next_page;
  321. uint8_t *x;
  322. unsigned int i;
  323. unsigned int n;
  324. BUG_ON(size > sizeof(shadow));
  325. page = src_addr & PAGE_MASK;
  326. next_addr = src_addr + size - 1;
  327. next_page = next_addr & PAGE_MASK;
  328. if (likely(page == next_page)) {
  329. /* Same page */
  330. x = kmemcheck_shadow_lookup(src_addr);
  331. if (x) {
  332. kmemcheck_save_addr(src_addr);
  333. for (i = 0; i < size; ++i)
  334. shadow[i] = x[i];
  335. } else {
  336. for (i = 0; i < size; ++i)
  337. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  338. }
  339. } else {
  340. n = next_page - src_addr;
  341. BUG_ON(n > sizeof(shadow));
  342. /* First page */
  343. x = kmemcheck_shadow_lookup(src_addr);
  344. if (x) {
  345. kmemcheck_save_addr(src_addr);
  346. for (i = 0; i < n; ++i)
  347. shadow[i] = x[i];
  348. } else {
  349. /* Not tracked */
  350. for (i = 0; i < n; ++i)
  351. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  352. }
  353. /* Second page */
  354. x = kmemcheck_shadow_lookup(next_page);
  355. if (x) {
  356. kmemcheck_save_addr(next_page);
  357. for (i = n; i < size; ++i)
  358. shadow[i] = x[i - n];
  359. } else {
  360. /* Not tracked */
  361. for (i = n; i < size; ++i)
  362. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  363. }
  364. }
  365. page = dst_addr & PAGE_MASK;
  366. next_addr = dst_addr + size - 1;
  367. next_page = next_addr & PAGE_MASK;
  368. if (likely(page == next_page)) {
  369. /* Same page */
  370. x = kmemcheck_shadow_lookup(dst_addr);
  371. if (x) {
  372. kmemcheck_save_addr(dst_addr);
  373. for (i = 0; i < size; ++i) {
  374. x[i] = shadow[i];
  375. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  376. }
  377. }
  378. } else {
  379. n = next_page - dst_addr;
  380. BUG_ON(n > sizeof(shadow));
  381. /* First page */
  382. x = kmemcheck_shadow_lookup(dst_addr);
  383. if (x) {
  384. kmemcheck_save_addr(dst_addr);
  385. for (i = 0; i < n; ++i) {
  386. x[i] = shadow[i];
  387. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  388. }
  389. }
  390. /* Second page */
  391. x = kmemcheck_shadow_lookup(next_page);
  392. if (x) {
  393. kmemcheck_save_addr(next_page);
  394. for (i = n; i < size; ++i) {
  395. x[i - n] = shadow[i];
  396. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  397. }
  398. }
  399. }
  400. status = kmemcheck_shadow_test(shadow, size);
  401. if (status == KMEMCHECK_SHADOW_INITIALIZED)
  402. return;
  403. if (kmemcheck_enabled)
  404. kmemcheck_error_save(status, src_addr, size, regs);
  405. if (kmemcheck_enabled == 2)
  406. kmemcheck_enabled = 0;
  407. }
  408. enum kmemcheck_method {
  409. KMEMCHECK_READ,
  410. KMEMCHECK_WRITE,
  411. };
  412. static void kmemcheck_access(struct pt_regs *regs,
  413. unsigned long fallback_address, enum kmemcheck_method fallback_method)
  414. {
  415. const uint8_t *insn;
  416. const uint8_t *insn_primary;
  417. unsigned int size;
  418. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  419. /* Recursive fault -- ouch. */
  420. if (data->busy) {
  421. kmemcheck_show_addr(fallback_address);
  422. kmemcheck_error_save_bug(regs);
  423. return;
  424. }
  425. data->busy = true;
  426. insn = (const uint8_t *) regs->ip;
  427. insn_primary = kmemcheck_opcode_get_primary(insn);
  428. kmemcheck_opcode_decode(insn, &size);
  429. switch (insn_primary[0]) {
  430. #ifdef CONFIG_KMEMCHECK_BITOPS_OK
  431. /* AND, OR, XOR */
  432. /*
  433. * Unfortunately, these instructions have to be excluded from
  434. * our regular checking since they access only some (and not
  435. * all) bits. This clears out "bogus" bitfield-access warnings.
  436. */
  437. case 0x80:
  438. case 0x81:
  439. case 0x82:
  440. case 0x83:
  441. switch ((insn_primary[1] >> 3) & 7) {
  442. /* OR */
  443. case 1:
  444. /* AND */
  445. case 4:
  446. /* XOR */
  447. case 6:
  448. kmemcheck_write(regs, fallback_address, size);
  449. goto out;
  450. /* ADD */
  451. case 0:
  452. /* ADC */
  453. case 2:
  454. /* SBB */
  455. case 3:
  456. /* SUB */
  457. case 5:
  458. /* CMP */
  459. case 7:
  460. break;
  461. }
  462. break;
  463. #endif
  464. /* MOVS, MOVSB, MOVSW, MOVSD */
  465. case 0xa4:
  466. case 0xa5:
  467. /*
  468. * These instructions are special because they take two
  469. * addresses, but we only get one page fault.
  470. */
  471. kmemcheck_copy(regs, regs->si, regs->di, size);
  472. goto out;
  473. /* CMPS, CMPSB, CMPSW, CMPSD */
  474. case 0xa6:
  475. case 0xa7:
  476. kmemcheck_read(regs, regs->si, size);
  477. kmemcheck_read(regs, regs->di, size);
  478. goto out;
  479. }
  480. /*
  481. * If the opcode isn't special in any way, we use the data from the
  482. * page fault handler to determine the address and type of memory
  483. * access.
  484. */
  485. switch (fallback_method) {
  486. case KMEMCHECK_READ:
  487. kmemcheck_read(regs, fallback_address, size);
  488. goto out;
  489. case KMEMCHECK_WRITE:
  490. kmemcheck_write(regs, fallback_address, size);
  491. goto out;
  492. }
  493. out:
  494. data->busy = false;
  495. }
  496. bool kmemcheck_fault(struct pt_regs *regs, unsigned long address,
  497. unsigned long error_code)
  498. {
  499. pte_t *pte;
  500. /*
  501. * XXX: Is it safe to assume that memory accesses from virtual 86
  502. * mode or non-kernel code segments will _never_ access kernel
  503. * memory (e.g. tracked pages)? For now, we need this to avoid
  504. * invoking kmemcheck for PnP BIOS calls.
  505. */
  506. if (regs->flags & X86_VM_MASK)
  507. return false;
  508. if (regs->cs != __KERNEL_CS)
  509. return false;
  510. pte = kmemcheck_pte_lookup(address);
  511. if (!pte)
  512. return false;
  513. if (error_code & 2)
  514. kmemcheck_access(regs, address, KMEMCHECK_WRITE);
  515. else
  516. kmemcheck_access(regs, address, KMEMCHECK_READ);
  517. kmemcheck_show(regs);
  518. return true;
  519. }
  520. bool kmemcheck_trap(struct pt_regs *regs)
  521. {
  522. if (!kmemcheck_active(regs))
  523. return false;
  524. /* We're done. */
  525. kmemcheck_hide(regs);
  526. return true;
  527. }