verifier.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479
  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 int check_ptr_alignment(struct bpf_verifier_env *env,
  642. struct bpf_reg_state *reg, int off, int size)
  643. {
  644. if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
  645. if (off % size != 0) {
  646. verbose("misaligned access off %d size %d\n",
  647. off, size);
  648. return -EACCES;
  649. } else {
  650. return 0;
  651. }
  652. }
  653. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  654. /* misaligned access to packet is ok on x86,arm,arm64 */
  655. return 0;
  656. if (reg->id && size != 1) {
  657. verbose("Unknown packet alignment. Only byte-sized access allowed\n");
  658. return -EACCES;
  659. }
  660. /* skb->data is NET_IP_ALIGN-ed */
  661. if (reg->type == PTR_TO_PACKET &&
  662. (NET_IP_ALIGN + reg->off + off) % size != 0) {
  663. verbose("misaligned packet access off %d+%d+%d size %d\n",
  664. NET_IP_ALIGN, reg->off, off, size);
  665. return -EACCES;
  666. }
  667. return 0;
  668. }
  669. /* check whether memory at (regno + off) is accessible for t = (read | write)
  670. * if t==write, value_regno is a register which value is stored into memory
  671. * if t==read, value_regno is a register which will receive the value from memory
  672. * if t==write && value_regno==-1, some unknown value is stored into memory
  673. * if t==read && value_regno==-1, don't care what we read from memory
  674. */
  675. static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
  676. int bpf_size, enum bpf_access_type t,
  677. int value_regno)
  678. {
  679. struct bpf_verifier_state *state = &env->cur_state;
  680. struct bpf_reg_state *reg = &state->regs[regno];
  681. int size, err = 0;
  682. if (reg->type == PTR_TO_STACK)
  683. off += reg->imm;
  684. size = bpf_size_to_bytes(bpf_size);
  685. if (size < 0)
  686. return size;
  687. err = check_ptr_alignment(env, reg, off, size);
  688. if (err)
  689. return err;
  690. if (reg->type == PTR_TO_MAP_VALUE ||
  691. reg->type == PTR_TO_MAP_VALUE_ADJ) {
  692. if (t == BPF_WRITE && value_regno >= 0 &&
  693. is_pointer_value(env, value_regno)) {
  694. verbose("R%d leaks addr into map\n", value_regno);
  695. return -EACCES;
  696. }
  697. /* If we adjusted the register to this map value at all then we
  698. * need to change off and size to min_value and max_value
  699. * respectively to make sure our theoretical access will be
  700. * safe.
  701. */
  702. if (reg->type == PTR_TO_MAP_VALUE_ADJ) {
  703. if (log_level)
  704. print_verifier_state(state);
  705. env->varlen_map_value_access = true;
  706. /* The minimum value is only important with signed
  707. * comparisons where we can't assume the floor of a
  708. * value is 0. If we are using signed variables for our
  709. * index'es we need to make sure that whatever we use
  710. * will have a set floor within our range.
  711. */
  712. if (reg->min_value < 0) {
  713. verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
  714. regno);
  715. return -EACCES;
  716. }
  717. err = check_map_access(env, regno, reg->min_value + off,
  718. size);
  719. if (err) {
  720. verbose("R%d min value is outside of the array range\n",
  721. regno);
  722. return err;
  723. }
  724. /* If we haven't set a max value then we need to bail
  725. * since we can't be sure we won't do bad things.
  726. */
  727. if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
  728. verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
  729. regno);
  730. return -EACCES;
  731. }
  732. off += reg->max_value;
  733. }
  734. err = check_map_access(env, regno, off, size);
  735. if (!err && t == BPF_READ && value_regno >= 0)
  736. mark_reg_unknown_value(state->regs, value_regno);
  737. } else if (reg->type == PTR_TO_CTX) {
  738. enum bpf_reg_type reg_type = UNKNOWN_VALUE;
  739. if (t == BPF_WRITE && value_regno >= 0 &&
  740. is_pointer_value(env, value_regno)) {
  741. verbose("R%d leaks addr into ctx\n", value_regno);
  742. return -EACCES;
  743. }
  744. err = check_ctx_access(env, off, size, t, &reg_type);
  745. if (!err && t == BPF_READ && value_regno >= 0) {
  746. mark_reg_unknown_value(state->regs, value_regno);
  747. /* note that reg.[id|off|range] == 0 */
  748. state->regs[value_regno].type = reg_type;
  749. }
  750. } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
  751. if (off >= 0 || off < -MAX_BPF_STACK) {
  752. verbose("invalid stack off=%d size=%d\n", off, size);
  753. return -EACCES;
  754. }
  755. if (t == BPF_WRITE) {
  756. if (!env->allow_ptr_leaks &&
  757. state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
  758. size != BPF_REG_SIZE) {
  759. verbose("attempt to corrupt spilled pointer on stack\n");
  760. return -EACCES;
  761. }
  762. err = check_stack_write(state, off, size, value_regno);
  763. } else {
  764. err = check_stack_read(state, off, size, value_regno);
  765. }
  766. } else if (state->regs[regno].type == PTR_TO_PACKET) {
  767. if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) {
  768. verbose("cannot write into packet\n");
  769. return -EACCES;
  770. }
  771. if (t == BPF_WRITE && value_regno >= 0 &&
  772. is_pointer_value(env, value_regno)) {
  773. verbose("R%d leaks addr into packet\n", value_regno);
  774. return -EACCES;
  775. }
  776. err = check_packet_access(env, regno, off, size);
  777. if (!err && t == BPF_READ && value_regno >= 0)
  778. mark_reg_unknown_value(state->regs, value_regno);
  779. } else {
  780. verbose("R%d invalid mem access '%s'\n",
  781. regno, reg_type_str[reg->type]);
  782. return -EACCES;
  783. }
  784. if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
  785. state->regs[value_regno].type == UNKNOWN_VALUE) {
  786. /* 1 or 2 byte load zero-extends, determine the number of
  787. * zero upper bits. Not doing it fo 4 byte load, since
  788. * such values cannot be added to ptr_to_packet anyway.
  789. */
  790. state->regs[value_regno].imm = 64 - size * 8;
  791. }
  792. return err;
  793. }
  794. static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
  795. {
  796. struct bpf_reg_state *regs = env->cur_state.regs;
  797. int err;
  798. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  799. insn->imm != 0) {
  800. verbose("BPF_XADD uses reserved fields\n");
  801. return -EINVAL;
  802. }
  803. /* check src1 operand */
  804. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  805. if (err)
  806. return err;
  807. /* check src2 operand */
  808. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  809. if (err)
  810. return err;
  811. if (is_pointer_value(env, insn->src_reg)) {
  812. verbose("R%d leaks addr into mem\n", insn->src_reg);
  813. return -EACCES;
  814. }
  815. /* check whether atomic_add can read the memory */
  816. err = check_mem_access(env, insn->dst_reg, insn->off,
  817. BPF_SIZE(insn->code), BPF_READ, -1);
  818. if (err)
  819. return err;
  820. /* check whether atomic_add can write into the same memory */
  821. return check_mem_access(env, insn->dst_reg, insn->off,
  822. BPF_SIZE(insn->code), BPF_WRITE, -1);
  823. }
  824. /* when register 'regno' is passed into function that will read 'access_size'
  825. * bytes from that pointer, make sure that it's within stack boundary
  826. * and all elements of stack are initialized
  827. */
  828. static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
  829. int access_size, bool zero_size_allowed,
  830. struct bpf_call_arg_meta *meta)
  831. {
  832. struct bpf_verifier_state *state = &env->cur_state;
  833. struct bpf_reg_state *regs = state->regs;
  834. int off, i;
  835. if (regs[regno].type != PTR_TO_STACK) {
  836. if (zero_size_allowed && access_size == 0 &&
  837. regs[regno].type == CONST_IMM &&
  838. regs[regno].imm == 0)
  839. return 0;
  840. verbose("R%d type=%s expected=%s\n", regno,
  841. reg_type_str[regs[regno].type],
  842. reg_type_str[PTR_TO_STACK]);
  843. return -EACCES;
  844. }
  845. off = regs[regno].imm;
  846. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  847. access_size <= 0) {
  848. verbose("invalid stack type R%d off=%d access_size=%d\n",
  849. regno, off, access_size);
  850. return -EACCES;
  851. }
  852. if (meta && meta->raw_mode) {
  853. meta->access_size = access_size;
  854. meta->regno = regno;
  855. return 0;
  856. }
  857. for (i = 0; i < access_size; i++) {
  858. if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
  859. verbose("invalid indirect read from stack off %d+%d size %d\n",
  860. off, i, access_size);
  861. return -EACCES;
  862. }
  863. }
  864. return 0;
  865. }
  866. static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
  867. enum bpf_arg_type arg_type,
  868. struct bpf_call_arg_meta *meta)
  869. {
  870. struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
  871. enum bpf_reg_type expected_type, type = reg->type;
  872. int err = 0;
  873. if (arg_type == ARG_DONTCARE)
  874. return 0;
  875. if (type == NOT_INIT) {
  876. verbose("R%d !read_ok\n", regno);
  877. return -EACCES;
  878. }
  879. if (arg_type == ARG_ANYTHING) {
  880. if (is_pointer_value(env, regno)) {
  881. verbose("R%d leaks addr into helper function\n", regno);
  882. return -EACCES;
  883. }
  884. return 0;
  885. }
  886. if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) {
  887. verbose("helper access to the packet is not allowed\n");
  888. return -EACCES;
  889. }
  890. if (arg_type == ARG_PTR_TO_MAP_KEY ||
  891. arg_type == ARG_PTR_TO_MAP_VALUE) {
  892. expected_type = PTR_TO_STACK;
  893. if (type != PTR_TO_PACKET && type != expected_type)
  894. goto err_type;
  895. } else if (arg_type == ARG_CONST_STACK_SIZE ||
  896. arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
  897. expected_type = CONST_IMM;
  898. if (type != expected_type)
  899. goto err_type;
  900. } else if (arg_type == ARG_CONST_MAP_PTR) {
  901. expected_type = CONST_PTR_TO_MAP;
  902. if (type != expected_type)
  903. goto err_type;
  904. } else if (arg_type == ARG_PTR_TO_CTX) {
  905. expected_type = PTR_TO_CTX;
  906. if (type != expected_type)
  907. goto err_type;
  908. } else if (arg_type == ARG_PTR_TO_STACK ||
  909. arg_type == ARG_PTR_TO_RAW_STACK) {
  910. expected_type = PTR_TO_STACK;
  911. /* One exception here. In case function allows for NULL to be
  912. * passed in as argument, it's a CONST_IMM type. Final test
  913. * happens during stack boundary checking.
  914. */
  915. if (type == CONST_IMM && reg->imm == 0)
  916. /* final test in check_stack_boundary() */;
  917. else if (type != PTR_TO_PACKET && type != expected_type)
  918. goto err_type;
  919. meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
  920. } else {
  921. verbose("unsupported arg_type %d\n", arg_type);
  922. return -EFAULT;
  923. }
  924. if (arg_type == ARG_CONST_MAP_PTR) {
  925. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  926. meta->map_ptr = reg->map_ptr;
  927. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  928. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  929. * check that [key, key + map->key_size) are within
  930. * stack limits and initialized
  931. */
  932. if (!meta->map_ptr) {
  933. /* in function declaration map_ptr must come before
  934. * map_key, so that it's verified and known before
  935. * we have to check map_key here. Otherwise it means
  936. * that kernel subsystem misconfigured verifier
  937. */
  938. verbose("invalid map_ptr to access map->key\n");
  939. return -EACCES;
  940. }
  941. if (type == PTR_TO_PACKET)
  942. err = check_packet_access(env, regno, 0,
  943. meta->map_ptr->key_size);
  944. else
  945. err = check_stack_boundary(env, regno,
  946. meta->map_ptr->key_size,
  947. false, NULL);
  948. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  949. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  950. * check [value, value + map->value_size) validity
  951. */
  952. if (!meta->map_ptr) {
  953. /* kernel subsystem misconfigured verifier */
  954. verbose("invalid map_ptr to access map->value\n");
  955. return -EACCES;
  956. }
  957. if (type == PTR_TO_PACKET)
  958. err = check_packet_access(env, regno, 0,
  959. meta->map_ptr->value_size);
  960. else
  961. err = check_stack_boundary(env, regno,
  962. meta->map_ptr->value_size,
  963. false, NULL);
  964. } else if (arg_type == ARG_CONST_STACK_SIZE ||
  965. arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
  966. bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
  967. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  968. * from stack pointer 'buf'. Check it
  969. * note: regno == len, regno - 1 == buf
  970. */
  971. if (regno == 0) {
  972. /* kernel subsystem misconfigured verifier */
  973. verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
  974. return -EACCES;
  975. }
  976. if (regs[regno - 1].type == PTR_TO_PACKET)
  977. err = check_packet_access(env, regno - 1, 0, reg->imm);
  978. else
  979. err = check_stack_boundary(env, regno - 1, reg->imm,
  980. zero_size_allowed, meta);
  981. }
  982. return err;
  983. err_type:
  984. verbose("R%d type=%s expected=%s\n", regno,
  985. reg_type_str[type], reg_type_str[expected_type]);
  986. return -EACCES;
  987. }
  988. static int check_map_func_compatibility(struct bpf_map *map, int func_id)
  989. {
  990. if (!map)
  991. return 0;
  992. /* We need a two way check, first is from map perspective ... */
  993. switch (map->map_type) {
  994. case BPF_MAP_TYPE_PROG_ARRAY:
  995. if (func_id != BPF_FUNC_tail_call)
  996. goto error;
  997. break;
  998. case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
  999. if (func_id != BPF_FUNC_perf_event_read &&
  1000. func_id != BPF_FUNC_perf_event_output)
  1001. goto error;
  1002. break;
  1003. case BPF_MAP_TYPE_STACK_TRACE:
  1004. if (func_id != BPF_FUNC_get_stackid)
  1005. goto error;
  1006. break;
  1007. case BPF_MAP_TYPE_CGROUP_ARRAY:
  1008. if (func_id != BPF_FUNC_skb_under_cgroup &&
  1009. func_id != BPF_FUNC_current_task_under_cgroup)
  1010. goto error;
  1011. break;
  1012. default:
  1013. break;
  1014. }
  1015. /* ... and second from the function itself. */
  1016. switch (func_id) {
  1017. case BPF_FUNC_tail_call:
  1018. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  1019. goto error;
  1020. break;
  1021. case BPF_FUNC_perf_event_read:
  1022. case BPF_FUNC_perf_event_output:
  1023. if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
  1024. goto error;
  1025. break;
  1026. case BPF_FUNC_get_stackid:
  1027. if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
  1028. goto error;
  1029. break;
  1030. case BPF_FUNC_current_task_under_cgroup:
  1031. case BPF_FUNC_skb_under_cgroup:
  1032. if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
  1033. goto error;
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. return 0;
  1039. error:
  1040. verbose("cannot pass map_type %d into func %d\n",
  1041. map->map_type, func_id);
  1042. return -EINVAL;
  1043. }
  1044. static int check_raw_mode(const struct bpf_func_proto *fn)
  1045. {
  1046. int count = 0;
  1047. if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
  1048. count++;
  1049. if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
  1050. count++;
  1051. if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
  1052. count++;
  1053. if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
  1054. count++;
  1055. if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
  1056. count++;
  1057. return count > 1 ? -EINVAL : 0;
  1058. }
  1059. static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
  1060. {
  1061. struct bpf_verifier_state *state = &env->cur_state;
  1062. struct bpf_reg_state *regs = state->regs, *reg;
  1063. int i;
  1064. for (i = 0; i < MAX_BPF_REG; i++)
  1065. if (regs[i].type == PTR_TO_PACKET ||
  1066. regs[i].type == PTR_TO_PACKET_END)
  1067. mark_reg_unknown_value(regs, i);
  1068. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1069. if (state->stack_slot_type[i] != STACK_SPILL)
  1070. continue;
  1071. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1072. if (reg->type != PTR_TO_PACKET &&
  1073. reg->type != PTR_TO_PACKET_END)
  1074. continue;
  1075. reg->type = UNKNOWN_VALUE;
  1076. reg->imm = 0;
  1077. }
  1078. }
  1079. static int check_call(struct bpf_verifier_env *env, int func_id)
  1080. {
  1081. struct bpf_verifier_state *state = &env->cur_state;
  1082. const struct bpf_func_proto *fn = NULL;
  1083. struct bpf_reg_state *regs = state->regs;
  1084. struct bpf_reg_state *reg;
  1085. struct bpf_call_arg_meta meta;
  1086. bool changes_data;
  1087. int i, err;
  1088. /* find function prototype */
  1089. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  1090. verbose("invalid func %d\n", func_id);
  1091. return -EINVAL;
  1092. }
  1093. if (env->prog->aux->ops->get_func_proto)
  1094. fn = env->prog->aux->ops->get_func_proto(func_id);
  1095. if (!fn) {
  1096. verbose("unknown func %d\n", func_id);
  1097. return -EINVAL;
  1098. }
  1099. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1100. if (!env->prog->gpl_compatible && fn->gpl_only) {
  1101. verbose("cannot call GPL only function from proprietary program\n");
  1102. return -EINVAL;
  1103. }
  1104. changes_data = bpf_helper_changes_skb_data(fn->func);
  1105. memset(&meta, 0, sizeof(meta));
  1106. meta.pkt_access = fn->pkt_access;
  1107. /* We only support one arg being in raw mode at the moment, which
  1108. * is sufficient for the helper functions we have right now.
  1109. */
  1110. err = check_raw_mode(fn);
  1111. if (err) {
  1112. verbose("kernel subsystem misconfigured func %d\n", func_id);
  1113. return err;
  1114. }
  1115. /* check args */
  1116. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
  1117. if (err)
  1118. return err;
  1119. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
  1120. if (err)
  1121. return err;
  1122. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
  1123. if (err)
  1124. return err;
  1125. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
  1126. if (err)
  1127. return err;
  1128. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
  1129. if (err)
  1130. return err;
  1131. /* Mark slots with STACK_MISC in case of raw mode, stack offset
  1132. * is inferred from register state.
  1133. */
  1134. for (i = 0; i < meta.access_size; i++) {
  1135. err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
  1136. if (err)
  1137. return err;
  1138. }
  1139. /* reset caller saved regs */
  1140. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  1141. reg = regs + caller_saved[i];
  1142. reg->type = NOT_INIT;
  1143. reg->imm = 0;
  1144. }
  1145. /* update return register */
  1146. if (fn->ret_type == RET_INTEGER) {
  1147. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  1148. } else if (fn->ret_type == RET_VOID) {
  1149. regs[BPF_REG_0].type = NOT_INIT;
  1150. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  1151. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  1152. regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
  1153. /* remember map_ptr, so that check_map_access()
  1154. * can check 'value_size' boundary of memory access
  1155. * to map element returned from bpf_map_lookup_elem()
  1156. */
  1157. if (meta.map_ptr == NULL) {
  1158. verbose("kernel subsystem misconfigured verifier\n");
  1159. return -EINVAL;
  1160. }
  1161. regs[BPF_REG_0].map_ptr = meta.map_ptr;
  1162. regs[BPF_REG_0].id = ++env->id_gen;
  1163. } else {
  1164. verbose("unknown return type %d of func %d\n",
  1165. fn->ret_type, func_id);
  1166. return -EINVAL;
  1167. }
  1168. err = check_map_func_compatibility(meta.map_ptr, func_id);
  1169. if (err)
  1170. return err;
  1171. if (changes_data)
  1172. clear_all_pkt_pointers(env);
  1173. return 0;
  1174. }
  1175. static int check_packet_ptr_add(struct bpf_verifier_env *env,
  1176. struct bpf_insn *insn)
  1177. {
  1178. struct bpf_reg_state *regs = env->cur_state.regs;
  1179. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1180. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1181. struct bpf_reg_state tmp_reg;
  1182. s32 imm;
  1183. if (BPF_SRC(insn->code) == BPF_K) {
  1184. /* pkt_ptr += imm */
  1185. imm = insn->imm;
  1186. add_imm:
  1187. if (imm <= 0) {
  1188. verbose("addition of negative constant to packet pointer is not allowed\n");
  1189. return -EACCES;
  1190. }
  1191. if (imm >= MAX_PACKET_OFF ||
  1192. imm + dst_reg->off >= MAX_PACKET_OFF) {
  1193. verbose("constant %d is too large to add to packet pointer\n",
  1194. imm);
  1195. return -EACCES;
  1196. }
  1197. /* a constant was added to pkt_ptr.
  1198. * Remember it while keeping the same 'id'
  1199. */
  1200. dst_reg->off += imm;
  1201. } else {
  1202. if (src_reg->type == PTR_TO_PACKET) {
  1203. /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
  1204. tmp_reg = *dst_reg; /* save r7 state */
  1205. *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
  1206. src_reg = &tmp_reg; /* pretend it's src_reg state */
  1207. /* if the checks below reject it, the copy won't matter,
  1208. * since we're rejecting the whole program. If all ok,
  1209. * then imm22 state will be added to r7
  1210. * and r7 will be pkt(id=0,off=22,r=62) while
  1211. * r6 will stay as pkt(id=0,off=0,r=62)
  1212. */
  1213. }
  1214. if (src_reg->type == CONST_IMM) {
  1215. /* pkt_ptr += reg where reg is known constant */
  1216. imm = src_reg->imm;
  1217. goto add_imm;
  1218. }
  1219. /* disallow pkt_ptr += reg
  1220. * if reg is not uknown_value with guaranteed zero upper bits
  1221. * otherwise pkt_ptr may overflow and addition will become
  1222. * subtraction which is not allowed
  1223. */
  1224. if (src_reg->type != UNKNOWN_VALUE) {
  1225. verbose("cannot add '%s' to ptr_to_packet\n",
  1226. reg_type_str[src_reg->type]);
  1227. return -EACCES;
  1228. }
  1229. if (src_reg->imm < 48) {
  1230. verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
  1231. src_reg->imm);
  1232. return -EACCES;
  1233. }
  1234. /* dst_reg stays as pkt_ptr type and since some positive
  1235. * integer value was added to the pointer, increment its 'id'
  1236. */
  1237. dst_reg->id = ++env->id_gen;
  1238. /* something was added to pkt_ptr, set range and off to zero */
  1239. dst_reg->off = 0;
  1240. dst_reg->range = 0;
  1241. }
  1242. return 0;
  1243. }
  1244. static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1245. {
  1246. struct bpf_reg_state *regs = env->cur_state.regs;
  1247. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1248. u8 opcode = BPF_OP(insn->code);
  1249. s64 imm_log2;
  1250. /* for type == UNKNOWN_VALUE:
  1251. * imm > 0 -> number of zero upper bits
  1252. * imm == 0 -> don't track which is the same as all bits can be non-zero
  1253. */
  1254. if (BPF_SRC(insn->code) == BPF_X) {
  1255. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1256. if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
  1257. dst_reg->imm && opcode == BPF_ADD) {
  1258. /* dreg += sreg
  1259. * where both have zero upper bits. Adding them
  1260. * can only result making one more bit non-zero
  1261. * in the larger value.
  1262. * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
  1263. * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
  1264. */
  1265. dst_reg->imm = min(dst_reg->imm, src_reg->imm);
  1266. dst_reg->imm--;
  1267. return 0;
  1268. }
  1269. if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
  1270. dst_reg->imm && opcode == BPF_ADD) {
  1271. /* dreg += sreg
  1272. * where dreg has zero upper bits and sreg is const.
  1273. * Adding them can only result making one more bit
  1274. * non-zero in the larger value.
  1275. */
  1276. imm_log2 = __ilog2_u64((long long)src_reg->imm);
  1277. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1278. dst_reg->imm--;
  1279. return 0;
  1280. }
  1281. /* all other cases non supported yet, just mark dst_reg */
  1282. dst_reg->imm = 0;
  1283. return 0;
  1284. }
  1285. /* sign extend 32-bit imm into 64-bit to make sure that
  1286. * negative values occupy bit 63. Note ilog2() would have
  1287. * been incorrect, since sizeof(insn->imm) == 4
  1288. */
  1289. imm_log2 = __ilog2_u64((long long)insn->imm);
  1290. if (dst_reg->imm && opcode == BPF_LSH) {
  1291. /* reg <<= imm
  1292. * if reg was a result of 2 byte load, then its imm == 48
  1293. * which means that upper 48 bits are zero and shifting this reg
  1294. * left by 4 would mean that upper 44 bits are still zero
  1295. */
  1296. dst_reg->imm -= insn->imm;
  1297. } else if (dst_reg->imm && opcode == BPF_MUL) {
  1298. /* reg *= imm
  1299. * if multiplying by 14 subtract 4
  1300. * This is conservative calculation of upper zero bits.
  1301. * It's not trying to special case insn->imm == 1 or 0 cases
  1302. */
  1303. dst_reg->imm -= imm_log2 + 1;
  1304. } else if (opcode == BPF_AND) {
  1305. /* reg &= imm */
  1306. dst_reg->imm = 63 - imm_log2;
  1307. } else if (dst_reg->imm && opcode == BPF_ADD) {
  1308. /* reg += imm */
  1309. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1310. dst_reg->imm--;
  1311. } else if (opcode == BPF_RSH) {
  1312. /* reg >>= imm
  1313. * which means that after right shift, upper bits will be zero
  1314. * note that verifier already checked that
  1315. * 0 <= imm < 64 for shift insn
  1316. */
  1317. dst_reg->imm += insn->imm;
  1318. if (unlikely(dst_reg->imm > 64))
  1319. /* some dumb code did:
  1320. * r2 = *(u32 *)mem;
  1321. * r2 >>= 32;
  1322. * and all bits are zero now */
  1323. dst_reg->imm = 64;
  1324. } else {
  1325. /* all other alu ops, means that we don't know what will
  1326. * happen to the value, mark it with unknown number of zero bits
  1327. */
  1328. dst_reg->imm = 0;
  1329. }
  1330. if (dst_reg->imm < 0) {
  1331. /* all 64 bits of the register can contain non-zero bits
  1332. * and such value cannot be added to ptr_to_packet, since it
  1333. * may overflow, mark it as unknown to avoid further eval
  1334. */
  1335. dst_reg->imm = 0;
  1336. }
  1337. return 0;
  1338. }
  1339. static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env *env,
  1340. struct bpf_insn *insn)
  1341. {
  1342. struct bpf_reg_state *regs = env->cur_state.regs;
  1343. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1344. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1345. u8 opcode = BPF_OP(insn->code);
  1346. s64 imm_log2 = __ilog2_u64((long long)dst_reg->imm);
  1347. /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
  1348. if (src_reg->imm > 0 && dst_reg->imm) {
  1349. switch (opcode) {
  1350. case BPF_ADD:
  1351. /* dreg += sreg
  1352. * where both have zero upper bits. Adding them
  1353. * can only result making one more bit non-zero
  1354. * in the larger value.
  1355. * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
  1356. * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
  1357. */
  1358. dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
  1359. dst_reg->imm--;
  1360. break;
  1361. case BPF_AND:
  1362. /* dreg &= sreg
  1363. * AND can not extend zero bits only shrink
  1364. * Ex. 0x00..00ffffff
  1365. * & 0x0f..ffffffff
  1366. * ----------------
  1367. * 0x00..00ffffff
  1368. */
  1369. dst_reg->imm = max(src_reg->imm, 63 - imm_log2);
  1370. break;
  1371. case BPF_OR:
  1372. /* dreg |= sreg
  1373. * OR can only extend zero bits
  1374. * Ex. 0x00..00ffffff
  1375. * | 0x0f..ffffffff
  1376. * ----------------
  1377. * 0x0f..00ffffff
  1378. */
  1379. dst_reg->imm = min(src_reg->imm, 63 - imm_log2);
  1380. break;
  1381. case BPF_SUB:
  1382. case BPF_MUL:
  1383. case BPF_RSH:
  1384. case BPF_LSH:
  1385. /* These may be flushed out later */
  1386. default:
  1387. mark_reg_unknown_value(regs, insn->dst_reg);
  1388. }
  1389. } else {
  1390. mark_reg_unknown_value(regs, insn->dst_reg);
  1391. }
  1392. dst_reg->type = UNKNOWN_VALUE;
  1393. return 0;
  1394. }
  1395. static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
  1396. struct bpf_insn *insn)
  1397. {
  1398. struct bpf_reg_state *regs = env->cur_state.regs;
  1399. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1400. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1401. u8 opcode = BPF_OP(insn->code);
  1402. if (BPF_SRC(insn->code) == BPF_X && src_reg->type == UNKNOWN_VALUE)
  1403. return evaluate_reg_imm_alu_unknown(env, insn);
  1404. /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
  1405. * Don't care about overflow or negative values, just add them
  1406. */
  1407. if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
  1408. dst_reg->imm += insn->imm;
  1409. else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
  1410. src_reg->type == CONST_IMM)
  1411. dst_reg->imm += src_reg->imm;
  1412. else
  1413. mark_reg_unknown_value(regs, insn->dst_reg);
  1414. return 0;
  1415. }
  1416. static void check_reg_overflow(struct bpf_reg_state *reg)
  1417. {
  1418. if (reg->max_value > BPF_REGISTER_MAX_RANGE)
  1419. reg->max_value = BPF_REGISTER_MAX_RANGE;
  1420. if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
  1421. reg->min_value > BPF_REGISTER_MAX_RANGE)
  1422. reg->min_value = BPF_REGISTER_MIN_RANGE;
  1423. }
  1424. static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
  1425. struct bpf_insn *insn)
  1426. {
  1427. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1428. s64 min_val = BPF_REGISTER_MIN_RANGE;
  1429. u64 max_val = BPF_REGISTER_MAX_RANGE;
  1430. bool min_set = false, max_set = false;
  1431. u8 opcode = BPF_OP(insn->code);
  1432. dst_reg = &regs[insn->dst_reg];
  1433. if (BPF_SRC(insn->code) == BPF_X) {
  1434. check_reg_overflow(&regs[insn->src_reg]);
  1435. min_val = regs[insn->src_reg].min_value;
  1436. max_val = regs[insn->src_reg].max_value;
  1437. /* If the source register is a random pointer then the
  1438. * min_value/max_value values represent the range of the known
  1439. * accesses into that value, not the actual min/max value of the
  1440. * register itself. In this case we have to reset the reg range
  1441. * values so we know it is not safe to look at.
  1442. */
  1443. if (regs[insn->src_reg].type != CONST_IMM &&
  1444. regs[insn->src_reg].type != UNKNOWN_VALUE) {
  1445. min_val = BPF_REGISTER_MIN_RANGE;
  1446. max_val = BPF_REGISTER_MAX_RANGE;
  1447. }
  1448. } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
  1449. (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
  1450. min_val = max_val = insn->imm;
  1451. min_set = max_set = true;
  1452. }
  1453. /* We don't know anything about what was done to this register, mark it
  1454. * as unknown. Also, if both derived bounds came from signed/unsigned
  1455. * mixed compares and one side is unbounded, we cannot really do anything
  1456. * with them as boundaries cannot be trusted. Thus, arithmetic of two
  1457. * regs of such kind will get invalidated bounds on the dst side.
  1458. */
  1459. if ((min_val == BPF_REGISTER_MIN_RANGE &&
  1460. max_val == BPF_REGISTER_MAX_RANGE) ||
  1461. (BPF_SRC(insn->code) == BPF_X &&
  1462. ((min_val != BPF_REGISTER_MIN_RANGE &&
  1463. max_val == BPF_REGISTER_MAX_RANGE) ||
  1464. (min_val == BPF_REGISTER_MIN_RANGE &&
  1465. max_val != BPF_REGISTER_MAX_RANGE) ||
  1466. (dst_reg->min_value != BPF_REGISTER_MIN_RANGE &&
  1467. dst_reg->max_value == BPF_REGISTER_MAX_RANGE) ||
  1468. (dst_reg->min_value == BPF_REGISTER_MIN_RANGE &&
  1469. dst_reg->max_value != BPF_REGISTER_MAX_RANGE)) &&
  1470. regs[insn->dst_reg].value_from_signed !=
  1471. regs[insn->src_reg].value_from_signed)) {
  1472. reset_reg_range_values(regs, insn->dst_reg);
  1473. return;
  1474. }
  1475. /* If one of our values was at the end of our ranges then we can't just
  1476. * do our normal operations to the register, we need to set the values
  1477. * to the min/max since they are undefined.
  1478. */
  1479. if (opcode != BPF_SUB) {
  1480. if (min_val == BPF_REGISTER_MIN_RANGE)
  1481. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1482. if (max_val == BPF_REGISTER_MAX_RANGE)
  1483. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1484. }
  1485. switch (opcode) {
  1486. case BPF_ADD:
  1487. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1488. dst_reg->min_value += min_val;
  1489. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1490. dst_reg->max_value += max_val;
  1491. break;
  1492. case BPF_SUB:
  1493. /* If one of our values was at the end of our ranges, then the
  1494. * _opposite_ value in the dst_reg goes to the end of our range.
  1495. */
  1496. if (min_val == BPF_REGISTER_MIN_RANGE)
  1497. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1498. if (max_val == BPF_REGISTER_MAX_RANGE)
  1499. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1500. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1501. dst_reg->min_value -= max_val;
  1502. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1503. dst_reg->max_value -= min_val;
  1504. break;
  1505. case BPF_MUL:
  1506. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1507. dst_reg->min_value *= min_val;
  1508. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1509. dst_reg->max_value *= max_val;
  1510. break;
  1511. case BPF_AND:
  1512. /* Disallow AND'ing of negative numbers, ain't nobody got time
  1513. * for that. Otherwise the minimum is 0 and the max is the max
  1514. * value we could AND against.
  1515. */
  1516. if (min_val < 0)
  1517. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1518. else
  1519. dst_reg->min_value = 0;
  1520. dst_reg->max_value = max_val;
  1521. break;
  1522. case BPF_LSH:
  1523. /* Gotta have special overflow logic here, if we're shifting
  1524. * more than MAX_RANGE then just assume we have an invalid
  1525. * range.
  1526. */
  1527. if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1528. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1529. else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1530. dst_reg->min_value <<= min_val;
  1531. if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1532. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1533. else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1534. dst_reg->max_value <<= max_val;
  1535. break;
  1536. case BPF_RSH:
  1537. /* RSH by a negative number is undefined, and the BPF_RSH is an
  1538. * unsigned shift, so make the appropriate casts.
  1539. */
  1540. if (min_val < 0 || dst_reg->min_value < 0)
  1541. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1542. else
  1543. dst_reg->min_value =
  1544. (u64)(dst_reg->min_value) >> min_val;
  1545. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1546. dst_reg->max_value >>= max_val;
  1547. break;
  1548. default:
  1549. reset_reg_range_values(regs, insn->dst_reg);
  1550. break;
  1551. }
  1552. check_reg_overflow(dst_reg);
  1553. }
  1554. /* check validity of 32-bit and 64-bit arithmetic operations */
  1555. static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1556. {
  1557. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1558. u8 opcode = BPF_OP(insn->code);
  1559. int err;
  1560. if (opcode == BPF_END || opcode == BPF_NEG) {
  1561. if (opcode == BPF_NEG) {
  1562. if (BPF_SRC(insn->code) != 0 ||
  1563. insn->src_reg != BPF_REG_0 ||
  1564. insn->off != 0 || insn->imm != 0) {
  1565. verbose("BPF_NEG uses reserved fields\n");
  1566. return -EINVAL;
  1567. }
  1568. } else {
  1569. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  1570. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
  1571. BPF_CLASS(insn->code) == BPF_ALU64) {
  1572. verbose("BPF_END uses reserved fields\n");
  1573. return -EINVAL;
  1574. }
  1575. }
  1576. /* check src operand */
  1577. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1578. if (err)
  1579. return err;
  1580. if (is_pointer_value(env, insn->dst_reg)) {
  1581. verbose("R%d pointer arithmetic prohibited\n",
  1582. insn->dst_reg);
  1583. return -EACCES;
  1584. }
  1585. /* check dest operand */
  1586. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1587. if (err)
  1588. return err;
  1589. } else if (opcode == BPF_MOV) {
  1590. if (BPF_SRC(insn->code) == BPF_X) {
  1591. if (insn->imm != 0 || insn->off != 0) {
  1592. verbose("BPF_MOV uses reserved fields\n");
  1593. return -EINVAL;
  1594. }
  1595. /* check src operand */
  1596. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1597. if (err)
  1598. return err;
  1599. } else {
  1600. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1601. verbose("BPF_MOV uses reserved fields\n");
  1602. return -EINVAL;
  1603. }
  1604. }
  1605. /* check dest operand */
  1606. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1607. if (err)
  1608. return err;
  1609. /* we are setting our register to something new, we need to
  1610. * reset its range values.
  1611. */
  1612. reset_reg_range_values(regs, insn->dst_reg);
  1613. if (BPF_SRC(insn->code) == BPF_X) {
  1614. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  1615. /* case: R1 = R2
  1616. * copy register state to dest reg
  1617. */
  1618. regs[insn->dst_reg] = regs[insn->src_reg];
  1619. } else {
  1620. if (is_pointer_value(env, insn->src_reg)) {
  1621. verbose("R%d partial copy of pointer\n",
  1622. insn->src_reg);
  1623. return -EACCES;
  1624. }
  1625. mark_reg_unknown_value(regs, insn->dst_reg);
  1626. }
  1627. } else {
  1628. /* case: R = imm
  1629. * remember the value we stored into this reg
  1630. */
  1631. regs[insn->dst_reg].type = CONST_IMM;
  1632. regs[insn->dst_reg].imm = insn->imm;
  1633. regs[insn->dst_reg].max_value = insn->imm;
  1634. regs[insn->dst_reg].min_value = insn->imm;
  1635. }
  1636. } else if (opcode > BPF_END) {
  1637. verbose("invalid BPF_ALU opcode %x\n", opcode);
  1638. return -EINVAL;
  1639. } else { /* all other ALU ops: and, sub, xor, add, ... */
  1640. if (BPF_SRC(insn->code) == BPF_X) {
  1641. if (insn->imm != 0 || insn->off != 0) {
  1642. verbose("BPF_ALU uses reserved fields\n");
  1643. return -EINVAL;
  1644. }
  1645. /* check src1 operand */
  1646. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1647. if (err)
  1648. return err;
  1649. } else {
  1650. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1651. verbose("BPF_ALU uses reserved fields\n");
  1652. return -EINVAL;
  1653. }
  1654. }
  1655. /* check src2 operand */
  1656. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1657. if (err)
  1658. return err;
  1659. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  1660. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  1661. verbose("div by zero\n");
  1662. return -EINVAL;
  1663. }
  1664. if ((opcode == BPF_LSH || opcode == BPF_RSH ||
  1665. opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
  1666. int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
  1667. if (insn->imm < 0 || insn->imm >= size) {
  1668. verbose("invalid shift %d\n", insn->imm);
  1669. return -EINVAL;
  1670. }
  1671. }
  1672. /* check dest operand */
  1673. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  1674. if (err)
  1675. return err;
  1676. dst_reg = &regs[insn->dst_reg];
  1677. /* first we want to adjust our ranges. */
  1678. adjust_reg_min_max_vals(env, insn);
  1679. /* pattern match 'bpf_add Rx, imm' instruction */
  1680. if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
  1681. dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
  1682. dst_reg->type = PTR_TO_STACK;
  1683. dst_reg->imm = insn->imm;
  1684. return 0;
  1685. } else if (opcode == BPF_ADD &&
  1686. BPF_CLASS(insn->code) == BPF_ALU64 &&
  1687. dst_reg->type == PTR_TO_STACK &&
  1688. ((BPF_SRC(insn->code) == BPF_X &&
  1689. regs[insn->src_reg].type == CONST_IMM) ||
  1690. BPF_SRC(insn->code) == BPF_K)) {
  1691. if (BPF_SRC(insn->code) == BPF_X)
  1692. dst_reg->imm += regs[insn->src_reg].imm;
  1693. else
  1694. dst_reg->imm += insn->imm;
  1695. return 0;
  1696. } else if (opcode == BPF_ADD &&
  1697. BPF_CLASS(insn->code) == BPF_ALU64 &&
  1698. (dst_reg->type == PTR_TO_PACKET ||
  1699. (BPF_SRC(insn->code) == BPF_X &&
  1700. regs[insn->src_reg].type == PTR_TO_PACKET))) {
  1701. /* ptr_to_packet += K|X */
  1702. return check_packet_ptr_add(env, insn);
  1703. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1704. dst_reg->type == UNKNOWN_VALUE &&
  1705. env->allow_ptr_leaks) {
  1706. /* unknown += K|X */
  1707. return evaluate_reg_alu(env, insn);
  1708. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1709. dst_reg->type == CONST_IMM &&
  1710. env->allow_ptr_leaks) {
  1711. /* reg_imm += K|X */
  1712. return evaluate_reg_imm_alu(env, insn);
  1713. } else if (is_pointer_value(env, insn->dst_reg)) {
  1714. verbose("R%d pointer arithmetic prohibited\n",
  1715. insn->dst_reg);
  1716. return -EACCES;
  1717. } else if (BPF_SRC(insn->code) == BPF_X &&
  1718. is_pointer_value(env, insn->src_reg)) {
  1719. verbose("R%d pointer arithmetic prohibited\n",
  1720. insn->src_reg);
  1721. return -EACCES;
  1722. }
  1723. /* If we did pointer math on a map value then just set it to our
  1724. * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
  1725. * loads to this register appropriately, otherwise just mark the
  1726. * register as unknown.
  1727. */
  1728. if (env->allow_ptr_leaks &&
  1729. BPF_CLASS(insn->code) == BPF_ALU64 && opcode == BPF_ADD &&
  1730. (dst_reg->type == PTR_TO_MAP_VALUE ||
  1731. dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
  1732. dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
  1733. else
  1734. mark_reg_unknown_value(regs, insn->dst_reg);
  1735. }
  1736. return 0;
  1737. }
  1738. static void find_good_pkt_pointers(struct bpf_verifier_state *state,
  1739. struct bpf_reg_state *dst_reg)
  1740. {
  1741. struct bpf_reg_state *regs = state->regs, *reg;
  1742. int i;
  1743. /* LLVM can generate two kind of checks:
  1744. *
  1745. * Type 1:
  1746. *
  1747. * r2 = r3;
  1748. * r2 += 8;
  1749. * if (r2 > pkt_end) goto <handle exception>
  1750. * <access okay>
  1751. *
  1752. * Where:
  1753. * r2 == dst_reg, pkt_end == src_reg
  1754. * r2=pkt(id=n,off=8,r=0)
  1755. * r3=pkt(id=n,off=0,r=0)
  1756. *
  1757. * Type 2:
  1758. *
  1759. * r2 = r3;
  1760. * r2 += 8;
  1761. * if (pkt_end >= r2) goto <access okay>
  1762. * <handle exception>
  1763. *
  1764. * Where:
  1765. * pkt_end == dst_reg, r2 == src_reg
  1766. * r2=pkt(id=n,off=8,r=0)
  1767. * r3=pkt(id=n,off=0,r=0)
  1768. *
  1769. * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
  1770. * so that range of bytes [r3, r3 + 8) is safe to access.
  1771. */
  1772. for (i = 0; i < MAX_BPF_REG; i++)
  1773. if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
  1774. /* keep the maximum range already checked */
  1775. regs[i].range = max(regs[i].range, dst_reg->off);
  1776. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1777. if (state->stack_slot_type[i] != STACK_SPILL)
  1778. continue;
  1779. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1780. if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
  1781. reg->range = max(reg->range, dst_reg->off);
  1782. }
  1783. }
  1784. /* Adjusts the register min/max values in the case that the dst_reg is the
  1785. * variable register that we are working on, and src_reg is a constant or we're
  1786. * simply doing a BPF_K check.
  1787. */
  1788. static void reg_set_min_max(struct bpf_reg_state *true_reg,
  1789. struct bpf_reg_state *false_reg, u64 val,
  1790. u8 opcode)
  1791. {
  1792. bool value_from_signed = true;
  1793. bool is_range = true;
  1794. switch (opcode) {
  1795. case BPF_JEQ:
  1796. /* If this is false then we know nothing Jon Snow, but if it is
  1797. * true then we know for sure.
  1798. */
  1799. true_reg->max_value = true_reg->min_value = val;
  1800. is_range = false;
  1801. break;
  1802. case BPF_JNE:
  1803. /* If this is true we know nothing Jon Snow, but if it is false
  1804. * we know the value for sure;
  1805. */
  1806. false_reg->max_value = false_reg->min_value = val;
  1807. is_range = false;
  1808. break;
  1809. case BPF_JGT:
  1810. value_from_signed = false;
  1811. /* fallthrough */
  1812. case BPF_JSGT:
  1813. if (true_reg->value_from_signed != value_from_signed)
  1814. reset_reg_range_values(true_reg, 0);
  1815. if (false_reg->value_from_signed != value_from_signed)
  1816. reset_reg_range_values(false_reg, 0);
  1817. if (opcode == BPF_JGT) {
  1818. /* Unsigned comparison, the minimum value is 0. */
  1819. false_reg->min_value = 0;
  1820. }
  1821. /* If this is false then we know the maximum val is val,
  1822. * otherwise we know the min val is val+1.
  1823. */
  1824. false_reg->max_value = val;
  1825. false_reg->value_from_signed = value_from_signed;
  1826. true_reg->min_value = val + 1;
  1827. true_reg->value_from_signed = value_from_signed;
  1828. break;
  1829. case BPF_JGE:
  1830. value_from_signed = false;
  1831. /* fallthrough */
  1832. case BPF_JSGE:
  1833. if (true_reg->value_from_signed != value_from_signed)
  1834. reset_reg_range_values(true_reg, 0);
  1835. if (false_reg->value_from_signed != value_from_signed)
  1836. reset_reg_range_values(false_reg, 0);
  1837. if (opcode == BPF_JGE) {
  1838. /* Unsigned comparison, the minimum value is 0. */
  1839. false_reg->min_value = 0;
  1840. }
  1841. /* If this is false then we know the maximum value is val - 1,
  1842. * otherwise we know the mimimum value is val.
  1843. */
  1844. false_reg->max_value = val - 1;
  1845. false_reg->value_from_signed = value_from_signed;
  1846. true_reg->min_value = val;
  1847. true_reg->value_from_signed = value_from_signed;
  1848. break;
  1849. default:
  1850. break;
  1851. }
  1852. check_reg_overflow(false_reg);
  1853. check_reg_overflow(true_reg);
  1854. if (is_range) {
  1855. if (__is_pointer_value(false, false_reg))
  1856. reset_reg_range_values(false_reg, 0);
  1857. if (__is_pointer_value(false, true_reg))
  1858. reset_reg_range_values(true_reg, 0);
  1859. }
  1860. }
  1861. /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
  1862. * is the variable reg.
  1863. */
  1864. static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
  1865. struct bpf_reg_state *false_reg, u64 val,
  1866. u8 opcode)
  1867. {
  1868. bool value_from_signed = true;
  1869. bool is_range = true;
  1870. switch (opcode) {
  1871. case BPF_JEQ:
  1872. /* If this is false then we know nothing Jon Snow, but if it is
  1873. * true then we know for sure.
  1874. */
  1875. true_reg->max_value = true_reg->min_value = val;
  1876. is_range = false;
  1877. break;
  1878. case BPF_JNE:
  1879. /* If this is true we know nothing Jon Snow, but if it is false
  1880. * we know the value for sure;
  1881. */
  1882. false_reg->max_value = false_reg->min_value = val;
  1883. is_range = false;
  1884. break;
  1885. case BPF_JGT:
  1886. value_from_signed = false;
  1887. /* fallthrough */
  1888. case BPF_JSGT:
  1889. if (true_reg->value_from_signed != value_from_signed)
  1890. reset_reg_range_values(true_reg, 0);
  1891. if (false_reg->value_from_signed != value_from_signed)
  1892. reset_reg_range_values(false_reg, 0);
  1893. if (opcode == BPF_JGT) {
  1894. /* Unsigned comparison, the minimum value is 0. */
  1895. true_reg->min_value = 0;
  1896. }
  1897. /*
  1898. * If this is false, then the val is <= the register, if it is
  1899. * true the register <= to the val.
  1900. */
  1901. false_reg->min_value = val;
  1902. false_reg->value_from_signed = value_from_signed;
  1903. true_reg->max_value = val - 1;
  1904. true_reg->value_from_signed = value_from_signed;
  1905. break;
  1906. case BPF_JGE:
  1907. value_from_signed = false;
  1908. /* fallthrough */
  1909. case BPF_JSGE:
  1910. if (true_reg->value_from_signed != value_from_signed)
  1911. reset_reg_range_values(true_reg, 0);
  1912. if (false_reg->value_from_signed != value_from_signed)
  1913. reset_reg_range_values(false_reg, 0);
  1914. if (opcode == BPF_JGE) {
  1915. /* Unsigned comparison, the minimum value is 0. */
  1916. true_reg->min_value = 0;
  1917. }
  1918. /* If this is false then constant < register, if it is true then
  1919. * the register < constant.
  1920. */
  1921. false_reg->min_value = val + 1;
  1922. false_reg->value_from_signed = value_from_signed;
  1923. true_reg->max_value = val;
  1924. true_reg->value_from_signed = value_from_signed;
  1925. break;
  1926. default:
  1927. break;
  1928. }
  1929. check_reg_overflow(false_reg);
  1930. check_reg_overflow(true_reg);
  1931. if (is_range) {
  1932. if (__is_pointer_value(false, false_reg))
  1933. reset_reg_range_values(false_reg, 0);
  1934. if (__is_pointer_value(false, true_reg))
  1935. reset_reg_range_values(true_reg, 0);
  1936. }
  1937. }
  1938. static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
  1939. enum bpf_reg_type type)
  1940. {
  1941. struct bpf_reg_state *reg = &regs[regno];
  1942. if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
  1943. reg->type = type;
  1944. /* We don't need id from this point onwards anymore, thus we
  1945. * should better reset it, so that state pruning has chances
  1946. * to take effect.
  1947. */
  1948. reg->id = 0;
  1949. if (type == UNKNOWN_VALUE)
  1950. __mark_reg_unknown_value(regs, regno);
  1951. }
  1952. }
  1953. /* The logic is similar to find_good_pkt_pointers(), both could eventually
  1954. * be folded together at some point.
  1955. */
  1956. static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
  1957. enum bpf_reg_type type)
  1958. {
  1959. struct bpf_reg_state *regs = state->regs;
  1960. u32 id = regs[regno].id;
  1961. int i;
  1962. for (i = 0; i < MAX_BPF_REG; i++)
  1963. mark_map_reg(regs, i, id, type);
  1964. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1965. if (state->stack_slot_type[i] != STACK_SPILL)
  1966. continue;
  1967. mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
  1968. }
  1969. }
  1970. static int check_cond_jmp_op(struct bpf_verifier_env *env,
  1971. struct bpf_insn *insn, int *insn_idx)
  1972. {
  1973. struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
  1974. struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
  1975. u8 opcode = BPF_OP(insn->code);
  1976. int err;
  1977. if (opcode > BPF_EXIT) {
  1978. verbose("invalid BPF_JMP opcode %x\n", opcode);
  1979. return -EINVAL;
  1980. }
  1981. if (BPF_SRC(insn->code) == BPF_X) {
  1982. if (insn->imm != 0) {
  1983. verbose("BPF_JMP uses reserved fields\n");
  1984. return -EINVAL;
  1985. }
  1986. /* check src1 operand */
  1987. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1988. if (err)
  1989. return err;
  1990. if (is_pointer_value(env, insn->src_reg)) {
  1991. verbose("R%d pointer comparison prohibited\n",
  1992. insn->src_reg);
  1993. return -EACCES;
  1994. }
  1995. } else {
  1996. if (insn->src_reg != BPF_REG_0) {
  1997. verbose("BPF_JMP uses reserved fields\n");
  1998. return -EINVAL;
  1999. }
  2000. }
  2001. /* check src2 operand */
  2002. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2003. if (err)
  2004. return err;
  2005. dst_reg = &regs[insn->dst_reg];
  2006. /* detect if R == 0 where R was initialized to zero earlier */
  2007. if (BPF_SRC(insn->code) == BPF_K &&
  2008. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2009. dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
  2010. if (opcode == BPF_JEQ) {
  2011. /* if (imm == imm) goto pc+off;
  2012. * only follow the goto, ignore fall-through
  2013. */
  2014. *insn_idx += insn->off;
  2015. return 0;
  2016. } else {
  2017. /* if (imm != imm) goto pc+off;
  2018. * only follow fall-through branch, since
  2019. * that's where the program will go
  2020. */
  2021. return 0;
  2022. }
  2023. }
  2024. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  2025. if (!other_branch)
  2026. return -EFAULT;
  2027. /* detect if we are comparing against a constant value so we can adjust
  2028. * our min/max values for our dst register.
  2029. */
  2030. if (BPF_SRC(insn->code) == BPF_X) {
  2031. if (regs[insn->src_reg].type == CONST_IMM)
  2032. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2033. dst_reg, regs[insn->src_reg].imm,
  2034. opcode);
  2035. else if (dst_reg->type == CONST_IMM)
  2036. reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
  2037. &regs[insn->src_reg], dst_reg->imm,
  2038. opcode);
  2039. } else {
  2040. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2041. dst_reg, insn->imm, opcode);
  2042. }
  2043. /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
  2044. if (BPF_SRC(insn->code) == BPF_K &&
  2045. insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2046. dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
  2047. /* Mark all identical map registers in each branch as either
  2048. * safe or unknown depending R == 0 or R != 0 conditional.
  2049. */
  2050. mark_map_regs(this_branch, insn->dst_reg,
  2051. opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
  2052. mark_map_regs(other_branch, insn->dst_reg,
  2053. opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
  2054. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
  2055. dst_reg->type == PTR_TO_PACKET &&
  2056. regs[insn->src_reg].type == PTR_TO_PACKET_END) {
  2057. find_good_pkt_pointers(this_branch, dst_reg);
  2058. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
  2059. dst_reg->type == PTR_TO_PACKET_END &&
  2060. regs[insn->src_reg].type == PTR_TO_PACKET) {
  2061. find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
  2062. } else if (is_pointer_value(env, insn->dst_reg)) {
  2063. verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
  2064. return -EACCES;
  2065. }
  2066. if (log_level)
  2067. print_verifier_state(this_branch);
  2068. return 0;
  2069. }
  2070. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  2071. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  2072. {
  2073. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  2074. return (struct bpf_map *) (unsigned long) imm64;
  2075. }
  2076. /* verify BPF_LD_IMM64 instruction */
  2077. static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2078. {
  2079. struct bpf_reg_state *regs = env->cur_state.regs;
  2080. int err;
  2081. if (BPF_SIZE(insn->code) != BPF_DW) {
  2082. verbose("invalid BPF_LD_IMM insn\n");
  2083. return -EINVAL;
  2084. }
  2085. if (insn->off != 0) {
  2086. verbose("BPF_LD_IMM64 uses reserved fields\n");
  2087. return -EINVAL;
  2088. }
  2089. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  2090. if (err)
  2091. return err;
  2092. if (insn->src_reg == 0) {
  2093. /* generic move 64-bit immediate into a register,
  2094. * only analyzer needs to collect the ld_imm value.
  2095. */
  2096. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  2097. if (!env->analyzer_ops)
  2098. return 0;
  2099. regs[insn->dst_reg].type = CONST_IMM;
  2100. regs[insn->dst_reg].imm = imm;
  2101. return 0;
  2102. }
  2103. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  2104. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  2105. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  2106. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  2107. return 0;
  2108. }
  2109. static bool may_access_skb(enum bpf_prog_type type)
  2110. {
  2111. switch (type) {
  2112. case BPF_PROG_TYPE_SOCKET_FILTER:
  2113. case BPF_PROG_TYPE_SCHED_CLS:
  2114. case BPF_PROG_TYPE_SCHED_ACT:
  2115. return true;
  2116. default:
  2117. return false;
  2118. }
  2119. }
  2120. /* verify safety of LD_ABS|LD_IND instructions:
  2121. * - they can only appear in the programs where ctx == skb
  2122. * - since they are wrappers of function calls, they scratch R1-R5 registers,
  2123. * preserve R6-R9, and store return value into R0
  2124. *
  2125. * Implicit input:
  2126. * ctx == skb == R6 == CTX
  2127. *
  2128. * Explicit input:
  2129. * SRC == any register
  2130. * IMM == 32-bit immediate
  2131. *
  2132. * Output:
  2133. * R0 - 8/16/32-bit skb data converted to cpu endianness
  2134. */
  2135. static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2136. {
  2137. struct bpf_reg_state *regs = env->cur_state.regs;
  2138. u8 mode = BPF_MODE(insn->code);
  2139. struct bpf_reg_state *reg;
  2140. int i, err;
  2141. if (!may_access_skb(env->prog->type)) {
  2142. verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
  2143. return -EINVAL;
  2144. }
  2145. if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
  2146. BPF_SIZE(insn->code) == BPF_DW ||
  2147. (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
  2148. verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
  2149. return -EINVAL;
  2150. }
  2151. /* check whether implicit source operand (register R6) is readable */
  2152. err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
  2153. if (err)
  2154. return err;
  2155. if (regs[BPF_REG_6].type != PTR_TO_CTX) {
  2156. verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
  2157. return -EINVAL;
  2158. }
  2159. if (mode == BPF_IND) {
  2160. /* check explicit source operand */
  2161. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2162. if (err)
  2163. return err;
  2164. }
  2165. /* reset caller saved regs to unreadable */
  2166. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  2167. reg = regs + caller_saved[i];
  2168. reg->type = NOT_INIT;
  2169. reg->imm = 0;
  2170. }
  2171. /* mark destination R0 register as readable, since it contains
  2172. * the value fetched from the packet
  2173. */
  2174. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  2175. return 0;
  2176. }
  2177. /* non-recursive DFS pseudo code
  2178. * 1 procedure DFS-iterative(G,v):
  2179. * 2 label v as discovered
  2180. * 3 let S be a stack
  2181. * 4 S.push(v)
  2182. * 5 while S is not empty
  2183. * 6 t <- S.pop()
  2184. * 7 if t is what we're looking for:
  2185. * 8 return t
  2186. * 9 for all edges e in G.adjacentEdges(t) do
  2187. * 10 if edge e is already labelled
  2188. * 11 continue with the next edge
  2189. * 12 w <- G.adjacentVertex(t,e)
  2190. * 13 if vertex w is not discovered and not explored
  2191. * 14 label e as tree-edge
  2192. * 15 label w as discovered
  2193. * 16 S.push(w)
  2194. * 17 continue at 5
  2195. * 18 else if vertex w is discovered
  2196. * 19 label e as back-edge
  2197. * 20 else
  2198. * 21 // vertex w is explored
  2199. * 22 label e as forward- or cross-edge
  2200. * 23 label t as explored
  2201. * 24 S.pop()
  2202. *
  2203. * convention:
  2204. * 0x10 - discovered
  2205. * 0x11 - discovered and fall-through edge labelled
  2206. * 0x12 - discovered and fall-through and branch edges labelled
  2207. * 0x20 - explored
  2208. */
  2209. enum {
  2210. DISCOVERED = 0x10,
  2211. EXPLORED = 0x20,
  2212. FALLTHROUGH = 1,
  2213. BRANCH = 2,
  2214. };
  2215. #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
  2216. static int *insn_stack; /* stack of insns to process */
  2217. static int cur_stack; /* current stack index */
  2218. static int *insn_state;
  2219. /* t, w, e - match pseudo-code above:
  2220. * t - index of current instruction
  2221. * w - next instruction
  2222. * e - edge
  2223. */
  2224. static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
  2225. {
  2226. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  2227. return 0;
  2228. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  2229. return 0;
  2230. if (w < 0 || w >= env->prog->len) {
  2231. verbose("jump out of range from insn %d to %d\n", t, w);
  2232. return -EINVAL;
  2233. }
  2234. if (e == BRANCH)
  2235. /* mark branch target for state pruning */
  2236. env->explored_states[w] = STATE_LIST_MARK;
  2237. if (insn_state[w] == 0) {
  2238. /* tree-edge */
  2239. insn_state[t] = DISCOVERED | e;
  2240. insn_state[w] = DISCOVERED;
  2241. if (cur_stack >= env->prog->len)
  2242. return -E2BIG;
  2243. insn_stack[cur_stack++] = w;
  2244. return 1;
  2245. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  2246. verbose("back-edge from insn %d to %d\n", t, w);
  2247. return -EINVAL;
  2248. } else if (insn_state[w] == EXPLORED) {
  2249. /* forward- or cross-edge */
  2250. insn_state[t] = DISCOVERED | e;
  2251. } else {
  2252. verbose("insn state internal bug\n");
  2253. return -EFAULT;
  2254. }
  2255. return 0;
  2256. }
  2257. /* non-recursive depth-first-search to detect loops in BPF program
  2258. * loop == back-edge in directed graph
  2259. */
  2260. static int check_cfg(struct bpf_verifier_env *env)
  2261. {
  2262. struct bpf_insn *insns = env->prog->insnsi;
  2263. int insn_cnt = env->prog->len;
  2264. int ret = 0;
  2265. int i, t;
  2266. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2267. if (!insn_state)
  2268. return -ENOMEM;
  2269. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2270. if (!insn_stack) {
  2271. kfree(insn_state);
  2272. return -ENOMEM;
  2273. }
  2274. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  2275. insn_stack[0] = 0; /* 0 is the first instruction */
  2276. cur_stack = 1;
  2277. peek_stack:
  2278. if (cur_stack == 0)
  2279. goto check_state;
  2280. t = insn_stack[cur_stack - 1];
  2281. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  2282. u8 opcode = BPF_OP(insns[t].code);
  2283. if (opcode == BPF_EXIT) {
  2284. goto mark_explored;
  2285. } else if (opcode == BPF_CALL) {
  2286. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2287. if (ret == 1)
  2288. goto peek_stack;
  2289. else if (ret < 0)
  2290. goto err_free;
  2291. if (t + 1 < insn_cnt)
  2292. env->explored_states[t + 1] = STATE_LIST_MARK;
  2293. } else if (opcode == BPF_JA) {
  2294. if (BPF_SRC(insns[t].code) != BPF_K) {
  2295. ret = -EINVAL;
  2296. goto err_free;
  2297. }
  2298. /* unconditional jump with single edge */
  2299. ret = push_insn(t, t + insns[t].off + 1,
  2300. FALLTHROUGH, env);
  2301. if (ret == 1)
  2302. goto peek_stack;
  2303. else if (ret < 0)
  2304. goto err_free;
  2305. /* tell verifier to check for equivalent states
  2306. * after every call and jump
  2307. */
  2308. if (t + 1 < insn_cnt)
  2309. env->explored_states[t + 1] = STATE_LIST_MARK;
  2310. } else {
  2311. /* conditional jump with two edges */
  2312. env->explored_states[t] = STATE_LIST_MARK;
  2313. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2314. if (ret == 1)
  2315. goto peek_stack;
  2316. else if (ret < 0)
  2317. goto err_free;
  2318. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  2319. if (ret == 1)
  2320. goto peek_stack;
  2321. else if (ret < 0)
  2322. goto err_free;
  2323. }
  2324. } else {
  2325. /* all other non-branch instructions with single
  2326. * fall-through edge
  2327. */
  2328. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2329. if (ret == 1)
  2330. goto peek_stack;
  2331. else if (ret < 0)
  2332. goto err_free;
  2333. }
  2334. mark_explored:
  2335. insn_state[t] = EXPLORED;
  2336. if (cur_stack-- <= 0) {
  2337. verbose("pop stack internal bug\n");
  2338. ret = -EFAULT;
  2339. goto err_free;
  2340. }
  2341. goto peek_stack;
  2342. check_state:
  2343. for (i = 0; i < insn_cnt; i++) {
  2344. if (insn_state[i] != EXPLORED) {
  2345. verbose("unreachable insn %d\n", i);
  2346. ret = -EINVAL;
  2347. goto err_free;
  2348. }
  2349. }
  2350. ret = 0; /* cfg looks good */
  2351. err_free:
  2352. kfree(insn_state);
  2353. kfree(insn_stack);
  2354. return ret;
  2355. }
  2356. /* the following conditions reduce the number of explored insns
  2357. * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
  2358. */
  2359. static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
  2360. struct bpf_reg_state *cur)
  2361. {
  2362. if (old->id != cur->id)
  2363. return false;
  2364. /* old ptr_to_packet is more conservative, since it allows smaller
  2365. * range. Ex:
  2366. * old(off=0,r=10) is equal to cur(off=0,r=20), because
  2367. * old(off=0,r=10) means that with range=10 the verifier proceeded
  2368. * further and found no issues with the program. Now we're in the same
  2369. * spot with cur(off=0,r=20), so we're safe too, since anything further
  2370. * will only be looking at most 10 bytes after this pointer.
  2371. */
  2372. if (old->off == cur->off && old->range < cur->range)
  2373. return true;
  2374. /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
  2375. * since both cannot be used for packet access and safe(old)
  2376. * pointer has smaller off that could be used for further
  2377. * 'if (ptr > data_end)' check
  2378. * Ex:
  2379. * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
  2380. * that we cannot access the packet.
  2381. * The safe range is:
  2382. * [ptr, ptr + range - off)
  2383. * so whenever off >=range, it means no safe bytes from this pointer.
  2384. * When comparing old->off <= cur->off, it means that older code
  2385. * went with smaller offset and that offset was later
  2386. * used to figure out the safe range after 'if (ptr > data_end)' check
  2387. * Say, 'old' state was explored like:
  2388. * ... R3(off=0, r=0)
  2389. * R4 = R3 + 20
  2390. * ... now R4(off=20,r=0) <-- here
  2391. * if (R4 > data_end)
  2392. * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
  2393. * ... the code further went all the way to bpf_exit.
  2394. * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
  2395. * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
  2396. * goes further, such cur_R4 will give larger safe packet range after
  2397. * 'if (R4 > data_end)' and all further insn were already good with r=20,
  2398. * so they will be good with r=30 and we can prune the search.
  2399. */
  2400. if (old->off <= cur->off &&
  2401. old->off >= old->range && cur->off >= cur->range)
  2402. return true;
  2403. return false;
  2404. }
  2405. /* compare two verifier states
  2406. *
  2407. * all states stored in state_list are known to be valid, since
  2408. * verifier reached 'bpf_exit' instruction through them
  2409. *
  2410. * this function is called when verifier exploring different branches of
  2411. * execution popped from the state stack. If it sees an old state that has
  2412. * more strict register state and more strict stack state then this execution
  2413. * branch doesn't need to be explored further, since verifier already
  2414. * concluded that more strict state leads to valid finish.
  2415. *
  2416. * Therefore two states are equivalent if register state is more conservative
  2417. * and explored stack state is more conservative than the current one.
  2418. * Example:
  2419. * explored current
  2420. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  2421. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  2422. *
  2423. * In other words if current stack state (one being explored) has more
  2424. * valid slots than old one that already passed validation, it means
  2425. * the verifier can stop exploring and conclude that current state is valid too
  2426. *
  2427. * Similarly with registers. If explored state has register type as invalid
  2428. * whereas register type in current state is meaningful, it means that
  2429. * the current state will reach 'bpf_exit' instruction safely
  2430. */
  2431. static bool states_equal(struct bpf_verifier_env *env,
  2432. struct bpf_verifier_state *old,
  2433. struct bpf_verifier_state *cur)
  2434. {
  2435. bool varlen_map_access = env->varlen_map_value_access;
  2436. struct bpf_reg_state *rold, *rcur;
  2437. int i;
  2438. for (i = 0; i < MAX_BPF_REG; i++) {
  2439. rold = &old->regs[i];
  2440. rcur = &cur->regs[i];
  2441. if (memcmp(rold, rcur, sizeof(*rold)) == 0)
  2442. continue;
  2443. /* If the ranges were not the same, but everything else was and
  2444. * we didn't do a variable access into a map then we are a-ok.
  2445. */
  2446. if (!varlen_map_access &&
  2447. memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
  2448. continue;
  2449. /* If we didn't map access then again we don't care about the
  2450. * mismatched range values and it's ok if our old type was
  2451. * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
  2452. */
  2453. if (rold->type == NOT_INIT ||
  2454. (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
  2455. rcur->type != NOT_INIT))
  2456. continue;
  2457. /* Don't care about the reg->id in this case. */
  2458. if (rold->type == PTR_TO_MAP_VALUE_OR_NULL &&
  2459. rcur->type == PTR_TO_MAP_VALUE_OR_NULL &&
  2460. rold->map_ptr == rcur->map_ptr)
  2461. continue;
  2462. if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
  2463. compare_ptrs_to_packet(rold, rcur))
  2464. continue;
  2465. return false;
  2466. }
  2467. for (i = 0; i < MAX_BPF_STACK; i++) {
  2468. if (old->stack_slot_type[i] == STACK_INVALID)
  2469. continue;
  2470. if (old->stack_slot_type[i] != cur->stack_slot_type[i])
  2471. /* Ex: old explored (safe) state has STACK_SPILL in
  2472. * this stack slot, but current has has STACK_MISC ->
  2473. * this verifier states are not equivalent,
  2474. * return false to continue verification of this path
  2475. */
  2476. return false;
  2477. if (i % BPF_REG_SIZE)
  2478. continue;
  2479. if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
  2480. &cur->spilled_regs[i / BPF_REG_SIZE],
  2481. sizeof(old->spilled_regs[0])))
  2482. /* when explored and current stack slot types are
  2483. * the same, check that stored pointers types
  2484. * are the same as well.
  2485. * Ex: explored safe path could have stored
  2486. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
  2487. * but current path has stored:
  2488. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
  2489. * such verifier states are not equivalent.
  2490. * return false to continue verification of this path
  2491. */
  2492. return false;
  2493. else
  2494. continue;
  2495. }
  2496. return true;
  2497. }
  2498. static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
  2499. {
  2500. struct bpf_verifier_state_list *new_sl;
  2501. struct bpf_verifier_state_list *sl;
  2502. sl = env->explored_states[insn_idx];
  2503. if (!sl)
  2504. /* this 'insn_idx' instruction wasn't marked, so we will not
  2505. * be doing state search here
  2506. */
  2507. return 0;
  2508. while (sl != STATE_LIST_MARK) {
  2509. if (states_equal(env, &sl->state, &env->cur_state))
  2510. /* reached equivalent register/stack state,
  2511. * prune the search
  2512. */
  2513. return 1;
  2514. sl = sl->next;
  2515. }
  2516. /* there were no equivalent states, remember current one.
  2517. * technically the current state is not proven to be safe yet,
  2518. * but it will either reach bpf_exit (which means it's safe) or
  2519. * it will be rejected. Since there are no loops, we won't be
  2520. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  2521. */
  2522. new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
  2523. if (!new_sl)
  2524. return -ENOMEM;
  2525. /* add new state to the head of linked list */
  2526. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  2527. new_sl->next = env->explored_states[insn_idx];
  2528. env->explored_states[insn_idx] = new_sl;
  2529. return 0;
  2530. }
  2531. static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
  2532. int insn_idx, int prev_insn_idx)
  2533. {
  2534. if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
  2535. return 0;
  2536. return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
  2537. }
  2538. static int do_check(struct bpf_verifier_env *env)
  2539. {
  2540. struct bpf_verifier_state *state = &env->cur_state;
  2541. struct bpf_insn *insns = env->prog->insnsi;
  2542. struct bpf_reg_state *regs = state->regs;
  2543. int insn_cnt = env->prog->len;
  2544. int insn_idx, prev_insn_idx = 0;
  2545. int insn_processed = 0;
  2546. bool do_print_state = false;
  2547. init_reg_state(regs);
  2548. insn_idx = 0;
  2549. env->varlen_map_value_access = false;
  2550. for (;;) {
  2551. struct bpf_insn *insn;
  2552. u8 class;
  2553. int err;
  2554. if (insn_idx >= insn_cnt) {
  2555. verbose("invalid insn idx %d insn_cnt %d\n",
  2556. insn_idx, insn_cnt);
  2557. return -EFAULT;
  2558. }
  2559. insn = &insns[insn_idx];
  2560. class = BPF_CLASS(insn->code);
  2561. if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
  2562. verbose("BPF program is too large. Proccessed %d insn\n",
  2563. insn_processed);
  2564. return -E2BIG;
  2565. }
  2566. err = is_state_visited(env, insn_idx);
  2567. if (err < 0)
  2568. return err;
  2569. if (err == 1) {
  2570. /* found equivalent state, can prune the search */
  2571. if (log_level) {
  2572. if (do_print_state)
  2573. verbose("\nfrom %d to %d: safe\n",
  2574. prev_insn_idx, insn_idx);
  2575. else
  2576. verbose("%d: safe\n", insn_idx);
  2577. }
  2578. goto process_bpf_exit;
  2579. }
  2580. if (need_resched())
  2581. cond_resched();
  2582. if (log_level && do_print_state) {
  2583. verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
  2584. print_verifier_state(&env->cur_state);
  2585. do_print_state = false;
  2586. }
  2587. if (log_level) {
  2588. verbose("%d: ", insn_idx);
  2589. print_bpf_insn(env, insn);
  2590. }
  2591. err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
  2592. if (err)
  2593. return err;
  2594. if (class == BPF_ALU || class == BPF_ALU64) {
  2595. err = check_alu_op(env, insn);
  2596. if (err)
  2597. return err;
  2598. } else if (class == BPF_LDX) {
  2599. enum bpf_reg_type *prev_src_type, src_reg_type;
  2600. /* check for reserved fields is already done */
  2601. /* check src operand */
  2602. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2603. if (err)
  2604. return err;
  2605. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  2606. if (err)
  2607. return err;
  2608. src_reg_type = regs[insn->src_reg].type;
  2609. /* check that memory (src_reg + off) is readable,
  2610. * the state of dst_reg will be updated by this func
  2611. */
  2612. err = check_mem_access(env, insn->src_reg, insn->off,
  2613. BPF_SIZE(insn->code), BPF_READ,
  2614. insn->dst_reg);
  2615. if (err)
  2616. return err;
  2617. reset_reg_range_values(regs, insn->dst_reg);
  2618. if (BPF_SIZE(insn->code) != BPF_W &&
  2619. BPF_SIZE(insn->code) != BPF_DW) {
  2620. insn_idx++;
  2621. continue;
  2622. }
  2623. prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
  2624. if (*prev_src_type == NOT_INIT) {
  2625. /* saw a valid insn
  2626. * dst_reg = *(u32 *)(src_reg + off)
  2627. * save type to validate intersecting paths
  2628. */
  2629. *prev_src_type = src_reg_type;
  2630. } else if (src_reg_type != *prev_src_type &&
  2631. (src_reg_type == PTR_TO_CTX ||
  2632. *prev_src_type == PTR_TO_CTX)) {
  2633. /* ABuser program is trying to use the same insn
  2634. * dst_reg = *(u32*) (src_reg + off)
  2635. * with different pointer types:
  2636. * src_reg == ctx in one branch and
  2637. * src_reg == stack|map in some other branch.
  2638. * Reject it.
  2639. */
  2640. verbose("same insn cannot be used with different pointers\n");
  2641. return -EINVAL;
  2642. }
  2643. } else if (class == BPF_STX) {
  2644. enum bpf_reg_type *prev_dst_type, dst_reg_type;
  2645. if (BPF_MODE(insn->code) == BPF_XADD) {
  2646. err = check_xadd(env, insn);
  2647. if (err)
  2648. return err;
  2649. insn_idx++;
  2650. continue;
  2651. }
  2652. /* check src1 operand */
  2653. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2654. if (err)
  2655. return err;
  2656. /* check src2 operand */
  2657. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2658. if (err)
  2659. return err;
  2660. dst_reg_type = regs[insn->dst_reg].type;
  2661. /* check that memory (dst_reg + off) is writeable */
  2662. err = check_mem_access(env, insn->dst_reg, insn->off,
  2663. BPF_SIZE(insn->code), BPF_WRITE,
  2664. insn->src_reg);
  2665. if (err)
  2666. return err;
  2667. prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
  2668. if (*prev_dst_type == NOT_INIT) {
  2669. *prev_dst_type = dst_reg_type;
  2670. } else if (dst_reg_type != *prev_dst_type &&
  2671. (dst_reg_type == PTR_TO_CTX ||
  2672. *prev_dst_type == PTR_TO_CTX)) {
  2673. verbose("same insn cannot be used with different pointers\n");
  2674. return -EINVAL;
  2675. }
  2676. } else if (class == BPF_ST) {
  2677. if (BPF_MODE(insn->code) != BPF_MEM ||
  2678. insn->src_reg != BPF_REG_0) {
  2679. verbose("BPF_ST uses reserved fields\n");
  2680. return -EINVAL;
  2681. }
  2682. /* check src operand */
  2683. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2684. if (err)
  2685. return err;
  2686. /* check that memory (dst_reg + off) is writeable */
  2687. err = check_mem_access(env, insn->dst_reg, insn->off,
  2688. BPF_SIZE(insn->code), BPF_WRITE,
  2689. -1);
  2690. if (err)
  2691. return err;
  2692. } else if (class == BPF_JMP) {
  2693. u8 opcode = BPF_OP(insn->code);
  2694. if (opcode == BPF_CALL) {
  2695. if (BPF_SRC(insn->code) != BPF_K ||
  2696. insn->off != 0 ||
  2697. insn->src_reg != BPF_REG_0 ||
  2698. insn->dst_reg != BPF_REG_0) {
  2699. verbose("BPF_CALL uses reserved fields\n");
  2700. return -EINVAL;
  2701. }
  2702. err = check_call(env, insn->imm);
  2703. if (err)
  2704. return err;
  2705. } else if (opcode == BPF_JA) {
  2706. if (BPF_SRC(insn->code) != BPF_K ||
  2707. insn->imm != 0 ||
  2708. insn->src_reg != BPF_REG_0 ||
  2709. insn->dst_reg != BPF_REG_0) {
  2710. verbose("BPF_JA uses reserved fields\n");
  2711. return -EINVAL;
  2712. }
  2713. insn_idx += insn->off + 1;
  2714. continue;
  2715. } else if (opcode == BPF_EXIT) {
  2716. if (BPF_SRC(insn->code) != BPF_K ||
  2717. insn->imm != 0 ||
  2718. insn->src_reg != BPF_REG_0 ||
  2719. insn->dst_reg != BPF_REG_0) {
  2720. verbose("BPF_EXIT uses reserved fields\n");
  2721. return -EINVAL;
  2722. }
  2723. /* eBPF calling convetion is such that R0 is used
  2724. * to return the value from eBPF program.
  2725. * Make sure that it's readable at this time
  2726. * of bpf_exit, which means that program wrote
  2727. * something into it earlier
  2728. */
  2729. err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
  2730. if (err)
  2731. return err;
  2732. if (is_pointer_value(env, BPF_REG_0)) {
  2733. verbose("R0 leaks addr as return value\n");
  2734. return -EACCES;
  2735. }
  2736. process_bpf_exit:
  2737. insn_idx = pop_stack(env, &prev_insn_idx);
  2738. if (insn_idx < 0) {
  2739. break;
  2740. } else {
  2741. do_print_state = true;
  2742. continue;
  2743. }
  2744. } else {
  2745. err = check_cond_jmp_op(env, insn, &insn_idx);
  2746. if (err)
  2747. return err;
  2748. }
  2749. } else if (class == BPF_LD) {
  2750. u8 mode = BPF_MODE(insn->code);
  2751. if (mode == BPF_ABS || mode == BPF_IND) {
  2752. err = check_ld_abs(env, insn);
  2753. if (err)
  2754. return err;
  2755. } else if (mode == BPF_IMM) {
  2756. err = check_ld_imm(env, insn);
  2757. if (err)
  2758. return err;
  2759. insn_idx++;
  2760. } else {
  2761. verbose("invalid BPF_LD mode\n");
  2762. return -EINVAL;
  2763. }
  2764. reset_reg_range_values(regs, insn->dst_reg);
  2765. } else {
  2766. verbose("unknown insn class %d\n", class);
  2767. return -EINVAL;
  2768. }
  2769. insn_idx++;
  2770. }
  2771. verbose("processed %d insns\n", insn_processed);
  2772. return 0;
  2773. }
  2774. static int check_map_prog_compatibility(struct bpf_map *map,
  2775. struct bpf_prog *prog)
  2776. {
  2777. if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
  2778. (map->map_type == BPF_MAP_TYPE_HASH ||
  2779. map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
  2780. (map->map_flags & BPF_F_NO_PREALLOC)) {
  2781. verbose("perf_event programs can only use preallocated hash map\n");
  2782. return -EINVAL;
  2783. }
  2784. return 0;
  2785. }
  2786. /* look for pseudo eBPF instructions that access map FDs and
  2787. * replace them with actual map pointers
  2788. */
  2789. static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
  2790. {
  2791. struct bpf_insn *insn = env->prog->insnsi;
  2792. int insn_cnt = env->prog->len;
  2793. int i, j, err;
  2794. for (i = 0; i < insn_cnt; i++, insn++) {
  2795. if (BPF_CLASS(insn->code) == BPF_LDX &&
  2796. (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
  2797. verbose("BPF_LDX uses reserved fields\n");
  2798. return -EINVAL;
  2799. }
  2800. if (BPF_CLASS(insn->code) == BPF_STX &&
  2801. ((BPF_MODE(insn->code) != BPF_MEM &&
  2802. BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
  2803. verbose("BPF_STX uses reserved fields\n");
  2804. return -EINVAL;
  2805. }
  2806. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  2807. struct bpf_map *map;
  2808. struct fd f;
  2809. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  2810. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  2811. insn[1].off != 0) {
  2812. verbose("invalid bpf_ld_imm64 insn\n");
  2813. return -EINVAL;
  2814. }
  2815. if (insn->src_reg == 0)
  2816. /* valid generic load 64-bit imm */
  2817. goto next_insn;
  2818. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  2819. verbose("unrecognized bpf_ld_imm64 insn\n");
  2820. return -EINVAL;
  2821. }
  2822. f = fdget(insn->imm);
  2823. map = __bpf_map_get(f);
  2824. if (IS_ERR(map)) {
  2825. verbose("fd %d is not pointing to valid bpf_map\n",
  2826. insn->imm);
  2827. return PTR_ERR(map);
  2828. }
  2829. err = check_map_prog_compatibility(map, env->prog);
  2830. if (err) {
  2831. fdput(f);
  2832. return err;
  2833. }
  2834. /* store map pointer inside BPF_LD_IMM64 instruction */
  2835. insn[0].imm = (u32) (unsigned long) map;
  2836. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  2837. /* check whether we recorded this map already */
  2838. for (j = 0; j < env->used_map_cnt; j++)
  2839. if (env->used_maps[j] == map) {
  2840. fdput(f);
  2841. goto next_insn;
  2842. }
  2843. if (env->used_map_cnt >= MAX_USED_MAPS) {
  2844. fdput(f);
  2845. return -E2BIG;
  2846. }
  2847. /* hold the map. If the program is rejected by verifier,
  2848. * the map will be released by release_maps() or it
  2849. * will be used by the valid program until it's unloaded
  2850. * and all maps are released in free_bpf_prog_info()
  2851. */
  2852. map = bpf_map_inc(map, false);
  2853. if (IS_ERR(map)) {
  2854. fdput(f);
  2855. return PTR_ERR(map);
  2856. }
  2857. env->used_maps[env->used_map_cnt++] = map;
  2858. fdput(f);
  2859. next_insn:
  2860. insn++;
  2861. i++;
  2862. }
  2863. }
  2864. /* now all pseudo BPF_LD_IMM64 instructions load valid
  2865. * 'struct bpf_map *' into a register instead of user map_fd.
  2866. * These pointers will be used later by verifier to validate map access.
  2867. */
  2868. return 0;
  2869. }
  2870. /* drop refcnt of maps used by the rejected program */
  2871. static void release_maps(struct bpf_verifier_env *env)
  2872. {
  2873. int i;
  2874. for (i = 0; i < env->used_map_cnt; i++)
  2875. bpf_map_put(env->used_maps[i]);
  2876. }
  2877. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  2878. static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
  2879. {
  2880. struct bpf_insn *insn = env->prog->insnsi;
  2881. int insn_cnt = env->prog->len;
  2882. int i;
  2883. for (i = 0; i < insn_cnt; i++, insn++)
  2884. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  2885. insn->src_reg = 0;
  2886. }
  2887. /* convert load instructions that access fields of 'struct __sk_buff'
  2888. * into sequence of instructions that access fields of 'struct sk_buff'
  2889. */
  2890. static int convert_ctx_accesses(struct bpf_verifier_env *env)
  2891. {
  2892. const struct bpf_verifier_ops *ops = env->prog->aux->ops;
  2893. const int insn_cnt = env->prog->len;
  2894. struct bpf_insn insn_buf[16], *insn;
  2895. struct bpf_prog *new_prog;
  2896. enum bpf_access_type type;
  2897. int i, cnt, delta = 0;
  2898. if (ops->gen_prologue) {
  2899. cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
  2900. env->prog);
  2901. if (cnt >= ARRAY_SIZE(insn_buf)) {
  2902. verbose("bpf verifier is misconfigured\n");
  2903. return -EINVAL;
  2904. } else if (cnt) {
  2905. new_prog = bpf_patch_insn_single(env->prog, 0,
  2906. insn_buf, cnt);
  2907. if (!new_prog)
  2908. return -ENOMEM;
  2909. env->prog = new_prog;
  2910. delta += cnt - 1;
  2911. }
  2912. }
  2913. if (!ops->convert_ctx_access)
  2914. return 0;
  2915. insn = env->prog->insnsi + delta;
  2916. for (i = 0; i < insn_cnt; i++, insn++) {
  2917. if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
  2918. insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
  2919. type = BPF_READ;
  2920. else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
  2921. insn->code == (BPF_STX | BPF_MEM | BPF_DW))
  2922. type = BPF_WRITE;
  2923. else
  2924. continue;
  2925. if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
  2926. continue;
  2927. cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg,
  2928. insn->off, insn_buf, env->prog);
  2929. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
  2930. verbose("bpf verifier is misconfigured\n");
  2931. return -EINVAL;
  2932. }
  2933. new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
  2934. cnt);
  2935. if (!new_prog)
  2936. return -ENOMEM;
  2937. delta += cnt - 1;
  2938. /* keep walking new program and skip insns we just inserted */
  2939. env->prog = new_prog;
  2940. insn = new_prog->insnsi + i + delta;
  2941. }
  2942. return 0;
  2943. }
  2944. static void free_states(struct bpf_verifier_env *env)
  2945. {
  2946. struct bpf_verifier_state_list *sl, *sln;
  2947. int i;
  2948. if (!env->explored_states)
  2949. return;
  2950. for (i = 0; i < env->prog->len; i++) {
  2951. sl = env->explored_states[i];
  2952. if (sl)
  2953. while (sl != STATE_LIST_MARK) {
  2954. sln = sl->next;
  2955. kfree(sl);
  2956. sl = sln;
  2957. }
  2958. }
  2959. kfree(env->explored_states);
  2960. }
  2961. int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  2962. {
  2963. char __user *log_ubuf = NULL;
  2964. struct bpf_verifier_env *env;
  2965. int ret = -EINVAL;
  2966. if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
  2967. return -E2BIG;
  2968. /* 'struct bpf_verifier_env' can be global, but since it's not small,
  2969. * allocate/free it every time bpf_check() is called
  2970. */
  2971. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  2972. if (!env)
  2973. return -ENOMEM;
  2974. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  2975. (*prog)->len);
  2976. ret = -ENOMEM;
  2977. if (!env->insn_aux_data)
  2978. goto err_free_env;
  2979. env->prog = *prog;
  2980. /* grab the mutex to protect few globals used by verifier */
  2981. mutex_lock(&bpf_verifier_lock);
  2982. if (attr->log_level || attr->log_buf || attr->log_size) {
  2983. /* user requested verbose verifier output
  2984. * and supplied buffer to store the verification trace
  2985. */
  2986. log_level = attr->log_level;
  2987. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  2988. log_size = attr->log_size;
  2989. log_len = 0;
  2990. ret = -EINVAL;
  2991. /* log_* values have to be sane */
  2992. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  2993. log_level == 0 || log_ubuf == NULL)
  2994. goto err_unlock;
  2995. ret = -ENOMEM;
  2996. log_buf = vmalloc(log_size);
  2997. if (!log_buf)
  2998. goto err_unlock;
  2999. } else {
  3000. log_level = 0;
  3001. }
  3002. ret = replace_map_fd_with_map_ptr(env);
  3003. if (ret < 0)
  3004. goto skip_full_check;
  3005. env->explored_states = kcalloc(env->prog->len,
  3006. sizeof(struct bpf_verifier_state_list *),
  3007. GFP_USER);
  3008. ret = -ENOMEM;
  3009. if (!env->explored_states)
  3010. goto skip_full_check;
  3011. ret = check_cfg(env);
  3012. if (ret < 0)
  3013. goto skip_full_check;
  3014. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3015. ret = do_check(env);
  3016. skip_full_check:
  3017. while (pop_stack(env, NULL) >= 0);
  3018. free_states(env);
  3019. if (ret == 0)
  3020. /* program is valid, convert *(u32*)(ctx + off) accesses */
  3021. ret = convert_ctx_accesses(env);
  3022. if (log_level && log_len >= log_size - 1) {
  3023. BUG_ON(log_len >= log_size);
  3024. /* verifier log exceeded user supplied buffer */
  3025. ret = -ENOSPC;
  3026. /* fall through to return what was recorded */
  3027. }
  3028. /* copy verifier log back to user space including trailing zero */
  3029. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  3030. ret = -EFAULT;
  3031. goto free_log_buf;
  3032. }
  3033. if (ret == 0 && env->used_map_cnt) {
  3034. /* if program passed verifier, update used_maps in bpf_prog_info */
  3035. env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  3036. sizeof(env->used_maps[0]),
  3037. GFP_KERNEL);
  3038. if (!env->prog->aux->used_maps) {
  3039. ret = -ENOMEM;
  3040. goto free_log_buf;
  3041. }
  3042. memcpy(env->prog->aux->used_maps, env->used_maps,
  3043. sizeof(env->used_maps[0]) * env->used_map_cnt);
  3044. env->prog->aux->used_map_cnt = env->used_map_cnt;
  3045. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  3046. * bpf_ld_imm64 instructions
  3047. */
  3048. convert_pseudo_ld_imm64(env);
  3049. }
  3050. free_log_buf:
  3051. if (log_level)
  3052. vfree(log_buf);
  3053. if (!env->prog->aux->used_maps)
  3054. /* if we didn't copy map pointers into bpf_prog_info, release
  3055. * them now. Otherwise free_bpf_prog_info() will release them.
  3056. */
  3057. release_maps(env);
  3058. *prog = env->prog;
  3059. err_unlock:
  3060. mutex_unlock(&bpf_verifier_lock);
  3061. vfree(env->insn_aux_data);
  3062. err_free_env:
  3063. kfree(env);
  3064. return ret;
  3065. }
  3066. int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
  3067. void *priv)
  3068. {
  3069. struct bpf_verifier_env *env;
  3070. int ret;
  3071. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  3072. if (!env)
  3073. return -ENOMEM;
  3074. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  3075. prog->len);
  3076. ret = -ENOMEM;
  3077. if (!env->insn_aux_data)
  3078. goto err_free_env;
  3079. env->prog = prog;
  3080. env->analyzer_ops = ops;
  3081. env->analyzer_priv = priv;
  3082. /* grab the mutex to protect few globals used by verifier */
  3083. mutex_lock(&bpf_verifier_lock);
  3084. log_level = 0;
  3085. env->explored_states = kcalloc(env->prog->len,
  3086. sizeof(struct bpf_verifier_state_list *),
  3087. GFP_KERNEL);
  3088. ret = -ENOMEM;
  3089. if (!env->explored_states)
  3090. goto skip_full_check;
  3091. ret = check_cfg(env);
  3092. if (ret < 0)
  3093. goto skip_full_check;
  3094. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3095. ret = do_check(env);
  3096. skip_full_check:
  3097. while (pop_stack(env, NULL) >= 0);
  3098. free_states(env);
  3099. mutex_unlock(&bpf_verifier_lock);
  3100. vfree(env->insn_aux_data);
  3101. err_free_env:
  3102. kfree(env);
  3103. return ret;
  3104. }
  3105. EXPORT_SYMBOL_GPL(bpf_analyzer);