verifier.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/bpf.h>
  17. #include <linux/bpf_verifier.h>
  18. #include <linux/filter.h>
  19. #include <net/netlink.h>
  20. #include <linux/file.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/stringify.h>
  23. /* bpf_check() is a static code analyzer that walks eBPF program
  24. * instruction by instruction and updates register/stack state.
  25. * All paths of conditional branches are analyzed until 'bpf_exit' insn.
  26. *
  27. * The first pass is depth-first-search to check that the program is a DAG.
  28. * It rejects the following programs:
  29. * - larger than BPF_MAXINSNS insns
  30. * - if loop is present (detected via back-edge)
  31. * - unreachable insns exist (shouldn't be a forest. program = one function)
  32. * - out of bounds or malformed jumps
  33. * The second pass is all possible path descent from the 1st insn.
  34. * Since it's analyzing all pathes through the program, the length of the
  35. * analysis is limited to 64k insn, which may be hit even if total number of
  36. * insn is less then 4K, but there are too many branches that change stack/regs.
  37. * Number of 'branches to be analyzed' is limited to 1k
  38. *
  39. * On entry to each instruction, each register has a type, and the instruction
  40. * changes the types of the registers depending on instruction semantics.
  41. * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
  42. * copied to R1.
  43. *
  44. * All registers are 64-bit.
  45. * R0 - return register
  46. * R1-R5 argument passing registers
  47. * R6-R9 callee saved registers
  48. * R10 - frame pointer read-only
  49. *
  50. * At the start of BPF program the register R1 contains a pointer to bpf_context
  51. * and has type PTR_TO_CTX.
  52. *
  53. * Verifier tracks arithmetic operations on pointers in case:
  54. * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  55. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
  56. * 1st insn copies R10 (which has FRAME_PTR) type into R1
  57. * and 2nd arithmetic instruction is pattern matched to recognize
  58. * that it wants to construct a pointer to some element within stack.
  59. * So after 2nd insn, the register R1 has type PTR_TO_STACK
  60. * (and -20 constant is saved for further stack bounds checking).
  61. * Meaning that this reg is a pointer to stack plus known immediate constant.
  62. *
  63. * Most of the time the registers have UNKNOWN_VALUE type, which
  64. * means the register has some value, but it's not a valid pointer.
  65. * (like pointer plus pointer becomes UNKNOWN_VALUE type)
  66. *
  67. * When verifier sees load or store instructions the type of base register
  68. * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
  69. * types recognized by check_mem_access() function.
  70. *
  71. * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
  72. * and the range of [ptr, ptr + map's value_size) is accessible.
  73. *
  74. * registers used to pass values to function calls are checked against
  75. * function argument constraints.
  76. *
  77. * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
  78. * It means that the register type passed to this function must be
  79. * PTR_TO_STACK and it will be used inside the function as
  80. * 'pointer to map element key'
  81. *
  82. * For example the argument constraints for bpf_map_lookup_elem():
  83. * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  84. * .arg1_type = ARG_CONST_MAP_PTR,
  85. * .arg2_type = ARG_PTR_TO_MAP_KEY,
  86. *
  87. * ret_type says that this function returns 'pointer to map elem value or null'
  88. * function expects 1st argument to be a const pointer to 'struct bpf_map' and
  89. * 2nd argument should be a pointer to stack, which will be used inside
  90. * the helper function as a pointer to map element key.
  91. *
  92. * On the kernel side the helper function looks like:
  93. * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  94. * {
  95. * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  96. * void *key = (void *) (unsigned long) r2;
  97. * void *value;
  98. *
  99. * here kernel can access 'key' and 'map' pointers safely, knowing that
  100. * [key, key + map->key_size) bytes are valid and were initialized on
  101. * the stack of eBPF program.
  102. * }
  103. *
  104. * Corresponding eBPF program may look like:
  105. * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
  106. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
  107. * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
  108. * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  109. * here verifier looks at prototype of map_lookup_elem() and sees:
  110. * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
  111. * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
  112. *
  113. * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
  114. * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
  115. * and were initialized prior to this call.
  116. * If it's ok, then verifier allows this BPF_CALL insn and looks at
  117. * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
  118. * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
  119. * returns ether pointer to map value or NULL.
  120. *
  121. * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
  122. * insn, the register holding that pointer in the true branch changes state to
  123. * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
  124. * branch. See check_cond_jmp_op().
  125. *
  126. * After the call R0 is set to return type of the function and registers R1-R5
  127. * are set to NOT_INIT to indicate that they are no longer readable.
  128. */
  129. /* verifier_state + insn_idx are pushed to stack when branch is encountered */
  130. struct bpf_verifier_stack_elem {
  131. /* verifer state is 'st'
  132. * before processing instruction 'insn_idx'
  133. * and after processing instruction 'prev_insn_idx'
  134. */
  135. struct bpf_verifier_state st;
  136. int insn_idx;
  137. int prev_insn_idx;
  138. struct bpf_verifier_stack_elem *next;
  139. };
  140. #define BPF_COMPLEXITY_LIMIT_INSNS 65536
  141. #define BPF_COMPLEXITY_LIMIT_STACK 1024
  142. struct bpf_call_arg_meta {
  143. struct bpf_map *map_ptr;
  144. bool raw_mode;
  145. bool pkt_access;
  146. int regno;
  147. int access_size;
  148. };
  149. /* verbose verifier prints what it's seeing
  150. * bpf_check() is called under lock, so no race to access these global vars
  151. */
  152. static u32 log_level, log_size, log_len;
  153. static char *log_buf;
  154. static DEFINE_MUTEX(bpf_verifier_lock);
  155. /* log_level controls verbosity level of eBPF verifier.
  156. * verbose() is used to dump the verification trace to the log, so the user
  157. * can figure out what's wrong with the program
  158. */
  159. static __printf(1, 2) void verbose(const char *fmt, ...)
  160. {
  161. va_list args;
  162. if (log_level == 0 || log_len >= log_size - 1)
  163. return;
  164. va_start(args, fmt);
  165. log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
  166. va_end(args);
  167. }
  168. /* string representation of 'enum bpf_reg_type' */
  169. static const char * const reg_type_str[] = {
  170. [NOT_INIT] = "?",
  171. [UNKNOWN_VALUE] = "inv",
  172. [PTR_TO_CTX] = "ctx",
  173. [CONST_PTR_TO_MAP] = "map_ptr",
  174. [PTR_TO_MAP_VALUE] = "map_value",
  175. [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
  176. [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj",
  177. [FRAME_PTR] = "fp",
  178. [PTR_TO_STACK] = "fp",
  179. [CONST_IMM] = "imm",
  180. [PTR_TO_PACKET] = "pkt",
  181. [PTR_TO_PACKET_END] = "pkt_end",
  182. };
  183. #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
  184. static const char * const func_id_str[] = {
  185. __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
  186. };
  187. #undef __BPF_FUNC_STR_FN
  188. static const char *func_id_name(int id)
  189. {
  190. BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
  191. if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
  192. return func_id_str[id];
  193. else
  194. return "unknown";
  195. }
  196. static void print_verifier_state(struct bpf_verifier_state *state)
  197. {
  198. struct bpf_reg_state *reg;
  199. enum bpf_reg_type t;
  200. int i;
  201. for (i = 0; i < MAX_BPF_REG; i++) {
  202. reg = &state->regs[i];
  203. t = reg->type;
  204. if (t == NOT_INIT)
  205. continue;
  206. verbose(" R%d=%s", i, reg_type_str[t]);
  207. if (t == CONST_IMM || t == PTR_TO_STACK)
  208. verbose("%lld", reg->imm);
  209. else if (t == PTR_TO_PACKET)
  210. verbose("(id=%d,off=%d,r=%d)",
  211. reg->id, reg->off, reg->range);
  212. else if (t == UNKNOWN_VALUE && reg->imm)
  213. verbose("%lld", reg->imm);
  214. else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
  215. t == PTR_TO_MAP_VALUE_OR_NULL ||
  216. t == PTR_TO_MAP_VALUE_ADJ)
  217. verbose("(ks=%d,vs=%d,id=%u)",
  218. reg->map_ptr->key_size,
  219. reg->map_ptr->value_size,
  220. reg->id);
  221. if (reg->min_value != BPF_REGISTER_MIN_RANGE)
  222. verbose(",min_value=%lld",
  223. (long long)reg->min_value);
  224. if (reg->max_value != BPF_REGISTER_MAX_RANGE)
  225. verbose(",max_value=%llu",
  226. (unsigned long long)reg->max_value);
  227. }
  228. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  229. if (state->stack_slot_type[i] == STACK_SPILL)
  230. verbose(" fp%d=%s", -MAX_BPF_STACK + i,
  231. reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
  232. }
  233. verbose("\n");
  234. }
  235. static const char *const bpf_class_string[] = {
  236. [BPF_LD] = "ld",
  237. [BPF_LDX] = "ldx",
  238. [BPF_ST] = "st",
  239. [BPF_STX] = "stx",
  240. [BPF_ALU] = "alu",
  241. [BPF_JMP] = "jmp",
  242. [BPF_RET] = "BUG",
  243. [BPF_ALU64] = "alu64",
  244. };
  245. static const char *const bpf_alu_string[16] = {
  246. [BPF_ADD >> 4] = "+=",
  247. [BPF_SUB >> 4] = "-=",
  248. [BPF_MUL >> 4] = "*=",
  249. [BPF_DIV >> 4] = "/=",
  250. [BPF_OR >> 4] = "|=",
  251. [BPF_AND >> 4] = "&=",
  252. [BPF_LSH >> 4] = "<<=",
  253. [BPF_RSH >> 4] = ">>=",
  254. [BPF_NEG >> 4] = "neg",
  255. [BPF_MOD >> 4] = "%=",
  256. [BPF_XOR >> 4] = "^=",
  257. [BPF_MOV >> 4] = "=",
  258. [BPF_ARSH >> 4] = "s>>=",
  259. [BPF_END >> 4] = "endian",
  260. };
  261. static const char *const bpf_ldst_string[] = {
  262. [BPF_W >> 3] = "u32",
  263. [BPF_H >> 3] = "u16",
  264. [BPF_B >> 3] = "u8",
  265. [BPF_DW >> 3] = "u64",
  266. };
  267. static const char *const bpf_jmp_string[16] = {
  268. [BPF_JA >> 4] = "jmp",
  269. [BPF_JEQ >> 4] = "==",
  270. [BPF_JGT >> 4] = ">",
  271. [BPF_JGE >> 4] = ">=",
  272. [BPF_JSET >> 4] = "&",
  273. [BPF_JNE >> 4] = "!=",
  274. [BPF_JSGT >> 4] = "s>",
  275. [BPF_JSGE >> 4] = "s>=",
  276. [BPF_CALL >> 4] = "call",
  277. [BPF_EXIT >> 4] = "exit",
  278. };
  279. static void print_bpf_insn(struct bpf_insn *insn)
  280. {
  281. u8 class = BPF_CLASS(insn->code);
  282. if (class == BPF_ALU || class == BPF_ALU64) {
  283. if (BPF_SRC(insn->code) == BPF_X)
  284. verbose("(%02x) %sr%d %s %sr%d\n",
  285. insn->code, class == BPF_ALU ? "(u32) " : "",
  286. insn->dst_reg,
  287. bpf_alu_string[BPF_OP(insn->code) >> 4],
  288. class == BPF_ALU ? "(u32) " : "",
  289. insn->src_reg);
  290. else
  291. verbose("(%02x) %sr%d %s %s%d\n",
  292. insn->code, class == BPF_ALU ? "(u32) " : "",
  293. insn->dst_reg,
  294. bpf_alu_string[BPF_OP(insn->code) >> 4],
  295. class == BPF_ALU ? "(u32) " : "",
  296. insn->imm);
  297. } else if (class == BPF_STX) {
  298. if (BPF_MODE(insn->code) == BPF_MEM)
  299. verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
  300. insn->code,
  301. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  302. insn->dst_reg,
  303. insn->off, insn->src_reg);
  304. else if (BPF_MODE(insn->code) == BPF_XADD)
  305. verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
  306. insn->code,
  307. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  308. insn->dst_reg, insn->off,
  309. insn->src_reg);
  310. else
  311. verbose("BUG_%02x\n", insn->code);
  312. } else if (class == BPF_ST) {
  313. if (BPF_MODE(insn->code) != BPF_MEM) {
  314. verbose("BUG_st_%02x\n", insn->code);
  315. return;
  316. }
  317. verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
  318. insn->code,
  319. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  320. insn->dst_reg,
  321. insn->off, insn->imm);
  322. } else if (class == BPF_LDX) {
  323. if (BPF_MODE(insn->code) != BPF_MEM) {
  324. verbose("BUG_ldx_%02x\n", insn->code);
  325. return;
  326. }
  327. verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
  328. insn->code, insn->dst_reg,
  329. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  330. insn->src_reg, insn->off);
  331. } else if (class == BPF_LD) {
  332. if (BPF_MODE(insn->code) == BPF_ABS) {
  333. verbose("(%02x) r0 = *(%s *)skb[%d]\n",
  334. insn->code,
  335. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  336. insn->imm);
  337. } else if (BPF_MODE(insn->code) == BPF_IND) {
  338. verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
  339. insn->code,
  340. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  341. insn->src_reg, insn->imm);
  342. } else if (BPF_MODE(insn->code) == BPF_IMM) {
  343. verbose("(%02x) r%d = 0x%x\n",
  344. insn->code, insn->dst_reg, insn->imm);
  345. } else {
  346. verbose("BUG_ld_%02x\n", insn->code);
  347. return;
  348. }
  349. } else if (class == BPF_JMP) {
  350. u8 opcode = BPF_OP(insn->code);
  351. if (opcode == BPF_CALL) {
  352. verbose("(%02x) call %s#%d\n", insn->code,
  353. func_id_name(insn->imm), insn->imm);
  354. } else if (insn->code == (BPF_JMP | BPF_JA)) {
  355. verbose("(%02x) goto pc%+d\n",
  356. insn->code, insn->off);
  357. } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
  358. verbose("(%02x) exit\n", insn->code);
  359. } else if (BPF_SRC(insn->code) == BPF_X) {
  360. verbose("(%02x) if r%d %s r%d goto pc%+d\n",
  361. insn->code, insn->dst_reg,
  362. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  363. insn->src_reg, insn->off);
  364. } else {
  365. verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
  366. insn->code, insn->dst_reg,
  367. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  368. insn->imm, insn->off);
  369. }
  370. } else {
  371. verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
  372. }
  373. }
  374. static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
  375. {
  376. struct bpf_verifier_stack_elem *elem;
  377. int insn_idx;
  378. if (env->head == NULL)
  379. return -1;
  380. memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
  381. insn_idx = env->head->insn_idx;
  382. if (prev_insn_idx)
  383. *prev_insn_idx = env->head->prev_insn_idx;
  384. elem = env->head->next;
  385. kfree(env->head);
  386. env->head = elem;
  387. env->stack_size--;
  388. return insn_idx;
  389. }
  390. static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
  391. int insn_idx, int prev_insn_idx)
  392. {
  393. struct bpf_verifier_stack_elem *elem;
  394. elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
  395. if (!elem)
  396. goto err;
  397. memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
  398. elem->insn_idx = insn_idx;
  399. elem->prev_insn_idx = prev_insn_idx;
  400. elem->next = env->head;
  401. env->head = elem;
  402. env->stack_size++;
  403. if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
  404. verbose("BPF program is too complex\n");
  405. goto err;
  406. }
  407. return &elem->st;
  408. err:
  409. /* pop all elements and return */
  410. while (pop_stack(env, NULL) >= 0);
  411. return NULL;
  412. }
  413. #define CALLER_SAVED_REGS 6
  414. static const int caller_saved[CALLER_SAVED_REGS] = {
  415. BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
  416. };
  417. static void init_reg_state(struct bpf_reg_state *regs)
  418. {
  419. int i;
  420. for (i = 0; i < MAX_BPF_REG; i++) {
  421. regs[i].type = NOT_INIT;
  422. regs[i].imm = 0;
  423. regs[i].min_value = BPF_REGISTER_MIN_RANGE;
  424. regs[i].max_value = BPF_REGISTER_MAX_RANGE;
  425. }
  426. /* frame pointer */
  427. regs[BPF_REG_FP].type = FRAME_PTR;
  428. /* 1st arg to a function */
  429. regs[BPF_REG_1].type = PTR_TO_CTX;
  430. }
  431. static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
  432. {
  433. regs[regno].type = UNKNOWN_VALUE;
  434. regs[regno].id = 0;
  435. regs[regno].imm = 0;
  436. }
  437. static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
  438. {
  439. BUG_ON(regno >= MAX_BPF_REG);
  440. __mark_reg_unknown_value(regs, regno);
  441. }
  442. static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
  443. {
  444. regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
  445. regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
  446. }
  447. static void mark_reg_unknown_value_and_range(struct bpf_reg_state *regs,
  448. u32 regno)
  449. {
  450. mark_reg_unknown_value(regs, regno);
  451. reset_reg_range_values(regs, regno);
  452. }
  453. enum reg_arg_type {
  454. SRC_OP, /* register is used as source operand */
  455. DST_OP, /* register is used as destination operand */
  456. DST_OP_NO_MARK /* same as above, check only, don't mark */
  457. };
  458. static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
  459. enum reg_arg_type t)
  460. {
  461. if (regno >= MAX_BPF_REG) {
  462. verbose("R%d is invalid\n", regno);
  463. return -EINVAL;
  464. }
  465. if (t == SRC_OP) {
  466. /* check whether register used as source operand can be read */
  467. if (regs[regno].type == NOT_INIT) {
  468. verbose("R%d !read_ok\n", regno);
  469. return -EACCES;
  470. }
  471. } else {
  472. /* check whether register used as dest operand can be written to */
  473. if (regno == BPF_REG_FP) {
  474. verbose("frame pointer is read only\n");
  475. return -EACCES;
  476. }
  477. if (t == DST_OP)
  478. mark_reg_unknown_value(regs, regno);
  479. }
  480. return 0;
  481. }
  482. static int bpf_size_to_bytes(int bpf_size)
  483. {
  484. if (bpf_size == BPF_W)
  485. return 4;
  486. else if (bpf_size == BPF_H)
  487. return 2;
  488. else if (bpf_size == BPF_B)
  489. return 1;
  490. else if (bpf_size == BPF_DW)
  491. return 8;
  492. else
  493. return -EINVAL;
  494. }
  495. static bool is_spillable_regtype(enum bpf_reg_type type)
  496. {
  497. switch (type) {
  498. case PTR_TO_MAP_VALUE:
  499. case PTR_TO_MAP_VALUE_OR_NULL:
  500. case PTR_TO_MAP_VALUE_ADJ:
  501. case PTR_TO_STACK:
  502. case PTR_TO_CTX:
  503. case PTR_TO_PACKET:
  504. case PTR_TO_PACKET_END:
  505. case FRAME_PTR:
  506. case CONST_PTR_TO_MAP:
  507. return true;
  508. default:
  509. return false;
  510. }
  511. }
  512. /* check_stack_read/write functions track spill/fill of registers,
  513. * stack boundary and alignment are checked in check_mem_access()
  514. */
  515. static int check_stack_write(struct bpf_verifier_state *state, int off,
  516. int size, int value_regno)
  517. {
  518. int i;
  519. /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
  520. * so it's aligned access and [off, off + size) are within stack limits
  521. */
  522. if (value_regno >= 0 &&
  523. is_spillable_regtype(state->regs[value_regno].type)) {
  524. /* register containing pointer is being spilled into stack */
  525. if (size != BPF_REG_SIZE) {
  526. verbose("invalid size of register spill\n");
  527. return -EACCES;
  528. }
  529. /* save register state */
  530. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  531. state->regs[value_regno];
  532. for (i = 0; i < BPF_REG_SIZE; i++)
  533. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
  534. } else {
  535. /* regular write of data into stack */
  536. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  537. (struct bpf_reg_state) {};
  538. for (i = 0; i < size; i++)
  539. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
  540. }
  541. return 0;
  542. }
  543. static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
  544. int value_regno)
  545. {
  546. u8 *slot_type;
  547. int i;
  548. slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
  549. if (slot_type[0] == STACK_SPILL) {
  550. if (size != BPF_REG_SIZE) {
  551. verbose("invalid size of register spill\n");
  552. return -EACCES;
  553. }
  554. for (i = 1; i < BPF_REG_SIZE; i++) {
  555. if (slot_type[i] != STACK_SPILL) {
  556. verbose("corrupted spill memory\n");
  557. return -EACCES;
  558. }
  559. }
  560. if (value_regno >= 0)
  561. /* restore register state from stack */
  562. state->regs[value_regno] =
  563. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
  564. return 0;
  565. } else {
  566. for (i = 0; i < size; i++) {
  567. if (slot_type[i] != STACK_MISC) {
  568. verbose("invalid read from stack off %d+%d size %d\n",
  569. off, i, size);
  570. return -EACCES;
  571. }
  572. }
  573. if (value_regno >= 0)
  574. /* have read misc data from the stack */
  575. mark_reg_unknown_value_and_range(state->regs,
  576. value_regno);
  577. return 0;
  578. }
  579. }
  580. /* check read/write into map element returned by bpf_map_lookup_elem() */
  581. static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
  582. int size)
  583. {
  584. struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
  585. if (off < 0 || size <= 0 || off + size > map->value_size) {
  586. verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
  587. map->value_size, off, size);
  588. return -EACCES;
  589. }
  590. return 0;
  591. }
  592. /* check read/write into an adjusted map element */
  593. static int check_map_access_adj(struct bpf_verifier_env *env, u32 regno,
  594. int off, int size)
  595. {
  596. struct bpf_verifier_state *state = &env->cur_state;
  597. struct bpf_reg_state *reg = &state->regs[regno];
  598. int err;
  599. /* We adjusted the register to this map value, so we
  600. * need to change off and size to min_value and max_value
  601. * respectively to make sure our theoretical access will be
  602. * safe.
  603. */
  604. if (log_level)
  605. print_verifier_state(state);
  606. env->varlen_map_value_access = true;
  607. /* The minimum value is only important with signed
  608. * comparisons where we can't assume the floor of a
  609. * value is 0. If we are using signed variables for our
  610. * index'es we need to make sure that whatever we use
  611. * will have a set floor within our range.
  612. */
  613. if (reg->min_value < 0) {
  614. verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
  615. regno);
  616. return -EACCES;
  617. }
  618. err = check_map_access(env, regno, reg->min_value + off, size);
  619. if (err) {
  620. verbose("R%d min value is outside of the array range\n",
  621. regno);
  622. return err;
  623. }
  624. /* If we haven't set a max value then we need to bail
  625. * since we can't be sure we won't do bad things.
  626. */
  627. if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
  628. verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
  629. regno);
  630. return -EACCES;
  631. }
  632. return check_map_access(env, regno, reg->max_value + off, size);
  633. }
  634. #define MAX_PACKET_OFF 0xffff
  635. static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
  636. const struct bpf_call_arg_meta *meta,
  637. enum bpf_access_type t)
  638. {
  639. switch (env->prog->type) {
  640. case BPF_PROG_TYPE_LWT_IN:
  641. case BPF_PROG_TYPE_LWT_OUT:
  642. /* dst_input() and dst_output() can't write for now */
  643. if (t == BPF_WRITE)
  644. return false;
  645. /* fallthrough */
  646. case BPF_PROG_TYPE_SCHED_CLS:
  647. case BPF_PROG_TYPE_SCHED_ACT:
  648. case BPF_PROG_TYPE_XDP:
  649. case BPF_PROG_TYPE_LWT_XMIT:
  650. if (meta)
  651. return meta->pkt_access;
  652. env->seen_direct_write = true;
  653. return true;
  654. default:
  655. return false;
  656. }
  657. }
  658. static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
  659. int size)
  660. {
  661. struct bpf_reg_state *regs = env->cur_state.regs;
  662. struct bpf_reg_state *reg = &regs[regno];
  663. off += reg->off;
  664. if (off < 0 || size <= 0 || off + size > reg->range) {
  665. verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
  666. off, size, regno, reg->id, reg->off, reg->range);
  667. return -EACCES;
  668. }
  669. return 0;
  670. }
  671. /* check access to 'struct bpf_context' fields */
  672. static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
  673. enum bpf_access_type t, enum bpf_reg_type *reg_type)
  674. {
  675. /* for analyzer ctx accesses are already validated and converted */
  676. if (env->analyzer_ops)
  677. return 0;
  678. if (env->prog->aux->ops->is_valid_access &&
  679. env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
  680. /* remember the offset of last byte accessed in ctx */
  681. if (env->prog->aux->max_ctx_offset < off + size)
  682. env->prog->aux->max_ctx_offset = off + size;
  683. return 0;
  684. }
  685. verbose("invalid bpf_context access off=%d size=%d\n", off, size);
  686. return -EACCES;
  687. }
  688. static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
  689. {
  690. if (env->allow_ptr_leaks)
  691. return false;
  692. switch (env->cur_state.regs[regno].type) {
  693. case UNKNOWN_VALUE:
  694. case CONST_IMM:
  695. return false;
  696. default:
  697. return true;
  698. }
  699. }
  700. static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
  701. int off, int size)
  702. {
  703. if (reg->id && size != 1) {
  704. verbose("Unknown alignment. Only byte-sized access allowed in packet access.\n");
  705. return -EACCES;
  706. }
  707. /* skb->data is NET_IP_ALIGN-ed */
  708. if ((NET_IP_ALIGN + reg->off + off) % size != 0) {
  709. verbose("misaligned packet access off %d+%d+%d size %d\n",
  710. NET_IP_ALIGN, reg->off, off, size);
  711. return -EACCES;
  712. }
  713. return 0;
  714. }
  715. static int check_val_ptr_alignment(const struct bpf_reg_state *reg,
  716. int size)
  717. {
  718. if (size != 1) {
  719. verbose("Unknown alignment. Only byte-sized access allowed in value access.\n");
  720. return -EACCES;
  721. }
  722. return 0;
  723. }
  724. static int check_ptr_alignment(const struct bpf_reg_state *reg,
  725. int off, int size)
  726. {
  727. switch (reg->type) {
  728. case PTR_TO_PACKET:
  729. return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ? 0 :
  730. check_pkt_ptr_alignment(reg, off, size);
  731. case PTR_TO_MAP_VALUE_ADJ:
  732. return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ? 0 :
  733. check_val_ptr_alignment(reg, size);
  734. default:
  735. if (off % size != 0) {
  736. verbose("misaligned access off %d size %d\n",
  737. off, size);
  738. return -EACCES;
  739. }
  740. return 0;
  741. }
  742. }
  743. /* check whether memory at (regno + off) is accessible for t = (read | write)
  744. * if t==write, value_regno is a register which value is stored into memory
  745. * if t==read, value_regno is a register which will receive the value from memory
  746. * if t==write && value_regno==-1, some unknown value is stored into memory
  747. * if t==read && value_regno==-1, don't care what we read from memory
  748. */
  749. static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
  750. int bpf_size, enum bpf_access_type t,
  751. int value_regno)
  752. {
  753. struct bpf_verifier_state *state = &env->cur_state;
  754. struct bpf_reg_state *reg = &state->regs[regno];
  755. int size, err = 0;
  756. if (reg->type == PTR_TO_STACK)
  757. off += reg->imm;
  758. size = bpf_size_to_bytes(bpf_size);
  759. if (size < 0)
  760. return size;
  761. err = check_ptr_alignment(reg, off, size);
  762. if (err)
  763. return err;
  764. if (reg->type == PTR_TO_MAP_VALUE ||
  765. reg->type == PTR_TO_MAP_VALUE_ADJ) {
  766. if (t == BPF_WRITE && value_regno >= 0 &&
  767. is_pointer_value(env, value_regno)) {
  768. verbose("R%d leaks addr into map\n", value_regno);
  769. return -EACCES;
  770. }
  771. if (reg->type == PTR_TO_MAP_VALUE_ADJ)
  772. err = check_map_access_adj(env, regno, off, size);
  773. else
  774. err = check_map_access(env, regno, off, size);
  775. if (!err && t == BPF_READ && value_regno >= 0)
  776. mark_reg_unknown_value_and_range(state->regs,
  777. value_regno);
  778. } else if (reg->type == PTR_TO_CTX) {
  779. enum bpf_reg_type reg_type = UNKNOWN_VALUE;
  780. if (t == BPF_WRITE && value_regno >= 0 &&
  781. is_pointer_value(env, value_regno)) {
  782. verbose("R%d leaks addr into ctx\n", value_regno);
  783. return -EACCES;
  784. }
  785. err = check_ctx_access(env, off, size, t, &reg_type);
  786. if (!err && t == BPF_READ && value_regno >= 0) {
  787. mark_reg_unknown_value_and_range(state->regs,
  788. value_regno);
  789. /* note that reg.[id|off|range] == 0 */
  790. state->regs[value_regno].type = reg_type;
  791. }
  792. } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
  793. if (off >= 0 || off < -MAX_BPF_STACK) {
  794. verbose("invalid stack off=%d size=%d\n", off, size);
  795. return -EACCES;
  796. }
  797. if (t == BPF_WRITE) {
  798. if (!env->allow_ptr_leaks &&
  799. state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
  800. size != BPF_REG_SIZE) {
  801. verbose("attempt to corrupt spilled pointer on stack\n");
  802. return -EACCES;
  803. }
  804. err = check_stack_write(state, off, size, value_regno);
  805. } else {
  806. err = check_stack_read(state, off, size, value_regno);
  807. }
  808. } else if (state->regs[regno].type == PTR_TO_PACKET) {
  809. if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
  810. verbose("cannot write into packet\n");
  811. return -EACCES;
  812. }
  813. if (t == BPF_WRITE && value_regno >= 0 &&
  814. is_pointer_value(env, value_regno)) {
  815. verbose("R%d leaks addr into packet\n", value_regno);
  816. return -EACCES;
  817. }
  818. err = check_packet_access(env, regno, off, size);
  819. if (!err && t == BPF_READ && value_regno >= 0)
  820. mark_reg_unknown_value_and_range(state->regs,
  821. value_regno);
  822. } else {
  823. verbose("R%d invalid mem access '%s'\n",
  824. regno, reg_type_str[reg->type]);
  825. return -EACCES;
  826. }
  827. if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
  828. state->regs[value_regno].type == UNKNOWN_VALUE) {
  829. /* 1 or 2 byte load zero-extends, determine the number of
  830. * zero upper bits. Not doing it fo 4 byte load, since
  831. * such values cannot be added to ptr_to_packet anyway.
  832. */
  833. state->regs[value_regno].imm = 64 - size * 8;
  834. }
  835. return err;
  836. }
  837. static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
  838. {
  839. struct bpf_reg_state *regs = env->cur_state.regs;
  840. int err;
  841. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  842. insn->imm != 0) {
  843. verbose("BPF_XADD uses reserved fields\n");
  844. return -EINVAL;
  845. }
  846. /* check src1 operand */
  847. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  848. if (err)
  849. return err;
  850. /* check src2 operand */
  851. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  852. if (err)
  853. return err;
  854. /* check whether atomic_add can read the memory */
  855. err = check_mem_access(env, insn->dst_reg, insn->off,
  856. BPF_SIZE(insn->code), BPF_READ, -1);
  857. if (err)
  858. return err;
  859. /* check whether atomic_add can write into the same memory */
  860. return check_mem_access(env, insn->dst_reg, insn->off,
  861. BPF_SIZE(insn->code), BPF_WRITE, -1);
  862. }
  863. /* when register 'regno' is passed into function that will read 'access_size'
  864. * bytes from that pointer, make sure that it's within stack boundary
  865. * and all elements of stack are initialized
  866. */
  867. static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
  868. int access_size, bool zero_size_allowed,
  869. struct bpf_call_arg_meta *meta)
  870. {
  871. struct bpf_verifier_state *state = &env->cur_state;
  872. struct bpf_reg_state *regs = state->regs;
  873. int off, i;
  874. if (regs[regno].type != PTR_TO_STACK) {
  875. if (zero_size_allowed && access_size == 0 &&
  876. regs[regno].type == CONST_IMM &&
  877. regs[regno].imm == 0)
  878. return 0;
  879. verbose("R%d type=%s expected=%s\n", regno,
  880. reg_type_str[regs[regno].type],
  881. reg_type_str[PTR_TO_STACK]);
  882. return -EACCES;
  883. }
  884. off = regs[regno].imm;
  885. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  886. access_size <= 0) {
  887. verbose("invalid stack type R%d off=%d access_size=%d\n",
  888. regno, off, access_size);
  889. return -EACCES;
  890. }
  891. if (meta && meta->raw_mode) {
  892. meta->access_size = access_size;
  893. meta->regno = regno;
  894. return 0;
  895. }
  896. for (i = 0; i < access_size; i++) {
  897. if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
  898. verbose("invalid indirect read from stack off %d+%d size %d\n",
  899. off, i, access_size);
  900. return -EACCES;
  901. }
  902. }
  903. return 0;
  904. }
  905. static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
  906. int access_size, bool zero_size_allowed,
  907. struct bpf_call_arg_meta *meta)
  908. {
  909. struct bpf_reg_state *regs = env->cur_state.regs;
  910. switch (regs[regno].type) {
  911. case PTR_TO_PACKET:
  912. return check_packet_access(env, regno, 0, access_size);
  913. case PTR_TO_MAP_VALUE:
  914. return check_map_access(env, regno, 0, access_size);
  915. case PTR_TO_MAP_VALUE_ADJ:
  916. return check_map_access_adj(env, regno, 0, access_size);
  917. default: /* const_imm|ptr_to_stack or invalid ptr */
  918. return check_stack_boundary(env, regno, access_size,
  919. zero_size_allowed, meta);
  920. }
  921. }
  922. static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
  923. enum bpf_arg_type arg_type,
  924. struct bpf_call_arg_meta *meta)
  925. {
  926. struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
  927. enum bpf_reg_type expected_type, type = reg->type;
  928. int err = 0;
  929. if (arg_type == ARG_DONTCARE)
  930. return 0;
  931. if (type == NOT_INIT) {
  932. verbose("R%d !read_ok\n", regno);
  933. return -EACCES;
  934. }
  935. if (arg_type == ARG_ANYTHING) {
  936. if (is_pointer_value(env, regno)) {
  937. verbose("R%d leaks addr into helper function\n", regno);
  938. return -EACCES;
  939. }
  940. return 0;
  941. }
  942. if (type == PTR_TO_PACKET &&
  943. !may_access_direct_pkt_data(env, meta, BPF_READ)) {
  944. verbose("helper access to the packet is not allowed\n");
  945. return -EACCES;
  946. }
  947. if (arg_type == ARG_PTR_TO_MAP_KEY ||
  948. arg_type == ARG_PTR_TO_MAP_VALUE) {
  949. expected_type = PTR_TO_STACK;
  950. if (type != PTR_TO_PACKET && type != expected_type)
  951. goto err_type;
  952. } else if (arg_type == ARG_CONST_SIZE ||
  953. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  954. expected_type = CONST_IMM;
  955. /* One exception. Allow UNKNOWN_VALUE registers when the
  956. * boundaries are known and don't cause unsafe memory accesses
  957. */
  958. if (type != UNKNOWN_VALUE && type != expected_type)
  959. goto err_type;
  960. } else if (arg_type == ARG_CONST_MAP_PTR) {
  961. expected_type = CONST_PTR_TO_MAP;
  962. if (type != expected_type)
  963. goto err_type;
  964. } else if (arg_type == ARG_PTR_TO_CTX) {
  965. expected_type = PTR_TO_CTX;
  966. if (type != expected_type)
  967. goto err_type;
  968. } else if (arg_type == ARG_PTR_TO_MEM ||
  969. arg_type == ARG_PTR_TO_UNINIT_MEM) {
  970. expected_type = PTR_TO_STACK;
  971. /* One exception here. In case function allows for NULL to be
  972. * passed in as argument, it's a CONST_IMM type. Final test
  973. * happens during stack boundary checking.
  974. */
  975. if (type == CONST_IMM && reg->imm == 0)
  976. /* final test in check_stack_boundary() */;
  977. else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
  978. type != PTR_TO_MAP_VALUE_ADJ && type != expected_type)
  979. goto err_type;
  980. meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
  981. } else {
  982. verbose("unsupported arg_type %d\n", arg_type);
  983. return -EFAULT;
  984. }
  985. if (arg_type == ARG_CONST_MAP_PTR) {
  986. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  987. meta->map_ptr = reg->map_ptr;
  988. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  989. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  990. * check that [key, key + map->key_size) are within
  991. * stack limits and initialized
  992. */
  993. if (!meta->map_ptr) {
  994. /* in function declaration map_ptr must come before
  995. * map_key, so that it's verified and known before
  996. * we have to check map_key here. Otherwise it means
  997. * that kernel subsystem misconfigured verifier
  998. */
  999. verbose("invalid map_ptr to access map->key\n");
  1000. return -EACCES;
  1001. }
  1002. if (type == PTR_TO_PACKET)
  1003. err = check_packet_access(env, regno, 0,
  1004. meta->map_ptr->key_size);
  1005. else
  1006. err = check_stack_boundary(env, regno,
  1007. meta->map_ptr->key_size,
  1008. false, NULL);
  1009. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  1010. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  1011. * check [value, value + map->value_size) validity
  1012. */
  1013. if (!meta->map_ptr) {
  1014. /* kernel subsystem misconfigured verifier */
  1015. verbose("invalid map_ptr to access map->value\n");
  1016. return -EACCES;
  1017. }
  1018. if (type == PTR_TO_PACKET)
  1019. err = check_packet_access(env, regno, 0,
  1020. meta->map_ptr->value_size);
  1021. else
  1022. err = check_stack_boundary(env, regno,
  1023. meta->map_ptr->value_size,
  1024. false, NULL);
  1025. } else if (arg_type == ARG_CONST_SIZE ||
  1026. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  1027. bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
  1028. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  1029. * from stack pointer 'buf'. Check it
  1030. * note: regno == len, regno - 1 == buf
  1031. */
  1032. if (regno == 0) {
  1033. /* kernel subsystem misconfigured verifier */
  1034. verbose("ARG_CONST_SIZE cannot be first argument\n");
  1035. return -EACCES;
  1036. }
  1037. /* If the register is UNKNOWN_VALUE, the access check happens
  1038. * using its boundaries. Otherwise, just use its imm
  1039. */
  1040. if (type == UNKNOWN_VALUE) {
  1041. /* For unprivileged variable accesses, disable raw
  1042. * mode so that the program is required to
  1043. * initialize all the memory that the helper could
  1044. * just partially fill up.
  1045. */
  1046. meta = NULL;
  1047. if (reg->min_value < 0) {
  1048. verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
  1049. regno);
  1050. return -EACCES;
  1051. }
  1052. if (reg->min_value == 0) {
  1053. err = check_helper_mem_access(env, regno - 1, 0,
  1054. zero_size_allowed,
  1055. meta);
  1056. if (err)
  1057. return err;
  1058. }
  1059. if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
  1060. verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
  1061. regno);
  1062. return -EACCES;
  1063. }
  1064. err = check_helper_mem_access(env, regno - 1,
  1065. reg->max_value,
  1066. zero_size_allowed, meta);
  1067. if (err)
  1068. return err;
  1069. } else {
  1070. /* register is CONST_IMM */
  1071. err = check_helper_mem_access(env, regno - 1, reg->imm,
  1072. zero_size_allowed, meta);
  1073. }
  1074. }
  1075. return err;
  1076. err_type:
  1077. verbose("R%d type=%s expected=%s\n", regno,
  1078. reg_type_str[type], reg_type_str[expected_type]);
  1079. return -EACCES;
  1080. }
  1081. static int check_map_func_compatibility(struct bpf_map *map, int func_id)
  1082. {
  1083. if (!map)
  1084. return 0;
  1085. /* We need a two way check, first is from map perspective ... */
  1086. switch (map->map_type) {
  1087. case BPF_MAP_TYPE_PROG_ARRAY:
  1088. if (func_id != BPF_FUNC_tail_call)
  1089. goto error;
  1090. break;
  1091. case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
  1092. if (func_id != BPF_FUNC_perf_event_read &&
  1093. func_id != BPF_FUNC_perf_event_output)
  1094. goto error;
  1095. break;
  1096. case BPF_MAP_TYPE_STACK_TRACE:
  1097. if (func_id != BPF_FUNC_get_stackid)
  1098. goto error;
  1099. break;
  1100. case BPF_MAP_TYPE_CGROUP_ARRAY:
  1101. if (func_id != BPF_FUNC_skb_under_cgroup &&
  1102. func_id != BPF_FUNC_current_task_under_cgroup)
  1103. goto error;
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. /* ... and second from the function itself. */
  1109. switch (func_id) {
  1110. case BPF_FUNC_tail_call:
  1111. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  1112. goto error;
  1113. break;
  1114. case BPF_FUNC_perf_event_read:
  1115. case BPF_FUNC_perf_event_output:
  1116. if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
  1117. goto error;
  1118. break;
  1119. case BPF_FUNC_get_stackid:
  1120. if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
  1121. goto error;
  1122. break;
  1123. case BPF_FUNC_current_task_under_cgroup:
  1124. case BPF_FUNC_skb_under_cgroup:
  1125. if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
  1126. goto error;
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. return 0;
  1132. error:
  1133. verbose("cannot pass map_type %d into func %s#%d\n",
  1134. map->map_type, func_id_name(func_id), func_id);
  1135. return -EINVAL;
  1136. }
  1137. static int check_raw_mode(const struct bpf_func_proto *fn)
  1138. {
  1139. int count = 0;
  1140. if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
  1141. count++;
  1142. if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
  1143. count++;
  1144. if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
  1145. count++;
  1146. if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
  1147. count++;
  1148. if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
  1149. count++;
  1150. return count > 1 ? -EINVAL : 0;
  1151. }
  1152. static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
  1153. {
  1154. struct bpf_verifier_state *state = &env->cur_state;
  1155. struct bpf_reg_state *regs = state->regs, *reg;
  1156. int i;
  1157. for (i = 0; i < MAX_BPF_REG; i++)
  1158. if (regs[i].type == PTR_TO_PACKET ||
  1159. regs[i].type == PTR_TO_PACKET_END)
  1160. mark_reg_unknown_value(regs, i);
  1161. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1162. if (state->stack_slot_type[i] != STACK_SPILL)
  1163. continue;
  1164. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1165. if (reg->type != PTR_TO_PACKET &&
  1166. reg->type != PTR_TO_PACKET_END)
  1167. continue;
  1168. reg->type = UNKNOWN_VALUE;
  1169. reg->imm = 0;
  1170. }
  1171. }
  1172. static int check_call(struct bpf_verifier_env *env, int func_id)
  1173. {
  1174. struct bpf_verifier_state *state = &env->cur_state;
  1175. const struct bpf_func_proto *fn = NULL;
  1176. struct bpf_reg_state *regs = state->regs;
  1177. struct bpf_reg_state *reg;
  1178. struct bpf_call_arg_meta meta;
  1179. bool changes_data;
  1180. int i, err;
  1181. /* find function prototype */
  1182. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  1183. verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
  1184. return -EINVAL;
  1185. }
  1186. if (env->prog->aux->ops->get_func_proto)
  1187. fn = env->prog->aux->ops->get_func_proto(func_id);
  1188. if (!fn) {
  1189. verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
  1190. return -EINVAL;
  1191. }
  1192. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1193. if (!env->prog->gpl_compatible && fn->gpl_only) {
  1194. verbose("cannot call GPL only function from proprietary program\n");
  1195. return -EINVAL;
  1196. }
  1197. changes_data = bpf_helper_changes_pkt_data(fn->func);
  1198. memset(&meta, 0, sizeof(meta));
  1199. meta.pkt_access = fn->pkt_access;
  1200. /* We only support one arg being in raw mode at the moment, which
  1201. * is sufficient for the helper functions we have right now.
  1202. */
  1203. err = check_raw_mode(fn);
  1204. if (err) {
  1205. verbose("kernel subsystem misconfigured func %s#%d\n",
  1206. func_id_name(func_id), func_id);
  1207. return err;
  1208. }
  1209. /* check args */
  1210. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
  1211. if (err)
  1212. return err;
  1213. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
  1214. if (err)
  1215. return err;
  1216. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
  1217. if (err)
  1218. return err;
  1219. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
  1220. if (err)
  1221. return err;
  1222. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
  1223. if (err)
  1224. return err;
  1225. /* Mark slots with STACK_MISC in case of raw mode, stack offset
  1226. * is inferred from register state.
  1227. */
  1228. for (i = 0; i < meta.access_size; i++) {
  1229. err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
  1230. if (err)
  1231. return err;
  1232. }
  1233. /* reset caller saved regs */
  1234. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  1235. reg = regs + caller_saved[i];
  1236. reg->type = NOT_INIT;
  1237. reg->imm = 0;
  1238. }
  1239. /* update return register */
  1240. if (fn->ret_type == RET_INTEGER) {
  1241. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  1242. } else if (fn->ret_type == RET_VOID) {
  1243. regs[BPF_REG_0].type = NOT_INIT;
  1244. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  1245. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  1246. regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
  1247. /* remember map_ptr, so that check_map_access()
  1248. * can check 'value_size' boundary of memory access
  1249. * to map element returned from bpf_map_lookup_elem()
  1250. */
  1251. if (meta.map_ptr == NULL) {
  1252. verbose("kernel subsystem misconfigured verifier\n");
  1253. return -EINVAL;
  1254. }
  1255. regs[BPF_REG_0].map_ptr = meta.map_ptr;
  1256. regs[BPF_REG_0].id = ++env->id_gen;
  1257. } else {
  1258. verbose("unknown return type %d of func %s#%d\n",
  1259. fn->ret_type, func_id_name(func_id), func_id);
  1260. return -EINVAL;
  1261. }
  1262. err = check_map_func_compatibility(meta.map_ptr, func_id);
  1263. if (err)
  1264. return err;
  1265. if (changes_data)
  1266. clear_all_pkt_pointers(env);
  1267. return 0;
  1268. }
  1269. static int check_packet_ptr_add(struct bpf_verifier_env *env,
  1270. struct bpf_insn *insn)
  1271. {
  1272. struct bpf_reg_state *regs = env->cur_state.regs;
  1273. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1274. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1275. struct bpf_reg_state tmp_reg;
  1276. s32 imm;
  1277. if (BPF_SRC(insn->code) == BPF_K) {
  1278. /* pkt_ptr += imm */
  1279. imm = insn->imm;
  1280. add_imm:
  1281. if (imm < 0) {
  1282. verbose("addition of negative constant to packet pointer is not allowed\n");
  1283. return -EACCES;
  1284. }
  1285. if (imm >= MAX_PACKET_OFF ||
  1286. imm + dst_reg->off >= MAX_PACKET_OFF) {
  1287. verbose("constant %d is too large to add to packet pointer\n",
  1288. imm);
  1289. return -EACCES;
  1290. }
  1291. /* a constant was added to pkt_ptr.
  1292. * Remember it while keeping the same 'id'
  1293. */
  1294. dst_reg->off += imm;
  1295. } else {
  1296. if (src_reg->type == PTR_TO_PACKET) {
  1297. /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
  1298. tmp_reg = *dst_reg; /* save r7 state */
  1299. *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
  1300. src_reg = &tmp_reg; /* pretend it's src_reg state */
  1301. /* if the checks below reject it, the copy won't matter,
  1302. * since we're rejecting the whole program. If all ok,
  1303. * then imm22 state will be added to r7
  1304. * and r7 will be pkt(id=0,off=22,r=62) while
  1305. * r6 will stay as pkt(id=0,off=0,r=62)
  1306. */
  1307. }
  1308. if (src_reg->type == CONST_IMM) {
  1309. /* pkt_ptr += reg where reg is known constant */
  1310. imm = src_reg->imm;
  1311. goto add_imm;
  1312. }
  1313. /* disallow pkt_ptr += reg
  1314. * if reg is not uknown_value with guaranteed zero upper bits
  1315. * otherwise pkt_ptr may overflow and addition will become
  1316. * subtraction which is not allowed
  1317. */
  1318. if (src_reg->type != UNKNOWN_VALUE) {
  1319. verbose("cannot add '%s' to ptr_to_packet\n",
  1320. reg_type_str[src_reg->type]);
  1321. return -EACCES;
  1322. }
  1323. if (src_reg->imm < 48) {
  1324. verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
  1325. src_reg->imm);
  1326. return -EACCES;
  1327. }
  1328. /* dst_reg stays as pkt_ptr type and since some positive
  1329. * integer value was added to the pointer, increment its 'id'
  1330. */
  1331. dst_reg->id = ++env->id_gen;
  1332. /* something was added to pkt_ptr, set range and off to zero */
  1333. dst_reg->off = 0;
  1334. dst_reg->range = 0;
  1335. }
  1336. return 0;
  1337. }
  1338. static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1339. {
  1340. struct bpf_reg_state *regs = env->cur_state.regs;
  1341. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1342. u8 opcode = BPF_OP(insn->code);
  1343. s64 imm_log2;
  1344. /* for type == UNKNOWN_VALUE:
  1345. * imm > 0 -> number of zero upper bits
  1346. * imm == 0 -> don't track which is the same as all bits can be non-zero
  1347. */
  1348. if (BPF_SRC(insn->code) == BPF_X) {
  1349. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1350. if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
  1351. dst_reg->imm && opcode == BPF_ADD) {
  1352. /* dreg += sreg
  1353. * where both have zero upper bits. Adding them
  1354. * can only result making one more bit non-zero
  1355. * in the larger value.
  1356. * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
  1357. * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
  1358. */
  1359. dst_reg->imm = min(dst_reg->imm, src_reg->imm);
  1360. dst_reg->imm--;
  1361. return 0;
  1362. }
  1363. if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
  1364. dst_reg->imm && opcode == BPF_ADD) {
  1365. /* dreg += sreg
  1366. * where dreg has zero upper bits and sreg is const.
  1367. * Adding them can only result making one more bit
  1368. * non-zero in the larger value.
  1369. */
  1370. imm_log2 = __ilog2_u64((long long)src_reg->imm);
  1371. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1372. dst_reg->imm--;
  1373. return 0;
  1374. }
  1375. /* all other cases non supported yet, just mark dst_reg */
  1376. dst_reg->imm = 0;
  1377. return 0;
  1378. }
  1379. /* sign extend 32-bit imm into 64-bit to make sure that
  1380. * negative values occupy bit 63. Note ilog2() would have
  1381. * been incorrect, since sizeof(insn->imm) == 4
  1382. */
  1383. imm_log2 = __ilog2_u64((long long)insn->imm);
  1384. if (dst_reg->imm && opcode == BPF_LSH) {
  1385. /* reg <<= imm
  1386. * if reg was a result of 2 byte load, then its imm == 48
  1387. * which means that upper 48 bits are zero and shifting this reg
  1388. * left by 4 would mean that upper 44 bits are still zero
  1389. */
  1390. dst_reg->imm -= insn->imm;
  1391. } else if (dst_reg->imm && opcode == BPF_MUL) {
  1392. /* reg *= imm
  1393. * if multiplying by 14 subtract 4
  1394. * This is conservative calculation of upper zero bits.
  1395. * It's not trying to special case insn->imm == 1 or 0 cases
  1396. */
  1397. dst_reg->imm -= imm_log2 + 1;
  1398. } else if (opcode == BPF_AND) {
  1399. /* reg &= imm */
  1400. dst_reg->imm = 63 - imm_log2;
  1401. } else if (dst_reg->imm && opcode == BPF_ADD) {
  1402. /* reg += imm */
  1403. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1404. dst_reg->imm--;
  1405. } else if (opcode == BPF_RSH) {
  1406. /* reg >>= imm
  1407. * which means that after right shift, upper bits will be zero
  1408. * note that verifier already checked that
  1409. * 0 <= imm < 64 for shift insn
  1410. */
  1411. dst_reg->imm += insn->imm;
  1412. if (unlikely(dst_reg->imm > 64))
  1413. /* some dumb code did:
  1414. * r2 = *(u32 *)mem;
  1415. * r2 >>= 32;
  1416. * and all bits are zero now */
  1417. dst_reg->imm = 64;
  1418. } else {
  1419. /* all other alu ops, means that we don't know what will
  1420. * happen to the value, mark it with unknown number of zero bits
  1421. */
  1422. dst_reg->imm = 0;
  1423. }
  1424. if (dst_reg->imm < 0) {
  1425. /* all 64 bits of the register can contain non-zero bits
  1426. * and such value cannot be added to ptr_to_packet, since it
  1427. * may overflow, mark it as unknown to avoid further eval
  1428. */
  1429. dst_reg->imm = 0;
  1430. }
  1431. return 0;
  1432. }
  1433. static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
  1434. struct bpf_insn *insn)
  1435. {
  1436. struct bpf_reg_state *regs = env->cur_state.regs;
  1437. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1438. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1439. u8 opcode = BPF_OP(insn->code);
  1440. u64 dst_imm = dst_reg->imm;
  1441. /* dst_reg->type == CONST_IMM here. Simulate execution of insns
  1442. * containing ALU ops. Don't care about overflow or negative
  1443. * values, just add/sub/... them; registers are in u64.
  1444. */
  1445. if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) {
  1446. dst_imm += insn->imm;
  1447. } else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
  1448. src_reg->type == CONST_IMM) {
  1449. dst_imm += src_reg->imm;
  1450. } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_K) {
  1451. dst_imm -= insn->imm;
  1452. } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_X &&
  1453. src_reg->type == CONST_IMM) {
  1454. dst_imm -= src_reg->imm;
  1455. } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_K) {
  1456. dst_imm *= insn->imm;
  1457. } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_X &&
  1458. src_reg->type == CONST_IMM) {
  1459. dst_imm *= src_reg->imm;
  1460. } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K) {
  1461. dst_imm |= insn->imm;
  1462. } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X &&
  1463. src_reg->type == CONST_IMM) {
  1464. dst_imm |= src_reg->imm;
  1465. } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_K) {
  1466. dst_imm &= insn->imm;
  1467. } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_X &&
  1468. src_reg->type == CONST_IMM) {
  1469. dst_imm &= src_reg->imm;
  1470. } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_K) {
  1471. dst_imm >>= insn->imm;
  1472. } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_X &&
  1473. src_reg->type == CONST_IMM) {
  1474. dst_imm >>= src_reg->imm;
  1475. } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_K) {
  1476. dst_imm <<= insn->imm;
  1477. } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_X &&
  1478. src_reg->type == CONST_IMM) {
  1479. dst_imm <<= src_reg->imm;
  1480. } else {
  1481. mark_reg_unknown_value(regs, insn->dst_reg);
  1482. goto out;
  1483. }
  1484. dst_reg->imm = dst_imm;
  1485. out:
  1486. return 0;
  1487. }
  1488. static void check_reg_overflow(struct bpf_reg_state *reg)
  1489. {
  1490. if (reg->max_value > BPF_REGISTER_MAX_RANGE)
  1491. reg->max_value = BPF_REGISTER_MAX_RANGE;
  1492. if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
  1493. reg->min_value > BPF_REGISTER_MAX_RANGE)
  1494. reg->min_value = BPF_REGISTER_MIN_RANGE;
  1495. }
  1496. static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
  1497. struct bpf_insn *insn)
  1498. {
  1499. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1500. s64 min_val = BPF_REGISTER_MIN_RANGE;
  1501. u64 max_val = BPF_REGISTER_MAX_RANGE;
  1502. u8 opcode = BPF_OP(insn->code);
  1503. dst_reg = &regs[insn->dst_reg];
  1504. if (BPF_SRC(insn->code) == BPF_X) {
  1505. check_reg_overflow(&regs[insn->src_reg]);
  1506. min_val = regs[insn->src_reg].min_value;
  1507. max_val = regs[insn->src_reg].max_value;
  1508. /* If the source register is a random pointer then the
  1509. * min_value/max_value values represent the range of the known
  1510. * accesses into that value, not the actual min/max value of the
  1511. * register itself. In this case we have to reset the reg range
  1512. * values so we know it is not safe to look at.
  1513. */
  1514. if (regs[insn->src_reg].type != CONST_IMM &&
  1515. regs[insn->src_reg].type != UNKNOWN_VALUE) {
  1516. min_val = BPF_REGISTER_MIN_RANGE;
  1517. max_val = BPF_REGISTER_MAX_RANGE;
  1518. }
  1519. } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
  1520. (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
  1521. min_val = max_val = insn->imm;
  1522. }
  1523. /* We don't know anything about what was done to this register, mark it
  1524. * as unknown.
  1525. */
  1526. if (min_val == BPF_REGISTER_MIN_RANGE &&
  1527. max_val == BPF_REGISTER_MAX_RANGE) {
  1528. reset_reg_range_values(regs, insn->dst_reg);
  1529. return;
  1530. }
  1531. /* If one of our values was at the end of our ranges then we can't just
  1532. * do our normal operations to the register, we need to set the values
  1533. * to the min/max since they are undefined.
  1534. */
  1535. if (min_val == BPF_REGISTER_MIN_RANGE)
  1536. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1537. if (max_val == BPF_REGISTER_MAX_RANGE)
  1538. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1539. switch (opcode) {
  1540. case BPF_ADD:
  1541. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1542. dst_reg->min_value += min_val;
  1543. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1544. dst_reg->max_value += max_val;
  1545. break;
  1546. case BPF_SUB:
  1547. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1548. dst_reg->min_value -= min_val;
  1549. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1550. dst_reg->max_value -= max_val;
  1551. break;
  1552. case BPF_MUL:
  1553. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1554. dst_reg->min_value *= min_val;
  1555. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1556. dst_reg->max_value *= max_val;
  1557. break;
  1558. case BPF_AND:
  1559. /* Disallow AND'ing of negative numbers, ain't nobody got time
  1560. * for that. Otherwise the minimum is 0 and the max is the max
  1561. * value we could AND against.
  1562. */
  1563. if (min_val < 0)
  1564. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1565. else
  1566. dst_reg->min_value = 0;
  1567. dst_reg->max_value = max_val;
  1568. break;
  1569. case BPF_LSH:
  1570. /* Gotta have special overflow logic here, if we're shifting
  1571. * more than MAX_RANGE then just assume we have an invalid
  1572. * range.
  1573. */
  1574. if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1575. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1576. else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1577. dst_reg->min_value <<= min_val;
  1578. if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1579. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1580. else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1581. dst_reg->max_value <<= max_val;
  1582. break;
  1583. case BPF_RSH:
  1584. /* RSH by a negative number is undefined, and the BPF_RSH is an
  1585. * unsigned shift, so make the appropriate casts.
  1586. */
  1587. if (min_val < 0 || dst_reg->min_value < 0)
  1588. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1589. else
  1590. dst_reg->min_value =
  1591. (u64)(dst_reg->min_value) >> min_val;
  1592. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1593. dst_reg->max_value >>= max_val;
  1594. break;
  1595. default:
  1596. reset_reg_range_values(regs, insn->dst_reg);
  1597. break;
  1598. }
  1599. check_reg_overflow(dst_reg);
  1600. }
  1601. /* check validity of 32-bit and 64-bit arithmetic operations */
  1602. static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1603. {
  1604. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1605. u8 opcode = BPF_OP(insn->code);
  1606. int err;
  1607. if (opcode == BPF_END || opcode == BPF_NEG) {
  1608. if (opcode == BPF_NEG) {
  1609. if (BPF_SRC(insn->code) != 0 ||
  1610. insn->src_reg != BPF_REG_0 ||
  1611. insn->off != 0 || insn->imm != 0) {
  1612. verbose("BPF_NEG uses reserved fields\n");
  1613. return -EINVAL;
  1614. }
  1615. } else {
  1616. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  1617. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
  1618. verbose("BPF_END uses reserved fields\n");
  1619. return -EINVAL;
  1620. }
  1621. }
  1622. /* check src operand */
  1623. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1624. if (err)
  1625. return err;
  1626. if (is_pointer_value(env, insn->dst_reg)) {
  1627. verbose("R%d pointer arithmetic prohibited\n",
  1628. insn->dst_reg);
  1629. return -EACCES;
  1630. }
  1631. /* check dest operand */
  1632. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1633. if (err)
  1634. return err;
  1635. } else if (opcode == BPF_MOV) {
  1636. if (BPF_SRC(insn->code) == BPF_X) {
  1637. if (insn->imm != 0 || insn->off != 0) {
  1638. verbose("BPF_MOV uses reserved fields\n");
  1639. return -EINVAL;
  1640. }
  1641. /* check src operand */
  1642. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1643. if (err)
  1644. return err;
  1645. } else {
  1646. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1647. verbose("BPF_MOV uses reserved fields\n");
  1648. return -EINVAL;
  1649. }
  1650. }
  1651. /* check dest operand */
  1652. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1653. if (err)
  1654. return err;
  1655. /* we are setting our register to something new, we need to
  1656. * reset its range values.
  1657. */
  1658. reset_reg_range_values(regs, insn->dst_reg);
  1659. if (BPF_SRC(insn->code) == BPF_X) {
  1660. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  1661. /* case: R1 = R2
  1662. * copy register state to dest reg
  1663. */
  1664. regs[insn->dst_reg] = regs[insn->src_reg];
  1665. } else {
  1666. if (is_pointer_value(env, insn->src_reg)) {
  1667. verbose("R%d partial copy of pointer\n",
  1668. insn->src_reg);
  1669. return -EACCES;
  1670. }
  1671. mark_reg_unknown_value(regs, insn->dst_reg);
  1672. }
  1673. } else {
  1674. /* case: R = imm
  1675. * remember the value we stored into this reg
  1676. */
  1677. regs[insn->dst_reg].type = CONST_IMM;
  1678. regs[insn->dst_reg].imm = insn->imm;
  1679. regs[insn->dst_reg].max_value = insn->imm;
  1680. regs[insn->dst_reg].min_value = insn->imm;
  1681. }
  1682. } else if (opcode > BPF_END) {
  1683. verbose("invalid BPF_ALU opcode %x\n", opcode);
  1684. return -EINVAL;
  1685. } else { /* all other ALU ops: and, sub, xor, add, ... */
  1686. if (BPF_SRC(insn->code) == BPF_X) {
  1687. if (insn->imm != 0 || insn->off != 0) {
  1688. verbose("BPF_ALU uses reserved fields\n");
  1689. return -EINVAL;
  1690. }
  1691. /* check src1 operand */
  1692. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1693. if (err)
  1694. return err;
  1695. } else {
  1696. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1697. verbose("BPF_ALU uses reserved fields\n");
  1698. return -EINVAL;
  1699. }
  1700. }
  1701. /* check src2 operand */
  1702. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1703. if (err)
  1704. return err;
  1705. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  1706. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  1707. verbose("div by zero\n");
  1708. return -EINVAL;
  1709. }
  1710. if ((opcode == BPF_LSH || opcode == BPF_RSH ||
  1711. opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
  1712. int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
  1713. if (insn->imm < 0 || insn->imm >= size) {
  1714. verbose("invalid shift %d\n", insn->imm);
  1715. return -EINVAL;
  1716. }
  1717. }
  1718. /* check dest operand */
  1719. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  1720. if (err)
  1721. return err;
  1722. dst_reg = &regs[insn->dst_reg];
  1723. /* first we want to adjust our ranges. */
  1724. adjust_reg_min_max_vals(env, insn);
  1725. /* pattern match 'bpf_add Rx, imm' instruction */
  1726. if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
  1727. dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
  1728. dst_reg->type = PTR_TO_STACK;
  1729. dst_reg->imm = insn->imm;
  1730. return 0;
  1731. } else if (opcode == BPF_ADD &&
  1732. BPF_CLASS(insn->code) == BPF_ALU64 &&
  1733. (dst_reg->type == PTR_TO_PACKET ||
  1734. (BPF_SRC(insn->code) == BPF_X &&
  1735. regs[insn->src_reg].type == PTR_TO_PACKET))) {
  1736. /* ptr_to_packet += K|X */
  1737. return check_packet_ptr_add(env, insn);
  1738. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1739. dst_reg->type == UNKNOWN_VALUE &&
  1740. env->allow_ptr_leaks) {
  1741. /* unknown += K|X */
  1742. return evaluate_reg_alu(env, insn);
  1743. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1744. dst_reg->type == CONST_IMM &&
  1745. env->allow_ptr_leaks) {
  1746. /* reg_imm += K|X */
  1747. return evaluate_reg_imm_alu(env, insn);
  1748. } else if (is_pointer_value(env, insn->dst_reg)) {
  1749. verbose("R%d pointer arithmetic prohibited\n",
  1750. insn->dst_reg);
  1751. return -EACCES;
  1752. } else if (BPF_SRC(insn->code) == BPF_X &&
  1753. is_pointer_value(env, insn->src_reg)) {
  1754. verbose("R%d pointer arithmetic prohibited\n",
  1755. insn->src_reg);
  1756. return -EACCES;
  1757. }
  1758. /* If we did pointer math on a map value then just set it to our
  1759. * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
  1760. * loads to this register appropriately, otherwise just mark the
  1761. * register as unknown.
  1762. */
  1763. if (env->allow_ptr_leaks &&
  1764. BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
  1765. (dst_reg->type == PTR_TO_MAP_VALUE ||
  1766. dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
  1767. dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
  1768. else
  1769. mark_reg_unknown_value(regs, insn->dst_reg);
  1770. }
  1771. return 0;
  1772. }
  1773. static void find_good_pkt_pointers(struct bpf_verifier_state *state,
  1774. struct bpf_reg_state *dst_reg)
  1775. {
  1776. struct bpf_reg_state *regs = state->regs, *reg;
  1777. int i;
  1778. /* LLVM can generate two kind of checks:
  1779. *
  1780. * Type 1:
  1781. *
  1782. * r2 = r3;
  1783. * r2 += 8;
  1784. * if (r2 > pkt_end) goto <handle exception>
  1785. * <access okay>
  1786. *
  1787. * Where:
  1788. * r2 == dst_reg, pkt_end == src_reg
  1789. * r2=pkt(id=n,off=8,r=0)
  1790. * r3=pkt(id=n,off=0,r=0)
  1791. *
  1792. * Type 2:
  1793. *
  1794. * r2 = r3;
  1795. * r2 += 8;
  1796. * if (pkt_end >= r2) goto <access okay>
  1797. * <handle exception>
  1798. *
  1799. * Where:
  1800. * pkt_end == dst_reg, r2 == src_reg
  1801. * r2=pkt(id=n,off=8,r=0)
  1802. * r3=pkt(id=n,off=0,r=0)
  1803. *
  1804. * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
  1805. * so that range of bytes [r3, r3 + 8) is safe to access.
  1806. */
  1807. for (i = 0; i < MAX_BPF_REG; i++)
  1808. if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
  1809. /* keep the maximum range already checked */
  1810. regs[i].range = max(regs[i].range, dst_reg->off);
  1811. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1812. if (state->stack_slot_type[i] != STACK_SPILL)
  1813. continue;
  1814. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1815. if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
  1816. reg->range = max(reg->range, dst_reg->off);
  1817. }
  1818. }
  1819. /* Adjusts the register min/max values in the case that the dst_reg is the
  1820. * variable register that we are working on, and src_reg is a constant or we're
  1821. * simply doing a BPF_K check.
  1822. */
  1823. static void reg_set_min_max(struct bpf_reg_state *true_reg,
  1824. struct bpf_reg_state *false_reg, u64 val,
  1825. u8 opcode)
  1826. {
  1827. switch (opcode) {
  1828. case BPF_JEQ:
  1829. /* If this is false then we know nothing Jon Snow, but if it is
  1830. * true then we know for sure.
  1831. */
  1832. true_reg->max_value = true_reg->min_value = val;
  1833. break;
  1834. case BPF_JNE:
  1835. /* If this is true we know nothing Jon Snow, but if it is false
  1836. * we know the value for sure;
  1837. */
  1838. false_reg->max_value = false_reg->min_value = val;
  1839. break;
  1840. case BPF_JGT:
  1841. /* Unsigned comparison, the minimum value is 0. */
  1842. false_reg->min_value = 0;
  1843. /* fallthrough */
  1844. case BPF_JSGT:
  1845. /* If this is false then we know the maximum val is val,
  1846. * otherwise we know the min val is val+1.
  1847. */
  1848. false_reg->max_value = val;
  1849. true_reg->min_value = val + 1;
  1850. break;
  1851. case BPF_JGE:
  1852. /* Unsigned comparison, the minimum value is 0. */
  1853. false_reg->min_value = 0;
  1854. /* fallthrough */
  1855. case BPF_JSGE:
  1856. /* If this is false then we know the maximum value is val - 1,
  1857. * otherwise we know the mimimum value is val.
  1858. */
  1859. false_reg->max_value = val - 1;
  1860. true_reg->min_value = val;
  1861. break;
  1862. default:
  1863. break;
  1864. }
  1865. check_reg_overflow(false_reg);
  1866. check_reg_overflow(true_reg);
  1867. }
  1868. /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
  1869. * is the variable reg.
  1870. */
  1871. static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
  1872. struct bpf_reg_state *false_reg, u64 val,
  1873. u8 opcode)
  1874. {
  1875. switch (opcode) {
  1876. case BPF_JEQ:
  1877. /* If this is false then we know nothing Jon Snow, but if it is
  1878. * true then we know for sure.
  1879. */
  1880. true_reg->max_value = true_reg->min_value = val;
  1881. break;
  1882. case BPF_JNE:
  1883. /* If this is true we know nothing Jon Snow, but if it is false
  1884. * we know the value for sure;
  1885. */
  1886. false_reg->max_value = false_reg->min_value = val;
  1887. break;
  1888. case BPF_JGT:
  1889. /* Unsigned comparison, the minimum value is 0. */
  1890. true_reg->min_value = 0;
  1891. /* fallthrough */
  1892. case BPF_JSGT:
  1893. /*
  1894. * If this is false, then the val is <= the register, if it is
  1895. * true the register <= to the val.
  1896. */
  1897. false_reg->min_value = val;
  1898. true_reg->max_value = val - 1;
  1899. break;
  1900. case BPF_JGE:
  1901. /* Unsigned comparison, the minimum value is 0. */
  1902. true_reg->min_value = 0;
  1903. /* fallthrough */
  1904. case BPF_JSGE:
  1905. /* If this is false then constant < register, if it is true then
  1906. * the register < constant.
  1907. */
  1908. false_reg->min_value = val + 1;
  1909. true_reg->max_value = val;
  1910. break;
  1911. default:
  1912. break;
  1913. }
  1914. check_reg_overflow(false_reg);
  1915. check_reg_overflow(true_reg);
  1916. }
  1917. static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
  1918. enum bpf_reg_type type)
  1919. {
  1920. struct bpf_reg_state *reg = &regs[regno];
  1921. if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
  1922. reg->type = type;
  1923. /* We don't need id from this point onwards anymore, thus we
  1924. * should better reset it, so that state pruning has chances
  1925. * to take effect.
  1926. */
  1927. reg->id = 0;
  1928. if (type == UNKNOWN_VALUE)
  1929. __mark_reg_unknown_value(regs, regno);
  1930. }
  1931. }
  1932. /* The logic is similar to find_good_pkt_pointers(), both could eventually
  1933. * be folded together at some point.
  1934. */
  1935. static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
  1936. enum bpf_reg_type type)
  1937. {
  1938. struct bpf_reg_state *regs = state->regs;
  1939. u32 id = regs[regno].id;
  1940. int i;
  1941. for (i = 0; i < MAX_BPF_REG; i++)
  1942. mark_map_reg(regs, i, id, type);
  1943. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1944. if (state->stack_slot_type[i] != STACK_SPILL)
  1945. continue;
  1946. mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
  1947. }
  1948. }
  1949. static int check_cond_jmp_op(struct bpf_verifier_env *env,
  1950. struct bpf_insn *insn, int *insn_idx)
  1951. {
  1952. struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
  1953. struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
  1954. u8 opcode = BPF_OP(insn->code);
  1955. int err;
  1956. if (opcode > BPF_EXIT) {
  1957. verbose("invalid BPF_JMP opcode %x\n", opcode);
  1958. return -EINVAL;
  1959. }
  1960. if (BPF_SRC(insn->code) == BPF_X) {
  1961. if (insn->imm != 0) {
  1962. verbose("BPF_JMP uses reserved fields\n");
  1963. return -EINVAL;
  1964. }
  1965. /* check src1 operand */
  1966. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1967. if (err)
  1968. return err;
  1969. if (is_pointer_value(env, insn->src_reg)) {
  1970. verbose("R%d pointer comparison prohibited\n",
  1971. insn->src_reg);
  1972. return -EACCES;
  1973. }
  1974. } else {
  1975. if (insn->src_reg != BPF_REG_0) {
  1976. verbose("BPF_JMP uses reserved fields\n");
  1977. return -EINVAL;
  1978. }
  1979. }
  1980. /* check src2 operand */
  1981. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1982. if (err)
  1983. return err;
  1984. dst_reg = &regs[insn->dst_reg];
  1985. /* detect if R == 0 where R was initialized to zero earlier */
  1986. if (BPF_SRC(insn->code) == BPF_K &&
  1987. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  1988. dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
  1989. if (opcode == BPF_JEQ) {
  1990. /* if (imm == imm) goto pc+off;
  1991. * only follow the goto, ignore fall-through
  1992. */
  1993. *insn_idx += insn->off;
  1994. return 0;
  1995. } else {
  1996. /* if (imm != imm) goto pc+off;
  1997. * only follow fall-through branch, since
  1998. * that's where the program will go
  1999. */
  2000. return 0;
  2001. }
  2002. }
  2003. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  2004. if (!other_branch)
  2005. return -EFAULT;
  2006. /* detect if we are comparing against a constant value so we can adjust
  2007. * our min/max values for our dst register.
  2008. */
  2009. if (BPF_SRC(insn->code) == BPF_X) {
  2010. if (regs[insn->src_reg].type == CONST_IMM)
  2011. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2012. dst_reg, regs[insn->src_reg].imm,
  2013. opcode);
  2014. else if (dst_reg->type == CONST_IMM)
  2015. reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
  2016. &regs[insn->src_reg], dst_reg->imm,
  2017. opcode);
  2018. } else {
  2019. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2020. dst_reg, insn->imm, opcode);
  2021. }
  2022. /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
  2023. if (BPF_SRC(insn->code) == BPF_K &&
  2024. insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2025. dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
  2026. /* Mark all identical map registers in each branch as either
  2027. * safe or unknown depending R == 0 or R != 0 conditional.
  2028. */
  2029. mark_map_regs(this_branch, insn->dst_reg,
  2030. opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
  2031. mark_map_regs(other_branch, insn->dst_reg,
  2032. opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
  2033. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
  2034. dst_reg->type == PTR_TO_PACKET &&
  2035. regs[insn->src_reg].type == PTR_TO_PACKET_END) {
  2036. find_good_pkt_pointers(this_branch, dst_reg);
  2037. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
  2038. dst_reg->type == PTR_TO_PACKET_END &&
  2039. regs[insn->src_reg].type == PTR_TO_PACKET) {
  2040. find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
  2041. } else if (is_pointer_value(env, insn->dst_reg)) {
  2042. verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
  2043. return -EACCES;
  2044. }
  2045. if (log_level)
  2046. print_verifier_state(this_branch);
  2047. return 0;
  2048. }
  2049. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  2050. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  2051. {
  2052. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  2053. return (struct bpf_map *) (unsigned long) imm64;
  2054. }
  2055. /* verify BPF_LD_IMM64 instruction */
  2056. static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2057. {
  2058. struct bpf_reg_state *regs = env->cur_state.regs;
  2059. int err;
  2060. if (BPF_SIZE(insn->code) != BPF_DW) {
  2061. verbose("invalid BPF_LD_IMM insn\n");
  2062. return -EINVAL;
  2063. }
  2064. if (insn->off != 0) {
  2065. verbose("BPF_LD_IMM64 uses reserved fields\n");
  2066. return -EINVAL;
  2067. }
  2068. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  2069. if (err)
  2070. return err;
  2071. if (insn->src_reg == 0) {
  2072. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  2073. regs[insn->dst_reg].type = CONST_IMM;
  2074. regs[insn->dst_reg].imm = imm;
  2075. return 0;
  2076. }
  2077. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  2078. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  2079. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  2080. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  2081. return 0;
  2082. }
  2083. static bool may_access_skb(enum bpf_prog_type type)
  2084. {
  2085. switch (type) {
  2086. case BPF_PROG_TYPE_SOCKET_FILTER:
  2087. case BPF_PROG_TYPE_SCHED_CLS:
  2088. case BPF_PROG_TYPE_SCHED_ACT:
  2089. return true;
  2090. default:
  2091. return false;
  2092. }
  2093. }
  2094. /* verify safety of LD_ABS|LD_IND instructions:
  2095. * - they can only appear in the programs where ctx == skb
  2096. * - since they are wrappers of function calls, they scratch R1-R5 registers,
  2097. * preserve R6-R9, and store return value into R0
  2098. *
  2099. * Implicit input:
  2100. * ctx == skb == R6 == CTX
  2101. *
  2102. * Explicit input:
  2103. * SRC == any register
  2104. * IMM == 32-bit immediate
  2105. *
  2106. * Output:
  2107. * R0 - 8/16/32-bit skb data converted to cpu endianness
  2108. */
  2109. static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2110. {
  2111. struct bpf_reg_state *regs = env->cur_state.regs;
  2112. u8 mode = BPF_MODE(insn->code);
  2113. struct bpf_reg_state *reg;
  2114. int i, err;
  2115. if (!may_access_skb(env->prog->type)) {
  2116. verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
  2117. return -EINVAL;
  2118. }
  2119. if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
  2120. BPF_SIZE(insn->code) == BPF_DW ||
  2121. (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
  2122. verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
  2123. return -EINVAL;
  2124. }
  2125. /* check whether implicit source operand (register R6) is readable */
  2126. err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
  2127. if (err)
  2128. return err;
  2129. if (regs[BPF_REG_6].type != PTR_TO_CTX) {
  2130. verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
  2131. return -EINVAL;
  2132. }
  2133. if (mode == BPF_IND) {
  2134. /* check explicit source operand */
  2135. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2136. if (err)
  2137. return err;
  2138. }
  2139. /* reset caller saved regs to unreadable */
  2140. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  2141. reg = regs + caller_saved[i];
  2142. reg->type = NOT_INIT;
  2143. reg->imm = 0;
  2144. }
  2145. /* mark destination R0 register as readable, since it contains
  2146. * the value fetched from the packet
  2147. */
  2148. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  2149. return 0;
  2150. }
  2151. /* non-recursive DFS pseudo code
  2152. * 1 procedure DFS-iterative(G,v):
  2153. * 2 label v as discovered
  2154. * 3 let S be a stack
  2155. * 4 S.push(v)
  2156. * 5 while S is not empty
  2157. * 6 t <- S.pop()
  2158. * 7 if t is what we're looking for:
  2159. * 8 return t
  2160. * 9 for all edges e in G.adjacentEdges(t) do
  2161. * 10 if edge e is already labelled
  2162. * 11 continue with the next edge
  2163. * 12 w <- G.adjacentVertex(t,e)
  2164. * 13 if vertex w is not discovered and not explored
  2165. * 14 label e as tree-edge
  2166. * 15 label w as discovered
  2167. * 16 S.push(w)
  2168. * 17 continue at 5
  2169. * 18 else if vertex w is discovered
  2170. * 19 label e as back-edge
  2171. * 20 else
  2172. * 21 // vertex w is explored
  2173. * 22 label e as forward- or cross-edge
  2174. * 23 label t as explored
  2175. * 24 S.pop()
  2176. *
  2177. * convention:
  2178. * 0x10 - discovered
  2179. * 0x11 - discovered and fall-through edge labelled
  2180. * 0x12 - discovered and fall-through and branch edges labelled
  2181. * 0x20 - explored
  2182. */
  2183. enum {
  2184. DISCOVERED = 0x10,
  2185. EXPLORED = 0x20,
  2186. FALLTHROUGH = 1,
  2187. BRANCH = 2,
  2188. };
  2189. #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
  2190. static int *insn_stack; /* stack of insns to process */
  2191. static int cur_stack; /* current stack index */
  2192. static int *insn_state;
  2193. /* t, w, e - match pseudo-code above:
  2194. * t - index of current instruction
  2195. * w - next instruction
  2196. * e - edge
  2197. */
  2198. static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
  2199. {
  2200. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  2201. return 0;
  2202. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  2203. return 0;
  2204. if (w < 0 || w >= env->prog->len) {
  2205. verbose("jump out of range from insn %d to %d\n", t, w);
  2206. return -EINVAL;
  2207. }
  2208. if (e == BRANCH)
  2209. /* mark branch target for state pruning */
  2210. env->explored_states[w] = STATE_LIST_MARK;
  2211. if (insn_state[w] == 0) {
  2212. /* tree-edge */
  2213. insn_state[t] = DISCOVERED | e;
  2214. insn_state[w] = DISCOVERED;
  2215. if (cur_stack >= env->prog->len)
  2216. return -E2BIG;
  2217. insn_stack[cur_stack++] = w;
  2218. return 1;
  2219. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  2220. verbose("back-edge from insn %d to %d\n", t, w);
  2221. return -EINVAL;
  2222. } else if (insn_state[w] == EXPLORED) {
  2223. /* forward- or cross-edge */
  2224. insn_state[t] = DISCOVERED | e;
  2225. } else {
  2226. verbose("insn state internal bug\n");
  2227. return -EFAULT;
  2228. }
  2229. return 0;
  2230. }
  2231. /* non-recursive depth-first-search to detect loops in BPF program
  2232. * loop == back-edge in directed graph
  2233. */
  2234. static int check_cfg(struct bpf_verifier_env *env)
  2235. {
  2236. struct bpf_insn *insns = env->prog->insnsi;
  2237. int insn_cnt = env->prog->len;
  2238. int ret = 0;
  2239. int i, t;
  2240. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2241. if (!insn_state)
  2242. return -ENOMEM;
  2243. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2244. if (!insn_stack) {
  2245. kfree(insn_state);
  2246. return -ENOMEM;
  2247. }
  2248. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  2249. insn_stack[0] = 0; /* 0 is the first instruction */
  2250. cur_stack = 1;
  2251. peek_stack:
  2252. if (cur_stack == 0)
  2253. goto check_state;
  2254. t = insn_stack[cur_stack - 1];
  2255. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  2256. u8 opcode = BPF_OP(insns[t].code);
  2257. if (opcode == BPF_EXIT) {
  2258. goto mark_explored;
  2259. } else if (opcode == BPF_CALL) {
  2260. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2261. if (ret == 1)
  2262. goto peek_stack;
  2263. else if (ret < 0)
  2264. goto err_free;
  2265. if (t + 1 < insn_cnt)
  2266. env->explored_states[t + 1] = STATE_LIST_MARK;
  2267. } else if (opcode == BPF_JA) {
  2268. if (BPF_SRC(insns[t].code) != BPF_K) {
  2269. ret = -EINVAL;
  2270. goto err_free;
  2271. }
  2272. /* unconditional jump with single edge */
  2273. ret = push_insn(t, t + insns[t].off + 1,
  2274. FALLTHROUGH, env);
  2275. if (ret == 1)
  2276. goto peek_stack;
  2277. else if (ret < 0)
  2278. goto err_free;
  2279. /* tell verifier to check for equivalent states
  2280. * after every call and jump
  2281. */
  2282. if (t + 1 < insn_cnt)
  2283. env->explored_states[t + 1] = STATE_LIST_MARK;
  2284. } else {
  2285. /* conditional jump with two edges */
  2286. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2287. if (ret == 1)
  2288. goto peek_stack;
  2289. else if (ret < 0)
  2290. goto err_free;
  2291. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  2292. if (ret == 1)
  2293. goto peek_stack;
  2294. else if (ret < 0)
  2295. goto err_free;
  2296. }
  2297. } else {
  2298. /* all other non-branch instructions with single
  2299. * fall-through edge
  2300. */
  2301. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2302. if (ret == 1)
  2303. goto peek_stack;
  2304. else if (ret < 0)
  2305. goto err_free;
  2306. }
  2307. mark_explored:
  2308. insn_state[t] = EXPLORED;
  2309. if (cur_stack-- <= 0) {
  2310. verbose("pop stack internal bug\n");
  2311. ret = -EFAULT;
  2312. goto err_free;
  2313. }
  2314. goto peek_stack;
  2315. check_state:
  2316. for (i = 0; i < insn_cnt; i++) {
  2317. if (insn_state[i] != EXPLORED) {
  2318. verbose("unreachable insn %d\n", i);
  2319. ret = -EINVAL;
  2320. goto err_free;
  2321. }
  2322. }
  2323. ret = 0; /* cfg looks good */
  2324. err_free:
  2325. kfree(insn_state);
  2326. kfree(insn_stack);
  2327. return ret;
  2328. }
  2329. /* the following conditions reduce the number of explored insns
  2330. * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
  2331. */
  2332. static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
  2333. struct bpf_reg_state *cur)
  2334. {
  2335. if (old->id != cur->id)
  2336. return false;
  2337. /* old ptr_to_packet is more conservative, since it allows smaller
  2338. * range. Ex:
  2339. * old(off=0,r=10) is equal to cur(off=0,r=20), because
  2340. * old(off=0,r=10) means that with range=10 the verifier proceeded
  2341. * further and found no issues with the program. Now we're in the same
  2342. * spot with cur(off=0,r=20), so we're safe too, since anything further
  2343. * will only be looking at most 10 bytes after this pointer.
  2344. */
  2345. if (old->off == cur->off && old->range < cur->range)
  2346. return true;
  2347. /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
  2348. * since both cannot be used for packet access and safe(old)
  2349. * pointer has smaller off that could be used for further
  2350. * 'if (ptr > data_end)' check
  2351. * Ex:
  2352. * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
  2353. * that we cannot access the packet.
  2354. * The safe range is:
  2355. * [ptr, ptr + range - off)
  2356. * so whenever off >=range, it means no safe bytes from this pointer.
  2357. * When comparing old->off <= cur->off, it means that older code
  2358. * went with smaller offset and that offset was later
  2359. * used to figure out the safe range after 'if (ptr > data_end)' check
  2360. * Say, 'old' state was explored like:
  2361. * ... R3(off=0, r=0)
  2362. * R4 = R3 + 20
  2363. * ... now R4(off=20,r=0) <-- here
  2364. * if (R4 > data_end)
  2365. * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
  2366. * ... the code further went all the way to bpf_exit.
  2367. * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
  2368. * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
  2369. * goes further, such cur_R4 will give larger safe packet range after
  2370. * 'if (R4 > data_end)' and all further insn were already good with r=20,
  2371. * so they will be good with r=30 and we can prune the search.
  2372. */
  2373. if (old->off <= cur->off &&
  2374. old->off >= old->range && cur->off >= cur->range)
  2375. return true;
  2376. return false;
  2377. }
  2378. /* compare two verifier states
  2379. *
  2380. * all states stored in state_list are known to be valid, since
  2381. * verifier reached 'bpf_exit' instruction through them
  2382. *
  2383. * this function is called when verifier exploring different branches of
  2384. * execution popped from the state stack. If it sees an old state that has
  2385. * more strict register state and more strict stack state then this execution
  2386. * branch doesn't need to be explored further, since verifier already
  2387. * concluded that more strict state leads to valid finish.
  2388. *
  2389. * Therefore two states are equivalent if register state is more conservative
  2390. * and explored stack state is more conservative than the current one.
  2391. * Example:
  2392. * explored current
  2393. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  2394. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  2395. *
  2396. * In other words if current stack state (one being explored) has more
  2397. * valid slots than old one that already passed validation, it means
  2398. * the verifier can stop exploring and conclude that current state is valid too
  2399. *
  2400. * Similarly with registers. If explored state has register type as invalid
  2401. * whereas register type in current state is meaningful, it means that
  2402. * the current state will reach 'bpf_exit' instruction safely
  2403. */
  2404. static bool states_equal(struct bpf_verifier_env *env,
  2405. struct bpf_verifier_state *old,
  2406. struct bpf_verifier_state *cur)
  2407. {
  2408. bool varlen_map_access = env->varlen_map_value_access;
  2409. struct bpf_reg_state *rold, *rcur;
  2410. int i;
  2411. for (i = 0; i < MAX_BPF_REG; i++) {
  2412. rold = &old->regs[i];
  2413. rcur = &cur->regs[i];
  2414. if (memcmp(rold, rcur, sizeof(*rold)) == 0)
  2415. continue;
  2416. /* If the ranges were not the same, but everything else was and
  2417. * we didn't do a variable access into a map then we are a-ok.
  2418. */
  2419. if (!varlen_map_access &&
  2420. memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
  2421. continue;
  2422. /* If we didn't map access then again we don't care about the
  2423. * mismatched range values and it's ok if our old type was
  2424. * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
  2425. */
  2426. if (rold->type == NOT_INIT ||
  2427. (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
  2428. rcur->type != NOT_INIT))
  2429. continue;
  2430. if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
  2431. compare_ptrs_to_packet(rold, rcur))
  2432. continue;
  2433. return false;
  2434. }
  2435. for (i = 0; i < MAX_BPF_STACK; i++) {
  2436. if (old->stack_slot_type[i] == STACK_INVALID)
  2437. continue;
  2438. if (old->stack_slot_type[i] != cur->stack_slot_type[i])
  2439. /* Ex: old explored (safe) state has STACK_SPILL in
  2440. * this stack slot, but current has has STACK_MISC ->
  2441. * this verifier states are not equivalent,
  2442. * return false to continue verification of this path
  2443. */
  2444. return false;
  2445. if (i % BPF_REG_SIZE)
  2446. continue;
  2447. if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
  2448. &cur->spilled_regs[i / BPF_REG_SIZE],
  2449. sizeof(old->spilled_regs[0])))
  2450. /* when explored and current stack slot types are
  2451. * the same, check that stored pointers types
  2452. * are the same as well.
  2453. * Ex: explored safe path could have stored
  2454. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
  2455. * but current path has stored:
  2456. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
  2457. * such verifier states are not equivalent.
  2458. * return false to continue verification of this path
  2459. */
  2460. return false;
  2461. else
  2462. continue;
  2463. }
  2464. return true;
  2465. }
  2466. static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
  2467. {
  2468. struct bpf_verifier_state_list *new_sl;
  2469. struct bpf_verifier_state_list *sl;
  2470. sl = env->explored_states[insn_idx];
  2471. if (!sl)
  2472. /* this 'insn_idx' instruction wasn't marked, so we will not
  2473. * be doing state search here
  2474. */
  2475. return 0;
  2476. while (sl != STATE_LIST_MARK) {
  2477. if (states_equal(env, &sl->state, &env->cur_state))
  2478. /* reached equivalent register/stack state,
  2479. * prune the search
  2480. */
  2481. return 1;
  2482. sl = sl->next;
  2483. }
  2484. /* there were no equivalent states, remember current one.
  2485. * technically the current state is not proven to be safe yet,
  2486. * but it will either reach bpf_exit (which means it's safe) or
  2487. * it will be rejected. Since there are no loops, we won't be
  2488. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  2489. */
  2490. new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
  2491. if (!new_sl)
  2492. return -ENOMEM;
  2493. /* add new state to the head of linked list */
  2494. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  2495. new_sl->next = env->explored_states[insn_idx];
  2496. env->explored_states[insn_idx] = new_sl;
  2497. return 0;
  2498. }
  2499. static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
  2500. int insn_idx, int prev_insn_idx)
  2501. {
  2502. if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
  2503. return 0;
  2504. return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
  2505. }
  2506. static int do_check(struct bpf_verifier_env *env)
  2507. {
  2508. struct bpf_verifier_state *state = &env->cur_state;
  2509. struct bpf_insn *insns = env->prog->insnsi;
  2510. struct bpf_reg_state *regs = state->regs;
  2511. int insn_cnt = env->prog->len;
  2512. int insn_idx, prev_insn_idx = 0;
  2513. int insn_processed = 0;
  2514. bool do_print_state = false;
  2515. init_reg_state(regs);
  2516. insn_idx = 0;
  2517. env->varlen_map_value_access = false;
  2518. for (;;) {
  2519. struct bpf_insn *insn;
  2520. u8 class;
  2521. int err;
  2522. if (insn_idx >= insn_cnt) {
  2523. verbose("invalid insn idx %d insn_cnt %d\n",
  2524. insn_idx, insn_cnt);
  2525. return -EFAULT;
  2526. }
  2527. insn = &insns[insn_idx];
  2528. class = BPF_CLASS(insn->code);
  2529. if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
  2530. verbose("BPF program is too large. Processed %d insn\n",
  2531. insn_processed);
  2532. return -E2BIG;
  2533. }
  2534. err = is_state_visited(env, insn_idx);
  2535. if (err < 0)
  2536. return err;
  2537. if (err == 1) {
  2538. /* found equivalent state, can prune the search */
  2539. if (log_level) {
  2540. if (do_print_state)
  2541. verbose("\nfrom %d to %d: safe\n",
  2542. prev_insn_idx, insn_idx);
  2543. else
  2544. verbose("%d: safe\n", insn_idx);
  2545. }
  2546. goto process_bpf_exit;
  2547. }
  2548. if (log_level && do_print_state) {
  2549. verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
  2550. print_verifier_state(&env->cur_state);
  2551. do_print_state = false;
  2552. }
  2553. if (log_level) {
  2554. verbose("%d: ", insn_idx);
  2555. print_bpf_insn(insn);
  2556. }
  2557. err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
  2558. if (err)
  2559. return err;
  2560. if (class == BPF_ALU || class == BPF_ALU64) {
  2561. err = check_alu_op(env, insn);
  2562. if (err)
  2563. return err;
  2564. } else if (class == BPF_LDX) {
  2565. enum bpf_reg_type *prev_src_type, src_reg_type;
  2566. /* check for reserved fields is already done */
  2567. /* check src operand */
  2568. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2569. if (err)
  2570. return err;
  2571. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  2572. if (err)
  2573. return err;
  2574. src_reg_type = regs[insn->src_reg].type;
  2575. /* check that memory (src_reg + off) is readable,
  2576. * the state of dst_reg will be updated by this func
  2577. */
  2578. err = check_mem_access(env, insn->src_reg, insn->off,
  2579. BPF_SIZE(insn->code), BPF_READ,
  2580. insn->dst_reg);
  2581. if (err)
  2582. return err;
  2583. if (BPF_SIZE(insn->code) != BPF_W &&
  2584. BPF_SIZE(insn->code) != BPF_DW) {
  2585. insn_idx++;
  2586. continue;
  2587. }
  2588. prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
  2589. if (*prev_src_type == NOT_INIT) {
  2590. /* saw a valid insn
  2591. * dst_reg = *(u32 *)(src_reg + off)
  2592. * save type to validate intersecting paths
  2593. */
  2594. *prev_src_type = src_reg_type;
  2595. } else if (src_reg_type != *prev_src_type &&
  2596. (src_reg_type == PTR_TO_CTX ||
  2597. *prev_src_type == PTR_TO_CTX)) {
  2598. /* ABuser program is trying to use the same insn
  2599. * dst_reg = *(u32*) (src_reg + off)
  2600. * with different pointer types:
  2601. * src_reg == ctx in one branch and
  2602. * src_reg == stack|map in some other branch.
  2603. * Reject it.
  2604. */
  2605. verbose("same insn cannot be used with different pointers\n");
  2606. return -EINVAL;
  2607. }
  2608. } else if (class == BPF_STX) {
  2609. enum bpf_reg_type *prev_dst_type, dst_reg_type;
  2610. if (BPF_MODE(insn->code) == BPF_XADD) {
  2611. err = check_xadd(env, insn);
  2612. if (err)
  2613. return err;
  2614. insn_idx++;
  2615. continue;
  2616. }
  2617. /* check src1 operand */
  2618. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2619. if (err)
  2620. return err;
  2621. /* check src2 operand */
  2622. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2623. if (err)
  2624. return err;
  2625. dst_reg_type = regs[insn->dst_reg].type;
  2626. /* check that memory (dst_reg + off) is writeable */
  2627. err = check_mem_access(env, insn->dst_reg, insn->off,
  2628. BPF_SIZE(insn->code), BPF_WRITE,
  2629. insn->src_reg);
  2630. if (err)
  2631. return err;
  2632. prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
  2633. if (*prev_dst_type == NOT_INIT) {
  2634. *prev_dst_type = dst_reg_type;
  2635. } else if (dst_reg_type != *prev_dst_type &&
  2636. (dst_reg_type == PTR_TO_CTX ||
  2637. *prev_dst_type == PTR_TO_CTX)) {
  2638. verbose("same insn cannot be used with different pointers\n");
  2639. return -EINVAL;
  2640. }
  2641. } else if (class == BPF_ST) {
  2642. if (BPF_MODE(insn->code) != BPF_MEM ||
  2643. insn->src_reg != BPF_REG_0) {
  2644. verbose("BPF_ST uses reserved fields\n");
  2645. return -EINVAL;
  2646. }
  2647. /* check src operand */
  2648. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2649. if (err)
  2650. return err;
  2651. /* check that memory (dst_reg + off) is writeable */
  2652. err = check_mem_access(env, insn->dst_reg, insn->off,
  2653. BPF_SIZE(insn->code), BPF_WRITE,
  2654. -1);
  2655. if (err)
  2656. return err;
  2657. } else if (class == BPF_JMP) {
  2658. u8 opcode = BPF_OP(insn->code);
  2659. if (opcode == BPF_CALL) {
  2660. if (BPF_SRC(insn->code) != BPF_K ||
  2661. insn->off != 0 ||
  2662. insn->src_reg != BPF_REG_0 ||
  2663. insn->dst_reg != BPF_REG_0) {
  2664. verbose("BPF_CALL uses reserved fields\n");
  2665. return -EINVAL;
  2666. }
  2667. err = check_call(env, insn->imm);
  2668. if (err)
  2669. return err;
  2670. } else if (opcode == BPF_JA) {
  2671. if (BPF_SRC(insn->code) != BPF_K ||
  2672. insn->imm != 0 ||
  2673. insn->src_reg != BPF_REG_0 ||
  2674. insn->dst_reg != BPF_REG_0) {
  2675. verbose("BPF_JA uses reserved fields\n");
  2676. return -EINVAL;
  2677. }
  2678. insn_idx += insn->off + 1;
  2679. continue;
  2680. } else if (opcode == BPF_EXIT) {
  2681. if (BPF_SRC(insn->code) != BPF_K ||
  2682. insn->imm != 0 ||
  2683. insn->src_reg != BPF_REG_0 ||
  2684. insn->dst_reg != BPF_REG_0) {
  2685. verbose("BPF_EXIT uses reserved fields\n");
  2686. return -EINVAL;
  2687. }
  2688. /* eBPF calling convetion is such that R0 is used
  2689. * to return the value from eBPF program.
  2690. * Make sure that it's readable at this time
  2691. * of bpf_exit, which means that program wrote
  2692. * something into it earlier
  2693. */
  2694. err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
  2695. if (err)
  2696. return err;
  2697. if (is_pointer_value(env, BPF_REG_0)) {
  2698. verbose("R0 leaks addr as return value\n");
  2699. return -EACCES;
  2700. }
  2701. process_bpf_exit:
  2702. insn_idx = pop_stack(env, &prev_insn_idx);
  2703. if (insn_idx < 0) {
  2704. break;
  2705. } else {
  2706. do_print_state = true;
  2707. continue;
  2708. }
  2709. } else {
  2710. err = check_cond_jmp_op(env, insn, &insn_idx);
  2711. if (err)
  2712. return err;
  2713. }
  2714. } else if (class == BPF_LD) {
  2715. u8 mode = BPF_MODE(insn->code);
  2716. if (mode == BPF_ABS || mode == BPF_IND) {
  2717. err = check_ld_abs(env, insn);
  2718. if (err)
  2719. return err;
  2720. } else if (mode == BPF_IMM) {
  2721. err = check_ld_imm(env, insn);
  2722. if (err)
  2723. return err;
  2724. insn_idx++;
  2725. } else {
  2726. verbose("invalid BPF_LD mode\n");
  2727. return -EINVAL;
  2728. }
  2729. reset_reg_range_values(regs, insn->dst_reg);
  2730. } else {
  2731. verbose("unknown insn class %d\n", class);
  2732. return -EINVAL;
  2733. }
  2734. insn_idx++;
  2735. }
  2736. verbose("processed %d insns\n", insn_processed);
  2737. return 0;
  2738. }
  2739. static int check_map_prog_compatibility(struct bpf_map *map,
  2740. struct bpf_prog *prog)
  2741. {
  2742. if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
  2743. (map->map_type == BPF_MAP_TYPE_HASH ||
  2744. map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
  2745. (map->map_flags & BPF_F_NO_PREALLOC)) {
  2746. verbose("perf_event programs can only use preallocated hash map\n");
  2747. return -EINVAL;
  2748. }
  2749. return 0;
  2750. }
  2751. /* look for pseudo eBPF instructions that access map FDs and
  2752. * replace them with actual map pointers
  2753. */
  2754. static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
  2755. {
  2756. struct bpf_insn *insn = env->prog->insnsi;
  2757. int insn_cnt = env->prog->len;
  2758. int i, j, err;
  2759. err = bpf_prog_calc_tag(env->prog);
  2760. if (err)
  2761. return err;
  2762. for (i = 0; i < insn_cnt; i++, insn++) {
  2763. if (BPF_CLASS(insn->code) == BPF_LDX &&
  2764. (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
  2765. verbose("BPF_LDX uses reserved fields\n");
  2766. return -EINVAL;
  2767. }
  2768. if (BPF_CLASS(insn->code) == BPF_STX &&
  2769. ((BPF_MODE(insn->code) != BPF_MEM &&
  2770. BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
  2771. verbose("BPF_STX uses reserved fields\n");
  2772. return -EINVAL;
  2773. }
  2774. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  2775. struct bpf_map *map;
  2776. struct fd f;
  2777. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  2778. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  2779. insn[1].off != 0) {
  2780. verbose("invalid bpf_ld_imm64 insn\n");
  2781. return -EINVAL;
  2782. }
  2783. if (insn->src_reg == 0)
  2784. /* valid generic load 64-bit imm */
  2785. goto next_insn;
  2786. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  2787. verbose("unrecognized bpf_ld_imm64 insn\n");
  2788. return -EINVAL;
  2789. }
  2790. f = fdget(insn->imm);
  2791. map = __bpf_map_get(f);
  2792. if (IS_ERR(map)) {
  2793. verbose("fd %d is not pointing to valid bpf_map\n",
  2794. insn->imm);
  2795. return PTR_ERR(map);
  2796. }
  2797. err = check_map_prog_compatibility(map, env->prog);
  2798. if (err) {
  2799. fdput(f);
  2800. return err;
  2801. }
  2802. /* store map pointer inside BPF_LD_IMM64 instruction */
  2803. insn[0].imm = (u32) (unsigned long) map;
  2804. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  2805. /* check whether we recorded this map already */
  2806. for (j = 0; j < env->used_map_cnt; j++)
  2807. if (env->used_maps[j] == map) {
  2808. fdput(f);
  2809. goto next_insn;
  2810. }
  2811. if (env->used_map_cnt >= MAX_USED_MAPS) {
  2812. fdput(f);
  2813. return -E2BIG;
  2814. }
  2815. /* hold the map. If the program is rejected by verifier,
  2816. * the map will be released by release_maps() or it
  2817. * will be used by the valid program until it's unloaded
  2818. * and all maps are released in free_bpf_prog_info()
  2819. */
  2820. map = bpf_map_inc(map, false);
  2821. if (IS_ERR(map)) {
  2822. fdput(f);
  2823. return PTR_ERR(map);
  2824. }
  2825. env->used_maps[env->used_map_cnt++] = map;
  2826. fdput(f);
  2827. next_insn:
  2828. insn++;
  2829. i++;
  2830. }
  2831. }
  2832. /* now all pseudo BPF_LD_IMM64 instructions load valid
  2833. * 'struct bpf_map *' into a register instead of user map_fd.
  2834. * These pointers will be used later by verifier to validate map access.
  2835. */
  2836. return 0;
  2837. }
  2838. /* drop refcnt of maps used by the rejected program */
  2839. static void release_maps(struct bpf_verifier_env *env)
  2840. {
  2841. int i;
  2842. for (i = 0; i < env->used_map_cnt; i++)
  2843. bpf_map_put(env->used_maps[i]);
  2844. }
  2845. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  2846. static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
  2847. {
  2848. struct bpf_insn *insn = env->prog->insnsi;
  2849. int insn_cnt = env->prog->len;
  2850. int i;
  2851. for (i = 0; i < insn_cnt; i++, insn++)
  2852. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  2853. insn->src_reg = 0;
  2854. }
  2855. /* convert load instructions that access fields of 'struct __sk_buff'
  2856. * into sequence of instructions that access fields of 'struct sk_buff'
  2857. */
  2858. static int convert_ctx_accesses(struct bpf_verifier_env *env)
  2859. {
  2860. const struct bpf_verifier_ops *ops = env->prog->aux->ops;
  2861. const int insn_cnt = env->prog->len;
  2862. struct bpf_insn insn_buf[16], *insn;
  2863. struct bpf_prog *new_prog;
  2864. enum bpf_access_type type;
  2865. int i, cnt, delta = 0;
  2866. if (ops->gen_prologue) {
  2867. cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
  2868. env->prog);
  2869. if (cnt >= ARRAY_SIZE(insn_buf)) {
  2870. verbose("bpf verifier is misconfigured\n");
  2871. return -EINVAL;
  2872. } else if (cnt) {
  2873. new_prog = bpf_patch_insn_single(env->prog, 0,
  2874. insn_buf, cnt);
  2875. if (!new_prog)
  2876. return -ENOMEM;
  2877. env->prog = new_prog;
  2878. delta += cnt - 1;
  2879. }
  2880. }
  2881. if (!ops->convert_ctx_access)
  2882. return 0;
  2883. insn = env->prog->insnsi + delta;
  2884. for (i = 0; i < insn_cnt; i++, insn++) {
  2885. if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
  2886. insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
  2887. insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
  2888. insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
  2889. type = BPF_READ;
  2890. else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
  2891. insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
  2892. insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
  2893. insn->code == (BPF_STX | BPF_MEM | BPF_DW))
  2894. type = BPF_WRITE;
  2895. else
  2896. continue;
  2897. if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
  2898. continue;
  2899. cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog);
  2900. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
  2901. verbose("bpf verifier is misconfigured\n");
  2902. return -EINVAL;
  2903. }
  2904. new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
  2905. cnt);
  2906. if (!new_prog)
  2907. return -ENOMEM;
  2908. delta += cnt - 1;
  2909. /* keep walking new program and skip insns we just inserted */
  2910. env->prog = new_prog;
  2911. insn = new_prog->insnsi + i + delta;
  2912. }
  2913. return 0;
  2914. }
  2915. static void free_states(struct bpf_verifier_env *env)
  2916. {
  2917. struct bpf_verifier_state_list *sl, *sln;
  2918. int i;
  2919. if (!env->explored_states)
  2920. return;
  2921. for (i = 0; i < env->prog->len; i++) {
  2922. sl = env->explored_states[i];
  2923. if (sl)
  2924. while (sl != STATE_LIST_MARK) {
  2925. sln = sl->next;
  2926. kfree(sl);
  2927. sl = sln;
  2928. }
  2929. }
  2930. kfree(env->explored_states);
  2931. }
  2932. int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  2933. {
  2934. char __user *log_ubuf = NULL;
  2935. struct bpf_verifier_env *env;
  2936. int ret = -EINVAL;
  2937. /* 'struct bpf_verifier_env' can be global, but since it's not small,
  2938. * allocate/free it every time bpf_check() is called
  2939. */
  2940. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  2941. if (!env)
  2942. return -ENOMEM;
  2943. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  2944. (*prog)->len);
  2945. ret = -ENOMEM;
  2946. if (!env->insn_aux_data)
  2947. goto err_free_env;
  2948. env->prog = *prog;
  2949. /* grab the mutex to protect few globals used by verifier */
  2950. mutex_lock(&bpf_verifier_lock);
  2951. if (attr->log_level || attr->log_buf || attr->log_size) {
  2952. /* user requested verbose verifier output
  2953. * and supplied buffer to store the verification trace
  2954. */
  2955. log_level = attr->log_level;
  2956. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  2957. log_size = attr->log_size;
  2958. log_len = 0;
  2959. ret = -EINVAL;
  2960. /* log_* values have to be sane */
  2961. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  2962. log_level == 0 || log_ubuf == NULL)
  2963. goto err_unlock;
  2964. ret = -ENOMEM;
  2965. log_buf = vmalloc(log_size);
  2966. if (!log_buf)
  2967. goto err_unlock;
  2968. } else {
  2969. log_level = 0;
  2970. }
  2971. ret = replace_map_fd_with_map_ptr(env);
  2972. if (ret < 0)
  2973. goto skip_full_check;
  2974. env->explored_states = kcalloc(env->prog->len,
  2975. sizeof(struct bpf_verifier_state_list *),
  2976. GFP_USER);
  2977. ret = -ENOMEM;
  2978. if (!env->explored_states)
  2979. goto skip_full_check;
  2980. ret = check_cfg(env);
  2981. if (ret < 0)
  2982. goto skip_full_check;
  2983. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  2984. ret = do_check(env);
  2985. skip_full_check:
  2986. while (pop_stack(env, NULL) >= 0);
  2987. free_states(env);
  2988. if (ret == 0)
  2989. /* program is valid, convert *(u32*)(ctx + off) accesses */
  2990. ret = convert_ctx_accesses(env);
  2991. if (log_level && log_len >= log_size - 1) {
  2992. BUG_ON(log_len >= log_size);
  2993. /* verifier log exceeded user supplied buffer */
  2994. ret = -ENOSPC;
  2995. /* fall through to return what was recorded */
  2996. }
  2997. /* copy verifier log back to user space including trailing zero */
  2998. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  2999. ret = -EFAULT;
  3000. goto free_log_buf;
  3001. }
  3002. if (ret == 0 && env->used_map_cnt) {
  3003. /* if program passed verifier, update used_maps in bpf_prog_info */
  3004. env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  3005. sizeof(env->used_maps[0]),
  3006. GFP_KERNEL);
  3007. if (!env->prog->aux->used_maps) {
  3008. ret = -ENOMEM;
  3009. goto free_log_buf;
  3010. }
  3011. memcpy(env->prog->aux->used_maps, env->used_maps,
  3012. sizeof(env->used_maps[0]) * env->used_map_cnt);
  3013. env->prog->aux->used_map_cnt = env->used_map_cnt;
  3014. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  3015. * bpf_ld_imm64 instructions
  3016. */
  3017. convert_pseudo_ld_imm64(env);
  3018. }
  3019. free_log_buf:
  3020. if (log_level)
  3021. vfree(log_buf);
  3022. if (!env->prog->aux->used_maps)
  3023. /* if we didn't copy map pointers into bpf_prog_info, release
  3024. * them now. Otherwise free_bpf_prog_info() will release them.
  3025. */
  3026. release_maps(env);
  3027. *prog = env->prog;
  3028. err_unlock:
  3029. mutex_unlock(&bpf_verifier_lock);
  3030. vfree(env->insn_aux_data);
  3031. err_free_env:
  3032. kfree(env);
  3033. return ret;
  3034. }
  3035. int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
  3036. void *priv)
  3037. {
  3038. struct bpf_verifier_env *env;
  3039. int ret;
  3040. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  3041. if (!env)
  3042. return -ENOMEM;
  3043. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  3044. prog->len);
  3045. ret = -ENOMEM;
  3046. if (!env->insn_aux_data)
  3047. goto err_free_env;
  3048. env->prog = prog;
  3049. env->analyzer_ops = ops;
  3050. env->analyzer_priv = priv;
  3051. /* grab the mutex to protect few globals used by verifier */
  3052. mutex_lock(&bpf_verifier_lock);
  3053. log_level = 0;
  3054. env->explored_states = kcalloc(env->prog->len,
  3055. sizeof(struct bpf_verifier_state_list *),
  3056. GFP_KERNEL);
  3057. ret = -ENOMEM;
  3058. if (!env->explored_states)
  3059. goto skip_full_check;
  3060. ret = check_cfg(env);
  3061. if (ret < 0)
  3062. goto skip_full_check;
  3063. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3064. ret = do_check(env);
  3065. skip_full_check:
  3066. while (pop_stack(env, NULL) >= 0);
  3067. free_states(env);
  3068. mutex_unlock(&bpf_verifier_lock);
  3069. vfree(env->insn_aux_data);
  3070. err_free_env:
  3071. kfree(env);
  3072. return ret;
  3073. }
  3074. EXPORT_SYMBOL_GPL(bpf_analyzer);