kprobes.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Kernel Probes (KProbes)
  4. *
  5. * Copyright (C) IBM Corporation, 2002, 2004
  6. *
  7. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  8. * Probes initial implementation (includes suggestions from
  9. * Rusty Russell).
  10. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  11. * hlists and exceptions notifier as suggested by Andi Kleen.
  12. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  13. * interface to access function arguments.
  14. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  15. * exceptions notifier to be first on the priority list.
  16. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  17. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  18. * <prasanna@in.ibm.com> added function-return probes.
  19. */
  20. #define pr_fmt(fmt) "kprobes: " fmt
  21. #include <linux/kprobes.h>
  22. #include <linux/hash.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/stddef.h>
  26. #include <linux/export.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/freezer.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/memory.h>
  34. #include <linux/ftrace.h>
  35. #include <linux/cpu.h>
  36. #include <linux/jump_label.h>
  37. #include <linux/static_call.h>
  38. #include <linux/perf_event.h>
  39. #include <linux/execmem.h>
  40. #include <asm/sections.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/errno.h>
  43. #include <linux/uaccess.h>
  44. #define KPROBE_HASH_BITS 6
  45. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  46. #if !defined(CONFIG_OPTPROBES) || !defined(CONFIG_SYSCTL)
  47. #define kprobe_sysctls_init() do { } while (0)
  48. #endif
  49. static int kprobes_initialized;
  50. /* kprobe_table can be accessed by
  51. * - Normal hlist traversal and RCU add/del under 'kprobe_mutex' is held.
  52. * Or
  53. * - RCU hlist traversal under disabling preempt (breakpoint handlers)
  54. */
  55. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  56. /* NOTE: change this value only with 'kprobe_mutex' held */
  57. static bool kprobes_all_disarmed;
  58. /* This protects 'kprobe_table' and 'optimizing_list' */
  59. static DEFINE_MUTEX(kprobe_mutex);
  60. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance);
  61. kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
  62. unsigned int __unused)
  63. {
  64. return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
  65. }
  66. /*
  67. * Blacklist -- list of 'struct kprobe_blacklist_entry' to store info where
  68. * kprobes can not probe.
  69. */
  70. static LIST_HEAD(kprobe_blacklist);
  71. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  72. /*
  73. * 'kprobe::ainsn.insn' points to the copy of the instruction to be
  74. * single-stepped. x86_64, POWER4 and above have no-exec support and
  75. * stepping on the instruction on a vmalloced/kmalloced/data page
  76. * is a recipe for disaster
  77. */
  78. struct kprobe_insn_page {
  79. struct list_head list;
  80. kprobe_opcode_t *insns; /* Page of instruction slots */
  81. struct kprobe_insn_cache *cache;
  82. int nused;
  83. int ngarbage;
  84. char slot_used[];
  85. };
  86. #define KPROBE_INSN_PAGE_SIZE(slots) \
  87. (offsetof(struct kprobe_insn_page, slot_used) + \
  88. (sizeof(char) * (slots)))
  89. static int slots_per_page(struct kprobe_insn_cache *c)
  90. {
  91. return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
  92. }
  93. enum kprobe_slot_state {
  94. SLOT_CLEAN = 0,
  95. SLOT_DIRTY = 1,
  96. SLOT_USED = 2,
  97. };
  98. void __weak *alloc_insn_page(void)
  99. {
  100. /*
  101. * Use execmem_alloc() so this page is within +/- 2GB of where the
  102. * kernel image and loaded module images reside. This is required
  103. * for most of the architectures.
  104. * (e.g. x86-64 needs this to handle the %rip-relative fixups.)
  105. */
  106. return execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
  107. }
  108. static void free_insn_page(void *page)
  109. {
  110. execmem_free(page);
  111. }
  112. struct kprobe_insn_cache kprobe_insn_slots = {
  113. .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
  114. .alloc = alloc_insn_page,
  115. .free = free_insn_page,
  116. .sym = KPROBE_INSN_PAGE_SYM,
  117. .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
  118. .insn_size = MAX_INSN_SIZE,
  119. .nr_garbage = 0,
  120. };
  121. static int collect_garbage_slots(struct kprobe_insn_cache *c);
  122. /**
  123. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  124. * We allocate an executable page if there's no room on existing ones.
  125. */
  126. kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
  127. {
  128. struct kprobe_insn_page *kip;
  129. kprobe_opcode_t *slot = NULL;
  130. /* Since the slot array is not protected by rcu, we need a mutex */
  131. mutex_lock(&c->mutex);
  132. retry:
  133. rcu_read_lock();
  134. list_for_each_entry_rcu(kip, &c->pages, list) {
  135. if (kip->nused < slots_per_page(c)) {
  136. int i;
  137. for (i = 0; i < slots_per_page(c); i++) {
  138. if (kip->slot_used[i] == SLOT_CLEAN) {
  139. kip->slot_used[i] = SLOT_USED;
  140. kip->nused++;
  141. slot = kip->insns + (i * c->insn_size);
  142. rcu_read_unlock();
  143. goto out;
  144. }
  145. }
  146. /* kip->nused is broken. Fix it. */
  147. kip->nused = slots_per_page(c);
  148. WARN_ON(1);
  149. }
  150. }
  151. rcu_read_unlock();
  152. /* If there are any garbage slots, collect it and try again. */
  153. if (c->nr_garbage && collect_garbage_slots(c) == 0)
  154. goto retry;
  155. /* All out of space. Need to allocate a new page. */
  156. kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
  157. if (!kip)
  158. goto out;
  159. kip->insns = c->alloc();
  160. if (!kip->insns) {
  161. kfree(kip);
  162. goto out;
  163. }
  164. INIT_LIST_HEAD(&kip->list);
  165. memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
  166. kip->slot_used[0] = SLOT_USED;
  167. kip->nused = 1;
  168. kip->ngarbage = 0;
  169. kip->cache = c;
  170. list_add_rcu(&kip->list, &c->pages);
  171. slot = kip->insns;
  172. /* Record the perf ksymbol register event after adding the page */
  173. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
  174. PAGE_SIZE, false, c->sym);
  175. out:
  176. mutex_unlock(&c->mutex);
  177. return slot;
  178. }
  179. /* Return true if all garbages are collected, otherwise false. */
  180. static bool collect_one_slot(struct kprobe_insn_page *kip, int idx)
  181. {
  182. kip->slot_used[idx] = SLOT_CLEAN;
  183. kip->nused--;
  184. if (kip->nused == 0) {
  185. /*
  186. * Page is no longer in use. Free it unless
  187. * it's the last one. We keep the last one
  188. * so as not to have to set it up again the
  189. * next time somebody inserts a probe.
  190. */
  191. if (!list_is_singular(&kip->list)) {
  192. /*
  193. * Record perf ksymbol unregister event before removing
  194. * the page.
  195. */
  196. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
  197. (unsigned long)kip->insns, PAGE_SIZE, true,
  198. kip->cache->sym);
  199. list_del_rcu(&kip->list);
  200. synchronize_rcu();
  201. kip->cache->free(kip->insns);
  202. kfree(kip);
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208. static int collect_garbage_slots(struct kprobe_insn_cache *c)
  209. {
  210. struct kprobe_insn_page *kip, *next;
  211. /* Ensure no-one is interrupted on the garbages */
  212. synchronize_rcu();
  213. list_for_each_entry_safe(kip, next, &c->pages, list) {
  214. int i;
  215. if (kip->ngarbage == 0)
  216. continue;
  217. kip->ngarbage = 0; /* we will collect all garbages */
  218. for (i = 0; i < slots_per_page(c); i++) {
  219. if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
  220. break;
  221. }
  222. }
  223. c->nr_garbage = 0;
  224. return 0;
  225. }
  226. void __free_insn_slot(struct kprobe_insn_cache *c,
  227. kprobe_opcode_t *slot, int dirty)
  228. {
  229. struct kprobe_insn_page *kip;
  230. long idx;
  231. mutex_lock(&c->mutex);
  232. rcu_read_lock();
  233. list_for_each_entry_rcu(kip, &c->pages, list) {
  234. idx = ((long)slot - (long)kip->insns) /
  235. (c->insn_size * sizeof(kprobe_opcode_t));
  236. if (idx >= 0 && idx < slots_per_page(c))
  237. goto out;
  238. }
  239. /* Could not find this slot. */
  240. WARN_ON(1);
  241. kip = NULL;
  242. out:
  243. rcu_read_unlock();
  244. /* Mark and sweep: this may sleep */
  245. if (kip) {
  246. /* Check double free */
  247. WARN_ON(kip->slot_used[idx] != SLOT_USED);
  248. if (dirty) {
  249. kip->slot_used[idx] = SLOT_DIRTY;
  250. kip->ngarbage++;
  251. if (++c->nr_garbage > slots_per_page(c))
  252. collect_garbage_slots(c);
  253. } else {
  254. collect_one_slot(kip, idx);
  255. }
  256. }
  257. mutex_unlock(&c->mutex);
  258. }
  259. /*
  260. * Check given address is on the page of kprobe instruction slots.
  261. * This will be used for checking whether the address on a stack
  262. * is on a text area or not.
  263. */
  264. bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
  265. {
  266. struct kprobe_insn_page *kip;
  267. bool ret = false;
  268. rcu_read_lock();
  269. list_for_each_entry_rcu(kip, &c->pages, list) {
  270. if (addr >= (unsigned long)kip->insns &&
  271. addr < (unsigned long)kip->insns + PAGE_SIZE) {
  272. ret = true;
  273. break;
  274. }
  275. }
  276. rcu_read_unlock();
  277. return ret;
  278. }
  279. int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
  280. unsigned long *value, char *type, char *sym)
  281. {
  282. struct kprobe_insn_page *kip;
  283. int ret = -ERANGE;
  284. rcu_read_lock();
  285. list_for_each_entry_rcu(kip, &c->pages, list) {
  286. if ((*symnum)--)
  287. continue;
  288. strscpy(sym, c->sym, KSYM_NAME_LEN);
  289. *type = 't';
  290. *value = (unsigned long)kip->insns;
  291. ret = 0;
  292. break;
  293. }
  294. rcu_read_unlock();
  295. return ret;
  296. }
  297. #ifdef CONFIG_OPTPROBES
  298. void __weak *alloc_optinsn_page(void)
  299. {
  300. return alloc_insn_page();
  301. }
  302. void __weak free_optinsn_page(void *page)
  303. {
  304. free_insn_page(page);
  305. }
  306. /* For optimized_kprobe buffer */
  307. struct kprobe_insn_cache kprobe_optinsn_slots = {
  308. .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
  309. .alloc = alloc_optinsn_page,
  310. .free = free_optinsn_page,
  311. .sym = KPROBE_OPTINSN_PAGE_SYM,
  312. .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
  313. /* .insn_size is initialized later */
  314. .nr_garbage = 0,
  315. };
  316. #endif
  317. #endif
  318. /* We have preemption disabled.. so it is safe to use __ versions */
  319. static inline void set_kprobe_instance(struct kprobe *kp)
  320. {
  321. __this_cpu_write(kprobe_instance, kp);
  322. }
  323. static inline void reset_kprobe_instance(void)
  324. {
  325. __this_cpu_write(kprobe_instance, NULL);
  326. }
  327. /*
  328. * This routine is called either:
  329. * - under the 'kprobe_mutex' - during kprobe_[un]register().
  330. * OR
  331. * - with preemption disabled - from architecture specific code.
  332. */
  333. struct kprobe *get_kprobe(void *addr)
  334. {
  335. struct hlist_head *head;
  336. struct kprobe *p;
  337. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  338. hlist_for_each_entry_rcu(p, head, hlist,
  339. lockdep_is_held(&kprobe_mutex)) {
  340. if (p->addr == addr)
  341. return p;
  342. }
  343. return NULL;
  344. }
  345. NOKPROBE_SYMBOL(get_kprobe);
  346. static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
  347. /* Return true if 'p' is an aggregator */
  348. static inline bool kprobe_aggrprobe(struct kprobe *p)
  349. {
  350. return p->pre_handler == aggr_pre_handler;
  351. }
  352. /* Return true if 'p' is unused */
  353. static inline bool kprobe_unused(struct kprobe *p)
  354. {
  355. return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
  356. list_empty(&p->list);
  357. }
  358. /* Keep all fields in the kprobe consistent. */
  359. static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
  360. {
  361. memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
  362. memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
  363. }
  364. #ifdef CONFIG_OPTPROBES
  365. /* NOTE: This is protected by 'kprobe_mutex'. */
  366. static bool kprobes_allow_optimization;
  367. /*
  368. * Call all 'kprobe::pre_handler' on the list, but ignores its return value.
  369. * This must be called from arch-dep optimized caller.
  370. */
  371. void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
  372. {
  373. struct kprobe *kp;
  374. list_for_each_entry_rcu(kp, &p->list, list) {
  375. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  376. set_kprobe_instance(kp);
  377. kp->pre_handler(kp, regs);
  378. }
  379. reset_kprobe_instance();
  380. }
  381. }
  382. NOKPROBE_SYMBOL(opt_pre_handler);
  383. /* Free optimized instructions and optimized_kprobe */
  384. static void free_aggr_kprobe(struct kprobe *p)
  385. {
  386. struct optimized_kprobe *op;
  387. op = container_of(p, struct optimized_kprobe, kp);
  388. arch_remove_optimized_kprobe(op);
  389. arch_remove_kprobe(p);
  390. kfree(op);
  391. }
  392. /* Return true if the kprobe is ready for optimization. */
  393. static inline int kprobe_optready(struct kprobe *p)
  394. {
  395. struct optimized_kprobe *op;
  396. if (kprobe_aggrprobe(p)) {
  397. op = container_of(p, struct optimized_kprobe, kp);
  398. return arch_prepared_optinsn(&op->optinsn);
  399. }
  400. return 0;
  401. }
  402. /* Return true if the kprobe is disarmed. Note: p must be on hash list */
  403. bool kprobe_disarmed(struct kprobe *p)
  404. {
  405. struct optimized_kprobe *op;
  406. /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
  407. if (!kprobe_aggrprobe(p))
  408. return kprobe_disabled(p);
  409. op = container_of(p, struct optimized_kprobe, kp);
  410. return kprobe_disabled(p) && list_empty(&op->list);
  411. }
  412. /* Return true if the probe is queued on (un)optimizing lists */
  413. static bool kprobe_queued(struct kprobe *p)
  414. {
  415. struct optimized_kprobe *op;
  416. if (kprobe_aggrprobe(p)) {
  417. op = container_of(p, struct optimized_kprobe, kp);
  418. if (!list_empty(&op->list))
  419. return true;
  420. }
  421. return false;
  422. }
  423. /*
  424. * Return an optimized kprobe whose optimizing code replaces
  425. * instructions including 'addr' (exclude breakpoint).
  426. */
  427. static struct kprobe *get_optimized_kprobe(kprobe_opcode_t *addr)
  428. {
  429. int i;
  430. struct kprobe *p = NULL;
  431. struct optimized_kprobe *op;
  432. /* Don't check i == 0, since that is a breakpoint case. */
  433. for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH / sizeof(kprobe_opcode_t); i++)
  434. p = get_kprobe(addr - i);
  435. if (p && kprobe_optready(p)) {
  436. op = container_of(p, struct optimized_kprobe, kp);
  437. if (arch_within_optimized_kprobe(op, addr))
  438. return p;
  439. }
  440. return NULL;
  441. }
  442. /* Optimization staging list, protected by 'kprobe_mutex' */
  443. static LIST_HEAD(optimizing_list);
  444. static LIST_HEAD(unoptimizing_list);
  445. static LIST_HEAD(freeing_list);
  446. static void kprobe_optimizer(struct work_struct *work);
  447. static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
  448. #define OPTIMIZE_DELAY 5
  449. /*
  450. * Optimize (replace a breakpoint with a jump) kprobes listed on
  451. * 'optimizing_list'.
  452. */
  453. static void do_optimize_kprobes(void)
  454. {
  455. lockdep_assert_held(&text_mutex);
  456. /*
  457. * The optimization/unoptimization refers 'online_cpus' via
  458. * stop_machine() and cpu-hotplug modifies the 'online_cpus'.
  459. * And same time, 'text_mutex' will be held in cpu-hotplug and here.
  460. * This combination can cause a deadlock (cpu-hotplug tries to lock
  461. * 'text_mutex' but stop_machine() can not be done because
  462. * the 'online_cpus' has been changed)
  463. * To avoid this deadlock, caller must have locked cpu-hotplug
  464. * for preventing cpu-hotplug outside of 'text_mutex' locking.
  465. */
  466. lockdep_assert_cpus_held();
  467. /* Optimization never be done when disarmed */
  468. if (kprobes_all_disarmed || !kprobes_allow_optimization ||
  469. list_empty(&optimizing_list))
  470. return;
  471. arch_optimize_kprobes(&optimizing_list);
  472. }
  473. /*
  474. * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
  475. * if need) kprobes listed on 'unoptimizing_list'.
  476. */
  477. static void do_unoptimize_kprobes(void)
  478. {
  479. struct optimized_kprobe *op, *tmp;
  480. lockdep_assert_held(&text_mutex);
  481. /* See comment in do_optimize_kprobes() */
  482. lockdep_assert_cpus_held();
  483. if (!list_empty(&unoptimizing_list))
  484. arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
  485. /* Loop on 'freeing_list' for disarming and removing from kprobe hash list */
  486. list_for_each_entry_safe(op, tmp, &freeing_list, list) {
  487. /* Switching from detour code to origin */
  488. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  489. /* Disarm probes if marked disabled and not gone */
  490. if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp))
  491. arch_disarm_kprobe(&op->kp);
  492. if (kprobe_unused(&op->kp)) {
  493. /*
  494. * Remove unused probes from hash list. After waiting
  495. * for synchronization, these probes are reclaimed.
  496. * (reclaiming is done by do_free_cleaned_kprobes().)
  497. */
  498. hlist_del_rcu(&op->kp.hlist);
  499. } else
  500. list_del_init(&op->list);
  501. }
  502. }
  503. /* Reclaim all kprobes on the 'freeing_list' */
  504. static void do_free_cleaned_kprobes(void)
  505. {
  506. struct optimized_kprobe *op, *tmp;
  507. list_for_each_entry_safe(op, tmp, &freeing_list, list) {
  508. list_del_init(&op->list);
  509. if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
  510. /*
  511. * This must not happen, but if there is a kprobe
  512. * still in use, keep it on kprobes hash list.
  513. */
  514. continue;
  515. }
  516. free_aggr_kprobe(&op->kp);
  517. }
  518. }
  519. /* Start optimizer after OPTIMIZE_DELAY passed */
  520. static void kick_kprobe_optimizer(void)
  521. {
  522. schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
  523. }
  524. /* Kprobe jump optimizer */
  525. static void kprobe_optimizer(struct work_struct *work)
  526. {
  527. mutex_lock(&kprobe_mutex);
  528. cpus_read_lock();
  529. mutex_lock(&text_mutex);
  530. /*
  531. * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
  532. * kprobes before waiting for quiesence period.
  533. */
  534. do_unoptimize_kprobes();
  535. /*
  536. * Step 2: Wait for quiesence period to ensure all potentially
  537. * preempted tasks to have normally scheduled. Because optprobe
  538. * may modify multiple instructions, there is a chance that Nth
  539. * instruction is preempted. In that case, such tasks can return
  540. * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
  541. * Note that on non-preemptive kernel, this is transparently converted
  542. * to synchronoze_sched() to wait for all interrupts to have completed.
  543. */
  544. synchronize_rcu_tasks();
  545. /* Step 3: Optimize kprobes after quiesence period */
  546. do_optimize_kprobes();
  547. /* Step 4: Free cleaned kprobes after quiesence period */
  548. do_free_cleaned_kprobes();
  549. mutex_unlock(&text_mutex);
  550. cpus_read_unlock();
  551. /* Step 5: Kick optimizer again if needed */
  552. if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
  553. kick_kprobe_optimizer();
  554. mutex_unlock(&kprobe_mutex);
  555. }
  556. /* Wait for completing optimization and unoptimization */
  557. void wait_for_kprobe_optimizer(void)
  558. {
  559. mutex_lock(&kprobe_mutex);
  560. while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
  561. mutex_unlock(&kprobe_mutex);
  562. /* This will also make 'optimizing_work' execute immmediately */
  563. flush_delayed_work(&optimizing_work);
  564. /* 'optimizing_work' might not have been queued yet, relax */
  565. cpu_relax();
  566. mutex_lock(&kprobe_mutex);
  567. }
  568. mutex_unlock(&kprobe_mutex);
  569. }
  570. bool optprobe_queued_unopt(struct optimized_kprobe *op)
  571. {
  572. struct optimized_kprobe *_op;
  573. list_for_each_entry(_op, &unoptimizing_list, list) {
  574. if (op == _op)
  575. return true;
  576. }
  577. return false;
  578. }
  579. /* Optimize kprobe if p is ready to be optimized */
  580. static void optimize_kprobe(struct kprobe *p)
  581. {
  582. struct optimized_kprobe *op;
  583. /* Check if the kprobe is disabled or not ready for optimization. */
  584. if (!kprobe_optready(p) || !kprobes_allow_optimization ||
  585. (kprobe_disabled(p) || kprobes_all_disarmed))
  586. return;
  587. /* kprobes with 'post_handler' can not be optimized */
  588. if (p->post_handler)
  589. return;
  590. op = container_of(p, struct optimized_kprobe, kp);
  591. /* Check there is no other kprobes at the optimized instructions */
  592. if (arch_check_optimized_kprobe(op) < 0)
  593. return;
  594. /* Check if it is already optimized. */
  595. if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
  596. if (optprobe_queued_unopt(op)) {
  597. /* This is under unoptimizing. Just dequeue the probe */
  598. list_del_init(&op->list);
  599. }
  600. return;
  601. }
  602. op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
  603. /*
  604. * On the 'unoptimizing_list' and 'optimizing_list',
  605. * 'op' must have OPTIMIZED flag
  606. */
  607. if (WARN_ON_ONCE(!list_empty(&op->list)))
  608. return;
  609. list_add(&op->list, &optimizing_list);
  610. kick_kprobe_optimizer();
  611. }
  612. /* Short cut to direct unoptimizing */
  613. static void force_unoptimize_kprobe(struct optimized_kprobe *op)
  614. {
  615. lockdep_assert_cpus_held();
  616. arch_unoptimize_kprobe(op);
  617. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  618. }
  619. /* Unoptimize a kprobe if p is optimized */
  620. static void unoptimize_kprobe(struct kprobe *p, bool force)
  621. {
  622. struct optimized_kprobe *op;
  623. if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
  624. return; /* This is not an optprobe nor optimized */
  625. op = container_of(p, struct optimized_kprobe, kp);
  626. if (!kprobe_optimized(p))
  627. return;
  628. if (!list_empty(&op->list)) {
  629. if (optprobe_queued_unopt(op)) {
  630. /* Queued in unoptimizing queue */
  631. if (force) {
  632. /*
  633. * Forcibly unoptimize the kprobe here, and queue it
  634. * in the freeing list for release afterwards.
  635. */
  636. force_unoptimize_kprobe(op);
  637. list_move(&op->list, &freeing_list);
  638. }
  639. } else {
  640. /* Dequeue from the optimizing queue */
  641. list_del_init(&op->list);
  642. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  643. }
  644. return;
  645. }
  646. /* Optimized kprobe case */
  647. if (force) {
  648. /* Forcibly update the code: this is a special case */
  649. force_unoptimize_kprobe(op);
  650. } else {
  651. list_add(&op->list, &unoptimizing_list);
  652. kick_kprobe_optimizer();
  653. }
  654. }
  655. /* Cancel unoptimizing for reusing */
  656. static int reuse_unused_kprobe(struct kprobe *ap)
  657. {
  658. struct optimized_kprobe *op;
  659. /*
  660. * Unused kprobe MUST be on the way of delayed unoptimizing (means
  661. * there is still a relative jump) and disabled.
  662. */
  663. op = container_of(ap, struct optimized_kprobe, kp);
  664. WARN_ON_ONCE(list_empty(&op->list));
  665. /* Enable the probe again */
  666. ap->flags &= ~KPROBE_FLAG_DISABLED;
  667. /* Optimize it again. (remove from 'op->list') */
  668. if (!kprobe_optready(ap))
  669. return -EINVAL;
  670. optimize_kprobe(ap);
  671. return 0;
  672. }
  673. /* Remove optimized instructions */
  674. static void kill_optimized_kprobe(struct kprobe *p)
  675. {
  676. struct optimized_kprobe *op;
  677. op = container_of(p, struct optimized_kprobe, kp);
  678. if (!list_empty(&op->list))
  679. /* Dequeue from the (un)optimization queue */
  680. list_del_init(&op->list);
  681. op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
  682. if (kprobe_unused(p)) {
  683. /*
  684. * Unused kprobe is on unoptimizing or freeing list. We move it
  685. * to freeing_list and let the kprobe_optimizer() remove it from
  686. * the kprobe hash list and free it.
  687. */
  688. if (optprobe_queued_unopt(op))
  689. list_move(&op->list, &freeing_list);
  690. }
  691. /* Don't touch the code, because it is already freed. */
  692. arch_remove_optimized_kprobe(op);
  693. }
  694. static inline
  695. void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
  696. {
  697. if (!kprobe_ftrace(p))
  698. arch_prepare_optimized_kprobe(op, p);
  699. }
  700. /* Try to prepare optimized instructions */
  701. static void prepare_optimized_kprobe(struct kprobe *p)
  702. {
  703. struct optimized_kprobe *op;
  704. op = container_of(p, struct optimized_kprobe, kp);
  705. __prepare_optimized_kprobe(op, p);
  706. }
  707. /* Allocate new optimized_kprobe and try to prepare optimized instructions. */
  708. static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  709. {
  710. struct optimized_kprobe *op;
  711. op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
  712. if (!op)
  713. return NULL;
  714. INIT_LIST_HEAD(&op->list);
  715. op->kp.addr = p->addr;
  716. __prepare_optimized_kprobe(op, p);
  717. return &op->kp;
  718. }
  719. static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
  720. /*
  721. * Prepare an optimized_kprobe and optimize it.
  722. * NOTE: 'p' must be a normal registered kprobe.
  723. */
  724. static void try_to_optimize_kprobe(struct kprobe *p)
  725. {
  726. struct kprobe *ap;
  727. struct optimized_kprobe *op;
  728. /* Impossible to optimize ftrace-based kprobe. */
  729. if (kprobe_ftrace(p))
  730. return;
  731. /* For preparing optimization, jump_label_text_reserved() is called. */
  732. cpus_read_lock();
  733. jump_label_lock();
  734. mutex_lock(&text_mutex);
  735. ap = alloc_aggr_kprobe(p);
  736. if (!ap)
  737. goto out;
  738. op = container_of(ap, struct optimized_kprobe, kp);
  739. if (!arch_prepared_optinsn(&op->optinsn)) {
  740. /* If failed to setup optimizing, fallback to kprobe. */
  741. arch_remove_optimized_kprobe(op);
  742. kfree(op);
  743. goto out;
  744. }
  745. init_aggr_kprobe(ap, p);
  746. optimize_kprobe(ap); /* This just kicks optimizer thread. */
  747. out:
  748. mutex_unlock(&text_mutex);
  749. jump_label_unlock();
  750. cpus_read_unlock();
  751. }
  752. static void optimize_all_kprobes(void)
  753. {
  754. struct hlist_head *head;
  755. struct kprobe *p;
  756. unsigned int i;
  757. mutex_lock(&kprobe_mutex);
  758. /* If optimization is already allowed, just return. */
  759. if (kprobes_allow_optimization)
  760. goto out;
  761. cpus_read_lock();
  762. kprobes_allow_optimization = true;
  763. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  764. head = &kprobe_table[i];
  765. hlist_for_each_entry(p, head, hlist)
  766. if (!kprobe_disabled(p))
  767. optimize_kprobe(p);
  768. }
  769. cpus_read_unlock();
  770. pr_info("kprobe jump-optimization is enabled. All kprobes are optimized if possible.\n");
  771. out:
  772. mutex_unlock(&kprobe_mutex);
  773. }
  774. #ifdef CONFIG_SYSCTL
  775. static void unoptimize_all_kprobes(void)
  776. {
  777. struct hlist_head *head;
  778. struct kprobe *p;
  779. unsigned int i;
  780. mutex_lock(&kprobe_mutex);
  781. /* If optimization is already prohibited, just return. */
  782. if (!kprobes_allow_optimization) {
  783. mutex_unlock(&kprobe_mutex);
  784. return;
  785. }
  786. cpus_read_lock();
  787. kprobes_allow_optimization = false;
  788. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  789. head = &kprobe_table[i];
  790. hlist_for_each_entry(p, head, hlist) {
  791. if (!kprobe_disabled(p))
  792. unoptimize_kprobe(p, false);
  793. }
  794. }
  795. cpus_read_unlock();
  796. mutex_unlock(&kprobe_mutex);
  797. /* Wait for unoptimizing completion. */
  798. wait_for_kprobe_optimizer();
  799. pr_info("kprobe jump-optimization is disabled. All kprobes are based on software breakpoint.\n");
  800. }
  801. static DEFINE_MUTEX(kprobe_sysctl_mutex);
  802. static int sysctl_kprobes_optimization;
  803. static int proc_kprobes_optimization_handler(const struct ctl_table *table,
  804. int write, void *buffer,
  805. size_t *length, loff_t *ppos)
  806. {
  807. int ret;
  808. mutex_lock(&kprobe_sysctl_mutex);
  809. sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
  810. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  811. if (sysctl_kprobes_optimization)
  812. optimize_all_kprobes();
  813. else
  814. unoptimize_all_kprobes();
  815. mutex_unlock(&kprobe_sysctl_mutex);
  816. return ret;
  817. }
  818. static struct ctl_table kprobe_sysctls[] = {
  819. {
  820. .procname = "kprobes-optimization",
  821. .data = &sysctl_kprobes_optimization,
  822. .maxlen = sizeof(int),
  823. .mode = 0644,
  824. .proc_handler = proc_kprobes_optimization_handler,
  825. .extra1 = SYSCTL_ZERO,
  826. .extra2 = SYSCTL_ONE,
  827. },
  828. };
  829. static void __init kprobe_sysctls_init(void)
  830. {
  831. register_sysctl_init("debug", kprobe_sysctls);
  832. }
  833. #endif /* CONFIG_SYSCTL */
  834. /* Put a breakpoint for a probe. */
  835. static void __arm_kprobe(struct kprobe *p)
  836. {
  837. struct kprobe *_p;
  838. lockdep_assert_held(&text_mutex);
  839. /* Find the overlapping optimized kprobes. */
  840. _p = get_optimized_kprobe(p->addr);
  841. if (unlikely(_p))
  842. /* Fallback to unoptimized kprobe */
  843. unoptimize_kprobe(_p, true);
  844. arch_arm_kprobe(p);
  845. optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
  846. }
  847. /* Remove the breakpoint of a probe. */
  848. static void __disarm_kprobe(struct kprobe *p, bool reopt)
  849. {
  850. struct kprobe *_p;
  851. lockdep_assert_held(&text_mutex);
  852. /* Try to unoptimize */
  853. unoptimize_kprobe(p, kprobes_all_disarmed);
  854. if (!kprobe_queued(p)) {
  855. arch_disarm_kprobe(p);
  856. /* If another kprobe was blocked, re-optimize it. */
  857. _p = get_optimized_kprobe(p->addr);
  858. if (unlikely(_p) && reopt)
  859. optimize_kprobe(_p);
  860. }
  861. /*
  862. * TODO: Since unoptimization and real disarming will be done by
  863. * the worker thread, we can not check whether another probe are
  864. * unoptimized because of this probe here. It should be re-optimized
  865. * by the worker thread.
  866. */
  867. }
  868. #else /* !CONFIG_OPTPROBES */
  869. #define optimize_kprobe(p) do {} while (0)
  870. #define unoptimize_kprobe(p, f) do {} while (0)
  871. #define kill_optimized_kprobe(p) do {} while (0)
  872. #define prepare_optimized_kprobe(p) do {} while (0)
  873. #define try_to_optimize_kprobe(p) do {} while (0)
  874. #define __arm_kprobe(p) arch_arm_kprobe(p)
  875. #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
  876. #define kprobe_disarmed(p) kprobe_disabled(p)
  877. #define wait_for_kprobe_optimizer() do {} while (0)
  878. static int reuse_unused_kprobe(struct kprobe *ap)
  879. {
  880. /*
  881. * If the optimized kprobe is NOT supported, the aggr kprobe is
  882. * released at the same time that the last aggregated kprobe is
  883. * unregistered.
  884. * Thus there should be no chance to reuse unused kprobe.
  885. */
  886. WARN_ON_ONCE(1);
  887. return -EINVAL;
  888. }
  889. static void free_aggr_kprobe(struct kprobe *p)
  890. {
  891. arch_remove_kprobe(p);
  892. kfree(p);
  893. }
  894. static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
  895. {
  896. return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  897. }
  898. #endif /* CONFIG_OPTPROBES */
  899. #ifdef CONFIG_KPROBES_ON_FTRACE
  900. static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
  901. .func = kprobe_ftrace_handler,
  902. .flags = FTRACE_OPS_FL_SAVE_REGS,
  903. };
  904. static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
  905. .func = kprobe_ftrace_handler,
  906. .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
  907. };
  908. static int kprobe_ipmodify_enabled;
  909. static int kprobe_ftrace_enabled;
  910. bool kprobe_ftrace_disabled;
  911. static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
  912. int *cnt)
  913. {
  914. int ret;
  915. lockdep_assert_held(&kprobe_mutex);
  916. ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
  917. if (WARN_ONCE(ret < 0, "Failed to arm kprobe-ftrace at %pS (error %d)\n", p->addr, ret))
  918. return ret;
  919. if (*cnt == 0) {
  920. ret = register_ftrace_function(ops);
  921. if (WARN(ret < 0, "Failed to register kprobe-ftrace (error %d)\n", ret))
  922. goto err_ftrace;
  923. }
  924. (*cnt)++;
  925. return ret;
  926. err_ftrace:
  927. /*
  928. * At this point, sinec ops is not registered, we should be sefe from
  929. * registering empty filter.
  930. */
  931. ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
  932. return ret;
  933. }
  934. static int arm_kprobe_ftrace(struct kprobe *p)
  935. {
  936. bool ipmodify = (p->post_handler != NULL);
  937. return __arm_kprobe_ftrace(p,
  938. ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
  939. ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
  940. }
  941. static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
  942. int *cnt)
  943. {
  944. int ret;
  945. lockdep_assert_held(&kprobe_mutex);
  946. if (*cnt == 1) {
  947. ret = unregister_ftrace_function(ops);
  948. if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (error %d)\n", ret))
  949. return ret;
  950. }
  951. (*cnt)--;
  952. ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
  953. WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n",
  954. p->addr, ret);
  955. return ret;
  956. }
  957. static int disarm_kprobe_ftrace(struct kprobe *p)
  958. {
  959. bool ipmodify = (p->post_handler != NULL);
  960. return __disarm_kprobe_ftrace(p,
  961. ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
  962. ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
  963. }
  964. void kprobe_ftrace_kill(void)
  965. {
  966. kprobe_ftrace_disabled = true;
  967. }
  968. #else /* !CONFIG_KPROBES_ON_FTRACE */
  969. static inline int arm_kprobe_ftrace(struct kprobe *p)
  970. {
  971. return -ENODEV;
  972. }
  973. static inline int disarm_kprobe_ftrace(struct kprobe *p)
  974. {
  975. return -ENODEV;
  976. }
  977. #endif
  978. static int prepare_kprobe(struct kprobe *p)
  979. {
  980. /* Must ensure p->addr is really on ftrace */
  981. if (kprobe_ftrace(p))
  982. return arch_prepare_kprobe_ftrace(p);
  983. return arch_prepare_kprobe(p);
  984. }
  985. static int arm_kprobe(struct kprobe *kp)
  986. {
  987. if (unlikely(kprobe_ftrace(kp)))
  988. return arm_kprobe_ftrace(kp);
  989. cpus_read_lock();
  990. mutex_lock(&text_mutex);
  991. __arm_kprobe(kp);
  992. mutex_unlock(&text_mutex);
  993. cpus_read_unlock();
  994. return 0;
  995. }
  996. static int disarm_kprobe(struct kprobe *kp, bool reopt)
  997. {
  998. if (unlikely(kprobe_ftrace(kp)))
  999. return disarm_kprobe_ftrace(kp);
  1000. cpus_read_lock();
  1001. mutex_lock(&text_mutex);
  1002. __disarm_kprobe(kp, reopt);
  1003. mutex_unlock(&text_mutex);
  1004. cpus_read_unlock();
  1005. return 0;
  1006. }
  1007. /*
  1008. * Aggregate handlers for multiple kprobes support - these handlers
  1009. * take care of invoking the individual kprobe handlers on p->list
  1010. */
  1011. static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  1012. {
  1013. struct kprobe *kp;
  1014. list_for_each_entry_rcu(kp, &p->list, list) {
  1015. if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
  1016. set_kprobe_instance(kp);
  1017. if (kp->pre_handler(kp, regs))
  1018. return 1;
  1019. }
  1020. reset_kprobe_instance();
  1021. }
  1022. return 0;
  1023. }
  1024. NOKPROBE_SYMBOL(aggr_pre_handler);
  1025. static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  1026. unsigned long flags)
  1027. {
  1028. struct kprobe *kp;
  1029. list_for_each_entry_rcu(kp, &p->list, list) {
  1030. if (kp->post_handler && likely(!kprobe_disabled(kp))) {
  1031. set_kprobe_instance(kp);
  1032. kp->post_handler(kp, regs, flags);
  1033. reset_kprobe_instance();
  1034. }
  1035. }
  1036. }
  1037. NOKPROBE_SYMBOL(aggr_post_handler);
  1038. /* Walks the list and increments 'nmissed' if 'p' has child probes. */
  1039. void kprobes_inc_nmissed_count(struct kprobe *p)
  1040. {
  1041. struct kprobe *kp;
  1042. if (!kprobe_aggrprobe(p)) {
  1043. p->nmissed++;
  1044. } else {
  1045. list_for_each_entry_rcu(kp, &p->list, list)
  1046. kp->nmissed++;
  1047. }
  1048. }
  1049. NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
  1050. static struct kprobe kprobe_busy = {
  1051. .addr = (void *) get_kprobe,
  1052. };
  1053. void kprobe_busy_begin(void)
  1054. {
  1055. struct kprobe_ctlblk *kcb;
  1056. preempt_disable();
  1057. __this_cpu_write(current_kprobe, &kprobe_busy);
  1058. kcb = get_kprobe_ctlblk();
  1059. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  1060. }
  1061. void kprobe_busy_end(void)
  1062. {
  1063. __this_cpu_write(current_kprobe, NULL);
  1064. preempt_enable();
  1065. }
  1066. /* Add the new probe to 'ap->list'. */
  1067. static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
  1068. {
  1069. if (p->post_handler)
  1070. unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
  1071. list_add_rcu(&p->list, &ap->list);
  1072. if (p->post_handler && !ap->post_handler)
  1073. ap->post_handler = aggr_post_handler;
  1074. return 0;
  1075. }
  1076. /*
  1077. * Fill in the required fields of the aggregator kprobe. Replace the
  1078. * earlier kprobe in the hlist with the aggregator kprobe.
  1079. */
  1080. static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  1081. {
  1082. /* Copy the insn slot of 'p' to 'ap'. */
  1083. copy_kprobe(p, ap);
  1084. flush_insn_slot(ap);
  1085. ap->addr = p->addr;
  1086. ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
  1087. ap->pre_handler = aggr_pre_handler;
  1088. /* We don't care the kprobe which has gone. */
  1089. if (p->post_handler && !kprobe_gone(p))
  1090. ap->post_handler = aggr_post_handler;
  1091. INIT_LIST_HEAD(&ap->list);
  1092. INIT_HLIST_NODE(&ap->hlist);
  1093. list_add_rcu(&p->list, &ap->list);
  1094. hlist_replace_rcu(&p->hlist, &ap->hlist);
  1095. }
  1096. /*
  1097. * This registers the second or subsequent kprobe at the same address.
  1098. */
  1099. static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
  1100. {
  1101. int ret = 0;
  1102. struct kprobe *ap = orig_p;
  1103. cpus_read_lock();
  1104. /* For preparing optimization, jump_label_text_reserved() is called */
  1105. jump_label_lock();
  1106. mutex_lock(&text_mutex);
  1107. if (!kprobe_aggrprobe(orig_p)) {
  1108. /* If 'orig_p' is not an 'aggr_kprobe', create new one. */
  1109. ap = alloc_aggr_kprobe(orig_p);
  1110. if (!ap) {
  1111. ret = -ENOMEM;
  1112. goto out;
  1113. }
  1114. init_aggr_kprobe(ap, orig_p);
  1115. } else if (kprobe_unused(ap)) {
  1116. /* This probe is going to die. Rescue it */
  1117. ret = reuse_unused_kprobe(ap);
  1118. if (ret)
  1119. goto out;
  1120. }
  1121. if (kprobe_gone(ap)) {
  1122. /*
  1123. * Attempting to insert new probe at the same location that
  1124. * had a probe in the module vaddr area which already
  1125. * freed. So, the instruction slot has already been
  1126. * released. We need a new slot for the new probe.
  1127. */
  1128. ret = arch_prepare_kprobe(ap);
  1129. if (ret)
  1130. /*
  1131. * Even if fail to allocate new slot, don't need to
  1132. * free the 'ap'. It will be used next time, or
  1133. * freed by unregister_kprobe().
  1134. */
  1135. goto out;
  1136. /* Prepare optimized instructions if possible. */
  1137. prepare_optimized_kprobe(ap);
  1138. /*
  1139. * Clear gone flag to prevent allocating new slot again, and
  1140. * set disabled flag because it is not armed yet.
  1141. */
  1142. ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
  1143. | KPROBE_FLAG_DISABLED;
  1144. }
  1145. /* Copy the insn slot of 'p' to 'ap'. */
  1146. copy_kprobe(ap, p);
  1147. ret = add_new_kprobe(ap, p);
  1148. out:
  1149. mutex_unlock(&text_mutex);
  1150. jump_label_unlock();
  1151. cpus_read_unlock();
  1152. if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
  1153. ap->flags &= ~KPROBE_FLAG_DISABLED;
  1154. if (!kprobes_all_disarmed) {
  1155. /* Arm the breakpoint again. */
  1156. ret = arm_kprobe(ap);
  1157. if (ret) {
  1158. ap->flags |= KPROBE_FLAG_DISABLED;
  1159. list_del_rcu(&p->list);
  1160. synchronize_rcu();
  1161. }
  1162. }
  1163. }
  1164. return ret;
  1165. }
  1166. bool __weak arch_within_kprobe_blacklist(unsigned long addr)
  1167. {
  1168. /* The '__kprobes' functions and entry code must not be probed. */
  1169. return addr >= (unsigned long)__kprobes_text_start &&
  1170. addr < (unsigned long)__kprobes_text_end;
  1171. }
  1172. static bool __within_kprobe_blacklist(unsigned long addr)
  1173. {
  1174. struct kprobe_blacklist_entry *ent;
  1175. if (arch_within_kprobe_blacklist(addr))
  1176. return true;
  1177. /*
  1178. * If 'kprobe_blacklist' is defined, check the address and
  1179. * reject any probe registration in the prohibited area.
  1180. */
  1181. list_for_each_entry(ent, &kprobe_blacklist, list) {
  1182. if (addr >= ent->start_addr && addr < ent->end_addr)
  1183. return true;
  1184. }
  1185. return false;
  1186. }
  1187. bool within_kprobe_blacklist(unsigned long addr)
  1188. {
  1189. char symname[KSYM_NAME_LEN], *p;
  1190. if (__within_kprobe_blacklist(addr))
  1191. return true;
  1192. /* Check if the address is on a suffixed-symbol */
  1193. if (!lookup_symbol_name(addr, symname)) {
  1194. p = strchr(symname, '.');
  1195. if (!p)
  1196. return false;
  1197. *p = '\0';
  1198. addr = (unsigned long)kprobe_lookup_name(symname, 0);
  1199. if (addr)
  1200. return __within_kprobe_blacklist(addr);
  1201. }
  1202. return false;
  1203. }
  1204. /*
  1205. * arch_adjust_kprobe_addr - adjust the address
  1206. * @addr: symbol base address
  1207. * @offset: offset within the symbol
  1208. * @on_func_entry: was this @addr+@offset on the function entry
  1209. *
  1210. * Typically returns @addr + @offset, except for special cases where the
  1211. * function might be prefixed by a CFI landing pad, in that case any offset
  1212. * inside the landing pad is mapped to the first 'real' instruction of the
  1213. * symbol.
  1214. *
  1215. * Specifically, for things like IBT/BTI, skip the resp. ENDBR/BTI.C
  1216. * instruction at +0.
  1217. */
  1218. kprobe_opcode_t *__weak arch_adjust_kprobe_addr(unsigned long addr,
  1219. unsigned long offset,
  1220. bool *on_func_entry)
  1221. {
  1222. *on_func_entry = !offset;
  1223. return (kprobe_opcode_t *)(addr + offset);
  1224. }
  1225. /*
  1226. * If 'symbol_name' is specified, look it up and add the 'offset'
  1227. * to it. This way, we can specify a relative address to a symbol.
  1228. * This returns encoded errors if it fails to look up symbol or invalid
  1229. * combination of parameters.
  1230. */
  1231. static kprobe_opcode_t *
  1232. _kprobe_addr(kprobe_opcode_t *addr, const char *symbol_name,
  1233. unsigned long offset, bool *on_func_entry)
  1234. {
  1235. if ((symbol_name && addr) || (!symbol_name && !addr))
  1236. goto invalid;
  1237. if (symbol_name) {
  1238. /*
  1239. * Input: @sym + @offset
  1240. * Output: @addr + @offset
  1241. *
  1242. * NOTE: kprobe_lookup_name() does *NOT* fold the offset
  1243. * argument into it's output!
  1244. */
  1245. addr = kprobe_lookup_name(symbol_name, offset);
  1246. if (!addr)
  1247. return ERR_PTR(-ENOENT);
  1248. }
  1249. /*
  1250. * So here we have @addr + @offset, displace it into a new
  1251. * @addr' + @offset' where @addr' is the symbol start address.
  1252. */
  1253. addr = (void *)addr + offset;
  1254. if (!kallsyms_lookup_size_offset((unsigned long)addr, NULL, &offset))
  1255. return ERR_PTR(-ENOENT);
  1256. addr = (void *)addr - offset;
  1257. /*
  1258. * Then ask the architecture to re-combine them, taking care of
  1259. * magical function entry details while telling us if this was indeed
  1260. * at the start of the function.
  1261. */
  1262. addr = arch_adjust_kprobe_addr((unsigned long)addr, offset, on_func_entry);
  1263. if (addr)
  1264. return addr;
  1265. invalid:
  1266. return ERR_PTR(-EINVAL);
  1267. }
  1268. static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
  1269. {
  1270. bool on_func_entry;
  1271. return _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry);
  1272. }
  1273. /*
  1274. * Check the 'p' is valid and return the aggregator kprobe
  1275. * at the same address.
  1276. */
  1277. static struct kprobe *__get_valid_kprobe(struct kprobe *p)
  1278. {
  1279. struct kprobe *ap, *list_p;
  1280. lockdep_assert_held(&kprobe_mutex);
  1281. ap = get_kprobe(p->addr);
  1282. if (unlikely(!ap))
  1283. return NULL;
  1284. if (p != ap) {
  1285. list_for_each_entry(list_p, &ap->list, list)
  1286. if (list_p == p)
  1287. /* kprobe p is a valid probe */
  1288. goto valid;
  1289. return NULL;
  1290. }
  1291. valid:
  1292. return ap;
  1293. }
  1294. /*
  1295. * Warn and return error if the kprobe is being re-registered since
  1296. * there must be a software bug.
  1297. */
  1298. static inline int warn_kprobe_rereg(struct kprobe *p)
  1299. {
  1300. int ret = 0;
  1301. mutex_lock(&kprobe_mutex);
  1302. if (WARN_ON_ONCE(__get_valid_kprobe(p)))
  1303. ret = -EINVAL;
  1304. mutex_unlock(&kprobe_mutex);
  1305. return ret;
  1306. }
  1307. static int check_ftrace_location(struct kprobe *p)
  1308. {
  1309. unsigned long addr = (unsigned long)p->addr;
  1310. if (ftrace_location(addr) == addr) {
  1311. #ifdef CONFIG_KPROBES_ON_FTRACE
  1312. p->flags |= KPROBE_FLAG_FTRACE;
  1313. #else /* !CONFIG_KPROBES_ON_FTRACE */
  1314. return -EINVAL;
  1315. #endif
  1316. }
  1317. return 0;
  1318. }
  1319. static bool is_cfi_preamble_symbol(unsigned long addr)
  1320. {
  1321. char symbuf[KSYM_NAME_LEN];
  1322. if (lookup_symbol_name(addr, symbuf))
  1323. return false;
  1324. return str_has_prefix(symbuf, "__cfi_") ||
  1325. str_has_prefix(symbuf, "__pfx_");
  1326. }
  1327. static int check_kprobe_address_safe(struct kprobe *p,
  1328. struct module **probed_mod)
  1329. {
  1330. int ret;
  1331. ret = check_ftrace_location(p);
  1332. if (ret)
  1333. return ret;
  1334. jump_label_lock();
  1335. preempt_disable();
  1336. /* Ensure the address is in a text area, and find a module if exists. */
  1337. *probed_mod = NULL;
  1338. if (!core_kernel_text((unsigned long) p->addr)) {
  1339. *probed_mod = __module_text_address((unsigned long) p->addr);
  1340. if (!(*probed_mod)) {
  1341. ret = -EINVAL;
  1342. goto out;
  1343. }
  1344. }
  1345. /* Ensure it is not in reserved area. */
  1346. if (in_gate_area_no_mm((unsigned long) p->addr) ||
  1347. within_kprobe_blacklist((unsigned long) p->addr) ||
  1348. jump_label_text_reserved(p->addr, p->addr) ||
  1349. static_call_text_reserved(p->addr, p->addr) ||
  1350. find_bug((unsigned long)p->addr) ||
  1351. is_cfi_preamble_symbol((unsigned long)p->addr)) {
  1352. ret = -EINVAL;
  1353. goto out;
  1354. }
  1355. /* Get module refcount and reject __init functions for loaded modules. */
  1356. if (IS_ENABLED(CONFIG_MODULES) && *probed_mod) {
  1357. /*
  1358. * We must hold a refcount of the probed module while updating
  1359. * its code to prohibit unexpected unloading.
  1360. */
  1361. if (unlikely(!try_module_get(*probed_mod))) {
  1362. ret = -ENOENT;
  1363. goto out;
  1364. }
  1365. /*
  1366. * If the module freed '.init.text', we couldn't insert
  1367. * kprobes in there.
  1368. */
  1369. if (within_module_init((unsigned long)p->addr, *probed_mod) &&
  1370. !module_is_coming(*probed_mod)) {
  1371. module_put(*probed_mod);
  1372. *probed_mod = NULL;
  1373. ret = -ENOENT;
  1374. }
  1375. }
  1376. out:
  1377. preempt_enable();
  1378. jump_label_unlock();
  1379. return ret;
  1380. }
  1381. int register_kprobe(struct kprobe *p)
  1382. {
  1383. int ret;
  1384. struct kprobe *old_p;
  1385. struct module *probed_mod;
  1386. kprobe_opcode_t *addr;
  1387. bool on_func_entry;
  1388. /* Adjust probe address from symbol */
  1389. addr = _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry);
  1390. if (IS_ERR(addr))
  1391. return PTR_ERR(addr);
  1392. p->addr = addr;
  1393. ret = warn_kprobe_rereg(p);
  1394. if (ret)
  1395. return ret;
  1396. /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
  1397. p->flags &= KPROBE_FLAG_DISABLED;
  1398. p->nmissed = 0;
  1399. INIT_LIST_HEAD(&p->list);
  1400. ret = check_kprobe_address_safe(p, &probed_mod);
  1401. if (ret)
  1402. return ret;
  1403. mutex_lock(&kprobe_mutex);
  1404. if (on_func_entry)
  1405. p->flags |= KPROBE_FLAG_ON_FUNC_ENTRY;
  1406. old_p = get_kprobe(p->addr);
  1407. if (old_p) {
  1408. /* Since this may unoptimize 'old_p', locking 'text_mutex'. */
  1409. ret = register_aggr_kprobe(old_p, p);
  1410. goto out;
  1411. }
  1412. cpus_read_lock();
  1413. /* Prevent text modification */
  1414. mutex_lock(&text_mutex);
  1415. ret = prepare_kprobe(p);
  1416. mutex_unlock(&text_mutex);
  1417. cpus_read_unlock();
  1418. if (ret)
  1419. goto out;
  1420. INIT_HLIST_NODE(&p->hlist);
  1421. hlist_add_head_rcu(&p->hlist,
  1422. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  1423. if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
  1424. ret = arm_kprobe(p);
  1425. if (ret) {
  1426. hlist_del_rcu(&p->hlist);
  1427. synchronize_rcu();
  1428. goto out;
  1429. }
  1430. }
  1431. /* Try to optimize kprobe */
  1432. try_to_optimize_kprobe(p);
  1433. out:
  1434. mutex_unlock(&kprobe_mutex);
  1435. if (probed_mod)
  1436. module_put(probed_mod);
  1437. return ret;
  1438. }
  1439. EXPORT_SYMBOL_GPL(register_kprobe);
  1440. /* Check if all probes on the 'ap' are disabled. */
  1441. static bool aggr_kprobe_disabled(struct kprobe *ap)
  1442. {
  1443. struct kprobe *kp;
  1444. lockdep_assert_held(&kprobe_mutex);
  1445. list_for_each_entry(kp, &ap->list, list)
  1446. if (!kprobe_disabled(kp))
  1447. /*
  1448. * Since there is an active probe on the list,
  1449. * we can't disable this 'ap'.
  1450. */
  1451. return false;
  1452. return true;
  1453. }
  1454. static struct kprobe *__disable_kprobe(struct kprobe *p)
  1455. {
  1456. struct kprobe *orig_p;
  1457. int ret;
  1458. lockdep_assert_held(&kprobe_mutex);
  1459. /* Get an original kprobe for return */
  1460. orig_p = __get_valid_kprobe(p);
  1461. if (unlikely(orig_p == NULL))
  1462. return ERR_PTR(-EINVAL);
  1463. if (!kprobe_disabled(p)) {
  1464. /* Disable probe if it is a child probe */
  1465. if (p != orig_p)
  1466. p->flags |= KPROBE_FLAG_DISABLED;
  1467. /* Try to disarm and disable this/parent probe */
  1468. if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
  1469. /*
  1470. * Don't be lazy here. Even if 'kprobes_all_disarmed'
  1471. * is false, 'orig_p' might not have been armed yet.
  1472. * Note arm_all_kprobes() __tries__ to arm all kprobes
  1473. * on the best effort basis.
  1474. */
  1475. if (!kprobes_all_disarmed && !kprobe_disabled(orig_p)) {
  1476. ret = disarm_kprobe(orig_p, true);
  1477. if (ret) {
  1478. p->flags &= ~KPROBE_FLAG_DISABLED;
  1479. return ERR_PTR(ret);
  1480. }
  1481. }
  1482. orig_p->flags |= KPROBE_FLAG_DISABLED;
  1483. }
  1484. }
  1485. return orig_p;
  1486. }
  1487. /*
  1488. * Unregister a kprobe without a scheduler synchronization.
  1489. */
  1490. static int __unregister_kprobe_top(struct kprobe *p)
  1491. {
  1492. struct kprobe *ap, *list_p;
  1493. /* Disable kprobe. This will disarm it if needed. */
  1494. ap = __disable_kprobe(p);
  1495. if (IS_ERR(ap))
  1496. return PTR_ERR(ap);
  1497. if (ap == p)
  1498. /*
  1499. * This probe is an independent(and non-optimized) kprobe
  1500. * (not an aggrprobe). Remove from the hash list.
  1501. */
  1502. goto disarmed;
  1503. /* Following process expects this probe is an aggrprobe */
  1504. WARN_ON(!kprobe_aggrprobe(ap));
  1505. if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
  1506. /*
  1507. * !disarmed could be happen if the probe is under delayed
  1508. * unoptimizing.
  1509. */
  1510. goto disarmed;
  1511. else {
  1512. /* If disabling probe has special handlers, update aggrprobe */
  1513. if (p->post_handler && !kprobe_gone(p)) {
  1514. list_for_each_entry(list_p, &ap->list, list) {
  1515. if ((list_p != p) && (list_p->post_handler))
  1516. goto noclean;
  1517. }
  1518. /*
  1519. * For the kprobe-on-ftrace case, we keep the
  1520. * post_handler setting to identify this aggrprobe
  1521. * armed with kprobe_ipmodify_ops.
  1522. */
  1523. if (!kprobe_ftrace(ap))
  1524. ap->post_handler = NULL;
  1525. }
  1526. noclean:
  1527. /*
  1528. * Remove from the aggrprobe: this path will do nothing in
  1529. * __unregister_kprobe_bottom().
  1530. */
  1531. list_del_rcu(&p->list);
  1532. if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
  1533. /*
  1534. * Try to optimize this probe again, because post
  1535. * handler may have been changed.
  1536. */
  1537. optimize_kprobe(ap);
  1538. }
  1539. return 0;
  1540. disarmed:
  1541. hlist_del_rcu(&ap->hlist);
  1542. return 0;
  1543. }
  1544. static void __unregister_kprobe_bottom(struct kprobe *p)
  1545. {
  1546. struct kprobe *ap;
  1547. if (list_empty(&p->list))
  1548. /* This is an independent kprobe */
  1549. arch_remove_kprobe(p);
  1550. else if (list_is_singular(&p->list)) {
  1551. /* This is the last child of an aggrprobe */
  1552. ap = list_entry(p->list.next, struct kprobe, list);
  1553. list_del(&p->list);
  1554. free_aggr_kprobe(ap);
  1555. }
  1556. /* Otherwise, do nothing. */
  1557. }
  1558. int register_kprobes(struct kprobe **kps, int num)
  1559. {
  1560. int i, ret = 0;
  1561. if (num <= 0)
  1562. return -EINVAL;
  1563. for (i = 0; i < num; i++) {
  1564. ret = register_kprobe(kps[i]);
  1565. if (ret < 0) {
  1566. if (i > 0)
  1567. unregister_kprobes(kps, i);
  1568. break;
  1569. }
  1570. }
  1571. return ret;
  1572. }
  1573. EXPORT_SYMBOL_GPL(register_kprobes);
  1574. void unregister_kprobe(struct kprobe *p)
  1575. {
  1576. unregister_kprobes(&p, 1);
  1577. }
  1578. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1579. void unregister_kprobes(struct kprobe **kps, int num)
  1580. {
  1581. int i;
  1582. if (num <= 0)
  1583. return;
  1584. mutex_lock(&kprobe_mutex);
  1585. for (i = 0; i < num; i++)
  1586. if (__unregister_kprobe_top(kps[i]) < 0)
  1587. kps[i]->addr = NULL;
  1588. mutex_unlock(&kprobe_mutex);
  1589. synchronize_rcu();
  1590. for (i = 0; i < num; i++)
  1591. if (kps[i]->addr)
  1592. __unregister_kprobe_bottom(kps[i]);
  1593. }
  1594. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1595. int __weak kprobe_exceptions_notify(struct notifier_block *self,
  1596. unsigned long val, void *data)
  1597. {
  1598. return NOTIFY_DONE;
  1599. }
  1600. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  1601. static struct notifier_block kprobe_exceptions_nb = {
  1602. .notifier_call = kprobe_exceptions_notify,
  1603. .priority = 0x7fffffff /* we need to be notified first */
  1604. };
  1605. #ifdef CONFIG_KRETPROBES
  1606. #if !defined(CONFIG_KRETPROBE_ON_RETHOOK)
  1607. /* callbacks for objpool of kretprobe instances */
  1608. static int kretprobe_init_inst(void *nod, void *context)
  1609. {
  1610. struct kretprobe_instance *ri = nod;
  1611. ri->rph = context;
  1612. return 0;
  1613. }
  1614. static int kretprobe_fini_pool(struct objpool_head *head, void *context)
  1615. {
  1616. kfree(context);
  1617. return 0;
  1618. }
  1619. static void free_rp_inst_rcu(struct rcu_head *head)
  1620. {
  1621. struct kretprobe_instance *ri = container_of(head, struct kretprobe_instance, rcu);
  1622. struct kretprobe_holder *rph = ri->rph;
  1623. objpool_drop(ri, &rph->pool);
  1624. }
  1625. NOKPROBE_SYMBOL(free_rp_inst_rcu);
  1626. static void recycle_rp_inst(struct kretprobe_instance *ri)
  1627. {
  1628. struct kretprobe *rp = get_kretprobe(ri);
  1629. if (likely(rp))
  1630. objpool_push(ri, &rp->rph->pool);
  1631. else
  1632. call_rcu(&ri->rcu, free_rp_inst_rcu);
  1633. }
  1634. NOKPROBE_SYMBOL(recycle_rp_inst);
  1635. /*
  1636. * This function is called from delayed_put_task_struct() when a task is
  1637. * dead and cleaned up to recycle any kretprobe instances associated with
  1638. * this task. These left over instances represent probed functions that
  1639. * have been called but will never return.
  1640. */
  1641. void kprobe_flush_task(struct task_struct *tk)
  1642. {
  1643. struct kretprobe_instance *ri;
  1644. struct llist_node *node;
  1645. /* Early boot, not yet initialized. */
  1646. if (unlikely(!kprobes_initialized))
  1647. return;
  1648. kprobe_busy_begin();
  1649. node = __llist_del_all(&tk->kretprobe_instances);
  1650. while (node) {
  1651. ri = container_of(node, struct kretprobe_instance, llist);
  1652. node = node->next;
  1653. recycle_rp_inst(ri);
  1654. }
  1655. kprobe_busy_end();
  1656. }
  1657. NOKPROBE_SYMBOL(kprobe_flush_task);
  1658. static inline void free_rp_inst(struct kretprobe *rp)
  1659. {
  1660. struct kretprobe_holder *rph = rp->rph;
  1661. if (!rph)
  1662. return;
  1663. rp->rph = NULL;
  1664. objpool_fini(&rph->pool);
  1665. }
  1666. /* This assumes the 'tsk' is the current task or the is not running. */
  1667. static kprobe_opcode_t *__kretprobe_find_ret_addr(struct task_struct *tsk,
  1668. struct llist_node **cur)
  1669. {
  1670. struct kretprobe_instance *ri = NULL;
  1671. struct llist_node *node = *cur;
  1672. if (!node)
  1673. node = tsk->kretprobe_instances.first;
  1674. else
  1675. node = node->next;
  1676. while (node) {
  1677. ri = container_of(node, struct kretprobe_instance, llist);
  1678. if (ri->ret_addr != kretprobe_trampoline_addr()) {
  1679. *cur = node;
  1680. return ri->ret_addr;
  1681. }
  1682. node = node->next;
  1683. }
  1684. return NULL;
  1685. }
  1686. NOKPROBE_SYMBOL(__kretprobe_find_ret_addr);
  1687. /**
  1688. * kretprobe_find_ret_addr -- Find correct return address modified by kretprobe
  1689. * @tsk: Target task
  1690. * @fp: A frame pointer
  1691. * @cur: a storage of the loop cursor llist_node pointer for next call
  1692. *
  1693. * Find the correct return address modified by a kretprobe on @tsk in unsigned
  1694. * long type. If it finds the return address, this returns that address value,
  1695. * or this returns 0.
  1696. * The @tsk must be 'current' or a task which is not running. @fp is a hint
  1697. * to get the currect return address - which is compared with the
  1698. * kretprobe_instance::fp field. The @cur is a loop cursor for searching the
  1699. * kretprobe return addresses on the @tsk. The '*@cur' should be NULL at the
  1700. * first call, but '@cur' itself must NOT NULL.
  1701. */
  1702. unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
  1703. struct llist_node **cur)
  1704. {
  1705. struct kretprobe_instance *ri;
  1706. kprobe_opcode_t *ret;
  1707. if (WARN_ON_ONCE(!cur))
  1708. return 0;
  1709. do {
  1710. ret = __kretprobe_find_ret_addr(tsk, cur);
  1711. if (!ret)
  1712. break;
  1713. ri = container_of(*cur, struct kretprobe_instance, llist);
  1714. } while (ri->fp != fp);
  1715. return (unsigned long)ret;
  1716. }
  1717. NOKPROBE_SYMBOL(kretprobe_find_ret_addr);
  1718. void __weak arch_kretprobe_fixup_return(struct pt_regs *regs,
  1719. kprobe_opcode_t *correct_ret_addr)
  1720. {
  1721. /*
  1722. * Do nothing by default. Please fill this to update the fake return
  1723. * address on the stack with the correct one on each arch if possible.
  1724. */
  1725. }
  1726. unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
  1727. void *frame_pointer)
  1728. {
  1729. struct kretprobe_instance *ri = NULL;
  1730. struct llist_node *first, *node = NULL;
  1731. kprobe_opcode_t *correct_ret_addr;
  1732. struct kretprobe *rp;
  1733. /* Find correct address and all nodes for this frame. */
  1734. correct_ret_addr = __kretprobe_find_ret_addr(current, &node);
  1735. if (!correct_ret_addr) {
  1736. pr_err("kretprobe: Return address not found, not execute handler. Maybe there is a bug in the kernel.\n");
  1737. BUG_ON(1);
  1738. }
  1739. /*
  1740. * Set the return address as the instruction pointer, because if the
  1741. * user handler calls stack_trace_save_regs() with this 'regs',
  1742. * the stack trace will start from the instruction pointer.
  1743. */
  1744. instruction_pointer_set(regs, (unsigned long)correct_ret_addr);
  1745. /* Run the user handler of the nodes. */
  1746. first = current->kretprobe_instances.first;
  1747. while (first) {
  1748. ri = container_of(first, struct kretprobe_instance, llist);
  1749. if (WARN_ON_ONCE(ri->fp != frame_pointer))
  1750. break;
  1751. rp = get_kretprobe(ri);
  1752. if (rp && rp->handler) {
  1753. struct kprobe *prev = kprobe_running();
  1754. __this_cpu_write(current_kprobe, &rp->kp);
  1755. ri->ret_addr = correct_ret_addr;
  1756. rp->handler(ri, regs);
  1757. __this_cpu_write(current_kprobe, prev);
  1758. }
  1759. if (first == node)
  1760. break;
  1761. first = first->next;
  1762. }
  1763. arch_kretprobe_fixup_return(regs, correct_ret_addr);
  1764. /* Unlink all nodes for this frame. */
  1765. first = current->kretprobe_instances.first;
  1766. current->kretprobe_instances.first = node->next;
  1767. node->next = NULL;
  1768. /* Recycle free instances. */
  1769. while (first) {
  1770. ri = container_of(first, struct kretprobe_instance, llist);
  1771. first = first->next;
  1772. recycle_rp_inst(ri);
  1773. }
  1774. return (unsigned long)correct_ret_addr;
  1775. }
  1776. NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
  1777. /*
  1778. * This kprobe pre_handler is registered with every kretprobe. When probe
  1779. * hits it will set up the return probe.
  1780. */
  1781. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  1782. {
  1783. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1784. struct kretprobe_holder *rph = rp->rph;
  1785. struct kretprobe_instance *ri;
  1786. ri = objpool_pop(&rph->pool);
  1787. if (!ri) {
  1788. rp->nmissed++;
  1789. return 0;
  1790. }
  1791. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  1792. objpool_push(ri, &rph->pool);
  1793. return 0;
  1794. }
  1795. arch_prepare_kretprobe(ri, regs);
  1796. __llist_add(&ri->llist, &current->kretprobe_instances);
  1797. return 0;
  1798. }
  1799. NOKPROBE_SYMBOL(pre_handler_kretprobe);
  1800. #else /* CONFIG_KRETPROBE_ON_RETHOOK */
  1801. /*
  1802. * This kprobe pre_handler is registered with every kretprobe. When probe
  1803. * hits it will set up the return probe.
  1804. */
  1805. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  1806. {
  1807. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  1808. struct kretprobe_instance *ri;
  1809. struct rethook_node *rhn;
  1810. rhn = rethook_try_get(rp->rh);
  1811. if (!rhn) {
  1812. rp->nmissed++;
  1813. return 0;
  1814. }
  1815. ri = container_of(rhn, struct kretprobe_instance, node);
  1816. if (rp->entry_handler && rp->entry_handler(ri, regs))
  1817. rethook_recycle(rhn);
  1818. else
  1819. rethook_hook(rhn, regs, kprobe_ftrace(p));
  1820. return 0;
  1821. }
  1822. NOKPROBE_SYMBOL(pre_handler_kretprobe);
  1823. static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
  1824. unsigned long ret_addr,
  1825. struct pt_regs *regs)
  1826. {
  1827. struct kretprobe *rp = (struct kretprobe *)data;
  1828. struct kretprobe_instance *ri;
  1829. struct kprobe_ctlblk *kcb;
  1830. /* The data must NOT be null. This means rethook data structure is broken. */
  1831. if (WARN_ON_ONCE(!data) || !rp->handler)
  1832. return;
  1833. __this_cpu_write(current_kprobe, &rp->kp);
  1834. kcb = get_kprobe_ctlblk();
  1835. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  1836. ri = container_of(rh, struct kretprobe_instance, node);
  1837. rp->handler(ri, regs);
  1838. __this_cpu_write(current_kprobe, NULL);
  1839. }
  1840. NOKPROBE_SYMBOL(kretprobe_rethook_handler);
  1841. #endif /* !CONFIG_KRETPROBE_ON_RETHOOK */
  1842. /**
  1843. * kprobe_on_func_entry() -- check whether given address is function entry
  1844. * @addr: Target address
  1845. * @sym: Target symbol name
  1846. * @offset: The offset from the symbol or the address
  1847. *
  1848. * This checks whether the given @addr+@offset or @sym+@offset is on the
  1849. * function entry address or not.
  1850. * This returns 0 if it is the function entry, or -EINVAL if it is not.
  1851. * And also it returns -ENOENT if it fails the symbol or address lookup.
  1852. * Caller must pass @addr or @sym (either one must be NULL), or this
  1853. * returns -EINVAL.
  1854. */
  1855. int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
  1856. {
  1857. bool on_func_entry;
  1858. kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset, &on_func_entry);
  1859. if (IS_ERR(kp_addr))
  1860. return PTR_ERR(kp_addr);
  1861. if (!on_func_entry)
  1862. return -EINVAL;
  1863. return 0;
  1864. }
  1865. int register_kretprobe(struct kretprobe *rp)
  1866. {
  1867. int ret;
  1868. int i;
  1869. void *addr;
  1870. ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset);
  1871. if (ret)
  1872. return ret;
  1873. /* If only 'rp->kp.addr' is specified, check reregistering kprobes */
  1874. if (rp->kp.addr && warn_kprobe_rereg(&rp->kp))
  1875. return -EINVAL;
  1876. if (kretprobe_blacklist_size) {
  1877. addr = kprobe_addr(&rp->kp);
  1878. if (IS_ERR(addr))
  1879. return PTR_ERR(addr);
  1880. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1881. if (kretprobe_blacklist[i].addr == addr)
  1882. return -EINVAL;
  1883. }
  1884. }
  1885. if (rp->data_size > KRETPROBE_MAX_DATA_SIZE)
  1886. return -E2BIG;
  1887. rp->kp.pre_handler = pre_handler_kretprobe;
  1888. rp->kp.post_handler = NULL;
  1889. /* Pre-allocate memory for max kretprobe instances */
  1890. if (rp->maxactive <= 0)
  1891. rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
  1892. #ifdef CONFIG_KRETPROBE_ON_RETHOOK
  1893. rp->rh = rethook_alloc((void *)rp, kretprobe_rethook_handler,
  1894. sizeof(struct kretprobe_instance) +
  1895. rp->data_size, rp->maxactive);
  1896. if (IS_ERR(rp->rh))
  1897. return PTR_ERR(rp->rh);
  1898. rp->nmissed = 0;
  1899. /* Establish function entry probe point */
  1900. ret = register_kprobe(&rp->kp);
  1901. if (ret != 0) {
  1902. rethook_free(rp->rh);
  1903. rp->rh = NULL;
  1904. }
  1905. #else /* !CONFIG_KRETPROBE_ON_RETHOOK */
  1906. rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL);
  1907. if (!rp->rph)
  1908. return -ENOMEM;
  1909. if (objpool_init(&rp->rph->pool, rp->maxactive, rp->data_size +
  1910. sizeof(struct kretprobe_instance), GFP_KERNEL,
  1911. rp->rph, kretprobe_init_inst, kretprobe_fini_pool)) {
  1912. kfree(rp->rph);
  1913. rp->rph = NULL;
  1914. return -ENOMEM;
  1915. }
  1916. rcu_assign_pointer(rp->rph->rp, rp);
  1917. rp->nmissed = 0;
  1918. /* Establish function entry probe point */
  1919. ret = register_kprobe(&rp->kp);
  1920. if (ret != 0)
  1921. free_rp_inst(rp);
  1922. #endif
  1923. return ret;
  1924. }
  1925. EXPORT_SYMBOL_GPL(register_kretprobe);
  1926. int register_kretprobes(struct kretprobe **rps, int num)
  1927. {
  1928. int ret = 0, i;
  1929. if (num <= 0)
  1930. return -EINVAL;
  1931. for (i = 0; i < num; i++) {
  1932. ret = register_kretprobe(rps[i]);
  1933. if (ret < 0) {
  1934. if (i > 0)
  1935. unregister_kretprobes(rps, i);
  1936. break;
  1937. }
  1938. }
  1939. return ret;
  1940. }
  1941. EXPORT_SYMBOL_GPL(register_kretprobes);
  1942. void unregister_kretprobe(struct kretprobe *rp)
  1943. {
  1944. unregister_kretprobes(&rp, 1);
  1945. }
  1946. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1947. void unregister_kretprobes(struct kretprobe **rps, int num)
  1948. {
  1949. int i;
  1950. if (num <= 0)
  1951. return;
  1952. mutex_lock(&kprobe_mutex);
  1953. for (i = 0; i < num; i++) {
  1954. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  1955. rps[i]->kp.addr = NULL;
  1956. #ifdef CONFIG_KRETPROBE_ON_RETHOOK
  1957. rethook_free(rps[i]->rh);
  1958. #else
  1959. rcu_assign_pointer(rps[i]->rph->rp, NULL);
  1960. #endif
  1961. }
  1962. mutex_unlock(&kprobe_mutex);
  1963. synchronize_rcu();
  1964. for (i = 0; i < num; i++) {
  1965. if (rps[i]->kp.addr) {
  1966. __unregister_kprobe_bottom(&rps[i]->kp);
  1967. #ifndef CONFIG_KRETPROBE_ON_RETHOOK
  1968. free_rp_inst(rps[i]);
  1969. #endif
  1970. }
  1971. }
  1972. }
  1973. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1974. #else /* CONFIG_KRETPROBES */
  1975. int register_kretprobe(struct kretprobe *rp)
  1976. {
  1977. return -EOPNOTSUPP;
  1978. }
  1979. EXPORT_SYMBOL_GPL(register_kretprobe);
  1980. int register_kretprobes(struct kretprobe **rps, int num)
  1981. {
  1982. return -EOPNOTSUPP;
  1983. }
  1984. EXPORT_SYMBOL_GPL(register_kretprobes);
  1985. void unregister_kretprobe(struct kretprobe *rp)
  1986. {
  1987. }
  1988. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1989. void unregister_kretprobes(struct kretprobe **rps, int num)
  1990. {
  1991. }
  1992. EXPORT_SYMBOL_GPL(unregister_kretprobes);
  1993. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  1994. {
  1995. return 0;
  1996. }
  1997. NOKPROBE_SYMBOL(pre_handler_kretprobe);
  1998. #endif /* CONFIG_KRETPROBES */
  1999. /* Set the kprobe gone and remove its instruction buffer. */
  2000. static void kill_kprobe(struct kprobe *p)
  2001. {
  2002. struct kprobe *kp;
  2003. lockdep_assert_held(&kprobe_mutex);
  2004. /*
  2005. * The module is going away. We should disarm the kprobe which
  2006. * is using ftrace, because ftrace framework is still available at
  2007. * 'MODULE_STATE_GOING' notification.
  2008. */
  2009. if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
  2010. disarm_kprobe_ftrace(p);
  2011. p->flags |= KPROBE_FLAG_GONE;
  2012. if (kprobe_aggrprobe(p)) {
  2013. /*
  2014. * If this is an aggr_kprobe, we have to list all the
  2015. * chained probes and mark them GONE.
  2016. */
  2017. list_for_each_entry(kp, &p->list, list)
  2018. kp->flags |= KPROBE_FLAG_GONE;
  2019. p->post_handler = NULL;
  2020. kill_optimized_kprobe(p);
  2021. }
  2022. /*
  2023. * Here, we can remove insn_slot safely, because no thread calls
  2024. * the original probed function (which will be freed soon) any more.
  2025. */
  2026. arch_remove_kprobe(p);
  2027. }
  2028. /* Disable one kprobe */
  2029. int disable_kprobe(struct kprobe *kp)
  2030. {
  2031. int ret = 0;
  2032. struct kprobe *p;
  2033. mutex_lock(&kprobe_mutex);
  2034. /* Disable this kprobe */
  2035. p = __disable_kprobe(kp);
  2036. if (IS_ERR(p))
  2037. ret = PTR_ERR(p);
  2038. mutex_unlock(&kprobe_mutex);
  2039. return ret;
  2040. }
  2041. EXPORT_SYMBOL_GPL(disable_kprobe);
  2042. /* Enable one kprobe */
  2043. int enable_kprobe(struct kprobe *kp)
  2044. {
  2045. int ret = 0;
  2046. struct kprobe *p;
  2047. mutex_lock(&kprobe_mutex);
  2048. /* Check whether specified probe is valid. */
  2049. p = __get_valid_kprobe(kp);
  2050. if (unlikely(p == NULL)) {
  2051. ret = -EINVAL;
  2052. goto out;
  2053. }
  2054. if (kprobe_gone(kp)) {
  2055. /* This kprobe has gone, we couldn't enable it. */
  2056. ret = -EINVAL;
  2057. goto out;
  2058. }
  2059. if (p != kp)
  2060. kp->flags &= ~KPROBE_FLAG_DISABLED;
  2061. if (!kprobes_all_disarmed && kprobe_disabled(p)) {
  2062. p->flags &= ~KPROBE_FLAG_DISABLED;
  2063. ret = arm_kprobe(p);
  2064. if (ret) {
  2065. p->flags |= KPROBE_FLAG_DISABLED;
  2066. if (p != kp)
  2067. kp->flags |= KPROBE_FLAG_DISABLED;
  2068. }
  2069. }
  2070. out:
  2071. mutex_unlock(&kprobe_mutex);
  2072. return ret;
  2073. }
  2074. EXPORT_SYMBOL_GPL(enable_kprobe);
  2075. /* Caller must NOT call this in usual path. This is only for critical case */
  2076. void dump_kprobe(struct kprobe *kp)
  2077. {
  2078. pr_err("Dump kprobe:\n.symbol_name = %s, .offset = %x, .addr = %pS\n",
  2079. kp->symbol_name, kp->offset, kp->addr);
  2080. }
  2081. NOKPROBE_SYMBOL(dump_kprobe);
  2082. int kprobe_add_ksym_blacklist(unsigned long entry)
  2083. {
  2084. struct kprobe_blacklist_entry *ent;
  2085. unsigned long offset = 0, size = 0;
  2086. if (!kernel_text_address(entry) ||
  2087. !kallsyms_lookup_size_offset(entry, &size, &offset))
  2088. return -EINVAL;
  2089. ent = kmalloc(sizeof(*ent), GFP_KERNEL);
  2090. if (!ent)
  2091. return -ENOMEM;
  2092. ent->start_addr = entry;
  2093. ent->end_addr = entry + size;
  2094. INIT_LIST_HEAD(&ent->list);
  2095. list_add_tail(&ent->list, &kprobe_blacklist);
  2096. return (int)size;
  2097. }
  2098. /* Add all symbols in given area into kprobe blacklist */
  2099. int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
  2100. {
  2101. unsigned long entry;
  2102. int ret = 0;
  2103. for (entry = start; entry < end; entry += ret) {
  2104. ret = kprobe_add_ksym_blacklist(entry);
  2105. if (ret < 0)
  2106. return ret;
  2107. if (ret == 0) /* In case of alias symbol */
  2108. ret = 1;
  2109. }
  2110. return 0;
  2111. }
  2112. int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
  2113. char *type, char *sym)
  2114. {
  2115. return -ERANGE;
  2116. }
  2117. int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2118. char *sym)
  2119. {
  2120. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  2121. if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
  2122. return 0;
  2123. #ifdef CONFIG_OPTPROBES
  2124. if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
  2125. return 0;
  2126. #endif
  2127. #endif
  2128. if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
  2129. return 0;
  2130. return -ERANGE;
  2131. }
  2132. int __init __weak arch_populate_kprobe_blacklist(void)
  2133. {
  2134. return 0;
  2135. }
  2136. /*
  2137. * Lookup and populate the kprobe_blacklist.
  2138. *
  2139. * Unlike the kretprobe blacklist, we'll need to determine
  2140. * the range of addresses that belong to the said functions,
  2141. * since a kprobe need not necessarily be at the beginning
  2142. * of a function.
  2143. */
  2144. static int __init populate_kprobe_blacklist(unsigned long *start,
  2145. unsigned long *end)
  2146. {
  2147. unsigned long entry;
  2148. unsigned long *iter;
  2149. int ret;
  2150. for (iter = start; iter < end; iter++) {
  2151. entry = (unsigned long)dereference_symbol_descriptor((void *)*iter);
  2152. ret = kprobe_add_ksym_blacklist(entry);
  2153. if (ret == -EINVAL)
  2154. continue;
  2155. if (ret < 0)
  2156. return ret;
  2157. }
  2158. /* Symbols in '__kprobes_text' are blacklisted */
  2159. ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
  2160. (unsigned long)__kprobes_text_end);
  2161. if (ret)
  2162. return ret;
  2163. /* Symbols in 'noinstr' section are blacklisted */
  2164. ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
  2165. (unsigned long)__noinstr_text_end);
  2166. return ret ? : arch_populate_kprobe_blacklist();
  2167. }
  2168. #ifdef CONFIG_MODULES
  2169. /* Remove all symbols in given area from kprobe blacklist */
  2170. static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
  2171. {
  2172. struct kprobe_blacklist_entry *ent, *n;
  2173. list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
  2174. if (ent->start_addr < start || ent->start_addr >= end)
  2175. continue;
  2176. list_del(&ent->list);
  2177. kfree(ent);
  2178. }
  2179. }
  2180. static void kprobe_remove_ksym_blacklist(unsigned long entry)
  2181. {
  2182. kprobe_remove_area_blacklist(entry, entry + 1);
  2183. }
  2184. static void add_module_kprobe_blacklist(struct module *mod)
  2185. {
  2186. unsigned long start, end;
  2187. int i;
  2188. if (mod->kprobe_blacklist) {
  2189. for (i = 0; i < mod->num_kprobe_blacklist; i++)
  2190. kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
  2191. }
  2192. start = (unsigned long)mod->kprobes_text_start;
  2193. if (start) {
  2194. end = start + mod->kprobes_text_size;
  2195. kprobe_add_area_blacklist(start, end);
  2196. }
  2197. start = (unsigned long)mod->noinstr_text_start;
  2198. if (start) {
  2199. end = start + mod->noinstr_text_size;
  2200. kprobe_add_area_blacklist(start, end);
  2201. }
  2202. }
  2203. static void remove_module_kprobe_blacklist(struct module *mod)
  2204. {
  2205. unsigned long start, end;
  2206. int i;
  2207. if (mod->kprobe_blacklist) {
  2208. for (i = 0; i < mod->num_kprobe_blacklist; i++)
  2209. kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
  2210. }
  2211. start = (unsigned long)mod->kprobes_text_start;
  2212. if (start) {
  2213. end = start + mod->kprobes_text_size;
  2214. kprobe_remove_area_blacklist(start, end);
  2215. }
  2216. start = (unsigned long)mod->noinstr_text_start;
  2217. if (start) {
  2218. end = start + mod->noinstr_text_size;
  2219. kprobe_remove_area_blacklist(start, end);
  2220. }
  2221. }
  2222. /* Module notifier call back, checking kprobes on the module */
  2223. static int kprobes_module_callback(struct notifier_block *nb,
  2224. unsigned long val, void *data)
  2225. {
  2226. struct module *mod = data;
  2227. struct hlist_head *head;
  2228. struct kprobe *p;
  2229. unsigned int i;
  2230. int checkcore = (val == MODULE_STATE_GOING);
  2231. if (val == MODULE_STATE_COMING) {
  2232. mutex_lock(&kprobe_mutex);
  2233. add_module_kprobe_blacklist(mod);
  2234. mutex_unlock(&kprobe_mutex);
  2235. }
  2236. if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
  2237. return NOTIFY_DONE;
  2238. /*
  2239. * When 'MODULE_STATE_GOING' was notified, both of module '.text' and
  2240. * '.init.text' sections would be freed. When 'MODULE_STATE_LIVE' was
  2241. * notified, only '.init.text' section would be freed. We need to
  2242. * disable kprobes which have been inserted in the sections.
  2243. */
  2244. mutex_lock(&kprobe_mutex);
  2245. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2246. head = &kprobe_table[i];
  2247. hlist_for_each_entry(p, head, hlist)
  2248. if (within_module_init((unsigned long)p->addr, mod) ||
  2249. (checkcore &&
  2250. within_module_core((unsigned long)p->addr, mod))) {
  2251. /*
  2252. * The vaddr this probe is installed will soon
  2253. * be vfreed buy not synced to disk. Hence,
  2254. * disarming the breakpoint isn't needed.
  2255. *
  2256. * Note, this will also move any optimized probes
  2257. * that are pending to be removed from their
  2258. * corresponding lists to the 'freeing_list' and
  2259. * will not be touched by the delayed
  2260. * kprobe_optimizer() work handler.
  2261. */
  2262. kill_kprobe(p);
  2263. }
  2264. }
  2265. if (val == MODULE_STATE_GOING)
  2266. remove_module_kprobe_blacklist(mod);
  2267. mutex_unlock(&kprobe_mutex);
  2268. return NOTIFY_DONE;
  2269. }
  2270. static struct notifier_block kprobe_module_nb = {
  2271. .notifier_call = kprobes_module_callback,
  2272. .priority = 0
  2273. };
  2274. static int kprobe_register_module_notifier(void)
  2275. {
  2276. return register_module_notifier(&kprobe_module_nb);
  2277. }
  2278. #else
  2279. static int kprobe_register_module_notifier(void)
  2280. {
  2281. return 0;
  2282. }
  2283. #endif /* CONFIG_MODULES */
  2284. void kprobe_free_init_mem(void)
  2285. {
  2286. void *start = (void *)(&__init_begin);
  2287. void *end = (void *)(&__init_end);
  2288. struct hlist_head *head;
  2289. struct kprobe *p;
  2290. int i;
  2291. mutex_lock(&kprobe_mutex);
  2292. /* Kill all kprobes on initmem because the target code has been freed. */
  2293. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2294. head = &kprobe_table[i];
  2295. hlist_for_each_entry(p, head, hlist) {
  2296. if (start <= (void *)p->addr && (void *)p->addr < end)
  2297. kill_kprobe(p);
  2298. }
  2299. }
  2300. mutex_unlock(&kprobe_mutex);
  2301. }
  2302. static int __init init_kprobes(void)
  2303. {
  2304. int i, err;
  2305. /* FIXME allocate the probe table, currently defined statically */
  2306. /* initialize all list heads */
  2307. for (i = 0; i < KPROBE_TABLE_SIZE; i++)
  2308. INIT_HLIST_HEAD(&kprobe_table[i]);
  2309. err = populate_kprobe_blacklist(__start_kprobe_blacklist,
  2310. __stop_kprobe_blacklist);
  2311. if (err)
  2312. pr_err("Failed to populate blacklist (error %d), kprobes not restricted, be careful using them!\n", err);
  2313. if (kretprobe_blacklist_size) {
  2314. /* lookup the function address from its name */
  2315. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  2316. kretprobe_blacklist[i].addr =
  2317. kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
  2318. if (!kretprobe_blacklist[i].addr)
  2319. pr_err("Failed to lookup symbol '%s' for kretprobe blacklist. Maybe the target function is removed or renamed.\n",
  2320. kretprobe_blacklist[i].name);
  2321. }
  2322. }
  2323. /* By default, kprobes are armed */
  2324. kprobes_all_disarmed = false;
  2325. #if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
  2326. /* Init 'kprobe_optinsn_slots' for allocation */
  2327. kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
  2328. #endif
  2329. err = arch_init_kprobes();
  2330. if (!err)
  2331. err = register_die_notifier(&kprobe_exceptions_nb);
  2332. if (!err)
  2333. err = kprobe_register_module_notifier();
  2334. kprobes_initialized = (err == 0);
  2335. kprobe_sysctls_init();
  2336. return err;
  2337. }
  2338. early_initcall(init_kprobes);
  2339. #if defined(CONFIG_OPTPROBES)
  2340. static int __init init_optprobes(void)
  2341. {
  2342. /*
  2343. * Enable kprobe optimization - this kicks the optimizer which
  2344. * depends on synchronize_rcu_tasks() and ksoftirqd, that is
  2345. * not spawned in early initcall. So delay the optimization.
  2346. */
  2347. optimize_all_kprobes();
  2348. return 0;
  2349. }
  2350. subsys_initcall(init_optprobes);
  2351. #endif
  2352. #ifdef CONFIG_DEBUG_FS
  2353. static void report_probe(struct seq_file *pi, struct kprobe *p,
  2354. const char *sym, int offset, char *modname, struct kprobe *pp)
  2355. {
  2356. char *kprobe_type;
  2357. void *addr = p->addr;
  2358. if (p->pre_handler == pre_handler_kretprobe)
  2359. kprobe_type = "r";
  2360. else
  2361. kprobe_type = "k";
  2362. if (!kallsyms_show_value(pi->file->f_cred))
  2363. addr = NULL;
  2364. if (sym)
  2365. seq_printf(pi, "%px %s %s+0x%x %s ",
  2366. addr, kprobe_type, sym, offset,
  2367. (modname ? modname : " "));
  2368. else /* try to use %pS */
  2369. seq_printf(pi, "%px %s %pS ",
  2370. addr, kprobe_type, p->addr);
  2371. if (!pp)
  2372. pp = p;
  2373. seq_printf(pi, "%s%s%s%s\n",
  2374. (kprobe_gone(p) ? "[GONE]" : ""),
  2375. ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
  2376. (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
  2377. (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
  2378. }
  2379. static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  2380. {
  2381. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  2382. }
  2383. static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  2384. {
  2385. (*pos)++;
  2386. if (*pos >= KPROBE_TABLE_SIZE)
  2387. return NULL;
  2388. return pos;
  2389. }
  2390. static void kprobe_seq_stop(struct seq_file *f, void *v)
  2391. {
  2392. /* Nothing to do */
  2393. }
  2394. static int show_kprobe_addr(struct seq_file *pi, void *v)
  2395. {
  2396. struct hlist_head *head;
  2397. struct kprobe *p, *kp;
  2398. const char *sym;
  2399. unsigned int i = *(loff_t *) v;
  2400. unsigned long offset = 0;
  2401. char *modname, namebuf[KSYM_NAME_LEN];
  2402. head = &kprobe_table[i];
  2403. preempt_disable();
  2404. hlist_for_each_entry_rcu(p, head, hlist) {
  2405. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  2406. &offset, &modname, namebuf);
  2407. if (kprobe_aggrprobe(p)) {
  2408. list_for_each_entry_rcu(kp, &p->list, list)
  2409. report_probe(pi, kp, sym, offset, modname, p);
  2410. } else
  2411. report_probe(pi, p, sym, offset, modname, NULL);
  2412. }
  2413. preempt_enable();
  2414. return 0;
  2415. }
  2416. static const struct seq_operations kprobes_sops = {
  2417. .start = kprobe_seq_start,
  2418. .next = kprobe_seq_next,
  2419. .stop = kprobe_seq_stop,
  2420. .show = show_kprobe_addr
  2421. };
  2422. DEFINE_SEQ_ATTRIBUTE(kprobes);
  2423. /* kprobes/blacklist -- shows which functions can not be probed */
  2424. static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
  2425. {
  2426. mutex_lock(&kprobe_mutex);
  2427. return seq_list_start(&kprobe_blacklist, *pos);
  2428. }
  2429. static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
  2430. {
  2431. return seq_list_next(v, &kprobe_blacklist, pos);
  2432. }
  2433. static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
  2434. {
  2435. struct kprobe_blacklist_entry *ent =
  2436. list_entry(v, struct kprobe_blacklist_entry, list);
  2437. /*
  2438. * If '/proc/kallsyms' is not showing kernel address, we won't
  2439. * show them here either.
  2440. */
  2441. if (!kallsyms_show_value(m->file->f_cred))
  2442. seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
  2443. (void *)ent->start_addr);
  2444. else
  2445. seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
  2446. (void *)ent->end_addr, (void *)ent->start_addr);
  2447. return 0;
  2448. }
  2449. static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
  2450. {
  2451. mutex_unlock(&kprobe_mutex);
  2452. }
  2453. static const struct seq_operations kprobe_blacklist_sops = {
  2454. .start = kprobe_blacklist_seq_start,
  2455. .next = kprobe_blacklist_seq_next,
  2456. .stop = kprobe_blacklist_seq_stop,
  2457. .show = kprobe_blacklist_seq_show,
  2458. };
  2459. DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
  2460. static int arm_all_kprobes(void)
  2461. {
  2462. struct hlist_head *head;
  2463. struct kprobe *p;
  2464. unsigned int i, total = 0, errors = 0;
  2465. int err, ret = 0;
  2466. mutex_lock(&kprobe_mutex);
  2467. /* If kprobes are armed, just return */
  2468. if (!kprobes_all_disarmed)
  2469. goto already_enabled;
  2470. /*
  2471. * optimize_kprobe() called by arm_kprobe() checks
  2472. * kprobes_all_disarmed, so set kprobes_all_disarmed before
  2473. * arm_kprobe.
  2474. */
  2475. kprobes_all_disarmed = false;
  2476. /* Arming kprobes doesn't optimize kprobe itself */
  2477. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2478. head = &kprobe_table[i];
  2479. /* Arm all kprobes on a best-effort basis */
  2480. hlist_for_each_entry(p, head, hlist) {
  2481. if (!kprobe_disabled(p)) {
  2482. err = arm_kprobe(p);
  2483. if (err) {
  2484. errors++;
  2485. ret = err;
  2486. }
  2487. total++;
  2488. }
  2489. }
  2490. }
  2491. if (errors)
  2492. pr_warn("Kprobes globally enabled, but failed to enable %d out of %d probes. Please check which kprobes are kept disabled via debugfs.\n",
  2493. errors, total);
  2494. else
  2495. pr_info("Kprobes globally enabled\n");
  2496. already_enabled:
  2497. mutex_unlock(&kprobe_mutex);
  2498. return ret;
  2499. }
  2500. static int disarm_all_kprobes(void)
  2501. {
  2502. struct hlist_head *head;
  2503. struct kprobe *p;
  2504. unsigned int i, total = 0, errors = 0;
  2505. int err, ret = 0;
  2506. mutex_lock(&kprobe_mutex);
  2507. /* If kprobes are already disarmed, just return */
  2508. if (kprobes_all_disarmed) {
  2509. mutex_unlock(&kprobe_mutex);
  2510. return 0;
  2511. }
  2512. kprobes_all_disarmed = true;
  2513. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  2514. head = &kprobe_table[i];
  2515. /* Disarm all kprobes on a best-effort basis */
  2516. hlist_for_each_entry(p, head, hlist) {
  2517. if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
  2518. err = disarm_kprobe(p, false);
  2519. if (err) {
  2520. errors++;
  2521. ret = err;
  2522. }
  2523. total++;
  2524. }
  2525. }
  2526. }
  2527. if (errors)
  2528. pr_warn("Kprobes globally disabled, but failed to disable %d out of %d probes. Please check which kprobes are kept enabled via debugfs.\n",
  2529. errors, total);
  2530. else
  2531. pr_info("Kprobes globally disabled\n");
  2532. mutex_unlock(&kprobe_mutex);
  2533. /* Wait for disarming all kprobes by optimizer */
  2534. wait_for_kprobe_optimizer();
  2535. return ret;
  2536. }
  2537. /*
  2538. * XXX: The debugfs bool file interface doesn't allow for callbacks
  2539. * when the bool state is switched. We can reuse that facility when
  2540. * available
  2541. */
  2542. static ssize_t read_enabled_file_bool(struct file *file,
  2543. char __user *user_buf, size_t count, loff_t *ppos)
  2544. {
  2545. char buf[3];
  2546. if (!kprobes_all_disarmed)
  2547. buf[0] = '1';
  2548. else
  2549. buf[0] = '0';
  2550. buf[1] = '\n';
  2551. buf[2] = 0x00;
  2552. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  2553. }
  2554. static ssize_t write_enabled_file_bool(struct file *file,
  2555. const char __user *user_buf, size_t count, loff_t *ppos)
  2556. {
  2557. bool enable;
  2558. int ret;
  2559. ret = kstrtobool_from_user(user_buf, count, &enable);
  2560. if (ret)
  2561. return ret;
  2562. ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
  2563. if (ret)
  2564. return ret;
  2565. return count;
  2566. }
  2567. static const struct file_operations fops_kp = {
  2568. .read = read_enabled_file_bool,
  2569. .write = write_enabled_file_bool,
  2570. .llseek = default_llseek,
  2571. };
  2572. static int __init debugfs_kprobe_init(void)
  2573. {
  2574. struct dentry *dir;
  2575. dir = debugfs_create_dir("kprobes", NULL);
  2576. debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
  2577. debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
  2578. debugfs_create_file("blacklist", 0400, dir, NULL,
  2579. &kprobe_blacklist_fops);
  2580. return 0;
  2581. }
  2582. late_initcall(debugfs_kprobe_init);
  2583. #endif /* CONFIG_DEBUG_FS */