bpf_trace.c 91 KB

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