kmemcheck.c 14 KB

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