kmemcheck.c 14 KB

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