uprobes.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * User-space Probes (UProbes)
  4. *
  5. * Copyright (C) IBM Corporation, 2008-2012
  6. * Authors:
  7. * Srikar Dronamraju
  8. * Jim Keniston
  9. * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/highmem.h>
  13. #include <linux/pagemap.h> /* read_mapping_page */
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/mm.h>
  17. #include <linux/sched/coredump.h>
  18. #include <linux/export.h>
  19. #include <linux/rmap.h> /* anon_vma_prepare */
  20. #include <linux/mmu_notifier.h>
  21. #include <linux/swap.h> /* folio_free_swap */
  22. #include <linux/ptrace.h> /* user_enable_single_step */
  23. #include <linux/kdebug.h> /* notifier mechanism */
  24. #include <linux/percpu-rwsem.h>
  25. #include <linux/task_work.h>
  26. #include <linux/shmem_fs.h>
  27. #include <linux/khugepaged.h>
  28. #include <linux/uprobes.h>
  29. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  30. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  31. static struct rb_root uprobes_tree = RB_ROOT;
  32. /*
  33. * allows us to skip the uprobe_mmap if there are no uprobe events active
  34. * at this time. Probably a fine grained per inode count is better?
  35. */
  36. #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
  37. static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */
  38. static seqcount_rwlock_t uprobes_seqcount = SEQCNT_RWLOCK_ZERO(uprobes_seqcount, &uprobes_treelock);
  39. DEFINE_STATIC_SRCU(uprobes_srcu);
  40. #define UPROBES_HASH_SZ 13
  41. /* serialize uprobe->pending_list */
  42. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  43. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  44. DEFINE_STATIC_PERCPU_RWSEM(dup_mmap_sem);
  45. /* Have a copy of original instruction */
  46. #define UPROBE_COPY_INSN 0
  47. struct uprobe {
  48. struct rb_node rb_node; /* node in the rb tree */
  49. refcount_t ref;
  50. struct rw_semaphore register_rwsem;
  51. struct rw_semaphore consumer_rwsem;
  52. struct list_head pending_list;
  53. struct list_head consumers;
  54. struct inode *inode; /* Also hold a ref to inode */
  55. struct rcu_head rcu;
  56. loff_t offset;
  57. loff_t ref_ctr_offset;
  58. unsigned long flags;
  59. /*
  60. * The generic code assumes that it has two members of unknown type
  61. * owned by the arch-specific code:
  62. *
  63. * insn - copy_insn() saves the original instruction here for
  64. * arch_uprobe_analyze_insn().
  65. *
  66. * ixol - potentially modified instruction to execute out of
  67. * line, copied to xol_area by xol_get_insn_slot().
  68. */
  69. struct arch_uprobe arch;
  70. };
  71. struct delayed_uprobe {
  72. struct list_head list;
  73. struct uprobe *uprobe;
  74. struct mm_struct *mm;
  75. };
  76. static DEFINE_MUTEX(delayed_uprobe_lock);
  77. static LIST_HEAD(delayed_uprobe_list);
  78. /*
  79. * Execute out of line area: anonymous executable mapping installed
  80. * by the probed task to execute the copy of the original instruction
  81. * mangled by set_swbp().
  82. *
  83. * On a breakpoint hit, thread contests for a slot. It frees the
  84. * slot after singlestep. Currently a fixed number of slots are
  85. * allocated.
  86. */
  87. struct xol_area {
  88. wait_queue_head_t wq; /* if all slots are busy */
  89. atomic_t slot_count; /* number of in-use slots */
  90. unsigned long *bitmap; /* 0 = free slot */
  91. struct page *page;
  92. /*
  93. * We keep the vma's vm_start rather than a pointer to the vma
  94. * itself. The probed process or a naughty kernel module could make
  95. * the vma go away, and we must handle that reasonably gracefully.
  96. */
  97. unsigned long vaddr; /* Page(s) of instruction slots */
  98. };
  99. static void uprobe_warn(struct task_struct *t, const char *msg)
  100. {
  101. pr_warn("uprobe: %s:%d failed to %s\n", t->comm, t->pid, msg);
  102. }
  103. /*
  104. * valid_vma: Verify if the specified vma is an executable vma
  105. * Relax restrictions while unregistering: vm_flags might have
  106. * changed after breakpoint was inserted.
  107. * - is_register: indicates if we are in register context.
  108. * - Return 1 if the specified virtual address is in an
  109. * executable vma.
  110. */
  111. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  112. {
  113. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE;
  114. if (is_register)
  115. flags |= VM_WRITE;
  116. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  117. }
  118. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  119. {
  120. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  121. }
  122. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  123. {
  124. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  125. }
  126. /**
  127. * __replace_page - replace page in vma by new page.
  128. * based on replace_page in mm/ksm.c
  129. *
  130. * @vma: vma that holds the pte pointing to page
  131. * @addr: address the old @page is mapped at
  132. * @old_page: the page we are replacing by new_page
  133. * @new_page: the modified page we replace page by
  134. *
  135. * If @new_page is NULL, only unmap @old_page.
  136. *
  137. * Returns 0 on success, negative error code otherwise.
  138. */
  139. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  140. struct page *old_page, struct page *new_page)
  141. {
  142. struct folio *old_folio = page_folio(old_page);
  143. struct folio *new_folio;
  144. struct mm_struct *mm = vma->vm_mm;
  145. DEFINE_FOLIO_VMA_WALK(pvmw, old_folio, vma, addr, 0);
  146. int err;
  147. struct mmu_notifier_range range;
  148. pte_t pte;
  149. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr,
  150. addr + PAGE_SIZE);
  151. if (new_page) {
  152. new_folio = page_folio(new_page);
  153. err = mem_cgroup_charge(new_folio, vma->vm_mm, GFP_KERNEL);
  154. if (err)
  155. return err;
  156. }
  157. /* For folio_free_swap() below */
  158. folio_lock(old_folio);
  159. mmu_notifier_invalidate_range_start(&range);
  160. err = -EAGAIN;
  161. if (!page_vma_mapped_walk(&pvmw))
  162. goto unlock;
  163. VM_BUG_ON_PAGE(addr != pvmw.address, old_page);
  164. pte = ptep_get(pvmw.pte);
  165. /*
  166. * Handle PFN swap PTES, such as device-exclusive ones, that actually
  167. * map pages: simply trigger GUP again to fix it up.
  168. */
  169. if (unlikely(!pte_present(pte))) {
  170. page_vma_mapped_walk_done(&pvmw);
  171. goto unlock;
  172. }
  173. if (new_page) {
  174. folio_get(new_folio);
  175. folio_add_new_anon_rmap(new_folio, vma, addr, RMAP_EXCLUSIVE);
  176. folio_add_lru_vma(new_folio, vma);
  177. } else
  178. /* no new page, just dec_mm_counter for old_page */
  179. dec_mm_counter(mm, MM_ANONPAGES);
  180. if (!folio_test_anon(old_folio)) {
  181. dec_mm_counter(mm, mm_counter_file(old_folio));
  182. inc_mm_counter(mm, MM_ANONPAGES);
  183. }
  184. flush_cache_page(vma, addr, pte_pfn(pte));
  185. ptep_clear_flush(vma, addr, pvmw.pte);
  186. if (new_page)
  187. set_pte_at(mm, addr, pvmw.pte,
  188. mk_pte(new_page, vma->vm_page_prot));
  189. folio_remove_rmap_pte(old_folio, old_page, vma);
  190. if (!folio_mapped(old_folio))
  191. folio_free_swap(old_folio);
  192. page_vma_mapped_walk_done(&pvmw);
  193. folio_put(old_folio);
  194. err = 0;
  195. unlock:
  196. mmu_notifier_invalidate_range_end(&range);
  197. folio_unlock(old_folio);
  198. return err;
  199. }
  200. /**
  201. * is_swbp_insn - check if instruction is breakpoint instruction.
  202. * @insn: instruction to be checked.
  203. * Default implementation of is_swbp_insn
  204. * Returns true if @insn is a breakpoint instruction.
  205. */
  206. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  207. {
  208. return *insn == UPROBE_SWBP_INSN;
  209. }
  210. /**
  211. * is_trap_insn - check if instruction is breakpoint instruction.
  212. * @insn: instruction to be checked.
  213. * Default implementation of is_trap_insn
  214. * Returns true if @insn is a breakpoint instruction.
  215. *
  216. * This function is needed for the case where an architecture has multiple
  217. * trap instructions (like powerpc).
  218. */
  219. bool __weak is_trap_insn(uprobe_opcode_t *insn)
  220. {
  221. return is_swbp_insn(insn);
  222. }
  223. static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
  224. {
  225. void *kaddr = kmap_atomic(page);
  226. memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
  227. kunmap_atomic(kaddr);
  228. }
  229. static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
  230. {
  231. void *kaddr = kmap_atomic(page);
  232. memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
  233. kunmap_atomic(kaddr);
  234. }
  235. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  236. {
  237. uprobe_opcode_t old_opcode;
  238. bool is_swbp;
  239. /*
  240. * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
  241. * We do not check if it is any other 'trap variant' which could
  242. * be conditional trap instruction such as the one powerpc supports.
  243. *
  244. * The logic is that we do not care if the underlying instruction
  245. * is a trap variant; uprobes always wins over any other (gdb)
  246. * breakpoint.
  247. */
  248. copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
  249. is_swbp = is_swbp_insn(&old_opcode);
  250. if (is_swbp_insn(new_opcode)) {
  251. if (is_swbp) /* register: already installed? */
  252. return 0;
  253. } else {
  254. if (!is_swbp) /* unregister: was it changed by us? */
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static struct delayed_uprobe *
  260. delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm)
  261. {
  262. struct delayed_uprobe *du;
  263. list_for_each_entry(du, &delayed_uprobe_list, list)
  264. if (du->uprobe == uprobe && du->mm == mm)
  265. return du;
  266. return NULL;
  267. }
  268. static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm)
  269. {
  270. struct delayed_uprobe *du;
  271. if (delayed_uprobe_check(uprobe, mm))
  272. return 0;
  273. du = kzalloc(sizeof(*du), GFP_KERNEL);
  274. if (!du)
  275. return -ENOMEM;
  276. du->uprobe = uprobe;
  277. du->mm = mm;
  278. list_add(&du->list, &delayed_uprobe_list);
  279. return 0;
  280. }
  281. static void delayed_uprobe_delete(struct delayed_uprobe *du)
  282. {
  283. if (WARN_ON(!du))
  284. return;
  285. list_del(&du->list);
  286. kfree(du);
  287. }
  288. static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm)
  289. {
  290. struct list_head *pos, *q;
  291. struct delayed_uprobe *du;
  292. if (!uprobe && !mm)
  293. return;
  294. list_for_each_safe(pos, q, &delayed_uprobe_list) {
  295. du = list_entry(pos, struct delayed_uprobe, list);
  296. if (uprobe && du->uprobe != uprobe)
  297. continue;
  298. if (mm && du->mm != mm)
  299. continue;
  300. delayed_uprobe_delete(du);
  301. }
  302. }
  303. static bool valid_ref_ctr_vma(struct uprobe *uprobe,
  304. struct vm_area_struct *vma)
  305. {
  306. unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset);
  307. return uprobe->ref_ctr_offset &&
  308. vma->vm_file &&
  309. file_inode(vma->vm_file) == uprobe->inode &&
  310. (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
  311. vma->vm_start <= vaddr &&
  312. vma->vm_end > vaddr;
  313. }
  314. static struct vm_area_struct *
  315. find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm)
  316. {
  317. VMA_ITERATOR(vmi, mm, 0);
  318. struct vm_area_struct *tmp;
  319. for_each_vma(vmi, tmp)
  320. if (valid_ref_ctr_vma(uprobe, tmp))
  321. return tmp;
  322. return NULL;
  323. }
  324. static int
  325. __update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d)
  326. {
  327. void *kaddr;
  328. struct page *page;
  329. int ret;
  330. short *ptr;
  331. if (!vaddr || !d)
  332. return -EINVAL;
  333. ret = get_user_pages_remote(mm, vaddr, 1,
  334. FOLL_WRITE, &page, NULL);
  335. if (unlikely(ret <= 0)) {
  336. /*
  337. * We are asking for 1 page. If get_user_pages_remote() fails,
  338. * it may return 0, in that case we have to return error.
  339. */
  340. return ret == 0 ? -EBUSY : ret;
  341. }
  342. kaddr = kmap_atomic(page);
  343. ptr = kaddr + (vaddr & ~PAGE_MASK);
  344. if (unlikely(*ptr + d < 0)) {
  345. pr_warn("ref_ctr going negative. vaddr: 0x%lx, "
  346. "curr val: %d, delta: %d\n", vaddr, *ptr, d);
  347. ret = -EINVAL;
  348. goto out;
  349. }
  350. *ptr += d;
  351. ret = 0;
  352. out:
  353. kunmap_atomic(kaddr);
  354. put_page(page);
  355. return ret;
  356. }
  357. static void update_ref_ctr_warn(struct uprobe *uprobe,
  358. struct mm_struct *mm, short d)
  359. {
  360. pr_warn("ref_ctr %s failed for inode: 0x%lx offset: "
  361. "0x%llx ref_ctr_offset: 0x%llx of mm: 0x%pK\n",
  362. d > 0 ? "increment" : "decrement", uprobe->inode->i_ino,
  363. (unsigned long long) uprobe->offset,
  364. (unsigned long long) uprobe->ref_ctr_offset, mm);
  365. }
  366. static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm,
  367. short d)
  368. {
  369. struct vm_area_struct *rc_vma;
  370. unsigned long rc_vaddr;
  371. int ret = 0;
  372. rc_vma = find_ref_ctr_vma(uprobe, mm);
  373. if (rc_vma) {
  374. rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset);
  375. ret = __update_ref_ctr(mm, rc_vaddr, d);
  376. if (ret)
  377. update_ref_ctr_warn(uprobe, mm, d);
  378. if (d > 0)
  379. return ret;
  380. }
  381. mutex_lock(&delayed_uprobe_lock);
  382. if (d > 0)
  383. ret = delayed_uprobe_add(uprobe, mm);
  384. else
  385. delayed_uprobe_remove(uprobe, mm);
  386. mutex_unlock(&delayed_uprobe_lock);
  387. return ret;
  388. }
  389. /*
  390. * NOTE:
  391. * Expect the breakpoint instruction to be the smallest size instruction for
  392. * the architecture. If an arch has variable length instruction and the
  393. * breakpoint instruction is not of the smallest length instruction
  394. * supported by that architecture then we need to modify is_trap_at_addr and
  395. * uprobe_write_opcode accordingly. This would never be a problem for archs
  396. * that have fixed length instructions.
  397. *
  398. * uprobe_write_opcode - write the opcode at a given virtual address.
  399. * @auprobe: arch specific probepoint information.
  400. * @mm: the probed process address space.
  401. * @vaddr: the virtual address to store the opcode.
  402. * @opcode: opcode to be written at @vaddr.
  403. *
  404. * Called with mm->mmap_lock held for read or write.
  405. * Return 0 (success) or a negative errno.
  406. */
  407. int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
  408. unsigned long vaddr, uprobe_opcode_t opcode)
  409. {
  410. struct uprobe *uprobe;
  411. struct page *old_page, *new_page;
  412. struct vm_area_struct *vma;
  413. int ret, is_register, ref_ctr_updated = 0;
  414. bool orig_page_huge = false;
  415. unsigned int gup_flags = FOLL_FORCE;
  416. is_register = is_swbp_insn(&opcode);
  417. uprobe = container_of(auprobe, struct uprobe, arch);
  418. retry:
  419. if (is_register)
  420. gup_flags |= FOLL_SPLIT_PMD;
  421. /* Read the page with vaddr into memory */
  422. old_page = get_user_page_vma_remote(mm, vaddr, gup_flags, &vma);
  423. if (IS_ERR(old_page))
  424. return PTR_ERR(old_page);
  425. ret = verify_opcode(old_page, vaddr, &opcode);
  426. if (ret <= 0)
  427. goto put_old;
  428. if (is_zero_page(old_page)) {
  429. ret = -EINVAL;
  430. goto put_old;
  431. }
  432. if (WARN(!is_register && PageCompound(old_page),
  433. "uprobe unregister should never work on compound page\n")) {
  434. ret = -EINVAL;
  435. goto put_old;
  436. }
  437. /* We are going to replace instruction, update ref_ctr. */
  438. if (!ref_ctr_updated && uprobe->ref_ctr_offset) {
  439. ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1);
  440. if (ret)
  441. goto put_old;
  442. ref_ctr_updated = 1;
  443. }
  444. ret = 0;
  445. if (!is_register && !PageAnon(old_page))
  446. goto put_old;
  447. ret = anon_vma_prepare(vma);
  448. if (ret)
  449. goto put_old;
  450. ret = -ENOMEM;
  451. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  452. if (!new_page)
  453. goto put_old;
  454. __SetPageUptodate(new_page);
  455. copy_highpage(new_page, old_page);
  456. copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  457. if (!is_register) {
  458. struct page *orig_page;
  459. pgoff_t index;
  460. VM_BUG_ON_PAGE(!PageAnon(old_page), old_page);
  461. index = vaddr_to_offset(vma, vaddr & PAGE_MASK) >> PAGE_SHIFT;
  462. orig_page = find_get_page(vma->vm_file->f_inode->i_mapping,
  463. index);
  464. if (orig_page) {
  465. if (PageUptodate(orig_page) &&
  466. pages_identical(new_page, orig_page)) {
  467. /* let go new_page */
  468. put_page(new_page);
  469. new_page = NULL;
  470. if (PageCompound(orig_page))
  471. orig_page_huge = true;
  472. }
  473. put_page(orig_page);
  474. }
  475. }
  476. ret = __replace_page(vma, vaddr & PAGE_MASK, old_page, new_page);
  477. if (new_page)
  478. put_page(new_page);
  479. put_old:
  480. put_page(old_page);
  481. if (unlikely(ret == -EAGAIN))
  482. goto retry;
  483. /* Revert back reference counter if instruction update failed. */
  484. if (ret && is_register && ref_ctr_updated)
  485. update_ref_ctr(uprobe, mm, -1);
  486. /* try collapse pmd for compound page */
  487. if (!ret && orig_page_huge)
  488. collapse_pte_mapped_thp(mm, vaddr, false);
  489. return ret;
  490. }
  491. /**
  492. * set_swbp - store breakpoint at a given address.
  493. * @auprobe: arch specific probepoint information.
  494. * @mm: the probed process address space.
  495. * @vaddr: the virtual address to insert the opcode.
  496. *
  497. * For mm @mm, store the breakpoint instruction at @vaddr.
  498. * Return 0 (success) or a negative errno.
  499. */
  500. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  501. {
  502. return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
  503. }
  504. /**
  505. * set_orig_insn - Restore the original instruction.
  506. * @mm: the probed process address space.
  507. * @auprobe: arch specific probepoint information.
  508. * @vaddr: the virtual address to insert the opcode.
  509. *
  510. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  511. * Return 0 (success) or a negative errno.
  512. */
  513. int __weak
  514. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  515. {
  516. return uprobe_write_opcode(auprobe, mm, vaddr,
  517. *(uprobe_opcode_t *)&auprobe->insn);
  518. }
  519. /* uprobe should have guaranteed positive refcount */
  520. static struct uprobe *get_uprobe(struct uprobe *uprobe)
  521. {
  522. refcount_inc(&uprobe->ref);
  523. return uprobe;
  524. }
  525. /*
  526. * uprobe should have guaranteed lifetime, which can be either of:
  527. * - caller already has refcount taken (and wants an extra one);
  528. * - uprobe is RCU protected and won't be freed until after grace period;
  529. * - we are holding uprobes_treelock (for read or write, doesn't matter).
  530. */
  531. static struct uprobe *try_get_uprobe(struct uprobe *uprobe)
  532. {
  533. if (refcount_inc_not_zero(&uprobe->ref))
  534. return uprobe;
  535. return NULL;
  536. }
  537. static inline bool uprobe_is_active(struct uprobe *uprobe)
  538. {
  539. return !RB_EMPTY_NODE(&uprobe->rb_node);
  540. }
  541. static void uprobe_free_rcu(struct rcu_head *rcu)
  542. {
  543. struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu);
  544. kfree(uprobe);
  545. }
  546. static void put_uprobe(struct uprobe *uprobe)
  547. {
  548. if (!refcount_dec_and_test(&uprobe->ref))
  549. return;
  550. write_lock(&uprobes_treelock);
  551. if (uprobe_is_active(uprobe)) {
  552. write_seqcount_begin(&uprobes_seqcount);
  553. rb_erase(&uprobe->rb_node, &uprobes_tree);
  554. write_seqcount_end(&uprobes_seqcount);
  555. }
  556. write_unlock(&uprobes_treelock);
  557. /*
  558. * If application munmap(exec_vma) before uprobe_unregister()
  559. * gets called, we don't get a chance to remove uprobe from
  560. * delayed_uprobe_list from remove_breakpoint(). Do it here.
  561. */
  562. mutex_lock(&delayed_uprobe_lock);
  563. delayed_uprobe_remove(uprobe, NULL);
  564. mutex_unlock(&delayed_uprobe_lock);
  565. call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_rcu);
  566. }
  567. static __always_inline
  568. int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset,
  569. const struct uprobe *r)
  570. {
  571. if (l_inode < r->inode)
  572. return -1;
  573. if (l_inode > r->inode)
  574. return 1;
  575. if (l_offset < r->offset)
  576. return -1;
  577. if (l_offset > r->offset)
  578. return 1;
  579. return 0;
  580. }
  581. #define __node_2_uprobe(node) \
  582. rb_entry((node), struct uprobe, rb_node)
  583. struct __uprobe_key {
  584. struct inode *inode;
  585. loff_t offset;
  586. };
  587. static inline int __uprobe_cmp_key(const void *key, const struct rb_node *b)
  588. {
  589. const struct __uprobe_key *a = key;
  590. return uprobe_cmp(a->inode, a->offset, __node_2_uprobe(b));
  591. }
  592. static inline int __uprobe_cmp(struct rb_node *a, const struct rb_node *b)
  593. {
  594. struct uprobe *u = __node_2_uprobe(a);
  595. return uprobe_cmp(u->inode, u->offset, __node_2_uprobe(b));
  596. }
  597. /*
  598. * Assumes being inside RCU protected region.
  599. * No refcount is taken on returned uprobe.
  600. */
  601. static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset)
  602. {
  603. struct __uprobe_key key = {
  604. .inode = inode,
  605. .offset = offset,
  606. };
  607. struct rb_node *node;
  608. unsigned int seq;
  609. lockdep_assert(srcu_read_lock_held(&uprobes_srcu));
  610. do {
  611. seq = read_seqcount_begin(&uprobes_seqcount);
  612. node = rb_find_rcu(&key, &uprobes_tree, __uprobe_cmp_key);
  613. /*
  614. * Lockless RB-tree lookups can result only in false negatives.
  615. * If the element is found, it is correct and can be returned
  616. * under RCU protection. If we find nothing, we need to
  617. * validate that seqcount didn't change. If it did, we have to
  618. * try again as we might have missed the element (false
  619. * negative). If seqcount is unchanged, search truly failed.
  620. */
  621. if (node)
  622. return __node_2_uprobe(node);
  623. } while (read_seqcount_retry(&uprobes_seqcount, seq));
  624. return NULL;
  625. }
  626. /*
  627. * Attempt to insert a new uprobe into uprobes_tree.
  628. *
  629. * If uprobe already exists (for given inode+offset), we just increment
  630. * refcount of previously existing uprobe.
  631. *
  632. * If not, a provided new instance of uprobe is inserted into the tree (with
  633. * assumed initial refcount == 1).
  634. *
  635. * In any case, we return a uprobe instance that ends up being in uprobes_tree.
  636. * Caller has to clean up new uprobe instance, if it ended up not being
  637. * inserted into the tree.
  638. *
  639. * We assume that uprobes_treelock is held for writing.
  640. */
  641. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  642. {
  643. struct rb_node *node;
  644. again:
  645. node = rb_find_add_rcu(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp);
  646. if (node) {
  647. struct uprobe *u = __node_2_uprobe(node);
  648. if (!try_get_uprobe(u)) {
  649. rb_erase(node, &uprobes_tree);
  650. RB_CLEAR_NODE(&u->rb_node);
  651. goto again;
  652. }
  653. return u;
  654. }
  655. return uprobe;
  656. }
  657. /*
  658. * Acquire uprobes_treelock and insert uprobe into uprobes_tree
  659. * (or reuse existing one, see __insert_uprobe() comments above).
  660. */
  661. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  662. {
  663. struct uprobe *u;
  664. write_lock(&uprobes_treelock);
  665. write_seqcount_begin(&uprobes_seqcount);
  666. u = __insert_uprobe(uprobe);
  667. write_seqcount_end(&uprobes_seqcount);
  668. write_unlock(&uprobes_treelock);
  669. return u;
  670. }
  671. static void
  672. ref_ctr_mismatch_warn(struct uprobe *cur_uprobe, struct uprobe *uprobe)
  673. {
  674. pr_warn("ref_ctr_offset mismatch. inode: 0x%lx offset: 0x%llx "
  675. "ref_ctr_offset(old): 0x%llx ref_ctr_offset(new): 0x%llx\n",
  676. uprobe->inode->i_ino, (unsigned long long) uprobe->offset,
  677. (unsigned long long) cur_uprobe->ref_ctr_offset,
  678. (unsigned long long) uprobe->ref_ctr_offset);
  679. }
  680. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset,
  681. loff_t ref_ctr_offset)
  682. {
  683. struct uprobe *uprobe, *cur_uprobe;
  684. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  685. if (!uprobe)
  686. return ERR_PTR(-ENOMEM);
  687. uprobe->inode = inode;
  688. uprobe->offset = offset;
  689. uprobe->ref_ctr_offset = ref_ctr_offset;
  690. INIT_LIST_HEAD(&uprobe->consumers);
  691. init_rwsem(&uprobe->register_rwsem);
  692. init_rwsem(&uprobe->consumer_rwsem);
  693. RB_CLEAR_NODE(&uprobe->rb_node);
  694. refcount_set(&uprobe->ref, 1);
  695. /* add to uprobes_tree, sorted on inode:offset */
  696. cur_uprobe = insert_uprobe(uprobe);
  697. /* a uprobe exists for this inode:offset combination */
  698. if (cur_uprobe != uprobe) {
  699. if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) {
  700. ref_ctr_mismatch_warn(cur_uprobe, uprobe);
  701. put_uprobe(cur_uprobe);
  702. kfree(uprobe);
  703. return ERR_PTR(-EINVAL);
  704. }
  705. kfree(uprobe);
  706. uprobe = cur_uprobe;
  707. }
  708. return uprobe;
  709. }
  710. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  711. {
  712. down_write(&uprobe->consumer_rwsem);
  713. list_add_rcu(&uc->cons_node, &uprobe->consumers);
  714. up_write(&uprobe->consumer_rwsem);
  715. }
  716. /*
  717. * For uprobe @uprobe, delete the consumer @uc.
  718. * Should never be called with consumer that's not part of @uprobe->consumers.
  719. */
  720. static void consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  721. {
  722. down_write(&uprobe->consumer_rwsem);
  723. list_del_rcu(&uc->cons_node);
  724. up_write(&uprobe->consumer_rwsem);
  725. }
  726. static int __copy_insn(struct address_space *mapping, struct file *filp,
  727. void *insn, int nbytes, loff_t offset)
  728. {
  729. struct page *page;
  730. /*
  731. * Ensure that the page that has the original instruction is populated
  732. * and in page-cache. If ->read_folio == NULL it must be shmem_mapping(),
  733. * see uprobe_register().
  734. */
  735. if (mapping->a_ops->read_folio)
  736. page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp);
  737. else
  738. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  739. if (IS_ERR(page))
  740. return PTR_ERR(page);
  741. copy_from_page(page, offset, insn, nbytes);
  742. put_page(page);
  743. return 0;
  744. }
  745. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  746. {
  747. struct address_space *mapping = uprobe->inode->i_mapping;
  748. loff_t offs = uprobe->offset;
  749. void *insn = &uprobe->arch.insn;
  750. int size = sizeof(uprobe->arch.insn);
  751. int len, err = -EIO;
  752. /* Copy only available bytes, -EIO if nothing was read */
  753. do {
  754. if (offs >= i_size_read(uprobe->inode))
  755. break;
  756. len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
  757. err = __copy_insn(mapping, filp, insn, len, offs);
  758. if (err)
  759. break;
  760. insn += len;
  761. offs += len;
  762. size -= len;
  763. } while (size);
  764. return err;
  765. }
  766. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  767. struct mm_struct *mm, unsigned long vaddr)
  768. {
  769. int ret = 0;
  770. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  771. return ret;
  772. /* TODO: move this into _register, until then we abuse this sem. */
  773. down_write(&uprobe->consumer_rwsem);
  774. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  775. goto out;
  776. ret = copy_insn(uprobe, file);
  777. if (ret)
  778. goto out;
  779. ret = -ENOTSUPP;
  780. if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn))
  781. goto out;
  782. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  783. if (ret)
  784. goto out;
  785. smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
  786. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  787. out:
  788. up_write(&uprobe->consumer_rwsem);
  789. return ret;
  790. }
  791. static inline bool consumer_filter(struct uprobe_consumer *uc, struct mm_struct *mm)
  792. {
  793. return !uc->filter || uc->filter(uc, mm);
  794. }
  795. static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
  796. {
  797. struct uprobe_consumer *uc;
  798. bool ret = false;
  799. down_read(&uprobe->consumer_rwsem);
  800. list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
  801. srcu_read_lock_held(&uprobes_srcu)) {
  802. ret = consumer_filter(uc, mm);
  803. if (ret)
  804. break;
  805. }
  806. up_read(&uprobe->consumer_rwsem);
  807. return ret;
  808. }
  809. static int
  810. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  811. struct vm_area_struct *vma, unsigned long vaddr)
  812. {
  813. bool first_uprobe;
  814. int ret;
  815. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  816. if (ret)
  817. return ret;
  818. /*
  819. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  820. * the task can hit this breakpoint right after __replace_page().
  821. */
  822. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  823. if (first_uprobe)
  824. set_bit(MMF_HAS_UPROBES, &mm->flags);
  825. ret = set_swbp(&uprobe->arch, mm, vaddr);
  826. if (!ret)
  827. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  828. else if (first_uprobe)
  829. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  830. return ret;
  831. }
  832. static int
  833. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  834. {
  835. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  836. return set_orig_insn(&uprobe->arch, mm, vaddr);
  837. }
  838. struct map_info {
  839. struct map_info *next;
  840. struct mm_struct *mm;
  841. unsigned long vaddr;
  842. };
  843. static inline struct map_info *free_map_info(struct map_info *info)
  844. {
  845. struct map_info *next = info->next;
  846. kfree(info);
  847. return next;
  848. }
  849. static struct map_info *
  850. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  851. {
  852. unsigned long pgoff = offset >> PAGE_SHIFT;
  853. struct vm_area_struct *vma;
  854. struct map_info *curr = NULL;
  855. struct map_info *prev = NULL;
  856. struct map_info *info;
  857. int more = 0;
  858. again:
  859. i_mmap_lock_read(mapping);
  860. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  861. if (!valid_vma(vma, is_register))
  862. continue;
  863. if (!prev && !more) {
  864. /*
  865. * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through
  866. * reclaim. This is optimistic, no harm done if it fails.
  867. */
  868. prev = kmalloc(sizeof(struct map_info),
  869. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  870. if (prev)
  871. prev->next = NULL;
  872. }
  873. if (!prev) {
  874. more++;
  875. continue;
  876. }
  877. if (!mmget_not_zero(vma->vm_mm))
  878. continue;
  879. info = prev;
  880. prev = prev->next;
  881. info->next = curr;
  882. curr = info;
  883. info->mm = vma->vm_mm;
  884. info->vaddr = offset_to_vaddr(vma, offset);
  885. }
  886. i_mmap_unlock_read(mapping);
  887. if (!more)
  888. goto out;
  889. prev = curr;
  890. while (curr) {
  891. mmput(curr->mm);
  892. curr = curr->next;
  893. }
  894. do {
  895. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  896. if (!info) {
  897. curr = ERR_PTR(-ENOMEM);
  898. goto out;
  899. }
  900. info->next = prev;
  901. prev = info;
  902. } while (--more);
  903. goto again;
  904. out:
  905. while (prev)
  906. prev = free_map_info(prev);
  907. return curr;
  908. }
  909. static int
  910. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  911. {
  912. bool is_register = !!new;
  913. struct map_info *info;
  914. int err = 0;
  915. percpu_down_write(&dup_mmap_sem);
  916. info = build_map_info(uprobe->inode->i_mapping,
  917. uprobe->offset, is_register);
  918. if (IS_ERR(info)) {
  919. err = PTR_ERR(info);
  920. goto out;
  921. }
  922. while (info) {
  923. struct mm_struct *mm = info->mm;
  924. struct vm_area_struct *vma;
  925. if (err && is_register)
  926. goto free;
  927. /*
  928. * We take mmap_lock for writing to avoid the race with
  929. * find_active_uprobe_rcu() which takes mmap_lock for reading.
  930. * Thus this install_breakpoint() can not make
  931. * is_trap_at_addr() true right after find_uprobe_rcu()
  932. * returns NULL in find_active_uprobe_rcu().
  933. */
  934. mmap_write_lock(mm);
  935. vma = find_vma(mm, info->vaddr);
  936. if (!vma || !valid_vma(vma, is_register) ||
  937. file_inode(vma->vm_file) != uprobe->inode)
  938. goto unlock;
  939. if (vma->vm_start > info->vaddr ||
  940. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  941. goto unlock;
  942. if (is_register) {
  943. /* consult only the "caller", new consumer. */
  944. if (consumer_filter(new, mm))
  945. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  946. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  947. if (!filter_chain(uprobe, mm))
  948. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  949. }
  950. unlock:
  951. mmap_write_unlock(mm);
  952. free:
  953. mmput(mm);
  954. info = free_map_info(info);
  955. }
  956. out:
  957. percpu_up_write(&dup_mmap_sem);
  958. return err;
  959. }
  960. /**
  961. * uprobe_unregister_nosync - unregister an already registered probe.
  962. * @uprobe: uprobe to remove
  963. * @uc: identify which probe if multiple probes are colocated.
  964. */
  965. void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
  966. {
  967. int err;
  968. down_write(&uprobe->register_rwsem);
  969. consumer_del(uprobe, uc);
  970. err = register_for_each_vma(uprobe, NULL);
  971. up_write(&uprobe->register_rwsem);
  972. /* TODO : cant unregister? schedule a worker thread */
  973. if (unlikely(err)) {
  974. uprobe_warn(current, "unregister, leaking uprobe");
  975. return;
  976. }
  977. put_uprobe(uprobe);
  978. }
  979. EXPORT_SYMBOL_GPL(uprobe_unregister_nosync);
  980. void uprobe_unregister_sync(void)
  981. {
  982. /*
  983. * Now that handler_chain() and handle_uretprobe_chain() iterate over
  984. * uprobe->consumers list under RCU protection without holding
  985. * uprobe->register_rwsem, we need to wait for RCU grace period to
  986. * make sure that we can't call into just unregistered
  987. * uprobe_consumer's callbacks anymore. If we don't do that, fast and
  988. * unlucky enough caller can free consumer's memory and cause
  989. * handler_chain() or handle_uretprobe_chain() to do an use-after-free.
  990. */
  991. synchronize_srcu(&uprobes_srcu);
  992. }
  993. EXPORT_SYMBOL_GPL(uprobe_unregister_sync);
  994. /**
  995. * uprobe_register - register a probe
  996. * @inode: the file in which the probe has to be placed.
  997. * @offset: offset from the start of the file.
  998. * @ref_ctr_offset: offset of SDT marker / reference counter
  999. * @uc: information on howto handle the probe..
  1000. *
  1001. * Apart from the access refcount, uprobe_register() takes a creation
  1002. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  1003. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  1004. * tuple). Creation refcount stops uprobe_unregister from freeing the
  1005. * @uprobe even before the register operation is complete. Creation
  1006. * refcount is released when the last @uc for the @uprobe
  1007. * unregisters. Caller of uprobe_register() is required to keep @inode
  1008. * (and the containing mount) referenced.
  1009. *
  1010. * Return: pointer to the new uprobe on success or an ERR_PTR on failure.
  1011. */
  1012. struct uprobe *uprobe_register(struct inode *inode,
  1013. loff_t offset, loff_t ref_ctr_offset,
  1014. struct uprobe_consumer *uc)
  1015. {
  1016. struct uprobe *uprobe;
  1017. int ret;
  1018. /* Uprobe must have at least one set consumer */
  1019. if (!uc->handler && !uc->ret_handler)
  1020. return ERR_PTR(-EINVAL);
  1021. /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */
  1022. if (!inode->i_mapping->a_ops->read_folio &&
  1023. !shmem_mapping(inode->i_mapping))
  1024. return ERR_PTR(-EIO);
  1025. /* Racy, just to catch the obvious mistakes */
  1026. if (offset > i_size_read(inode))
  1027. return ERR_PTR(-EINVAL);
  1028. /*
  1029. * This ensures that copy_from_page(), copy_to_page() and
  1030. * __update_ref_ctr() can't cross page boundary.
  1031. */
  1032. if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE))
  1033. return ERR_PTR(-EINVAL);
  1034. if (!IS_ALIGNED(ref_ctr_offset, sizeof(short)))
  1035. return ERR_PTR(-EINVAL);
  1036. uprobe = alloc_uprobe(inode, offset, ref_ctr_offset);
  1037. if (IS_ERR(uprobe))
  1038. return uprobe;
  1039. down_write(&uprobe->register_rwsem);
  1040. consumer_add(uprobe, uc);
  1041. ret = register_for_each_vma(uprobe, uc);
  1042. up_write(&uprobe->register_rwsem);
  1043. if (ret) {
  1044. uprobe_unregister_nosync(uprobe, uc);
  1045. /*
  1046. * Registration might have partially succeeded, so we can have
  1047. * this consumer being called right at this time. We need to
  1048. * sync here. It's ok, it's unlikely slow path.
  1049. */
  1050. uprobe_unregister_sync();
  1051. return ERR_PTR(ret);
  1052. }
  1053. return uprobe;
  1054. }
  1055. EXPORT_SYMBOL_GPL(uprobe_register);
  1056. /**
  1057. * uprobe_apply - add or remove the breakpoints according to @uc->filter
  1058. * @uprobe: uprobe which "owns" the breakpoint
  1059. * @uc: consumer which wants to add more or remove some breakpoints
  1060. * @add: add or remove the breakpoints
  1061. * Return: 0 on success or negative error code.
  1062. */
  1063. int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool add)
  1064. {
  1065. struct uprobe_consumer *con;
  1066. int ret = -ENOENT, srcu_idx;
  1067. down_write(&uprobe->register_rwsem);
  1068. srcu_idx = srcu_read_lock(&uprobes_srcu);
  1069. list_for_each_entry_srcu(con, &uprobe->consumers, cons_node,
  1070. srcu_read_lock_held(&uprobes_srcu)) {
  1071. if (con == uc) {
  1072. ret = register_for_each_vma(uprobe, add ? uc : NULL);
  1073. break;
  1074. }
  1075. }
  1076. srcu_read_unlock(&uprobes_srcu, srcu_idx);
  1077. up_write(&uprobe->register_rwsem);
  1078. return ret;
  1079. }
  1080. static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
  1081. {
  1082. VMA_ITERATOR(vmi, mm, 0);
  1083. struct vm_area_struct *vma;
  1084. int err = 0;
  1085. mmap_read_lock(mm);
  1086. for_each_vma(vmi, vma) {
  1087. unsigned long vaddr;
  1088. loff_t offset;
  1089. if (!valid_vma(vma, false) ||
  1090. file_inode(vma->vm_file) != uprobe->inode)
  1091. continue;
  1092. offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  1093. if (uprobe->offset < offset ||
  1094. uprobe->offset >= offset + vma->vm_end - vma->vm_start)
  1095. continue;
  1096. vaddr = offset_to_vaddr(vma, uprobe->offset);
  1097. err |= remove_breakpoint(uprobe, mm, vaddr);
  1098. }
  1099. mmap_read_unlock(mm);
  1100. return err;
  1101. }
  1102. static struct rb_node *
  1103. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  1104. {
  1105. struct rb_node *n = uprobes_tree.rb_node;
  1106. while (n) {
  1107. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  1108. if (inode < u->inode) {
  1109. n = n->rb_left;
  1110. } else if (inode > u->inode) {
  1111. n = n->rb_right;
  1112. } else {
  1113. if (max < u->offset)
  1114. n = n->rb_left;
  1115. else if (min > u->offset)
  1116. n = n->rb_right;
  1117. else
  1118. break;
  1119. }
  1120. }
  1121. return n;
  1122. }
  1123. /*
  1124. * For a given range in vma, build a list of probes that need to be inserted.
  1125. */
  1126. static void build_probe_list(struct inode *inode,
  1127. struct vm_area_struct *vma,
  1128. unsigned long start, unsigned long end,
  1129. struct list_head *head)
  1130. {
  1131. loff_t min, max;
  1132. struct rb_node *n, *t;
  1133. struct uprobe *u;
  1134. INIT_LIST_HEAD(head);
  1135. min = vaddr_to_offset(vma, start);
  1136. max = min + (end - start) - 1;
  1137. read_lock(&uprobes_treelock);
  1138. n = find_node_in_range(inode, min, max);
  1139. if (n) {
  1140. for (t = n; t; t = rb_prev(t)) {
  1141. u = rb_entry(t, struct uprobe, rb_node);
  1142. if (u->inode != inode || u->offset < min)
  1143. break;
  1144. /* if uprobe went away, it's safe to ignore it */
  1145. if (try_get_uprobe(u))
  1146. list_add(&u->pending_list, head);
  1147. }
  1148. for (t = n; (t = rb_next(t)); ) {
  1149. u = rb_entry(t, struct uprobe, rb_node);
  1150. if (u->inode != inode || u->offset > max)
  1151. break;
  1152. /* if uprobe went away, it's safe to ignore it */
  1153. if (try_get_uprobe(u))
  1154. list_add(&u->pending_list, head);
  1155. }
  1156. }
  1157. read_unlock(&uprobes_treelock);
  1158. }
  1159. /* @vma contains reference counter, not the probed instruction. */
  1160. static int delayed_ref_ctr_inc(struct vm_area_struct *vma)
  1161. {
  1162. struct list_head *pos, *q;
  1163. struct delayed_uprobe *du;
  1164. unsigned long vaddr;
  1165. int ret = 0, err = 0;
  1166. mutex_lock(&delayed_uprobe_lock);
  1167. list_for_each_safe(pos, q, &delayed_uprobe_list) {
  1168. du = list_entry(pos, struct delayed_uprobe, list);
  1169. if (du->mm != vma->vm_mm ||
  1170. !valid_ref_ctr_vma(du->uprobe, vma))
  1171. continue;
  1172. vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset);
  1173. ret = __update_ref_ctr(vma->vm_mm, vaddr, 1);
  1174. if (ret) {
  1175. update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1);
  1176. if (!err)
  1177. err = ret;
  1178. }
  1179. delayed_uprobe_delete(du);
  1180. }
  1181. mutex_unlock(&delayed_uprobe_lock);
  1182. return err;
  1183. }
  1184. /*
  1185. * Called from mmap_region/vma_merge with mm->mmap_lock acquired.
  1186. *
  1187. * Currently we ignore all errors and always return 0, the callers
  1188. * can't handle the failure anyway.
  1189. */
  1190. int uprobe_mmap(struct vm_area_struct *vma)
  1191. {
  1192. struct list_head tmp_list;
  1193. struct uprobe *uprobe, *u;
  1194. struct inode *inode;
  1195. if (no_uprobe_events())
  1196. return 0;
  1197. if (vma->vm_file &&
  1198. (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE &&
  1199. test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags))
  1200. delayed_ref_ctr_inc(vma);
  1201. if (!valid_vma(vma, true))
  1202. return 0;
  1203. inode = file_inode(vma->vm_file);
  1204. if (!inode)
  1205. return 0;
  1206. mutex_lock(uprobes_mmap_hash(inode));
  1207. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  1208. /*
  1209. * We can race with uprobe_unregister(), this uprobe can be already
  1210. * removed. But in this case filter_chain() must return false, all
  1211. * consumers have gone away.
  1212. */
  1213. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  1214. if (!fatal_signal_pending(current) &&
  1215. filter_chain(uprobe, vma->vm_mm)) {
  1216. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  1217. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  1218. }
  1219. put_uprobe(uprobe);
  1220. }
  1221. mutex_unlock(uprobes_mmap_hash(inode));
  1222. return 0;
  1223. }
  1224. static bool
  1225. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1226. {
  1227. loff_t min, max;
  1228. struct inode *inode;
  1229. struct rb_node *n;
  1230. inode = file_inode(vma->vm_file);
  1231. min = vaddr_to_offset(vma, start);
  1232. max = min + (end - start) - 1;
  1233. read_lock(&uprobes_treelock);
  1234. n = find_node_in_range(inode, min, max);
  1235. read_unlock(&uprobes_treelock);
  1236. return !!n;
  1237. }
  1238. /*
  1239. * Called in context of a munmap of a vma.
  1240. */
  1241. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1242. {
  1243. if (no_uprobe_events() || !valid_vma(vma, false))
  1244. return;
  1245. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  1246. return;
  1247. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  1248. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  1249. return;
  1250. if (vma_has_uprobes(vma, start, end))
  1251. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  1252. }
  1253. static vm_fault_t xol_fault(const struct vm_special_mapping *sm,
  1254. struct vm_area_struct *vma, struct vm_fault *vmf)
  1255. {
  1256. struct xol_area *area = vma->vm_mm->uprobes_state.xol_area;
  1257. vmf->page = area->page;
  1258. get_page(vmf->page);
  1259. return 0;
  1260. }
  1261. static const struct vm_special_mapping xol_mapping = {
  1262. .name = "[uprobes]",
  1263. .fault = xol_fault,
  1264. };
  1265. /* Slot allocation for XOL */
  1266. static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
  1267. {
  1268. struct vm_area_struct *vma;
  1269. int ret;
  1270. if (mmap_write_lock_killable(mm))
  1271. return -EINTR;
  1272. if (mm->uprobes_state.xol_area) {
  1273. ret = -EALREADY;
  1274. goto fail;
  1275. }
  1276. if (!area->vaddr) {
  1277. /* Try to map as high as possible, this is only a hint. */
  1278. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
  1279. PAGE_SIZE, 0, 0);
  1280. if (IS_ERR_VALUE(area->vaddr)) {
  1281. ret = area->vaddr;
  1282. goto fail;
  1283. }
  1284. }
  1285. vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  1286. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
  1287. &xol_mapping);
  1288. if (IS_ERR(vma)) {
  1289. ret = PTR_ERR(vma);
  1290. goto fail;
  1291. }
  1292. ret = 0;
  1293. /* pairs with get_xol_area() */
  1294. smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
  1295. fail:
  1296. mmap_write_unlock(mm);
  1297. return ret;
  1298. }
  1299. void * __weak arch_uprobe_trampoline(unsigned long *psize)
  1300. {
  1301. static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  1302. *psize = UPROBE_SWBP_INSN_SIZE;
  1303. return &insn;
  1304. }
  1305. static struct xol_area *__create_xol_area(unsigned long vaddr)
  1306. {
  1307. struct mm_struct *mm = current->mm;
  1308. unsigned long insns_size;
  1309. struct xol_area *area;
  1310. void *insns;
  1311. area = kzalloc(sizeof(*area), GFP_KERNEL);
  1312. if (unlikely(!area))
  1313. goto out;
  1314. area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long),
  1315. GFP_KERNEL);
  1316. if (!area->bitmap)
  1317. goto free_area;
  1318. area->page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
  1319. if (!area->page)
  1320. goto free_bitmap;
  1321. area->vaddr = vaddr;
  1322. init_waitqueue_head(&area->wq);
  1323. /* Reserve the 1st slot for get_trampoline_vaddr() */
  1324. set_bit(0, area->bitmap);
  1325. atomic_set(&area->slot_count, 1);
  1326. insns = arch_uprobe_trampoline(&insns_size);
  1327. arch_uprobe_copy_ixol(area->page, 0, insns, insns_size);
  1328. if (!xol_add_vma(mm, area))
  1329. return area;
  1330. __free_page(area->page);
  1331. free_bitmap:
  1332. kfree(area->bitmap);
  1333. free_area:
  1334. kfree(area);
  1335. out:
  1336. return NULL;
  1337. }
  1338. /*
  1339. * get_xol_area - Allocate process's xol_area if necessary.
  1340. * This area will be used for storing instructions for execution out of line.
  1341. *
  1342. * Returns the allocated area or NULL.
  1343. */
  1344. static struct xol_area *get_xol_area(void)
  1345. {
  1346. struct mm_struct *mm = current->mm;
  1347. struct xol_area *area;
  1348. if (!mm->uprobes_state.xol_area)
  1349. __create_xol_area(0);
  1350. /* Pairs with xol_add_vma() smp_store_release() */
  1351. area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */
  1352. return area;
  1353. }
  1354. /*
  1355. * uprobe_clear_state - Free the area allocated for slots.
  1356. */
  1357. void uprobe_clear_state(struct mm_struct *mm)
  1358. {
  1359. struct xol_area *area = mm->uprobes_state.xol_area;
  1360. mutex_lock(&delayed_uprobe_lock);
  1361. delayed_uprobe_remove(NULL, mm);
  1362. mutex_unlock(&delayed_uprobe_lock);
  1363. if (!area)
  1364. return;
  1365. put_page(area->page);
  1366. kfree(area->bitmap);
  1367. kfree(area);
  1368. }
  1369. void uprobe_start_dup_mmap(void)
  1370. {
  1371. percpu_down_read(&dup_mmap_sem);
  1372. }
  1373. void uprobe_end_dup_mmap(void)
  1374. {
  1375. percpu_up_read(&dup_mmap_sem);
  1376. }
  1377. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1378. {
  1379. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1380. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1381. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1382. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1383. }
  1384. }
  1385. /*
  1386. * - search for a free slot.
  1387. */
  1388. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1389. {
  1390. unsigned long slot_addr;
  1391. int slot_nr;
  1392. do {
  1393. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1394. if (slot_nr < UINSNS_PER_PAGE) {
  1395. if (!test_and_set_bit(slot_nr, area->bitmap))
  1396. break;
  1397. slot_nr = UINSNS_PER_PAGE;
  1398. continue;
  1399. }
  1400. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1401. } while (slot_nr >= UINSNS_PER_PAGE);
  1402. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1403. atomic_inc(&area->slot_count);
  1404. return slot_addr;
  1405. }
  1406. /*
  1407. * xol_get_insn_slot - allocate a slot for xol.
  1408. * Returns the allocated slot address or 0.
  1409. */
  1410. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1411. {
  1412. struct xol_area *area;
  1413. unsigned long xol_vaddr;
  1414. area = get_xol_area();
  1415. if (!area)
  1416. return 0;
  1417. xol_vaddr = xol_take_insn_slot(area);
  1418. if (unlikely(!xol_vaddr))
  1419. return 0;
  1420. arch_uprobe_copy_ixol(area->page, xol_vaddr,
  1421. &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
  1422. return xol_vaddr;
  1423. }
  1424. /*
  1425. * xol_free_insn_slot - If slot was earlier allocated by
  1426. * @xol_get_insn_slot(), make the slot available for
  1427. * subsequent requests.
  1428. */
  1429. static void xol_free_insn_slot(struct task_struct *tsk)
  1430. {
  1431. struct xol_area *area;
  1432. unsigned long vma_end;
  1433. unsigned long slot_addr;
  1434. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1435. return;
  1436. slot_addr = tsk->utask->xol_vaddr;
  1437. if (unlikely(!slot_addr))
  1438. return;
  1439. area = tsk->mm->uprobes_state.xol_area;
  1440. vma_end = area->vaddr + PAGE_SIZE;
  1441. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1442. unsigned long offset;
  1443. int slot_nr;
  1444. offset = slot_addr - area->vaddr;
  1445. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1446. if (slot_nr >= UINSNS_PER_PAGE)
  1447. return;
  1448. clear_bit(slot_nr, area->bitmap);
  1449. atomic_dec(&area->slot_count);
  1450. smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
  1451. if (waitqueue_active(&area->wq))
  1452. wake_up(&area->wq);
  1453. tsk->utask->xol_vaddr = 0;
  1454. }
  1455. }
  1456. void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  1457. void *src, unsigned long len)
  1458. {
  1459. /* Initialize the slot */
  1460. copy_to_page(page, vaddr, src, len);
  1461. /*
  1462. * We probably need flush_icache_user_page() but it needs vma.
  1463. * This should work on most of architectures by default. If
  1464. * architecture needs to do something different it can define
  1465. * its own version of the function.
  1466. */
  1467. flush_dcache_page(page);
  1468. }
  1469. /**
  1470. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1471. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1472. * instruction.
  1473. * Return the address of the breakpoint instruction.
  1474. */
  1475. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1476. {
  1477. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1478. }
  1479. unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
  1480. {
  1481. struct uprobe_task *utask = current->utask;
  1482. if (unlikely(utask && utask->active_uprobe))
  1483. return utask->vaddr;
  1484. return instruction_pointer(regs);
  1485. }
  1486. static struct return_instance *free_ret_instance(struct return_instance *ri)
  1487. {
  1488. struct return_instance *next = ri->next;
  1489. put_uprobe(ri->uprobe);
  1490. kfree(ri);
  1491. return next;
  1492. }
  1493. /*
  1494. * Called with no locks held.
  1495. * Called in context of an exiting or an exec-ing thread.
  1496. */
  1497. void uprobe_free_utask(struct task_struct *t)
  1498. {
  1499. struct uprobe_task *utask = t->utask;
  1500. struct return_instance *ri;
  1501. if (!utask)
  1502. return;
  1503. t->utask = NULL;
  1504. if (utask->active_uprobe)
  1505. put_uprobe(utask->active_uprobe);
  1506. ri = utask->return_instances;
  1507. while (ri)
  1508. ri = free_ret_instance(ri);
  1509. xol_free_insn_slot(t);
  1510. kfree(utask);
  1511. }
  1512. /*
  1513. * Allocate a uprobe_task object for the task if necessary.
  1514. * Called when the thread hits a breakpoint.
  1515. *
  1516. * Returns:
  1517. * - pointer to new uprobe_task on success
  1518. * - NULL otherwise
  1519. */
  1520. static struct uprobe_task *get_utask(void)
  1521. {
  1522. if (!current->utask)
  1523. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1524. return current->utask;
  1525. }
  1526. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1527. {
  1528. struct uprobe_task *n_utask;
  1529. struct return_instance **p, *o, *n;
  1530. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1531. if (!n_utask)
  1532. return -ENOMEM;
  1533. t->utask = n_utask;
  1534. p = &n_utask->return_instances;
  1535. for (o = o_utask->return_instances; o; o = o->next) {
  1536. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1537. if (!n)
  1538. return -ENOMEM;
  1539. *n = *o;
  1540. /*
  1541. * uprobe's refcnt has to be positive at this point, kept by
  1542. * utask->return_instances items; return_instances can't be
  1543. * removed right now, as task is blocked due to duping; so
  1544. * get_uprobe() is safe to use here.
  1545. */
  1546. get_uprobe(n->uprobe);
  1547. n->next = NULL;
  1548. *p = n;
  1549. p = &n->next;
  1550. n_utask->depth++;
  1551. }
  1552. return 0;
  1553. }
  1554. static void dup_xol_work(struct callback_head *work)
  1555. {
  1556. if (current->flags & PF_EXITING)
  1557. return;
  1558. if (!__create_xol_area(current->utask->dup_xol_addr) &&
  1559. !fatal_signal_pending(current))
  1560. uprobe_warn(current, "dup xol area");
  1561. }
  1562. /*
  1563. * Called in context of a new clone/fork from copy_process.
  1564. */
  1565. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1566. {
  1567. struct uprobe_task *utask = current->utask;
  1568. struct mm_struct *mm = current->mm;
  1569. struct xol_area *area;
  1570. t->utask = NULL;
  1571. if (!utask || !utask->return_instances)
  1572. return;
  1573. if (mm == t->mm && !(flags & CLONE_VFORK))
  1574. return;
  1575. if (dup_utask(t, utask))
  1576. return uprobe_warn(t, "dup ret instances");
  1577. /* The task can fork() after dup_xol_work() fails */
  1578. area = mm->uprobes_state.xol_area;
  1579. if (!area)
  1580. return uprobe_warn(t, "dup xol area");
  1581. if (mm == t->mm)
  1582. return;
  1583. t->utask->dup_xol_addr = area->vaddr;
  1584. init_task_work(&t->utask->dup_xol_work, dup_xol_work);
  1585. task_work_add(t, &t->utask->dup_xol_work, TWA_RESUME);
  1586. }
  1587. /*
  1588. * Current area->vaddr notion assume the trampoline address is always
  1589. * equal area->vaddr.
  1590. *
  1591. * Returns -1 in case the xol_area is not allocated.
  1592. */
  1593. unsigned long uprobe_get_trampoline_vaddr(void)
  1594. {
  1595. unsigned long trampoline_vaddr = UPROBE_NO_TRAMPOLINE_VADDR;
  1596. struct xol_area *area;
  1597. /* Pairs with xol_add_vma() smp_store_release() */
  1598. area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */
  1599. if (area)
  1600. trampoline_vaddr = area->vaddr;
  1601. return trampoline_vaddr;
  1602. }
  1603. static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
  1604. struct pt_regs *regs)
  1605. {
  1606. struct return_instance *ri = utask->return_instances;
  1607. enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;
  1608. while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
  1609. ri = free_ret_instance(ri);
  1610. utask->depth--;
  1611. }
  1612. utask->return_instances = ri;
  1613. }
  1614. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1615. {
  1616. struct return_instance *ri;
  1617. struct uprobe_task *utask;
  1618. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1619. bool chained;
  1620. if (!get_xol_area())
  1621. return;
  1622. utask = get_utask();
  1623. if (!utask)
  1624. return;
  1625. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1626. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1627. " nestedness limit pid/tgid=%d/%d\n",
  1628. current->pid, current->tgid);
  1629. return;
  1630. }
  1631. /* we need to bump refcount to store uprobe in utask */
  1632. if (!try_get_uprobe(uprobe))
  1633. return;
  1634. ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1635. if (!ri)
  1636. goto fail;
  1637. trampoline_vaddr = uprobe_get_trampoline_vaddr();
  1638. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1639. if (orig_ret_vaddr == -1)
  1640. goto fail;
  1641. /* drop the entries invalidated by longjmp() */
  1642. chained = (orig_ret_vaddr == trampoline_vaddr);
  1643. cleanup_return_instances(utask, chained, regs);
  1644. /*
  1645. * We don't want to keep trampoline address in stack, rather keep the
  1646. * original return address of first caller thru all the consequent
  1647. * instances. This also makes breakpoint unwrapping easier.
  1648. */
  1649. if (chained) {
  1650. if (!utask->return_instances) {
  1651. /*
  1652. * This situation is not possible. Likely we have an
  1653. * attack from user-space.
  1654. */
  1655. uprobe_warn(current, "handle tail call");
  1656. goto fail;
  1657. }
  1658. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1659. }
  1660. ri->uprobe = uprobe;
  1661. ri->func = instruction_pointer(regs);
  1662. ri->stack = user_stack_pointer(regs);
  1663. ri->orig_ret_vaddr = orig_ret_vaddr;
  1664. ri->chained = chained;
  1665. utask->depth++;
  1666. ri->next = utask->return_instances;
  1667. utask->return_instances = ri;
  1668. return;
  1669. fail:
  1670. kfree(ri);
  1671. put_uprobe(uprobe);
  1672. }
  1673. /* Prepare to single-step probed instruction out of line. */
  1674. static int
  1675. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1676. {
  1677. struct uprobe_task *utask;
  1678. unsigned long xol_vaddr;
  1679. int err;
  1680. utask = get_utask();
  1681. if (!utask)
  1682. return -ENOMEM;
  1683. if (!try_get_uprobe(uprobe))
  1684. return -EINVAL;
  1685. xol_vaddr = xol_get_insn_slot(uprobe);
  1686. if (!xol_vaddr) {
  1687. err = -ENOMEM;
  1688. goto err_out;
  1689. }
  1690. utask->xol_vaddr = xol_vaddr;
  1691. utask->vaddr = bp_vaddr;
  1692. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1693. if (unlikely(err)) {
  1694. xol_free_insn_slot(current);
  1695. goto err_out;
  1696. }
  1697. utask->active_uprobe = uprobe;
  1698. utask->state = UTASK_SSTEP;
  1699. return 0;
  1700. err_out:
  1701. put_uprobe(uprobe);
  1702. return err;
  1703. }
  1704. /*
  1705. * If we are singlestepping, then ensure this thread is not connected to
  1706. * non-fatal signals until completion of singlestep. When xol insn itself
  1707. * triggers the signal, restart the original insn even if the task is
  1708. * already SIGKILL'ed (since coredump should report the correct ip). This
  1709. * is even more important if the task has a handler for SIGSEGV/etc, The
  1710. * _same_ instruction should be repeated again after return from the signal
  1711. * handler, and SSTEP can never finish in this case.
  1712. */
  1713. bool uprobe_deny_signal(void)
  1714. {
  1715. struct task_struct *t = current;
  1716. struct uprobe_task *utask = t->utask;
  1717. if (likely(!utask || !utask->active_uprobe))
  1718. return false;
  1719. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1720. if (task_sigpending(t)) {
  1721. spin_lock_irq(&t->sighand->siglock);
  1722. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1723. spin_unlock_irq(&t->sighand->siglock);
  1724. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1725. utask->state = UTASK_SSTEP_TRAPPED;
  1726. set_tsk_thread_flag(t, TIF_UPROBE);
  1727. }
  1728. }
  1729. return true;
  1730. }
  1731. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1732. {
  1733. VMA_ITERATOR(vmi, mm, 0);
  1734. struct vm_area_struct *vma;
  1735. for_each_vma(vmi, vma) {
  1736. if (!valid_vma(vma, false))
  1737. continue;
  1738. /*
  1739. * This is not strictly accurate, we can race with
  1740. * uprobe_unregister() and see the already removed
  1741. * uprobe if delete_uprobe() was not yet called.
  1742. * Or this uprobe can be filtered out.
  1743. */
  1744. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1745. return;
  1746. }
  1747. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1748. }
  1749. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1750. {
  1751. struct page *page;
  1752. uprobe_opcode_t opcode;
  1753. int result;
  1754. if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE)))
  1755. return -EINVAL;
  1756. pagefault_disable();
  1757. result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
  1758. pagefault_enable();
  1759. if (likely(result == 0))
  1760. goto out;
  1761. result = get_user_pages(vaddr, 1, FOLL_FORCE, &page);
  1762. if (result < 0)
  1763. return result;
  1764. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1765. put_page(page);
  1766. out:
  1767. /* This needs to return true for any variant of the trap insn */
  1768. return is_trap_insn(&opcode);
  1769. }
  1770. /* assumes being inside RCU protected region */
  1771. static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swbp)
  1772. {
  1773. struct mm_struct *mm = current->mm;
  1774. struct uprobe *uprobe = NULL;
  1775. struct vm_area_struct *vma;
  1776. mmap_read_lock(mm);
  1777. vma = vma_lookup(mm, bp_vaddr);
  1778. if (vma) {
  1779. if (valid_vma(vma, false)) {
  1780. struct inode *inode = file_inode(vma->vm_file);
  1781. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1782. uprobe = find_uprobe_rcu(inode, offset);
  1783. }
  1784. if (!uprobe)
  1785. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1786. } else {
  1787. *is_swbp = -EFAULT;
  1788. }
  1789. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1790. mmf_recalc_uprobes(mm);
  1791. mmap_read_unlock(mm);
  1792. return uprobe;
  1793. }
  1794. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1795. {
  1796. struct uprobe_consumer *uc;
  1797. int remove = UPROBE_HANDLER_REMOVE;
  1798. bool need_prep = false; /* prepare return uprobe, when needed */
  1799. bool has_consumers = false;
  1800. current->utask->auprobe = &uprobe->arch;
  1801. list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
  1802. srcu_read_lock_held(&uprobes_srcu)) {
  1803. int rc = 0;
  1804. if (uc->handler) {
  1805. rc = uc->handler(uc, regs);
  1806. WARN(rc & ~UPROBE_HANDLER_MASK,
  1807. "bad rc=0x%x from %ps()\n", rc, uc->handler);
  1808. }
  1809. if (uc->ret_handler)
  1810. need_prep = true;
  1811. remove &= rc;
  1812. has_consumers = true;
  1813. }
  1814. current->utask->auprobe = NULL;
  1815. if (need_prep && !remove)
  1816. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1817. if (remove && has_consumers) {
  1818. down_read(&uprobe->register_rwsem);
  1819. /* re-check that removal is still required, this time under lock */
  1820. if (!filter_chain(uprobe, current->mm)) {
  1821. WARN_ON(!uprobe_is_active(uprobe));
  1822. unapply_uprobe(uprobe, current->mm);
  1823. }
  1824. up_read(&uprobe->register_rwsem);
  1825. }
  1826. }
  1827. static void
  1828. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1829. {
  1830. struct uprobe *uprobe = ri->uprobe;
  1831. struct uprobe_consumer *uc;
  1832. int srcu_idx;
  1833. srcu_idx = srcu_read_lock(&uprobes_srcu);
  1834. list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node,
  1835. srcu_read_lock_held(&uprobes_srcu)) {
  1836. if (uc->ret_handler)
  1837. uc->ret_handler(uc, ri->func, regs);
  1838. }
  1839. srcu_read_unlock(&uprobes_srcu, srcu_idx);
  1840. }
  1841. static struct return_instance *find_next_ret_chain(struct return_instance *ri)
  1842. {
  1843. bool chained;
  1844. do {
  1845. chained = ri->chained;
  1846. ri = ri->next; /* can't be NULL if chained */
  1847. } while (chained);
  1848. return ri;
  1849. }
  1850. void uprobe_handle_trampoline(struct pt_regs *regs)
  1851. {
  1852. struct uprobe_task *utask;
  1853. struct return_instance *ri, *next;
  1854. bool valid;
  1855. utask = current->utask;
  1856. if (!utask)
  1857. goto sigill;
  1858. ri = utask->return_instances;
  1859. if (!ri)
  1860. goto sigill;
  1861. do {
  1862. /*
  1863. * We should throw out the frames invalidated by longjmp().
  1864. * If this chain is valid, then the next one should be alive
  1865. * or NULL; the latter case means that nobody but ri->func
  1866. * could hit this trampoline on return. TODO: sigaltstack().
  1867. */
  1868. next = find_next_ret_chain(ri);
  1869. valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
  1870. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1871. do {
  1872. /* pop current instance from the stack of pending return instances,
  1873. * as it's not pending anymore: we just fixed up original
  1874. * instruction pointer in regs and are about to call handlers;
  1875. * this allows fixup_uretprobe_trampoline_entries() to properly fix up
  1876. * captured stack traces from uretprobe handlers, in which pending
  1877. * trampoline addresses on the stack are replaced with correct
  1878. * original return addresses
  1879. */
  1880. utask->return_instances = ri->next;
  1881. if (valid)
  1882. handle_uretprobe_chain(ri, regs);
  1883. ri = free_ret_instance(ri);
  1884. utask->depth--;
  1885. } while (ri != next);
  1886. } while (!valid);
  1887. utask->return_instances = ri;
  1888. return;
  1889. sigill:
  1890. uprobe_warn(current, "handle uretprobe, sending SIGILL.");
  1891. force_sig(SIGILL);
  1892. }
  1893. bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
  1894. {
  1895. return false;
  1896. }
  1897. bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  1898. struct pt_regs *regs)
  1899. {
  1900. return true;
  1901. }
  1902. /*
  1903. * Run handler and ask thread to singlestep.
  1904. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1905. */
  1906. static void handle_swbp(struct pt_regs *regs)
  1907. {
  1908. struct uprobe *uprobe;
  1909. unsigned long bp_vaddr;
  1910. int is_swbp, srcu_idx;
  1911. bp_vaddr = uprobe_get_swbp_addr(regs);
  1912. if (bp_vaddr == uprobe_get_trampoline_vaddr())
  1913. return uprobe_handle_trampoline(regs);
  1914. srcu_idx = srcu_read_lock(&uprobes_srcu);
  1915. uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp);
  1916. if (!uprobe) {
  1917. if (is_swbp > 0) {
  1918. /* No matching uprobe; signal SIGTRAP. */
  1919. force_sig(SIGTRAP);
  1920. } else {
  1921. /*
  1922. * Either we raced with uprobe_unregister() or we can't
  1923. * access this memory. The latter is only possible if
  1924. * another thread plays with our ->mm. In both cases
  1925. * we can simply restart. If this vma was unmapped we
  1926. * can pretend this insn was not executed yet and get
  1927. * the (correct) SIGSEGV after restart.
  1928. */
  1929. instruction_pointer_set(regs, bp_vaddr);
  1930. }
  1931. goto out;
  1932. }
  1933. /* change it in advance for ->handler() and restart */
  1934. instruction_pointer_set(regs, bp_vaddr);
  1935. /*
  1936. * TODO: move copy_insn/etc into _register and remove this hack.
  1937. * After we hit the bp, _unregister + _register can install the
  1938. * new and not-yet-analyzed uprobe at the same address, restart.
  1939. */
  1940. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1941. goto out;
  1942. /*
  1943. * Pairs with the smp_wmb() in prepare_uprobe().
  1944. *
  1945. * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
  1946. * we must also see the stores to &uprobe->arch performed by the
  1947. * prepare_uprobe() call.
  1948. */
  1949. smp_rmb();
  1950. /* Tracing handlers use ->utask to communicate with fetch methods */
  1951. if (!get_utask())
  1952. goto out;
  1953. if (arch_uprobe_ignore(&uprobe->arch, regs))
  1954. goto out;
  1955. handler_chain(uprobe, regs);
  1956. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1957. goto out;
  1958. if (pre_ssout(uprobe, regs, bp_vaddr))
  1959. goto out;
  1960. out:
  1961. /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
  1962. srcu_read_unlock(&uprobes_srcu, srcu_idx);
  1963. }
  1964. /*
  1965. * Perform required fix-ups and disable singlestep.
  1966. * Allow pending signals to take effect.
  1967. */
  1968. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1969. {
  1970. struct uprobe *uprobe;
  1971. int err = 0;
  1972. uprobe = utask->active_uprobe;
  1973. if (utask->state == UTASK_SSTEP_ACK)
  1974. err = arch_uprobe_post_xol(&uprobe->arch, regs);
  1975. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1976. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1977. else
  1978. WARN_ON_ONCE(1);
  1979. put_uprobe(uprobe);
  1980. utask->active_uprobe = NULL;
  1981. utask->state = UTASK_RUNNING;
  1982. xol_free_insn_slot(current);
  1983. spin_lock_irq(&current->sighand->siglock);
  1984. recalc_sigpending(); /* see uprobe_deny_signal() */
  1985. spin_unlock_irq(&current->sighand->siglock);
  1986. if (unlikely(err)) {
  1987. uprobe_warn(current, "execute the probed insn, sending SIGILL.");
  1988. force_sig(SIGILL);
  1989. }
  1990. }
  1991. /*
  1992. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1993. * allows the thread to return from interrupt. After that handle_swbp()
  1994. * sets utask->active_uprobe.
  1995. *
  1996. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1997. * and allows the thread to return from interrupt.
  1998. *
  1999. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  2000. * uprobe_notify_resume().
  2001. */
  2002. void uprobe_notify_resume(struct pt_regs *regs)
  2003. {
  2004. struct uprobe_task *utask;
  2005. clear_thread_flag(TIF_UPROBE);
  2006. utask = current->utask;
  2007. if (utask && utask->active_uprobe)
  2008. handle_singlestep(utask, regs);
  2009. else
  2010. handle_swbp(regs);
  2011. }
  2012. /*
  2013. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  2014. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  2015. */
  2016. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  2017. {
  2018. if (!current->mm)
  2019. return 0;
  2020. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  2021. (!current->utask || !current->utask->return_instances))
  2022. return 0;
  2023. set_thread_flag(TIF_UPROBE);
  2024. return 1;
  2025. }
  2026. /*
  2027. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  2028. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  2029. */
  2030. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  2031. {
  2032. struct uprobe_task *utask = current->utask;
  2033. if (!current->mm || !utask || !utask->active_uprobe)
  2034. /* task is currently not uprobed */
  2035. return 0;
  2036. utask->state = UTASK_SSTEP_ACK;
  2037. set_thread_flag(TIF_UPROBE);
  2038. return 1;
  2039. }
  2040. static struct notifier_block uprobe_exception_nb = {
  2041. .notifier_call = arch_uprobe_exception_notify,
  2042. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  2043. };
  2044. void __init uprobes_init(void)
  2045. {
  2046. int i;
  2047. for (i = 0; i < UPROBES_HASH_SZ; i++)
  2048. mutex_init(&uprobes_mmap_mutex[i]);
  2049. BUG_ON(register_die_notifier(&uprobe_exception_nb));
  2050. }