bpf_trace.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
  3. * Copyright (c) 2016 Facebook
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include <linux/bpf.h>
  9. #include <linux/bpf_verifier.h>
  10. #include <linux/bpf_perf_event.h>
  11. #include <linux/btf.h>
  12. #include <linux/filter.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ctype.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/error-injection.h>
  19. #include <linux/btf_ids.h>
  20. #include <linux/bpf_lsm.h>
  21. #include <linux/fprobe.h>
  22. #include <linux/bsearch.h>
  23. #include <linux/sort.h>
  24. #include <linux/key.h>
  25. #include <linux/verification.h>
  26. #include <linux/namei.h>
  27. #include <net/bpf_sk_storage.h>
  28. #include <uapi/linux/bpf.h>
  29. #include <uapi/linux/btf.h>
  30. #include <asm/tlb.h>
  31. #include "trace_probe.h"
  32. #include "trace.h"
  33. #define CREATE_TRACE_POINTS
  34. #include "bpf_trace.h"
  35. #define bpf_event_rcu_dereference(p) \
  36. rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
  37. #define MAX_UPROBE_MULTI_CNT (1U << 20)
  38. #define MAX_KPROBE_MULTI_CNT (1U << 20)
  39. #ifdef CONFIG_MODULES
  40. struct bpf_trace_module {
  41. struct module *module;
  42. struct list_head list;
  43. };
  44. static LIST_HEAD(bpf_trace_modules);
  45. static DEFINE_MUTEX(bpf_module_mutex);
  46. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  47. {
  48. struct bpf_raw_event_map *btp, *ret = NULL;
  49. struct bpf_trace_module *btm;
  50. unsigned int i;
  51. mutex_lock(&bpf_module_mutex);
  52. list_for_each_entry(btm, &bpf_trace_modules, list) {
  53. for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
  54. btp = &btm->module->bpf_raw_events[i];
  55. if (!strcmp(btp->tp->name, name)) {
  56. if (try_module_get(btm->module))
  57. ret = btp;
  58. goto out;
  59. }
  60. }
  61. }
  62. out:
  63. mutex_unlock(&bpf_module_mutex);
  64. return ret;
  65. }
  66. #else
  67. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  68. {
  69. return NULL;
  70. }
  71. #endif /* CONFIG_MODULES */
  72. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  73. u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  74. static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
  75. u64 flags, const struct btf **btf,
  76. s32 *btf_id);
  77. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
  78. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
  79. static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);
  80. static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
  81. /**
  82. * trace_call_bpf - invoke BPF program
  83. * @call: tracepoint event
  84. * @ctx: opaque context pointer
  85. *
  86. * kprobe handlers execute BPF programs via this helper.
  87. * Can be used from static tracepoints in the future.
  88. *
  89. * Return: BPF programs always return an integer which is interpreted by
  90. * kprobe handler as:
  91. * 0 - return from kprobe (event is filtered out)
  92. * 1 - store kprobe event into ring buffer
  93. * Other values are reserved and currently alias to 1
  94. */
  95. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  96. {
  97. unsigned int ret;
  98. cant_sleep();
  99. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  100. /*
  101. * since some bpf program is already running on this cpu,
  102. * don't call into another bpf program (same or different)
  103. * and don't send kprobe event into ring-buffer,
  104. * so return zero here
  105. */
  106. rcu_read_lock();
  107. bpf_prog_inc_misses_counters(rcu_dereference(call->prog_array));
  108. rcu_read_unlock();
  109. ret = 0;
  110. goto out;
  111. }
  112. /*
  113. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  114. * to all call sites, we did a bpf_prog_array_valid() there to check
  115. * whether call->prog_array is empty or not, which is
  116. * a heuristic to speed up execution.
  117. *
  118. * If bpf_prog_array_valid() fetched prog_array was
  119. * non-NULL, we go into trace_call_bpf() and do the actual
  120. * proper rcu_dereference() under RCU lock.
  121. * If it turns out that prog_array is NULL then, we bail out.
  122. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  123. * was NULL, you'll skip the prog_array with the risk of missing
  124. * out of events when it was updated in between this and the
  125. * rcu_dereference() which is accepted risk.
  126. */
  127. rcu_read_lock();
  128. ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
  129. ctx, bpf_prog_run);
  130. rcu_read_unlock();
  131. out:
  132. __this_cpu_dec(bpf_prog_active);
  133. return ret;
  134. }
  135. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  136. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  137. {
  138. regs_set_return_value(regs, rc);
  139. override_function_with_return(regs);
  140. return 0;
  141. }
  142. static const struct bpf_func_proto bpf_override_return_proto = {
  143. .func = bpf_override_return,
  144. .gpl_only = true,
  145. .ret_type = RET_INTEGER,
  146. .arg1_type = ARG_PTR_TO_CTX,
  147. .arg2_type = ARG_ANYTHING,
  148. };
  149. #endif
  150. static __always_inline int
  151. bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
  152. {
  153. int ret;
  154. ret = copy_from_user_nofault(dst, unsafe_ptr, size);
  155. if (unlikely(ret < 0))
  156. memset(dst, 0, size);
  157. return ret;
  158. }
  159. BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
  160. const void __user *, unsafe_ptr)
  161. {
  162. return bpf_probe_read_user_common(dst, size, unsafe_ptr);
  163. }
  164. const struct bpf_func_proto bpf_probe_read_user_proto = {
  165. .func = bpf_probe_read_user,
  166. .gpl_only = true,
  167. .ret_type = RET_INTEGER,
  168. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  169. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  170. .arg3_type = ARG_ANYTHING,
  171. };
  172. static __always_inline int
  173. bpf_probe_read_user_str_common(void *dst, u32 size,
  174. const void __user *unsafe_ptr)
  175. {
  176. int ret;
  177. /*
  178. * NB: We rely on strncpy_from_user() not copying junk past the NUL
  179. * terminator into `dst`.
  180. *
  181. * strncpy_from_user() does long-sized strides in the fast path. If the
  182. * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
  183. * then there could be junk after the NUL in `dst`. If user takes `dst`
  184. * and keys a hash map with it, then semantically identical strings can
  185. * occupy multiple entries in the map.
  186. */
  187. ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
  188. if (unlikely(ret < 0))
  189. memset(dst, 0, size);
  190. return ret;
  191. }
  192. BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
  193. const void __user *, unsafe_ptr)
  194. {
  195. return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
  196. }
  197. const struct bpf_func_proto bpf_probe_read_user_str_proto = {
  198. .func = bpf_probe_read_user_str,
  199. .gpl_only = true,
  200. .ret_type = RET_INTEGER,
  201. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  202. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  203. .arg3_type = ARG_ANYTHING,
  204. };
  205. BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
  206. const void *, unsafe_ptr)
  207. {
  208. return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
  209. }
  210. const struct bpf_func_proto bpf_probe_read_kernel_proto = {
  211. .func = bpf_probe_read_kernel,
  212. .gpl_only = true,
  213. .ret_type = RET_INTEGER,
  214. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  215. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  216. .arg3_type = ARG_ANYTHING,
  217. };
  218. static __always_inline int
  219. bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
  220. {
  221. int ret;
  222. /*
  223. * The strncpy_from_kernel_nofault() call will likely not fill the
  224. * entire buffer, but that's okay in this circumstance as we're probing
  225. * arbitrary memory anyway similar to bpf_probe_read_*() and might
  226. * as well probe the stack. Thus, memory is explicitly cleared
  227. * only in error case, so that improper users ignoring return
  228. * code altogether don't copy garbage; otherwise length of string
  229. * is returned that can be used for bpf_perf_event_output() et al.
  230. */
  231. ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
  232. if (unlikely(ret < 0))
  233. memset(dst, 0, size);
  234. return ret;
  235. }
  236. BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
  237. const void *, unsafe_ptr)
  238. {
  239. return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
  240. }
  241. const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
  242. .func = bpf_probe_read_kernel_str,
  243. .gpl_only = true,
  244. .ret_type = RET_INTEGER,
  245. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  246. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  247. .arg3_type = ARG_ANYTHING,
  248. };
  249. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  250. BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
  251. const void *, unsafe_ptr)
  252. {
  253. if ((unsigned long)unsafe_ptr < TASK_SIZE) {
  254. return bpf_probe_read_user_common(dst, size,
  255. (__force void __user *)unsafe_ptr);
  256. }
  257. return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
  258. }
  259. static const struct bpf_func_proto bpf_probe_read_compat_proto = {
  260. .func = bpf_probe_read_compat,
  261. .gpl_only = true,
  262. .ret_type = RET_INTEGER,
  263. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  264. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  265. .arg3_type = ARG_ANYTHING,
  266. };
  267. BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
  268. const void *, unsafe_ptr)
  269. {
  270. if ((unsigned long)unsafe_ptr < TASK_SIZE) {
  271. return bpf_probe_read_user_str_common(dst, size,
  272. (__force void __user *)unsafe_ptr);
  273. }
  274. return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
  275. }
  276. static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
  277. .func = bpf_probe_read_compat_str,
  278. .gpl_only = true,
  279. .ret_type = RET_INTEGER,
  280. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  281. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  282. .arg3_type = ARG_ANYTHING,
  283. };
  284. #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
  285. BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
  286. u32, size)
  287. {
  288. /*
  289. * Ensure we're in user context which is safe for the helper to
  290. * run. This helper has no business in a kthread.
  291. *
  292. * access_ok() should prevent writing to non-user memory, but in
  293. * some situations (nommu, temporary switch, etc) access_ok() does
  294. * not provide enough validation, hence the check on KERNEL_DS.
  295. *
  296. * nmi_uaccess_okay() ensures the probe is not run in an interim
  297. * state, when the task or mm are switched. This is specifically
  298. * required to prevent the use of temporary mm.
  299. */
  300. if (unlikely(in_interrupt() ||
  301. current->flags & (PF_KTHREAD | PF_EXITING)))
  302. return -EPERM;
  303. if (unlikely(!nmi_uaccess_okay()))
  304. return -EPERM;
  305. return copy_to_user_nofault(unsafe_ptr, src, size);
  306. }
  307. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  308. .func = bpf_probe_write_user,
  309. .gpl_only = true,
  310. .ret_type = RET_INTEGER,
  311. .arg1_type = ARG_ANYTHING,
  312. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  313. .arg3_type = ARG_CONST_SIZE,
  314. };
  315. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  316. {
  317. if (!capable(CAP_SYS_ADMIN))
  318. return NULL;
  319. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  320. current->comm, task_pid_nr(current));
  321. return &bpf_probe_write_user_proto;
  322. }
  323. #define MAX_TRACE_PRINTK_VARARGS 3
  324. #define BPF_TRACE_PRINTK_SIZE 1024
  325. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  326. u64, arg2, u64, arg3)
  327. {
  328. u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
  329. struct bpf_bprintf_data data = {
  330. .get_bin_args = true,
  331. .get_buf = true,
  332. };
  333. int ret;
  334. ret = bpf_bprintf_prepare(fmt, fmt_size, args,
  335. MAX_TRACE_PRINTK_VARARGS, &data);
  336. if (ret < 0)
  337. return ret;
  338. ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
  339. trace_bpf_trace_printk(data.buf);
  340. bpf_bprintf_cleanup(&data);
  341. return ret;
  342. }
  343. static const struct bpf_func_proto bpf_trace_printk_proto = {
  344. .func = bpf_trace_printk,
  345. .gpl_only = true,
  346. .ret_type = RET_INTEGER,
  347. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  348. .arg2_type = ARG_CONST_SIZE,
  349. };
  350. static void __set_printk_clr_event(struct work_struct *work)
  351. {
  352. /*
  353. * This program might be calling bpf_trace_printk,
  354. * so enable the associated bpf_trace/bpf_trace_printk event.
  355. * Repeat this each time as it is possible a user has
  356. * disabled bpf_trace_printk events. By loading a program
  357. * calling bpf_trace_printk() however the user has expressed
  358. * the intent to see such events.
  359. */
  360. if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
  361. pr_warn_ratelimited("could not enable bpf_trace_printk events");
  362. }
  363. static DECLARE_WORK(set_printk_work, __set_printk_clr_event);
  364. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  365. {
  366. schedule_work(&set_printk_work);
  367. return &bpf_trace_printk_proto;
  368. }
  369. BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
  370. u32, data_len)
  371. {
  372. struct bpf_bprintf_data data = {
  373. .get_bin_args = true,
  374. .get_buf = true,
  375. };
  376. int ret, num_args;
  377. if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
  378. (data_len && !args))
  379. return -EINVAL;
  380. num_args = data_len / 8;
  381. ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
  382. if (ret < 0)
  383. return ret;
  384. ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
  385. trace_bpf_trace_printk(data.buf);
  386. bpf_bprintf_cleanup(&data);
  387. return ret;
  388. }
  389. static const struct bpf_func_proto bpf_trace_vprintk_proto = {
  390. .func = bpf_trace_vprintk,
  391. .gpl_only = true,
  392. .ret_type = RET_INTEGER,
  393. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  394. .arg2_type = ARG_CONST_SIZE,
  395. .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
  396. .arg4_type = ARG_CONST_SIZE_OR_ZERO,
  397. };
  398. const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
  399. {
  400. schedule_work(&set_printk_work);
  401. return &bpf_trace_vprintk_proto;
  402. }
  403. BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
  404. const void *, args, u32, data_len)
  405. {
  406. struct bpf_bprintf_data data = {
  407. .get_bin_args = true,
  408. };
  409. int err, num_args;
  410. if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
  411. (data_len && !args))
  412. return -EINVAL;
  413. num_args = data_len / 8;
  414. err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
  415. if (err < 0)
  416. return err;
  417. seq_bprintf(m, fmt, data.bin_args);
  418. bpf_bprintf_cleanup(&data);
  419. return seq_has_overflowed(m) ? -EOVERFLOW : 0;
  420. }
  421. BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
  422. static const struct bpf_func_proto bpf_seq_printf_proto = {
  423. .func = bpf_seq_printf,
  424. .gpl_only = true,
  425. .ret_type = RET_INTEGER,
  426. .arg1_type = ARG_PTR_TO_BTF_ID,
  427. .arg1_btf_id = &btf_seq_file_ids[0],
  428. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  429. .arg3_type = ARG_CONST_SIZE,
  430. .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
  431. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  432. };
  433. BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
  434. {
  435. return seq_write(m, data, len) ? -EOVERFLOW : 0;
  436. }
  437. static const struct bpf_func_proto bpf_seq_write_proto = {
  438. .func = bpf_seq_write,
  439. .gpl_only = true,
  440. .ret_type = RET_INTEGER,
  441. .arg1_type = ARG_PTR_TO_BTF_ID,
  442. .arg1_btf_id = &btf_seq_file_ids[0],
  443. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  444. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  445. };
  446. BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
  447. u32, btf_ptr_size, u64, flags)
  448. {
  449. const struct btf *btf;
  450. s32 btf_id;
  451. int ret;
  452. ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
  453. if (ret)
  454. return ret;
  455. return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
  456. }
  457. static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
  458. .func = bpf_seq_printf_btf,
  459. .gpl_only = true,
  460. .ret_type = RET_INTEGER,
  461. .arg1_type = ARG_PTR_TO_BTF_ID,
  462. .arg1_btf_id = &btf_seq_file_ids[0],
  463. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  464. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  465. .arg4_type = ARG_ANYTHING,
  466. };
  467. static __always_inline int
  468. get_map_perf_counter(struct bpf_map *map, u64 flags,
  469. u64 *value, u64 *enabled, u64 *running)
  470. {
  471. struct bpf_array *array = container_of(map, struct bpf_array, map);
  472. unsigned int cpu = smp_processor_id();
  473. u64 index = flags & BPF_F_INDEX_MASK;
  474. struct bpf_event_entry *ee;
  475. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  476. return -EINVAL;
  477. if (index == BPF_F_CURRENT_CPU)
  478. index = cpu;
  479. if (unlikely(index >= array->map.max_entries))
  480. return -E2BIG;
  481. ee = READ_ONCE(array->ptrs[index]);
  482. if (!ee)
  483. return -ENOENT;
  484. return perf_event_read_local(ee->event, value, enabled, running);
  485. }
  486. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  487. {
  488. u64 value = 0;
  489. int err;
  490. err = get_map_perf_counter(map, flags, &value, NULL, NULL);
  491. /*
  492. * this api is ugly since we miss [-22..-2] range of valid
  493. * counter values, but that's uapi
  494. */
  495. if (err)
  496. return err;
  497. return value;
  498. }
  499. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  500. .func = bpf_perf_event_read,
  501. .gpl_only = true,
  502. .ret_type = RET_INTEGER,
  503. .arg1_type = ARG_CONST_MAP_PTR,
  504. .arg2_type = ARG_ANYTHING,
  505. };
  506. BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
  507. struct bpf_perf_event_value *, buf, u32, size)
  508. {
  509. int err = -EINVAL;
  510. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  511. goto clear;
  512. err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
  513. &buf->running);
  514. if (unlikely(err))
  515. goto clear;
  516. return 0;
  517. clear:
  518. memset(buf, 0, size);
  519. return err;
  520. }
  521. static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
  522. .func = bpf_perf_event_read_value,
  523. .gpl_only = true,
  524. .ret_type = RET_INTEGER,
  525. .arg1_type = ARG_CONST_MAP_PTR,
  526. .arg2_type = ARG_ANYTHING,
  527. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  528. .arg4_type = ARG_CONST_SIZE,
  529. };
  530. static __always_inline u64
  531. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  532. u64 flags, struct perf_raw_record *raw,
  533. struct perf_sample_data *sd)
  534. {
  535. struct bpf_array *array = container_of(map, struct bpf_array, map);
  536. unsigned int cpu = smp_processor_id();
  537. u64 index = flags & BPF_F_INDEX_MASK;
  538. struct bpf_event_entry *ee;
  539. struct perf_event *event;
  540. if (index == BPF_F_CURRENT_CPU)
  541. index = cpu;
  542. if (unlikely(index >= array->map.max_entries))
  543. return -E2BIG;
  544. ee = READ_ONCE(array->ptrs[index]);
  545. if (!ee)
  546. return -ENOENT;
  547. event = ee->event;
  548. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  549. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  550. return -EINVAL;
  551. if (unlikely(event->oncpu != cpu))
  552. return -EOPNOTSUPP;
  553. perf_sample_save_raw_data(sd, event, raw);
  554. return perf_event_output(event, sd, regs);
  555. }
  556. /*
  557. * Support executing tracepoints in normal, irq, and nmi context that each call
  558. * bpf_perf_event_output
  559. */
  560. struct bpf_trace_sample_data {
  561. struct perf_sample_data sds[3];
  562. };
  563. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
  564. static DEFINE_PER_CPU(int, bpf_trace_nest_level);
  565. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  566. u64, flags, void *, data, u64, size)
  567. {
  568. struct bpf_trace_sample_data *sds;
  569. struct perf_raw_record raw = {
  570. .frag = {
  571. .size = size,
  572. .data = data,
  573. },
  574. };
  575. struct perf_sample_data *sd;
  576. int nest_level, err;
  577. preempt_disable();
  578. sds = this_cpu_ptr(&bpf_trace_sds);
  579. nest_level = this_cpu_inc_return(bpf_trace_nest_level);
  580. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
  581. err = -EBUSY;
  582. goto out;
  583. }
  584. sd = &sds->sds[nest_level - 1];
  585. if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
  586. err = -EINVAL;
  587. goto out;
  588. }
  589. perf_sample_data_init(sd, 0, 0);
  590. err = __bpf_perf_event_output(regs, map, flags, &raw, sd);
  591. out:
  592. this_cpu_dec(bpf_trace_nest_level);
  593. preempt_enable();
  594. return err;
  595. }
  596. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  597. .func = bpf_perf_event_output,
  598. .gpl_only = true,
  599. .ret_type = RET_INTEGER,
  600. .arg1_type = ARG_PTR_TO_CTX,
  601. .arg2_type = ARG_CONST_MAP_PTR,
  602. .arg3_type = ARG_ANYTHING,
  603. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  604. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  605. };
  606. static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
  607. struct bpf_nested_pt_regs {
  608. struct pt_regs regs[3];
  609. };
  610. static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
  611. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
  612. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  613. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  614. {
  615. struct perf_raw_frag frag = {
  616. .copy = ctx_copy,
  617. .size = ctx_size,
  618. .data = ctx,
  619. };
  620. struct perf_raw_record raw = {
  621. .frag = {
  622. {
  623. .next = ctx_size ? &frag : NULL,
  624. },
  625. .size = meta_size,
  626. .data = meta,
  627. },
  628. };
  629. struct perf_sample_data *sd;
  630. struct pt_regs *regs;
  631. int nest_level;
  632. u64 ret;
  633. preempt_disable();
  634. nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
  635. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
  636. ret = -EBUSY;
  637. goto out;
  638. }
  639. sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
  640. regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
  641. perf_fetch_caller_regs(regs);
  642. perf_sample_data_init(sd, 0, 0);
  643. ret = __bpf_perf_event_output(regs, map, flags, &raw, sd);
  644. out:
  645. this_cpu_dec(bpf_event_output_nest_level);
  646. preempt_enable();
  647. return ret;
  648. }
  649. BPF_CALL_0(bpf_get_current_task)
  650. {
  651. return (long) current;
  652. }
  653. const struct bpf_func_proto bpf_get_current_task_proto = {
  654. .func = bpf_get_current_task,
  655. .gpl_only = true,
  656. .ret_type = RET_INTEGER,
  657. };
  658. BPF_CALL_0(bpf_get_current_task_btf)
  659. {
  660. return (unsigned long) current;
  661. }
  662. const struct bpf_func_proto bpf_get_current_task_btf_proto = {
  663. .func = bpf_get_current_task_btf,
  664. .gpl_only = true,
  665. .ret_type = RET_PTR_TO_BTF_ID_TRUSTED,
  666. .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  667. };
  668. BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
  669. {
  670. return (unsigned long) task_pt_regs(task);
  671. }
  672. BTF_ID_LIST(bpf_task_pt_regs_ids)
  673. BTF_ID(struct, pt_regs)
  674. const struct bpf_func_proto bpf_task_pt_regs_proto = {
  675. .func = bpf_task_pt_regs,
  676. .gpl_only = true,
  677. .arg1_type = ARG_PTR_TO_BTF_ID,
  678. .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  679. .ret_type = RET_PTR_TO_BTF_ID,
  680. .ret_btf_id = &bpf_task_pt_regs_ids[0],
  681. };
  682. struct send_signal_irq_work {
  683. struct irq_work irq_work;
  684. struct task_struct *task;
  685. u32 sig;
  686. enum pid_type type;
  687. };
  688. static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
  689. static void do_bpf_send_signal(struct irq_work *entry)
  690. {
  691. struct send_signal_irq_work *work;
  692. work = container_of(entry, struct send_signal_irq_work, irq_work);
  693. group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
  694. put_task_struct(work->task);
  695. }
  696. static int bpf_send_signal_common(u32 sig, enum pid_type type)
  697. {
  698. struct send_signal_irq_work *work = NULL;
  699. /* Similar to bpf_probe_write_user, task needs to be
  700. * in a sound condition and kernel memory access be
  701. * permitted in order to send signal to the current
  702. * task.
  703. */
  704. if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
  705. return -EPERM;
  706. if (unlikely(!nmi_uaccess_okay()))
  707. return -EPERM;
  708. /* Task should not be pid=1 to avoid kernel panic. */
  709. if (unlikely(is_global_init(current)))
  710. return -EPERM;
  711. if (preempt_count() != 0 || irqs_disabled()) {
  712. /* Do an early check on signal validity. Otherwise,
  713. * the error is lost in deferred irq_work.
  714. */
  715. if (unlikely(!valid_signal(sig)))
  716. return -EINVAL;
  717. work = this_cpu_ptr(&send_signal_work);
  718. if (irq_work_is_busy(&work->irq_work))
  719. return -EBUSY;
  720. /* Add the current task, which is the target of sending signal,
  721. * to the irq_work. The current task may change when queued
  722. * irq works get executed.
  723. */
  724. work->task = get_task_struct(current);
  725. work->sig = sig;
  726. work->type = type;
  727. irq_work_queue(&work->irq_work);
  728. return 0;
  729. }
  730. return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
  731. }
  732. BPF_CALL_1(bpf_send_signal, u32, sig)
  733. {
  734. return bpf_send_signal_common(sig, PIDTYPE_TGID);
  735. }
  736. static const struct bpf_func_proto bpf_send_signal_proto = {
  737. .func = bpf_send_signal,
  738. .gpl_only = false,
  739. .ret_type = RET_INTEGER,
  740. .arg1_type = ARG_ANYTHING,
  741. };
  742. BPF_CALL_1(bpf_send_signal_thread, u32, sig)
  743. {
  744. return bpf_send_signal_common(sig, PIDTYPE_PID);
  745. }
  746. static const struct bpf_func_proto bpf_send_signal_thread_proto = {
  747. .func = bpf_send_signal_thread,
  748. .gpl_only = false,
  749. .ret_type = RET_INTEGER,
  750. .arg1_type = ARG_ANYTHING,
  751. };
  752. BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
  753. {
  754. struct path copy;
  755. long len;
  756. char *p;
  757. if (!sz)
  758. return 0;
  759. /*
  760. * The path pointer is verified as trusted and safe to use,
  761. * but let's double check it's valid anyway to workaround
  762. * potentially broken verifier.
  763. */
  764. len = copy_from_kernel_nofault(&copy, path, sizeof(*path));
  765. if (len < 0)
  766. return len;
  767. p = d_path(&copy, buf, sz);
  768. if (IS_ERR(p)) {
  769. len = PTR_ERR(p);
  770. } else {
  771. len = buf + sz - p;
  772. memmove(buf, p, len);
  773. }
  774. return len;
  775. }
  776. BTF_SET_START(btf_allowlist_d_path)
  777. #ifdef CONFIG_SECURITY
  778. BTF_ID(func, security_file_permission)
  779. BTF_ID(func, security_inode_getattr)
  780. BTF_ID(func, security_file_open)
  781. #endif
  782. #ifdef CONFIG_SECURITY_PATH
  783. BTF_ID(func, security_path_truncate)
  784. #endif
  785. BTF_ID(func, vfs_truncate)
  786. BTF_ID(func, vfs_fallocate)
  787. BTF_ID(func, dentry_open)
  788. BTF_ID(func, vfs_getattr)
  789. BTF_ID(func, filp_close)
  790. BTF_SET_END(btf_allowlist_d_path)
  791. static bool bpf_d_path_allowed(const struct bpf_prog *prog)
  792. {
  793. if (prog->type == BPF_PROG_TYPE_TRACING &&
  794. prog->expected_attach_type == BPF_TRACE_ITER)
  795. return true;
  796. if (prog->type == BPF_PROG_TYPE_LSM)
  797. return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
  798. return btf_id_set_contains(&btf_allowlist_d_path,
  799. prog->aux->attach_btf_id);
  800. }
  801. BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
  802. static const struct bpf_func_proto bpf_d_path_proto = {
  803. .func = bpf_d_path,
  804. .gpl_only = false,
  805. .ret_type = RET_INTEGER,
  806. .arg1_type = ARG_PTR_TO_BTF_ID,
  807. .arg1_btf_id = &bpf_d_path_btf_ids[0],
  808. .arg2_type = ARG_PTR_TO_MEM,
  809. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  810. .allowed = bpf_d_path_allowed,
  811. };
  812. #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
  813. BTF_F_PTR_RAW | BTF_F_ZERO)
  814. static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
  815. u64 flags, const struct btf **btf,
  816. s32 *btf_id)
  817. {
  818. const struct btf_type *t;
  819. if (unlikely(flags & ~(BTF_F_ALL)))
  820. return -EINVAL;
  821. if (btf_ptr_size != sizeof(struct btf_ptr))
  822. return -EINVAL;
  823. *btf = bpf_get_btf_vmlinux();
  824. if (IS_ERR_OR_NULL(*btf))
  825. return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
  826. if (ptr->type_id > 0)
  827. *btf_id = ptr->type_id;
  828. else
  829. return -EINVAL;
  830. if (*btf_id > 0)
  831. t = btf_type_by_id(*btf, *btf_id);
  832. if (*btf_id <= 0 || !t)
  833. return -ENOENT;
  834. return 0;
  835. }
  836. BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
  837. u32, btf_ptr_size, u64, flags)
  838. {
  839. const struct btf *btf;
  840. s32 btf_id;
  841. int ret;
  842. ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
  843. if (ret)
  844. return ret;
  845. return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
  846. flags);
  847. }
  848. const struct bpf_func_proto bpf_snprintf_btf_proto = {
  849. .func = bpf_snprintf_btf,
  850. .gpl_only = false,
  851. .ret_type = RET_INTEGER,
  852. .arg1_type = ARG_PTR_TO_MEM,
  853. .arg2_type = ARG_CONST_SIZE,
  854. .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  855. .arg4_type = ARG_CONST_SIZE,
  856. .arg5_type = ARG_ANYTHING,
  857. };
  858. BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
  859. {
  860. /* This helper call is inlined by verifier. */
  861. return ((u64 *)ctx)[-2];
  862. }
  863. static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
  864. .func = bpf_get_func_ip_tracing,
  865. .gpl_only = true,
  866. .ret_type = RET_INTEGER,
  867. .arg1_type = ARG_PTR_TO_CTX,
  868. };
  869. #ifdef CONFIG_X86_KERNEL_IBT
  870. static unsigned long get_entry_ip(unsigned long fentry_ip)
  871. {
  872. u32 instr;
  873. /* We want to be extra safe in case entry ip is on the page edge,
  874. * but otherwise we need to avoid get_kernel_nofault()'s overhead.
  875. */
  876. if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) {
  877. if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE)))
  878. return fentry_ip;
  879. } else {
  880. instr = *(u32 *)(fentry_ip - ENDBR_INSN_SIZE);
  881. }
  882. if (is_endbr(instr))
  883. fentry_ip -= ENDBR_INSN_SIZE;
  884. return fentry_ip;
  885. }
  886. #else
  887. #define get_entry_ip(fentry_ip) fentry_ip
  888. #endif
  889. BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
  890. {
  891. struct bpf_trace_run_ctx *run_ctx __maybe_unused;
  892. struct kprobe *kp;
  893. #ifdef CONFIG_UPROBES
  894. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  895. if (run_ctx->is_uprobe)
  896. return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
  897. #endif
  898. kp = kprobe_running();
  899. if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
  900. return 0;
  901. return get_entry_ip((uintptr_t)kp->addr);
  902. }
  903. static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
  904. .func = bpf_get_func_ip_kprobe,
  905. .gpl_only = true,
  906. .ret_type = RET_INTEGER,
  907. .arg1_type = ARG_PTR_TO_CTX,
  908. };
  909. BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
  910. {
  911. return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
  912. }
  913. static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
  914. .func = bpf_get_func_ip_kprobe_multi,
  915. .gpl_only = false,
  916. .ret_type = RET_INTEGER,
  917. .arg1_type = ARG_PTR_TO_CTX,
  918. };
  919. BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
  920. {
  921. return bpf_kprobe_multi_cookie(current->bpf_ctx);
  922. }
  923. static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
  924. .func = bpf_get_attach_cookie_kprobe_multi,
  925. .gpl_only = false,
  926. .ret_type = RET_INTEGER,
  927. .arg1_type = ARG_PTR_TO_CTX,
  928. };
  929. BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
  930. {
  931. return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
  932. }
  933. static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
  934. .func = bpf_get_func_ip_uprobe_multi,
  935. .gpl_only = false,
  936. .ret_type = RET_INTEGER,
  937. .arg1_type = ARG_PTR_TO_CTX,
  938. };
  939. BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
  940. {
  941. return bpf_uprobe_multi_cookie(current->bpf_ctx);
  942. }
  943. static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
  944. .func = bpf_get_attach_cookie_uprobe_multi,
  945. .gpl_only = false,
  946. .ret_type = RET_INTEGER,
  947. .arg1_type = ARG_PTR_TO_CTX,
  948. };
  949. BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
  950. {
  951. struct bpf_trace_run_ctx *run_ctx;
  952. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  953. return run_ctx->bpf_cookie;
  954. }
  955. static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
  956. .func = bpf_get_attach_cookie_trace,
  957. .gpl_only = false,
  958. .ret_type = RET_INTEGER,
  959. .arg1_type = ARG_PTR_TO_CTX,
  960. };
  961. BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
  962. {
  963. return ctx->event->bpf_cookie;
  964. }
  965. static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
  966. .func = bpf_get_attach_cookie_pe,
  967. .gpl_only = false,
  968. .ret_type = RET_INTEGER,
  969. .arg1_type = ARG_PTR_TO_CTX,
  970. };
  971. BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
  972. {
  973. struct bpf_trace_run_ctx *run_ctx;
  974. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  975. return run_ctx->bpf_cookie;
  976. }
  977. static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
  978. .func = bpf_get_attach_cookie_tracing,
  979. .gpl_only = false,
  980. .ret_type = RET_INTEGER,
  981. .arg1_type = ARG_PTR_TO_CTX,
  982. };
  983. BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
  984. {
  985. static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  986. u32 entry_cnt = size / br_entry_size;
  987. entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
  988. if (unlikely(flags))
  989. return -EINVAL;
  990. if (!entry_cnt)
  991. return -ENOENT;
  992. return entry_cnt * br_entry_size;
  993. }
  994. static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
  995. .func = bpf_get_branch_snapshot,
  996. .gpl_only = true,
  997. .ret_type = RET_INTEGER,
  998. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  999. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  1000. };
  1001. BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
  1002. {
  1003. /* This helper call is inlined by verifier. */
  1004. u64 nr_args = ((u64 *)ctx)[-1];
  1005. if ((u64) n >= nr_args)
  1006. return -EINVAL;
  1007. *value = ((u64 *)ctx)[n];
  1008. return 0;
  1009. }
  1010. static const struct bpf_func_proto bpf_get_func_arg_proto = {
  1011. .func = get_func_arg,
  1012. .ret_type = RET_INTEGER,
  1013. .arg1_type = ARG_PTR_TO_CTX,
  1014. .arg2_type = ARG_ANYTHING,
  1015. .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
  1016. .arg3_size = sizeof(u64),
  1017. };
  1018. BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
  1019. {
  1020. /* This helper call is inlined by verifier. */
  1021. u64 nr_args = ((u64 *)ctx)[-1];
  1022. *value = ((u64 *)ctx)[nr_args];
  1023. return 0;
  1024. }
  1025. static const struct bpf_func_proto bpf_get_func_ret_proto = {
  1026. .func = get_func_ret,
  1027. .ret_type = RET_INTEGER,
  1028. .arg1_type = ARG_PTR_TO_CTX,
  1029. .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
  1030. .arg2_size = sizeof(u64),
  1031. };
  1032. BPF_CALL_1(get_func_arg_cnt, void *, ctx)
  1033. {
  1034. /* This helper call is inlined by verifier. */
  1035. return ((u64 *)ctx)[-1];
  1036. }
  1037. static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
  1038. .func = get_func_arg_cnt,
  1039. .ret_type = RET_INTEGER,
  1040. .arg1_type = ARG_PTR_TO_CTX,
  1041. };
  1042. #ifdef CONFIG_KEYS
  1043. __bpf_kfunc_start_defs();
  1044. /**
  1045. * bpf_lookup_user_key - lookup a key by its serial
  1046. * @serial: key handle serial number
  1047. * @flags: lookup-specific flags
  1048. *
  1049. * Search a key with a given *serial* and the provided *flags*.
  1050. * If found, increment the reference count of the key by one, and
  1051. * return it in the bpf_key structure.
  1052. *
  1053. * The bpf_key structure must be passed to bpf_key_put() when done
  1054. * with it, so that the key reference count is decremented and the
  1055. * bpf_key structure is freed.
  1056. *
  1057. * Permission checks are deferred to the time the key is used by
  1058. * one of the available key-specific kfuncs.
  1059. *
  1060. * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
  1061. * special keyring (e.g. session keyring), if it doesn't yet exist.
  1062. * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
  1063. * for the key construction, and to retrieve uninstantiated keys (keys
  1064. * without data attached to them).
  1065. *
  1066. * Return: a bpf_key pointer with a valid key pointer if the key is found, a
  1067. * NULL pointer otherwise.
  1068. */
  1069. __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
  1070. {
  1071. key_ref_t key_ref;
  1072. struct bpf_key *bkey;
  1073. if (flags & ~KEY_LOOKUP_ALL)
  1074. return NULL;
  1075. /*
  1076. * Permission check is deferred until the key is used, as the
  1077. * intent of the caller is unknown here.
  1078. */
  1079. key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
  1080. if (IS_ERR(key_ref))
  1081. return NULL;
  1082. bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
  1083. if (!bkey) {
  1084. key_put(key_ref_to_ptr(key_ref));
  1085. return NULL;
  1086. }
  1087. bkey->key = key_ref_to_ptr(key_ref);
  1088. bkey->has_ref = true;
  1089. return bkey;
  1090. }
  1091. /**
  1092. * bpf_lookup_system_key - lookup a key by a system-defined ID
  1093. * @id: key ID
  1094. *
  1095. * Obtain a bpf_key structure with a key pointer set to the passed key ID.
  1096. * The key pointer is marked as invalid, to prevent bpf_key_put() from
  1097. * attempting to decrement the key reference count on that pointer. The key
  1098. * pointer set in such way is currently understood only by
  1099. * verify_pkcs7_signature().
  1100. *
  1101. * Set *id* to one of the values defined in include/linux/verification.h:
  1102. * 0 for the primary keyring (immutable keyring of system keys);
  1103. * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
  1104. * (where keys can be added only if they are vouched for by existing keys
  1105. * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
  1106. * keyring (primarily used by the integrity subsystem to verify a kexec'ed
  1107. * kerned image and, possibly, the initramfs signature).
  1108. *
  1109. * Return: a bpf_key pointer with an invalid key pointer set from the
  1110. * pre-determined ID on success, a NULL pointer otherwise
  1111. */
  1112. __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
  1113. {
  1114. struct bpf_key *bkey;
  1115. if (system_keyring_id_check(id) < 0)
  1116. return NULL;
  1117. bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
  1118. if (!bkey)
  1119. return NULL;
  1120. bkey->key = (struct key *)(unsigned long)id;
  1121. bkey->has_ref = false;
  1122. return bkey;
  1123. }
  1124. /**
  1125. * bpf_key_put - decrement key reference count if key is valid and free bpf_key
  1126. * @bkey: bpf_key structure
  1127. *
  1128. * Decrement the reference count of the key inside *bkey*, if the pointer
  1129. * is valid, and free *bkey*.
  1130. */
  1131. __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
  1132. {
  1133. if (bkey->has_ref)
  1134. key_put(bkey->key);
  1135. kfree(bkey);
  1136. }
  1137. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  1138. /**
  1139. * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
  1140. * @data_p: data to verify
  1141. * @sig_p: signature of the data
  1142. * @trusted_keyring: keyring with keys trusted for signature verification
  1143. *
  1144. * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
  1145. * with keys in a keyring referenced by *trusted_keyring*.
  1146. *
  1147. * Return: 0 on success, a negative value on error.
  1148. */
  1149. __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_p,
  1150. struct bpf_dynptr *sig_p,
  1151. struct bpf_key *trusted_keyring)
  1152. {
  1153. struct bpf_dynptr_kern *data_ptr = (struct bpf_dynptr_kern *)data_p;
  1154. struct bpf_dynptr_kern *sig_ptr = (struct bpf_dynptr_kern *)sig_p;
  1155. const void *data, *sig;
  1156. u32 data_len, sig_len;
  1157. int ret;
  1158. if (trusted_keyring->has_ref) {
  1159. /*
  1160. * Do the permission check deferred in bpf_lookup_user_key().
  1161. * See bpf_lookup_user_key() for more details.
  1162. *
  1163. * A call to key_task_permission() here would be redundant, as
  1164. * it is already done by keyring_search() called by
  1165. * find_asymmetric_key().
  1166. */
  1167. ret = key_validate(trusted_keyring->key);
  1168. if (ret < 0)
  1169. return ret;
  1170. }
  1171. data_len = __bpf_dynptr_size(data_ptr);
  1172. data = __bpf_dynptr_data(data_ptr, data_len);
  1173. sig_len = __bpf_dynptr_size(sig_ptr);
  1174. sig = __bpf_dynptr_data(sig_ptr, sig_len);
  1175. return verify_pkcs7_signature(data, data_len, sig, sig_len,
  1176. trusted_keyring->key,
  1177. VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
  1178. NULL);
  1179. }
  1180. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
  1181. __bpf_kfunc_end_defs();
  1182. BTF_KFUNCS_START(key_sig_kfunc_set)
  1183. BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
  1184. BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
  1185. BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
  1186. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  1187. BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
  1188. #endif
  1189. BTF_KFUNCS_END(key_sig_kfunc_set)
  1190. static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
  1191. .owner = THIS_MODULE,
  1192. .set = &key_sig_kfunc_set,
  1193. };
  1194. static int __init bpf_key_sig_kfuncs_init(void)
  1195. {
  1196. return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
  1197. &bpf_key_sig_kfunc_set);
  1198. }
  1199. late_initcall(bpf_key_sig_kfuncs_init);
  1200. #endif /* CONFIG_KEYS */
  1201. static const struct bpf_func_proto *
  1202. bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1203. {
  1204. switch (func_id) {
  1205. case BPF_FUNC_map_lookup_elem:
  1206. return &bpf_map_lookup_elem_proto;
  1207. case BPF_FUNC_map_update_elem:
  1208. return &bpf_map_update_elem_proto;
  1209. case BPF_FUNC_map_delete_elem:
  1210. return &bpf_map_delete_elem_proto;
  1211. case BPF_FUNC_map_push_elem:
  1212. return &bpf_map_push_elem_proto;
  1213. case BPF_FUNC_map_pop_elem:
  1214. return &bpf_map_pop_elem_proto;
  1215. case BPF_FUNC_map_peek_elem:
  1216. return &bpf_map_peek_elem_proto;
  1217. case BPF_FUNC_map_lookup_percpu_elem:
  1218. return &bpf_map_lookup_percpu_elem_proto;
  1219. case BPF_FUNC_ktime_get_ns:
  1220. return &bpf_ktime_get_ns_proto;
  1221. case BPF_FUNC_ktime_get_boot_ns:
  1222. return &bpf_ktime_get_boot_ns_proto;
  1223. case BPF_FUNC_tail_call:
  1224. return &bpf_tail_call_proto;
  1225. case BPF_FUNC_get_current_task:
  1226. return &bpf_get_current_task_proto;
  1227. case BPF_FUNC_get_current_task_btf:
  1228. return &bpf_get_current_task_btf_proto;
  1229. case BPF_FUNC_task_pt_regs:
  1230. return &bpf_task_pt_regs_proto;
  1231. case BPF_FUNC_get_current_uid_gid:
  1232. return &bpf_get_current_uid_gid_proto;
  1233. case BPF_FUNC_get_current_comm:
  1234. return &bpf_get_current_comm_proto;
  1235. case BPF_FUNC_trace_printk:
  1236. return bpf_get_trace_printk_proto();
  1237. case BPF_FUNC_get_smp_processor_id:
  1238. return &bpf_get_smp_processor_id_proto;
  1239. case BPF_FUNC_get_numa_node_id:
  1240. return &bpf_get_numa_node_id_proto;
  1241. case BPF_FUNC_perf_event_read:
  1242. return &bpf_perf_event_read_proto;
  1243. case BPF_FUNC_get_prandom_u32:
  1244. return &bpf_get_prandom_u32_proto;
  1245. case BPF_FUNC_probe_write_user:
  1246. return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
  1247. NULL : bpf_get_probe_write_proto();
  1248. case BPF_FUNC_probe_read_user:
  1249. return &bpf_probe_read_user_proto;
  1250. case BPF_FUNC_probe_read_kernel:
  1251. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1252. NULL : &bpf_probe_read_kernel_proto;
  1253. case BPF_FUNC_probe_read_user_str:
  1254. return &bpf_probe_read_user_str_proto;
  1255. case BPF_FUNC_probe_read_kernel_str:
  1256. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1257. NULL : &bpf_probe_read_kernel_str_proto;
  1258. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  1259. case BPF_FUNC_probe_read:
  1260. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1261. NULL : &bpf_probe_read_compat_proto;
  1262. case BPF_FUNC_probe_read_str:
  1263. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1264. NULL : &bpf_probe_read_compat_str_proto;
  1265. #endif
  1266. #ifdef CONFIG_CGROUPS
  1267. case BPF_FUNC_cgrp_storage_get:
  1268. return &bpf_cgrp_storage_get_proto;
  1269. case BPF_FUNC_cgrp_storage_delete:
  1270. return &bpf_cgrp_storage_delete_proto;
  1271. case BPF_FUNC_current_task_under_cgroup:
  1272. return &bpf_current_task_under_cgroup_proto;
  1273. #endif
  1274. case BPF_FUNC_send_signal:
  1275. return &bpf_send_signal_proto;
  1276. case BPF_FUNC_send_signal_thread:
  1277. return &bpf_send_signal_thread_proto;
  1278. case BPF_FUNC_perf_event_read_value:
  1279. return &bpf_perf_event_read_value_proto;
  1280. case BPF_FUNC_ringbuf_output:
  1281. return &bpf_ringbuf_output_proto;
  1282. case BPF_FUNC_ringbuf_reserve:
  1283. return &bpf_ringbuf_reserve_proto;
  1284. case BPF_FUNC_ringbuf_submit:
  1285. return &bpf_ringbuf_submit_proto;
  1286. case BPF_FUNC_ringbuf_discard:
  1287. return &bpf_ringbuf_discard_proto;
  1288. case BPF_FUNC_ringbuf_query:
  1289. return &bpf_ringbuf_query_proto;
  1290. case BPF_FUNC_jiffies64:
  1291. return &bpf_jiffies64_proto;
  1292. case BPF_FUNC_get_task_stack:
  1293. return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
  1294. : &bpf_get_task_stack_proto;
  1295. case BPF_FUNC_copy_from_user:
  1296. return &bpf_copy_from_user_proto;
  1297. case BPF_FUNC_copy_from_user_task:
  1298. return &bpf_copy_from_user_task_proto;
  1299. case BPF_FUNC_snprintf_btf:
  1300. return &bpf_snprintf_btf_proto;
  1301. case BPF_FUNC_per_cpu_ptr:
  1302. return &bpf_per_cpu_ptr_proto;
  1303. case BPF_FUNC_this_cpu_ptr:
  1304. return &bpf_this_cpu_ptr_proto;
  1305. case BPF_FUNC_task_storage_get:
  1306. if (bpf_prog_check_recur(prog))
  1307. return &bpf_task_storage_get_recur_proto;
  1308. return &bpf_task_storage_get_proto;
  1309. case BPF_FUNC_task_storage_delete:
  1310. if (bpf_prog_check_recur(prog))
  1311. return &bpf_task_storage_delete_recur_proto;
  1312. return &bpf_task_storage_delete_proto;
  1313. case BPF_FUNC_for_each_map_elem:
  1314. return &bpf_for_each_map_elem_proto;
  1315. case BPF_FUNC_snprintf:
  1316. return &bpf_snprintf_proto;
  1317. case BPF_FUNC_get_func_ip:
  1318. return &bpf_get_func_ip_proto_tracing;
  1319. case BPF_FUNC_get_branch_snapshot:
  1320. return &bpf_get_branch_snapshot_proto;
  1321. case BPF_FUNC_find_vma:
  1322. return &bpf_find_vma_proto;
  1323. case BPF_FUNC_trace_vprintk:
  1324. return bpf_get_trace_vprintk_proto();
  1325. default:
  1326. return bpf_base_func_proto(func_id, prog);
  1327. }
  1328. }
  1329. static bool is_kprobe_multi(const struct bpf_prog *prog)
  1330. {
  1331. return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ||
  1332. prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
  1333. }
  1334. static inline bool is_kprobe_session(const struct bpf_prog *prog)
  1335. {
  1336. return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
  1337. }
  1338. static const struct bpf_func_proto *
  1339. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1340. {
  1341. switch (func_id) {
  1342. case BPF_FUNC_perf_event_output:
  1343. return &bpf_perf_event_output_proto;
  1344. case BPF_FUNC_get_stackid:
  1345. return &bpf_get_stackid_proto;
  1346. case BPF_FUNC_get_stack:
  1347. return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto;
  1348. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  1349. case BPF_FUNC_override_return:
  1350. return &bpf_override_return_proto;
  1351. #endif
  1352. case BPF_FUNC_get_func_ip:
  1353. if (is_kprobe_multi(prog))
  1354. return &bpf_get_func_ip_proto_kprobe_multi;
  1355. if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
  1356. return &bpf_get_func_ip_proto_uprobe_multi;
  1357. return &bpf_get_func_ip_proto_kprobe;
  1358. case BPF_FUNC_get_attach_cookie:
  1359. if (is_kprobe_multi(prog))
  1360. return &bpf_get_attach_cookie_proto_kmulti;
  1361. if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
  1362. return &bpf_get_attach_cookie_proto_umulti;
  1363. return &bpf_get_attach_cookie_proto_trace;
  1364. default:
  1365. return bpf_tracing_func_proto(func_id, prog);
  1366. }
  1367. }
  1368. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  1369. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1370. const struct bpf_prog *prog,
  1371. struct bpf_insn_access_aux *info)
  1372. {
  1373. if (off < 0 || off >= sizeof(struct pt_regs))
  1374. return false;
  1375. if (type != BPF_READ)
  1376. return false;
  1377. if (off % size != 0)
  1378. return false;
  1379. /*
  1380. * Assertion for 32 bit to make sure last 8 byte access
  1381. * (BPF_DW) to the last 4 byte member is disallowed.
  1382. */
  1383. if (off + size > sizeof(struct pt_regs))
  1384. return false;
  1385. return true;
  1386. }
  1387. const struct bpf_verifier_ops kprobe_verifier_ops = {
  1388. .get_func_proto = kprobe_prog_func_proto,
  1389. .is_valid_access = kprobe_prog_is_valid_access,
  1390. };
  1391. const struct bpf_prog_ops kprobe_prog_ops = {
  1392. };
  1393. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  1394. u64, flags, void *, data, u64, size)
  1395. {
  1396. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1397. /*
  1398. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  1399. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  1400. * from there and call the same bpf_perf_event_output() helper inline.
  1401. */
  1402. return ____bpf_perf_event_output(regs, map, flags, data, size);
  1403. }
  1404. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  1405. .func = bpf_perf_event_output_tp,
  1406. .gpl_only = true,
  1407. .ret_type = RET_INTEGER,
  1408. .arg1_type = ARG_PTR_TO_CTX,
  1409. .arg2_type = ARG_CONST_MAP_PTR,
  1410. .arg3_type = ARG_ANYTHING,
  1411. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1412. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  1413. };
  1414. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  1415. u64, flags)
  1416. {
  1417. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1418. /*
  1419. * Same comment as in bpf_perf_event_output_tp(), only that this time
  1420. * the other helper's function body cannot be inlined due to being
  1421. * external, thus we need to call raw helper function.
  1422. */
  1423. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  1424. flags, 0, 0);
  1425. }
  1426. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  1427. .func = bpf_get_stackid_tp,
  1428. .gpl_only = true,
  1429. .ret_type = RET_INTEGER,
  1430. .arg1_type = ARG_PTR_TO_CTX,
  1431. .arg2_type = ARG_CONST_MAP_PTR,
  1432. .arg3_type = ARG_ANYTHING,
  1433. };
  1434. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  1435. u64, flags)
  1436. {
  1437. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1438. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  1439. (unsigned long) size, flags, 0);
  1440. }
  1441. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  1442. .func = bpf_get_stack_tp,
  1443. .gpl_only = true,
  1444. .ret_type = RET_INTEGER,
  1445. .arg1_type = ARG_PTR_TO_CTX,
  1446. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1447. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1448. .arg4_type = ARG_ANYTHING,
  1449. };
  1450. static const struct bpf_func_proto *
  1451. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1452. {
  1453. switch (func_id) {
  1454. case BPF_FUNC_perf_event_output:
  1455. return &bpf_perf_event_output_proto_tp;
  1456. case BPF_FUNC_get_stackid:
  1457. return &bpf_get_stackid_proto_tp;
  1458. case BPF_FUNC_get_stack:
  1459. return &bpf_get_stack_proto_tp;
  1460. case BPF_FUNC_get_attach_cookie:
  1461. return &bpf_get_attach_cookie_proto_trace;
  1462. default:
  1463. return bpf_tracing_func_proto(func_id, prog);
  1464. }
  1465. }
  1466. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1467. const struct bpf_prog *prog,
  1468. struct bpf_insn_access_aux *info)
  1469. {
  1470. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  1471. return false;
  1472. if (type != BPF_READ)
  1473. return false;
  1474. if (off % size != 0)
  1475. return false;
  1476. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  1477. return true;
  1478. }
  1479. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  1480. .get_func_proto = tp_prog_func_proto,
  1481. .is_valid_access = tp_prog_is_valid_access,
  1482. };
  1483. const struct bpf_prog_ops tracepoint_prog_ops = {
  1484. };
  1485. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  1486. struct bpf_perf_event_value *, buf, u32, size)
  1487. {
  1488. int err = -EINVAL;
  1489. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  1490. goto clear;
  1491. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  1492. &buf->running);
  1493. if (unlikely(err))
  1494. goto clear;
  1495. return 0;
  1496. clear:
  1497. memset(buf, 0, size);
  1498. return err;
  1499. }
  1500. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  1501. .func = bpf_perf_prog_read_value,
  1502. .gpl_only = true,
  1503. .ret_type = RET_INTEGER,
  1504. .arg1_type = ARG_PTR_TO_CTX,
  1505. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1506. .arg3_type = ARG_CONST_SIZE,
  1507. };
  1508. BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
  1509. void *, buf, u32, size, u64, flags)
  1510. {
  1511. static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  1512. struct perf_branch_stack *br_stack = ctx->data->br_stack;
  1513. u32 to_copy;
  1514. if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
  1515. return -EINVAL;
  1516. if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
  1517. return -ENOENT;
  1518. if (unlikely(!br_stack))
  1519. return -ENOENT;
  1520. if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
  1521. return br_stack->nr * br_entry_size;
  1522. if (!buf || (size % br_entry_size != 0))
  1523. return -EINVAL;
  1524. to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
  1525. memcpy(buf, br_stack->entries, to_copy);
  1526. return to_copy;
  1527. }
  1528. static const struct bpf_func_proto bpf_read_branch_records_proto = {
  1529. .func = bpf_read_branch_records,
  1530. .gpl_only = true,
  1531. .ret_type = RET_INTEGER,
  1532. .arg1_type = ARG_PTR_TO_CTX,
  1533. .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
  1534. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1535. .arg4_type = ARG_ANYTHING,
  1536. };
  1537. static const struct bpf_func_proto *
  1538. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1539. {
  1540. switch (func_id) {
  1541. case BPF_FUNC_perf_event_output:
  1542. return &bpf_perf_event_output_proto_tp;
  1543. case BPF_FUNC_get_stackid:
  1544. return &bpf_get_stackid_proto_pe;
  1545. case BPF_FUNC_get_stack:
  1546. return &bpf_get_stack_proto_pe;
  1547. case BPF_FUNC_perf_prog_read_value:
  1548. return &bpf_perf_prog_read_value_proto;
  1549. case BPF_FUNC_read_branch_records:
  1550. return &bpf_read_branch_records_proto;
  1551. case BPF_FUNC_get_attach_cookie:
  1552. return &bpf_get_attach_cookie_proto_pe;
  1553. default:
  1554. return bpf_tracing_func_proto(func_id, prog);
  1555. }
  1556. }
  1557. /*
  1558. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  1559. * to avoid potential recursive reuse issue when/if tracepoints are added
  1560. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
  1561. *
  1562. * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
  1563. * in normal, irq, and nmi context.
  1564. */
  1565. struct bpf_raw_tp_regs {
  1566. struct pt_regs regs[3];
  1567. };
  1568. static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
  1569. static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
  1570. static struct pt_regs *get_bpf_raw_tp_regs(void)
  1571. {
  1572. struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
  1573. int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
  1574. if (nest_level > ARRAY_SIZE(tp_regs->regs)) {
  1575. this_cpu_dec(bpf_raw_tp_nest_level);
  1576. return ERR_PTR(-EBUSY);
  1577. }
  1578. return &tp_regs->regs[nest_level - 1];
  1579. }
  1580. static void put_bpf_raw_tp_regs(void)
  1581. {
  1582. this_cpu_dec(bpf_raw_tp_nest_level);
  1583. }
  1584. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1585. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  1586. {
  1587. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1588. int ret;
  1589. if (IS_ERR(regs))
  1590. return PTR_ERR(regs);
  1591. perf_fetch_caller_regs(regs);
  1592. ret = ____bpf_perf_event_output(regs, map, flags, data, size);
  1593. put_bpf_raw_tp_regs();
  1594. return ret;
  1595. }
  1596. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  1597. .func = bpf_perf_event_output_raw_tp,
  1598. .gpl_only = true,
  1599. .ret_type = RET_INTEGER,
  1600. .arg1_type = ARG_PTR_TO_CTX,
  1601. .arg2_type = ARG_CONST_MAP_PTR,
  1602. .arg3_type = ARG_ANYTHING,
  1603. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1604. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  1605. };
  1606. extern const struct bpf_func_proto bpf_skb_output_proto;
  1607. extern const struct bpf_func_proto bpf_xdp_output_proto;
  1608. extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
  1609. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1610. struct bpf_map *, map, u64, flags)
  1611. {
  1612. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1613. int ret;
  1614. if (IS_ERR(regs))
  1615. return PTR_ERR(regs);
  1616. perf_fetch_caller_regs(regs);
  1617. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  1618. ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  1619. flags, 0, 0);
  1620. put_bpf_raw_tp_regs();
  1621. return ret;
  1622. }
  1623. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  1624. .func = bpf_get_stackid_raw_tp,
  1625. .gpl_only = true,
  1626. .ret_type = RET_INTEGER,
  1627. .arg1_type = ARG_PTR_TO_CTX,
  1628. .arg2_type = ARG_CONST_MAP_PTR,
  1629. .arg3_type = ARG_ANYTHING,
  1630. };
  1631. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1632. void *, buf, u32, size, u64, flags)
  1633. {
  1634. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1635. int ret;
  1636. if (IS_ERR(regs))
  1637. return PTR_ERR(regs);
  1638. perf_fetch_caller_regs(regs);
  1639. ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  1640. (unsigned long) size, flags, 0);
  1641. put_bpf_raw_tp_regs();
  1642. return ret;
  1643. }
  1644. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  1645. .func = bpf_get_stack_raw_tp,
  1646. .gpl_only = true,
  1647. .ret_type = RET_INTEGER,
  1648. .arg1_type = ARG_PTR_TO_CTX,
  1649. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1650. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1651. .arg4_type = ARG_ANYTHING,
  1652. };
  1653. static const struct bpf_func_proto *
  1654. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1655. {
  1656. switch (func_id) {
  1657. case BPF_FUNC_perf_event_output:
  1658. return &bpf_perf_event_output_proto_raw_tp;
  1659. case BPF_FUNC_get_stackid:
  1660. return &bpf_get_stackid_proto_raw_tp;
  1661. case BPF_FUNC_get_stack:
  1662. return &bpf_get_stack_proto_raw_tp;
  1663. case BPF_FUNC_get_attach_cookie:
  1664. return &bpf_get_attach_cookie_proto_tracing;
  1665. default:
  1666. return bpf_tracing_func_proto(func_id, prog);
  1667. }
  1668. }
  1669. const struct bpf_func_proto *
  1670. tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1671. {
  1672. const struct bpf_func_proto *fn;
  1673. switch (func_id) {
  1674. #ifdef CONFIG_NET
  1675. case BPF_FUNC_skb_output:
  1676. return &bpf_skb_output_proto;
  1677. case BPF_FUNC_xdp_output:
  1678. return &bpf_xdp_output_proto;
  1679. case BPF_FUNC_skc_to_tcp6_sock:
  1680. return &bpf_skc_to_tcp6_sock_proto;
  1681. case BPF_FUNC_skc_to_tcp_sock:
  1682. return &bpf_skc_to_tcp_sock_proto;
  1683. case BPF_FUNC_skc_to_tcp_timewait_sock:
  1684. return &bpf_skc_to_tcp_timewait_sock_proto;
  1685. case BPF_FUNC_skc_to_tcp_request_sock:
  1686. return &bpf_skc_to_tcp_request_sock_proto;
  1687. case BPF_FUNC_skc_to_udp6_sock:
  1688. return &bpf_skc_to_udp6_sock_proto;
  1689. case BPF_FUNC_skc_to_unix_sock:
  1690. return &bpf_skc_to_unix_sock_proto;
  1691. case BPF_FUNC_skc_to_mptcp_sock:
  1692. return &bpf_skc_to_mptcp_sock_proto;
  1693. case BPF_FUNC_sk_storage_get:
  1694. return &bpf_sk_storage_get_tracing_proto;
  1695. case BPF_FUNC_sk_storage_delete:
  1696. return &bpf_sk_storage_delete_tracing_proto;
  1697. case BPF_FUNC_sock_from_file:
  1698. return &bpf_sock_from_file_proto;
  1699. case BPF_FUNC_get_socket_cookie:
  1700. return &bpf_get_socket_ptr_cookie_proto;
  1701. case BPF_FUNC_xdp_get_buff_len:
  1702. return &bpf_xdp_get_buff_len_trace_proto;
  1703. #endif
  1704. case BPF_FUNC_seq_printf:
  1705. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1706. &bpf_seq_printf_proto :
  1707. NULL;
  1708. case BPF_FUNC_seq_write:
  1709. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1710. &bpf_seq_write_proto :
  1711. NULL;
  1712. case BPF_FUNC_seq_printf_btf:
  1713. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1714. &bpf_seq_printf_btf_proto :
  1715. NULL;
  1716. case BPF_FUNC_d_path:
  1717. return &bpf_d_path_proto;
  1718. case BPF_FUNC_get_func_arg:
  1719. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
  1720. case BPF_FUNC_get_func_ret:
  1721. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
  1722. case BPF_FUNC_get_func_arg_cnt:
  1723. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
  1724. case BPF_FUNC_get_attach_cookie:
  1725. if (prog->type == BPF_PROG_TYPE_TRACING &&
  1726. prog->expected_attach_type == BPF_TRACE_RAW_TP)
  1727. return &bpf_get_attach_cookie_proto_tracing;
  1728. return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
  1729. default:
  1730. fn = raw_tp_prog_func_proto(func_id, prog);
  1731. if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
  1732. fn = bpf_iter_get_func_proto(func_id, prog);
  1733. return fn;
  1734. }
  1735. }
  1736. static bool raw_tp_prog_is_valid_access(int off, int size,
  1737. enum bpf_access_type type,
  1738. const struct bpf_prog *prog,
  1739. struct bpf_insn_access_aux *info)
  1740. {
  1741. return bpf_tracing_ctx_access(off, size, type);
  1742. }
  1743. static bool tracing_prog_is_valid_access(int off, int size,
  1744. enum bpf_access_type type,
  1745. const struct bpf_prog *prog,
  1746. struct bpf_insn_access_aux *info)
  1747. {
  1748. return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
  1749. }
  1750. int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
  1751. const union bpf_attr *kattr,
  1752. union bpf_attr __user *uattr)
  1753. {
  1754. return -ENOTSUPP;
  1755. }
  1756. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  1757. .get_func_proto = raw_tp_prog_func_proto,
  1758. .is_valid_access = raw_tp_prog_is_valid_access,
  1759. };
  1760. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  1761. #ifdef CONFIG_NET
  1762. .test_run = bpf_prog_test_run_raw_tp,
  1763. #endif
  1764. };
  1765. const struct bpf_verifier_ops tracing_verifier_ops = {
  1766. .get_func_proto = tracing_prog_func_proto,
  1767. .is_valid_access = tracing_prog_is_valid_access,
  1768. };
  1769. const struct bpf_prog_ops tracing_prog_ops = {
  1770. .test_run = bpf_prog_test_run_tracing,
  1771. };
  1772. static bool raw_tp_writable_prog_is_valid_access(int off, int size,
  1773. enum bpf_access_type type,
  1774. const struct bpf_prog *prog,
  1775. struct bpf_insn_access_aux *info)
  1776. {
  1777. if (off == 0) {
  1778. if (size != sizeof(u64) || type != BPF_READ)
  1779. return false;
  1780. info->reg_type = PTR_TO_TP_BUFFER;
  1781. }
  1782. return raw_tp_prog_is_valid_access(off, size, type, prog, info);
  1783. }
  1784. const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
  1785. .get_func_proto = raw_tp_prog_func_proto,
  1786. .is_valid_access = raw_tp_writable_prog_is_valid_access,
  1787. };
  1788. const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
  1789. };
  1790. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1791. const struct bpf_prog *prog,
  1792. struct bpf_insn_access_aux *info)
  1793. {
  1794. const int size_u64 = sizeof(u64);
  1795. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  1796. return false;
  1797. if (type != BPF_READ)
  1798. return false;
  1799. if (off % size != 0) {
  1800. if (sizeof(unsigned long) != 4)
  1801. return false;
  1802. if (size != 8)
  1803. return false;
  1804. if (off % size != 4)
  1805. return false;
  1806. }
  1807. switch (off) {
  1808. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  1809. bpf_ctx_record_field_size(info, size_u64);
  1810. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1811. return false;
  1812. break;
  1813. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  1814. bpf_ctx_record_field_size(info, size_u64);
  1815. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1816. return false;
  1817. break;
  1818. default:
  1819. if (size != sizeof(long))
  1820. return false;
  1821. }
  1822. return true;
  1823. }
  1824. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  1825. const struct bpf_insn *si,
  1826. struct bpf_insn *insn_buf,
  1827. struct bpf_prog *prog, u32 *target_size)
  1828. {
  1829. struct bpf_insn *insn = insn_buf;
  1830. switch (si->off) {
  1831. case offsetof(struct bpf_perf_event_data, sample_period):
  1832. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1833. data), si->dst_reg, si->src_reg,
  1834. offsetof(struct bpf_perf_event_data_kern, data));
  1835. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1836. bpf_target_off(struct perf_sample_data, period, 8,
  1837. target_size));
  1838. break;
  1839. case offsetof(struct bpf_perf_event_data, addr):
  1840. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1841. data), si->dst_reg, si->src_reg,
  1842. offsetof(struct bpf_perf_event_data_kern, data));
  1843. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1844. bpf_target_off(struct perf_sample_data, addr, 8,
  1845. target_size));
  1846. break;
  1847. default:
  1848. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1849. regs), si->dst_reg, si->src_reg,
  1850. offsetof(struct bpf_perf_event_data_kern, regs));
  1851. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  1852. si->off);
  1853. break;
  1854. }
  1855. return insn - insn_buf;
  1856. }
  1857. const struct bpf_verifier_ops perf_event_verifier_ops = {
  1858. .get_func_proto = pe_prog_func_proto,
  1859. .is_valid_access = pe_prog_is_valid_access,
  1860. .convert_ctx_access = pe_prog_convert_ctx_access,
  1861. };
  1862. const struct bpf_prog_ops perf_event_prog_ops = {
  1863. };
  1864. static DEFINE_MUTEX(bpf_event_mutex);
  1865. #define BPF_TRACE_MAX_PROGS 64
  1866. int perf_event_attach_bpf_prog(struct perf_event *event,
  1867. struct bpf_prog *prog,
  1868. u64 bpf_cookie)
  1869. {
  1870. struct bpf_prog_array *old_array;
  1871. struct bpf_prog_array *new_array;
  1872. int ret = -EEXIST;
  1873. /*
  1874. * Kprobe override only works if they are on the function entry,
  1875. * and only if they are on the opt-in list.
  1876. */
  1877. if (prog->kprobe_override &&
  1878. (!trace_kprobe_on_func_entry(event->tp_event) ||
  1879. !trace_kprobe_error_injectable(event->tp_event)))
  1880. return -EINVAL;
  1881. mutex_lock(&bpf_event_mutex);
  1882. if (event->prog)
  1883. goto unlock;
  1884. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1885. if (old_array &&
  1886. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  1887. ret = -E2BIG;
  1888. goto unlock;
  1889. }
  1890. ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
  1891. if (ret < 0)
  1892. goto unlock;
  1893. /* set the new array to event->tp_event and set event->prog */
  1894. event->prog = prog;
  1895. event->bpf_cookie = bpf_cookie;
  1896. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1897. bpf_prog_array_free_sleepable(old_array);
  1898. unlock:
  1899. mutex_unlock(&bpf_event_mutex);
  1900. return ret;
  1901. }
  1902. void perf_event_detach_bpf_prog(struct perf_event *event)
  1903. {
  1904. struct bpf_prog_array *old_array;
  1905. struct bpf_prog_array *new_array;
  1906. int ret;
  1907. mutex_lock(&bpf_event_mutex);
  1908. if (!event->prog)
  1909. goto unlock;
  1910. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1911. if (!old_array)
  1912. goto put;
  1913. ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
  1914. if (ret < 0) {
  1915. bpf_prog_array_delete_safe(old_array, event->prog);
  1916. } else {
  1917. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1918. bpf_prog_array_free_sleepable(old_array);
  1919. }
  1920. put:
  1921. /*
  1922. * It could be that the bpf_prog is not sleepable (and will be freed
  1923. * via normal RCU), but is called from a point that supports sleepable
  1924. * programs and uses tasks-trace-RCU.
  1925. */
  1926. synchronize_rcu_tasks_trace();
  1927. bpf_prog_put(event->prog);
  1928. event->prog = NULL;
  1929. unlock:
  1930. mutex_unlock(&bpf_event_mutex);
  1931. }
  1932. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  1933. {
  1934. struct perf_event_query_bpf __user *uquery = info;
  1935. struct perf_event_query_bpf query = {};
  1936. struct bpf_prog_array *progs;
  1937. u32 *ids, prog_cnt, ids_len;
  1938. int ret;
  1939. if (!perfmon_capable())
  1940. return -EPERM;
  1941. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  1942. return -EINVAL;
  1943. if (copy_from_user(&query, uquery, sizeof(query)))
  1944. return -EFAULT;
  1945. ids_len = query.ids_len;
  1946. if (ids_len > BPF_TRACE_MAX_PROGS)
  1947. return -E2BIG;
  1948. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  1949. if (!ids)
  1950. return -ENOMEM;
  1951. /*
  1952. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  1953. * is required when user only wants to check for uquery->prog_cnt.
  1954. * There is no need to check for it since the case is handled
  1955. * gracefully in bpf_prog_array_copy_info.
  1956. */
  1957. mutex_lock(&bpf_event_mutex);
  1958. progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1959. ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
  1960. mutex_unlock(&bpf_event_mutex);
  1961. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  1962. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  1963. ret = -EFAULT;
  1964. kfree(ids);
  1965. return ret;
  1966. }
  1967. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  1968. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  1969. struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
  1970. {
  1971. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  1972. for (; btp < __stop__bpf_raw_tp; btp++) {
  1973. if (!strcmp(btp->tp->name, name))
  1974. return btp;
  1975. }
  1976. return bpf_get_raw_tracepoint_module(name);
  1977. }
  1978. void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
  1979. {
  1980. struct module *mod;
  1981. preempt_disable();
  1982. mod = __module_address((unsigned long)btp);
  1983. module_put(mod);
  1984. preempt_enable();
  1985. }
  1986. static __always_inline
  1987. void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
  1988. {
  1989. struct bpf_prog *prog = link->link.prog;
  1990. struct bpf_run_ctx *old_run_ctx;
  1991. struct bpf_trace_run_ctx run_ctx;
  1992. cant_sleep();
  1993. if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
  1994. bpf_prog_inc_misses_counter(prog);
  1995. goto out;
  1996. }
  1997. run_ctx.bpf_cookie = link->cookie;
  1998. old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  1999. rcu_read_lock();
  2000. (void) bpf_prog_run(prog, args);
  2001. rcu_read_unlock();
  2002. bpf_reset_run_ctx(old_run_ctx);
  2003. out:
  2004. this_cpu_dec(*(prog->active));
  2005. }
  2006. #define UNPACK(...) __VA_ARGS__
  2007. #define REPEAT_1(FN, DL, X, ...) FN(X)
  2008. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  2009. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  2010. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  2011. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  2012. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  2013. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  2014. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  2015. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  2016. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  2017. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  2018. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  2019. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  2020. #define SARG(X) u64 arg##X
  2021. #define COPY(X) args[X] = arg##X
  2022. #define __DL_COM (,)
  2023. #define __DL_SEM (;)
  2024. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  2025. #define BPF_TRACE_DEFN_x(x) \
  2026. void bpf_trace_run##x(struct bpf_raw_tp_link *link, \
  2027. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  2028. { \
  2029. u64 args[x]; \
  2030. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  2031. __bpf_trace_run(link, args); \
  2032. } \
  2033. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  2034. BPF_TRACE_DEFN_x(1);
  2035. BPF_TRACE_DEFN_x(2);
  2036. BPF_TRACE_DEFN_x(3);
  2037. BPF_TRACE_DEFN_x(4);
  2038. BPF_TRACE_DEFN_x(5);
  2039. BPF_TRACE_DEFN_x(6);
  2040. BPF_TRACE_DEFN_x(7);
  2041. BPF_TRACE_DEFN_x(8);
  2042. BPF_TRACE_DEFN_x(9);
  2043. BPF_TRACE_DEFN_x(10);
  2044. BPF_TRACE_DEFN_x(11);
  2045. BPF_TRACE_DEFN_x(12);
  2046. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
  2047. {
  2048. struct tracepoint *tp = btp->tp;
  2049. struct bpf_prog *prog = link->link.prog;
  2050. /*
  2051. * check that program doesn't access arguments beyond what's
  2052. * available in this tracepoint
  2053. */
  2054. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  2055. return -EINVAL;
  2056. if (prog->aux->max_tp_access > btp->writable_size)
  2057. return -EINVAL;
  2058. return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link);
  2059. }
  2060. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
  2061. {
  2062. return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, link);
  2063. }
  2064. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  2065. u32 *fd_type, const char **buf,
  2066. u64 *probe_offset, u64 *probe_addr,
  2067. unsigned long *missed)
  2068. {
  2069. bool is_tracepoint, is_syscall_tp;
  2070. struct bpf_prog *prog;
  2071. int flags, err = 0;
  2072. prog = event->prog;
  2073. if (!prog)
  2074. return -ENOENT;
  2075. /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
  2076. if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
  2077. return -EOPNOTSUPP;
  2078. *prog_id = prog->aux->id;
  2079. flags = event->tp_event->flags;
  2080. is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
  2081. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  2082. if (is_tracepoint || is_syscall_tp) {
  2083. *buf = is_tracepoint ? event->tp_event->tp->name
  2084. : event->tp_event->name;
  2085. /* We allow NULL pointer for tracepoint */
  2086. if (fd_type)
  2087. *fd_type = BPF_FD_TYPE_TRACEPOINT;
  2088. if (probe_offset)
  2089. *probe_offset = 0x0;
  2090. if (probe_addr)
  2091. *probe_addr = 0x0;
  2092. } else {
  2093. /* kprobe/uprobe */
  2094. err = -EOPNOTSUPP;
  2095. #ifdef CONFIG_KPROBE_EVENTS
  2096. if (flags & TRACE_EVENT_FL_KPROBE)
  2097. err = bpf_get_kprobe_info(event, fd_type, buf,
  2098. probe_offset, probe_addr, missed,
  2099. event->attr.type == PERF_TYPE_TRACEPOINT);
  2100. #endif
  2101. #ifdef CONFIG_UPROBE_EVENTS
  2102. if (flags & TRACE_EVENT_FL_UPROBE)
  2103. err = bpf_get_uprobe_info(event, fd_type, buf,
  2104. probe_offset, probe_addr,
  2105. event->attr.type == PERF_TYPE_TRACEPOINT);
  2106. #endif
  2107. }
  2108. return err;
  2109. }
  2110. static int __init send_signal_irq_work_init(void)
  2111. {
  2112. int cpu;
  2113. struct send_signal_irq_work *work;
  2114. for_each_possible_cpu(cpu) {
  2115. work = per_cpu_ptr(&send_signal_work, cpu);
  2116. init_irq_work(&work->irq_work, do_bpf_send_signal);
  2117. }
  2118. return 0;
  2119. }
  2120. subsys_initcall(send_signal_irq_work_init);
  2121. #ifdef CONFIG_MODULES
  2122. static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
  2123. void *module)
  2124. {
  2125. struct bpf_trace_module *btm, *tmp;
  2126. struct module *mod = module;
  2127. int ret = 0;
  2128. if (mod->num_bpf_raw_events == 0 ||
  2129. (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
  2130. goto out;
  2131. mutex_lock(&bpf_module_mutex);
  2132. switch (op) {
  2133. case MODULE_STATE_COMING:
  2134. btm = kzalloc(sizeof(*btm), GFP_KERNEL);
  2135. if (btm) {
  2136. btm->module = module;
  2137. list_add(&btm->list, &bpf_trace_modules);
  2138. } else {
  2139. ret = -ENOMEM;
  2140. }
  2141. break;
  2142. case MODULE_STATE_GOING:
  2143. list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
  2144. if (btm->module == module) {
  2145. list_del(&btm->list);
  2146. kfree(btm);
  2147. break;
  2148. }
  2149. }
  2150. break;
  2151. }
  2152. mutex_unlock(&bpf_module_mutex);
  2153. out:
  2154. return notifier_from_errno(ret);
  2155. }
  2156. static struct notifier_block bpf_module_nb = {
  2157. .notifier_call = bpf_event_notify,
  2158. };
  2159. static int __init bpf_event_init(void)
  2160. {
  2161. register_module_notifier(&bpf_module_nb);
  2162. return 0;
  2163. }
  2164. fs_initcall(bpf_event_init);
  2165. #endif /* CONFIG_MODULES */
  2166. struct bpf_session_run_ctx {
  2167. struct bpf_run_ctx run_ctx;
  2168. bool is_return;
  2169. void *data;
  2170. };
  2171. #ifdef CONFIG_FPROBE
  2172. struct bpf_kprobe_multi_link {
  2173. struct bpf_link link;
  2174. struct fprobe fp;
  2175. unsigned long *addrs;
  2176. u64 *cookies;
  2177. u32 cnt;
  2178. u32 mods_cnt;
  2179. struct module **mods;
  2180. u32 flags;
  2181. };
  2182. struct bpf_kprobe_multi_run_ctx {
  2183. struct bpf_session_run_ctx session_ctx;
  2184. struct bpf_kprobe_multi_link *link;
  2185. unsigned long entry_ip;
  2186. };
  2187. struct user_syms {
  2188. const char **syms;
  2189. char *buf;
  2190. };
  2191. static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
  2192. {
  2193. unsigned long __user usymbol;
  2194. const char **syms = NULL;
  2195. char *buf = NULL, *p;
  2196. int err = -ENOMEM;
  2197. unsigned int i;
  2198. syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
  2199. if (!syms)
  2200. goto error;
  2201. buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
  2202. if (!buf)
  2203. goto error;
  2204. for (p = buf, i = 0; i < cnt; i++) {
  2205. if (__get_user(usymbol, usyms + i)) {
  2206. err = -EFAULT;
  2207. goto error;
  2208. }
  2209. err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
  2210. if (err == KSYM_NAME_LEN)
  2211. err = -E2BIG;
  2212. if (err < 0)
  2213. goto error;
  2214. syms[i] = p;
  2215. p += err + 1;
  2216. }
  2217. us->syms = syms;
  2218. us->buf = buf;
  2219. return 0;
  2220. error:
  2221. if (err) {
  2222. kvfree(syms);
  2223. kvfree(buf);
  2224. }
  2225. return err;
  2226. }
  2227. static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
  2228. {
  2229. u32 i;
  2230. for (i = 0; i < cnt; i++)
  2231. module_put(mods[i]);
  2232. }
  2233. static void free_user_syms(struct user_syms *us)
  2234. {
  2235. kvfree(us->syms);
  2236. kvfree(us->buf);
  2237. }
  2238. static void bpf_kprobe_multi_link_release(struct bpf_link *link)
  2239. {
  2240. struct bpf_kprobe_multi_link *kmulti_link;
  2241. kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2242. unregister_fprobe(&kmulti_link->fp);
  2243. kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
  2244. }
  2245. static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
  2246. {
  2247. struct bpf_kprobe_multi_link *kmulti_link;
  2248. kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2249. kvfree(kmulti_link->addrs);
  2250. kvfree(kmulti_link->cookies);
  2251. kfree(kmulti_link->mods);
  2252. kfree(kmulti_link);
  2253. }
  2254. static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
  2255. struct bpf_link_info *info)
  2256. {
  2257. u64 __user *ucookies = u64_to_user_ptr(info->kprobe_multi.cookies);
  2258. u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
  2259. struct bpf_kprobe_multi_link *kmulti_link;
  2260. u32 ucount = info->kprobe_multi.count;
  2261. int err = 0, i;
  2262. if (!uaddrs ^ !ucount)
  2263. return -EINVAL;
  2264. if (ucookies && !ucount)
  2265. return -EINVAL;
  2266. kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2267. info->kprobe_multi.count = kmulti_link->cnt;
  2268. info->kprobe_multi.flags = kmulti_link->flags;
  2269. info->kprobe_multi.missed = kmulti_link->fp.nmissed;
  2270. if (!uaddrs)
  2271. return 0;
  2272. if (ucount < kmulti_link->cnt)
  2273. err = -ENOSPC;
  2274. else
  2275. ucount = kmulti_link->cnt;
  2276. if (ucookies) {
  2277. if (kmulti_link->cookies) {
  2278. if (copy_to_user(ucookies, kmulti_link->cookies, ucount * sizeof(u64)))
  2279. return -EFAULT;
  2280. } else {
  2281. for (i = 0; i < ucount; i++) {
  2282. if (put_user(0, ucookies + i))
  2283. return -EFAULT;
  2284. }
  2285. }
  2286. }
  2287. if (kallsyms_show_value(current_cred())) {
  2288. if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
  2289. return -EFAULT;
  2290. } else {
  2291. for (i = 0; i < ucount; i++) {
  2292. if (put_user(0, uaddrs + i))
  2293. return -EFAULT;
  2294. }
  2295. }
  2296. return err;
  2297. }
  2298. static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
  2299. .release = bpf_kprobe_multi_link_release,
  2300. .dealloc_deferred = bpf_kprobe_multi_link_dealloc,
  2301. .fill_link_info = bpf_kprobe_multi_link_fill_link_info,
  2302. };
  2303. static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
  2304. {
  2305. const struct bpf_kprobe_multi_link *link = priv;
  2306. unsigned long *addr_a = a, *addr_b = b;
  2307. u64 *cookie_a, *cookie_b;
  2308. cookie_a = link->cookies + (addr_a - link->addrs);
  2309. cookie_b = link->cookies + (addr_b - link->addrs);
  2310. /* swap addr_a/addr_b and cookie_a/cookie_b values */
  2311. swap(*addr_a, *addr_b);
  2312. swap(*cookie_a, *cookie_b);
  2313. }
  2314. static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
  2315. {
  2316. const unsigned long *addr_a = a, *addr_b = b;
  2317. if (*addr_a == *addr_b)
  2318. return 0;
  2319. return *addr_a < *addr_b ? -1 : 1;
  2320. }
  2321. static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
  2322. {
  2323. return bpf_kprobe_multi_addrs_cmp(a, b);
  2324. }
  2325. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2326. {
  2327. struct bpf_kprobe_multi_run_ctx *run_ctx;
  2328. struct bpf_kprobe_multi_link *link;
  2329. u64 *cookie, entry_ip;
  2330. unsigned long *addr;
  2331. if (WARN_ON_ONCE(!ctx))
  2332. return 0;
  2333. run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
  2334. session_ctx.run_ctx);
  2335. link = run_ctx->link;
  2336. if (!link->cookies)
  2337. return 0;
  2338. entry_ip = run_ctx->entry_ip;
  2339. addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
  2340. bpf_kprobe_multi_addrs_cmp);
  2341. if (!addr)
  2342. return 0;
  2343. cookie = link->cookies + (addr - link->addrs);
  2344. return *cookie;
  2345. }
  2346. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2347. {
  2348. struct bpf_kprobe_multi_run_ctx *run_ctx;
  2349. run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
  2350. session_ctx.run_ctx);
  2351. return run_ctx->entry_ip;
  2352. }
  2353. static int
  2354. kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
  2355. unsigned long entry_ip, struct pt_regs *regs,
  2356. bool is_return, void *data)
  2357. {
  2358. struct bpf_kprobe_multi_run_ctx run_ctx = {
  2359. .session_ctx = {
  2360. .is_return = is_return,
  2361. .data = data,
  2362. },
  2363. .link = link,
  2364. .entry_ip = entry_ip,
  2365. };
  2366. struct bpf_run_ctx *old_run_ctx;
  2367. int err;
  2368. /*
  2369. * graph tracer framework ensures we won't migrate, so there is no need
  2370. * to use migrate_disable for bpf_prog_run again. The check here just for
  2371. * __this_cpu_inc_return.
  2372. */
  2373. cant_sleep();
  2374. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  2375. bpf_prog_inc_misses_counter(link->link.prog);
  2376. err = 0;
  2377. goto out;
  2378. }
  2379. rcu_read_lock();
  2380. old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
  2381. err = bpf_prog_run(link->link.prog, regs);
  2382. bpf_reset_run_ctx(old_run_ctx);
  2383. rcu_read_unlock();
  2384. out:
  2385. __this_cpu_dec(bpf_prog_active);
  2386. return err;
  2387. }
  2388. static int
  2389. kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
  2390. unsigned long ret_ip, struct pt_regs *regs,
  2391. void *data)
  2392. {
  2393. struct bpf_kprobe_multi_link *link;
  2394. int err;
  2395. link = container_of(fp, struct bpf_kprobe_multi_link, fp);
  2396. err = kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, false, data);
  2397. return is_kprobe_session(link->link.prog) ? err : 0;
  2398. }
  2399. static void
  2400. kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
  2401. unsigned long ret_ip, struct pt_regs *regs,
  2402. void *data)
  2403. {
  2404. struct bpf_kprobe_multi_link *link;
  2405. link = container_of(fp, struct bpf_kprobe_multi_link, fp);
  2406. kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, data);
  2407. }
  2408. static int symbols_cmp_r(const void *a, const void *b, const void *priv)
  2409. {
  2410. const char **str_a = (const char **) a;
  2411. const char **str_b = (const char **) b;
  2412. return strcmp(*str_a, *str_b);
  2413. }
  2414. struct multi_symbols_sort {
  2415. const char **funcs;
  2416. u64 *cookies;
  2417. };
  2418. static void symbols_swap_r(void *a, void *b, int size, const void *priv)
  2419. {
  2420. const struct multi_symbols_sort *data = priv;
  2421. const char **name_a = a, **name_b = b;
  2422. swap(*name_a, *name_b);
  2423. /* If defined, swap also related cookies. */
  2424. if (data->cookies) {
  2425. u64 *cookie_a, *cookie_b;
  2426. cookie_a = data->cookies + (name_a - data->funcs);
  2427. cookie_b = data->cookies + (name_b - data->funcs);
  2428. swap(*cookie_a, *cookie_b);
  2429. }
  2430. }
  2431. struct modules_array {
  2432. struct module **mods;
  2433. int mods_cnt;
  2434. int mods_cap;
  2435. };
  2436. static int add_module(struct modules_array *arr, struct module *mod)
  2437. {
  2438. struct module **mods;
  2439. if (arr->mods_cnt == arr->mods_cap) {
  2440. arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
  2441. mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
  2442. if (!mods)
  2443. return -ENOMEM;
  2444. arr->mods = mods;
  2445. }
  2446. arr->mods[arr->mods_cnt] = mod;
  2447. arr->mods_cnt++;
  2448. return 0;
  2449. }
  2450. static bool has_module(struct modules_array *arr, struct module *mod)
  2451. {
  2452. int i;
  2453. for (i = arr->mods_cnt - 1; i >= 0; i--) {
  2454. if (arr->mods[i] == mod)
  2455. return true;
  2456. }
  2457. return false;
  2458. }
  2459. static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
  2460. {
  2461. struct modules_array arr = {};
  2462. u32 i, err = 0;
  2463. for (i = 0; i < addrs_cnt; i++) {
  2464. struct module *mod;
  2465. preempt_disable();
  2466. mod = __module_address(addrs[i]);
  2467. /* Either no module or we it's already stored */
  2468. if (!mod || has_module(&arr, mod)) {
  2469. preempt_enable();
  2470. continue;
  2471. }
  2472. if (!try_module_get(mod))
  2473. err = -EINVAL;
  2474. preempt_enable();
  2475. if (err)
  2476. break;
  2477. err = add_module(&arr, mod);
  2478. if (err) {
  2479. module_put(mod);
  2480. break;
  2481. }
  2482. }
  2483. /* We return either err < 0 in case of error, ... */
  2484. if (err) {
  2485. kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
  2486. kfree(arr.mods);
  2487. return err;
  2488. }
  2489. /* or number of modules found if everything is ok. */
  2490. *mods = arr.mods;
  2491. return arr.mods_cnt;
  2492. }
  2493. static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
  2494. {
  2495. u32 i;
  2496. for (i = 0; i < cnt; i++) {
  2497. if (!within_error_injection_list(addrs[i]))
  2498. return -EINVAL;
  2499. }
  2500. return 0;
  2501. }
  2502. int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2503. {
  2504. struct bpf_kprobe_multi_link *link = NULL;
  2505. struct bpf_link_primer link_primer;
  2506. void __user *ucookies;
  2507. unsigned long *addrs;
  2508. u32 flags, cnt, size;
  2509. void __user *uaddrs;
  2510. u64 *cookies = NULL;
  2511. void __user *usyms;
  2512. int err;
  2513. /* no support for 32bit archs yet */
  2514. if (sizeof(u64) != sizeof(void *))
  2515. return -EOPNOTSUPP;
  2516. if (attr->link_create.flags)
  2517. return -EINVAL;
  2518. if (!is_kprobe_multi(prog))
  2519. return -EINVAL;
  2520. flags = attr->link_create.kprobe_multi.flags;
  2521. if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
  2522. return -EINVAL;
  2523. uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
  2524. usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
  2525. if (!!uaddrs == !!usyms)
  2526. return -EINVAL;
  2527. cnt = attr->link_create.kprobe_multi.cnt;
  2528. if (!cnt)
  2529. return -EINVAL;
  2530. if (cnt > MAX_KPROBE_MULTI_CNT)
  2531. return -E2BIG;
  2532. size = cnt * sizeof(*addrs);
  2533. addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
  2534. if (!addrs)
  2535. return -ENOMEM;
  2536. ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
  2537. if (ucookies) {
  2538. cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
  2539. if (!cookies) {
  2540. err = -ENOMEM;
  2541. goto error;
  2542. }
  2543. if (copy_from_user(cookies, ucookies, size)) {
  2544. err = -EFAULT;
  2545. goto error;
  2546. }
  2547. }
  2548. if (uaddrs) {
  2549. if (copy_from_user(addrs, uaddrs, size)) {
  2550. err = -EFAULT;
  2551. goto error;
  2552. }
  2553. } else {
  2554. struct multi_symbols_sort data = {
  2555. .cookies = cookies,
  2556. };
  2557. struct user_syms us;
  2558. err = copy_user_syms(&us, usyms, cnt);
  2559. if (err)
  2560. goto error;
  2561. if (cookies)
  2562. data.funcs = us.syms;
  2563. sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
  2564. symbols_swap_r, &data);
  2565. err = ftrace_lookup_symbols(us.syms, cnt, addrs);
  2566. free_user_syms(&us);
  2567. if (err)
  2568. goto error;
  2569. }
  2570. if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
  2571. err = -EINVAL;
  2572. goto error;
  2573. }
  2574. link = kzalloc(sizeof(*link), GFP_KERNEL);
  2575. if (!link) {
  2576. err = -ENOMEM;
  2577. goto error;
  2578. }
  2579. bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
  2580. &bpf_kprobe_multi_link_lops, prog);
  2581. err = bpf_link_prime(&link->link, &link_primer);
  2582. if (err)
  2583. goto error;
  2584. if (!(flags & BPF_F_KPROBE_MULTI_RETURN))
  2585. link->fp.entry_handler = kprobe_multi_link_handler;
  2586. if ((flags & BPF_F_KPROBE_MULTI_RETURN) || is_kprobe_session(prog))
  2587. link->fp.exit_handler = kprobe_multi_link_exit_handler;
  2588. if (is_kprobe_session(prog))
  2589. link->fp.entry_data_size = sizeof(u64);
  2590. link->addrs = addrs;
  2591. link->cookies = cookies;
  2592. link->cnt = cnt;
  2593. link->flags = flags;
  2594. if (cookies) {
  2595. /*
  2596. * Sorting addresses will trigger sorting cookies as well
  2597. * (check bpf_kprobe_multi_cookie_swap). This way we can
  2598. * find cookie based on the address in bpf_get_attach_cookie
  2599. * helper.
  2600. */
  2601. sort_r(addrs, cnt, sizeof(*addrs),
  2602. bpf_kprobe_multi_cookie_cmp,
  2603. bpf_kprobe_multi_cookie_swap,
  2604. link);
  2605. }
  2606. err = get_modules_for_addrs(&link->mods, addrs, cnt);
  2607. if (err < 0) {
  2608. bpf_link_cleanup(&link_primer);
  2609. return err;
  2610. }
  2611. link->mods_cnt = err;
  2612. err = register_fprobe_ips(&link->fp, addrs, cnt);
  2613. if (err) {
  2614. kprobe_multi_put_modules(link->mods, link->mods_cnt);
  2615. bpf_link_cleanup(&link_primer);
  2616. return err;
  2617. }
  2618. return bpf_link_settle(&link_primer);
  2619. error:
  2620. kfree(link);
  2621. kvfree(addrs);
  2622. kvfree(cookies);
  2623. return err;
  2624. }
  2625. #else /* !CONFIG_FPROBE */
  2626. int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2627. {
  2628. return -EOPNOTSUPP;
  2629. }
  2630. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2631. {
  2632. return 0;
  2633. }
  2634. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2635. {
  2636. return 0;
  2637. }
  2638. #endif
  2639. #ifdef CONFIG_UPROBES
  2640. struct bpf_uprobe_multi_link;
  2641. struct bpf_uprobe {
  2642. struct bpf_uprobe_multi_link *link;
  2643. loff_t offset;
  2644. unsigned long ref_ctr_offset;
  2645. u64 cookie;
  2646. struct uprobe *uprobe;
  2647. struct uprobe_consumer consumer;
  2648. };
  2649. struct bpf_uprobe_multi_link {
  2650. struct path path;
  2651. struct bpf_link link;
  2652. u32 cnt;
  2653. u32 flags;
  2654. struct bpf_uprobe *uprobes;
  2655. struct task_struct *task;
  2656. };
  2657. struct bpf_uprobe_multi_run_ctx {
  2658. struct bpf_run_ctx run_ctx;
  2659. unsigned long entry_ip;
  2660. struct bpf_uprobe *uprobe;
  2661. };
  2662. static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt)
  2663. {
  2664. u32 i;
  2665. for (i = 0; i < cnt; i++)
  2666. uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer);
  2667. if (cnt)
  2668. uprobe_unregister_sync();
  2669. }
  2670. static void bpf_uprobe_multi_link_release(struct bpf_link *link)
  2671. {
  2672. struct bpf_uprobe_multi_link *umulti_link;
  2673. umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
  2674. bpf_uprobe_unregister(umulti_link->uprobes, umulti_link->cnt);
  2675. if (umulti_link->task)
  2676. put_task_struct(umulti_link->task);
  2677. path_put(&umulti_link->path);
  2678. }
  2679. static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
  2680. {
  2681. struct bpf_uprobe_multi_link *umulti_link;
  2682. umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
  2683. kvfree(umulti_link->uprobes);
  2684. kfree(umulti_link);
  2685. }
  2686. static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link,
  2687. struct bpf_link_info *info)
  2688. {
  2689. u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets);
  2690. u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies);
  2691. u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets);
  2692. u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path);
  2693. u32 upath_size = info->uprobe_multi.path_size;
  2694. struct bpf_uprobe_multi_link *umulti_link;
  2695. u32 ucount = info->uprobe_multi.count;
  2696. int err = 0, i;
  2697. char *p, *buf;
  2698. long left = 0;
  2699. if (!upath ^ !upath_size)
  2700. return -EINVAL;
  2701. if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount)
  2702. return -EINVAL;
  2703. umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
  2704. info->uprobe_multi.count = umulti_link->cnt;
  2705. info->uprobe_multi.flags = umulti_link->flags;
  2706. info->uprobe_multi.pid = umulti_link->task ?
  2707. task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0;
  2708. upath_size = upath_size ? min_t(u32, upath_size, PATH_MAX) : PATH_MAX;
  2709. buf = kmalloc(upath_size, GFP_KERNEL);
  2710. if (!buf)
  2711. return -ENOMEM;
  2712. p = d_path(&umulti_link->path, buf, upath_size);
  2713. if (IS_ERR(p)) {
  2714. kfree(buf);
  2715. return PTR_ERR(p);
  2716. }
  2717. upath_size = buf + upath_size - p;
  2718. if (upath)
  2719. left = copy_to_user(upath, p, upath_size);
  2720. kfree(buf);
  2721. if (left)
  2722. return -EFAULT;
  2723. info->uprobe_multi.path_size = upath_size;
  2724. if (!uoffsets && !ucookies && !uref_ctr_offsets)
  2725. return 0;
  2726. if (ucount < umulti_link->cnt)
  2727. err = -ENOSPC;
  2728. else
  2729. ucount = umulti_link->cnt;
  2730. for (i = 0; i < ucount; i++) {
  2731. if (uoffsets &&
  2732. put_user(umulti_link->uprobes[i].offset, uoffsets + i))
  2733. return -EFAULT;
  2734. if (uref_ctr_offsets &&
  2735. put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i))
  2736. return -EFAULT;
  2737. if (ucookies &&
  2738. put_user(umulti_link->uprobes[i].cookie, ucookies + i))
  2739. return -EFAULT;
  2740. }
  2741. return err;
  2742. }
  2743. static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
  2744. .release = bpf_uprobe_multi_link_release,
  2745. .dealloc_deferred = bpf_uprobe_multi_link_dealloc,
  2746. .fill_link_info = bpf_uprobe_multi_link_fill_link_info,
  2747. };
  2748. static int uprobe_prog_run(struct bpf_uprobe *uprobe,
  2749. unsigned long entry_ip,
  2750. struct pt_regs *regs)
  2751. {
  2752. struct bpf_uprobe_multi_link *link = uprobe->link;
  2753. struct bpf_uprobe_multi_run_ctx run_ctx = {
  2754. .entry_ip = entry_ip,
  2755. .uprobe = uprobe,
  2756. };
  2757. struct bpf_prog *prog = link->link.prog;
  2758. bool sleepable = prog->sleepable;
  2759. struct bpf_run_ctx *old_run_ctx;
  2760. if (link->task && !same_thread_group(current, link->task))
  2761. return 0;
  2762. if (sleepable)
  2763. rcu_read_lock_trace();
  2764. else
  2765. rcu_read_lock();
  2766. migrate_disable();
  2767. old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  2768. bpf_prog_run(link->link.prog, regs);
  2769. bpf_reset_run_ctx(old_run_ctx);
  2770. migrate_enable();
  2771. if (sleepable)
  2772. rcu_read_unlock_trace();
  2773. else
  2774. rcu_read_unlock();
  2775. return 0;
  2776. }
  2777. static bool
  2778. uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm)
  2779. {
  2780. struct bpf_uprobe *uprobe;
  2781. uprobe = container_of(con, struct bpf_uprobe, consumer);
  2782. return uprobe->link->task->mm == mm;
  2783. }
  2784. static int
  2785. uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs)
  2786. {
  2787. struct bpf_uprobe *uprobe;
  2788. uprobe = container_of(con, struct bpf_uprobe, consumer);
  2789. return uprobe_prog_run(uprobe, instruction_pointer(regs), regs);
  2790. }
  2791. static int
  2792. uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs)
  2793. {
  2794. struct bpf_uprobe *uprobe;
  2795. uprobe = container_of(con, struct bpf_uprobe, consumer);
  2796. return uprobe_prog_run(uprobe, func, regs);
  2797. }
  2798. static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2799. {
  2800. struct bpf_uprobe_multi_run_ctx *run_ctx;
  2801. run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
  2802. return run_ctx->entry_ip;
  2803. }
  2804. static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2805. {
  2806. struct bpf_uprobe_multi_run_ctx *run_ctx;
  2807. run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
  2808. return run_ctx->uprobe->cookie;
  2809. }
  2810. int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2811. {
  2812. struct bpf_uprobe_multi_link *link = NULL;
  2813. unsigned long __user *uref_ctr_offsets;
  2814. struct bpf_link_primer link_primer;
  2815. struct bpf_uprobe *uprobes = NULL;
  2816. struct task_struct *task = NULL;
  2817. unsigned long __user *uoffsets;
  2818. u64 __user *ucookies;
  2819. void __user *upath;
  2820. u32 flags, cnt, i;
  2821. struct path path;
  2822. char *name;
  2823. pid_t pid;
  2824. int err;
  2825. /* no support for 32bit archs yet */
  2826. if (sizeof(u64) != sizeof(void *))
  2827. return -EOPNOTSUPP;
  2828. if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI)
  2829. return -EINVAL;
  2830. flags = attr->link_create.uprobe_multi.flags;
  2831. if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
  2832. return -EINVAL;
  2833. /*
  2834. * path, offsets and cnt are mandatory,
  2835. * ref_ctr_offsets and cookies are optional
  2836. */
  2837. upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
  2838. uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
  2839. cnt = attr->link_create.uprobe_multi.cnt;
  2840. pid = attr->link_create.uprobe_multi.pid;
  2841. if (!upath || !uoffsets || !cnt || pid < 0)
  2842. return -EINVAL;
  2843. if (cnt > MAX_UPROBE_MULTI_CNT)
  2844. return -E2BIG;
  2845. uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
  2846. ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
  2847. name = strndup_user(upath, PATH_MAX);
  2848. if (IS_ERR(name)) {
  2849. err = PTR_ERR(name);
  2850. return err;
  2851. }
  2852. err = kern_path(name, LOOKUP_FOLLOW, &path);
  2853. kfree(name);
  2854. if (err)
  2855. return err;
  2856. if (!d_is_reg(path.dentry)) {
  2857. err = -EBADF;
  2858. goto error_path_put;
  2859. }
  2860. if (pid) {
  2861. rcu_read_lock();
  2862. task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
  2863. rcu_read_unlock();
  2864. if (!task) {
  2865. err = -ESRCH;
  2866. goto error_path_put;
  2867. }
  2868. }
  2869. err = -ENOMEM;
  2870. link = kzalloc(sizeof(*link), GFP_KERNEL);
  2871. uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
  2872. if (!uprobes || !link)
  2873. goto error_free;
  2874. for (i = 0; i < cnt; i++) {
  2875. if (__get_user(uprobes[i].offset, uoffsets + i)) {
  2876. err = -EFAULT;
  2877. goto error_free;
  2878. }
  2879. if (uprobes[i].offset < 0) {
  2880. err = -EINVAL;
  2881. goto error_free;
  2882. }
  2883. if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
  2884. err = -EFAULT;
  2885. goto error_free;
  2886. }
  2887. if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
  2888. err = -EFAULT;
  2889. goto error_free;
  2890. }
  2891. uprobes[i].link = link;
  2892. if (flags & BPF_F_UPROBE_MULTI_RETURN)
  2893. uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
  2894. else
  2895. uprobes[i].consumer.handler = uprobe_multi_link_handler;
  2896. if (pid)
  2897. uprobes[i].consumer.filter = uprobe_multi_link_filter;
  2898. }
  2899. link->cnt = cnt;
  2900. link->uprobes = uprobes;
  2901. link->path = path;
  2902. link->task = task;
  2903. link->flags = flags;
  2904. bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
  2905. &bpf_uprobe_multi_link_lops, prog);
  2906. for (i = 0; i < cnt; i++) {
  2907. uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry),
  2908. uprobes[i].offset,
  2909. uprobes[i].ref_ctr_offset,
  2910. &uprobes[i].consumer);
  2911. if (IS_ERR(uprobes[i].uprobe)) {
  2912. err = PTR_ERR(uprobes[i].uprobe);
  2913. link->cnt = i;
  2914. goto error_unregister;
  2915. }
  2916. }
  2917. err = bpf_link_prime(&link->link, &link_primer);
  2918. if (err)
  2919. goto error_unregister;
  2920. return bpf_link_settle(&link_primer);
  2921. error_unregister:
  2922. bpf_uprobe_unregister(uprobes, link->cnt);
  2923. error_free:
  2924. kvfree(uprobes);
  2925. kfree(link);
  2926. if (task)
  2927. put_task_struct(task);
  2928. error_path_put:
  2929. path_put(&path);
  2930. return err;
  2931. }
  2932. #else /* !CONFIG_UPROBES */
  2933. int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2934. {
  2935. return -EOPNOTSUPP;
  2936. }
  2937. static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2938. {
  2939. return 0;
  2940. }
  2941. static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2942. {
  2943. return 0;
  2944. }
  2945. #endif /* CONFIG_UPROBES */
  2946. __bpf_kfunc_start_defs();
  2947. __bpf_kfunc bool bpf_session_is_return(void)
  2948. {
  2949. struct bpf_session_run_ctx *session_ctx;
  2950. session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
  2951. return session_ctx->is_return;
  2952. }
  2953. __bpf_kfunc __u64 *bpf_session_cookie(void)
  2954. {
  2955. struct bpf_session_run_ctx *session_ctx;
  2956. session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
  2957. return session_ctx->data;
  2958. }
  2959. __bpf_kfunc_end_defs();
  2960. BTF_KFUNCS_START(kprobe_multi_kfunc_set_ids)
  2961. BTF_ID_FLAGS(func, bpf_session_is_return)
  2962. BTF_ID_FLAGS(func, bpf_session_cookie)
  2963. BTF_KFUNCS_END(kprobe_multi_kfunc_set_ids)
  2964. static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id)
  2965. {
  2966. if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id))
  2967. return 0;
  2968. if (!is_kprobe_session(prog))
  2969. return -EACCES;
  2970. return 0;
  2971. }
  2972. static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = {
  2973. .owner = THIS_MODULE,
  2974. .set = &kprobe_multi_kfunc_set_ids,
  2975. .filter = bpf_kprobe_multi_filter,
  2976. };
  2977. static int __init bpf_kprobe_multi_kfuncs_init(void)
  2978. {
  2979. return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
  2980. }
  2981. late_initcall(bpf_kprobe_multi_kfuncs_init);