verifier.c 104 KB

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