uprobes.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. utask->depth--;
  1410. if (!chained)
  1411. break;
  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);