kmemcheck.c 14 KB

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