uprobes.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /*
  2. * User-space Probes (UProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2008-2012
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/highmem.h>
  26. #include <linux/pagemap.h> /* read_mapping_page */
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/export.h>
  30. #include <linux/rmap.h> /* anon_vma_prepare */
  31. #include <linux/mmu_notifier.h> /* set_pte_at_notify */
  32. #include <linux/swap.h> /* try_to_free_swap */
  33. #include <linux/ptrace.h> /* user_enable_single_step */
  34. #include <linux/kdebug.h> /* notifier mechanism */
  35. #include "../../mm/internal.h" /* munlock_vma_page */
  36. #include <linux/percpu-rwsem.h>
  37. #include <linux/task_work.h>
  38. #include <linux/uprobes.h>
  39. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  40. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  41. static struct rb_root uprobes_tree = RB_ROOT;
  42. /*
  43. * allows us to skip the uprobe_mmap if there are no uprobe events active
  44. * at this time. Probably a fine grained per inode count is better?
  45. */
  46. #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
  47. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  48. #define UPROBES_HASH_SZ 13
  49. /* serialize uprobe->pending_list */
  50. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  51. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  52. static struct percpu_rw_semaphore dup_mmap_sem;
  53. /* Have a copy of original instruction */
  54. #define UPROBE_COPY_INSN 0
  55. /* Can skip singlestep */
  56. #define UPROBE_SKIP_SSTEP 1
  57. struct uprobe {
  58. struct rb_node rb_node; /* node in the rb tree */
  59. atomic_t ref;
  60. struct rw_semaphore register_rwsem;
  61. struct rw_semaphore consumer_rwsem;
  62. struct list_head pending_list;
  63. struct uprobe_consumer *consumers;
  64. struct inode *inode; /* Also hold a ref to inode */
  65. loff_t offset;
  66. unsigned long flags;
  67. struct arch_uprobe arch;
  68. };
  69. struct return_instance {
  70. struct uprobe *uprobe;
  71. unsigned long func;
  72. unsigned long orig_ret_vaddr; /* original return address */
  73. bool chained; /* true, if instance is nested */
  74. struct return_instance *next; /* keep as stack */
  75. };
  76. /*
  77. * valid_vma: Verify if the specified vma is an executable vma
  78. * Relax restrictions while unregistering: vm_flags might have
  79. * changed after breakpoint was inserted.
  80. * - is_register: indicates if we are in register context.
  81. * - Return 1 if the specified virtual address is in an
  82. * executable vma.
  83. */
  84. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  85. {
  86. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
  87. if (is_register)
  88. flags |= VM_WRITE;
  89. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  90. }
  91. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  92. {
  93. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  94. }
  95. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  96. {
  97. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  98. }
  99. /**
  100. * __replace_page - replace page in vma by new page.
  101. * based on replace_page in mm/ksm.c
  102. *
  103. * @vma: vma that holds the pte pointing to page
  104. * @addr: address the old @page is mapped at
  105. * @page: the cowed page we are replacing by kpage
  106. * @kpage: the modified page we replace page by
  107. *
  108. * Returns 0 on success, -EFAULT on failure.
  109. */
  110. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  111. struct page *page, struct page *kpage)
  112. {
  113. struct mm_struct *mm = vma->vm_mm;
  114. spinlock_t *ptl;
  115. pte_t *ptep;
  116. int err;
  117. /* For mmu_notifiers */
  118. const unsigned long mmun_start = addr;
  119. const unsigned long mmun_end = addr + PAGE_SIZE;
  120. /* For try_to_free_swap() and munlock_vma_page() below */
  121. lock_page(page);
  122. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  123. err = -EAGAIN;
  124. ptep = page_check_address(page, mm, addr, &ptl, 0);
  125. if (!ptep)
  126. goto unlock;
  127. get_page(kpage);
  128. page_add_new_anon_rmap(kpage, vma, addr);
  129. if (!PageAnon(page)) {
  130. dec_mm_counter(mm, MM_FILEPAGES);
  131. inc_mm_counter(mm, MM_ANONPAGES);
  132. }
  133. flush_cache_page(vma, addr, pte_pfn(*ptep));
  134. ptep_clear_flush(vma, addr, ptep);
  135. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  136. page_remove_rmap(page);
  137. if (!page_mapped(page))
  138. try_to_free_swap(page);
  139. pte_unmap_unlock(ptep, ptl);
  140. if (vma->vm_flags & VM_LOCKED)
  141. munlock_vma_page(page);
  142. put_page(page);
  143. err = 0;
  144. unlock:
  145. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  146. unlock_page(page);
  147. return err;
  148. }
  149. /**
  150. * is_swbp_insn - check if instruction is breakpoint instruction.
  151. * @insn: instruction to be checked.
  152. * Default implementation of is_swbp_insn
  153. * Returns true if @insn is a breakpoint instruction.
  154. */
  155. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  156. {
  157. return *insn == UPROBE_SWBP_INSN;
  158. }
  159. /**
  160. * is_trap_insn - check if instruction is breakpoint instruction.
  161. * @insn: instruction to be checked.
  162. * Default implementation of is_trap_insn
  163. * Returns true if @insn is a breakpoint instruction.
  164. *
  165. * This function is needed for the case where an architecture has multiple
  166. * trap instructions (like powerpc).
  167. */
  168. bool __weak is_trap_insn(uprobe_opcode_t *insn)
  169. {
  170. return is_swbp_insn(insn);
  171. }
  172. static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
  173. {
  174. void *kaddr = kmap_atomic(page);
  175. memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
  176. kunmap_atomic(kaddr);
  177. }
  178. static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
  179. {
  180. void *kaddr = kmap_atomic(page);
  181. memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
  182. kunmap_atomic(kaddr);
  183. }
  184. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  185. {
  186. uprobe_opcode_t old_opcode;
  187. bool is_swbp;
  188. /*
  189. * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
  190. * We do not check if it is any other 'trap variant' which could
  191. * be conditional trap instruction such as the one powerpc supports.
  192. *
  193. * The logic is that we do not care if the underlying instruction
  194. * is a trap variant; uprobes always wins over any other (gdb)
  195. * breakpoint.
  196. */
  197. copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
  198. is_swbp = is_swbp_insn(&old_opcode);
  199. if (is_swbp_insn(new_opcode)) {
  200. if (is_swbp) /* register: already installed? */
  201. return 0;
  202. } else {
  203. if (!is_swbp) /* unregister: was it changed by us? */
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. /*
  209. * NOTE:
  210. * Expect the breakpoint instruction to be the smallest size instruction for
  211. * the architecture. If an arch has variable length instruction and the
  212. * breakpoint instruction is not of the smallest length instruction
  213. * supported by that architecture then we need to modify is_trap_at_addr and
  214. * write_opcode accordingly. This would never be a problem for archs that
  215. * have fixed length instructions.
  216. */
  217. /*
  218. * write_opcode - write the opcode at a given virtual address.
  219. * @mm: the probed process address space.
  220. * @vaddr: the virtual address to store the opcode.
  221. * @opcode: opcode to be written at @vaddr.
  222. *
  223. * Called with mm->mmap_sem held (for read and with a reference to
  224. * mm).
  225. *
  226. * For mm @mm, write the opcode at @vaddr.
  227. * Return 0 (success) or a negative errno.
  228. */
  229. static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
  230. uprobe_opcode_t opcode)
  231. {
  232. struct page *old_page, *new_page;
  233. struct vm_area_struct *vma;
  234. int ret;
  235. retry:
  236. /* Read the page with vaddr into memory */
  237. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
  238. if (ret <= 0)
  239. return ret;
  240. ret = verify_opcode(old_page, vaddr, &opcode);
  241. if (ret <= 0)
  242. goto put_old;
  243. ret = -ENOMEM;
  244. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  245. if (!new_page)
  246. goto put_old;
  247. __SetPageUptodate(new_page);
  248. copy_highpage(new_page, old_page);
  249. copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  250. ret = anon_vma_prepare(vma);
  251. if (ret)
  252. goto put_new;
  253. ret = __replace_page(vma, vaddr, old_page, new_page);
  254. put_new:
  255. page_cache_release(new_page);
  256. put_old:
  257. put_page(old_page);
  258. if (unlikely(ret == -EAGAIN))
  259. goto retry;
  260. return ret;
  261. }
  262. /**
  263. * set_swbp - store breakpoint at a given address.
  264. * @auprobe: arch specific probepoint information.
  265. * @mm: the probed process address space.
  266. * @vaddr: the virtual address to insert the opcode.
  267. *
  268. * For mm @mm, store the breakpoint instruction at @vaddr.
  269. * Return 0 (success) or a negative errno.
  270. */
  271. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  272. {
  273. return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  274. }
  275. /**
  276. * set_orig_insn - Restore the original instruction.
  277. * @mm: the probed process address space.
  278. * @auprobe: arch specific probepoint information.
  279. * @vaddr: the virtual address to insert the opcode.
  280. *
  281. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  282. * Return 0 (success) or a negative errno.
  283. */
  284. int __weak
  285. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  286. {
  287. return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
  288. }
  289. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  290. {
  291. if (l->inode < r->inode)
  292. return -1;
  293. if (l->inode > r->inode)
  294. return 1;
  295. if (l->offset < r->offset)
  296. return -1;
  297. if (l->offset > r->offset)
  298. return 1;
  299. return 0;
  300. }
  301. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  302. {
  303. struct uprobe u = { .inode = inode, .offset = offset };
  304. struct rb_node *n = uprobes_tree.rb_node;
  305. struct uprobe *uprobe;
  306. int match;
  307. while (n) {
  308. uprobe = rb_entry(n, struct uprobe, rb_node);
  309. match = match_uprobe(&u, uprobe);
  310. if (!match) {
  311. atomic_inc(&uprobe->ref);
  312. return uprobe;
  313. }
  314. if (match < 0)
  315. n = n->rb_left;
  316. else
  317. n = n->rb_right;
  318. }
  319. return NULL;
  320. }
  321. /*
  322. * Find a uprobe corresponding to a given inode:offset
  323. * Acquires uprobes_treelock
  324. */
  325. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  326. {
  327. struct uprobe *uprobe;
  328. spin_lock(&uprobes_treelock);
  329. uprobe = __find_uprobe(inode, offset);
  330. spin_unlock(&uprobes_treelock);
  331. return uprobe;
  332. }
  333. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  334. {
  335. struct rb_node **p = &uprobes_tree.rb_node;
  336. struct rb_node *parent = NULL;
  337. struct uprobe *u;
  338. int match;
  339. while (*p) {
  340. parent = *p;
  341. u = rb_entry(parent, struct uprobe, rb_node);
  342. match = match_uprobe(uprobe, u);
  343. if (!match) {
  344. atomic_inc(&u->ref);
  345. return u;
  346. }
  347. if (match < 0)
  348. p = &parent->rb_left;
  349. else
  350. p = &parent->rb_right;
  351. }
  352. u = NULL;
  353. rb_link_node(&uprobe->rb_node, parent, p);
  354. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  355. /* get access + creation ref */
  356. atomic_set(&uprobe->ref, 2);
  357. return u;
  358. }
  359. /*
  360. * Acquire uprobes_treelock.
  361. * Matching uprobe already exists in rbtree;
  362. * increment (access refcount) and return the matching uprobe.
  363. *
  364. * No matching uprobe; insert the uprobe in rb_tree;
  365. * get a double refcount (access + creation) and return NULL.
  366. */
  367. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  368. {
  369. struct uprobe *u;
  370. spin_lock(&uprobes_treelock);
  371. u = __insert_uprobe(uprobe);
  372. spin_unlock(&uprobes_treelock);
  373. return u;
  374. }
  375. static void put_uprobe(struct uprobe *uprobe)
  376. {
  377. if (atomic_dec_and_test(&uprobe->ref))
  378. kfree(uprobe);
  379. }
  380. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  381. {
  382. struct uprobe *uprobe, *cur_uprobe;
  383. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  384. if (!uprobe)
  385. return NULL;
  386. uprobe->inode = igrab(inode);
  387. uprobe->offset = offset;
  388. init_rwsem(&uprobe->register_rwsem);
  389. init_rwsem(&uprobe->consumer_rwsem);
  390. /* For now assume that the instruction need not be single-stepped */
  391. __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  392. /* add to uprobes_tree, sorted on inode:offset */
  393. cur_uprobe = insert_uprobe(uprobe);
  394. /* a uprobe exists for this inode:offset combination */
  395. if (cur_uprobe) {
  396. kfree(uprobe);
  397. uprobe = cur_uprobe;
  398. iput(inode);
  399. }
  400. return uprobe;
  401. }
  402. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  403. {
  404. down_write(&uprobe->consumer_rwsem);
  405. uc->next = uprobe->consumers;
  406. uprobe->consumers = uc;
  407. up_write(&uprobe->consumer_rwsem);
  408. }
  409. /*
  410. * For uprobe @uprobe, delete the consumer @uc.
  411. * Return true if the @uc is deleted successfully
  412. * or return false.
  413. */
  414. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  415. {
  416. struct uprobe_consumer **con;
  417. bool ret = false;
  418. down_write(&uprobe->consumer_rwsem);
  419. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  420. if (*con == uc) {
  421. *con = uc->next;
  422. ret = true;
  423. break;
  424. }
  425. }
  426. up_write(&uprobe->consumer_rwsem);
  427. return ret;
  428. }
  429. static int
  430. __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
  431. unsigned long nbytes, loff_t offset)
  432. {
  433. struct page *page;
  434. if (!mapping->a_ops->readpage)
  435. return -EIO;
  436. /*
  437. * Ensure that the page that has the original instruction is
  438. * populated and in page-cache.
  439. */
  440. page = read_mapping_page(mapping, offset >> PAGE_CACHE_SHIFT, filp);
  441. if (IS_ERR(page))
  442. return PTR_ERR(page);
  443. copy_from_page(page, offset, insn, nbytes);
  444. page_cache_release(page);
  445. return 0;
  446. }
  447. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  448. {
  449. struct address_space *mapping;
  450. unsigned long nbytes;
  451. int bytes;
  452. nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
  453. mapping = uprobe->inode->i_mapping;
  454. /* Instruction at end of binary; copy only available bytes */
  455. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  456. bytes = uprobe->inode->i_size - uprobe->offset;
  457. else
  458. bytes = MAX_UINSN_BYTES;
  459. /* Instruction at the page-boundary; copy bytes in second page */
  460. if (nbytes < bytes) {
  461. int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
  462. bytes - nbytes, uprobe->offset + nbytes);
  463. if (err)
  464. return err;
  465. bytes = nbytes;
  466. }
  467. return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
  468. }
  469. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  470. struct mm_struct *mm, unsigned long vaddr)
  471. {
  472. int ret = 0;
  473. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  474. return ret;
  475. /* TODO: move this into _register, until then we abuse this sem. */
  476. down_write(&uprobe->consumer_rwsem);
  477. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  478. goto out;
  479. ret = copy_insn(uprobe, file);
  480. if (ret)
  481. goto out;
  482. ret = -ENOTSUPP;
  483. if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn))
  484. goto out;
  485. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  486. if (ret)
  487. goto out;
  488. /* write_opcode() assumes we don't cross page boundary */
  489. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  490. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  491. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  492. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  493. out:
  494. up_write(&uprobe->consumer_rwsem);
  495. return ret;
  496. }
  497. static inline bool consumer_filter(struct uprobe_consumer *uc,
  498. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  499. {
  500. return !uc->filter || uc->filter(uc, ctx, mm);
  501. }
  502. static bool filter_chain(struct uprobe *uprobe,
  503. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  504. {
  505. struct uprobe_consumer *uc;
  506. bool ret = false;
  507. down_read(&uprobe->consumer_rwsem);
  508. for (uc = uprobe->consumers; uc; uc = uc->next) {
  509. ret = consumer_filter(uc, ctx, mm);
  510. if (ret)
  511. break;
  512. }
  513. up_read(&uprobe->consumer_rwsem);
  514. return ret;
  515. }
  516. static int
  517. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  518. struct vm_area_struct *vma, unsigned long vaddr)
  519. {
  520. bool first_uprobe;
  521. int ret;
  522. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  523. if (ret)
  524. return ret;
  525. /*
  526. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  527. * the task can hit this breakpoint right after __replace_page().
  528. */
  529. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  530. if (first_uprobe)
  531. set_bit(MMF_HAS_UPROBES, &mm->flags);
  532. ret = set_swbp(&uprobe->arch, mm, vaddr);
  533. if (!ret)
  534. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  535. else if (first_uprobe)
  536. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  537. return ret;
  538. }
  539. static int
  540. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  541. {
  542. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  543. return set_orig_insn(&uprobe->arch, mm, vaddr);
  544. }
  545. static inline bool uprobe_is_active(struct uprobe *uprobe)
  546. {
  547. return !RB_EMPTY_NODE(&uprobe->rb_node);
  548. }
  549. /*
  550. * There could be threads that have already hit the breakpoint. They
  551. * will recheck the current insn and restart if find_uprobe() fails.
  552. * See find_active_uprobe().
  553. */
  554. static void delete_uprobe(struct uprobe *uprobe)
  555. {
  556. if (WARN_ON(!uprobe_is_active(uprobe)))
  557. return;
  558. spin_lock(&uprobes_treelock);
  559. rb_erase(&uprobe->rb_node, &uprobes_tree);
  560. spin_unlock(&uprobes_treelock);
  561. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  562. iput(uprobe->inode);
  563. put_uprobe(uprobe);
  564. }
  565. struct map_info {
  566. struct map_info *next;
  567. struct mm_struct *mm;
  568. unsigned long vaddr;
  569. };
  570. static inline struct map_info *free_map_info(struct map_info *info)
  571. {
  572. struct map_info *next = info->next;
  573. kfree(info);
  574. return next;
  575. }
  576. static struct map_info *
  577. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  578. {
  579. unsigned long pgoff = offset >> PAGE_SHIFT;
  580. struct vm_area_struct *vma;
  581. struct map_info *curr = NULL;
  582. struct map_info *prev = NULL;
  583. struct map_info *info;
  584. int more = 0;
  585. again:
  586. mutex_lock(&mapping->i_mmap_mutex);
  587. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  588. if (!valid_vma(vma, is_register))
  589. continue;
  590. if (!prev && !more) {
  591. /*
  592. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  593. * reclaim. This is optimistic, no harm done if it fails.
  594. */
  595. prev = kmalloc(sizeof(struct map_info),
  596. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  597. if (prev)
  598. prev->next = NULL;
  599. }
  600. if (!prev) {
  601. more++;
  602. continue;
  603. }
  604. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  605. continue;
  606. info = prev;
  607. prev = prev->next;
  608. info->next = curr;
  609. curr = info;
  610. info->mm = vma->vm_mm;
  611. info->vaddr = offset_to_vaddr(vma, offset);
  612. }
  613. mutex_unlock(&mapping->i_mmap_mutex);
  614. if (!more)
  615. goto out;
  616. prev = curr;
  617. while (curr) {
  618. mmput(curr->mm);
  619. curr = curr->next;
  620. }
  621. do {
  622. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  623. if (!info) {
  624. curr = ERR_PTR(-ENOMEM);
  625. goto out;
  626. }
  627. info->next = prev;
  628. prev = info;
  629. } while (--more);
  630. goto again;
  631. out:
  632. while (prev)
  633. prev = free_map_info(prev);
  634. return curr;
  635. }
  636. static int
  637. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  638. {
  639. bool is_register = !!new;
  640. struct map_info *info;
  641. int err = 0;
  642. percpu_down_write(&dup_mmap_sem);
  643. info = build_map_info(uprobe->inode->i_mapping,
  644. uprobe->offset, is_register);
  645. if (IS_ERR(info)) {
  646. err = PTR_ERR(info);
  647. goto out;
  648. }
  649. while (info) {
  650. struct mm_struct *mm = info->mm;
  651. struct vm_area_struct *vma;
  652. if (err && is_register)
  653. goto free;
  654. down_write(&mm->mmap_sem);
  655. vma = find_vma(mm, info->vaddr);
  656. if (!vma || !valid_vma(vma, is_register) ||
  657. file_inode(vma->vm_file) != uprobe->inode)
  658. goto unlock;
  659. if (vma->vm_start > info->vaddr ||
  660. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  661. goto unlock;
  662. if (is_register) {
  663. /* consult only the "caller", new consumer. */
  664. if (consumer_filter(new,
  665. UPROBE_FILTER_REGISTER, mm))
  666. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  667. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  668. if (!filter_chain(uprobe,
  669. UPROBE_FILTER_UNREGISTER, mm))
  670. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  671. }
  672. unlock:
  673. up_write(&mm->mmap_sem);
  674. free:
  675. mmput(mm);
  676. info = free_map_info(info);
  677. }
  678. out:
  679. percpu_up_write(&dup_mmap_sem);
  680. return err;
  681. }
  682. static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
  683. {
  684. consumer_add(uprobe, uc);
  685. return register_for_each_vma(uprobe, uc);
  686. }
  687. static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  688. {
  689. int err;
  690. if (!consumer_del(uprobe, uc)) /* WARN? */
  691. return;
  692. err = register_for_each_vma(uprobe, NULL);
  693. /* TODO : cant unregister? schedule a worker thread */
  694. if (!uprobe->consumers && !err)
  695. delete_uprobe(uprobe);
  696. }
  697. /*
  698. * uprobe_register - register a probe
  699. * @inode: the file in which the probe has to be placed.
  700. * @offset: offset from the start of the file.
  701. * @uc: information on howto handle the probe..
  702. *
  703. * Apart from the access refcount, uprobe_register() takes a creation
  704. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  705. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  706. * tuple). Creation refcount stops uprobe_unregister from freeing the
  707. * @uprobe even before the register operation is complete. Creation
  708. * refcount is released when the last @uc for the @uprobe
  709. * unregisters.
  710. *
  711. * Return errno if it cannot successully install probes
  712. * else return 0 (success)
  713. */
  714. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  715. {
  716. struct uprobe *uprobe;
  717. int ret;
  718. /* Uprobe must have at least one set consumer */
  719. if (!uc->handler && !uc->ret_handler)
  720. return -EINVAL;
  721. /* Racy, just to catch the obvious mistakes */
  722. if (offset > i_size_read(inode))
  723. return -EINVAL;
  724. retry:
  725. uprobe = alloc_uprobe(inode, offset);
  726. if (!uprobe)
  727. return -ENOMEM;
  728. /*
  729. * We can race with uprobe_unregister()->delete_uprobe().
  730. * Check uprobe_is_active() and retry if it is false.
  731. */
  732. down_write(&uprobe->register_rwsem);
  733. ret = -EAGAIN;
  734. if (likely(uprobe_is_active(uprobe))) {
  735. ret = __uprobe_register(uprobe, uc);
  736. if (ret)
  737. __uprobe_unregister(uprobe, uc);
  738. }
  739. up_write(&uprobe->register_rwsem);
  740. put_uprobe(uprobe);
  741. if (unlikely(ret == -EAGAIN))
  742. goto retry;
  743. return ret;
  744. }
  745. EXPORT_SYMBOL_GPL(uprobe_register);
  746. /*
  747. * uprobe_apply - unregister a already registered probe.
  748. * @inode: the file in which the probe has to be removed.
  749. * @offset: offset from the start of the file.
  750. * @uc: consumer which wants to add more or remove some breakpoints
  751. * @add: add or remove the breakpoints
  752. */
  753. int uprobe_apply(struct inode *inode, loff_t offset,
  754. struct uprobe_consumer *uc, bool add)
  755. {
  756. struct uprobe *uprobe;
  757. struct uprobe_consumer *con;
  758. int ret = -ENOENT;
  759. uprobe = find_uprobe(inode, offset);
  760. if (!uprobe)
  761. return ret;
  762. down_write(&uprobe->register_rwsem);
  763. for (con = uprobe->consumers; con && con != uc ; con = con->next)
  764. ;
  765. if (con)
  766. ret = register_for_each_vma(uprobe, add ? uc : NULL);
  767. up_write(&uprobe->register_rwsem);
  768. put_uprobe(uprobe);
  769. return ret;
  770. }
  771. /*
  772. * uprobe_unregister - unregister a already registered probe.
  773. * @inode: the file in which the probe has to be removed.
  774. * @offset: offset from the start of the file.
  775. * @uc: identify which probe if multiple probes are colocated.
  776. */
  777. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  778. {
  779. struct uprobe *uprobe;
  780. uprobe = find_uprobe(inode, offset);
  781. if (!uprobe)
  782. return;
  783. down_write(&uprobe->register_rwsem);
  784. __uprobe_unregister(uprobe, uc);
  785. up_write(&uprobe->register_rwsem);
  786. put_uprobe(uprobe);
  787. }
  788. EXPORT_SYMBOL_GPL(uprobe_unregister);
  789. static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
  790. {
  791. struct vm_area_struct *vma;
  792. int err = 0;
  793. down_read(&mm->mmap_sem);
  794. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  795. unsigned long vaddr;
  796. loff_t offset;
  797. if (!valid_vma(vma, false) ||
  798. file_inode(vma->vm_file) != uprobe->inode)
  799. continue;
  800. offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  801. if (uprobe->offset < offset ||
  802. uprobe->offset >= offset + vma->vm_end - vma->vm_start)
  803. continue;
  804. vaddr = offset_to_vaddr(vma, uprobe->offset);
  805. err |= remove_breakpoint(uprobe, mm, vaddr);
  806. }
  807. up_read(&mm->mmap_sem);
  808. return err;
  809. }
  810. static struct rb_node *
  811. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  812. {
  813. struct rb_node *n = uprobes_tree.rb_node;
  814. while (n) {
  815. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  816. if (inode < u->inode) {
  817. n = n->rb_left;
  818. } else if (inode > u->inode) {
  819. n = n->rb_right;
  820. } else {
  821. if (max < u->offset)
  822. n = n->rb_left;
  823. else if (min > u->offset)
  824. n = n->rb_right;
  825. else
  826. break;
  827. }
  828. }
  829. return n;
  830. }
  831. /*
  832. * For a given range in vma, build a list of probes that need to be inserted.
  833. */
  834. static void build_probe_list(struct inode *inode,
  835. struct vm_area_struct *vma,
  836. unsigned long start, unsigned long end,
  837. struct list_head *head)
  838. {
  839. loff_t min, max;
  840. struct rb_node *n, *t;
  841. struct uprobe *u;
  842. INIT_LIST_HEAD(head);
  843. min = vaddr_to_offset(vma, start);
  844. max = min + (end - start) - 1;
  845. spin_lock(&uprobes_treelock);
  846. n = find_node_in_range(inode, min, max);
  847. if (n) {
  848. for (t = n; t; t = rb_prev(t)) {
  849. u = rb_entry(t, struct uprobe, rb_node);
  850. if (u->inode != inode || u->offset < min)
  851. break;
  852. list_add(&u->pending_list, head);
  853. atomic_inc(&u->ref);
  854. }
  855. for (t = n; (t = rb_next(t)); ) {
  856. u = rb_entry(t, struct uprobe, rb_node);
  857. if (u->inode != inode || u->offset > max)
  858. break;
  859. list_add(&u->pending_list, head);
  860. atomic_inc(&u->ref);
  861. }
  862. }
  863. spin_unlock(&uprobes_treelock);
  864. }
  865. /*
  866. * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
  867. *
  868. * Currently we ignore all errors and always return 0, the callers
  869. * can't handle the failure anyway.
  870. */
  871. int uprobe_mmap(struct vm_area_struct *vma)
  872. {
  873. struct list_head tmp_list;
  874. struct uprobe *uprobe, *u;
  875. struct inode *inode;
  876. if (no_uprobe_events() || !valid_vma(vma, true))
  877. return 0;
  878. inode = file_inode(vma->vm_file);
  879. if (!inode)
  880. return 0;
  881. mutex_lock(uprobes_mmap_hash(inode));
  882. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  883. /*
  884. * We can race with uprobe_unregister(), this uprobe can be already
  885. * removed. But in this case filter_chain() must return false, all
  886. * consumers have gone away.
  887. */
  888. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  889. if (!fatal_signal_pending(current) &&
  890. filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
  891. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  892. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  893. }
  894. put_uprobe(uprobe);
  895. }
  896. mutex_unlock(uprobes_mmap_hash(inode));
  897. return 0;
  898. }
  899. static bool
  900. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  901. {
  902. loff_t min, max;
  903. struct inode *inode;
  904. struct rb_node *n;
  905. inode = file_inode(vma->vm_file);
  906. min = vaddr_to_offset(vma, start);
  907. max = min + (end - start) - 1;
  908. spin_lock(&uprobes_treelock);
  909. n = find_node_in_range(inode, min, max);
  910. spin_unlock(&uprobes_treelock);
  911. return !!n;
  912. }
  913. /*
  914. * Called in context of a munmap of a vma.
  915. */
  916. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  917. {
  918. if (no_uprobe_events() || !valid_vma(vma, false))
  919. return;
  920. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  921. return;
  922. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  923. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  924. return;
  925. if (vma_has_uprobes(vma, start, end))
  926. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  927. }
  928. /* Slot allocation for XOL */
  929. static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
  930. {
  931. int ret = -EALREADY;
  932. down_write(&mm->mmap_sem);
  933. if (mm->uprobes_state.xol_area)
  934. goto fail;
  935. if (!area->vaddr) {
  936. /* Try to map as high as possible, this is only a hint. */
  937. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
  938. PAGE_SIZE, 0, 0);
  939. if (area->vaddr & ~PAGE_MASK) {
  940. ret = area->vaddr;
  941. goto fail;
  942. }
  943. }
  944. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  945. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  946. if (ret)
  947. goto fail;
  948. smp_wmb(); /* pairs with get_xol_area() */
  949. mm->uprobes_state.xol_area = area;
  950. fail:
  951. up_write(&mm->mmap_sem);
  952. return ret;
  953. }
  954. static struct xol_area *__create_xol_area(unsigned long vaddr)
  955. {
  956. struct mm_struct *mm = current->mm;
  957. uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  958. struct xol_area *area;
  959. area = kmalloc(sizeof(*area), GFP_KERNEL);
  960. if (unlikely(!area))
  961. goto out;
  962. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  963. if (!area->bitmap)
  964. goto free_area;
  965. area->page = alloc_page(GFP_HIGHUSER);
  966. if (!area->page)
  967. goto free_bitmap;
  968. area->vaddr = vaddr;
  969. init_waitqueue_head(&area->wq);
  970. /* Reserve the 1st slot for get_trampoline_vaddr() */
  971. set_bit(0, area->bitmap);
  972. atomic_set(&area->slot_count, 1);
  973. copy_to_page(area->page, 0, &insn, UPROBE_SWBP_INSN_SIZE);
  974. if (!xol_add_vma(mm, area))
  975. return area;
  976. __free_page(area->page);
  977. free_bitmap:
  978. kfree(area->bitmap);
  979. free_area:
  980. kfree(area);
  981. out:
  982. return NULL;
  983. }
  984. /*
  985. * get_xol_area - Allocate process's xol_area if necessary.
  986. * This area will be used for storing instructions for execution out of line.
  987. *
  988. * Returns the allocated area or NULL.
  989. */
  990. static struct xol_area *get_xol_area(void)
  991. {
  992. struct mm_struct *mm = current->mm;
  993. struct xol_area *area;
  994. if (!mm->uprobes_state.xol_area)
  995. __create_xol_area(0);
  996. area = mm->uprobes_state.xol_area;
  997. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  998. return area;
  999. }
  1000. /*
  1001. * uprobe_clear_state - Free the area allocated for slots.
  1002. */
  1003. void uprobe_clear_state(struct mm_struct *mm)
  1004. {
  1005. struct xol_area *area = mm->uprobes_state.xol_area;
  1006. if (!area)
  1007. return;
  1008. put_page(area->page);
  1009. kfree(area->bitmap);
  1010. kfree(area);
  1011. }
  1012. void uprobe_start_dup_mmap(void)
  1013. {
  1014. percpu_down_read(&dup_mmap_sem);
  1015. }
  1016. void uprobe_end_dup_mmap(void)
  1017. {
  1018. percpu_up_read(&dup_mmap_sem);
  1019. }
  1020. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1021. {
  1022. newmm->uprobes_state.xol_area = NULL;
  1023. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1024. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1025. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1026. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1027. }
  1028. }
  1029. /*
  1030. * - search for a free slot.
  1031. */
  1032. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1033. {
  1034. unsigned long slot_addr;
  1035. int slot_nr;
  1036. do {
  1037. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1038. if (slot_nr < UINSNS_PER_PAGE) {
  1039. if (!test_and_set_bit(slot_nr, area->bitmap))
  1040. break;
  1041. slot_nr = UINSNS_PER_PAGE;
  1042. continue;
  1043. }
  1044. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1045. } while (slot_nr >= UINSNS_PER_PAGE);
  1046. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1047. atomic_inc(&area->slot_count);
  1048. return slot_addr;
  1049. }
  1050. /*
  1051. * xol_get_insn_slot - allocate a slot for xol.
  1052. * Returns the allocated slot address or 0.
  1053. */
  1054. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1055. {
  1056. struct xol_area *area;
  1057. unsigned long xol_vaddr;
  1058. area = get_xol_area();
  1059. if (!area)
  1060. return 0;
  1061. xol_vaddr = xol_take_insn_slot(area);
  1062. if (unlikely(!xol_vaddr))
  1063. return 0;
  1064. /* Initialize the slot */
  1065. copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
  1066. /*
  1067. * We probably need flush_icache_user_range() but it needs vma.
  1068. * This should work on supported architectures too.
  1069. */
  1070. flush_dcache_page(area->page);
  1071. return xol_vaddr;
  1072. }
  1073. /*
  1074. * xol_free_insn_slot - If slot was earlier allocated by
  1075. * @xol_get_insn_slot(), make the slot available for
  1076. * subsequent requests.
  1077. */
  1078. static void xol_free_insn_slot(struct task_struct *tsk)
  1079. {
  1080. struct xol_area *area;
  1081. unsigned long vma_end;
  1082. unsigned long slot_addr;
  1083. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1084. return;
  1085. slot_addr = tsk->utask->xol_vaddr;
  1086. if (unlikely(!slot_addr))
  1087. return;
  1088. area = tsk->mm->uprobes_state.xol_area;
  1089. vma_end = area->vaddr + PAGE_SIZE;
  1090. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1091. unsigned long offset;
  1092. int slot_nr;
  1093. offset = slot_addr - area->vaddr;
  1094. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1095. if (slot_nr >= UINSNS_PER_PAGE)
  1096. return;
  1097. clear_bit(slot_nr, area->bitmap);
  1098. atomic_dec(&area->slot_count);
  1099. if (waitqueue_active(&area->wq))
  1100. wake_up(&area->wq);
  1101. tsk->utask->xol_vaddr = 0;
  1102. }
  1103. }
  1104. /**
  1105. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1106. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1107. * instruction.
  1108. * Return the address of the breakpoint instruction.
  1109. */
  1110. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1111. {
  1112. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1113. }
  1114. /*
  1115. * Called with no locks held.
  1116. * Called in context of a exiting or a exec-ing thread.
  1117. */
  1118. void uprobe_free_utask(struct task_struct *t)
  1119. {
  1120. struct uprobe_task *utask = t->utask;
  1121. struct return_instance *ri, *tmp;
  1122. if (!utask)
  1123. return;
  1124. if (utask->active_uprobe)
  1125. put_uprobe(utask->active_uprobe);
  1126. ri = utask->return_instances;
  1127. while (ri) {
  1128. tmp = ri;
  1129. ri = ri->next;
  1130. put_uprobe(tmp->uprobe);
  1131. kfree(tmp);
  1132. }
  1133. xol_free_insn_slot(t);
  1134. kfree(utask);
  1135. t->utask = NULL;
  1136. }
  1137. /*
  1138. * Allocate a uprobe_task object for the task if if necessary.
  1139. * Called when the thread hits a breakpoint.
  1140. *
  1141. * Returns:
  1142. * - pointer to new uprobe_task on success
  1143. * - NULL otherwise
  1144. */
  1145. static struct uprobe_task *get_utask(void)
  1146. {
  1147. if (!current->utask)
  1148. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1149. return current->utask;
  1150. }
  1151. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1152. {
  1153. struct uprobe_task *n_utask;
  1154. struct return_instance **p, *o, *n;
  1155. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1156. if (!n_utask)
  1157. return -ENOMEM;
  1158. t->utask = n_utask;
  1159. p = &n_utask->return_instances;
  1160. for (o = o_utask->return_instances; o; o = o->next) {
  1161. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1162. if (!n)
  1163. return -ENOMEM;
  1164. *n = *o;
  1165. atomic_inc(&n->uprobe->ref);
  1166. n->next = NULL;
  1167. *p = n;
  1168. p = &n->next;
  1169. n_utask->depth++;
  1170. }
  1171. return 0;
  1172. }
  1173. static void uprobe_warn(struct task_struct *t, const char *msg)
  1174. {
  1175. pr_warn("uprobe: %s:%d failed to %s\n",
  1176. current->comm, current->pid, msg);
  1177. }
  1178. static void dup_xol_work(struct callback_head *work)
  1179. {
  1180. kfree(work);
  1181. if (current->flags & PF_EXITING)
  1182. return;
  1183. if (!__create_xol_area(current->utask->vaddr))
  1184. uprobe_warn(current, "dup xol area");
  1185. }
  1186. /*
  1187. * Called in context of a new clone/fork from copy_process.
  1188. */
  1189. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1190. {
  1191. struct uprobe_task *utask = current->utask;
  1192. struct mm_struct *mm = current->mm;
  1193. struct callback_head *work;
  1194. struct xol_area *area;
  1195. t->utask = NULL;
  1196. if (!utask || !utask->return_instances)
  1197. return;
  1198. if (mm == t->mm && !(flags & CLONE_VFORK))
  1199. return;
  1200. if (dup_utask(t, utask))
  1201. return uprobe_warn(t, "dup ret instances");
  1202. /* The task can fork() after dup_xol_work() fails */
  1203. area = mm->uprobes_state.xol_area;
  1204. if (!area)
  1205. return uprobe_warn(t, "dup xol area");
  1206. if (mm == t->mm)
  1207. return;
  1208. /* TODO: move it into the union in uprobe_task */
  1209. work = kmalloc(sizeof(*work), GFP_KERNEL);
  1210. if (!work)
  1211. return uprobe_warn(t, "dup xol area");
  1212. utask->vaddr = area->vaddr;
  1213. init_task_work(work, dup_xol_work);
  1214. task_work_add(t, work, true);
  1215. }
  1216. /*
  1217. * Current area->vaddr notion assume the trampoline address is always
  1218. * equal area->vaddr.
  1219. *
  1220. * Returns -1 in case the xol_area is not allocated.
  1221. */
  1222. static unsigned long get_trampoline_vaddr(void)
  1223. {
  1224. struct xol_area *area;
  1225. unsigned long trampoline_vaddr = -1;
  1226. area = current->mm->uprobes_state.xol_area;
  1227. smp_read_barrier_depends();
  1228. if (area)
  1229. trampoline_vaddr = area->vaddr;
  1230. return trampoline_vaddr;
  1231. }
  1232. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1233. {
  1234. struct return_instance *ri;
  1235. struct uprobe_task *utask;
  1236. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1237. bool chained = false;
  1238. if (!get_xol_area())
  1239. return;
  1240. utask = get_utask();
  1241. if (!utask)
  1242. return;
  1243. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1244. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1245. " nestedness limit pid/tgid=%d/%d\n",
  1246. current->pid, current->tgid);
  1247. return;
  1248. }
  1249. ri = kzalloc(sizeof(struct return_instance), GFP_KERNEL);
  1250. if (!ri)
  1251. goto fail;
  1252. trampoline_vaddr = get_trampoline_vaddr();
  1253. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1254. if (orig_ret_vaddr == -1)
  1255. goto fail;
  1256. /*
  1257. * We don't want to keep trampoline address in stack, rather keep the
  1258. * original return address of first caller thru all the consequent
  1259. * instances. This also makes breakpoint unwrapping easier.
  1260. */
  1261. if (orig_ret_vaddr == trampoline_vaddr) {
  1262. if (!utask->return_instances) {
  1263. /*
  1264. * This situation is not possible. Likely we have an
  1265. * attack from user-space.
  1266. */
  1267. pr_warn("uprobe: unable to set uretprobe pid/tgid=%d/%d\n",
  1268. current->pid, current->tgid);
  1269. goto fail;
  1270. }
  1271. chained = true;
  1272. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1273. }
  1274. atomic_inc(&uprobe->ref);
  1275. ri->uprobe = uprobe;
  1276. ri->func = instruction_pointer(regs);
  1277. ri->orig_ret_vaddr = orig_ret_vaddr;
  1278. ri->chained = chained;
  1279. utask->depth++;
  1280. /* add instance to the stack */
  1281. ri->next = utask->return_instances;
  1282. utask->return_instances = ri;
  1283. return;
  1284. fail:
  1285. kfree(ri);
  1286. }
  1287. /* Prepare to single-step probed instruction out of line. */
  1288. static int
  1289. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1290. {
  1291. struct uprobe_task *utask;
  1292. unsigned long xol_vaddr;
  1293. int err;
  1294. utask = get_utask();
  1295. if (!utask)
  1296. return -ENOMEM;
  1297. xol_vaddr = xol_get_insn_slot(uprobe);
  1298. if (!xol_vaddr)
  1299. return -ENOMEM;
  1300. utask->xol_vaddr = xol_vaddr;
  1301. utask->vaddr = bp_vaddr;
  1302. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1303. if (unlikely(err)) {
  1304. xol_free_insn_slot(current);
  1305. return err;
  1306. }
  1307. utask->active_uprobe = uprobe;
  1308. utask->state = UTASK_SSTEP;
  1309. return 0;
  1310. }
  1311. /*
  1312. * If we are singlestepping, then ensure this thread is not connected to
  1313. * non-fatal signals until completion of singlestep. When xol insn itself
  1314. * triggers the signal, restart the original insn even if the task is
  1315. * already SIGKILL'ed (since coredump should report the correct ip). This
  1316. * is even more important if the task has a handler for SIGSEGV/etc, The
  1317. * _same_ instruction should be repeated again after return from the signal
  1318. * handler, and SSTEP can never finish in this case.
  1319. */
  1320. bool uprobe_deny_signal(void)
  1321. {
  1322. struct task_struct *t = current;
  1323. struct uprobe_task *utask = t->utask;
  1324. if (likely(!utask || !utask->active_uprobe))
  1325. return false;
  1326. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1327. if (signal_pending(t)) {
  1328. spin_lock_irq(&t->sighand->siglock);
  1329. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1330. spin_unlock_irq(&t->sighand->siglock);
  1331. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1332. utask->state = UTASK_SSTEP_TRAPPED;
  1333. set_tsk_thread_flag(t, TIF_UPROBE);
  1334. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1335. }
  1336. }
  1337. return true;
  1338. }
  1339. /*
  1340. * Avoid singlestepping the original instruction if the original instruction
  1341. * is a NOP or can be emulated.
  1342. */
  1343. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1344. {
  1345. if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
  1346. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1347. return true;
  1348. clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  1349. }
  1350. return false;
  1351. }
  1352. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1353. {
  1354. struct vm_area_struct *vma;
  1355. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1356. if (!valid_vma(vma, false))
  1357. continue;
  1358. /*
  1359. * This is not strictly accurate, we can race with
  1360. * uprobe_unregister() and see the already removed
  1361. * uprobe if delete_uprobe() was not yet called.
  1362. * Or this uprobe can be filtered out.
  1363. */
  1364. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1365. return;
  1366. }
  1367. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1368. }
  1369. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1370. {
  1371. struct page *page;
  1372. uprobe_opcode_t opcode;
  1373. int result;
  1374. pagefault_disable();
  1375. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1376. sizeof(opcode));
  1377. pagefault_enable();
  1378. if (likely(result == 0))
  1379. goto out;
  1380. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1381. if (result < 0)
  1382. return result;
  1383. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1384. put_page(page);
  1385. out:
  1386. /* This needs to return true for any variant of the trap insn */
  1387. return is_trap_insn(&opcode);
  1388. }
  1389. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1390. {
  1391. struct mm_struct *mm = current->mm;
  1392. struct uprobe *uprobe = NULL;
  1393. struct vm_area_struct *vma;
  1394. down_read(&mm->mmap_sem);
  1395. vma = find_vma(mm, bp_vaddr);
  1396. if (vma && vma->vm_start <= bp_vaddr) {
  1397. if (valid_vma(vma, false)) {
  1398. struct inode *inode = file_inode(vma->vm_file);
  1399. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1400. uprobe = find_uprobe(inode, offset);
  1401. }
  1402. if (!uprobe)
  1403. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1404. } else {
  1405. *is_swbp = -EFAULT;
  1406. }
  1407. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1408. mmf_recalc_uprobes(mm);
  1409. up_read(&mm->mmap_sem);
  1410. return uprobe;
  1411. }
  1412. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1413. {
  1414. struct uprobe_consumer *uc;
  1415. int remove = UPROBE_HANDLER_REMOVE;
  1416. bool need_prep = false; /* prepare return uprobe, when needed */
  1417. down_read(&uprobe->register_rwsem);
  1418. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1419. int rc = 0;
  1420. if (uc->handler) {
  1421. rc = uc->handler(uc, regs);
  1422. WARN(rc & ~UPROBE_HANDLER_MASK,
  1423. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1424. }
  1425. if (uc->ret_handler)
  1426. need_prep = true;
  1427. remove &= rc;
  1428. }
  1429. if (need_prep && !remove)
  1430. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1431. if (remove && uprobe->consumers) {
  1432. WARN_ON(!uprobe_is_active(uprobe));
  1433. unapply_uprobe(uprobe, current->mm);
  1434. }
  1435. up_read(&uprobe->register_rwsem);
  1436. }
  1437. static void
  1438. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1439. {
  1440. struct uprobe *uprobe = ri->uprobe;
  1441. struct uprobe_consumer *uc;
  1442. down_read(&uprobe->register_rwsem);
  1443. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1444. if (uc->ret_handler)
  1445. uc->ret_handler(uc, ri->func, regs);
  1446. }
  1447. up_read(&uprobe->register_rwsem);
  1448. }
  1449. static bool handle_trampoline(struct pt_regs *regs)
  1450. {
  1451. struct uprobe_task *utask;
  1452. struct return_instance *ri, *tmp;
  1453. bool chained;
  1454. utask = current->utask;
  1455. if (!utask)
  1456. return false;
  1457. ri = utask->return_instances;
  1458. if (!ri)
  1459. return false;
  1460. /*
  1461. * TODO: we should throw out return_instance's invalidated by
  1462. * longjmp(), currently we assume that the probed function always
  1463. * returns.
  1464. */
  1465. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1466. for (;;) {
  1467. handle_uretprobe_chain(ri, regs);
  1468. chained = ri->chained;
  1469. put_uprobe(ri->uprobe);
  1470. tmp = ri;
  1471. ri = ri->next;
  1472. kfree(tmp);
  1473. utask->depth--;
  1474. if (!chained)
  1475. break;
  1476. BUG_ON(!ri);
  1477. }
  1478. utask->return_instances = ri;
  1479. return true;
  1480. }
  1481. /*
  1482. * Run handler and ask thread to singlestep.
  1483. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1484. */
  1485. static void handle_swbp(struct pt_regs *regs)
  1486. {
  1487. struct uprobe *uprobe;
  1488. unsigned long bp_vaddr;
  1489. int uninitialized_var(is_swbp);
  1490. bp_vaddr = uprobe_get_swbp_addr(regs);
  1491. if (bp_vaddr == get_trampoline_vaddr()) {
  1492. if (handle_trampoline(regs))
  1493. return;
  1494. pr_warn("uprobe: unable to handle uretprobe pid/tgid=%d/%d\n",
  1495. current->pid, current->tgid);
  1496. }
  1497. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1498. if (!uprobe) {
  1499. if (is_swbp > 0) {
  1500. /* No matching uprobe; signal SIGTRAP. */
  1501. send_sig(SIGTRAP, current, 0);
  1502. } else {
  1503. /*
  1504. * Either we raced with uprobe_unregister() or we can't
  1505. * access this memory. The latter is only possible if
  1506. * another thread plays with our ->mm. In both cases
  1507. * we can simply restart. If this vma was unmapped we
  1508. * can pretend this insn was not executed yet and get
  1509. * the (correct) SIGSEGV after restart.
  1510. */
  1511. instruction_pointer_set(regs, bp_vaddr);
  1512. }
  1513. return;
  1514. }
  1515. /* change it in advance for ->handler() and restart */
  1516. instruction_pointer_set(regs, bp_vaddr);
  1517. /*
  1518. * TODO: move copy_insn/etc into _register and remove this hack.
  1519. * After we hit the bp, _unregister + _register can install the
  1520. * new and not-yet-analyzed uprobe at the same address, restart.
  1521. */
  1522. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1523. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1524. goto out;
  1525. handler_chain(uprobe, regs);
  1526. if (can_skip_sstep(uprobe, regs))
  1527. goto out;
  1528. if (!pre_ssout(uprobe, regs, bp_vaddr))
  1529. return;
  1530. /* can_skip_sstep() succeeded, or restart if can't singlestep */
  1531. out:
  1532. put_uprobe(uprobe);
  1533. }
  1534. /*
  1535. * Perform required fix-ups and disable singlestep.
  1536. * Allow pending signals to take effect.
  1537. */
  1538. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1539. {
  1540. struct uprobe *uprobe;
  1541. uprobe = utask->active_uprobe;
  1542. if (utask->state == UTASK_SSTEP_ACK)
  1543. arch_uprobe_post_xol(&uprobe->arch, regs);
  1544. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1545. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1546. else
  1547. WARN_ON_ONCE(1);
  1548. put_uprobe(uprobe);
  1549. utask->active_uprobe = NULL;
  1550. utask->state = UTASK_RUNNING;
  1551. xol_free_insn_slot(current);
  1552. spin_lock_irq(&current->sighand->siglock);
  1553. recalc_sigpending(); /* see uprobe_deny_signal() */
  1554. spin_unlock_irq(&current->sighand->siglock);
  1555. }
  1556. /*
  1557. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1558. * allows the thread to return from interrupt. After that handle_swbp()
  1559. * sets utask->active_uprobe.
  1560. *
  1561. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1562. * and allows the thread to return from interrupt.
  1563. *
  1564. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1565. * uprobe_notify_resume().
  1566. */
  1567. void uprobe_notify_resume(struct pt_regs *regs)
  1568. {
  1569. struct uprobe_task *utask;
  1570. clear_thread_flag(TIF_UPROBE);
  1571. utask = current->utask;
  1572. if (utask && utask->active_uprobe)
  1573. handle_singlestep(utask, regs);
  1574. else
  1575. handle_swbp(regs);
  1576. }
  1577. /*
  1578. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1579. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1580. */
  1581. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1582. {
  1583. if (!current->mm)
  1584. return 0;
  1585. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  1586. (!current->utask || !current->utask->return_instances))
  1587. return 0;
  1588. set_thread_flag(TIF_UPROBE);
  1589. return 1;
  1590. }
  1591. /*
  1592. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1593. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1594. */
  1595. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1596. {
  1597. struct uprobe_task *utask = current->utask;
  1598. if (!current->mm || !utask || !utask->active_uprobe)
  1599. /* task is currently not uprobed */
  1600. return 0;
  1601. utask->state = UTASK_SSTEP_ACK;
  1602. set_thread_flag(TIF_UPROBE);
  1603. return 1;
  1604. }
  1605. static struct notifier_block uprobe_exception_nb = {
  1606. .notifier_call = arch_uprobe_exception_notify,
  1607. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1608. };
  1609. static int __init init_uprobes(void)
  1610. {
  1611. int i;
  1612. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1613. mutex_init(&uprobes_mmap_mutex[i]);
  1614. if (percpu_init_rwsem(&dup_mmap_sem))
  1615. return -ENOMEM;
  1616. return register_die_notifier(&uprobe_exception_nb);
  1617. }
  1618. __initcall(init_uprobes);