kprobes.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /*
  2. * Kernel Probes (KProbes)
  3. * kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation (includes suggestions from
  23. * Rusty Russell).
  24. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  25. * hlists and exceptions notifier as suggested by Andi Kleen.
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  29. * exceptions notifier to be first on the priority list.
  30. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  31. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  32. * <prasanna@in.ibm.com> added function-return probes.
  33. */
  34. #include <linux/kprobes.h>
  35. #include <linux/hash.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleloader.h>
  41. #include <linux/kallsyms.h>
  42. #include <linux/freezer.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/kdebug.h>
  46. #include <linux/memory.h>
  47. #include <asm-generic/sections.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/errno.h>
  50. #include <asm/uaccess.h>
  51. #define KPROBE_HASH_BITS 6
  52. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  53. /*
  54. * Some oddball architectures like 64bit powerpc have function descriptors
  55. * so this must be overridable.
  56. */
  57. #ifndef kprobe_lookup_name
  58. #define kprobe_lookup_name(name, addr) \
  59. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  60. #endif
  61. static int kprobes_initialized;
  62. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  63. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  64. /* NOTE: change this value only with kprobe_mutex held */
  65. static bool kprobes_all_disarmed;
  66. static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  67. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  68. static struct {
  69. spinlock_t lock ____cacheline_aligned_in_smp;
  70. } kretprobe_table_locks[KPROBE_TABLE_SIZE];
  71. static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  72. {
  73. return &(kretprobe_table_locks[hash].lock);
  74. }
  75. /*
  76. * Normally, functions that we'd want to prohibit kprobes in, are marked
  77. * __kprobes. But, there are cases where such functions already belong to
  78. * a different section (__sched for preempt_schedule)
  79. *
  80. * For such cases, we now have a blacklist
  81. */
  82. static struct kprobe_blackpoint kprobe_blacklist[] = {
  83. {"preempt_schedule",},
  84. {NULL} /* Terminator */
  85. };
  86. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  87. /*
  88. * kprobe->ainsn.insn points to the copy of the instruction to be
  89. * single-stepped. x86_64, POWER4 and above have no-exec support and
  90. * stepping on the instruction on a vmalloced/kmalloced/data page
  91. * is a recipe for disaster
  92. */
  93. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  94. struct kprobe_insn_page {
  95. struct hlist_node hlist;
  96. kprobe_opcode_t *insns; /* Page of instruction slots */
  97. char slot_used[INSNS_PER_PAGE];
  98. int nused;
  99. int ngarbage;
  100. };
  101. enum kprobe_slot_state {
  102. SLOT_CLEAN = 0,
  103. SLOT_DIRTY = 1,
  104. SLOT_USED = 2,
  105. };
  106. static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
  107. static struct hlist_head kprobe_insn_pages;
  108. static int kprobe_garbage_slots;
  109. static int collect_garbage_slots(void);
  110. static int __kprobes check_safety(void)
  111. {
  112. int ret = 0;
  113. #if defined(CONFIG_PREEMPT) && defined(CONFIG_FREEZER)
  114. ret = freeze_processes();
  115. if (ret == 0) {
  116. struct task_struct *p, *q;
  117. do_each_thread(p, q) {
  118. if (p != current && p->state == TASK_RUNNING &&
  119. p->pid != 0) {
  120. printk("Check failed: %s is running\n",p->comm);
  121. ret = -1;
  122. goto loop_end;
  123. }
  124. } while_each_thread(p, q);
  125. }
  126. loop_end:
  127. thaw_processes();
  128. #else
  129. synchronize_sched();
  130. #endif
  131. return ret;
  132. }
  133. /**
  134. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  135. * We allocate an executable page if there's no room on existing ones.
  136. */
  137. static kprobe_opcode_t __kprobes *__get_insn_slot(void)
  138. {
  139. struct kprobe_insn_page *kip;
  140. struct hlist_node *pos;
  141. retry:
  142. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  143. if (kip->nused < INSNS_PER_PAGE) {
  144. int i;
  145. for (i = 0; i < INSNS_PER_PAGE; i++) {
  146. if (kip->slot_used[i] == SLOT_CLEAN) {
  147. kip->slot_used[i] = SLOT_USED;
  148. kip->nused++;
  149. return kip->insns + (i * MAX_INSN_SIZE);
  150. }
  151. }
  152. /* Surprise! No unused slots. Fix kip->nused. */
  153. kip->nused = INSNS_PER_PAGE;
  154. }
  155. }
  156. /* If there are any garbage slots, collect it and try again. */
  157. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  158. goto retry;
  159. }
  160. /* All out of space. Need to allocate a new page. Use slot 0. */
  161. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  162. if (!kip)
  163. return NULL;
  164. /*
  165. * Use module_alloc so this page is within +/- 2GB of where the
  166. * kernel image and loaded module images reside. This is required
  167. * so x86_64 can correctly handle the %rip-relative fixups.
  168. */
  169. kip->insns = module_alloc(PAGE_SIZE);
  170. if (!kip->insns) {
  171. kfree(kip);
  172. return NULL;
  173. }
  174. INIT_HLIST_NODE(&kip->hlist);
  175. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  176. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  177. kip->slot_used[0] = SLOT_USED;
  178. kip->nused = 1;
  179. kip->ngarbage = 0;
  180. return kip->insns;
  181. }
  182. kprobe_opcode_t __kprobes *get_insn_slot(void)
  183. {
  184. kprobe_opcode_t *ret;
  185. mutex_lock(&kprobe_insn_mutex);
  186. ret = __get_insn_slot();
  187. mutex_unlock(&kprobe_insn_mutex);
  188. return ret;
  189. }
  190. /* Return 1 if all garbages are collected, otherwise 0. */
  191. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  192. {
  193. kip->slot_used[idx] = SLOT_CLEAN;
  194. kip->nused--;
  195. if (kip->nused == 0) {
  196. /*
  197. * Page is no longer in use. Free it unless
  198. * it's the last one. We keep the last one
  199. * so as not to have to set it up again the
  200. * next time somebody inserts a probe.
  201. */
  202. hlist_del(&kip->hlist);
  203. if (hlist_empty(&kprobe_insn_pages)) {
  204. INIT_HLIST_NODE(&kip->hlist);
  205. hlist_add_head(&kip->hlist,
  206. &kprobe_insn_pages);
  207. } else {
  208. module_free(NULL, kip->insns);
  209. kfree(kip);
  210. }
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. static int __kprobes collect_garbage_slots(void)
  216. {
  217. struct kprobe_insn_page *kip;
  218. struct hlist_node *pos, *next;
  219. int safety;
  220. /* Ensure no-one is preepmted on the garbages */
  221. mutex_unlock(&kprobe_insn_mutex);
  222. safety = check_safety();
  223. mutex_lock(&kprobe_insn_mutex);
  224. if (safety != 0)
  225. return -EAGAIN;
  226. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  227. int i;
  228. if (kip->ngarbage == 0)
  229. continue;
  230. kip->ngarbage = 0; /* we will collect all garbages */
  231. for (i = 0; i < INSNS_PER_PAGE; i++) {
  232. if (kip->slot_used[i] == SLOT_DIRTY &&
  233. collect_one_slot(kip, i))
  234. break;
  235. }
  236. }
  237. kprobe_garbage_slots = 0;
  238. return 0;
  239. }
  240. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  241. {
  242. struct kprobe_insn_page *kip;
  243. struct hlist_node *pos;
  244. mutex_lock(&kprobe_insn_mutex);
  245. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  246. if (kip->insns <= slot &&
  247. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  248. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  249. if (dirty) {
  250. kip->slot_used[i] = SLOT_DIRTY;
  251. kip->ngarbage++;
  252. } else {
  253. collect_one_slot(kip, i);
  254. }
  255. break;
  256. }
  257. }
  258. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  259. collect_garbage_slots();
  260. mutex_unlock(&kprobe_insn_mutex);
  261. }
  262. #endif
  263. /* We have preemption disabled.. so it is safe to use __ versions */
  264. static inline void set_kprobe_instance(struct kprobe *kp)
  265. {
  266. __get_cpu_var(kprobe_instance) = kp;
  267. }
  268. static inline void reset_kprobe_instance(void)
  269. {
  270. __get_cpu_var(kprobe_instance) = NULL;
  271. }
  272. /*
  273. * This routine is called either:
  274. * - under the kprobe_mutex - during kprobe_[un]register()
  275. * OR
  276. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  277. */
  278. struct kprobe __kprobes *get_kprobe(void *addr)
  279. {
  280. struct hlist_head *head;
  281. struct hlist_node *node;
  282. struct kprobe *p;
  283. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  284. hlist_for_each_entry_rcu(p, node, head, hlist) {
  285. if (p->addr == addr)
  286. return p;
  287. }
  288. return NULL;
  289. }
  290. /*
  291. * Aggregate handlers for multiple kprobes support - these handlers
  292. * take care of invoking the individual kprobe handlers on p->list
  293. */
  294. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  295. {
  296. struct kprobe *kp;
  297. list_for_each_entry_rcu(kp, &p->list, list) {
  298. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  299. set_kprobe_instance(kp);
  300. if (kp->pre_handler(kp, regs))
  301. return 1;
  302. }
  303. reset_kprobe_instance();
  304. }
  305. return 0;
  306. }
  307. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  308. unsigned long flags)
  309. {
  310. struct kprobe *kp;
  311. list_for_each_entry_rcu(kp, &p->list, list) {
  312. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  313. set_kprobe_instance(kp);
  314. kp->post_handler(kp, regs, flags);
  315. reset_kprobe_instance();
  316. }
  317. }
  318. }
  319. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  320. int trapnr)
  321. {
  322. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  323. /*
  324. * if we faulted "during" the execution of a user specified
  325. * probe handler, invoke just that probe's fault handler
  326. */
  327. if (cur && cur->fault_handler) {
  328. if (cur->fault_handler(cur, regs, trapnr))
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  334. {
  335. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  336. int ret = 0;
  337. if (cur && cur->break_handler) {
  338. if (cur->break_handler(cur, regs))
  339. ret = 1;
  340. }
  341. reset_kprobe_instance();
  342. return ret;
  343. }
  344. /* Walks the list and increments nmissed count for multiprobe case */
  345. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  346. {
  347. struct kprobe *kp;
  348. if (p->pre_handler != aggr_pre_handler) {
  349. p->nmissed++;
  350. } else {
  351. list_for_each_entry_rcu(kp, &p->list, list)
  352. kp->nmissed++;
  353. }
  354. return;
  355. }
  356. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  357. struct hlist_head *head)
  358. {
  359. struct kretprobe *rp = ri->rp;
  360. /* remove rp inst off the rprobe_inst_table */
  361. hlist_del(&ri->hlist);
  362. INIT_HLIST_NODE(&ri->hlist);
  363. if (likely(rp)) {
  364. spin_lock(&rp->lock);
  365. hlist_add_head(&ri->hlist, &rp->free_instances);
  366. spin_unlock(&rp->lock);
  367. } else
  368. /* Unregistering */
  369. hlist_add_head(&ri->hlist, head);
  370. }
  371. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  372. struct hlist_head **head, unsigned long *flags)
  373. {
  374. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  375. spinlock_t *hlist_lock;
  376. *head = &kretprobe_inst_table[hash];
  377. hlist_lock = kretprobe_table_lock_ptr(hash);
  378. spin_lock_irqsave(hlist_lock, *flags);
  379. }
  380. static void __kprobes kretprobe_table_lock(unsigned long hash,
  381. unsigned long *flags)
  382. {
  383. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  384. spin_lock_irqsave(hlist_lock, *flags);
  385. }
  386. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  387. unsigned long *flags)
  388. {
  389. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  390. spinlock_t *hlist_lock;
  391. hlist_lock = kretprobe_table_lock_ptr(hash);
  392. spin_unlock_irqrestore(hlist_lock, *flags);
  393. }
  394. void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
  395. {
  396. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  397. spin_unlock_irqrestore(hlist_lock, *flags);
  398. }
  399. /*
  400. * This function is called from finish_task_switch when task tk becomes dead,
  401. * so that we can recycle any function-return probe instances associated
  402. * with this task. These left over instances represent probed functions
  403. * that have been called but will never return.
  404. */
  405. void __kprobes kprobe_flush_task(struct task_struct *tk)
  406. {
  407. struct kretprobe_instance *ri;
  408. struct hlist_head *head, empty_rp;
  409. struct hlist_node *node, *tmp;
  410. unsigned long hash, flags = 0;
  411. if (unlikely(!kprobes_initialized))
  412. /* Early boot. kretprobe_table_locks not yet initialized. */
  413. return;
  414. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  415. head = &kretprobe_inst_table[hash];
  416. kretprobe_table_lock(hash, &flags);
  417. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  418. if (ri->task == tk)
  419. recycle_rp_inst(ri, &empty_rp);
  420. }
  421. kretprobe_table_unlock(hash, &flags);
  422. INIT_HLIST_HEAD(&empty_rp);
  423. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  424. hlist_del(&ri->hlist);
  425. kfree(ri);
  426. }
  427. }
  428. static inline void free_rp_inst(struct kretprobe *rp)
  429. {
  430. struct kretprobe_instance *ri;
  431. struct hlist_node *pos, *next;
  432. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  433. hlist_del(&ri->hlist);
  434. kfree(ri);
  435. }
  436. }
  437. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  438. {
  439. unsigned long flags, hash;
  440. struct kretprobe_instance *ri;
  441. struct hlist_node *pos, *next;
  442. struct hlist_head *head;
  443. /* No race here */
  444. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  445. kretprobe_table_lock(hash, &flags);
  446. head = &kretprobe_inst_table[hash];
  447. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  448. if (ri->rp == rp)
  449. ri->rp = NULL;
  450. }
  451. kretprobe_table_unlock(hash, &flags);
  452. }
  453. free_rp_inst(rp);
  454. }
  455. /*
  456. * Keep all fields in the kprobe consistent
  457. */
  458. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  459. {
  460. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  461. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  462. }
  463. /*
  464. * Add the new probe to ap->list. Fail if this is the
  465. * second jprobe at the address - two jprobes can't coexist
  466. */
  467. static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  468. {
  469. BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
  470. if (p->break_handler) {
  471. if (ap->break_handler)
  472. return -EEXIST;
  473. list_add_tail_rcu(&p->list, &ap->list);
  474. ap->break_handler = aggr_break_handler;
  475. } else
  476. list_add_rcu(&p->list, &ap->list);
  477. if (p->post_handler && !ap->post_handler)
  478. ap->post_handler = aggr_post_handler;
  479. if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
  480. ap->flags &= ~KPROBE_FLAG_DISABLED;
  481. if (!kprobes_all_disarmed)
  482. /* Arm the breakpoint again. */
  483. arch_arm_kprobe(ap);
  484. }
  485. return 0;
  486. }
  487. /*
  488. * Fill in the required fields of the "manager kprobe". Replace the
  489. * earlier kprobe in the hlist with the manager kprobe
  490. */
  491. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  492. {
  493. copy_kprobe(p, ap);
  494. flush_insn_slot(ap);
  495. ap->addr = p->addr;
  496. ap->flags = p->flags;
  497. ap->pre_handler = aggr_pre_handler;
  498. ap->fault_handler = aggr_fault_handler;
  499. /* We don't care the kprobe which has gone. */
  500. if (p->post_handler && !kprobe_gone(p))
  501. ap->post_handler = aggr_post_handler;
  502. if (p->break_handler && !kprobe_gone(p))
  503. ap->break_handler = aggr_break_handler;
  504. INIT_LIST_HEAD(&ap->list);
  505. list_add_rcu(&p->list, &ap->list);
  506. hlist_replace_rcu(&p->hlist, &ap->hlist);
  507. }
  508. /*
  509. * This is the second or subsequent kprobe at the address - handle
  510. * the intricacies
  511. */
  512. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  513. struct kprobe *p)
  514. {
  515. int ret = 0;
  516. struct kprobe *ap = old_p;
  517. if (old_p->pre_handler != aggr_pre_handler) {
  518. /* If old_p is not an aggr_probe, create new aggr_kprobe. */
  519. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  520. if (!ap)
  521. return -ENOMEM;
  522. add_aggr_kprobe(ap, old_p);
  523. }
  524. if (kprobe_gone(ap)) {
  525. /*
  526. * Attempting to insert new probe at the same location that
  527. * had a probe in the module vaddr area which already
  528. * freed. So, the instruction slot has already been
  529. * released. We need a new slot for the new probe.
  530. */
  531. ret = arch_prepare_kprobe(ap);
  532. if (ret)
  533. /*
  534. * Even if fail to allocate new slot, don't need to
  535. * free aggr_probe. It will be used next time, or
  536. * freed by unregister_kprobe.
  537. */
  538. return ret;
  539. /*
  540. * Clear gone flag to prevent allocating new slot again, and
  541. * set disabled flag because it is not armed yet.
  542. */
  543. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  544. | KPROBE_FLAG_DISABLED;
  545. }
  546. copy_kprobe(ap, p);
  547. return add_new_kprobe(ap, p);
  548. }
  549. /* Try to disable aggr_kprobe, and return 1 if succeeded.*/
  550. static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
  551. {
  552. struct kprobe *kp;
  553. list_for_each_entry_rcu(kp, &p->list, list) {
  554. if (!kprobe_disabled(kp))
  555. /*
  556. * There is an active probe on the list.
  557. * We can't disable aggr_kprobe.
  558. */
  559. return 0;
  560. }
  561. p->flags |= KPROBE_FLAG_DISABLED;
  562. return 1;
  563. }
  564. static int __kprobes in_kprobes_functions(unsigned long addr)
  565. {
  566. struct kprobe_blackpoint *kb;
  567. if (addr >= (unsigned long)__kprobes_text_start &&
  568. addr < (unsigned long)__kprobes_text_end)
  569. return -EINVAL;
  570. /*
  571. * If there exists a kprobe_blacklist, verify and
  572. * fail any probe registration in the prohibited area
  573. */
  574. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  575. if (kb->start_addr) {
  576. if (addr >= kb->start_addr &&
  577. addr < (kb->start_addr + kb->range))
  578. return -EINVAL;
  579. }
  580. }
  581. return 0;
  582. }
  583. /*
  584. * If we have a symbol_name argument, look it up and add the offset field
  585. * to it. This way, we can specify a relative address to a symbol.
  586. */
  587. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  588. {
  589. kprobe_opcode_t *addr = p->addr;
  590. if (p->symbol_name) {
  591. if (addr)
  592. return NULL;
  593. kprobe_lookup_name(p->symbol_name, addr);
  594. }
  595. if (!addr)
  596. return NULL;
  597. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  598. }
  599. int __kprobes register_kprobe(struct kprobe *p)
  600. {
  601. int ret = 0;
  602. struct kprobe *old_p;
  603. struct module *probed_mod;
  604. kprobe_opcode_t *addr;
  605. addr = kprobe_addr(p);
  606. if (!addr)
  607. return -EINVAL;
  608. p->addr = addr;
  609. preempt_disable();
  610. if (!__kernel_text_address((unsigned long) p->addr) ||
  611. in_kprobes_functions((unsigned long) p->addr)) {
  612. preempt_enable();
  613. return -EINVAL;
  614. }
  615. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  616. p->flags &= KPROBE_FLAG_DISABLED;
  617. /*
  618. * Check if are we probing a module.
  619. */
  620. probed_mod = __module_text_address((unsigned long) p->addr);
  621. if (probed_mod) {
  622. /*
  623. * We must hold a refcount of the probed module while updating
  624. * its code to prohibit unexpected unloading.
  625. */
  626. if (unlikely(!try_module_get(probed_mod))) {
  627. preempt_enable();
  628. return -EINVAL;
  629. }
  630. /*
  631. * If the module freed .init.text, we couldn't insert
  632. * kprobes in there.
  633. */
  634. if (within_module_init((unsigned long)p->addr, probed_mod) &&
  635. probed_mod->state != MODULE_STATE_COMING) {
  636. module_put(probed_mod);
  637. preempt_enable();
  638. return -EINVAL;
  639. }
  640. }
  641. preempt_enable();
  642. p->nmissed = 0;
  643. INIT_LIST_HEAD(&p->list);
  644. mutex_lock(&kprobe_mutex);
  645. old_p = get_kprobe(p->addr);
  646. if (old_p) {
  647. ret = register_aggr_kprobe(old_p, p);
  648. goto out;
  649. }
  650. mutex_lock(&text_mutex);
  651. ret = arch_prepare_kprobe(p);
  652. if (ret)
  653. goto out_unlock_text;
  654. INIT_HLIST_NODE(&p->hlist);
  655. hlist_add_head_rcu(&p->hlist,
  656. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  657. if (!kprobes_all_disarmed && !kprobe_disabled(p))
  658. arch_arm_kprobe(p);
  659. out_unlock_text:
  660. mutex_unlock(&text_mutex);
  661. out:
  662. mutex_unlock(&kprobe_mutex);
  663. if (probed_mod)
  664. module_put(probed_mod);
  665. return ret;
  666. }
  667. EXPORT_SYMBOL_GPL(register_kprobe);
  668. /* Check passed kprobe is valid and return kprobe in kprobe_table. */
  669. static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
  670. {
  671. struct kprobe *old_p, *list_p;
  672. old_p = get_kprobe(p->addr);
  673. if (unlikely(!old_p))
  674. return NULL;
  675. if (p != old_p) {
  676. list_for_each_entry_rcu(list_p, &old_p->list, list)
  677. if (list_p == p)
  678. /* kprobe p is a valid probe */
  679. goto valid;
  680. return NULL;
  681. }
  682. valid:
  683. return old_p;
  684. }
  685. /*
  686. * Unregister a kprobe without a scheduler synchronization.
  687. */
  688. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  689. {
  690. struct kprobe *old_p, *list_p;
  691. old_p = __get_valid_kprobe(p);
  692. if (old_p == NULL)
  693. return -EINVAL;
  694. if (old_p == p ||
  695. (old_p->pre_handler == aggr_pre_handler &&
  696. list_is_singular(&old_p->list))) {
  697. /*
  698. * Only probe on the hash list. Disarm only if kprobes are
  699. * enabled and not gone - otherwise, the breakpoint would
  700. * already have been removed. We save on flushing icache.
  701. */
  702. if (!kprobes_all_disarmed && !kprobe_disabled(old_p)) {
  703. mutex_lock(&text_mutex);
  704. arch_disarm_kprobe(p);
  705. mutex_unlock(&text_mutex);
  706. }
  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. arch_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. /* Module notifier call back, checking kprobes on the module */
  1009. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  1010. unsigned long val, void *data)
  1011. {
  1012. struct module *mod = data;
  1013. struct hlist_head *head;
  1014. struct hlist_node *node;
  1015. struct kprobe *p;
  1016. unsigned int i;
  1017. int checkcore = (val == MODULE_STATE_GOING);
  1018. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  1019. return NOTIFY_DONE;
  1020. /*
  1021. * When MODULE_STATE_GOING was notified, both of module .text and
  1022. * .init.text sections would be freed. When MODULE_STATE_LIVE was
  1023. * notified, only .init.text section would be freed. We need to
  1024. * disable kprobes which have been inserted in the sections.
  1025. */
  1026. mutex_lock(&kprobe_mutex);
  1027. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1028. head = &kprobe_table[i];
  1029. hlist_for_each_entry_rcu(p, node, head, hlist)
  1030. if (within_module_init((unsigned long)p->addr, mod) ||
  1031. (checkcore &&
  1032. within_module_core((unsigned long)p->addr, mod))) {
  1033. /*
  1034. * The vaddr this probe is installed will soon
  1035. * be vfreed buy not synced to disk. Hence,
  1036. * disarming the breakpoint isn't needed.
  1037. */
  1038. kill_kprobe(p);
  1039. }
  1040. }
  1041. mutex_unlock(&kprobe_mutex);
  1042. return NOTIFY_DONE;
  1043. }
  1044. static struct notifier_block kprobe_module_nb = {
  1045. .notifier_call = kprobes_module_callback,
  1046. .priority = 0
  1047. };
  1048. static int __init init_kprobes(void)
  1049. {
  1050. int i, err = 0;
  1051. unsigned long offset = 0, size = 0;
  1052. char *modname, namebuf[128];
  1053. const char *symbol_name;
  1054. void *addr;
  1055. struct kprobe_blackpoint *kb;
  1056. /* FIXME allocate the probe table, currently defined statically */
  1057. /* initialize all list heads */
  1058. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1059. INIT_HLIST_HEAD(&kprobe_table[i]);
  1060. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  1061. spin_lock_init(&(kretprobe_table_locks[i].lock));
  1062. }
  1063. /*
  1064. * Lookup and populate the kprobe_blacklist.
  1065. *
  1066. * Unlike the kretprobe blacklist, we'll need to determine
  1067. * the range of addresses that belong to the said functions,
  1068. * since a kprobe need not necessarily be at the beginning
  1069. * of a function.
  1070. */
  1071. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1072. kprobe_lookup_name(kb->name, addr);
  1073. if (!addr)
  1074. continue;
  1075. kb->start_addr = (unsigned long)addr;
  1076. symbol_name = kallsyms_lookup(kb->start_addr,
  1077. &size, &offset, &modname, namebuf);
  1078. if (!symbol_name)
  1079. kb->range = 0;
  1080. else
  1081. kb->range = size;
  1082. }
  1083. if (kretprobe_blacklist_size) {
  1084. /* lookup the function address from its name */
  1085. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1086. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1087. kretprobe_blacklist[i].addr);
  1088. if (!kretprobe_blacklist[i].addr)
  1089. printk("kretprobe: lookup failed: %s\n",
  1090. kretprobe_blacklist[i].name);
  1091. }
  1092. }
  1093. /* By default, kprobes are armed */
  1094. kprobes_all_disarmed = false;
  1095. err = arch_init_kprobes();
  1096. if (!err)
  1097. err = register_die_notifier(&kprobe_exceptions_nb);
  1098. if (!err)
  1099. err = register_module_notifier(&kprobe_module_nb);
  1100. kprobes_initialized = (err == 0);
  1101. if (!err)
  1102. init_test_probes();
  1103. return err;
  1104. }
  1105. #ifdef CONFIG_DEBUG_FS
  1106. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1107. const char *sym, int offset,char *modname)
  1108. {
  1109. char *kprobe_type;
  1110. if (p->pre_handler == pre_handler_kretprobe)
  1111. kprobe_type = "r";
  1112. else if (p->pre_handler == setjmp_pre_handler)
  1113. kprobe_type = "j";
  1114. else
  1115. kprobe_type = "k";
  1116. if (sym)
  1117. seq_printf(pi, "%p %s %s+0x%x %s %s%s\n",
  1118. p->addr, kprobe_type, sym, offset,
  1119. (modname ? modname : " "),
  1120. (kprobe_gone(p) ? "[GONE]" : ""),
  1121. ((kprobe_disabled(p) && !kprobe_gone(p)) ?
  1122. "[DISABLED]" : ""));
  1123. else
  1124. seq_printf(pi, "%p %s %p %s%s\n",
  1125. p->addr, kprobe_type, p->addr,
  1126. (kprobe_gone(p) ? "[GONE]" : ""),
  1127. ((kprobe_disabled(p) && !kprobe_gone(p)) ?
  1128. "[DISABLED]" : ""));
  1129. }
  1130. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1131. {
  1132. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1133. }
  1134. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1135. {
  1136. (*pos)++;
  1137. if (*pos >= KPROBE_TABLE_SIZE)
  1138. return NULL;
  1139. return pos;
  1140. }
  1141. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1142. {
  1143. /* Nothing to do */
  1144. }
  1145. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1146. {
  1147. struct hlist_head *head;
  1148. struct hlist_node *node;
  1149. struct kprobe *p, *kp;
  1150. const char *sym = NULL;
  1151. unsigned int i = *(loff_t *) v;
  1152. unsigned long offset = 0;
  1153. char *modname, namebuf[128];
  1154. head = &kprobe_table[i];
  1155. preempt_disable();
  1156. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1157. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1158. &offset, &modname, namebuf);
  1159. if (p->pre_handler == aggr_pre_handler) {
  1160. list_for_each_entry_rcu(kp, &p->list, list)
  1161. report_probe(pi, kp, sym, offset, modname);
  1162. } else
  1163. report_probe(pi, p, sym, offset, modname);
  1164. }
  1165. preempt_enable();
  1166. return 0;
  1167. }
  1168. static struct seq_operations kprobes_seq_ops = {
  1169. .start = kprobe_seq_start,
  1170. .next = kprobe_seq_next,
  1171. .stop = kprobe_seq_stop,
  1172. .show = show_kprobe_addr
  1173. };
  1174. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1175. {
  1176. return seq_open(filp, &kprobes_seq_ops);
  1177. }
  1178. static struct file_operations debugfs_kprobes_operations = {
  1179. .open = kprobes_open,
  1180. .read = seq_read,
  1181. .llseek = seq_lseek,
  1182. .release = seq_release,
  1183. };
  1184. /* Disable one kprobe */
  1185. int __kprobes disable_kprobe(struct kprobe *kp)
  1186. {
  1187. int ret = 0;
  1188. struct kprobe *p;
  1189. mutex_lock(&kprobe_mutex);
  1190. /* Check whether specified probe is valid. */
  1191. p = __get_valid_kprobe(kp);
  1192. if (unlikely(p == NULL)) {
  1193. ret = -EINVAL;
  1194. goto out;
  1195. }
  1196. /* If the probe is already disabled (or gone), just return */
  1197. if (kprobe_disabled(kp))
  1198. goto out;
  1199. kp->flags |= KPROBE_FLAG_DISABLED;
  1200. if (p != kp)
  1201. /* When kp != p, p is always enabled. */
  1202. try_to_disable_aggr_kprobe(p);
  1203. if (!kprobes_all_disarmed && kprobe_disabled(p))
  1204. arch_disarm_kprobe(p);
  1205. out:
  1206. mutex_unlock(&kprobe_mutex);
  1207. return ret;
  1208. }
  1209. EXPORT_SYMBOL_GPL(disable_kprobe);
  1210. /* Enable one kprobe */
  1211. int __kprobes enable_kprobe(struct kprobe *kp)
  1212. {
  1213. int ret = 0;
  1214. struct kprobe *p;
  1215. mutex_lock(&kprobe_mutex);
  1216. /* Check whether specified probe is valid. */
  1217. p = __get_valid_kprobe(kp);
  1218. if (unlikely(p == NULL)) {
  1219. ret = -EINVAL;
  1220. goto out;
  1221. }
  1222. if (kprobe_gone(kp)) {
  1223. /* This kprobe has gone, we couldn't enable it. */
  1224. ret = -EINVAL;
  1225. goto out;
  1226. }
  1227. if (!kprobes_all_disarmed && kprobe_disabled(p))
  1228. arch_arm_kprobe(p);
  1229. p->flags &= ~KPROBE_FLAG_DISABLED;
  1230. if (p != kp)
  1231. kp->flags &= ~KPROBE_FLAG_DISABLED;
  1232. out:
  1233. mutex_unlock(&kprobe_mutex);
  1234. return ret;
  1235. }
  1236. EXPORT_SYMBOL_GPL(enable_kprobe);
  1237. static void __kprobes arm_all_kprobes(void)
  1238. {
  1239. struct hlist_head *head;
  1240. struct hlist_node *node;
  1241. struct kprobe *p;
  1242. unsigned int i;
  1243. mutex_lock(&kprobe_mutex);
  1244. /* If kprobes are armed, just return */
  1245. if (!kprobes_all_disarmed)
  1246. goto already_enabled;
  1247. mutex_lock(&text_mutex);
  1248. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1249. head = &kprobe_table[i];
  1250. hlist_for_each_entry_rcu(p, node, head, hlist)
  1251. if (!kprobe_disabled(p))
  1252. arch_arm_kprobe(p);
  1253. }
  1254. mutex_unlock(&text_mutex);
  1255. kprobes_all_disarmed = false;
  1256. printk(KERN_INFO "Kprobes globally enabled\n");
  1257. already_enabled:
  1258. mutex_unlock(&kprobe_mutex);
  1259. return;
  1260. }
  1261. static void __kprobes disarm_all_kprobes(void)
  1262. {
  1263. struct hlist_head *head;
  1264. struct hlist_node *node;
  1265. struct kprobe *p;
  1266. unsigned int i;
  1267. mutex_lock(&kprobe_mutex);
  1268. /* If kprobes are already disarmed, just return */
  1269. if (kprobes_all_disarmed)
  1270. goto already_disabled;
  1271. kprobes_all_disarmed = true;
  1272. printk(KERN_INFO "Kprobes globally disabled\n");
  1273. mutex_lock(&text_mutex);
  1274. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1275. head = &kprobe_table[i];
  1276. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1277. if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
  1278. arch_disarm_kprobe(p);
  1279. }
  1280. }
  1281. mutex_unlock(&text_mutex);
  1282. mutex_unlock(&kprobe_mutex);
  1283. /* Allow all currently running kprobes to complete */
  1284. synchronize_sched();
  1285. return;
  1286. already_disabled:
  1287. mutex_unlock(&kprobe_mutex);
  1288. return;
  1289. }
  1290. /*
  1291. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1292. * when the bool state is switched. We can reuse that facility when
  1293. * available
  1294. */
  1295. static ssize_t read_enabled_file_bool(struct file *file,
  1296. char __user *user_buf, size_t count, loff_t *ppos)
  1297. {
  1298. char buf[3];
  1299. if (!kprobes_all_disarmed)
  1300. buf[0] = '1';
  1301. else
  1302. buf[0] = '0';
  1303. buf[1] = '\n';
  1304. buf[2] = 0x00;
  1305. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1306. }
  1307. static ssize_t write_enabled_file_bool(struct file *file,
  1308. const char __user *user_buf, size_t count, loff_t *ppos)
  1309. {
  1310. char buf[32];
  1311. int buf_size;
  1312. buf_size = min(count, (sizeof(buf)-1));
  1313. if (copy_from_user(buf, user_buf, buf_size))
  1314. return -EFAULT;
  1315. switch (buf[0]) {
  1316. case 'y':
  1317. case 'Y':
  1318. case '1':
  1319. arm_all_kprobes();
  1320. break;
  1321. case 'n':
  1322. case 'N':
  1323. case '0':
  1324. disarm_all_kprobes();
  1325. break;
  1326. }
  1327. return count;
  1328. }
  1329. static struct file_operations fops_kp = {
  1330. .read = read_enabled_file_bool,
  1331. .write = write_enabled_file_bool,
  1332. };
  1333. static int __kprobes debugfs_kprobe_init(void)
  1334. {
  1335. struct dentry *dir, *file;
  1336. unsigned int value = 1;
  1337. dir = debugfs_create_dir("kprobes", NULL);
  1338. if (!dir)
  1339. return -ENOMEM;
  1340. file = debugfs_create_file("list", 0444, dir, NULL,
  1341. &debugfs_kprobes_operations);
  1342. if (!file) {
  1343. debugfs_remove(dir);
  1344. return -ENOMEM;
  1345. }
  1346. file = debugfs_create_file("enabled", 0600, dir,
  1347. &value, &fops_kp);
  1348. if (!file) {
  1349. debugfs_remove(dir);
  1350. return -ENOMEM;
  1351. }
  1352. return 0;
  1353. }
  1354. late_initcall(debugfs_kprobe_init);
  1355. #endif /* CONFIG_DEBUG_FS */
  1356. module_init(init_kprobes);
  1357. /* defined in arch/.../kernel/kprobes.c */
  1358. EXPORT_SYMBOL_GPL(jprobe_return);