kprobes.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. static atomic_t kprobe_count;
  63. /* NOTE: change this value only with kprobe_mutex held */
  64. static bool kprobe_enabled;
  65. DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  66. DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
  67. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  68. static struct notifier_block kprobe_page_fault_nb = {
  69. .notifier_call = kprobe_exceptions_notify,
  70. .priority = 0x7fffffff /* we need to notified first */
  71. };
  72. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  73. /*
  74. * kprobe->ainsn.insn points to the copy of the instruction to be
  75. * single-stepped. x86_64, POWER4 and above have no-exec support and
  76. * stepping on the instruction on a vmalloced/kmalloced/data page
  77. * is a recipe for disaster
  78. */
  79. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  80. struct kprobe_insn_page {
  81. struct hlist_node hlist;
  82. kprobe_opcode_t *insns; /* Page of instruction slots */
  83. char slot_used[INSNS_PER_PAGE];
  84. int nused;
  85. int ngarbage;
  86. };
  87. enum kprobe_slot_state {
  88. SLOT_CLEAN = 0,
  89. SLOT_DIRTY = 1,
  90. SLOT_USED = 2,
  91. };
  92. static struct hlist_head kprobe_insn_pages;
  93. static int kprobe_garbage_slots;
  94. static int collect_garbage_slots(void);
  95. static int __kprobes check_safety(void)
  96. {
  97. int ret = 0;
  98. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  99. ret = freeze_processes();
  100. if (ret == 0) {
  101. struct task_struct *p, *q;
  102. do_each_thread(p, q) {
  103. if (p != current && p->state == TASK_RUNNING &&
  104. p->pid != 0) {
  105. printk("Check failed: %s is running\n",p->comm);
  106. ret = -1;
  107. goto loop_end;
  108. }
  109. } while_each_thread(p, q);
  110. }
  111. loop_end:
  112. thaw_processes();
  113. #else
  114. synchronize_sched();
  115. #endif
  116. return ret;
  117. }
  118. /**
  119. * get_insn_slot() - Find a slot on an executable page for an instruction.
  120. * We allocate an executable page if there's no room on existing ones.
  121. */
  122. kprobe_opcode_t __kprobes *get_insn_slot(void)
  123. {
  124. struct kprobe_insn_page *kip;
  125. struct hlist_node *pos;
  126. retry:
  127. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  128. if (kip->nused < INSNS_PER_PAGE) {
  129. int i;
  130. for (i = 0; i < INSNS_PER_PAGE; i++) {
  131. if (kip->slot_used[i] == SLOT_CLEAN) {
  132. kip->slot_used[i] = SLOT_USED;
  133. kip->nused++;
  134. return kip->insns + (i * MAX_INSN_SIZE);
  135. }
  136. }
  137. /* Surprise! No unused slots. Fix kip->nused. */
  138. kip->nused = INSNS_PER_PAGE;
  139. }
  140. }
  141. /* If there are any garbage slots, collect it and try again. */
  142. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  143. goto retry;
  144. }
  145. /* All out of space. Need to allocate a new page. Use slot 0. */
  146. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  147. if (!kip)
  148. return NULL;
  149. /*
  150. * Use module_alloc so this page is within +/- 2GB of where the
  151. * kernel image and loaded module images reside. This is required
  152. * so x86_64 can correctly handle the %rip-relative fixups.
  153. */
  154. kip->insns = module_alloc(PAGE_SIZE);
  155. if (!kip->insns) {
  156. kfree(kip);
  157. return NULL;
  158. }
  159. INIT_HLIST_NODE(&kip->hlist);
  160. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  161. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  162. kip->slot_used[0] = SLOT_USED;
  163. kip->nused = 1;
  164. kip->ngarbage = 0;
  165. return kip->insns;
  166. }
  167. /* Return 1 if all garbages are collected, otherwise 0. */
  168. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  169. {
  170. kip->slot_used[idx] = SLOT_CLEAN;
  171. kip->nused--;
  172. if (kip->nused == 0) {
  173. /*
  174. * Page is no longer in use. Free it unless
  175. * it's the last one. We keep the last one
  176. * so as not to have to set it up again the
  177. * next time somebody inserts a probe.
  178. */
  179. hlist_del(&kip->hlist);
  180. if (hlist_empty(&kprobe_insn_pages)) {
  181. INIT_HLIST_NODE(&kip->hlist);
  182. hlist_add_head(&kip->hlist,
  183. &kprobe_insn_pages);
  184. } else {
  185. module_free(NULL, kip->insns);
  186. kfree(kip);
  187. }
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. static int __kprobes collect_garbage_slots(void)
  193. {
  194. struct kprobe_insn_page *kip;
  195. struct hlist_node *pos, *next;
  196. /* Ensure no-one is preepmted on the garbages */
  197. if (check_safety() != 0)
  198. return -EAGAIN;
  199. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  200. int i;
  201. if (kip->ngarbage == 0)
  202. continue;
  203. kip->ngarbage = 0; /* we will collect all garbages */
  204. for (i = 0; i < INSNS_PER_PAGE; i++) {
  205. if (kip->slot_used[i] == SLOT_DIRTY &&
  206. collect_one_slot(kip, i))
  207. break;
  208. }
  209. }
  210. kprobe_garbage_slots = 0;
  211. return 0;
  212. }
  213. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  214. {
  215. struct kprobe_insn_page *kip;
  216. struct hlist_node *pos;
  217. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  218. if (kip->insns <= slot &&
  219. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  220. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  221. if (dirty) {
  222. kip->slot_used[i] = SLOT_DIRTY;
  223. kip->ngarbage++;
  224. } else {
  225. collect_one_slot(kip, i);
  226. }
  227. break;
  228. }
  229. }
  230. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  231. collect_garbage_slots();
  232. }
  233. #endif
  234. /* We have preemption disabled.. so it is safe to use __ versions */
  235. static inline void set_kprobe_instance(struct kprobe *kp)
  236. {
  237. __get_cpu_var(kprobe_instance) = kp;
  238. }
  239. static inline void reset_kprobe_instance(void)
  240. {
  241. __get_cpu_var(kprobe_instance) = NULL;
  242. }
  243. /*
  244. * This routine is called either:
  245. * - under the kprobe_mutex - during kprobe_[un]register()
  246. * OR
  247. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  248. */
  249. struct kprobe __kprobes *get_kprobe(void *addr)
  250. {
  251. struct hlist_head *head;
  252. struct hlist_node *node;
  253. struct kprobe *p;
  254. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  255. hlist_for_each_entry_rcu(p, node, head, hlist) {
  256. if (p->addr == addr)
  257. return p;
  258. }
  259. return NULL;
  260. }
  261. /*
  262. * Aggregate handlers for multiple kprobes support - these handlers
  263. * take care of invoking the individual kprobe handlers on p->list
  264. */
  265. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  266. {
  267. struct kprobe *kp;
  268. list_for_each_entry_rcu(kp, &p->list, list) {
  269. if (kp->pre_handler) {
  270. set_kprobe_instance(kp);
  271. if (kp->pre_handler(kp, regs))
  272. return 1;
  273. }
  274. reset_kprobe_instance();
  275. }
  276. return 0;
  277. }
  278. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  279. unsigned long flags)
  280. {
  281. struct kprobe *kp;
  282. list_for_each_entry_rcu(kp, &p->list, list) {
  283. if (kp->post_handler) {
  284. set_kprobe_instance(kp);
  285. kp->post_handler(kp, regs, flags);
  286. reset_kprobe_instance();
  287. }
  288. }
  289. }
  290. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  291. int trapnr)
  292. {
  293. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  294. /*
  295. * if we faulted "during" the execution of a user specified
  296. * probe handler, invoke just that probe's fault handler
  297. */
  298. if (cur && cur->fault_handler) {
  299. if (cur->fault_handler(cur, regs, trapnr))
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  305. {
  306. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  307. int ret = 0;
  308. if (cur && cur->break_handler) {
  309. if (cur->break_handler(cur, regs))
  310. ret = 1;
  311. }
  312. reset_kprobe_instance();
  313. return ret;
  314. }
  315. /* Walks the list and increments nmissed count for multiprobe case */
  316. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  317. {
  318. struct kprobe *kp;
  319. if (p->pre_handler != aggr_pre_handler) {
  320. p->nmissed++;
  321. } else {
  322. list_for_each_entry_rcu(kp, &p->list, list)
  323. kp->nmissed++;
  324. }
  325. return;
  326. }
  327. /* Called with kretprobe_lock held */
  328. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  329. struct hlist_head *head)
  330. {
  331. /* remove rp inst off the rprobe_inst_table */
  332. hlist_del(&ri->hlist);
  333. if (ri->rp) {
  334. /* remove rp inst off the used list */
  335. hlist_del(&ri->uflist);
  336. /* put rp inst back onto the free list */
  337. INIT_HLIST_NODE(&ri->uflist);
  338. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  339. } else
  340. /* Unregistering */
  341. hlist_add_head(&ri->hlist, head);
  342. }
  343. struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  344. {
  345. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  346. }
  347. /*
  348. * This function is called from finish_task_switch when task tk becomes dead,
  349. * so that we can recycle any function-return probe instances associated
  350. * with this task. These left over instances represent probed functions
  351. * that have been called but will never return.
  352. */
  353. void __kprobes kprobe_flush_task(struct task_struct *tk)
  354. {
  355. struct kretprobe_instance *ri;
  356. struct hlist_head *head, empty_rp;
  357. struct hlist_node *node, *tmp;
  358. unsigned long flags = 0;
  359. INIT_HLIST_HEAD(&empty_rp);
  360. spin_lock_irqsave(&kretprobe_lock, flags);
  361. head = kretprobe_inst_table_head(tk);
  362. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  363. if (ri->task == tk)
  364. recycle_rp_inst(ri, &empty_rp);
  365. }
  366. spin_unlock_irqrestore(&kretprobe_lock, flags);
  367. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  368. hlist_del(&ri->hlist);
  369. kfree(ri);
  370. }
  371. }
  372. static inline void free_rp_inst(struct kretprobe *rp)
  373. {
  374. struct kretprobe_instance *ri;
  375. struct hlist_node *pos, *next;
  376. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
  377. hlist_del(&ri->uflist);
  378. kfree(ri);
  379. }
  380. }
  381. /*
  382. * Keep all fields in the kprobe consistent
  383. */
  384. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  385. {
  386. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  387. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  388. }
  389. /*
  390. * Add the new probe to old_p->list. Fail if this is the
  391. * second jprobe at the address - two jprobes can't coexist
  392. */
  393. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  394. {
  395. if (p->break_handler) {
  396. if (old_p->break_handler)
  397. return -EEXIST;
  398. list_add_tail_rcu(&p->list, &old_p->list);
  399. old_p->break_handler = aggr_break_handler;
  400. } else
  401. list_add_rcu(&p->list, &old_p->list);
  402. if (p->post_handler && !old_p->post_handler)
  403. old_p->post_handler = aggr_post_handler;
  404. return 0;
  405. }
  406. /*
  407. * Fill in the required fields of the "manager kprobe". Replace the
  408. * earlier kprobe in the hlist with the manager kprobe
  409. */
  410. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  411. {
  412. copy_kprobe(p, ap);
  413. flush_insn_slot(ap);
  414. ap->addr = p->addr;
  415. ap->pre_handler = aggr_pre_handler;
  416. ap->fault_handler = aggr_fault_handler;
  417. if (p->post_handler)
  418. ap->post_handler = aggr_post_handler;
  419. if (p->break_handler)
  420. ap->break_handler = aggr_break_handler;
  421. INIT_LIST_HEAD(&ap->list);
  422. list_add_rcu(&p->list, &ap->list);
  423. hlist_replace_rcu(&p->hlist, &ap->hlist);
  424. }
  425. /*
  426. * This is the second or subsequent kprobe at the address - handle
  427. * the intricacies
  428. */
  429. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  430. struct kprobe *p)
  431. {
  432. int ret = 0;
  433. struct kprobe *ap;
  434. if (old_p->pre_handler == aggr_pre_handler) {
  435. copy_kprobe(old_p, p);
  436. ret = add_new_kprobe(old_p, p);
  437. } else {
  438. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  439. if (!ap)
  440. return -ENOMEM;
  441. add_aggr_kprobe(ap, old_p);
  442. copy_kprobe(ap, p);
  443. ret = add_new_kprobe(ap, p);
  444. }
  445. return ret;
  446. }
  447. static int __kprobes in_kprobes_functions(unsigned long addr)
  448. {
  449. if (addr >= (unsigned long)__kprobes_text_start &&
  450. addr < (unsigned long)__kprobes_text_end)
  451. return -EINVAL;
  452. return 0;
  453. }
  454. static int __kprobes __register_kprobe(struct kprobe *p,
  455. unsigned long called_from)
  456. {
  457. int ret = 0;
  458. struct kprobe *old_p;
  459. struct module *probed_mod;
  460. /*
  461. * If we have a symbol_name argument look it up,
  462. * and add it to the address. That way the addr
  463. * field can either be global or relative to a symbol.
  464. */
  465. if (p->symbol_name) {
  466. if (p->addr)
  467. return -EINVAL;
  468. kprobe_lookup_name(p->symbol_name, p->addr);
  469. }
  470. if (!p->addr)
  471. return -EINVAL;
  472. p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
  473. if (!kernel_text_address((unsigned long) p->addr) ||
  474. in_kprobes_functions((unsigned long) p->addr))
  475. return -EINVAL;
  476. p->mod_refcounted = 0;
  477. /*
  478. * Check if are we probing a module.
  479. */
  480. probed_mod = module_text_address((unsigned long) p->addr);
  481. if (probed_mod) {
  482. struct module *calling_mod = module_text_address(called_from);
  483. /*
  484. * We must allow modules to probe themself and in this case
  485. * avoid incrementing the module refcount, so as to allow
  486. * unloading of self probing modules.
  487. */
  488. if (calling_mod && calling_mod != probed_mod) {
  489. if (unlikely(!try_module_get(probed_mod)))
  490. return -EINVAL;
  491. p->mod_refcounted = 1;
  492. } else
  493. probed_mod = NULL;
  494. }
  495. p->nmissed = 0;
  496. mutex_lock(&kprobe_mutex);
  497. old_p = get_kprobe(p->addr);
  498. if (old_p) {
  499. ret = register_aggr_kprobe(old_p, p);
  500. if (!ret)
  501. atomic_inc(&kprobe_count);
  502. goto out;
  503. }
  504. ret = arch_prepare_kprobe(p);
  505. if (ret)
  506. goto out;
  507. INIT_HLIST_NODE(&p->hlist);
  508. hlist_add_head_rcu(&p->hlist,
  509. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  510. if (kprobe_enabled) {
  511. if (atomic_add_return(1, &kprobe_count) == \
  512. (ARCH_INACTIVE_KPROBE_COUNT + 1))
  513. register_page_fault_notifier(&kprobe_page_fault_nb);
  514. arch_arm_kprobe(p);
  515. }
  516. out:
  517. mutex_unlock(&kprobe_mutex);
  518. if (ret && probed_mod)
  519. module_put(probed_mod);
  520. return ret;
  521. }
  522. int __kprobes register_kprobe(struct kprobe *p)
  523. {
  524. return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
  525. }
  526. void __kprobes unregister_kprobe(struct kprobe *p)
  527. {
  528. struct module *mod;
  529. struct kprobe *old_p, *list_p;
  530. int cleanup_p;
  531. mutex_lock(&kprobe_mutex);
  532. old_p = get_kprobe(p->addr);
  533. if (unlikely(!old_p)) {
  534. mutex_unlock(&kprobe_mutex);
  535. return;
  536. }
  537. if (p != old_p) {
  538. list_for_each_entry_rcu(list_p, &old_p->list, list)
  539. if (list_p == p)
  540. /* kprobe p is a valid probe */
  541. goto valid_p;
  542. mutex_unlock(&kprobe_mutex);
  543. return;
  544. }
  545. valid_p:
  546. if (old_p == p ||
  547. (old_p->pre_handler == aggr_pre_handler &&
  548. p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
  549. /*
  550. * Only probe on the hash list. Disarm only if kprobes are
  551. * enabled - otherwise, the breakpoint would already have
  552. * been removed. We save on flushing icache.
  553. */
  554. if (kprobe_enabled)
  555. arch_disarm_kprobe(p);
  556. hlist_del_rcu(&old_p->hlist);
  557. cleanup_p = 1;
  558. } else {
  559. list_del_rcu(&p->list);
  560. cleanup_p = 0;
  561. }
  562. mutex_unlock(&kprobe_mutex);
  563. synchronize_sched();
  564. if (p->mod_refcounted) {
  565. mod = module_text_address((unsigned long)p->addr);
  566. if (mod)
  567. module_put(mod);
  568. }
  569. if (cleanup_p) {
  570. if (p != old_p) {
  571. list_del_rcu(&p->list);
  572. kfree(old_p);
  573. }
  574. arch_remove_kprobe(p);
  575. } else {
  576. mutex_lock(&kprobe_mutex);
  577. if (p->break_handler)
  578. old_p->break_handler = NULL;
  579. if (p->post_handler){
  580. list_for_each_entry_rcu(list_p, &old_p->list, list){
  581. if (list_p->post_handler){
  582. cleanup_p = 2;
  583. break;
  584. }
  585. }
  586. if (cleanup_p == 0)
  587. old_p->post_handler = NULL;
  588. }
  589. mutex_unlock(&kprobe_mutex);
  590. }
  591. /* Call unregister_page_fault_notifier()
  592. * if no probes are active
  593. */
  594. mutex_lock(&kprobe_mutex);
  595. if (atomic_add_return(-1, &kprobe_count) == \
  596. ARCH_INACTIVE_KPROBE_COUNT)
  597. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  598. mutex_unlock(&kprobe_mutex);
  599. return;
  600. }
  601. static struct notifier_block kprobe_exceptions_nb = {
  602. .notifier_call = kprobe_exceptions_notify,
  603. .priority = 0x7fffffff /* we need to be notified first */
  604. };
  605. unsigned long __weak arch_deref_entry_point(void *entry)
  606. {
  607. return (unsigned long)entry;
  608. }
  609. int __kprobes register_jprobe(struct jprobe *jp)
  610. {
  611. unsigned long addr = arch_deref_entry_point(jp->entry);
  612. if (!kernel_text_address(addr))
  613. return -EINVAL;
  614. /* Todo: Verify probepoint is a function entry point */
  615. jp->kp.pre_handler = setjmp_pre_handler;
  616. jp->kp.break_handler = longjmp_break_handler;
  617. return __register_kprobe(&jp->kp,
  618. (unsigned long)__builtin_return_address(0));
  619. }
  620. void __kprobes unregister_jprobe(struct jprobe *jp)
  621. {
  622. unregister_kprobe(&jp->kp);
  623. }
  624. #ifdef ARCH_SUPPORTS_KRETPROBES
  625. /*
  626. * This kprobe pre_handler is registered with every kretprobe. When probe
  627. * hits it will set up the return probe.
  628. */
  629. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  630. struct pt_regs *regs)
  631. {
  632. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  633. unsigned long flags = 0;
  634. /*TODO: consider to only swap the RA after the last pre_handler fired */
  635. spin_lock_irqsave(&kretprobe_lock, flags);
  636. if (!hlist_empty(&rp->free_instances)) {
  637. struct kretprobe_instance *ri;
  638. ri = hlist_entry(rp->free_instances.first,
  639. struct kretprobe_instance, uflist);
  640. ri->rp = rp;
  641. ri->task = current;
  642. arch_prepare_kretprobe(ri, regs);
  643. /* XXX(hch): why is there no hlist_move_head? */
  644. hlist_del(&ri->uflist);
  645. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  646. hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
  647. } else
  648. rp->nmissed++;
  649. spin_unlock_irqrestore(&kretprobe_lock, flags);
  650. return 0;
  651. }
  652. int __kprobes register_kretprobe(struct kretprobe *rp)
  653. {
  654. int ret = 0;
  655. struct kretprobe_instance *inst;
  656. int i;
  657. rp->kp.pre_handler = pre_handler_kretprobe;
  658. rp->kp.post_handler = NULL;
  659. rp->kp.fault_handler = NULL;
  660. rp->kp.break_handler = NULL;
  661. /* Pre-allocate memory for max kretprobe instances */
  662. if (rp->maxactive <= 0) {
  663. #ifdef CONFIG_PREEMPT
  664. rp->maxactive = max(10, 2 * NR_CPUS);
  665. #else
  666. rp->maxactive = NR_CPUS;
  667. #endif
  668. }
  669. INIT_HLIST_HEAD(&rp->used_instances);
  670. INIT_HLIST_HEAD(&rp->free_instances);
  671. for (i = 0; i < rp->maxactive; i++) {
  672. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  673. if (inst == NULL) {
  674. free_rp_inst(rp);
  675. return -ENOMEM;
  676. }
  677. INIT_HLIST_NODE(&inst->uflist);
  678. hlist_add_head(&inst->uflist, &rp->free_instances);
  679. }
  680. rp->nmissed = 0;
  681. /* Establish function entry probe point */
  682. if ((ret = __register_kprobe(&rp->kp,
  683. (unsigned long)__builtin_return_address(0))) != 0)
  684. free_rp_inst(rp);
  685. return ret;
  686. }
  687. #else /* ARCH_SUPPORTS_KRETPROBES */
  688. int __kprobes register_kretprobe(struct kretprobe *rp)
  689. {
  690. return -ENOSYS;
  691. }
  692. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  693. struct pt_regs *regs)
  694. {
  695. return 0;
  696. }
  697. #endif /* ARCH_SUPPORTS_KRETPROBES */
  698. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  699. {
  700. unsigned long flags;
  701. struct kretprobe_instance *ri;
  702. struct hlist_node *pos, *next;
  703. unregister_kprobe(&rp->kp);
  704. /* No race here */
  705. spin_lock_irqsave(&kretprobe_lock, flags);
  706. hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
  707. ri->rp = NULL;
  708. hlist_del(&ri->uflist);
  709. }
  710. spin_unlock_irqrestore(&kretprobe_lock, flags);
  711. free_rp_inst(rp);
  712. }
  713. static int __init init_kprobes(void)
  714. {
  715. int i, err = 0;
  716. /* FIXME allocate the probe table, currently defined statically */
  717. /* initialize all list heads */
  718. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  719. INIT_HLIST_HEAD(&kprobe_table[i]);
  720. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  721. }
  722. atomic_set(&kprobe_count, 0);
  723. /* By default, kprobes are enabled */
  724. kprobe_enabled = true;
  725. err = arch_init_kprobes();
  726. if (!err)
  727. err = register_die_notifier(&kprobe_exceptions_nb);
  728. return err;
  729. }
  730. #ifdef CONFIG_DEBUG_FS
  731. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  732. const char *sym, int offset,char *modname)
  733. {
  734. char *kprobe_type;
  735. if (p->pre_handler == pre_handler_kretprobe)
  736. kprobe_type = "r";
  737. else if (p->pre_handler == setjmp_pre_handler)
  738. kprobe_type = "j";
  739. else
  740. kprobe_type = "k";
  741. if (sym)
  742. seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
  743. sym, offset, (modname ? modname : " "));
  744. else
  745. seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
  746. }
  747. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  748. {
  749. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  750. }
  751. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  752. {
  753. (*pos)++;
  754. if (*pos >= KPROBE_TABLE_SIZE)
  755. return NULL;
  756. return pos;
  757. }
  758. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  759. {
  760. /* Nothing to do */
  761. }
  762. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  763. {
  764. struct hlist_head *head;
  765. struct hlist_node *node;
  766. struct kprobe *p, *kp;
  767. const char *sym = NULL;
  768. unsigned int i = *(loff_t *) v;
  769. unsigned long offset = 0;
  770. char *modname, namebuf[128];
  771. head = &kprobe_table[i];
  772. preempt_disable();
  773. hlist_for_each_entry_rcu(p, node, head, hlist) {
  774. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  775. &offset, &modname, namebuf);
  776. if (p->pre_handler == aggr_pre_handler) {
  777. list_for_each_entry_rcu(kp, &p->list, list)
  778. report_probe(pi, kp, sym, offset, modname);
  779. } else
  780. report_probe(pi, p, sym, offset, modname);
  781. }
  782. preempt_enable();
  783. return 0;
  784. }
  785. static struct seq_operations kprobes_seq_ops = {
  786. .start = kprobe_seq_start,
  787. .next = kprobe_seq_next,
  788. .stop = kprobe_seq_stop,
  789. .show = show_kprobe_addr
  790. };
  791. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  792. {
  793. return seq_open(filp, &kprobes_seq_ops);
  794. }
  795. static struct file_operations debugfs_kprobes_operations = {
  796. .open = kprobes_open,
  797. .read = seq_read,
  798. .llseek = seq_lseek,
  799. .release = seq_release,
  800. };
  801. static void __kprobes enable_all_kprobes(void)
  802. {
  803. struct hlist_head *head;
  804. struct hlist_node *node;
  805. struct kprobe *p;
  806. unsigned int i;
  807. mutex_lock(&kprobe_mutex);
  808. /* If kprobes are already enabled, just return */
  809. if (kprobe_enabled)
  810. goto already_enabled;
  811. /*
  812. * Re-register the page fault notifier only if there are any
  813. * active probes at the time of enabling kprobes globally
  814. */
  815. if (atomic_read(&kprobe_count) > ARCH_INACTIVE_KPROBE_COUNT)
  816. register_page_fault_notifier(&kprobe_page_fault_nb);
  817. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  818. head = &kprobe_table[i];
  819. hlist_for_each_entry_rcu(p, node, head, hlist)
  820. arch_arm_kprobe(p);
  821. }
  822. kprobe_enabled = true;
  823. printk(KERN_INFO "Kprobes globally enabled\n");
  824. already_enabled:
  825. mutex_unlock(&kprobe_mutex);
  826. return;
  827. }
  828. static void __kprobes disable_all_kprobes(void)
  829. {
  830. struct hlist_head *head;
  831. struct hlist_node *node;
  832. struct kprobe *p;
  833. unsigned int i;
  834. mutex_lock(&kprobe_mutex);
  835. /* If kprobes are already disabled, just return */
  836. if (!kprobe_enabled)
  837. goto already_disabled;
  838. kprobe_enabled = false;
  839. printk(KERN_INFO "Kprobes globally disabled\n");
  840. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  841. head = &kprobe_table[i];
  842. hlist_for_each_entry_rcu(p, node, head, hlist) {
  843. if (!arch_trampoline_kprobe(p))
  844. arch_disarm_kprobe(p);
  845. }
  846. }
  847. mutex_unlock(&kprobe_mutex);
  848. /* Allow all currently running kprobes to complete */
  849. synchronize_sched();
  850. mutex_lock(&kprobe_mutex);
  851. /* Unconditionally unregister the page_fault notifier */
  852. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  853. already_disabled:
  854. mutex_unlock(&kprobe_mutex);
  855. return;
  856. }
  857. /*
  858. * XXX: The debugfs bool file interface doesn't allow for callbacks
  859. * when the bool state is switched. We can reuse that facility when
  860. * available
  861. */
  862. static ssize_t read_enabled_file_bool(struct file *file,
  863. char __user *user_buf, size_t count, loff_t *ppos)
  864. {
  865. char buf[3];
  866. if (kprobe_enabled)
  867. buf[0] = '1';
  868. else
  869. buf[0] = '0';
  870. buf[1] = '\n';
  871. buf[2] = 0x00;
  872. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  873. }
  874. static ssize_t write_enabled_file_bool(struct file *file,
  875. const char __user *user_buf, size_t count, loff_t *ppos)
  876. {
  877. char buf[32];
  878. int buf_size;
  879. buf_size = min(count, (sizeof(buf)-1));
  880. if (copy_from_user(buf, user_buf, buf_size))
  881. return -EFAULT;
  882. switch (buf[0]) {
  883. case 'y':
  884. case 'Y':
  885. case '1':
  886. enable_all_kprobes();
  887. break;
  888. case 'n':
  889. case 'N':
  890. case '0':
  891. disable_all_kprobes();
  892. break;
  893. }
  894. return count;
  895. }
  896. static struct file_operations fops_kp = {
  897. .read = read_enabled_file_bool,
  898. .write = write_enabled_file_bool,
  899. };
  900. static int __kprobes debugfs_kprobe_init(void)
  901. {
  902. struct dentry *dir, *file;
  903. unsigned int value = 1;
  904. dir = debugfs_create_dir("kprobes", NULL);
  905. if (!dir)
  906. return -ENOMEM;
  907. file = debugfs_create_file("list", 0444, dir, NULL,
  908. &debugfs_kprobes_operations);
  909. if (!file) {
  910. debugfs_remove(dir);
  911. return -ENOMEM;
  912. }
  913. file = debugfs_create_file("enabled", 0600, dir,
  914. &value, &fops_kp);
  915. if (!file) {
  916. debugfs_remove(dir);
  917. return -ENOMEM;
  918. }
  919. return 0;
  920. }
  921. late_initcall(debugfs_kprobe_init);
  922. #endif /* CONFIG_DEBUG_FS */
  923. module_init(init_kprobes);
  924. EXPORT_SYMBOL_GPL(register_kprobe);
  925. EXPORT_SYMBOL_GPL(unregister_kprobe);
  926. EXPORT_SYMBOL_GPL(register_jprobe);
  927. EXPORT_SYMBOL_GPL(unregister_jprobe);
  928. #ifdef CONFIG_KPROBES
  929. EXPORT_SYMBOL_GPL(jprobe_return);
  930. #endif
  931. #ifdef CONFIG_KPROBES
  932. EXPORT_SYMBOL_GPL(register_kretprobe);
  933. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  934. #endif