kmemcheck.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 (data->balance == 0)
  184. return;
  185. if (unlikely(data->balance != 1)) {
  186. kmemcheck_show_all();
  187. kmemcheck_error_save_bug(regs);
  188. data->n_addrs = 0;
  189. data->balance = 0;
  190. if (!(data->flags & X86_EFLAGS_TF))
  191. regs->flags &= ~X86_EFLAGS_TF;
  192. if (data->flags & X86_EFLAGS_IF)
  193. regs->flags |= X86_EFLAGS_IF;
  194. return;
  195. }
  196. if (kmemcheck_enabled)
  197. n = kmemcheck_hide_all();
  198. else
  199. n = kmemcheck_show_all();
  200. if (n == 0)
  201. return;
  202. --data->balance;
  203. data->n_addrs = 0;
  204. if (!(data->flags & X86_EFLAGS_TF))
  205. regs->flags &= ~X86_EFLAGS_TF;
  206. if (data->flags & X86_EFLAGS_IF)
  207. regs->flags |= X86_EFLAGS_IF;
  208. }
  209. void kmemcheck_show_pages(struct page *p, unsigned int n)
  210. {
  211. unsigned int i;
  212. for (i = 0; i < n; ++i) {
  213. unsigned long address;
  214. pte_t *pte;
  215. unsigned int level;
  216. address = (unsigned long) page_address(&p[i]);
  217. pte = lookup_address(address, &level);
  218. BUG_ON(!pte);
  219. BUG_ON(level != PG_LEVEL_4K);
  220. set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
  221. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_HIDDEN));
  222. __flush_tlb_one(address);
  223. }
  224. }
  225. bool kmemcheck_page_is_tracked(struct page *p)
  226. {
  227. /* This will also check the "hidden" flag of the PTE. */
  228. return kmemcheck_pte_lookup((unsigned long) page_address(p));
  229. }
  230. void kmemcheck_hide_pages(struct page *p, unsigned int n)
  231. {
  232. unsigned int i;
  233. for (i = 0; i < n; ++i) {
  234. unsigned long address;
  235. pte_t *pte;
  236. unsigned int level;
  237. address = (unsigned long) page_address(&p[i]);
  238. pte = lookup_address(address, &level);
  239. BUG_ON(!pte);
  240. BUG_ON(level != PG_LEVEL_4K);
  241. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
  242. set_pte(pte, __pte(pte_val(*pte) | _PAGE_HIDDEN));
  243. __flush_tlb_one(address);
  244. }
  245. }
  246. /* Access may NOT cross page boundary */
  247. static void kmemcheck_read_strict(struct pt_regs *regs,
  248. unsigned long addr, unsigned int size)
  249. {
  250. void *shadow;
  251. enum kmemcheck_shadow status;
  252. shadow = kmemcheck_shadow_lookup(addr);
  253. if (!shadow)
  254. return;
  255. kmemcheck_save_addr(addr);
  256. status = kmemcheck_shadow_test(shadow, size);
  257. if (status == KMEMCHECK_SHADOW_INITIALIZED)
  258. return;
  259. if (kmemcheck_enabled)
  260. kmemcheck_error_save(status, addr, size, regs);
  261. if (kmemcheck_enabled == 2)
  262. kmemcheck_enabled = 0;
  263. /* Don't warn about it again. */
  264. kmemcheck_shadow_set(shadow, size);
  265. }
  266. /* Access may cross page boundary */
  267. static void kmemcheck_read(struct pt_regs *regs,
  268. unsigned long addr, unsigned int size)
  269. {
  270. unsigned long page = addr & PAGE_MASK;
  271. unsigned long next_addr = addr + size - 1;
  272. unsigned long next_page = next_addr & PAGE_MASK;
  273. if (likely(page == next_page)) {
  274. kmemcheck_read_strict(regs, addr, size);
  275. return;
  276. }
  277. /*
  278. * What we do is basically to split the access across the
  279. * two pages and handle each part separately. Yes, this means
  280. * that we may now see reads that are 3 + 5 bytes, for
  281. * example (and if both are uninitialized, there will be two
  282. * reports), but it makes the code a lot simpler.
  283. */
  284. kmemcheck_read_strict(regs, addr, next_page - addr);
  285. kmemcheck_read_strict(regs, next_page, next_addr - next_page);
  286. }
  287. static void kmemcheck_write_strict(struct pt_regs *regs,
  288. unsigned long addr, unsigned int size)
  289. {
  290. void *shadow;
  291. shadow = kmemcheck_shadow_lookup(addr);
  292. if (!shadow)
  293. return;
  294. kmemcheck_save_addr(addr);
  295. kmemcheck_shadow_set(shadow, size);
  296. }
  297. static void kmemcheck_write(struct pt_regs *regs,
  298. unsigned long addr, unsigned int size)
  299. {
  300. unsigned long page = addr & PAGE_MASK;
  301. unsigned long next_addr = addr + size - 1;
  302. unsigned long next_page = next_addr & PAGE_MASK;
  303. if (likely(page == next_page)) {
  304. kmemcheck_write_strict(regs, addr, size);
  305. return;
  306. }
  307. /* See comment in kmemcheck_read(). */
  308. kmemcheck_write_strict(regs, addr, next_page - addr);
  309. kmemcheck_write_strict(regs, next_page, next_addr - next_page);
  310. }
  311. /*
  312. * Copying is hard. We have two addresses, each of which may be split across
  313. * a page (and each page will have different shadow addresses).
  314. */
  315. static void kmemcheck_copy(struct pt_regs *regs,
  316. unsigned long src_addr, unsigned long dst_addr, unsigned int size)
  317. {
  318. uint8_t shadow[8];
  319. enum kmemcheck_shadow status;
  320. unsigned long page;
  321. unsigned long next_addr;
  322. unsigned long next_page;
  323. uint8_t *x;
  324. unsigned int i;
  325. unsigned int n;
  326. BUG_ON(size > sizeof(shadow));
  327. page = src_addr & PAGE_MASK;
  328. next_addr = src_addr + size - 1;
  329. next_page = next_addr & PAGE_MASK;
  330. if (likely(page == next_page)) {
  331. /* Same page */
  332. x = kmemcheck_shadow_lookup(src_addr);
  333. if (x) {
  334. kmemcheck_save_addr(src_addr);
  335. for (i = 0; i < size; ++i)
  336. shadow[i] = x[i];
  337. } else {
  338. for (i = 0; i < size; ++i)
  339. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  340. }
  341. } else {
  342. n = next_page - src_addr;
  343. BUG_ON(n > sizeof(shadow));
  344. /* First page */
  345. x = kmemcheck_shadow_lookup(src_addr);
  346. if (x) {
  347. kmemcheck_save_addr(src_addr);
  348. for (i = 0; i < n; ++i)
  349. shadow[i] = x[i];
  350. } else {
  351. /* Not tracked */
  352. for (i = 0; i < n; ++i)
  353. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  354. }
  355. /* Second page */
  356. x = kmemcheck_shadow_lookup(next_page);
  357. if (x) {
  358. kmemcheck_save_addr(next_page);
  359. for (i = n; i < size; ++i)
  360. shadow[i] = x[i - n];
  361. } else {
  362. /* Not tracked */
  363. for (i = n; i < size; ++i)
  364. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  365. }
  366. }
  367. page = dst_addr & PAGE_MASK;
  368. next_addr = dst_addr + size - 1;
  369. next_page = next_addr & PAGE_MASK;
  370. if (likely(page == next_page)) {
  371. /* Same page */
  372. x = kmemcheck_shadow_lookup(dst_addr);
  373. if (x) {
  374. kmemcheck_save_addr(dst_addr);
  375. for (i = 0; i < size; ++i) {
  376. x[i] = shadow[i];
  377. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  378. }
  379. }
  380. } else {
  381. n = next_page - dst_addr;
  382. BUG_ON(n > sizeof(shadow));
  383. /* First page */
  384. x = kmemcheck_shadow_lookup(dst_addr);
  385. if (x) {
  386. kmemcheck_save_addr(dst_addr);
  387. for (i = 0; i < n; ++i) {
  388. x[i] = shadow[i];
  389. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  390. }
  391. }
  392. /* Second page */
  393. x = kmemcheck_shadow_lookup(next_page);
  394. if (x) {
  395. kmemcheck_save_addr(next_page);
  396. for (i = n; i < size; ++i) {
  397. x[i - n] = shadow[i];
  398. shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
  399. }
  400. }
  401. }
  402. status = kmemcheck_shadow_test(shadow, size);
  403. if (status == KMEMCHECK_SHADOW_INITIALIZED)
  404. return;
  405. if (kmemcheck_enabled)
  406. kmemcheck_error_save(status, src_addr, size, regs);
  407. if (kmemcheck_enabled == 2)
  408. kmemcheck_enabled = 0;
  409. }
  410. enum kmemcheck_method {
  411. KMEMCHECK_READ,
  412. KMEMCHECK_WRITE,
  413. };
  414. static void kmemcheck_access(struct pt_regs *regs,
  415. unsigned long fallback_address, enum kmemcheck_method fallback_method)
  416. {
  417. const uint8_t *insn;
  418. const uint8_t *insn_primary;
  419. unsigned int size;
  420. struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
  421. /* Recursive fault -- ouch. */
  422. if (data->busy) {
  423. kmemcheck_show_addr(fallback_address);
  424. kmemcheck_error_save_bug(regs);
  425. return;
  426. }
  427. data->busy = true;
  428. insn = (const uint8_t *) regs->ip;
  429. insn_primary = kmemcheck_opcode_get_primary(insn);
  430. kmemcheck_opcode_decode(insn, &size);
  431. switch (insn_primary[0]) {
  432. #ifdef CONFIG_KMEMCHECK_BITOPS_OK
  433. /* AND, OR, XOR */
  434. /*
  435. * Unfortunately, these instructions have to be excluded from
  436. * our regular checking since they access only some (and not
  437. * all) bits. This clears out "bogus" bitfield-access warnings.
  438. */
  439. case 0x80:
  440. case 0x81:
  441. case 0x82:
  442. case 0x83:
  443. switch ((insn_primary[1] >> 3) & 7) {
  444. /* OR */
  445. case 1:
  446. /* AND */
  447. case 4:
  448. /* XOR */
  449. case 6:
  450. kmemcheck_write(regs, fallback_address, size);
  451. goto out;
  452. /* ADD */
  453. case 0:
  454. /* ADC */
  455. case 2:
  456. /* SBB */
  457. case 3:
  458. /* SUB */
  459. case 5:
  460. /* CMP */
  461. case 7:
  462. break;
  463. }
  464. break;
  465. #endif
  466. /* MOVS, MOVSB, MOVSW, MOVSD */
  467. case 0xa4:
  468. case 0xa5:
  469. /*
  470. * These instructions are special because they take two
  471. * addresses, but we only get one page fault.
  472. */
  473. kmemcheck_copy(regs, regs->si, regs->di, size);
  474. goto out;
  475. /* CMPS, CMPSB, CMPSW, CMPSD */
  476. case 0xa6:
  477. case 0xa7:
  478. kmemcheck_read(regs, regs->si, size);
  479. kmemcheck_read(regs, regs->di, size);
  480. goto out;
  481. }
  482. /*
  483. * If the opcode isn't special in any way, we use the data from the
  484. * page fault handler to determine the address and type of memory
  485. * access.
  486. */
  487. switch (fallback_method) {
  488. case KMEMCHECK_READ:
  489. kmemcheck_read(regs, fallback_address, size);
  490. goto out;
  491. case KMEMCHECK_WRITE:
  492. kmemcheck_write(regs, fallback_address, size);
  493. goto out;
  494. }
  495. out:
  496. data->busy = false;
  497. }
  498. bool kmemcheck_fault(struct pt_regs *regs, unsigned long address,
  499. unsigned long error_code)
  500. {
  501. pte_t *pte;
  502. /*
  503. * XXX: Is it safe to assume that memory accesses from virtual 86
  504. * mode or non-kernel code segments will _never_ access kernel
  505. * memory (e.g. tracked pages)? For now, we need this to avoid
  506. * invoking kmemcheck for PnP BIOS calls.
  507. */
  508. if (regs->flags & X86_VM_MASK)
  509. return false;
  510. if (regs->cs != __KERNEL_CS)
  511. return false;
  512. pte = kmemcheck_pte_lookup(address);
  513. if (!pte)
  514. return false;
  515. if (error_code & 2)
  516. kmemcheck_access(regs, address, KMEMCHECK_WRITE);
  517. else
  518. kmemcheck_access(regs, address, KMEMCHECK_READ);
  519. kmemcheck_show(regs);
  520. return true;
  521. }
  522. bool kmemcheck_trap(struct pt_regs *regs)
  523. {
  524. if (!kmemcheck_active(regs))
  525. return false;
  526. /* We're done. */
  527. kmemcheck_hide(regs);
  528. return true;
  529. }