kprobes.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 int kprobes_initialized;
  61. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  62. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  63. /* NOTE: change this value only with kprobe_mutex held */
  64. static bool kprobe_enabled;
  65. static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  66. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  67. static struct {
  68. spinlock_t lock ____cacheline_aligned_in_smp;
  69. } kretprobe_table_locks[KPROBE_TABLE_SIZE];
  70. static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  71. {
  72. return &(kretprobe_table_locks[hash].lock);
  73. }
  74. /*
  75. * Normally, functions that we'd want to prohibit kprobes in, are marked
  76. * __kprobes. But, there are cases where such functions already belong to
  77. * a different section (__sched for preempt_schedule)
  78. *
  79. * For such cases, we now have a blacklist
  80. */
  81. static struct kprobe_blackpoint kprobe_blacklist[] = {
  82. {"preempt_schedule",},
  83. {NULL} /* Terminator */
  84. };
  85. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  86. /*
  87. * kprobe->ainsn.insn points to the copy of the instruction to be
  88. * single-stepped. x86_64, POWER4 and above have no-exec support and
  89. * stepping on the instruction on a vmalloced/kmalloced/data page
  90. * is a recipe for disaster
  91. */
  92. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  93. struct kprobe_insn_page {
  94. struct hlist_node hlist;
  95. kprobe_opcode_t *insns; /* Page of instruction slots */
  96. char slot_used[INSNS_PER_PAGE];
  97. int nused;
  98. int ngarbage;
  99. };
  100. enum kprobe_slot_state {
  101. SLOT_CLEAN = 0,
  102. SLOT_DIRTY = 1,
  103. SLOT_USED = 2,
  104. };
  105. static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
  106. static struct hlist_head kprobe_insn_pages;
  107. static int kprobe_garbage_slots;
  108. static int collect_garbage_slots(void);
  109. static int __kprobes check_safety(void)
  110. {
  111. int ret = 0;
  112. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  113. ret = freeze_processes();
  114. if (ret == 0) {
  115. struct task_struct *p, *q;
  116. do_each_thread(p, q) {
  117. if (p != current && p->state == TASK_RUNNING &&
  118. p->pid != 0) {
  119. printk("Check failed: %s is running\n",p->comm);
  120. ret = -1;
  121. goto loop_end;
  122. }
  123. } while_each_thread(p, q);
  124. }
  125. loop_end:
  126. thaw_processes();
  127. #else
  128. synchronize_sched();
  129. #endif
  130. return ret;
  131. }
  132. /**
  133. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  134. * We allocate an executable page if there's no room on existing ones.
  135. */
  136. static kprobe_opcode_t __kprobes *__get_insn_slot(void)
  137. {
  138. struct kprobe_insn_page *kip;
  139. struct hlist_node *pos;
  140. retry:
  141. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  142. if (kip->nused < INSNS_PER_PAGE) {
  143. int i;
  144. for (i = 0; i < INSNS_PER_PAGE; i++) {
  145. if (kip->slot_used[i] == SLOT_CLEAN) {
  146. kip->slot_used[i] = SLOT_USED;
  147. kip->nused++;
  148. return kip->insns + (i * MAX_INSN_SIZE);
  149. }
  150. }
  151. /* Surprise! No unused slots. Fix kip->nused. */
  152. kip->nused = INSNS_PER_PAGE;
  153. }
  154. }
  155. /* If there are any garbage slots, collect it and try again. */
  156. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  157. goto retry;
  158. }
  159. /* All out of space. Need to allocate a new page. Use slot 0. */
  160. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  161. if (!kip)
  162. return NULL;
  163. /*
  164. * Use module_alloc so this page is within +/- 2GB of where the
  165. * kernel image and loaded module images reside. This is required
  166. * so x86_64 can correctly handle the %rip-relative fixups.
  167. */
  168. kip->insns = module_alloc(PAGE_SIZE);
  169. if (!kip->insns) {
  170. kfree(kip);
  171. return NULL;
  172. }
  173. INIT_HLIST_NODE(&kip->hlist);
  174. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  175. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  176. kip->slot_used[0] = SLOT_USED;
  177. kip->nused = 1;
  178. kip->ngarbage = 0;
  179. return kip->insns;
  180. }
  181. kprobe_opcode_t __kprobes *get_insn_slot(void)
  182. {
  183. kprobe_opcode_t *ret;
  184. mutex_lock(&kprobe_insn_mutex);
  185. ret = __get_insn_slot();
  186. mutex_unlock(&kprobe_insn_mutex);
  187. return ret;
  188. }
  189. /* Return 1 if all garbages are collected, otherwise 0. */
  190. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  191. {
  192. kip->slot_used[idx] = SLOT_CLEAN;
  193. kip->nused--;
  194. if (kip->nused == 0) {
  195. /*
  196. * Page is no longer in use. Free it unless
  197. * it's the last one. We keep the last one
  198. * so as not to have to set it up again the
  199. * next time somebody inserts a probe.
  200. */
  201. hlist_del(&kip->hlist);
  202. if (hlist_empty(&kprobe_insn_pages)) {
  203. INIT_HLIST_NODE(&kip->hlist);
  204. hlist_add_head(&kip->hlist,
  205. &kprobe_insn_pages);
  206. } else {
  207. module_free(NULL, kip->insns);
  208. kfree(kip);
  209. }
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int __kprobes collect_garbage_slots(void)
  215. {
  216. struct kprobe_insn_page *kip;
  217. struct hlist_node *pos, *next;
  218. int safety;
  219. /* Ensure no-one is preepmted on the garbages */
  220. mutex_unlock(&kprobe_insn_mutex);
  221. safety = check_safety();
  222. mutex_lock(&kprobe_insn_mutex);
  223. if (safety != 0)
  224. return -EAGAIN;
  225. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  226. int i;
  227. if (kip->ngarbage == 0)
  228. continue;
  229. kip->ngarbage = 0; /* we will collect all garbages */
  230. for (i = 0; i < INSNS_PER_PAGE; i++) {
  231. if (kip->slot_used[i] == SLOT_DIRTY &&
  232. collect_one_slot(kip, i))
  233. break;
  234. }
  235. }
  236. kprobe_garbage_slots = 0;
  237. return 0;
  238. }
  239. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  240. {
  241. struct kprobe_insn_page *kip;
  242. struct hlist_node *pos;
  243. mutex_lock(&kprobe_insn_mutex);
  244. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  245. if (kip->insns <= slot &&
  246. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  247. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  248. if (dirty) {
  249. kip->slot_used[i] = SLOT_DIRTY;
  250. kip->ngarbage++;
  251. } else {
  252. collect_one_slot(kip, i);
  253. }
  254. break;
  255. }
  256. }
  257. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  258. collect_garbage_slots();
  259. mutex_unlock(&kprobe_insn_mutex);
  260. }
  261. #endif
  262. /* We have preemption disabled.. so it is safe to use __ versions */
  263. static inline void set_kprobe_instance(struct kprobe *kp)
  264. {
  265. __get_cpu_var(kprobe_instance) = kp;
  266. }
  267. static inline void reset_kprobe_instance(void)
  268. {
  269. __get_cpu_var(kprobe_instance) = NULL;
  270. }
  271. /*
  272. * This routine is called either:
  273. * - under the kprobe_mutex - during kprobe_[un]register()
  274. * OR
  275. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  276. */
  277. struct kprobe __kprobes *get_kprobe(void *addr)
  278. {
  279. struct hlist_head *head;
  280. struct hlist_node *node;
  281. struct kprobe *p;
  282. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  283. hlist_for_each_entry_rcu(p, node, head, hlist) {
  284. if (p->addr == addr)
  285. return p;
  286. }
  287. return NULL;
  288. }
  289. /*
  290. * Aggregate handlers for multiple kprobes support - these handlers
  291. * take care of invoking the individual kprobe handlers on p->list
  292. */
  293. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  294. {
  295. struct kprobe *kp;
  296. list_for_each_entry_rcu(kp, &p->list, list) {
  297. if (kp->pre_handler && !kprobe_gone(kp)) {
  298. set_kprobe_instance(kp);
  299. if (kp->pre_handler(kp, regs))
  300. return 1;
  301. }
  302. reset_kprobe_instance();
  303. }
  304. return 0;
  305. }
  306. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  307. unsigned long flags)
  308. {
  309. struct kprobe *kp;
  310. list_for_each_entry_rcu(kp, &p->list, list) {
  311. if (kp->post_handler && !kprobe_gone(kp)) {
  312. set_kprobe_instance(kp);
  313. kp->post_handler(kp, regs, flags);
  314. reset_kprobe_instance();
  315. }
  316. }
  317. }
  318. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  319. int trapnr)
  320. {
  321. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  322. /*
  323. * if we faulted "during" the execution of a user specified
  324. * probe handler, invoke just that probe's fault handler
  325. */
  326. if (cur && cur->fault_handler) {
  327. if (cur->fault_handler(cur, regs, trapnr))
  328. return 1;
  329. }
  330. return 0;
  331. }
  332. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  333. {
  334. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  335. int ret = 0;
  336. if (cur && cur->break_handler) {
  337. if (cur->break_handler(cur, regs))
  338. ret = 1;
  339. }
  340. reset_kprobe_instance();
  341. return ret;
  342. }
  343. /* Walks the list and increments nmissed count for multiprobe case */
  344. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  345. {
  346. struct kprobe *kp;
  347. if (p->pre_handler != aggr_pre_handler) {
  348. p->nmissed++;
  349. } else {
  350. list_for_each_entry_rcu(kp, &p->list, list)
  351. kp->nmissed++;
  352. }
  353. return;
  354. }
  355. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  356. struct hlist_head *head)
  357. {
  358. struct kretprobe *rp = ri->rp;
  359. /* remove rp inst off the rprobe_inst_table */
  360. hlist_del(&ri->hlist);
  361. INIT_HLIST_NODE(&ri->hlist);
  362. if (likely(rp)) {
  363. spin_lock(&rp->lock);
  364. hlist_add_head(&ri->hlist, &rp->free_instances);
  365. spin_unlock(&rp->lock);
  366. } else
  367. /* Unregistering */
  368. hlist_add_head(&ri->hlist, head);
  369. }
  370. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  371. struct hlist_head **head, unsigned long *flags)
  372. {
  373. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  374. spinlock_t *hlist_lock;
  375. *head = &kretprobe_inst_table[hash];
  376. hlist_lock = kretprobe_table_lock_ptr(hash);
  377. spin_lock_irqsave(hlist_lock, *flags);
  378. }
  379. static void __kprobes kretprobe_table_lock(unsigned long hash,
  380. unsigned long *flags)
  381. {
  382. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  383. spin_lock_irqsave(hlist_lock, *flags);
  384. }
  385. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  386. unsigned long *flags)
  387. {
  388. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  389. spinlock_t *hlist_lock;
  390. hlist_lock = kretprobe_table_lock_ptr(hash);
  391. spin_unlock_irqrestore(hlist_lock, *flags);
  392. }
  393. void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
  394. {
  395. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  396. spin_unlock_irqrestore(hlist_lock, *flags);
  397. }
  398. /*
  399. * This function is called from finish_task_switch when task tk becomes dead,
  400. * so that we can recycle any function-return probe instances associated
  401. * with this task. These left over instances represent probed functions
  402. * that have been called but will never return.
  403. */
  404. void __kprobes kprobe_flush_task(struct task_struct *tk)
  405. {
  406. struct kretprobe_instance *ri;
  407. struct hlist_head *head, empty_rp;
  408. struct hlist_node *node, *tmp;
  409. unsigned long hash, flags = 0;
  410. if (unlikely(!kprobes_initialized))
  411. /* Early boot. kretprobe_table_locks not yet initialized. */
  412. return;
  413. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  414. head = &kretprobe_inst_table[hash];
  415. kretprobe_table_lock(hash, &flags);
  416. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  417. if (ri->task == tk)
  418. recycle_rp_inst(ri, &empty_rp);
  419. }
  420. kretprobe_table_unlock(hash, &flags);
  421. INIT_HLIST_HEAD(&empty_rp);
  422. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  423. hlist_del(&ri->hlist);
  424. kfree(ri);
  425. }
  426. }
  427. static inline void free_rp_inst(struct kretprobe *rp)
  428. {
  429. struct kretprobe_instance *ri;
  430. struct hlist_node *pos, *next;
  431. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  432. hlist_del(&ri->hlist);
  433. kfree(ri);
  434. }
  435. }
  436. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  437. {
  438. unsigned long flags, hash;
  439. struct kretprobe_instance *ri;
  440. struct hlist_node *pos, *next;
  441. struct hlist_head *head;
  442. /* No race here */
  443. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  444. kretprobe_table_lock(hash, &flags);
  445. head = &kretprobe_inst_table[hash];
  446. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  447. if (ri->rp == rp)
  448. ri->rp = NULL;
  449. }
  450. kretprobe_table_unlock(hash, &flags);
  451. }
  452. free_rp_inst(rp);
  453. }
  454. /*
  455. * Keep all fields in the kprobe consistent
  456. */
  457. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  458. {
  459. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  460. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  461. }
  462. /*
  463. * Add the new probe to old_p->list. Fail if this is the
  464. * second jprobe at the address - two jprobes can't coexist
  465. */
  466. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  467. {
  468. if (p->break_handler) {
  469. if (old_p->break_handler)
  470. return -EEXIST;
  471. list_add_tail_rcu(&p->list, &old_p->list);
  472. old_p->break_handler = aggr_break_handler;
  473. } else
  474. list_add_rcu(&p->list, &old_p->list);
  475. if (p->post_handler && !old_p->post_handler)
  476. old_p->post_handler = aggr_post_handler;
  477. return 0;
  478. }
  479. /*
  480. * Fill in the required fields of the "manager kprobe". Replace the
  481. * earlier kprobe in the hlist with the manager kprobe
  482. */
  483. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  484. {
  485. copy_kprobe(p, ap);
  486. flush_insn_slot(ap);
  487. ap->addr = p->addr;
  488. ap->pre_handler = aggr_pre_handler;
  489. ap->fault_handler = aggr_fault_handler;
  490. /* We don't care the kprobe which has gone. */
  491. if (p->post_handler && !kprobe_gone(p))
  492. ap->post_handler = aggr_post_handler;
  493. if (p->break_handler && !kprobe_gone(p))
  494. ap->break_handler = aggr_break_handler;
  495. INIT_LIST_HEAD(&ap->list);
  496. list_add_rcu(&p->list, &ap->list);
  497. hlist_replace_rcu(&p->hlist, &ap->hlist);
  498. }
  499. /*
  500. * This is the second or subsequent kprobe at the address - handle
  501. * the intricacies
  502. */
  503. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  504. struct kprobe *p)
  505. {
  506. int ret = 0;
  507. struct kprobe *ap;
  508. if (kprobe_gone(old_p)) {
  509. /*
  510. * Attempting to insert new probe at the same location that
  511. * had a probe in the module vaddr area which already
  512. * freed. So, the instruction slot has already been
  513. * released. We need a new slot for the new probe.
  514. */
  515. ret = arch_prepare_kprobe(old_p);
  516. if (ret)
  517. return ret;
  518. }
  519. if (old_p->pre_handler == aggr_pre_handler) {
  520. copy_kprobe(old_p, p);
  521. ret = add_new_kprobe(old_p, p);
  522. ap = old_p;
  523. } else {
  524. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  525. if (!ap) {
  526. if (kprobe_gone(old_p))
  527. arch_remove_kprobe(old_p);
  528. return -ENOMEM;
  529. }
  530. add_aggr_kprobe(ap, old_p);
  531. copy_kprobe(ap, p);
  532. ret = add_new_kprobe(ap, p);
  533. }
  534. if (kprobe_gone(old_p)) {
  535. /*
  536. * If the old_p has gone, its breakpoint has been disarmed.
  537. * We have to arm it again after preparing real kprobes.
  538. */
  539. ap->flags &= ~KPROBE_FLAG_GONE;
  540. if (kprobe_enabled)
  541. arch_arm_kprobe(ap);
  542. }
  543. return ret;
  544. }
  545. static int __kprobes in_kprobes_functions(unsigned long addr)
  546. {
  547. struct kprobe_blackpoint *kb;
  548. if (addr >= (unsigned long)__kprobes_text_start &&
  549. addr < (unsigned long)__kprobes_text_end)
  550. return -EINVAL;
  551. /*
  552. * If there exists a kprobe_blacklist, verify and
  553. * fail any probe registration in the prohibited area
  554. */
  555. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  556. if (kb->start_addr) {
  557. if (addr >= kb->start_addr &&
  558. addr < (kb->start_addr + kb->range))
  559. return -EINVAL;
  560. }
  561. }
  562. return 0;
  563. }
  564. /*
  565. * If we have a symbol_name argument, look it up and add the offset field
  566. * to it. This way, we can specify a relative address to a symbol.
  567. */
  568. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  569. {
  570. kprobe_opcode_t *addr = p->addr;
  571. if (p->symbol_name) {
  572. if (addr)
  573. return NULL;
  574. kprobe_lookup_name(p->symbol_name, addr);
  575. }
  576. if (!addr)
  577. return NULL;
  578. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  579. }
  580. static int __kprobes __register_kprobe(struct kprobe *p,
  581. unsigned long called_from)
  582. {
  583. int ret = 0;
  584. struct kprobe *old_p;
  585. struct module *probed_mod;
  586. kprobe_opcode_t *addr;
  587. addr = kprobe_addr(p);
  588. if (!addr)
  589. return -EINVAL;
  590. p->addr = addr;
  591. preempt_disable();
  592. if (!__kernel_text_address((unsigned long) p->addr) ||
  593. in_kprobes_functions((unsigned long) p->addr)) {
  594. preempt_enable();
  595. return -EINVAL;
  596. }
  597. p->flags = 0;
  598. /*
  599. * Check if are we probing a module.
  600. */
  601. probed_mod = __module_text_address((unsigned long) p->addr);
  602. if (probed_mod) {
  603. struct module *calling_mod;
  604. calling_mod = __module_text_address(called_from);
  605. /*
  606. * We must hold a refcount of the probed module while updating
  607. * its code to prohibit unexpected unloading.
  608. */
  609. if (calling_mod != probed_mod) {
  610. if (unlikely(!try_module_get(probed_mod))) {
  611. preempt_enable();
  612. return -EINVAL;
  613. }
  614. } else
  615. probed_mod = NULL;
  616. }
  617. preempt_enable();
  618. p->nmissed = 0;
  619. INIT_LIST_HEAD(&p->list);
  620. mutex_lock(&kprobe_mutex);
  621. old_p = get_kprobe(p->addr);
  622. if (old_p) {
  623. ret = register_aggr_kprobe(old_p, p);
  624. goto out;
  625. }
  626. ret = arch_prepare_kprobe(p);
  627. if (ret)
  628. goto out;
  629. INIT_HLIST_NODE(&p->hlist);
  630. hlist_add_head_rcu(&p->hlist,
  631. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  632. if (kprobe_enabled)
  633. arch_arm_kprobe(p);
  634. out:
  635. mutex_unlock(&kprobe_mutex);
  636. if (probed_mod)
  637. module_put(probed_mod);
  638. return ret;
  639. }
  640. /*
  641. * Unregister a kprobe without a scheduler synchronization.
  642. */
  643. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  644. {
  645. struct kprobe *old_p, *list_p;
  646. old_p = get_kprobe(p->addr);
  647. if (unlikely(!old_p))
  648. return -EINVAL;
  649. if (p != old_p) {
  650. list_for_each_entry_rcu(list_p, &old_p->list, list)
  651. if (list_p == p)
  652. /* kprobe p is a valid probe */
  653. goto valid_p;
  654. return -EINVAL;
  655. }
  656. valid_p:
  657. if (old_p == p ||
  658. (old_p->pre_handler == aggr_pre_handler &&
  659. list_is_singular(&old_p->list))) {
  660. /*
  661. * Only probe on the hash list. Disarm only if kprobes are
  662. * enabled and not gone - otherwise, the breakpoint would
  663. * already have been removed. We save on flushing icache.
  664. */
  665. if (kprobe_enabled && !kprobe_gone(old_p))
  666. arch_disarm_kprobe(p);
  667. hlist_del_rcu(&old_p->hlist);
  668. } else {
  669. if (p->break_handler && !kprobe_gone(p))
  670. old_p->break_handler = NULL;
  671. if (p->post_handler && !kprobe_gone(p)) {
  672. list_for_each_entry_rcu(list_p, &old_p->list, list) {
  673. if ((list_p != p) && (list_p->post_handler))
  674. goto noclean;
  675. }
  676. old_p->post_handler = NULL;
  677. }
  678. noclean:
  679. list_del_rcu(&p->list);
  680. }
  681. return 0;
  682. }
  683. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  684. {
  685. struct kprobe *old_p;
  686. if (list_empty(&p->list))
  687. arch_remove_kprobe(p);
  688. else if (list_is_singular(&p->list)) {
  689. /* "p" is the last child of an aggr_kprobe */
  690. old_p = list_entry(p->list.next, struct kprobe, list);
  691. list_del(&p->list);
  692. arch_remove_kprobe(old_p);
  693. kfree(old_p);
  694. }
  695. }
  696. static int __kprobes __register_kprobes(struct kprobe **kps, int num,
  697. unsigned long called_from)
  698. {
  699. int i, ret = 0;
  700. if (num <= 0)
  701. return -EINVAL;
  702. for (i = 0; i < num; i++) {
  703. ret = __register_kprobe(kps[i], called_from);
  704. if (ret < 0) {
  705. if (i > 0)
  706. unregister_kprobes(kps, i);
  707. break;
  708. }
  709. }
  710. return ret;
  711. }
  712. /*
  713. * Registration and unregistration functions for kprobe.
  714. */
  715. int __kprobes register_kprobe(struct kprobe *p)
  716. {
  717. return __register_kprobes(&p, 1,
  718. (unsigned long)__builtin_return_address(0));
  719. }
  720. void __kprobes unregister_kprobe(struct kprobe *p)
  721. {
  722. unregister_kprobes(&p, 1);
  723. }
  724. int __kprobes register_kprobes(struct kprobe **kps, int num)
  725. {
  726. return __register_kprobes(kps, num,
  727. (unsigned long)__builtin_return_address(0));
  728. }
  729. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  730. {
  731. int i;
  732. if (num <= 0)
  733. return;
  734. mutex_lock(&kprobe_mutex);
  735. for (i = 0; i < num; i++)
  736. if (__unregister_kprobe_top(kps[i]) < 0)
  737. kps[i]->addr = NULL;
  738. mutex_unlock(&kprobe_mutex);
  739. synchronize_sched();
  740. for (i = 0; i < num; i++)
  741. if (kps[i]->addr)
  742. __unregister_kprobe_bottom(kps[i]);
  743. }
  744. static struct notifier_block kprobe_exceptions_nb = {
  745. .notifier_call = kprobe_exceptions_notify,
  746. .priority = 0x7fffffff /* we need to be notified first */
  747. };
  748. unsigned long __weak arch_deref_entry_point(void *entry)
  749. {
  750. return (unsigned long)entry;
  751. }
  752. static int __kprobes __register_jprobes(struct jprobe **jps, int num,
  753. unsigned long called_from)
  754. {
  755. struct jprobe *jp;
  756. int ret = 0, i;
  757. if (num <= 0)
  758. return -EINVAL;
  759. for (i = 0; i < num; i++) {
  760. unsigned long addr;
  761. jp = jps[i];
  762. addr = arch_deref_entry_point(jp->entry);
  763. if (!kernel_text_address(addr))
  764. ret = -EINVAL;
  765. else {
  766. /* Todo: Verify probepoint is a function entry point */
  767. jp->kp.pre_handler = setjmp_pre_handler;
  768. jp->kp.break_handler = longjmp_break_handler;
  769. ret = __register_kprobe(&jp->kp, called_from);
  770. }
  771. if (ret < 0) {
  772. if (i > 0)
  773. unregister_jprobes(jps, i);
  774. break;
  775. }
  776. }
  777. return ret;
  778. }
  779. int __kprobes register_jprobe(struct jprobe *jp)
  780. {
  781. return __register_jprobes(&jp, 1,
  782. (unsigned long)__builtin_return_address(0));
  783. }
  784. void __kprobes unregister_jprobe(struct jprobe *jp)
  785. {
  786. unregister_jprobes(&jp, 1);
  787. }
  788. int __kprobes register_jprobes(struct jprobe **jps, int num)
  789. {
  790. return __register_jprobes(jps, num,
  791. (unsigned long)__builtin_return_address(0));
  792. }
  793. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  794. {
  795. int i;
  796. if (num <= 0)
  797. return;
  798. mutex_lock(&kprobe_mutex);
  799. for (i = 0; i < num; i++)
  800. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  801. jps[i]->kp.addr = NULL;
  802. mutex_unlock(&kprobe_mutex);
  803. synchronize_sched();
  804. for (i = 0; i < num; i++) {
  805. if (jps[i]->kp.addr)
  806. __unregister_kprobe_bottom(&jps[i]->kp);
  807. }
  808. }
  809. #ifdef CONFIG_KRETPROBES
  810. /*
  811. * This kprobe pre_handler is registered with every kretprobe. When probe
  812. * hits it will set up the return probe.
  813. */
  814. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  815. struct pt_regs *regs)
  816. {
  817. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  818. unsigned long hash, flags = 0;
  819. struct kretprobe_instance *ri;
  820. /*TODO: consider to only swap the RA after the last pre_handler fired */
  821. hash = hash_ptr(current, KPROBE_HASH_BITS);
  822. spin_lock_irqsave(&rp->lock, flags);
  823. if (!hlist_empty(&rp->free_instances)) {
  824. ri = hlist_entry(rp->free_instances.first,
  825. struct kretprobe_instance, hlist);
  826. hlist_del(&ri->hlist);
  827. spin_unlock_irqrestore(&rp->lock, flags);
  828. ri->rp = rp;
  829. ri->task = current;
  830. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  831. spin_unlock_irqrestore(&rp->lock, flags);
  832. return 0;
  833. }
  834. arch_prepare_kretprobe(ri, regs);
  835. /* XXX(hch): why is there no hlist_move_head? */
  836. INIT_HLIST_NODE(&ri->hlist);
  837. kretprobe_table_lock(hash, &flags);
  838. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  839. kretprobe_table_unlock(hash, &flags);
  840. } else {
  841. rp->nmissed++;
  842. spin_unlock_irqrestore(&rp->lock, flags);
  843. }
  844. return 0;
  845. }
  846. static int __kprobes __register_kretprobe(struct kretprobe *rp,
  847. unsigned long called_from)
  848. {
  849. int ret = 0;
  850. struct kretprobe_instance *inst;
  851. int i;
  852. void *addr;
  853. if (kretprobe_blacklist_size) {
  854. addr = kprobe_addr(&rp->kp);
  855. if (!addr)
  856. return -EINVAL;
  857. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  858. if (kretprobe_blacklist[i].addr == addr)
  859. return -EINVAL;
  860. }
  861. }
  862. rp->kp.pre_handler = pre_handler_kretprobe;
  863. rp->kp.post_handler = NULL;
  864. rp->kp.fault_handler = NULL;
  865. rp->kp.break_handler = NULL;
  866. /* Pre-allocate memory for max kretprobe instances */
  867. if (rp->maxactive <= 0) {
  868. #ifdef CONFIG_PREEMPT
  869. rp->maxactive = max(10, 2 * NR_CPUS);
  870. #else
  871. rp->maxactive = NR_CPUS;
  872. #endif
  873. }
  874. spin_lock_init(&rp->lock);
  875. INIT_HLIST_HEAD(&rp->free_instances);
  876. for (i = 0; i < rp->maxactive; i++) {
  877. inst = kmalloc(sizeof(struct kretprobe_instance) +
  878. rp->data_size, GFP_KERNEL);
  879. if (inst == NULL) {
  880. free_rp_inst(rp);
  881. return -ENOMEM;
  882. }
  883. INIT_HLIST_NODE(&inst->hlist);
  884. hlist_add_head(&inst->hlist, &rp->free_instances);
  885. }
  886. rp->nmissed = 0;
  887. /* Establish function entry probe point */
  888. ret = __register_kprobe(&rp->kp, called_from);
  889. if (ret != 0)
  890. free_rp_inst(rp);
  891. return ret;
  892. }
  893. static int __kprobes __register_kretprobes(struct kretprobe **rps, int num,
  894. unsigned long called_from)
  895. {
  896. int ret = 0, i;
  897. if (num <= 0)
  898. return -EINVAL;
  899. for (i = 0; i < num; i++) {
  900. ret = __register_kretprobe(rps[i], called_from);
  901. if (ret < 0) {
  902. if (i > 0)
  903. unregister_kretprobes(rps, i);
  904. break;
  905. }
  906. }
  907. return ret;
  908. }
  909. int __kprobes register_kretprobe(struct kretprobe *rp)
  910. {
  911. return __register_kretprobes(&rp, 1,
  912. (unsigned long)__builtin_return_address(0));
  913. }
  914. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  915. {
  916. unregister_kretprobes(&rp, 1);
  917. }
  918. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  919. {
  920. return __register_kretprobes(rps, num,
  921. (unsigned long)__builtin_return_address(0));
  922. }
  923. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  924. {
  925. int i;
  926. if (num <= 0)
  927. return;
  928. mutex_lock(&kprobe_mutex);
  929. for (i = 0; i < num; i++)
  930. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  931. rps[i]->kp.addr = NULL;
  932. mutex_unlock(&kprobe_mutex);
  933. synchronize_sched();
  934. for (i = 0; i < num; i++) {
  935. if (rps[i]->kp.addr) {
  936. __unregister_kprobe_bottom(&rps[i]->kp);
  937. cleanup_rp_inst(rps[i]);
  938. }
  939. }
  940. }
  941. #else /* CONFIG_KRETPROBES */
  942. int __kprobes register_kretprobe(struct kretprobe *rp)
  943. {
  944. return -ENOSYS;
  945. }
  946. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  947. {
  948. return -ENOSYS;
  949. }
  950. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  951. {
  952. }
  953. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  954. {
  955. }
  956. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  957. struct pt_regs *regs)
  958. {
  959. return 0;
  960. }
  961. #endif /* CONFIG_KRETPROBES */
  962. /* Set the kprobe gone and remove its instruction buffer. */
  963. static void __kprobes kill_kprobe(struct kprobe *p)
  964. {
  965. struct kprobe *kp;
  966. p->flags |= KPROBE_FLAG_GONE;
  967. if (p->pre_handler == aggr_pre_handler) {
  968. /*
  969. * If this is an aggr_kprobe, we have to list all the
  970. * chained probes and mark them GONE.
  971. */
  972. list_for_each_entry_rcu(kp, &p->list, list)
  973. kp->flags |= KPROBE_FLAG_GONE;
  974. p->post_handler = NULL;
  975. p->break_handler = NULL;
  976. }
  977. /*
  978. * Here, we can remove insn_slot safely, because no thread calls
  979. * the original probed function (which will be freed soon) any more.
  980. */
  981. arch_remove_kprobe(p);
  982. }
  983. /* Module notifier call back, checking kprobes on the module */
  984. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  985. unsigned long val, void *data)
  986. {
  987. struct module *mod = data;
  988. struct hlist_head *head;
  989. struct hlist_node *node;
  990. struct kprobe *p;
  991. unsigned int i;
  992. if (val != MODULE_STATE_GOING)
  993. return NOTIFY_DONE;
  994. /*
  995. * module .text section will be freed. We need to
  996. * disable kprobes which have been inserted in the section.
  997. */
  998. mutex_lock(&kprobe_mutex);
  999. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1000. head = &kprobe_table[i];
  1001. hlist_for_each_entry_rcu(p, node, head, hlist)
  1002. if (within_module_core((unsigned long)p->addr, mod)) {
  1003. /*
  1004. * The vaddr this probe is installed will soon
  1005. * be vfreed buy not synced to disk. Hence,
  1006. * disarming the breakpoint isn't needed.
  1007. */
  1008. kill_kprobe(p);
  1009. }
  1010. }
  1011. mutex_unlock(&kprobe_mutex);
  1012. return NOTIFY_DONE;
  1013. }
  1014. static struct notifier_block kprobe_module_nb = {
  1015. .notifier_call = kprobes_module_callback,
  1016. .priority = 0
  1017. };
  1018. static int __init init_kprobes(void)
  1019. {
  1020. int i, err = 0;
  1021. unsigned long offset = 0, size = 0;
  1022. char *modname, namebuf[128];
  1023. const char *symbol_name;
  1024. void *addr;
  1025. struct kprobe_blackpoint *kb;
  1026. /* FIXME allocate the probe table, currently defined statically */
  1027. /* initialize all list heads */
  1028. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1029. INIT_HLIST_HEAD(&kprobe_table[i]);
  1030. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1031. spin_lock_init(&(kretprobe_table_locks[i].lock));
  1032. }
  1033. /*
  1034. * Lookup and populate the kprobe_blacklist.
  1035. *
  1036. * Unlike the kretprobe blacklist, we'll need to determine
  1037. * the range of addresses that belong to the said functions,
  1038. * since a kprobe need not necessarily be at the beginning
  1039. * of a function.
  1040. */
  1041. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1042. kprobe_lookup_name(kb->name, addr);
  1043. if (!addr)
  1044. continue;
  1045. kb->start_addr = (unsigned long)addr;
  1046. symbol_name = kallsyms_lookup(kb->start_addr,
  1047. &size, &offset, &modname, namebuf);
  1048. if (!symbol_name)
  1049. kb->range = 0;
  1050. else
  1051. kb->range = size;
  1052. }
  1053. if (kretprobe_blacklist_size) {
  1054. /* lookup the function address from its name */
  1055. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1056. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1057. kretprobe_blacklist[i].addr);
  1058. if (!kretprobe_blacklist[i].addr)
  1059. printk("kretprobe: lookup failed: %s\n",
  1060. kretprobe_blacklist[i].name);
  1061. }
  1062. }
  1063. /* By default, kprobes are enabled */
  1064. kprobe_enabled = true;
  1065. err = arch_init_kprobes();
  1066. if (!err)
  1067. err = register_die_notifier(&kprobe_exceptions_nb);
  1068. if (!err)
  1069. err = register_module_notifier(&kprobe_module_nb);
  1070. kprobes_initialized = (err == 0);
  1071. if (!err)
  1072. init_test_probes();
  1073. return err;
  1074. }
  1075. #ifdef CONFIG_DEBUG_FS
  1076. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1077. const char *sym, int offset,char *modname)
  1078. {
  1079. char *kprobe_type;
  1080. if (p->pre_handler == pre_handler_kretprobe)
  1081. kprobe_type = "r";
  1082. else if (p->pre_handler == setjmp_pre_handler)
  1083. kprobe_type = "j";
  1084. else
  1085. kprobe_type = "k";
  1086. if (sym)
  1087. seq_printf(pi, "%p %s %s+0x%x %s %s\n", p->addr, kprobe_type,
  1088. sym, offset, (modname ? modname : " "),
  1089. (kprobe_gone(p) ? "[GONE]" : ""));
  1090. else
  1091. seq_printf(pi, "%p %s %p %s\n", p->addr, kprobe_type, p->addr,
  1092. (kprobe_gone(p) ? "[GONE]" : ""));
  1093. }
  1094. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1095. {
  1096. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1097. }
  1098. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1099. {
  1100. (*pos)++;
  1101. if (*pos >= KPROBE_TABLE_SIZE)
  1102. return NULL;
  1103. return pos;
  1104. }
  1105. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1106. {
  1107. /* Nothing to do */
  1108. }
  1109. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1110. {
  1111. struct hlist_head *head;
  1112. struct hlist_node *node;
  1113. struct kprobe *p, *kp;
  1114. const char *sym = NULL;
  1115. unsigned int i = *(loff_t *) v;
  1116. unsigned long offset = 0;
  1117. char *modname, namebuf[128];
  1118. head = &kprobe_table[i];
  1119. preempt_disable();
  1120. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1121. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1122. &offset, &modname, namebuf);
  1123. if (p->pre_handler == aggr_pre_handler) {
  1124. list_for_each_entry_rcu(kp, &p->list, list)
  1125. report_probe(pi, kp, sym, offset, modname);
  1126. } else
  1127. report_probe(pi, p, sym, offset, modname);
  1128. }
  1129. preempt_enable();
  1130. return 0;
  1131. }
  1132. static struct seq_operations kprobes_seq_ops = {
  1133. .start = kprobe_seq_start,
  1134. .next = kprobe_seq_next,
  1135. .stop = kprobe_seq_stop,
  1136. .show = show_kprobe_addr
  1137. };
  1138. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1139. {
  1140. return seq_open(filp, &kprobes_seq_ops);
  1141. }
  1142. static struct file_operations debugfs_kprobes_operations = {
  1143. .open = kprobes_open,
  1144. .read = seq_read,
  1145. .llseek = seq_lseek,
  1146. .release = seq_release,
  1147. };
  1148. static void __kprobes enable_all_kprobes(void)
  1149. {
  1150. struct hlist_head *head;
  1151. struct hlist_node *node;
  1152. struct kprobe *p;
  1153. unsigned int i;
  1154. mutex_lock(&kprobe_mutex);
  1155. /* If kprobes are already enabled, just return */
  1156. if (kprobe_enabled)
  1157. goto already_enabled;
  1158. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1159. head = &kprobe_table[i];
  1160. hlist_for_each_entry_rcu(p, node, head, hlist)
  1161. if (!kprobe_gone(p))
  1162. arch_arm_kprobe(p);
  1163. }
  1164. kprobe_enabled = true;
  1165. printk(KERN_INFO "Kprobes globally enabled\n");
  1166. already_enabled:
  1167. mutex_unlock(&kprobe_mutex);
  1168. return;
  1169. }
  1170. static void __kprobes disable_all_kprobes(void)
  1171. {
  1172. struct hlist_head *head;
  1173. struct hlist_node *node;
  1174. struct kprobe *p;
  1175. unsigned int i;
  1176. mutex_lock(&kprobe_mutex);
  1177. /* If kprobes are already disabled, just return */
  1178. if (!kprobe_enabled)
  1179. goto already_disabled;
  1180. kprobe_enabled = false;
  1181. printk(KERN_INFO "Kprobes globally disabled\n");
  1182. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1183. head = &kprobe_table[i];
  1184. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1185. if (!arch_trampoline_kprobe(p) && !kprobe_gone(p))
  1186. arch_disarm_kprobe(p);
  1187. }
  1188. }
  1189. mutex_unlock(&kprobe_mutex);
  1190. /* Allow all currently running kprobes to complete */
  1191. synchronize_sched();
  1192. return;
  1193. already_disabled:
  1194. mutex_unlock(&kprobe_mutex);
  1195. return;
  1196. }
  1197. /*
  1198. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1199. * when the bool state is switched. We can reuse that facility when
  1200. * available
  1201. */
  1202. static ssize_t read_enabled_file_bool(struct file *file,
  1203. char __user *user_buf, size_t count, loff_t *ppos)
  1204. {
  1205. char buf[3];
  1206. if (kprobe_enabled)
  1207. buf[0] = '1';
  1208. else
  1209. buf[0] = '0';
  1210. buf[1] = '\n';
  1211. buf[2] = 0x00;
  1212. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1213. }
  1214. static ssize_t write_enabled_file_bool(struct file *file,
  1215. const char __user *user_buf, size_t count, loff_t *ppos)
  1216. {
  1217. char buf[32];
  1218. int buf_size;
  1219. buf_size = min(count, (sizeof(buf)-1));
  1220. if (copy_from_user(buf, user_buf, buf_size))
  1221. return -EFAULT;
  1222. switch (buf[0]) {
  1223. case 'y':
  1224. case 'Y':
  1225. case '1':
  1226. enable_all_kprobes();
  1227. break;
  1228. case 'n':
  1229. case 'N':
  1230. case '0':
  1231. disable_all_kprobes();
  1232. break;
  1233. }
  1234. return count;
  1235. }
  1236. static struct file_operations fops_kp = {
  1237. .read = read_enabled_file_bool,
  1238. .write = write_enabled_file_bool,
  1239. };
  1240. static int __kprobes debugfs_kprobe_init(void)
  1241. {
  1242. struct dentry *dir, *file;
  1243. unsigned int value = 1;
  1244. dir = debugfs_create_dir("kprobes", NULL);
  1245. if (!dir)
  1246. return -ENOMEM;
  1247. file = debugfs_create_file("list", 0444, dir, NULL,
  1248. &debugfs_kprobes_operations);
  1249. if (!file) {
  1250. debugfs_remove(dir);
  1251. return -ENOMEM;
  1252. }
  1253. file = debugfs_create_file("enabled", 0600, dir,
  1254. &value, &fops_kp);
  1255. if (!file) {
  1256. debugfs_remove(dir);
  1257. return -ENOMEM;
  1258. }
  1259. return 0;
  1260. }
  1261. late_initcall(debugfs_kprobe_init);
  1262. #endif /* CONFIG_DEBUG_FS */
  1263. module_init(init_kprobes);
  1264. EXPORT_SYMBOL_GPL(register_kprobe);
  1265. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1266. EXPORT_SYMBOL_GPL(register_kprobes);
  1267. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1268. EXPORT_SYMBOL_GPL(register_jprobe);
  1269. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1270. EXPORT_SYMBOL_GPL(register_jprobes);
  1271. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1272. EXPORT_SYMBOL_GPL(jprobe_return);
  1273. EXPORT_SYMBOL_GPL(register_kretprobe);
  1274. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1275. EXPORT_SYMBOL_GPL(register_kretprobes);
  1276. EXPORT_SYMBOL_GPL(unregister_kretprobes);