kprobes.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. mutex_lock(&kprobe_mutex);
  523. old_p = get_kprobe(p->addr);
  524. if (old_p) {
  525. ret = register_aggr_kprobe(old_p, p);
  526. goto out;
  527. }
  528. ret = arch_prepare_kprobe(p);
  529. if (ret)
  530. goto out;
  531. INIT_HLIST_NODE(&p->hlist);
  532. hlist_add_head_rcu(&p->hlist,
  533. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  534. if (kprobe_enabled)
  535. arch_arm_kprobe(p);
  536. out:
  537. mutex_unlock(&kprobe_mutex);
  538. if (ret && probed_mod)
  539. module_put(probed_mod);
  540. return ret;
  541. }
  542. int __kprobes register_kprobe(struct kprobe *p)
  543. {
  544. return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
  545. }
  546. void __kprobes unregister_kprobe(struct kprobe *p)
  547. {
  548. struct module *mod;
  549. struct kprobe *old_p, *list_p;
  550. int cleanup_p;
  551. mutex_lock(&kprobe_mutex);
  552. old_p = get_kprobe(p->addr);
  553. if (unlikely(!old_p)) {
  554. mutex_unlock(&kprobe_mutex);
  555. return;
  556. }
  557. if (p != old_p) {
  558. list_for_each_entry_rcu(list_p, &old_p->list, list)
  559. if (list_p == p)
  560. /* kprobe p is a valid probe */
  561. goto valid_p;
  562. mutex_unlock(&kprobe_mutex);
  563. return;
  564. }
  565. valid_p:
  566. if (old_p == p ||
  567. (old_p->pre_handler == aggr_pre_handler &&
  568. p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
  569. /*
  570. * Only probe on the hash list. Disarm only if kprobes are
  571. * enabled - otherwise, the breakpoint would already have
  572. * been removed. We save on flushing icache.
  573. */
  574. if (kprobe_enabled)
  575. arch_disarm_kprobe(p);
  576. hlist_del_rcu(&old_p->hlist);
  577. cleanup_p = 1;
  578. } else {
  579. list_del_rcu(&p->list);
  580. cleanup_p = 0;
  581. }
  582. mutex_unlock(&kprobe_mutex);
  583. synchronize_sched();
  584. if (p->mod_refcounted) {
  585. mod = module_text_address((unsigned long)p->addr);
  586. if (mod)
  587. module_put(mod);
  588. }
  589. if (cleanup_p) {
  590. if (p != old_p) {
  591. list_del_rcu(&p->list);
  592. kfree(old_p);
  593. }
  594. arch_remove_kprobe(p);
  595. } else {
  596. mutex_lock(&kprobe_mutex);
  597. if (p->break_handler)
  598. old_p->break_handler = NULL;
  599. if (p->post_handler){
  600. list_for_each_entry_rcu(list_p, &old_p->list, list){
  601. if (list_p->post_handler){
  602. cleanup_p = 2;
  603. break;
  604. }
  605. }
  606. if (cleanup_p == 0)
  607. old_p->post_handler = NULL;
  608. }
  609. mutex_unlock(&kprobe_mutex);
  610. }
  611. }
  612. static struct notifier_block kprobe_exceptions_nb = {
  613. .notifier_call = kprobe_exceptions_notify,
  614. .priority = 0x7fffffff /* we need to be notified first */
  615. };
  616. unsigned long __weak arch_deref_entry_point(void *entry)
  617. {
  618. return (unsigned long)entry;
  619. }
  620. int __kprobes register_jprobe(struct jprobe *jp)
  621. {
  622. unsigned long addr = arch_deref_entry_point(jp->entry);
  623. if (!kernel_text_address(addr))
  624. return -EINVAL;
  625. /* Todo: Verify probepoint is a function entry point */
  626. jp->kp.pre_handler = setjmp_pre_handler;
  627. jp->kp.break_handler = longjmp_break_handler;
  628. return __register_kprobe(&jp->kp,
  629. (unsigned long)__builtin_return_address(0));
  630. }
  631. void __kprobes unregister_jprobe(struct jprobe *jp)
  632. {
  633. unregister_kprobe(&jp->kp);
  634. }
  635. #ifdef CONFIG_KRETPROBES
  636. /*
  637. * This kprobe pre_handler is registered with every kretprobe. When probe
  638. * hits it will set up the return probe.
  639. */
  640. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  641. struct pt_regs *regs)
  642. {
  643. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  644. unsigned long flags = 0;
  645. /*TODO: consider to only swap the RA after the last pre_handler fired */
  646. spin_lock_irqsave(&kretprobe_lock, flags);
  647. if (!hlist_empty(&rp->free_instances)) {
  648. struct kretprobe_instance *ri;
  649. ri = hlist_entry(rp->free_instances.first,
  650. struct kretprobe_instance, uflist);
  651. ri->rp = rp;
  652. ri->task = current;
  653. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  654. spin_unlock_irqrestore(&kretprobe_lock, flags);
  655. return 0;
  656. }
  657. arch_prepare_kretprobe(ri, regs);
  658. /* XXX(hch): why is there no hlist_move_head? */
  659. hlist_del(&ri->uflist);
  660. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  661. hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
  662. } else
  663. rp->nmissed++;
  664. spin_unlock_irqrestore(&kretprobe_lock, flags);
  665. return 0;
  666. }
  667. int __kprobes register_kretprobe(struct kretprobe *rp)
  668. {
  669. int ret = 0;
  670. struct kretprobe_instance *inst;
  671. int i;
  672. void *addr;
  673. if (kretprobe_blacklist_size) {
  674. addr = kprobe_addr(&rp->kp);
  675. if (!addr)
  676. return -EINVAL;
  677. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  678. if (kretprobe_blacklist[i].addr == addr)
  679. return -EINVAL;
  680. }
  681. }
  682. rp->kp.pre_handler = pre_handler_kretprobe;
  683. rp->kp.post_handler = NULL;
  684. rp->kp.fault_handler = NULL;
  685. rp->kp.break_handler = NULL;
  686. /* Pre-allocate memory for max kretprobe instances */
  687. if (rp->maxactive <= 0) {
  688. #ifdef CONFIG_PREEMPT
  689. rp->maxactive = max(10, 2 * NR_CPUS);
  690. #else
  691. rp->maxactive = NR_CPUS;
  692. #endif
  693. }
  694. INIT_HLIST_HEAD(&rp->used_instances);
  695. INIT_HLIST_HEAD(&rp->free_instances);
  696. for (i = 0; i < rp->maxactive; i++) {
  697. inst = kmalloc(sizeof(struct kretprobe_instance) +
  698. rp->data_size, GFP_KERNEL);
  699. if (inst == NULL) {
  700. free_rp_inst(rp);
  701. return -ENOMEM;
  702. }
  703. INIT_HLIST_NODE(&inst->uflist);
  704. hlist_add_head(&inst->uflist, &rp->free_instances);
  705. }
  706. rp->nmissed = 0;
  707. /* Establish function entry probe point */
  708. if ((ret = __register_kprobe(&rp->kp,
  709. (unsigned long)__builtin_return_address(0))) != 0)
  710. free_rp_inst(rp);
  711. return ret;
  712. }
  713. #else /* CONFIG_KRETPROBES */
  714. int __kprobes register_kretprobe(struct kretprobe *rp)
  715. {
  716. return -ENOSYS;
  717. }
  718. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  719. struct pt_regs *regs)
  720. {
  721. return 0;
  722. }
  723. #endif /* CONFIG_KRETPROBES */
  724. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  725. {
  726. unsigned long flags;
  727. struct kretprobe_instance *ri;
  728. struct hlist_node *pos, *next;
  729. unregister_kprobe(&rp->kp);
  730. /* No race here */
  731. spin_lock_irqsave(&kretprobe_lock, flags);
  732. hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
  733. ri->rp = NULL;
  734. hlist_del(&ri->uflist);
  735. }
  736. spin_unlock_irqrestore(&kretprobe_lock, flags);
  737. free_rp_inst(rp);
  738. }
  739. static int __init init_kprobes(void)
  740. {
  741. int i, err = 0;
  742. unsigned long offset = 0, size = 0;
  743. char *modname, namebuf[128];
  744. const char *symbol_name;
  745. void *addr;
  746. struct kprobe_blackpoint *kb;
  747. /* FIXME allocate the probe table, currently defined statically */
  748. /* initialize all list heads */
  749. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  750. INIT_HLIST_HEAD(&kprobe_table[i]);
  751. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  752. }
  753. /*
  754. * Lookup and populate the kprobe_blacklist.
  755. *
  756. * Unlike the kretprobe blacklist, we'll need to determine
  757. * the range of addresses that belong to the said functions,
  758. * since a kprobe need not necessarily be at the beginning
  759. * of a function.
  760. */
  761. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  762. kprobe_lookup_name(kb->name, addr);
  763. if (!addr)
  764. continue;
  765. kb->start_addr = (unsigned long)addr;
  766. symbol_name = kallsyms_lookup(kb->start_addr,
  767. &size, &offset, &modname, namebuf);
  768. if (!symbol_name)
  769. kb->range = 0;
  770. else
  771. kb->range = size;
  772. }
  773. if (kretprobe_blacklist_size) {
  774. /* lookup the function address from its name */
  775. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  776. kprobe_lookup_name(kretprobe_blacklist[i].name,
  777. kretprobe_blacklist[i].addr);
  778. if (!kretprobe_blacklist[i].addr)
  779. printk("kretprobe: lookup failed: %s\n",
  780. kretprobe_blacklist[i].name);
  781. }
  782. }
  783. /* By default, kprobes are enabled */
  784. kprobe_enabled = true;
  785. err = arch_init_kprobes();
  786. if (!err)
  787. err = register_die_notifier(&kprobe_exceptions_nb);
  788. if (!err)
  789. init_test_probes();
  790. return err;
  791. }
  792. #ifdef CONFIG_DEBUG_FS
  793. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  794. const char *sym, int offset,char *modname)
  795. {
  796. char *kprobe_type;
  797. if (p->pre_handler == pre_handler_kretprobe)
  798. kprobe_type = "r";
  799. else if (p->pre_handler == setjmp_pre_handler)
  800. kprobe_type = "j";
  801. else
  802. kprobe_type = "k";
  803. if (sym)
  804. seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
  805. sym, offset, (modname ? modname : " "));
  806. else
  807. seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
  808. }
  809. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  810. {
  811. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  812. }
  813. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  814. {
  815. (*pos)++;
  816. if (*pos >= KPROBE_TABLE_SIZE)
  817. return NULL;
  818. return pos;
  819. }
  820. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  821. {
  822. /* Nothing to do */
  823. }
  824. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  825. {
  826. struct hlist_head *head;
  827. struct hlist_node *node;
  828. struct kprobe *p, *kp;
  829. const char *sym = NULL;
  830. unsigned int i = *(loff_t *) v;
  831. unsigned long offset = 0;
  832. char *modname, namebuf[128];
  833. head = &kprobe_table[i];
  834. preempt_disable();
  835. hlist_for_each_entry_rcu(p, node, head, hlist) {
  836. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  837. &offset, &modname, namebuf);
  838. if (p->pre_handler == aggr_pre_handler) {
  839. list_for_each_entry_rcu(kp, &p->list, list)
  840. report_probe(pi, kp, sym, offset, modname);
  841. } else
  842. report_probe(pi, p, sym, offset, modname);
  843. }
  844. preempt_enable();
  845. return 0;
  846. }
  847. static struct seq_operations kprobes_seq_ops = {
  848. .start = kprobe_seq_start,
  849. .next = kprobe_seq_next,
  850. .stop = kprobe_seq_stop,
  851. .show = show_kprobe_addr
  852. };
  853. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  854. {
  855. return seq_open(filp, &kprobes_seq_ops);
  856. }
  857. static struct file_operations debugfs_kprobes_operations = {
  858. .open = kprobes_open,
  859. .read = seq_read,
  860. .llseek = seq_lseek,
  861. .release = seq_release,
  862. };
  863. static void __kprobes enable_all_kprobes(void)
  864. {
  865. struct hlist_head *head;
  866. struct hlist_node *node;
  867. struct kprobe *p;
  868. unsigned int i;
  869. mutex_lock(&kprobe_mutex);
  870. /* If kprobes are already enabled, just return */
  871. if (kprobe_enabled)
  872. goto already_enabled;
  873. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  874. head = &kprobe_table[i];
  875. hlist_for_each_entry_rcu(p, node, head, hlist)
  876. arch_arm_kprobe(p);
  877. }
  878. kprobe_enabled = true;
  879. printk(KERN_INFO "Kprobes globally enabled\n");
  880. already_enabled:
  881. mutex_unlock(&kprobe_mutex);
  882. return;
  883. }
  884. static void __kprobes disable_all_kprobes(void)
  885. {
  886. struct hlist_head *head;
  887. struct hlist_node *node;
  888. struct kprobe *p;
  889. unsigned int i;
  890. mutex_lock(&kprobe_mutex);
  891. /* If kprobes are already disabled, just return */
  892. if (!kprobe_enabled)
  893. goto already_disabled;
  894. kprobe_enabled = false;
  895. printk(KERN_INFO "Kprobes globally disabled\n");
  896. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  897. head = &kprobe_table[i];
  898. hlist_for_each_entry_rcu(p, node, head, hlist) {
  899. if (!arch_trampoline_kprobe(p))
  900. arch_disarm_kprobe(p);
  901. }
  902. }
  903. mutex_unlock(&kprobe_mutex);
  904. /* Allow all currently running kprobes to complete */
  905. synchronize_sched();
  906. return;
  907. already_disabled:
  908. mutex_unlock(&kprobe_mutex);
  909. return;
  910. }
  911. /*
  912. * XXX: The debugfs bool file interface doesn't allow for callbacks
  913. * when the bool state is switched. We can reuse that facility when
  914. * available
  915. */
  916. static ssize_t read_enabled_file_bool(struct file *file,
  917. char __user *user_buf, size_t count, loff_t *ppos)
  918. {
  919. char buf[3];
  920. if (kprobe_enabled)
  921. buf[0] = '1';
  922. else
  923. buf[0] = '0';
  924. buf[1] = '\n';
  925. buf[2] = 0x00;
  926. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  927. }
  928. static ssize_t write_enabled_file_bool(struct file *file,
  929. const char __user *user_buf, size_t count, loff_t *ppos)
  930. {
  931. char buf[32];
  932. int buf_size;
  933. buf_size = min(count, (sizeof(buf)-1));
  934. if (copy_from_user(buf, user_buf, buf_size))
  935. return -EFAULT;
  936. switch (buf[0]) {
  937. case 'y':
  938. case 'Y':
  939. case '1':
  940. enable_all_kprobes();
  941. break;
  942. case 'n':
  943. case 'N':
  944. case '0':
  945. disable_all_kprobes();
  946. break;
  947. }
  948. return count;
  949. }
  950. static struct file_operations fops_kp = {
  951. .read = read_enabled_file_bool,
  952. .write = write_enabled_file_bool,
  953. };
  954. static int __kprobes debugfs_kprobe_init(void)
  955. {
  956. struct dentry *dir, *file;
  957. unsigned int value = 1;
  958. dir = debugfs_create_dir("kprobes", NULL);
  959. if (!dir)
  960. return -ENOMEM;
  961. file = debugfs_create_file("list", 0444, dir, NULL,
  962. &debugfs_kprobes_operations);
  963. if (!file) {
  964. debugfs_remove(dir);
  965. return -ENOMEM;
  966. }
  967. file = debugfs_create_file("enabled", 0600, dir,
  968. &value, &fops_kp);
  969. if (!file) {
  970. debugfs_remove(dir);
  971. return -ENOMEM;
  972. }
  973. return 0;
  974. }
  975. late_initcall(debugfs_kprobe_init);
  976. #endif /* CONFIG_DEBUG_FS */
  977. module_init(init_kprobes);
  978. EXPORT_SYMBOL_GPL(register_kprobe);
  979. EXPORT_SYMBOL_GPL(unregister_kprobe);
  980. EXPORT_SYMBOL_GPL(register_jprobe);
  981. EXPORT_SYMBOL_GPL(unregister_jprobe);
  982. #ifdef CONFIG_KPROBES
  983. EXPORT_SYMBOL_GPL(jprobe_return);
  984. #endif
  985. #ifdef CONFIG_KPROBES
  986. EXPORT_SYMBOL_GPL(register_kretprobe);
  987. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  988. #endif