kprobes.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * Kernel Probes (KProbes)
  3. * kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation (includes suggestions from
  23. * Rusty Russell).
  24. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  25. * hlists and exceptions notifier as suggested by Andi Kleen.
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  29. * exceptions notifier to be first on the priority list.
  30. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  31. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  32. * <prasanna@in.ibm.com> added function-return probes.
  33. */
  34. #include <linux/kprobes.h>
  35. #include <linux/hash.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleloader.h>
  41. #include <linux/kallsyms.h>
  42. #include <linux/freezer.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/kdebug.h>
  46. #include <asm-generic/sections.h>
  47. #include <asm/cacheflush.h>
  48. #include <asm/errno.h>
  49. #include <asm/uaccess.h>
  50. #define KPROBE_HASH_BITS 6
  51. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  52. /*
  53. * Some oddball architectures like 64bit powerpc have function descriptors
  54. * so this must be overridable.
  55. */
  56. #ifndef kprobe_lookup_name
  57. #define kprobe_lookup_name(name, addr) \
  58. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  59. #endif
  60. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  61. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  62. /* NOTE: change this value only with kprobe_mutex held */
  63. static bool kprobe_enabled;
  64. DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  65. DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
  66. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  67. /*
  68. * Normally, functions that we'd want to prohibit kprobes in, are marked
  69. * __kprobes. But, there are cases where such functions already belong to
  70. * a different section (__sched for preempt_schedule)
  71. *
  72. * For such cases, we now have a blacklist
  73. */
  74. struct kprobe_blackpoint kprobe_blacklist[] = {
  75. {"preempt_schedule",},
  76. {NULL} /* Terminator */
  77. };
  78. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  79. /*
  80. * kprobe->ainsn.insn points to the copy of the instruction to be
  81. * single-stepped. x86_64, POWER4 and above have no-exec support and
  82. * stepping on the instruction on a vmalloced/kmalloced/data page
  83. * is a recipe for disaster
  84. */
  85. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  86. struct kprobe_insn_page {
  87. struct hlist_node hlist;
  88. kprobe_opcode_t *insns; /* Page of instruction slots */
  89. char slot_used[INSNS_PER_PAGE];
  90. int nused;
  91. int ngarbage;
  92. };
  93. enum kprobe_slot_state {
  94. SLOT_CLEAN = 0,
  95. SLOT_DIRTY = 1,
  96. SLOT_USED = 2,
  97. };
  98. static struct hlist_head kprobe_insn_pages;
  99. static int kprobe_garbage_slots;
  100. static int collect_garbage_slots(void);
  101. static int __kprobes check_safety(void)
  102. {
  103. int ret = 0;
  104. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  105. ret = freeze_processes();
  106. if (ret == 0) {
  107. struct task_struct *p, *q;
  108. do_each_thread(p, q) {
  109. if (p != current && p->state == TASK_RUNNING &&
  110. p->pid != 0) {
  111. printk("Check failed: %s is running\n",p->comm);
  112. ret = -1;
  113. goto loop_end;
  114. }
  115. } while_each_thread(p, q);
  116. }
  117. loop_end:
  118. thaw_processes();
  119. #else
  120. synchronize_sched();
  121. #endif
  122. return ret;
  123. }
  124. /**
  125. * get_insn_slot() - Find a slot on an executable page for an instruction.
  126. * We allocate an executable page if there's no room on existing ones.
  127. */
  128. kprobe_opcode_t __kprobes *get_insn_slot(void)
  129. {
  130. struct kprobe_insn_page *kip;
  131. struct hlist_node *pos;
  132. retry:
  133. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  134. if (kip->nused < INSNS_PER_PAGE) {
  135. int i;
  136. for (i = 0; i < INSNS_PER_PAGE; i++) {
  137. if (kip->slot_used[i] == SLOT_CLEAN) {
  138. kip->slot_used[i] = SLOT_USED;
  139. kip->nused++;
  140. return kip->insns + (i * MAX_INSN_SIZE);
  141. }
  142. }
  143. /* Surprise! No unused slots. Fix kip->nused. */
  144. kip->nused = INSNS_PER_PAGE;
  145. }
  146. }
  147. /* If there are any garbage slots, collect it and try again. */
  148. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  149. goto retry;
  150. }
  151. /* All out of space. Need to allocate a new page. Use slot 0. */
  152. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  153. if (!kip)
  154. return NULL;
  155. /*
  156. * Use module_alloc so this page is within +/- 2GB of where the
  157. * kernel image and loaded module images reside. This is required
  158. * so x86_64 can correctly handle the %rip-relative fixups.
  159. */
  160. kip->insns = module_alloc(PAGE_SIZE);
  161. if (!kip->insns) {
  162. kfree(kip);
  163. return NULL;
  164. }
  165. INIT_HLIST_NODE(&kip->hlist);
  166. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  167. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  168. kip->slot_used[0] = SLOT_USED;
  169. kip->nused = 1;
  170. kip->ngarbage = 0;
  171. return kip->insns;
  172. }
  173. /* Return 1 if all garbages are collected, otherwise 0. */
  174. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  175. {
  176. kip->slot_used[idx] = SLOT_CLEAN;
  177. kip->nused--;
  178. if (kip->nused == 0) {
  179. /*
  180. * Page is no longer in use. Free it unless
  181. * it's the last one. We keep the last one
  182. * so as not to have to set it up again the
  183. * next time somebody inserts a probe.
  184. */
  185. hlist_del(&kip->hlist);
  186. if (hlist_empty(&kprobe_insn_pages)) {
  187. INIT_HLIST_NODE(&kip->hlist);
  188. hlist_add_head(&kip->hlist,
  189. &kprobe_insn_pages);
  190. } else {
  191. module_free(NULL, kip->insns);
  192. kfree(kip);
  193. }
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static int __kprobes collect_garbage_slots(void)
  199. {
  200. struct kprobe_insn_page *kip;
  201. struct hlist_node *pos, *next;
  202. /* Ensure no-one is preepmted on the garbages */
  203. if (check_safety() != 0)
  204. return -EAGAIN;
  205. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  206. int i;
  207. if (kip->ngarbage == 0)
  208. continue;
  209. kip->ngarbage = 0; /* we will collect all garbages */
  210. for (i = 0; i < INSNS_PER_PAGE; i++) {
  211. if (kip->slot_used[i] == SLOT_DIRTY &&
  212. collect_one_slot(kip, i))
  213. break;
  214. }
  215. }
  216. kprobe_garbage_slots = 0;
  217. return 0;
  218. }
  219. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  220. {
  221. struct kprobe_insn_page *kip;
  222. struct hlist_node *pos;
  223. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  224. if (kip->insns <= slot &&
  225. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  226. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  227. if (dirty) {
  228. kip->slot_used[i] = SLOT_DIRTY;
  229. kip->ngarbage++;
  230. } else {
  231. collect_one_slot(kip, i);
  232. }
  233. break;
  234. }
  235. }
  236. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  237. collect_garbage_slots();
  238. }
  239. #endif
  240. /* We have preemption disabled.. so it is safe to use __ versions */
  241. static inline void set_kprobe_instance(struct kprobe *kp)
  242. {
  243. __get_cpu_var(kprobe_instance) = kp;
  244. }
  245. static inline void reset_kprobe_instance(void)
  246. {
  247. __get_cpu_var(kprobe_instance) = NULL;
  248. }
  249. /*
  250. * This routine is called either:
  251. * - under the kprobe_mutex - during kprobe_[un]register()
  252. * OR
  253. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  254. */
  255. struct kprobe __kprobes *get_kprobe(void *addr)
  256. {
  257. struct hlist_head *head;
  258. struct hlist_node *node;
  259. struct kprobe *p;
  260. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  261. hlist_for_each_entry_rcu(p, node, head, hlist) {
  262. if (p->addr == addr)
  263. return p;
  264. }
  265. return NULL;
  266. }
  267. /*
  268. * Aggregate handlers for multiple kprobes support - these handlers
  269. * take care of invoking the individual kprobe handlers on p->list
  270. */
  271. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  272. {
  273. struct kprobe *kp;
  274. list_for_each_entry_rcu(kp, &p->list, list) {
  275. if (kp->pre_handler) {
  276. set_kprobe_instance(kp);
  277. if (kp->pre_handler(kp, regs))
  278. return 1;
  279. }
  280. reset_kprobe_instance();
  281. }
  282. return 0;
  283. }
  284. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  285. unsigned long flags)
  286. {
  287. struct kprobe *kp;
  288. list_for_each_entry_rcu(kp, &p->list, list) {
  289. if (kp->post_handler) {
  290. set_kprobe_instance(kp);
  291. kp->post_handler(kp, regs, flags);
  292. reset_kprobe_instance();
  293. }
  294. }
  295. }
  296. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  297. int trapnr)
  298. {
  299. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  300. /*
  301. * if we faulted "during" the execution of a user specified
  302. * probe handler, invoke just that probe's fault handler
  303. */
  304. if (cur && cur->fault_handler) {
  305. if (cur->fault_handler(cur, regs, trapnr))
  306. return 1;
  307. }
  308. return 0;
  309. }
  310. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  311. {
  312. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  313. int ret = 0;
  314. if (cur && cur->break_handler) {
  315. if (cur->break_handler(cur, regs))
  316. ret = 1;
  317. }
  318. reset_kprobe_instance();
  319. return ret;
  320. }
  321. /* Walks the list and increments nmissed count for multiprobe case */
  322. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  323. {
  324. struct kprobe *kp;
  325. if (p->pre_handler != aggr_pre_handler) {
  326. p->nmissed++;
  327. } else {
  328. list_for_each_entry_rcu(kp, &p->list, list)
  329. kp->nmissed++;
  330. }
  331. return;
  332. }
  333. /* Called with kretprobe_lock held */
  334. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  335. struct hlist_head *head)
  336. {
  337. /* remove rp inst off the rprobe_inst_table */
  338. hlist_del(&ri->hlist);
  339. if (ri->rp) {
  340. /* remove rp inst off the used list */
  341. hlist_del(&ri->uflist);
  342. /* put rp inst back onto the free list */
  343. INIT_HLIST_NODE(&ri->uflist);
  344. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  345. } else
  346. /* Unregistering */
  347. hlist_add_head(&ri->hlist, head);
  348. }
  349. struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  350. {
  351. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  352. }
  353. /*
  354. * This function is called from finish_task_switch when task tk becomes dead,
  355. * so that we can recycle any function-return probe instances associated
  356. * with this task. These left over instances represent probed functions
  357. * that have been called but will never return.
  358. */
  359. void __kprobes kprobe_flush_task(struct task_struct *tk)
  360. {
  361. struct kretprobe_instance *ri;
  362. struct hlist_head *head, empty_rp;
  363. struct hlist_node *node, *tmp;
  364. unsigned long flags = 0;
  365. INIT_HLIST_HEAD(&empty_rp);
  366. spin_lock_irqsave(&kretprobe_lock, flags);
  367. head = kretprobe_inst_table_head(tk);
  368. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  369. if (ri->task == tk)
  370. recycle_rp_inst(ri, &empty_rp);
  371. }
  372. spin_unlock_irqrestore(&kretprobe_lock, flags);
  373. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  374. hlist_del(&ri->hlist);
  375. kfree(ri);
  376. }
  377. }
  378. static inline void free_rp_inst(struct kretprobe *rp)
  379. {
  380. struct kretprobe_instance *ri;
  381. struct hlist_node *pos, *next;
  382. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
  383. hlist_del(&ri->uflist);
  384. kfree(ri);
  385. }
  386. }
  387. /*
  388. * Keep all fields in the kprobe consistent
  389. */
  390. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  391. {
  392. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  393. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  394. }
  395. /*
  396. * Add the new probe to old_p->list. Fail if this is the
  397. * second jprobe at the address - two jprobes can't coexist
  398. */
  399. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  400. {
  401. if (p->break_handler) {
  402. if (old_p->break_handler)
  403. return -EEXIST;
  404. list_add_tail_rcu(&p->list, &old_p->list);
  405. old_p->break_handler = aggr_break_handler;
  406. } else
  407. list_add_rcu(&p->list, &old_p->list);
  408. if (p->post_handler && !old_p->post_handler)
  409. old_p->post_handler = aggr_post_handler;
  410. return 0;
  411. }
  412. /*
  413. * Fill in the required fields of the "manager kprobe". Replace the
  414. * earlier kprobe in the hlist with the manager kprobe
  415. */
  416. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  417. {
  418. copy_kprobe(p, ap);
  419. flush_insn_slot(ap);
  420. ap->addr = p->addr;
  421. ap->pre_handler = aggr_pre_handler;
  422. ap->fault_handler = aggr_fault_handler;
  423. if (p->post_handler)
  424. ap->post_handler = aggr_post_handler;
  425. if (p->break_handler)
  426. ap->break_handler = aggr_break_handler;
  427. INIT_LIST_HEAD(&ap->list);
  428. list_add_rcu(&p->list, &ap->list);
  429. hlist_replace_rcu(&p->hlist, &ap->hlist);
  430. }
  431. /*
  432. * This is the second or subsequent kprobe at the address - handle
  433. * the intricacies
  434. */
  435. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  436. struct kprobe *p)
  437. {
  438. int ret = 0;
  439. struct kprobe *ap;
  440. if (old_p->pre_handler == aggr_pre_handler) {
  441. copy_kprobe(old_p, p);
  442. ret = add_new_kprobe(old_p, p);
  443. } else {
  444. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  445. if (!ap)
  446. return -ENOMEM;
  447. add_aggr_kprobe(ap, old_p);
  448. copy_kprobe(ap, p);
  449. ret = add_new_kprobe(ap, p);
  450. }
  451. return ret;
  452. }
  453. static int __kprobes in_kprobes_functions(unsigned long addr)
  454. {
  455. struct kprobe_blackpoint *kb;
  456. if (addr >= (unsigned long)__kprobes_text_start &&
  457. addr < (unsigned long)__kprobes_text_end)
  458. return -EINVAL;
  459. /*
  460. * If there exists a kprobe_blacklist, verify and
  461. * fail any probe registration in the prohibited area
  462. */
  463. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  464. if (kb->start_addr) {
  465. if (addr >= kb->start_addr &&
  466. addr < (kb->start_addr + kb->range))
  467. return -EINVAL;
  468. }
  469. }
  470. return 0;
  471. }
  472. /*
  473. * If we have a symbol_name argument, look it up and add the offset field
  474. * to it. This way, we can specify a relative address to a symbol.
  475. */
  476. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  477. {
  478. kprobe_opcode_t *addr = p->addr;
  479. if (p->symbol_name) {
  480. if (addr)
  481. return NULL;
  482. kprobe_lookup_name(p->symbol_name, addr);
  483. }
  484. if (!addr)
  485. return NULL;
  486. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  487. }
  488. static int __kprobes __register_kprobe(struct kprobe *p,
  489. unsigned long called_from)
  490. {
  491. int ret = 0;
  492. struct kprobe *old_p;
  493. struct module *probed_mod;
  494. kprobe_opcode_t *addr;
  495. addr = kprobe_addr(p);
  496. if (!addr)
  497. return -EINVAL;
  498. p->addr = addr;
  499. if (!kernel_text_address((unsigned long) p->addr) ||
  500. in_kprobes_functions((unsigned long) p->addr))
  501. return -EINVAL;
  502. p->mod_refcounted = 0;
  503. /*
  504. * Check if are we probing a module.
  505. */
  506. probed_mod = module_text_address((unsigned long) p->addr);
  507. if (probed_mod) {
  508. struct module *calling_mod = module_text_address(called_from);
  509. /*
  510. * We must allow modules to probe themself and in this case
  511. * avoid incrementing the module refcount, so as to allow
  512. * unloading of self probing modules.
  513. */
  514. if (calling_mod && calling_mod != probed_mod) {
  515. if (unlikely(!try_module_get(probed_mod)))
  516. return -EINVAL;
  517. p->mod_refcounted = 1;
  518. } else
  519. probed_mod = NULL;
  520. }
  521. p->nmissed = 0;
  522. INIT_LIST_HEAD(&p->list);
  523. mutex_lock(&kprobe_mutex);
  524. old_p = get_kprobe(p->addr);
  525. if (old_p) {
  526. ret = register_aggr_kprobe(old_p, p);
  527. goto out;
  528. }
  529. ret = arch_prepare_kprobe(p);
  530. if (ret)
  531. goto out;
  532. INIT_HLIST_NODE(&p->hlist);
  533. hlist_add_head_rcu(&p->hlist,
  534. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  535. if (kprobe_enabled)
  536. arch_arm_kprobe(p);
  537. out:
  538. mutex_unlock(&kprobe_mutex);
  539. if (ret && probed_mod)
  540. module_put(probed_mod);
  541. return ret;
  542. }
  543. /*
  544. * Unregister a kprobe without a scheduler synchronization.
  545. */
  546. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  547. {
  548. struct kprobe *old_p, *list_p;
  549. old_p = get_kprobe(p->addr);
  550. if (unlikely(!old_p))
  551. return -EINVAL;
  552. if (p != old_p) {
  553. list_for_each_entry_rcu(list_p, &old_p->list, list)
  554. if (list_p == p)
  555. /* kprobe p is a valid probe */
  556. goto valid_p;
  557. return -EINVAL;
  558. }
  559. valid_p:
  560. if (old_p == p ||
  561. (old_p->pre_handler == aggr_pre_handler &&
  562. list_is_singular(&old_p->list))) {
  563. /*
  564. * Only probe on the hash list. Disarm only if kprobes are
  565. * enabled - otherwise, the breakpoint would already have
  566. * been removed. We save on flushing icache.
  567. */
  568. if (kprobe_enabled)
  569. arch_disarm_kprobe(p);
  570. hlist_del_rcu(&old_p->hlist);
  571. } else {
  572. if (p->break_handler)
  573. old_p->break_handler = NULL;
  574. if (p->post_handler) {
  575. list_for_each_entry_rcu(list_p, &old_p->list, list) {
  576. if ((list_p != p) && (list_p->post_handler))
  577. goto noclean;
  578. }
  579. old_p->post_handler = NULL;
  580. }
  581. noclean:
  582. list_del_rcu(&p->list);
  583. }
  584. return 0;
  585. }
  586. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  587. {
  588. struct module *mod;
  589. struct kprobe *old_p;
  590. if (p->mod_refcounted) {
  591. mod = module_text_address((unsigned long)p->addr);
  592. if (mod)
  593. module_put(mod);
  594. }
  595. if (list_empty(&p->list) || list_is_singular(&p->list)) {
  596. if (!list_empty(&p->list)) {
  597. /* "p" is the last child of an aggr_kprobe */
  598. old_p = list_entry(p->list.next, struct kprobe, list);
  599. list_del(&p->list);
  600. kfree(old_p);
  601. }
  602. arch_remove_kprobe(p);
  603. }
  604. }
  605. static int __register_kprobes(struct kprobe **kps, int num,
  606. unsigned long called_from)
  607. {
  608. int i, ret = 0;
  609. if (num <= 0)
  610. return -EINVAL;
  611. for (i = 0; i < num; i++) {
  612. ret = __register_kprobe(kps[i], called_from);
  613. if (ret < 0 && i > 0) {
  614. unregister_kprobes(kps, i);
  615. break;
  616. }
  617. }
  618. return ret;
  619. }
  620. /*
  621. * Registration and unregistration functions for kprobe.
  622. */
  623. int __kprobes register_kprobe(struct kprobe *p)
  624. {
  625. return __register_kprobes(&p, 1,
  626. (unsigned long)__builtin_return_address(0));
  627. }
  628. void __kprobes unregister_kprobe(struct kprobe *p)
  629. {
  630. unregister_kprobes(&p, 1);
  631. }
  632. int __kprobes register_kprobes(struct kprobe **kps, int num)
  633. {
  634. return __register_kprobes(kps, num,
  635. (unsigned long)__builtin_return_address(0));
  636. }
  637. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  638. {
  639. int i;
  640. if (num <= 0)
  641. return;
  642. mutex_lock(&kprobe_mutex);
  643. for (i = 0; i < num; i++)
  644. if (__unregister_kprobe_top(kps[i]) < 0)
  645. kps[i]->addr = NULL;
  646. mutex_unlock(&kprobe_mutex);
  647. synchronize_sched();
  648. for (i = 0; i < num; i++)
  649. if (kps[i]->addr)
  650. __unregister_kprobe_bottom(kps[i]);
  651. }
  652. static struct notifier_block kprobe_exceptions_nb = {
  653. .notifier_call = kprobe_exceptions_notify,
  654. .priority = 0x7fffffff /* we need to be notified first */
  655. };
  656. unsigned long __weak arch_deref_entry_point(void *entry)
  657. {
  658. return (unsigned long)entry;
  659. }
  660. int __kprobes register_jprobe(struct jprobe *jp)
  661. {
  662. unsigned long addr = arch_deref_entry_point(jp->entry);
  663. if (!kernel_text_address(addr))
  664. return -EINVAL;
  665. /* Todo: Verify probepoint is a function entry point */
  666. jp->kp.pre_handler = setjmp_pre_handler;
  667. jp->kp.break_handler = longjmp_break_handler;
  668. return __register_kprobe(&jp->kp,
  669. (unsigned long)__builtin_return_address(0));
  670. }
  671. void __kprobes unregister_jprobe(struct jprobe *jp)
  672. {
  673. unregister_kprobe(&jp->kp);
  674. }
  675. #ifdef CONFIG_KRETPROBES
  676. /*
  677. * This kprobe pre_handler is registered with every kretprobe. When probe
  678. * hits it will set up the return probe.
  679. */
  680. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  681. struct pt_regs *regs)
  682. {
  683. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  684. unsigned long flags = 0;
  685. /*TODO: consider to only swap the RA after the last pre_handler fired */
  686. spin_lock_irqsave(&kretprobe_lock, flags);
  687. if (!hlist_empty(&rp->free_instances)) {
  688. struct kretprobe_instance *ri;
  689. ri = hlist_entry(rp->free_instances.first,
  690. struct kretprobe_instance, uflist);
  691. ri->rp = rp;
  692. ri->task = current;
  693. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  694. spin_unlock_irqrestore(&kretprobe_lock, flags);
  695. return 0;
  696. }
  697. arch_prepare_kretprobe(ri, regs);
  698. /* XXX(hch): why is there no hlist_move_head? */
  699. hlist_del(&ri->uflist);
  700. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  701. hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
  702. } else
  703. rp->nmissed++;
  704. spin_unlock_irqrestore(&kretprobe_lock, flags);
  705. return 0;
  706. }
  707. int __kprobes register_kretprobe(struct kretprobe *rp)
  708. {
  709. int ret = 0;
  710. struct kretprobe_instance *inst;
  711. int i;
  712. void *addr;
  713. if (kretprobe_blacklist_size) {
  714. addr = kprobe_addr(&rp->kp);
  715. if (!addr)
  716. return -EINVAL;
  717. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  718. if (kretprobe_blacklist[i].addr == addr)
  719. return -EINVAL;
  720. }
  721. }
  722. rp->kp.pre_handler = pre_handler_kretprobe;
  723. rp->kp.post_handler = NULL;
  724. rp->kp.fault_handler = NULL;
  725. rp->kp.break_handler = NULL;
  726. /* Pre-allocate memory for max kretprobe instances */
  727. if (rp->maxactive <= 0) {
  728. #ifdef CONFIG_PREEMPT
  729. rp->maxactive = max(10, 2 * NR_CPUS);
  730. #else
  731. rp->maxactive = NR_CPUS;
  732. #endif
  733. }
  734. INIT_HLIST_HEAD(&rp->used_instances);
  735. INIT_HLIST_HEAD(&rp->free_instances);
  736. for (i = 0; i < rp->maxactive; i++) {
  737. inst = kmalloc(sizeof(struct kretprobe_instance) +
  738. rp->data_size, GFP_KERNEL);
  739. if (inst == NULL) {
  740. free_rp_inst(rp);
  741. return -ENOMEM;
  742. }
  743. INIT_HLIST_NODE(&inst->uflist);
  744. hlist_add_head(&inst->uflist, &rp->free_instances);
  745. }
  746. rp->nmissed = 0;
  747. /* Establish function entry probe point */
  748. if ((ret = __register_kprobe(&rp->kp,
  749. (unsigned long)__builtin_return_address(0))) != 0)
  750. free_rp_inst(rp);
  751. return ret;
  752. }
  753. #else /* CONFIG_KRETPROBES */
  754. int __kprobes register_kretprobe(struct kretprobe *rp)
  755. {
  756. return -ENOSYS;
  757. }
  758. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  759. struct pt_regs *regs)
  760. {
  761. return 0;
  762. }
  763. #endif /* CONFIG_KRETPROBES */
  764. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  765. {
  766. unsigned long flags;
  767. struct kretprobe_instance *ri;
  768. struct hlist_node *pos, *next;
  769. unregister_kprobe(&rp->kp);
  770. /* No race here */
  771. spin_lock_irqsave(&kretprobe_lock, flags);
  772. hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
  773. ri->rp = NULL;
  774. hlist_del(&ri->uflist);
  775. }
  776. spin_unlock_irqrestore(&kretprobe_lock, flags);
  777. free_rp_inst(rp);
  778. }
  779. static int __init init_kprobes(void)
  780. {
  781. int i, err = 0;
  782. unsigned long offset = 0, size = 0;
  783. char *modname, namebuf[128];
  784. const char *symbol_name;
  785. void *addr;
  786. struct kprobe_blackpoint *kb;
  787. /* FIXME allocate the probe table, currently defined statically */
  788. /* initialize all list heads */
  789. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  790. INIT_HLIST_HEAD(&kprobe_table[i]);
  791. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  792. }
  793. /*
  794. * Lookup and populate the kprobe_blacklist.
  795. *
  796. * Unlike the kretprobe blacklist, we'll need to determine
  797. * the range of addresses that belong to the said functions,
  798. * since a kprobe need not necessarily be at the beginning
  799. * of a function.
  800. */
  801. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  802. kprobe_lookup_name(kb->name, addr);
  803. if (!addr)
  804. continue;
  805. kb->start_addr = (unsigned long)addr;
  806. symbol_name = kallsyms_lookup(kb->start_addr,
  807. &size, &offset, &modname, namebuf);
  808. if (!symbol_name)
  809. kb->range = 0;
  810. else
  811. kb->range = size;
  812. }
  813. if (kretprobe_blacklist_size) {
  814. /* lookup the function address from its name */
  815. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  816. kprobe_lookup_name(kretprobe_blacklist[i].name,
  817. kretprobe_blacklist[i].addr);
  818. if (!kretprobe_blacklist[i].addr)
  819. printk("kretprobe: lookup failed: %s\n",
  820. kretprobe_blacklist[i].name);
  821. }
  822. }
  823. /* By default, kprobes are enabled */
  824. kprobe_enabled = true;
  825. err = arch_init_kprobes();
  826. if (!err)
  827. err = register_die_notifier(&kprobe_exceptions_nb);
  828. if (!err)
  829. init_test_probes();
  830. return err;
  831. }
  832. #ifdef CONFIG_DEBUG_FS
  833. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  834. const char *sym, int offset,char *modname)
  835. {
  836. char *kprobe_type;
  837. if (p->pre_handler == pre_handler_kretprobe)
  838. kprobe_type = "r";
  839. else if (p->pre_handler == setjmp_pre_handler)
  840. kprobe_type = "j";
  841. else
  842. kprobe_type = "k";
  843. if (sym)
  844. seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
  845. sym, offset, (modname ? modname : " "));
  846. else
  847. seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
  848. }
  849. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  850. {
  851. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  852. }
  853. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  854. {
  855. (*pos)++;
  856. if (*pos >= KPROBE_TABLE_SIZE)
  857. return NULL;
  858. return pos;
  859. }
  860. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  861. {
  862. /* Nothing to do */
  863. }
  864. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  865. {
  866. struct hlist_head *head;
  867. struct hlist_node *node;
  868. struct kprobe *p, *kp;
  869. const char *sym = NULL;
  870. unsigned int i = *(loff_t *) v;
  871. unsigned long offset = 0;
  872. char *modname, namebuf[128];
  873. head = &kprobe_table[i];
  874. preempt_disable();
  875. hlist_for_each_entry_rcu(p, node, head, hlist) {
  876. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  877. &offset, &modname, namebuf);
  878. if (p->pre_handler == aggr_pre_handler) {
  879. list_for_each_entry_rcu(kp, &p->list, list)
  880. report_probe(pi, kp, sym, offset, modname);
  881. } else
  882. report_probe(pi, p, sym, offset, modname);
  883. }
  884. preempt_enable();
  885. return 0;
  886. }
  887. static struct seq_operations kprobes_seq_ops = {
  888. .start = kprobe_seq_start,
  889. .next = kprobe_seq_next,
  890. .stop = kprobe_seq_stop,
  891. .show = show_kprobe_addr
  892. };
  893. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  894. {
  895. return seq_open(filp, &kprobes_seq_ops);
  896. }
  897. static struct file_operations debugfs_kprobes_operations = {
  898. .open = kprobes_open,
  899. .read = seq_read,
  900. .llseek = seq_lseek,
  901. .release = seq_release,
  902. };
  903. static void __kprobes enable_all_kprobes(void)
  904. {
  905. struct hlist_head *head;
  906. struct hlist_node *node;
  907. struct kprobe *p;
  908. unsigned int i;
  909. mutex_lock(&kprobe_mutex);
  910. /* If kprobes are already enabled, just return */
  911. if (kprobe_enabled)
  912. goto already_enabled;
  913. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  914. head = &kprobe_table[i];
  915. hlist_for_each_entry_rcu(p, node, head, hlist)
  916. arch_arm_kprobe(p);
  917. }
  918. kprobe_enabled = true;
  919. printk(KERN_INFO "Kprobes globally enabled\n");
  920. already_enabled:
  921. mutex_unlock(&kprobe_mutex);
  922. return;
  923. }
  924. static void __kprobes disable_all_kprobes(void)
  925. {
  926. struct hlist_head *head;
  927. struct hlist_node *node;
  928. struct kprobe *p;
  929. unsigned int i;
  930. mutex_lock(&kprobe_mutex);
  931. /* If kprobes are already disabled, just return */
  932. if (!kprobe_enabled)
  933. goto already_disabled;
  934. kprobe_enabled = false;
  935. printk(KERN_INFO "Kprobes globally disabled\n");
  936. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  937. head = &kprobe_table[i];
  938. hlist_for_each_entry_rcu(p, node, head, hlist) {
  939. if (!arch_trampoline_kprobe(p))
  940. arch_disarm_kprobe(p);
  941. }
  942. }
  943. mutex_unlock(&kprobe_mutex);
  944. /* Allow all currently running kprobes to complete */
  945. synchronize_sched();
  946. return;
  947. already_disabled:
  948. mutex_unlock(&kprobe_mutex);
  949. return;
  950. }
  951. /*
  952. * XXX: The debugfs bool file interface doesn't allow for callbacks
  953. * when the bool state is switched. We can reuse that facility when
  954. * available
  955. */
  956. static ssize_t read_enabled_file_bool(struct file *file,
  957. char __user *user_buf, size_t count, loff_t *ppos)
  958. {
  959. char buf[3];
  960. if (kprobe_enabled)
  961. buf[0] = '1';
  962. else
  963. buf[0] = '0';
  964. buf[1] = '\n';
  965. buf[2] = 0x00;
  966. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  967. }
  968. static ssize_t write_enabled_file_bool(struct file *file,
  969. const char __user *user_buf, size_t count, loff_t *ppos)
  970. {
  971. char buf[32];
  972. int buf_size;
  973. buf_size = min(count, (sizeof(buf)-1));
  974. if (copy_from_user(buf, user_buf, buf_size))
  975. return -EFAULT;
  976. switch (buf[0]) {
  977. case 'y':
  978. case 'Y':
  979. case '1':
  980. enable_all_kprobes();
  981. break;
  982. case 'n':
  983. case 'N':
  984. case '0':
  985. disable_all_kprobes();
  986. break;
  987. }
  988. return count;
  989. }
  990. static struct file_operations fops_kp = {
  991. .read = read_enabled_file_bool,
  992. .write = write_enabled_file_bool,
  993. };
  994. static int __kprobes debugfs_kprobe_init(void)
  995. {
  996. struct dentry *dir, *file;
  997. unsigned int value = 1;
  998. dir = debugfs_create_dir("kprobes", NULL);
  999. if (!dir)
  1000. return -ENOMEM;
  1001. file = debugfs_create_file("list", 0444, dir, NULL,
  1002. &debugfs_kprobes_operations);
  1003. if (!file) {
  1004. debugfs_remove(dir);
  1005. return -ENOMEM;
  1006. }
  1007. file = debugfs_create_file("enabled", 0600, dir,
  1008. &value, &fops_kp);
  1009. if (!file) {
  1010. debugfs_remove(dir);
  1011. return -ENOMEM;
  1012. }
  1013. return 0;
  1014. }
  1015. late_initcall(debugfs_kprobe_init);
  1016. #endif /* CONFIG_DEBUG_FS */
  1017. module_init(init_kprobes);
  1018. EXPORT_SYMBOL_GPL(register_kprobe);
  1019. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1020. EXPORT_SYMBOL_GPL(register_kprobes);
  1021. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1022. EXPORT_SYMBOL_GPL(register_jprobe);
  1023. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1024. #ifdef CONFIG_KPROBES
  1025. EXPORT_SYMBOL_GPL(jprobe_return);
  1026. #endif
  1027. #ifdef CONFIG_KPROBES
  1028. EXPORT_SYMBOL_GPL(register_kretprobe);
  1029. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1030. #endif