kprobes.c 38 KB

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