verifier.c 91 KB

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