uprobes.c 45 KB

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