bpf_trace.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842
  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_perf_event.h>
  10. #include <linux/btf.h>
  11. #include <linux/filter.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/ctype.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/error-injection.h>
  18. #include <linux/btf_ids.h>
  19. #include <linux/bpf_lsm.h>
  20. #include <linux/fprobe.h>
  21. #include <linux/bsearch.h>
  22. #include <linux/sort.h>
  23. #include <linux/key.h>
  24. #include <linux/verification.h>
  25. #include <net/bpf_sk_storage.h>
  26. #include <uapi/linux/bpf.h>
  27. #include <uapi/linux/btf.h>
  28. #include <asm/tlb.h>
  29. #include "trace_probe.h"
  30. #include "trace.h"
  31. #define CREATE_TRACE_POINTS
  32. #include "bpf_trace.h"
  33. #define bpf_event_rcu_dereference(p) \
  34. rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
  35. #ifdef CONFIG_MODULES
  36. struct bpf_trace_module {
  37. struct module *module;
  38. struct list_head list;
  39. };
  40. static LIST_HEAD(bpf_trace_modules);
  41. static DEFINE_MUTEX(bpf_module_mutex);
  42. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  43. {
  44. struct bpf_raw_event_map *btp, *ret = NULL;
  45. struct bpf_trace_module *btm;
  46. unsigned int i;
  47. mutex_lock(&bpf_module_mutex);
  48. list_for_each_entry(btm, &bpf_trace_modules, list) {
  49. for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
  50. btp = &btm->module->bpf_raw_events[i];
  51. if (!strcmp(btp->tp->name, name)) {
  52. if (try_module_get(btm->module))
  53. ret = btp;
  54. goto out;
  55. }
  56. }
  57. }
  58. out:
  59. mutex_unlock(&bpf_module_mutex);
  60. return ret;
  61. }
  62. #else
  63. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  64. {
  65. return NULL;
  66. }
  67. #endif /* CONFIG_MODULES */
  68. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  69. u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  70. static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
  71. u64 flags, const struct btf **btf,
  72. s32 *btf_id);
  73. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
  74. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
  75. /**
  76. * trace_call_bpf - invoke BPF program
  77. * @call: tracepoint event
  78. * @ctx: opaque context pointer
  79. *
  80. * kprobe handlers execute BPF programs via this helper.
  81. * Can be used from static tracepoints in the future.
  82. *
  83. * Return: BPF programs always return an integer which is interpreted by
  84. * kprobe handler as:
  85. * 0 - return from kprobe (event is filtered out)
  86. * 1 - store kprobe event into ring buffer
  87. * Other values are reserved and currently alias to 1
  88. */
  89. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  90. {
  91. unsigned int ret;
  92. cant_sleep();
  93. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  94. /*
  95. * since some bpf program is already running on this cpu,
  96. * don't call into another bpf program (same or different)
  97. * and don't send kprobe event into ring-buffer,
  98. * so return zero here
  99. */
  100. ret = 0;
  101. goto out;
  102. }
  103. /*
  104. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  105. * to all call sites, we did a bpf_prog_array_valid() there to check
  106. * whether call->prog_array is empty or not, which is
  107. * a heuristic to speed up execution.
  108. *
  109. * If bpf_prog_array_valid() fetched prog_array was
  110. * non-NULL, we go into trace_call_bpf() and do the actual
  111. * proper rcu_dereference() under RCU lock.
  112. * If it turns out that prog_array is NULL then, we bail out.
  113. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  114. * was NULL, you'll skip the prog_array with the risk of missing
  115. * out of events when it was updated in between this and the
  116. * rcu_dereference() which is accepted risk.
  117. */
  118. rcu_read_lock();
  119. ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
  120. ctx, bpf_prog_run);
  121. rcu_read_unlock();
  122. out:
  123. __this_cpu_dec(bpf_prog_active);
  124. return ret;
  125. }
  126. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  127. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  128. {
  129. regs_set_return_value(regs, rc);
  130. override_function_with_return(regs);
  131. return 0;
  132. }
  133. static const struct bpf_func_proto bpf_override_return_proto = {
  134. .func = bpf_override_return,
  135. .gpl_only = true,
  136. .ret_type = RET_INTEGER,
  137. .arg1_type = ARG_PTR_TO_CTX,
  138. .arg2_type = ARG_ANYTHING,
  139. };
  140. #endif
  141. static __always_inline int
  142. bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
  143. {
  144. int ret;
  145. ret = copy_from_user_nofault(dst, unsafe_ptr, size);
  146. if (unlikely(ret < 0))
  147. memset(dst, 0, size);
  148. return ret;
  149. }
  150. BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
  151. const void __user *, unsafe_ptr)
  152. {
  153. return bpf_probe_read_user_common(dst, size, unsafe_ptr);
  154. }
  155. const struct bpf_func_proto bpf_probe_read_user_proto = {
  156. .func = bpf_probe_read_user,
  157. .gpl_only = true,
  158. .ret_type = RET_INTEGER,
  159. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  160. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  161. .arg3_type = ARG_ANYTHING,
  162. };
  163. static __always_inline int
  164. bpf_probe_read_user_str_common(void *dst, u32 size,
  165. const void __user *unsafe_ptr)
  166. {
  167. int ret;
  168. /*
  169. * NB: We rely on strncpy_from_user() not copying junk past the NUL
  170. * terminator into `dst`.
  171. *
  172. * strncpy_from_user() does long-sized strides in the fast path. If the
  173. * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
  174. * then there could be junk after the NUL in `dst`. If user takes `dst`
  175. * and keys a hash map with it, then semantically identical strings can
  176. * occupy multiple entries in the map.
  177. */
  178. ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
  179. if (unlikely(ret < 0))
  180. memset(dst, 0, size);
  181. return ret;
  182. }
  183. BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
  184. const void __user *, unsafe_ptr)
  185. {
  186. return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
  187. }
  188. const struct bpf_func_proto bpf_probe_read_user_str_proto = {
  189. .func = bpf_probe_read_user_str,
  190. .gpl_only = true,
  191. .ret_type = RET_INTEGER,
  192. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  193. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  194. .arg3_type = ARG_ANYTHING,
  195. };
  196. static __always_inline int
  197. bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
  198. {
  199. int ret;
  200. ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
  201. if (unlikely(ret < 0))
  202. memset(dst, 0, size);
  203. return ret;
  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. static DEFINE_RAW_SPINLOCK(trace_printk_lock);
  324. #define MAX_TRACE_PRINTK_VARARGS 3
  325. #define BPF_TRACE_PRINTK_SIZE 1024
  326. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  327. u64, arg2, u64, arg3)
  328. {
  329. u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
  330. u32 *bin_args;
  331. static char buf[BPF_TRACE_PRINTK_SIZE];
  332. unsigned long flags;
  333. int ret;
  334. ret = bpf_bprintf_prepare(fmt, fmt_size, args, &bin_args,
  335. MAX_TRACE_PRINTK_VARARGS);
  336. if (ret < 0)
  337. return ret;
  338. raw_spin_lock_irqsave(&trace_printk_lock, flags);
  339. ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
  340. trace_bpf_trace_printk(buf);
  341. raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
  342. bpf_bprintf_cleanup();
  343. return ret;
  344. }
  345. static const struct bpf_func_proto bpf_trace_printk_proto = {
  346. .func = bpf_trace_printk,
  347. .gpl_only = true,
  348. .ret_type = RET_INTEGER,
  349. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  350. .arg2_type = ARG_CONST_SIZE,
  351. };
  352. static void __set_printk_clr_event(void)
  353. {
  354. /*
  355. * This program might be calling bpf_trace_printk,
  356. * so enable the associated bpf_trace/bpf_trace_printk event.
  357. * Repeat this each time as it is possible a user has
  358. * disabled bpf_trace_printk events. By loading a program
  359. * calling bpf_trace_printk() however the user has expressed
  360. * the intent to see such events.
  361. */
  362. if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
  363. pr_warn_ratelimited("could not enable bpf_trace_printk events");
  364. }
  365. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  366. {
  367. __set_printk_clr_event();
  368. return &bpf_trace_printk_proto;
  369. }
  370. BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
  371. u32, data_len)
  372. {
  373. static char buf[BPF_TRACE_PRINTK_SIZE];
  374. unsigned long flags;
  375. int ret, num_args;
  376. u32 *bin_args;
  377. if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
  378. (data_len && !data))
  379. return -EINVAL;
  380. num_args = data_len / 8;
  381. ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
  382. if (ret < 0)
  383. return ret;
  384. raw_spin_lock_irqsave(&trace_printk_lock, flags);
  385. ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
  386. trace_bpf_trace_printk(buf);
  387. raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
  388. bpf_bprintf_cleanup();
  389. return ret;
  390. }
  391. static const struct bpf_func_proto bpf_trace_vprintk_proto = {
  392. .func = bpf_trace_vprintk,
  393. .gpl_only = true,
  394. .ret_type = RET_INTEGER,
  395. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  396. .arg2_type = ARG_CONST_SIZE,
  397. .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
  398. .arg4_type = ARG_CONST_SIZE_OR_ZERO,
  399. };
  400. const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
  401. {
  402. __set_printk_clr_event();
  403. return &bpf_trace_vprintk_proto;
  404. }
  405. BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
  406. const void *, data, u32, data_len)
  407. {
  408. int err, num_args;
  409. u32 *bin_args;
  410. if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
  411. (data_len && !data))
  412. return -EINVAL;
  413. num_args = data_len / 8;
  414. err = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
  415. if (err < 0)
  416. return err;
  417. seq_bprintf(m, fmt, bin_args);
  418. bpf_bprintf_cleanup();
  419. return seq_has_overflowed(m) ? -EOVERFLOW : 0;
  420. }
  421. BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
  422. static const struct bpf_func_proto bpf_seq_printf_proto = {
  423. .func = bpf_seq_printf,
  424. .gpl_only = true,
  425. .ret_type = RET_INTEGER,
  426. .arg1_type = ARG_PTR_TO_BTF_ID,
  427. .arg1_btf_id = &btf_seq_file_ids[0],
  428. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  429. .arg3_type = ARG_CONST_SIZE,
  430. .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
  431. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  432. };
  433. BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
  434. {
  435. return seq_write(m, data, len) ? -EOVERFLOW : 0;
  436. }
  437. static const struct bpf_func_proto bpf_seq_write_proto = {
  438. .func = bpf_seq_write,
  439. .gpl_only = true,
  440. .ret_type = RET_INTEGER,
  441. .arg1_type = ARG_PTR_TO_BTF_ID,
  442. .arg1_btf_id = &btf_seq_file_ids[0],
  443. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  444. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  445. };
  446. BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
  447. u32, btf_ptr_size, u64, flags)
  448. {
  449. const struct btf *btf;
  450. s32 btf_id;
  451. int ret;
  452. ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
  453. if (ret)
  454. return ret;
  455. return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
  456. }
  457. static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
  458. .func = bpf_seq_printf_btf,
  459. .gpl_only = true,
  460. .ret_type = RET_INTEGER,
  461. .arg1_type = ARG_PTR_TO_BTF_ID,
  462. .arg1_btf_id = &btf_seq_file_ids[0],
  463. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  464. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  465. .arg4_type = ARG_ANYTHING,
  466. };
  467. static __always_inline int
  468. get_map_perf_counter(struct bpf_map *map, u64 flags,
  469. u64 *value, u64 *enabled, u64 *running)
  470. {
  471. struct bpf_array *array = container_of(map, struct bpf_array, map);
  472. unsigned int cpu = smp_processor_id();
  473. u64 index = flags & BPF_F_INDEX_MASK;
  474. struct bpf_event_entry *ee;
  475. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  476. return -EINVAL;
  477. if (index == BPF_F_CURRENT_CPU)
  478. index = cpu;
  479. if (unlikely(index >= array->map.max_entries))
  480. return -E2BIG;
  481. ee = READ_ONCE(array->ptrs[index]);
  482. if (!ee)
  483. return -ENOENT;
  484. return perf_event_read_local(ee->event, value, enabled, running);
  485. }
  486. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  487. {
  488. u64 value = 0;
  489. int err;
  490. err = get_map_perf_counter(map, flags, &value, NULL, NULL);
  491. /*
  492. * this api is ugly since we miss [-22..-2] range of valid
  493. * counter values, but that's uapi
  494. */
  495. if (err)
  496. return err;
  497. return value;
  498. }
  499. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  500. .func = bpf_perf_event_read,
  501. .gpl_only = true,
  502. .ret_type = RET_INTEGER,
  503. .arg1_type = ARG_CONST_MAP_PTR,
  504. .arg2_type = ARG_ANYTHING,
  505. };
  506. BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
  507. struct bpf_perf_event_value *, buf, u32, size)
  508. {
  509. int err = -EINVAL;
  510. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  511. goto clear;
  512. err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
  513. &buf->running);
  514. if (unlikely(err))
  515. goto clear;
  516. return 0;
  517. clear:
  518. memset(buf, 0, size);
  519. return err;
  520. }
  521. static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
  522. .func = bpf_perf_event_read_value,
  523. .gpl_only = true,
  524. .ret_type = RET_INTEGER,
  525. .arg1_type = ARG_CONST_MAP_PTR,
  526. .arg2_type = ARG_ANYTHING,
  527. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  528. .arg4_type = ARG_CONST_SIZE,
  529. };
  530. static __always_inline u64
  531. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  532. u64 flags, struct perf_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. return perf_event_output(event, sd, regs);
  553. }
  554. /*
  555. * Support executing tracepoints in normal, irq, and nmi context that each call
  556. * bpf_perf_event_output
  557. */
  558. struct bpf_trace_sample_data {
  559. struct perf_sample_data sds[3];
  560. };
  561. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
  562. static DEFINE_PER_CPU(int, bpf_trace_nest_level);
  563. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  564. u64, flags, void *, data, u64, size)
  565. {
  566. struct bpf_trace_sample_data *sds;
  567. struct perf_raw_record raw = {
  568. .frag = {
  569. .size = size,
  570. .data = data,
  571. },
  572. };
  573. struct perf_sample_data *sd;
  574. int nest_level, err;
  575. preempt_disable();
  576. sds = this_cpu_ptr(&bpf_trace_sds);
  577. nest_level = this_cpu_inc_return(bpf_trace_nest_level);
  578. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
  579. err = -EBUSY;
  580. goto out;
  581. }
  582. sd = &sds->sds[nest_level - 1];
  583. if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
  584. err = -EINVAL;
  585. goto out;
  586. }
  587. perf_sample_data_init(sd, 0, 0);
  588. sd->raw = &raw;
  589. sd->sample_flags |= PERF_SAMPLE_RAW;
  590. err = __bpf_perf_event_output(regs, map, flags, sd);
  591. out:
  592. this_cpu_dec(bpf_trace_nest_level);
  593. preempt_enable();
  594. return err;
  595. }
  596. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  597. .func = bpf_perf_event_output,
  598. .gpl_only = true,
  599. .ret_type = RET_INTEGER,
  600. .arg1_type = ARG_PTR_TO_CTX,
  601. .arg2_type = ARG_CONST_MAP_PTR,
  602. .arg3_type = ARG_ANYTHING,
  603. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  604. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  605. };
  606. static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
  607. struct bpf_nested_pt_regs {
  608. struct pt_regs regs[3];
  609. };
  610. static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
  611. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
  612. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  613. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  614. {
  615. struct perf_raw_frag frag = {
  616. .copy = ctx_copy,
  617. .size = ctx_size,
  618. .data = ctx,
  619. };
  620. struct perf_raw_record raw = {
  621. .frag = {
  622. {
  623. .next = ctx_size ? &frag : NULL,
  624. },
  625. .size = meta_size,
  626. .data = meta,
  627. },
  628. };
  629. struct perf_sample_data *sd;
  630. struct pt_regs *regs;
  631. int nest_level;
  632. u64 ret;
  633. preempt_disable();
  634. nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
  635. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
  636. ret = -EBUSY;
  637. goto out;
  638. }
  639. sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
  640. regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
  641. perf_fetch_caller_regs(regs);
  642. perf_sample_data_init(sd, 0, 0);
  643. sd->raw = &raw;
  644. sd->sample_flags |= PERF_SAMPLE_RAW;
  645. ret = __bpf_perf_event_output(regs, map, flags, sd);
  646. out:
  647. this_cpu_dec(bpf_event_output_nest_level);
  648. preempt_enable();
  649. return ret;
  650. }
  651. BPF_CALL_0(bpf_get_current_task)
  652. {
  653. return (long) current;
  654. }
  655. const struct bpf_func_proto bpf_get_current_task_proto = {
  656. .func = bpf_get_current_task,
  657. .gpl_only = true,
  658. .ret_type = RET_INTEGER,
  659. };
  660. BPF_CALL_0(bpf_get_current_task_btf)
  661. {
  662. return (unsigned long) current;
  663. }
  664. const struct bpf_func_proto bpf_get_current_task_btf_proto = {
  665. .func = bpf_get_current_task_btf,
  666. .gpl_only = true,
  667. .ret_type = RET_PTR_TO_BTF_ID,
  668. .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  669. };
  670. BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
  671. {
  672. return (unsigned long) task_pt_regs(task);
  673. }
  674. BTF_ID_LIST(bpf_task_pt_regs_ids)
  675. BTF_ID(struct, pt_regs)
  676. const struct bpf_func_proto bpf_task_pt_regs_proto = {
  677. .func = bpf_task_pt_regs,
  678. .gpl_only = true,
  679. .arg1_type = ARG_PTR_TO_BTF_ID,
  680. .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  681. .ret_type = RET_PTR_TO_BTF_ID,
  682. .ret_btf_id = &bpf_task_pt_regs_ids[0],
  683. };
  684. BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
  685. {
  686. struct bpf_array *array = container_of(map, struct bpf_array, map);
  687. struct cgroup *cgrp;
  688. if (unlikely(idx >= array->map.max_entries))
  689. return -E2BIG;
  690. cgrp = READ_ONCE(array->ptrs[idx]);
  691. if (unlikely(!cgrp))
  692. return -EAGAIN;
  693. return task_under_cgroup_hierarchy(current, cgrp);
  694. }
  695. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  696. .func = bpf_current_task_under_cgroup,
  697. .gpl_only = false,
  698. .ret_type = RET_INTEGER,
  699. .arg1_type = ARG_CONST_MAP_PTR,
  700. .arg2_type = ARG_ANYTHING,
  701. };
  702. struct send_signal_irq_work {
  703. struct irq_work irq_work;
  704. struct task_struct *task;
  705. u32 sig;
  706. enum pid_type type;
  707. };
  708. static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
  709. static void do_bpf_send_signal(struct irq_work *entry)
  710. {
  711. struct send_signal_irq_work *work;
  712. work = container_of(entry, struct send_signal_irq_work, irq_work);
  713. group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
  714. put_task_struct(work->task);
  715. }
  716. static int bpf_send_signal_common(u32 sig, enum pid_type type)
  717. {
  718. struct send_signal_irq_work *work = NULL;
  719. /* Similar to bpf_probe_write_user, task needs to be
  720. * in a sound condition and kernel memory access be
  721. * permitted in order to send signal to the current
  722. * task.
  723. */
  724. if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
  725. return -EPERM;
  726. if (unlikely(!nmi_uaccess_okay()))
  727. return -EPERM;
  728. /* Task should not be pid=1 to avoid kernel panic. */
  729. if (unlikely(is_global_init(current)))
  730. return -EPERM;
  731. if (irqs_disabled()) {
  732. /* Do an early check on signal validity. Otherwise,
  733. * the error is lost in deferred irq_work.
  734. */
  735. if (unlikely(!valid_signal(sig)))
  736. return -EINVAL;
  737. work = this_cpu_ptr(&send_signal_work);
  738. if (irq_work_is_busy(&work->irq_work))
  739. return -EBUSY;
  740. /* Add the current task, which is the target of sending signal,
  741. * to the irq_work. The current task may change when queued
  742. * irq works get executed.
  743. */
  744. work->task = get_task_struct(current);
  745. work->sig = sig;
  746. work->type = type;
  747. irq_work_queue(&work->irq_work);
  748. return 0;
  749. }
  750. return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
  751. }
  752. BPF_CALL_1(bpf_send_signal, u32, sig)
  753. {
  754. return bpf_send_signal_common(sig, PIDTYPE_TGID);
  755. }
  756. static const struct bpf_func_proto bpf_send_signal_proto = {
  757. .func = bpf_send_signal,
  758. .gpl_only = false,
  759. .ret_type = RET_INTEGER,
  760. .arg1_type = ARG_ANYTHING,
  761. };
  762. BPF_CALL_1(bpf_send_signal_thread, u32, sig)
  763. {
  764. return bpf_send_signal_common(sig, PIDTYPE_PID);
  765. }
  766. static const struct bpf_func_proto bpf_send_signal_thread_proto = {
  767. .func = bpf_send_signal_thread,
  768. .gpl_only = false,
  769. .ret_type = RET_INTEGER,
  770. .arg1_type = ARG_ANYTHING,
  771. };
  772. BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
  773. {
  774. struct path copy;
  775. long len;
  776. char *p;
  777. if (!sz)
  778. return 0;
  779. /*
  780. * The path pointer is verified as trusted and safe to use,
  781. * but let's double check it's valid anyway to workaround
  782. * potentially broken verifier.
  783. */
  784. len = copy_from_kernel_nofault(&copy, path, sizeof(*path));
  785. if (len < 0)
  786. return len;
  787. p = d_path(&copy, buf, sz);
  788. if (IS_ERR(p)) {
  789. len = PTR_ERR(p);
  790. } else {
  791. len = buf + sz - p;
  792. memmove(buf, p, len);
  793. }
  794. return len;
  795. }
  796. BTF_SET_START(btf_allowlist_d_path)
  797. #ifdef CONFIG_SECURITY
  798. BTF_ID(func, security_file_permission)
  799. BTF_ID(func, security_inode_getattr)
  800. BTF_ID(func, security_file_open)
  801. #endif
  802. #ifdef CONFIG_SECURITY_PATH
  803. BTF_ID(func, security_path_truncate)
  804. #endif
  805. BTF_ID(func, vfs_truncate)
  806. BTF_ID(func, vfs_fallocate)
  807. BTF_ID(func, dentry_open)
  808. BTF_ID(func, vfs_getattr)
  809. BTF_ID(func, filp_close)
  810. BTF_SET_END(btf_allowlist_d_path)
  811. static bool bpf_d_path_allowed(const struct bpf_prog *prog)
  812. {
  813. if (prog->type == BPF_PROG_TYPE_TRACING &&
  814. prog->expected_attach_type == BPF_TRACE_ITER)
  815. return true;
  816. if (prog->type == BPF_PROG_TYPE_LSM)
  817. return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
  818. return btf_id_set_contains(&btf_allowlist_d_path,
  819. prog->aux->attach_btf_id);
  820. }
  821. BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
  822. static const struct bpf_func_proto bpf_d_path_proto = {
  823. .func = bpf_d_path,
  824. .gpl_only = false,
  825. .ret_type = RET_INTEGER,
  826. .arg1_type = ARG_PTR_TO_BTF_ID,
  827. .arg1_btf_id = &bpf_d_path_btf_ids[0],
  828. .arg2_type = ARG_PTR_TO_MEM,
  829. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  830. .allowed = bpf_d_path_allowed,
  831. };
  832. #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
  833. BTF_F_PTR_RAW | BTF_F_ZERO)
  834. static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
  835. u64 flags, const struct btf **btf,
  836. s32 *btf_id)
  837. {
  838. const struct btf_type *t;
  839. if (unlikely(flags & ~(BTF_F_ALL)))
  840. return -EINVAL;
  841. if (btf_ptr_size != sizeof(struct btf_ptr))
  842. return -EINVAL;
  843. *btf = bpf_get_btf_vmlinux();
  844. if (IS_ERR_OR_NULL(*btf))
  845. return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
  846. if (ptr->type_id > 0)
  847. *btf_id = ptr->type_id;
  848. else
  849. return -EINVAL;
  850. if (*btf_id > 0)
  851. t = btf_type_by_id(*btf, *btf_id);
  852. if (*btf_id <= 0 || !t)
  853. return -ENOENT;
  854. return 0;
  855. }
  856. BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
  857. u32, btf_ptr_size, u64, flags)
  858. {
  859. const struct btf *btf;
  860. s32 btf_id;
  861. int ret;
  862. ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
  863. if (ret)
  864. return ret;
  865. return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
  866. flags);
  867. }
  868. const struct bpf_func_proto bpf_snprintf_btf_proto = {
  869. .func = bpf_snprintf_btf,
  870. .gpl_only = false,
  871. .ret_type = RET_INTEGER,
  872. .arg1_type = ARG_PTR_TO_MEM,
  873. .arg2_type = ARG_CONST_SIZE,
  874. .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  875. .arg4_type = ARG_CONST_SIZE,
  876. .arg5_type = ARG_ANYTHING,
  877. };
  878. BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
  879. {
  880. /* This helper call is inlined by verifier. */
  881. return ((u64 *)ctx)[-2];
  882. }
  883. static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
  884. .func = bpf_get_func_ip_tracing,
  885. .gpl_only = true,
  886. .ret_type = RET_INTEGER,
  887. .arg1_type = ARG_PTR_TO_CTX,
  888. };
  889. #ifdef CONFIG_X86_KERNEL_IBT
  890. static unsigned long get_entry_ip(unsigned long fentry_ip)
  891. {
  892. u32 instr;
  893. /* Being extra safe in here in case entry ip is on the page-edge. */
  894. if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
  895. return fentry_ip;
  896. if (is_endbr(instr))
  897. fentry_ip -= ENDBR_INSN_SIZE;
  898. return fentry_ip;
  899. }
  900. #else
  901. #define get_entry_ip(fentry_ip) fentry_ip
  902. #endif
  903. BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
  904. {
  905. struct kprobe *kp = kprobe_running();
  906. if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
  907. return 0;
  908. return get_entry_ip((uintptr_t)kp->addr);
  909. }
  910. static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
  911. .func = bpf_get_func_ip_kprobe,
  912. .gpl_only = true,
  913. .ret_type = RET_INTEGER,
  914. .arg1_type = ARG_PTR_TO_CTX,
  915. };
  916. BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
  917. {
  918. return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
  919. }
  920. static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
  921. .func = bpf_get_func_ip_kprobe_multi,
  922. .gpl_only = false,
  923. .ret_type = RET_INTEGER,
  924. .arg1_type = ARG_PTR_TO_CTX,
  925. };
  926. BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
  927. {
  928. return bpf_kprobe_multi_cookie(current->bpf_ctx);
  929. }
  930. static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
  931. .func = bpf_get_attach_cookie_kprobe_multi,
  932. .gpl_only = false,
  933. .ret_type = RET_INTEGER,
  934. .arg1_type = ARG_PTR_TO_CTX,
  935. };
  936. BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
  937. {
  938. struct bpf_trace_run_ctx *run_ctx;
  939. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  940. return run_ctx->bpf_cookie;
  941. }
  942. static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
  943. .func = bpf_get_attach_cookie_trace,
  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_pe, struct bpf_perf_event_data_kern *, ctx)
  949. {
  950. return ctx->event->bpf_cookie;
  951. }
  952. static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
  953. .func = bpf_get_attach_cookie_pe,
  954. .gpl_only = false,
  955. .ret_type = RET_INTEGER,
  956. .arg1_type = ARG_PTR_TO_CTX,
  957. };
  958. BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
  959. {
  960. struct bpf_trace_run_ctx *run_ctx;
  961. run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
  962. return run_ctx->bpf_cookie;
  963. }
  964. static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
  965. .func = bpf_get_attach_cookie_tracing,
  966. .gpl_only = false,
  967. .ret_type = RET_INTEGER,
  968. .arg1_type = ARG_PTR_TO_CTX,
  969. };
  970. BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
  971. {
  972. #ifndef CONFIG_X86
  973. return -ENOENT;
  974. #else
  975. static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  976. u32 entry_cnt = size / br_entry_size;
  977. entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
  978. if (unlikely(flags))
  979. return -EINVAL;
  980. if (!entry_cnt)
  981. return -ENOENT;
  982. return entry_cnt * br_entry_size;
  983. #endif
  984. }
  985. static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
  986. .func = bpf_get_branch_snapshot,
  987. .gpl_only = true,
  988. .ret_type = RET_INTEGER,
  989. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  990. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  991. };
  992. BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
  993. {
  994. /* This helper call is inlined by verifier. */
  995. u64 nr_args = ((u64 *)ctx)[-1];
  996. if ((u64) n >= nr_args)
  997. return -EINVAL;
  998. *value = ((u64 *)ctx)[n];
  999. return 0;
  1000. }
  1001. static const struct bpf_func_proto bpf_get_func_arg_proto = {
  1002. .func = get_func_arg,
  1003. .ret_type = RET_INTEGER,
  1004. .arg1_type = ARG_PTR_TO_CTX,
  1005. .arg2_type = ARG_ANYTHING,
  1006. .arg3_type = ARG_PTR_TO_LONG,
  1007. };
  1008. BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
  1009. {
  1010. /* This helper call is inlined by verifier. */
  1011. u64 nr_args = ((u64 *)ctx)[-1];
  1012. *value = ((u64 *)ctx)[nr_args];
  1013. return 0;
  1014. }
  1015. static const struct bpf_func_proto bpf_get_func_ret_proto = {
  1016. .func = get_func_ret,
  1017. .ret_type = RET_INTEGER,
  1018. .arg1_type = ARG_PTR_TO_CTX,
  1019. .arg2_type = ARG_PTR_TO_LONG,
  1020. };
  1021. BPF_CALL_1(get_func_arg_cnt, void *, ctx)
  1022. {
  1023. /* This helper call is inlined by verifier. */
  1024. return ((u64 *)ctx)[-1];
  1025. }
  1026. static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
  1027. .func = get_func_arg_cnt,
  1028. .ret_type = RET_INTEGER,
  1029. .arg1_type = ARG_PTR_TO_CTX,
  1030. };
  1031. #ifdef CONFIG_KEYS
  1032. __diag_push();
  1033. __diag_ignore_all("-Wmissing-prototypes",
  1034. "kfuncs which will be used in BPF programs");
  1035. /**
  1036. * bpf_lookup_user_key - lookup a key by its serial
  1037. * @serial: key handle serial number
  1038. * @flags: lookup-specific flags
  1039. *
  1040. * Search a key with a given *serial* and the provided *flags*.
  1041. * If found, increment the reference count of the key by one, and
  1042. * return it in the bpf_key structure.
  1043. *
  1044. * The bpf_key structure must be passed to bpf_key_put() when done
  1045. * with it, so that the key reference count is decremented and the
  1046. * bpf_key structure is freed.
  1047. *
  1048. * Permission checks are deferred to the time the key is used by
  1049. * one of the available key-specific kfuncs.
  1050. *
  1051. * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
  1052. * special keyring (e.g. session keyring), if it doesn't yet exist.
  1053. * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
  1054. * for the key construction, and to retrieve uninstantiated keys (keys
  1055. * without data attached to them).
  1056. *
  1057. * Return: a bpf_key pointer with a valid key pointer if the key is found, a
  1058. * NULL pointer otherwise.
  1059. */
  1060. struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
  1061. {
  1062. key_ref_t key_ref;
  1063. struct bpf_key *bkey;
  1064. if (flags & ~KEY_LOOKUP_ALL)
  1065. return NULL;
  1066. /*
  1067. * Permission check is deferred until the key is used, as the
  1068. * intent of the caller is unknown here.
  1069. */
  1070. key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
  1071. if (IS_ERR(key_ref))
  1072. return NULL;
  1073. bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
  1074. if (!bkey) {
  1075. key_put(key_ref_to_ptr(key_ref));
  1076. return NULL;
  1077. }
  1078. bkey->key = key_ref_to_ptr(key_ref);
  1079. bkey->has_ref = true;
  1080. return bkey;
  1081. }
  1082. /**
  1083. * bpf_lookup_system_key - lookup a key by a system-defined ID
  1084. * @id: key ID
  1085. *
  1086. * Obtain a bpf_key structure with a key pointer set to the passed key ID.
  1087. * The key pointer is marked as invalid, to prevent bpf_key_put() from
  1088. * attempting to decrement the key reference count on that pointer. The key
  1089. * pointer set in such way is currently understood only by
  1090. * verify_pkcs7_signature().
  1091. *
  1092. * Set *id* to one of the values defined in include/linux/verification.h:
  1093. * 0 for the primary keyring (immutable keyring of system keys);
  1094. * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
  1095. * (where keys can be added only if they are vouched for by existing keys
  1096. * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
  1097. * keyring (primarily used by the integrity subsystem to verify a kexec'ed
  1098. * kerned image and, possibly, the initramfs signature).
  1099. *
  1100. * Return: a bpf_key pointer with an invalid key pointer set from the
  1101. * pre-determined ID on success, a NULL pointer otherwise
  1102. */
  1103. struct bpf_key *bpf_lookup_system_key(u64 id)
  1104. {
  1105. struct bpf_key *bkey;
  1106. if (system_keyring_id_check(id) < 0)
  1107. return NULL;
  1108. bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
  1109. if (!bkey)
  1110. return NULL;
  1111. bkey->key = (struct key *)(unsigned long)id;
  1112. bkey->has_ref = false;
  1113. return bkey;
  1114. }
  1115. /**
  1116. * bpf_key_put - decrement key reference count if key is valid and free bpf_key
  1117. * @bkey: bpf_key structure
  1118. *
  1119. * Decrement the reference count of the key inside *bkey*, if the pointer
  1120. * is valid, and free *bkey*.
  1121. */
  1122. void bpf_key_put(struct bpf_key *bkey)
  1123. {
  1124. if (bkey->has_ref)
  1125. key_put(bkey->key);
  1126. kfree(bkey);
  1127. }
  1128. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  1129. /**
  1130. * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
  1131. * @data_ptr: data to verify
  1132. * @sig_ptr: signature of the data
  1133. * @trusted_keyring: keyring with keys trusted for signature verification
  1134. *
  1135. * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
  1136. * with keys in a keyring referenced by *trusted_keyring*.
  1137. *
  1138. * Return: 0 on success, a negative value on error.
  1139. */
  1140. int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
  1141. struct bpf_dynptr_kern *sig_ptr,
  1142. struct bpf_key *trusted_keyring)
  1143. {
  1144. int ret;
  1145. if (trusted_keyring->has_ref) {
  1146. /*
  1147. * Do the permission check deferred in bpf_lookup_user_key().
  1148. * See bpf_lookup_user_key() for more details.
  1149. *
  1150. * A call to key_task_permission() here would be redundant, as
  1151. * it is already done by keyring_search() called by
  1152. * find_asymmetric_key().
  1153. */
  1154. ret = key_validate(trusted_keyring->key);
  1155. if (ret < 0)
  1156. return ret;
  1157. }
  1158. return verify_pkcs7_signature(data_ptr->data,
  1159. bpf_dynptr_get_size(data_ptr),
  1160. sig_ptr->data,
  1161. bpf_dynptr_get_size(sig_ptr),
  1162. trusted_keyring->key,
  1163. VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
  1164. NULL);
  1165. }
  1166. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
  1167. __diag_pop();
  1168. BTF_SET8_START(key_sig_kfunc_set)
  1169. BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
  1170. BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
  1171. BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
  1172. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  1173. BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
  1174. #endif
  1175. BTF_SET8_END(key_sig_kfunc_set)
  1176. static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
  1177. .owner = THIS_MODULE,
  1178. .set = &key_sig_kfunc_set,
  1179. };
  1180. static int __init bpf_key_sig_kfuncs_init(void)
  1181. {
  1182. return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
  1183. &bpf_key_sig_kfunc_set);
  1184. }
  1185. late_initcall(bpf_key_sig_kfuncs_init);
  1186. #endif /* CONFIG_KEYS */
  1187. static const struct bpf_func_proto *
  1188. bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1189. {
  1190. switch (func_id) {
  1191. case BPF_FUNC_map_lookup_elem:
  1192. return &bpf_map_lookup_elem_proto;
  1193. case BPF_FUNC_map_update_elem:
  1194. return &bpf_map_update_elem_proto;
  1195. case BPF_FUNC_map_delete_elem:
  1196. return &bpf_map_delete_elem_proto;
  1197. case BPF_FUNC_map_push_elem:
  1198. return &bpf_map_push_elem_proto;
  1199. case BPF_FUNC_map_pop_elem:
  1200. return &bpf_map_pop_elem_proto;
  1201. case BPF_FUNC_map_peek_elem:
  1202. return &bpf_map_peek_elem_proto;
  1203. case BPF_FUNC_map_lookup_percpu_elem:
  1204. return &bpf_map_lookup_percpu_elem_proto;
  1205. case BPF_FUNC_ktime_get_ns:
  1206. return &bpf_ktime_get_ns_proto;
  1207. case BPF_FUNC_ktime_get_boot_ns:
  1208. return &bpf_ktime_get_boot_ns_proto;
  1209. case BPF_FUNC_tail_call:
  1210. return &bpf_tail_call_proto;
  1211. case BPF_FUNC_get_current_pid_tgid:
  1212. return &bpf_get_current_pid_tgid_proto;
  1213. case BPF_FUNC_get_current_task:
  1214. return &bpf_get_current_task_proto;
  1215. case BPF_FUNC_get_current_task_btf:
  1216. return &bpf_get_current_task_btf_proto;
  1217. case BPF_FUNC_task_pt_regs:
  1218. return &bpf_task_pt_regs_proto;
  1219. case BPF_FUNC_get_current_uid_gid:
  1220. return &bpf_get_current_uid_gid_proto;
  1221. case BPF_FUNC_get_current_comm:
  1222. return &bpf_get_current_comm_proto;
  1223. case BPF_FUNC_trace_printk:
  1224. return bpf_get_trace_printk_proto();
  1225. case BPF_FUNC_get_smp_processor_id:
  1226. return &bpf_get_smp_processor_id_proto;
  1227. case BPF_FUNC_get_numa_node_id:
  1228. return &bpf_get_numa_node_id_proto;
  1229. case BPF_FUNC_perf_event_read:
  1230. return &bpf_perf_event_read_proto;
  1231. case BPF_FUNC_current_task_under_cgroup:
  1232. return &bpf_current_task_under_cgroup_proto;
  1233. case BPF_FUNC_get_prandom_u32:
  1234. return &bpf_get_prandom_u32_proto;
  1235. case BPF_FUNC_probe_write_user:
  1236. return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
  1237. NULL : bpf_get_probe_write_proto();
  1238. case BPF_FUNC_probe_read_user:
  1239. return &bpf_probe_read_user_proto;
  1240. case BPF_FUNC_probe_read_kernel:
  1241. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1242. NULL : &bpf_probe_read_kernel_proto;
  1243. case BPF_FUNC_probe_read_user_str:
  1244. return &bpf_probe_read_user_str_proto;
  1245. case BPF_FUNC_probe_read_kernel_str:
  1246. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1247. NULL : &bpf_probe_read_kernel_str_proto;
  1248. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  1249. case BPF_FUNC_probe_read:
  1250. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1251. NULL : &bpf_probe_read_compat_proto;
  1252. case BPF_FUNC_probe_read_str:
  1253. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1254. NULL : &bpf_probe_read_compat_str_proto;
  1255. #endif
  1256. #ifdef CONFIG_CGROUPS
  1257. case BPF_FUNC_get_current_cgroup_id:
  1258. return &bpf_get_current_cgroup_id_proto;
  1259. case BPF_FUNC_get_current_ancestor_cgroup_id:
  1260. return &bpf_get_current_ancestor_cgroup_id_proto;
  1261. #endif
  1262. case BPF_FUNC_send_signal:
  1263. return &bpf_send_signal_proto;
  1264. case BPF_FUNC_send_signal_thread:
  1265. return &bpf_send_signal_thread_proto;
  1266. case BPF_FUNC_perf_event_read_value:
  1267. return &bpf_perf_event_read_value_proto;
  1268. case BPF_FUNC_get_ns_current_pid_tgid:
  1269. return &bpf_get_ns_current_pid_tgid_proto;
  1270. case BPF_FUNC_ringbuf_output:
  1271. return &bpf_ringbuf_output_proto;
  1272. case BPF_FUNC_ringbuf_reserve:
  1273. return &bpf_ringbuf_reserve_proto;
  1274. case BPF_FUNC_ringbuf_submit:
  1275. return &bpf_ringbuf_submit_proto;
  1276. case BPF_FUNC_ringbuf_discard:
  1277. return &bpf_ringbuf_discard_proto;
  1278. case BPF_FUNC_ringbuf_query:
  1279. return &bpf_ringbuf_query_proto;
  1280. case BPF_FUNC_jiffies64:
  1281. return &bpf_jiffies64_proto;
  1282. case BPF_FUNC_get_task_stack:
  1283. return &bpf_get_task_stack_proto;
  1284. case BPF_FUNC_copy_from_user:
  1285. return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
  1286. case BPF_FUNC_copy_from_user_task:
  1287. return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
  1288. case BPF_FUNC_snprintf_btf:
  1289. return &bpf_snprintf_btf_proto;
  1290. case BPF_FUNC_per_cpu_ptr:
  1291. return &bpf_per_cpu_ptr_proto;
  1292. case BPF_FUNC_this_cpu_ptr:
  1293. return &bpf_this_cpu_ptr_proto;
  1294. case BPF_FUNC_task_storage_get:
  1295. return &bpf_task_storage_get_proto;
  1296. case BPF_FUNC_task_storage_delete:
  1297. return &bpf_task_storage_delete_proto;
  1298. case BPF_FUNC_for_each_map_elem:
  1299. return &bpf_for_each_map_elem_proto;
  1300. case BPF_FUNC_snprintf:
  1301. return &bpf_snprintf_proto;
  1302. case BPF_FUNC_get_func_ip:
  1303. return &bpf_get_func_ip_proto_tracing;
  1304. case BPF_FUNC_get_branch_snapshot:
  1305. return &bpf_get_branch_snapshot_proto;
  1306. case BPF_FUNC_find_vma:
  1307. return &bpf_find_vma_proto;
  1308. case BPF_FUNC_trace_vprintk:
  1309. return bpf_get_trace_vprintk_proto();
  1310. default:
  1311. return bpf_base_func_proto(func_id);
  1312. }
  1313. }
  1314. static const struct bpf_func_proto *
  1315. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1316. {
  1317. switch (func_id) {
  1318. case BPF_FUNC_perf_event_output:
  1319. return &bpf_perf_event_output_proto;
  1320. case BPF_FUNC_get_stackid:
  1321. return &bpf_get_stackid_proto;
  1322. case BPF_FUNC_get_stack:
  1323. return &bpf_get_stack_proto;
  1324. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  1325. case BPF_FUNC_override_return:
  1326. return &bpf_override_return_proto;
  1327. #endif
  1328. case BPF_FUNC_get_func_ip:
  1329. return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
  1330. &bpf_get_func_ip_proto_kprobe_multi :
  1331. &bpf_get_func_ip_proto_kprobe;
  1332. case BPF_FUNC_get_attach_cookie:
  1333. return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
  1334. &bpf_get_attach_cookie_proto_kmulti :
  1335. &bpf_get_attach_cookie_proto_trace;
  1336. default:
  1337. return bpf_tracing_func_proto(func_id, prog);
  1338. }
  1339. }
  1340. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  1341. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1342. const struct bpf_prog *prog,
  1343. struct bpf_insn_access_aux *info)
  1344. {
  1345. if (off < 0 || off >= sizeof(struct pt_regs))
  1346. return false;
  1347. if (type != BPF_READ)
  1348. return false;
  1349. if (off % size != 0)
  1350. return false;
  1351. /*
  1352. * Assertion for 32 bit to make sure last 8 byte access
  1353. * (BPF_DW) to the last 4 byte member is disallowed.
  1354. */
  1355. if (off + size > sizeof(struct pt_regs))
  1356. return false;
  1357. return true;
  1358. }
  1359. const struct bpf_verifier_ops kprobe_verifier_ops = {
  1360. .get_func_proto = kprobe_prog_func_proto,
  1361. .is_valid_access = kprobe_prog_is_valid_access,
  1362. };
  1363. const struct bpf_prog_ops kprobe_prog_ops = {
  1364. };
  1365. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  1366. u64, flags, void *, data, u64, size)
  1367. {
  1368. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1369. /*
  1370. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  1371. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  1372. * from there and call the same bpf_perf_event_output() helper inline.
  1373. */
  1374. return ____bpf_perf_event_output(regs, map, flags, data, size);
  1375. }
  1376. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  1377. .func = bpf_perf_event_output_tp,
  1378. .gpl_only = true,
  1379. .ret_type = RET_INTEGER,
  1380. .arg1_type = ARG_PTR_TO_CTX,
  1381. .arg2_type = ARG_CONST_MAP_PTR,
  1382. .arg3_type = ARG_ANYTHING,
  1383. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1384. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  1385. };
  1386. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  1387. u64, flags)
  1388. {
  1389. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1390. /*
  1391. * Same comment as in bpf_perf_event_output_tp(), only that this time
  1392. * the other helper's function body cannot be inlined due to being
  1393. * external, thus we need to call raw helper function.
  1394. */
  1395. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  1396. flags, 0, 0);
  1397. }
  1398. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  1399. .func = bpf_get_stackid_tp,
  1400. .gpl_only = true,
  1401. .ret_type = RET_INTEGER,
  1402. .arg1_type = ARG_PTR_TO_CTX,
  1403. .arg2_type = ARG_CONST_MAP_PTR,
  1404. .arg3_type = ARG_ANYTHING,
  1405. };
  1406. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  1407. u64, flags)
  1408. {
  1409. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  1410. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  1411. (unsigned long) size, flags, 0);
  1412. }
  1413. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  1414. .func = bpf_get_stack_tp,
  1415. .gpl_only = true,
  1416. .ret_type = RET_INTEGER,
  1417. .arg1_type = ARG_PTR_TO_CTX,
  1418. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1419. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1420. .arg4_type = ARG_ANYTHING,
  1421. };
  1422. static const struct bpf_func_proto *
  1423. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1424. {
  1425. switch (func_id) {
  1426. case BPF_FUNC_perf_event_output:
  1427. return &bpf_perf_event_output_proto_tp;
  1428. case BPF_FUNC_get_stackid:
  1429. return &bpf_get_stackid_proto_tp;
  1430. case BPF_FUNC_get_stack:
  1431. return &bpf_get_stack_proto_tp;
  1432. case BPF_FUNC_get_attach_cookie:
  1433. return &bpf_get_attach_cookie_proto_trace;
  1434. default:
  1435. return bpf_tracing_func_proto(func_id, prog);
  1436. }
  1437. }
  1438. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1439. const struct bpf_prog *prog,
  1440. struct bpf_insn_access_aux *info)
  1441. {
  1442. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  1443. return false;
  1444. if (type != BPF_READ)
  1445. return false;
  1446. if (off % size != 0)
  1447. return false;
  1448. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  1449. return true;
  1450. }
  1451. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  1452. .get_func_proto = tp_prog_func_proto,
  1453. .is_valid_access = tp_prog_is_valid_access,
  1454. };
  1455. const struct bpf_prog_ops tracepoint_prog_ops = {
  1456. };
  1457. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  1458. struct bpf_perf_event_value *, buf, u32, size)
  1459. {
  1460. int err = -EINVAL;
  1461. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  1462. goto clear;
  1463. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  1464. &buf->running);
  1465. if (unlikely(err))
  1466. goto clear;
  1467. return 0;
  1468. clear:
  1469. memset(buf, 0, size);
  1470. return err;
  1471. }
  1472. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  1473. .func = bpf_perf_prog_read_value,
  1474. .gpl_only = true,
  1475. .ret_type = RET_INTEGER,
  1476. .arg1_type = ARG_PTR_TO_CTX,
  1477. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1478. .arg3_type = ARG_CONST_SIZE,
  1479. };
  1480. BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
  1481. void *, buf, u32, size, u64, flags)
  1482. {
  1483. static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  1484. struct perf_branch_stack *br_stack = ctx->data->br_stack;
  1485. u32 to_copy;
  1486. if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
  1487. return -EINVAL;
  1488. if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
  1489. return -ENOENT;
  1490. if (unlikely(!br_stack))
  1491. return -ENOENT;
  1492. if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
  1493. return br_stack->nr * br_entry_size;
  1494. if (!buf || (size % br_entry_size != 0))
  1495. return -EINVAL;
  1496. to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
  1497. memcpy(buf, br_stack->entries, to_copy);
  1498. return to_copy;
  1499. }
  1500. static const struct bpf_func_proto bpf_read_branch_records_proto = {
  1501. .func = bpf_read_branch_records,
  1502. .gpl_only = true,
  1503. .ret_type = RET_INTEGER,
  1504. .arg1_type = ARG_PTR_TO_CTX,
  1505. .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
  1506. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1507. .arg4_type = ARG_ANYTHING,
  1508. };
  1509. static const struct bpf_func_proto *
  1510. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1511. {
  1512. switch (func_id) {
  1513. case BPF_FUNC_perf_event_output:
  1514. return &bpf_perf_event_output_proto_tp;
  1515. case BPF_FUNC_get_stackid:
  1516. return &bpf_get_stackid_proto_pe;
  1517. case BPF_FUNC_get_stack:
  1518. return &bpf_get_stack_proto_pe;
  1519. case BPF_FUNC_perf_prog_read_value:
  1520. return &bpf_perf_prog_read_value_proto;
  1521. case BPF_FUNC_read_branch_records:
  1522. return &bpf_read_branch_records_proto;
  1523. case BPF_FUNC_get_attach_cookie:
  1524. return &bpf_get_attach_cookie_proto_pe;
  1525. default:
  1526. return bpf_tracing_func_proto(func_id, prog);
  1527. }
  1528. }
  1529. /*
  1530. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  1531. * to avoid potential recursive reuse issue when/if tracepoints are added
  1532. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
  1533. *
  1534. * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
  1535. * in normal, irq, and nmi context.
  1536. */
  1537. struct bpf_raw_tp_regs {
  1538. struct pt_regs regs[3];
  1539. };
  1540. static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
  1541. static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
  1542. static struct pt_regs *get_bpf_raw_tp_regs(void)
  1543. {
  1544. struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
  1545. int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
  1546. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
  1547. this_cpu_dec(bpf_raw_tp_nest_level);
  1548. return ERR_PTR(-EBUSY);
  1549. }
  1550. return &tp_regs->regs[nest_level - 1];
  1551. }
  1552. static void put_bpf_raw_tp_regs(void)
  1553. {
  1554. this_cpu_dec(bpf_raw_tp_nest_level);
  1555. }
  1556. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1557. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  1558. {
  1559. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1560. int ret;
  1561. if (IS_ERR(regs))
  1562. return PTR_ERR(regs);
  1563. perf_fetch_caller_regs(regs);
  1564. ret = ____bpf_perf_event_output(regs, map, flags, data, size);
  1565. put_bpf_raw_tp_regs();
  1566. return ret;
  1567. }
  1568. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  1569. .func = bpf_perf_event_output_raw_tp,
  1570. .gpl_only = true,
  1571. .ret_type = RET_INTEGER,
  1572. .arg1_type = ARG_PTR_TO_CTX,
  1573. .arg2_type = ARG_CONST_MAP_PTR,
  1574. .arg3_type = ARG_ANYTHING,
  1575. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1576. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  1577. };
  1578. extern const struct bpf_func_proto bpf_skb_output_proto;
  1579. extern const struct bpf_func_proto bpf_xdp_output_proto;
  1580. extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
  1581. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1582. struct bpf_map *, map, u64, flags)
  1583. {
  1584. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1585. int ret;
  1586. if (IS_ERR(regs))
  1587. return PTR_ERR(regs);
  1588. perf_fetch_caller_regs(regs);
  1589. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  1590. ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  1591. flags, 0, 0);
  1592. put_bpf_raw_tp_regs();
  1593. return ret;
  1594. }
  1595. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  1596. .func = bpf_get_stackid_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. };
  1603. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1604. void *, buf, u32, size, u64, flags)
  1605. {
  1606. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1607. int ret;
  1608. if (IS_ERR(regs))
  1609. return PTR_ERR(regs);
  1610. perf_fetch_caller_regs(regs);
  1611. ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  1612. (unsigned long) size, flags, 0);
  1613. put_bpf_raw_tp_regs();
  1614. return ret;
  1615. }
  1616. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  1617. .func = bpf_get_stack_raw_tp,
  1618. .gpl_only = true,
  1619. .ret_type = RET_INTEGER,
  1620. .arg1_type = ARG_PTR_TO_CTX,
  1621. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1622. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1623. .arg4_type = ARG_ANYTHING,
  1624. };
  1625. static const struct bpf_func_proto *
  1626. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1627. {
  1628. switch (func_id) {
  1629. case BPF_FUNC_perf_event_output:
  1630. return &bpf_perf_event_output_proto_raw_tp;
  1631. case BPF_FUNC_get_stackid:
  1632. return &bpf_get_stackid_proto_raw_tp;
  1633. case BPF_FUNC_get_stack:
  1634. return &bpf_get_stack_proto_raw_tp;
  1635. default:
  1636. return bpf_tracing_func_proto(func_id, prog);
  1637. }
  1638. }
  1639. const struct bpf_func_proto *
  1640. tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1641. {
  1642. const struct bpf_func_proto *fn;
  1643. switch (func_id) {
  1644. #ifdef CONFIG_NET
  1645. case BPF_FUNC_skb_output:
  1646. return &bpf_skb_output_proto;
  1647. case BPF_FUNC_xdp_output:
  1648. return &bpf_xdp_output_proto;
  1649. case BPF_FUNC_skc_to_tcp6_sock:
  1650. return &bpf_skc_to_tcp6_sock_proto;
  1651. case BPF_FUNC_skc_to_tcp_sock:
  1652. return &bpf_skc_to_tcp_sock_proto;
  1653. case BPF_FUNC_skc_to_tcp_timewait_sock:
  1654. return &bpf_skc_to_tcp_timewait_sock_proto;
  1655. case BPF_FUNC_skc_to_tcp_request_sock:
  1656. return &bpf_skc_to_tcp_request_sock_proto;
  1657. case BPF_FUNC_skc_to_udp6_sock:
  1658. return &bpf_skc_to_udp6_sock_proto;
  1659. case BPF_FUNC_skc_to_unix_sock:
  1660. return &bpf_skc_to_unix_sock_proto;
  1661. case BPF_FUNC_skc_to_mptcp_sock:
  1662. return &bpf_skc_to_mptcp_sock_proto;
  1663. case BPF_FUNC_sk_storage_get:
  1664. return &bpf_sk_storage_get_tracing_proto;
  1665. case BPF_FUNC_sk_storage_delete:
  1666. return &bpf_sk_storage_delete_tracing_proto;
  1667. case BPF_FUNC_sock_from_file:
  1668. return &bpf_sock_from_file_proto;
  1669. case BPF_FUNC_get_socket_cookie:
  1670. return &bpf_get_socket_ptr_cookie_proto;
  1671. case BPF_FUNC_xdp_get_buff_len:
  1672. return &bpf_xdp_get_buff_len_trace_proto;
  1673. #endif
  1674. case BPF_FUNC_seq_printf:
  1675. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1676. &bpf_seq_printf_proto :
  1677. NULL;
  1678. case BPF_FUNC_seq_write:
  1679. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1680. &bpf_seq_write_proto :
  1681. NULL;
  1682. case BPF_FUNC_seq_printf_btf:
  1683. return prog->expected_attach_type == BPF_TRACE_ITER ?
  1684. &bpf_seq_printf_btf_proto :
  1685. NULL;
  1686. case BPF_FUNC_d_path:
  1687. return &bpf_d_path_proto;
  1688. case BPF_FUNC_get_func_arg:
  1689. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
  1690. case BPF_FUNC_get_func_ret:
  1691. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
  1692. case BPF_FUNC_get_func_arg_cnt:
  1693. return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
  1694. case BPF_FUNC_get_attach_cookie:
  1695. return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
  1696. default:
  1697. fn = raw_tp_prog_func_proto(func_id, prog);
  1698. if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
  1699. fn = bpf_iter_get_func_proto(func_id, prog);
  1700. return fn;
  1701. }
  1702. }
  1703. static bool raw_tp_prog_is_valid_access(int off, int size,
  1704. enum bpf_access_type type,
  1705. const struct bpf_prog *prog,
  1706. struct bpf_insn_access_aux *info)
  1707. {
  1708. return bpf_tracing_ctx_access(off, size, type);
  1709. }
  1710. static bool tracing_prog_is_valid_access(int off, int size,
  1711. enum bpf_access_type type,
  1712. const struct bpf_prog *prog,
  1713. struct bpf_insn_access_aux *info)
  1714. {
  1715. return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
  1716. }
  1717. int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
  1718. const union bpf_attr *kattr,
  1719. union bpf_attr __user *uattr)
  1720. {
  1721. return -ENOTSUPP;
  1722. }
  1723. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  1724. .get_func_proto = raw_tp_prog_func_proto,
  1725. .is_valid_access = raw_tp_prog_is_valid_access,
  1726. };
  1727. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  1728. #ifdef CONFIG_NET
  1729. .test_run = bpf_prog_test_run_raw_tp,
  1730. #endif
  1731. };
  1732. const struct bpf_verifier_ops tracing_verifier_ops = {
  1733. .get_func_proto = tracing_prog_func_proto,
  1734. .is_valid_access = tracing_prog_is_valid_access,
  1735. };
  1736. const struct bpf_prog_ops tracing_prog_ops = {
  1737. .test_run = bpf_prog_test_run_tracing,
  1738. };
  1739. static bool raw_tp_writable_prog_is_valid_access(int off, int size,
  1740. enum bpf_access_type type,
  1741. const struct bpf_prog *prog,
  1742. struct bpf_insn_access_aux *info)
  1743. {
  1744. if (off == 0) {
  1745. if (size != sizeof(u64) || type != BPF_READ)
  1746. return false;
  1747. info->reg_type = PTR_TO_TP_BUFFER;
  1748. }
  1749. return raw_tp_prog_is_valid_access(off, size, type, prog, info);
  1750. }
  1751. const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
  1752. .get_func_proto = raw_tp_prog_func_proto,
  1753. .is_valid_access = raw_tp_writable_prog_is_valid_access,
  1754. };
  1755. const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
  1756. };
  1757. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1758. const struct bpf_prog *prog,
  1759. struct bpf_insn_access_aux *info)
  1760. {
  1761. const int size_u64 = sizeof(u64);
  1762. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  1763. return false;
  1764. if (type != BPF_READ)
  1765. return false;
  1766. if (off % size != 0) {
  1767. if (sizeof(unsigned long) != 4)
  1768. return false;
  1769. if (size != 8)
  1770. return false;
  1771. if (off % size != 4)
  1772. return false;
  1773. }
  1774. switch (off) {
  1775. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  1776. bpf_ctx_record_field_size(info, size_u64);
  1777. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1778. return false;
  1779. break;
  1780. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  1781. bpf_ctx_record_field_size(info, size_u64);
  1782. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1783. return false;
  1784. break;
  1785. default:
  1786. if (size != sizeof(long))
  1787. return false;
  1788. }
  1789. return true;
  1790. }
  1791. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  1792. const struct bpf_insn *si,
  1793. struct bpf_insn *insn_buf,
  1794. struct bpf_prog *prog, u32 *target_size)
  1795. {
  1796. struct bpf_insn *insn = insn_buf;
  1797. switch (si->off) {
  1798. case offsetof(struct bpf_perf_event_data, sample_period):
  1799. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1800. data), si->dst_reg, si->src_reg,
  1801. offsetof(struct bpf_perf_event_data_kern, data));
  1802. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1803. bpf_target_off(struct perf_sample_data, period, 8,
  1804. target_size));
  1805. break;
  1806. case offsetof(struct bpf_perf_event_data, addr):
  1807. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1808. data), si->dst_reg, si->src_reg,
  1809. offsetof(struct bpf_perf_event_data_kern, data));
  1810. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1811. bpf_target_off(struct perf_sample_data, addr, 8,
  1812. target_size));
  1813. break;
  1814. default:
  1815. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1816. regs), si->dst_reg, si->src_reg,
  1817. offsetof(struct bpf_perf_event_data_kern, regs));
  1818. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  1819. si->off);
  1820. break;
  1821. }
  1822. return insn - insn_buf;
  1823. }
  1824. const struct bpf_verifier_ops perf_event_verifier_ops = {
  1825. .get_func_proto = pe_prog_func_proto,
  1826. .is_valid_access = pe_prog_is_valid_access,
  1827. .convert_ctx_access = pe_prog_convert_ctx_access,
  1828. };
  1829. const struct bpf_prog_ops perf_event_prog_ops = {
  1830. };
  1831. static DEFINE_MUTEX(bpf_event_mutex);
  1832. #define BPF_TRACE_MAX_PROGS 64
  1833. int perf_event_attach_bpf_prog(struct perf_event *event,
  1834. struct bpf_prog *prog,
  1835. u64 bpf_cookie)
  1836. {
  1837. struct bpf_prog_array *old_array;
  1838. struct bpf_prog_array *new_array;
  1839. int ret = -EEXIST;
  1840. /*
  1841. * Kprobe override only works if they are on the function entry,
  1842. * and only if they are on the opt-in list.
  1843. */
  1844. if (prog->kprobe_override &&
  1845. (!trace_kprobe_on_func_entry(event->tp_event) ||
  1846. !trace_kprobe_error_injectable(event->tp_event)))
  1847. return -EINVAL;
  1848. mutex_lock(&bpf_event_mutex);
  1849. if (event->prog)
  1850. goto unlock;
  1851. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1852. if (old_array &&
  1853. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  1854. ret = -E2BIG;
  1855. goto unlock;
  1856. }
  1857. ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
  1858. if (ret < 0)
  1859. goto unlock;
  1860. /* set the new array to event->tp_event and set event->prog */
  1861. event->prog = prog;
  1862. event->bpf_cookie = bpf_cookie;
  1863. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1864. bpf_prog_array_free_sleepable(old_array);
  1865. unlock:
  1866. mutex_unlock(&bpf_event_mutex);
  1867. return ret;
  1868. }
  1869. void perf_event_detach_bpf_prog(struct perf_event *event)
  1870. {
  1871. struct bpf_prog_array *old_array;
  1872. struct bpf_prog_array *new_array;
  1873. int ret;
  1874. mutex_lock(&bpf_event_mutex);
  1875. if (!event->prog)
  1876. goto unlock;
  1877. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1878. ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
  1879. if (ret == -ENOENT)
  1880. goto unlock;
  1881. if (ret < 0) {
  1882. bpf_prog_array_delete_safe(old_array, event->prog);
  1883. } else {
  1884. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1885. bpf_prog_array_free_sleepable(old_array);
  1886. }
  1887. bpf_prog_put(event->prog);
  1888. event->prog = NULL;
  1889. unlock:
  1890. mutex_unlock(&bpf_event_mutex);
  1891. }
  1892. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  1893. {
  1894. struct perf_event_query_bpf __user *uquery = info;
  1895. struct perf_event_query_bpf query = {};
  1896. struct bpf_prog_array *progs;
  1897. u32 *ids, prog_cnt, ids_len;
  1898. int ret;
  1899. if (!perfmon_capable())
  1900. return -EPERM;
  1901. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  1902. return -EINVAL;
  1903. if (copy_from_user(&query, uquery, sizeof(query)))
  1904. return -EFAULT;
  1905. ids_len = query.ids_len;
  1906. if (ids_len > BPF_TRACE_MAX_PROGS)
  1907. return -E2BIG;
  1908. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  1909. if (!ids)
  1910. return -ENOMEM;
  1911. /*
  1912. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  1913. * is required when user only wants to check for uquery->prog_cnt.
  1914. * There is no need to check for it since the case is handled
  1915. * gracefully in bpf_prog_array_copy_info.
  1916. */
  1917. mutex_lock(&bpf_event_mutex);
  1918. progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1919. ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
  1920. mutex_unlock(&bpf_event_mutex);
  1921. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  1922. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  1923. ret = -EFAULT;
  1924. kfree(ids);
  1925. return ret;
  1926. }
  1927. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  1928. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  1929. struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
  1930. {
  1931. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  1932. for (; btp < __stop__bpf_raw_tp; btp++) {
  1933. if (!strcmp(btp->tp->name, name))
  1934. return btp;
  1935. }
  1936. return bpf_get_raw_tracepoint_module(name);
  1937. }
  1938. void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
  1939. {
  1940. struct module *mod;
  1941. preempt_disable();
  1942. mod = __module_address((unsigned long)btp);
  1943. module_put(mod);
  1944. preempt_enable();
  1945. }
  1946. static __always_inline
  1947. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  1948. {
  1949. cant_sleep();
  1950. if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
  1951. bpf_prog_inc_misses_counter(prog);
  1952. goto out;
  1953. }
  1954. rcu_read_lock();
  1955. (void) bpf_prog_run(prog, args);
  1956. rcu_read_unlock();
  1957. out:
  1958. this_cpu_dec(*(prog->active));
  1959. }
  1960. #define UNPACK(...) __VA_ARGS__
  1961. #define REPEAT_1(FN, DL, X, ...) FN(X)
  1962. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  1963. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  1964. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  1965. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  1966. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  1967. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  1968. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  1969. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  1970. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  1971. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  1972. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  1973. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  1974. #define SARG(X) u64 arg##X
  1975. #define COPY(X) args[X] = arg##X
  1976. #define __DL_COM (,)
  1977. #define __DL_SEM (;)
  1978. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  1979. #define BPF_TRACE_DEFN_x(x) \
  1980. void bpf_trace_run##x(struct bpf_prog *prog, \
  1981. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  1982. { \
  1983. u64 args[x]; \
  1984. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  1985. __bpf_trace_run(prog, args); \
  1986. } \
  1987. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  1988. BPF_TRACE_DEFN_x(1);
  1989. BPF_TRACE_DEFN_x(2);
  1990. BPF_TRACE_DEFN_x(3);
  1991. BPF_TRACE_DEFN_x(4);
  1992. BPF_TRACE_DEFN_x(5);
  1993. BPF_TRACE_DEFN_x(6);
  1994. BPF_TRACE_DEFN_x(7);
  1995. BPF_TRACE_DEFN_x(8);
  1996. BPF_TRACE_DEFN_x(9);
  1997. BPF_TRACE_DEFN_x(10);
  1998. BPF_TRACE_DEFN_x(11);
  1999. BPF_TRACE_DEFN_x(12);
  2000. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  2001. {
  2002. struct tracepoint *tp = btp->tp;
  2003. /*
  2004. * check that program doesn't access arguments beyond what's
  2005. * available in this tracepoint
  2006. */
  2007. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  2008. return -EINVAL;
  2009. if (prog->aux->max_tp_access > btp->writable_size)
  2010. return -EINVAL;
  2011. return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
  2012. prog);
  2013. }
  2014. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  2015. {
  2016. return __bpf_probe_register(btp, prog);
  2017. }
  2018. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  2019. {
  2020. return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  2021. }
  2022. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  2023. u32 *fd_type, const char **buf,
  2024. u64 *probe_offset, u64 *probe_addr)
  2025. {
  2026. bool is_tracepoint, is_syscall_tp;
  2027. struct bpf_prog *prog;
  2028. int flags, err = 0;
  2029. prog = event->prog;
  2030. if (!prog)
  2031. return -ENOENT;
  2032. /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
  2033. if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
  2034. return -EOPNOTSUPP;
  2035. *prog_id = prog->aux->id;
  2036. flags = event->tp_event->flags;
  2037. is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
  2038. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  2039. if (is_tracepoint || is_syscall_tp) {
  2040. *buf = is_tracepoint ? event->tp_event->tp->name
  2041. : event->tp_event->name;
  2042. *fd_type = BPF_FD_TYPE_TRACEPOINT;
  2043. *probe_offset = 0x0;
  2044. *probe_addr = 0x0;
  2045. } else {
  2046. /* kprobe/uprobe */
  2047. err = -EOPNOTSUPP;
  2048. #ifdef CONFIG_KPROBE_EVENTS
  2049. if (flags & TRACE_EVENT_FL_KPROBE)
  2050. err = bpf_get_kprobe_info(event, fd_type, buf,
  2051. probe_offset, probe_addr,
  2052. event->attr.type == PERF_TYPE_TRACEPOINT);
  2053. #endif
  2054. #ifdef CONFIG_UPROBE_EVENTS
  2055. if (flags & TRACE_EVENT_FL_UPROBE)
  2056. err = bpf_get_uprobe_info(event, fd_type, buf,
  2057. probe_offset, probe_addr,
  2058. event->attr.type == PERF_TYPE_TRACEPOINT);
  2059. #endif
  2060. }
  2061. return err;
  2062. }
  2063. static int __init send_signal_irq_work_init(void)
  2064. {
  2065. int cpu;
  2066. struct send_signal_irq_work *work;
  2067. for_each_possible_cpu(cpu) {
  2068. work = per_cpu_ptr(&send_signal_work, cpu);
  2069. init_irq_work(&work->irq_work, do_bpf_send_signal);
  2070. }
  2071. return 0;
  2072. }
  2073. subsys_initcall(send_signal_irq_work_init);
  2074. #ifdef CONFIG_MODULES
  2075. static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
  2076. void *module)
  2077. {
  2078. struct bpf_trace_module *btm, *tmp;
  2079. struct module *mod = module;
  2080. int ret = 0;
  2081. if (mod->num_bpf_raw_events == 0 ||
  2082. (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
  2083. goto out;
  2084. mutex_lock(&bpf_module_mutex);
  2085. switch (op) {
  2086. case MODULE_STATE_COMING:
  2087. btm = kzalloc(sizeof(*btm), GFP_KERNEL);
  2088. if (btm) {
  2089. btm->module = module;
  2090. list_add(&btm->list, &bpf_trace_modules);
  2091. } else {
  2092. ret = -ENOMEM;
  2093. }
  2094. break;
  2095. case MODULE_STATE_GOING:
  2096. list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
  2097. if (btm->module == module) {
  2098. list_del(&btm->list);
  2099. kfree(btm);
  2100. break;
  2101. }
  2102. }
  2103. break;
  2104. }
  2105. mutex_unlock(&bpf_module_mutex);
  2106. out:
  2107. return notifier_from_errno(ret);
  2108. }
  2109. static struct notifier_block bpf_module_nb = {
  2110. .notifier_call = bpf_event_notify,
  2111. };
  2112. static int __init bpf_event_init(void)
  2113. {
  2114. register_module_notifier(&bpf_module_nb);
  2115. return 0;
  2116. }
  2117. fs_initcall(bpf_event_init);
  2118. #endif /* CONFIG_MODULES */
  2119. #ifdef CONFIG_FPROBE
  2120. struct bpf_kprobe_multi_link {
  2121. struct bpf_link link;
  2122. struct fprobe fp;
  2123. unsigned long *addrs;
  2124. u64 *cookies;
  2125. u32 cnt;
  2126. };
  2127. struct bpf_kprobe_multi_run_ctx {
  2128. struct bpf_run_ctx run_ctx;
  2129. struct bpf_kprobe_multi_link *link;
  2130. unsigned long entry_ip;
  2131. };
  2132. struct user_syms {
  2133. const char **syms;
  2134. char *buf;
  2135. };
  2136. static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
  2137. {
  2138. unsigned long __user usymbol;
  2139. const char **syms = NULL;
  2140. char *buf = NULL, *p;
  2141. int err = -ENOMEM;
  2142. unsigned int i;
  2143. syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
  2144. if (!syms)
  2145. goto error;
  2146. buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
  2147. if (!buf)
  2148. goto error;
  2149. for (p = buf, i = 0; i < cnt; i++) {
  2150. if (__get_user(usymbol, usyms + i)) {
  2151. err = -EFAULT;
  2152. goto error;
  2153. }
  2154. err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
  2155. if (err == KSYM_NAME_LEN)
  2156. err = -E2BIG;
  2157. if (err < 0)
  2158. goto error;
  2159. syms[i] = p;
  2160. p += err + 1;
  2161. }
  2162. us->syms = syms;
  2163. us->buf = buf;
  2164. return 0;
  2165. error:
  2166. if (err) {
  2167. kvfree(syms);
  2168. kvfree(buf);
  2169. }
  2170. return err;
  2171. }
  2172. static void free_user_syms(struct user_syms *us)
  2173. {
  2174. kvfree(us->syms);
  2175. kvfree(us->buf);
  2176. }
  2177. static void bpf_kprobe_multi_link_release(struct bpf_link *link)
  2178. {
  2179. struct bpf_kprobe_multi_link *kmulti_link;
  2180. kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2181. unregister_fprobe(&kmulti_link->fp);
  2182. }
  2183. static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
  2184. {
  2185. struct bpf_kprobe_multi_link *kmulti_link;
  2186. kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2187. kvfree(kmulti_link->addrs);
  2188. kvfree(kmulti_link->cookies);
  2189. kfree(kmulti_link);
  2190. }
  2191. static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
  2192. .release = bpf_kprobe_multi_link_release,
  2193. .dealloc = bpf_kprobe_multi_link_dealloc,
  2194. };
  2195. static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
  2196. {
  2197. const struct bpf_kprobe_multi_link *link = priv;
  2198. unsigned long *addr_a = a, *addr_b = b;
  2199. u64 *cookie_a, *cookie_b;
  2200. cookie_a = link->cookies + (addr_a - link->addrs);
  2201. cookie_b = link->cookies + (addr_b - link->addrs);
  2202. /* swap addr_a/addr_b and cookie_a/cookie_b values */
  2203. swap(*addr_a, *addr_b);
  2204. swap(*cookie_a, *cookie_b);
  2205. }
  2206. static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b)
  2207. {
  2208. const unsigned long *addr_a = a, *addr_b = b;
  2209. if (*addr_a == *addr_b)
  2210. return 0;
  2211. return *addr_a < *addr_b ? -1 : 1;
  2212. }
  2213. static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
  2214. {
  2215. return __bpf_kprobe_multi_cookie_cmp(a, b);
  2216. }
  2217. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2218. {
  2219. struct bpf_kprobe_multi_run_ctx *run_ctx;
  2220. struct bpf_kprobe_multi_link *link;
  2221. u64 *cookie, entry_ip;
  2222. unsigned long *addr;
  2223. if (WARN_ON_ONCE(!ctx))
  2224. return 0;
  2225. run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
  2226. link = run_ctx->link;
  2227. if (!link->cookies)
  2228. return 0;
  2229. entry_ip = run_ctx->entry_ip;
  2230. addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
  2231. __bpf_kprobe_multi_cookie_cmp);
  2232. if (!addr)
  2233. return 0;
  2234. cookie = link->cookies + (addr - link->addrs);
  2235. return *cookie;
  2236. }
  2237. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2238. {
  2239. struct bpf_kprobe_multi_run_ctx *run_ctx;
  2240. run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
  2241. return run_ctx->entry_ip;
  2242. }
  2243. static int
  2244. kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
  2245. unsigned long entry_ip, struct pt_regs *regs)
  2246. {
  2247. struct bpf_kprobe_multi_run_ctx run_ctx = {
  2248. .link = link,
  2249. .entry_ip = entry_ip,
  2250. };
  2251. struct bpf_run_ctx *old_run_ctx;
  2252. int err;
  2253. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  2254. err = 0;
  2255. goto out;
  2256. }
  2257. migrate_disable();
  2258. rcu_read_lock();
  2259. old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  2260. err = bpf_prog_run(link->link.prog, regs);
  2261. bpf_reset_run_ctx(old_run_ctx);
  2262. rcu_read_unlock();
  2263. migrate_enable();
  2264. out:
  2265. __this_cpu_dec(bpf_prog_active);
  2266. return err;
  2267. }
  2268. static void
  2269. kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
  2270. struct pt_regs *regs, void *data)
  2271. {
  2272. struct bpf_kprobe_multi_link *link;
  2273. link = container_of(fp, struct bpf_kprobe_multi_link, fp);
  2274. kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
  2275. }
  2276. static int symbols_cmp_r(const void *a, const void *b, const void *priv)
  2277. {
  2278. const char **str_a = (const char **) a;
  2279. const char **str_b = (const char **) b;
  2280. return strcmp(*str_a, *str_b);
  2281. }
  2282. struct multi_symbols_sort {
  2283. const char **funcs;
  2284. u64 *cookies;
  2285. };
  2286. static void symbols_swap_r(void *a, void *b, int size, const void *priv)
  2287. {
  2288. const struct multi_symbols_sort *data = priv;
  2289. const char **name_a = a, **name_b = b;
  2290. swap(*name_a, *name_b);
  2291. /* If defined, swap also related cookies. */
  2292. if (data->cookies) {
  2293. u64 *cookie_a, *cookie_b;
  2294. cookie_a = data->cookies + (name_a - data->funcs);
  2295. cookie_b = data->cookies + (name_b - data->funcs);
  2296. swap(*cookie_a, *cookie_b);
  2297. }
  2298. }
  2299. static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
  2300. {
  2301. u32 i;
  2302. for (i = 0; i < cnt; i++) {
  2303. if (!within_error_injection_list(addrs[i]))
  2304. return -EINVAL;
  2305. }
  2306. return 0;
  2307. }
  2308. int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2309. {
  2310. struct bpf_kprobe_multi_link *link = NULL;
  2311. struct bpf_link_primer link_primer;
  2312. void __user *ucookies;
  2313. unsigned long *addrs;
  2314. u32 flags, cnt, size;
  2315. void __user *uaddrs;
  2316. u64 *cookies = NULL;
  2317. void __user *usyms;
  2318. int err;
  2319. /* no support for 32bit archs yet */
  2320. if (sizeof(u64) != sizeof(void *))
  2321. return -EOPNOTSUPP;
  2322. if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
  2323. return -EINVAL;
  2324. flags = attr->link_create.kprobe_multi.flags;
  2325. if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
  2326. return -EINVAL;
  2327. uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
  2328. usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
  2329. if (!!uaddrs == !!usyms)
  2330. return -EINVAL;
  2331. cnt = attr->link_create.kprobe_multi.cnt;
  2332. if (!cnt)
  2333. return -EINVAL;
  2334. size = cnt * sizeof(*addrs);
  2335. addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
  2336. if (!addrs)
  2337. return -ENOMEM;
  2338. ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
  2339. if (ucookies) {
  2340. cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
  2341. if (!cookies) {
  2342. err = -ENOMEM;
  2343. goto error;
  2344. }
  2345. if (copy_from_user(cookies, ucookies, size)) {
  2346. err = -EFAULT;
  2347. goto error;
  2348. }
  2349. }
  2350. if (uaddrs) {
  2351. if (copy_from_user(addrs, uaddrs, size)) {
  2352. err = -EFAULT;
  2353. goto error;
  2354. }
  2355. } else {
  2356. struct multi_symbols_sort data = {
  2357. .cookies = cookies,
  2358. };
  2359. struct user_syms us;
  2360. err = copy_user_syms(&us, usyms, cnt);
  2361. if (err)
  2362. goto error;
  2363. if (cookies)
  2364. data.funcs = us.syms;
  2365. sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
  2366. symbols_swap_r, &data);
  2367. err = ftrace_lookup_symbols(us.syms, cnt, addrs);
  2368. free_user_syms(&us);
  2369. if (err)
  2370. goto error;
  2371. }
  2372. if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
  2373. err = -EINVAL;
  2374. goto error;
  2375. }
  2376. link = kzalloc(sizeof(*link), GFP_KERNEL);
  2377. if (!link) {
  2378. err = -ENOMEM;
  2379. goto error;
  2380. }
  2381. bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
  2382. &bpf_kprobe_multi_link_lops, prog);
  2383. err = bpf_link_prime(&link->link, &link_primer);
  2384. if (err)
  2385. goto error;
  2386. if (flags & BPF_F_KPROBE_MULTI_RETURN)
  2387. link->fp.exit_handler = kprobe_multi_link_handler;
  2388. else
  2389. link->fp.entry_handler = kprobe_multi_link_handler;
  2390. link->addrs = addrs;
  2391. link->cookies = cookies;
  2392. link->cnt = cnt;
  2393. if (cookies) {
  2394. /*
  2395. * Sorting addresses will trigger sorting cookies as well
  2396. * (check bpf_kprobe_multi_cookie_swap). This way we can
  2397. * find cookie based on the address in bpf_get_attach_cookie
  2398. * helper.
  2399. */
  2400. sort_r(addrs, cnt, sizeof(*addrs),
  2401. bpf_kprobe_multi_cookie_cmp,
  2402. bpf_kprobe_multi_cookie_swap,
  2403. link);
  2404. }
  2405. err = register_fprobe_ips(&link->fp, addrs, cnt);
  2406. if (err) {
  2407. bpf_link_cleanup(&link_primer);
  2408. return err;
  2409. }
  2410. return bpf_link_settle(&link_primer);
  2411. error:
  2412. kfree(link);
  2413. kvfree(addrs);
  2414. kvfree(cookies);
  2415. return err;
  2416. }
  2417. #else /* !CONFIG_FPROBE */
  2418. int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2419. {
  2420. return -EOPNOTSUPP;
  2421. }
  2422. static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
  2423. {
  2424. return 0;
  2425. }
  2426. static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
  2427. {
  2428. return 0;
  2429. }
  2430. #endif