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