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