kprobes.c 74 KB

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