kprobes.c 38 KB

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