verifier.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/bpf.h>
  16. #include <linux/filter.h>
  17. #include <net/netlink.h>
  18. #include <linux/file.h>
  19. #include <linux/vmalloc.h>
  20. /* bpf_check() is a static code analyzer that walks eBPF program
  21. * instruction by instruction and updates register/stack state.
  22. * All paths of conditional branches are analyzed until 'bpf_exit' insn.
  23. *
  24. * The first pass is depth-first-search to check that the program is a DAG.
  25. * It rejects the following programs:
  26. * - larger than BPF_MAXINSNS insns
  27. * - if loop is present (detected via back-edge)
  28. * - unreachable insns exist (shouldn't be a forest. program = one function)
  29. * - out of bounds or malformed jumps
  30. * The second pass is all possible path descent from the 1st insn.
  31. * Since it's analyzing all pathes through the program, the length of the
  32. * analysis is limited to 32k insn, which may be hit even if total number of
  33. * insn is less then 4K, but there are too many branches that change stack/regs.
  34. * Number of 'branches to be analyzed' is limited to 1k
  35. *
  36. * On entry to each instruction, each register has a type, and the instruction
  37. * changes the types of the registers depending on instruction semantics.
  38. * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
  39. * copied to R1.
  40. *
  41. * All registers are 64-bit.
  42. * R0 - return register
  43. * R1-R5 argument passing registers
  44. * R6-R9 callee saved registers
  45. * R10 - frame pointer read-only
  46. *
  47. * At the start of BPF program the register R1 contains a pointer to bpf_context
  48. * and has type PTR_TO_CTX.
  49. *
  50. * Verifier tracks arithmetic operations on pointers in case:
  51. * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  52. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
  53. * 1st insn copies R10 (which has FRAME_PTR) type into R1
  54. * and 2nd arithmetic instruction is pattern matched to recognize
  55. * that it wants to construct a pointer to some element within stack.
  56. * So after 2nd insn, the register R1 has type PTR_TO_STACK
  57. * (and -20 constant is saved for further stack bounds checking).
  58. * Meaning that this reg is a pointer to stack plus known immediate constant.
  59. *
  60. * Most of the time the registers have UNKNOWN_VALUE type, which
  61. * means the register has some value, but it's not a valid pointer.
  62. * (like pointer plus pointer becomes UNKNOWN_VALUE type)
  63. *
  64. * When verifier sees load or store instructions the type of base register
  65. * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
  66. * types recognized by check_mem_access() function.
  67. *
  68. * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
  69. * and the range of [ptr, ptr + map's value_size) is accessible.
  70. *
  71. * registers used to pass values to function calls are checked against
  72. * function argument constraints.
  73. *
  74. * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
  75. * It means that the register type passed to this function must be
  76. * PTR_TO_STACK and it will be used inside the function as
  77. * 'pointer to map element key'
  78. *
  79. * For example the argument constraints for bpf_map_lookup_elem():
  80. * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  81. * .arg1_type = ARG_CONST_MAP_PTR,
  82. * .arg2_type = ARG_PTR_TO_MAP_KEY,
  83. *
  84. * ret_type says that this function returns 'pointer to map elem value or null'
  85. * function expects 1st argument to be a const pointer to 'struct bpf_map' and
  86. * 2nd argument should be a pointer to stack, which will be used inside
  87. * the helper function as a pointer to map element key.
  88. *
  89. * On the kernel side the helper function looks like:
  90. * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  91. * {
  92. * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  93. * void *key = (void *) (unsigned long) r2;
  94. * void *value;
  95. *
  96. * here kernel can access 'key' and 'map' pointers safely, knowing that
  97. * [key, key + map->key_size) bytes are valid and were initialized on
  98. * the stack of eBPF program.
  99. * }
  100. *
  101. * Corresponding eBPF program may look like:
  102. * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
  103. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
  104. * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
  105. * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  106. * here verifier looks at prototype of map_lookup_elem() and sees:
  107. * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
  108. * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
  109. *
  110. * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
  111. * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
  112. * and were initialized prior to this call.
  113. * If it's ok, then verifier allows this BPF_CALL insn and looks at
  114. * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
  115. * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
  116. * returns ether pointer to map value or NULL.
  117. *
  118. * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
  119. * insn, the register holding that pointer in the true branch changes state to
  120. * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
  121. * branch. See check_cond_jmp_op().
  122. *
  123. * After the call R0 is set to return type of the function and registers R1-R5
  124. * are set to NOT_INIT to indicate that they are no longer readable.
  125. */
  126. /* types of values stored in eBPF registers */
  127. enum bpf_reg_type {
  128. NOT_INIT = 0, /* nothing was written into register */
  129. UNKNOWN_VALUE, /* reg doesn't contain a valid pointer */
  130. PTR_TO_CTX, /* reg points to bpf_context */
  131. CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
  132. PTR_TO_MAP_VALUE, /* reg points to map element value */
  133. PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
  134. FRAME_PTR, /* reg == frame_pointer */
  135. PTR_TO_STACK, /* reg == frame_pointer + imm */
  136. CONST_IMM, /* constant integer value */
  137. };
  138. struct reg_state {
  139. enum bpf_reg_type type;
  140. union {
  141. /* valid when type == CONST_IMM | PTR_TO_STACK */
  142. long imm;
  143. /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
  144. * PTR_TO_MAP_VALUE_OR_NULL
  145. */
  146. struct bpf_map *map_ptr;
  147. };
  148. };
  149. enum bpf_stack_slot_type {
  150. STACK_INVALID, /* nothing was stored in this stack slot */
  151. STACK_SPILL, /* register spilled into stack */
  152. STACK_MISC /* BPF program wrote some data into this slot */
  153. };
  154. #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
  155. /* state of the program:
  156. * type of all registers and stack info
  157. */
  158. struct verifier_state {
  159. struct reg_state regs[MAX_BPF_REG];
  160. u8 stack_slot_type[MAX_BPF_STACK];
  161. struct reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
  162. };
  163. /* linked list of verifier states used to prune search */
  164. struct verifier_state_list {
  165. struct verifier_state state;
  166. struct verifier_state_list *next;
  167. };
  168. /* verifier_state + insn_idx are pushed to stack when branch is encountered */
  169. struct verifier_stack_elem {
  170. /* verifer state is 'st'
  171. * before processing instruction 'insn_idx'
  172. * and after processing instruction 'prev_insn_idx'
  173. */
  174. struct verifier_state st;
  175. int insn_idx;
  176. int prev_insn_idx;
  177. struct verifier_stack_elem *next;
  178. };
  179. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  180. /* single container for all structs
  181. * one verifier_env per bpf_check() call
  182. */
  183. struct verifier_env {
  184. struct bpf_prog *prog; /* eBPF program being verified */
  185. struct verifier_stack_elem *head; /* stack of verifier states to be processed */
  186. int stack_size; /* number of states to be processed */
  187. struct verifier_state cur_state; /* current verifier state */
  188. struct verifier_state_list **explored_states; /* search pruning optimization */
  189. struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
  190. u32 used_map_cnt; /* number of used maps */
  191. bool allow_ptr_leaks;
  192. };
  193. #define BPF_COMPLEXITY_LIMIT_INSNS 65536
  194. #define BPF_COMPLEXITY_LIMIT_STACK 1024
  195. struct bpf_call_arg_meta {
  196. struct bpf_map *map_ptr;
  197. bool raw_mode;
  198. int regno;
  199. int access_size;
  200. };
  201. /* verbose verifier prints what it's seeing
  202. * bpf_check() is called under lock, so no race to access these global vars
  203. */
  204. static u32 log_level, log_size, log_len;
  205. static char *log_buf;
  206. static DEFINE_MUTEX(bpf_verifier_lock);
  207. /* log_level controls verbosity level of eBPF verifier.
  208. * verbose() is used to dump the verification trace to the log, so the user
  209. * can figure out what's wrong with the program
  210. */
  211. static __printf(1, 2) void verbose(const char *fmt, ...)
  212. {
  213. va_list args;
  214. if (log_level == 0 || log_len >= log_size - 1)
  215. return;
  216. va_start(args, fmt);
  217. log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
  218. va_end(args);
  219. }
  220. /* string representation of 'enum bpf_reg_type' */
  221. static const char * const reg_type_str[] = {
  222. [NOT_INIT] = "?",
  223. [UNKNOWN_VALUE] = "inv",
  224. [PTR_TO_CTX] = "ctx",
  225. [CONST_PTR_TO_MAP] = "map_ptr",
  226. [PTR_TO_MAP_VALUE] = "map_value",
  227. [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
  228. [FRAME_PTR] = "fp",
  229. [PTR_TO_STACK] = "fp",
  230. [CONST_IMM] = "imm",
  231. };
  232. static const struct {
  233. int map_type;
  234. int func_id;
  235. } func_limit[] = {
  236. {BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call},
  237. {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read},
  238. {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_output},
  239. {BPF_MAP_TYPE_STACK_TRACE, BPF_FUNC_get_stackid},
  240. };
  241. static void print_verifier_state(struct verifier_env *env)
  242. {
  243. enum bpf_reg_type t;
  244. int i;
  245. for (i = 0; i < MAX_BPF_REG; i++) {
  246. t = env->cur_state.regs[i].type;
  247. if (t == NOT_INIT)
  248. continue;
  249. verbose(" R%d=%s", i, reg_type_str[t]);
  250. if (t == CONST_IMM || t == PTR_TO_STACK)
  251. verbose("%ld", env->cur_state.regs[i].imm);
  252. else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
  253. t == PTR_TO_MAP_VALUE_OR_NULL)
  254. verbose("(ks=%d,vs=%d)",
  255. env->cur_state.regs[i].map_ptr->key_size,
  256. env->cur_state.regs[i].map_ptr->value_size);
  257. }
  258. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  259. if (env->cur_state.stack_slot_type[i] == STACK_SPILL)
  260. verbose(" fp%d=%s", -MAX_BPF_STACK + i,
  261. reg_type_str[env->cur_state.spilled_regs[i / BPF_REG_SIZE].type]);
  262. }
  263. verbose("\n");
  264. }
  265. static const char *const bpf_class_string[] = {
  266. [BPF_LD] = "ld",
  267. [BPF_LDX] = "ldx",
  268. [BPF_ST] = "st",
  269. [BPF_STX] = "stx",
  270. [BPF_ALU] = "alu",
  271. [BPF_JMP] = "jmp",
  272. [BPF_RET] = "BUG",
  273. [BPF_ALU64] = "alu64",
  274. };
  275. static const char *const bpf_alu_string[16] = {
  276. [BPF_ADD >> 4] = "+=",
  277. [BPF_SUB >> 4] = "-=",
  278. [BPF_MUL >> 4] = "*=",
  279. [BPF_DIV >> 4] = "/=",
  280. [BPF_OR >> 4] = "|=",
  281. [BPF_AND >> 4] = "&=",
  282. [BPF_LSH >> 4] = "<<=",
  283. [BPF_RSH >> 4] = ">>=",
  284. [BPF_NEG >> 4] = "neg",
  285. [BPF_MOD >> 4] = "%=",
  286. [BPF_XOR >> 4] = "^=",
  287. [BPF_MOV >> 4] = "=",
  288. [BPF_ARSH >> 4] = "s>>=",
  289. [BPF_END >> 4] = "endian",
  290. };
  291. static const char *const bpf_ldst_string[] = {
  292. [BPF_W >> 3] = "u32",
  293. [BPF_H >> 3] = "u16",
  294. [BPF_B >> 3] = "u8",
  295. [BPF_DW >> 3] = "u64",
  296. };
  297. static const char *const bpf_jmp_string[16] = {
  298. [BPF_JA >> 4] = "jmp",
  299. [BPF_JEQ >> 4] = "==",
  300. [BPF_JGT >> 4] = ">",
  301. [BPF_JGE >> 4] = ">=",
  302. [BPF_JSET >> 4] = "&",
  303. [BPF_JNE >> 4] = "!=",
  304. [BPF_JSGT >> 4] = "s>",
  305. [BPF_JSGE >> 4] = "s>=",
  306. [BPF_CALL >> 4] = "call",
  307. [BPF_EXIT >> 4] = "exit",
  308. };
  309. static void print_bpf_insn(struct bpf_insn *insn)
  310. {
  311. u8 class = BPF_CLASS(insn->code);
  312. if (class == BPF_ALU || class == BPF_ALU64) {
  313. if (BPF_SRC(insn->code) == BPF_X)
  314. verbose("(%02x) %sr%d %s %sr%d\n",
  315. insn->code, class == BPF_ALU ? "(u32) " : "",
  316. insn->dst_reg,
  317. bpf_alu_string[BPF_OP(insn->code) >> 4],
  318. class == BPF_ALU ? "(u32) " : "",
  319. insn->src_reg);
  320. else
  321. verbose("(%02x) %sr%d %s %s%d\n",
  322. insn->code, class == BPF_ALU ? "(u32) " : "",
  323. insn->dst_reg,
  324. bpf_alu_string[BPF_OP(insn->code) >> 4],
  325. class == BPF_ALU ? "(u32) " : "",
  326. insn->imm);
  327. } else if (class == BPF_STX) {
  328. if (BPF_MODE(insn->code) == BPF_MEM)
  329. verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
  330. insn->code,
  331. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  332. insn->dst_reg,
  333. insn->off, insn->src_reg);
  334. else if (BPF_MODE(insn->code) == BPF_XADD)
  335. verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
  336. insn->code,
  337. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  338. insn->dst_reg, insn->off,
  339. insn->src_reg);
  340. else
  341. verbose("BUG_%02x\n", insn->code);
  342. } else if (class == BPF_ST) {
  343. if (BPF_MODE(insn->code) != BPF_MEM) {
  344. verbose("BUG_st_%02x\n", insn->code);
  345. return;
  346. }
  347. verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
  348. insn->code,
  349. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  350. insn->dst_reg,
  351. insn->off, insn->imm);
  352. } else if (class == BPF_LDX) {
  353. if (BPF_MODE(insn->code) != BPF_MEM) {
  354. verbose("BUG_ldx_%02x\n", insn->code);
  355. return;
  356. }
  357. verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
  358. insn->code, insn->dst_reg,
  359. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  360. insn->src_reg, insn->off);
  361. } else if (class == BPF_LD) {
  362. if (BPF_MODE(insn->code) == BPF_ABS) {
  363. verbose("(%02x) r0 = *(%s *)skb[%d]\n",
  364. insn->code,
  365. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  366. insn->imm);
  367. } else if (BPF_MODE(insn->code) == BPF_IND) {
  368. verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
  369. insn->code,
  370. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  371. insn->src_reg, insn->imm);
  372. } else if (BPF_MODE(insn->code) == BPF_IMM) {
  373. verbose("(%02x) r%d = 0x%x\n",
  374. insn->code, insn->dst_reg, insn->imm);
  375. } else {
  376. verbose("BUG_ld_%02x\n", insn->code);
  377. return;
  378. }
  379. } else if (class == BPF_JMP) {
  380. u8 opcode = BPF_OP(insn->code);
  381. if (opcode == BPF_CALL) {
  382. verbose("(%02x) call %d\n", insn->code, insn->imm);
  383. } else if (insn->code == (BPF_JMP | BPF_JA)) {
  384. verbose("(%02x) goto pc%+d\n",
  385. insn->code, insn->off);
  386. } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
  387. verbose("(%02x) exit\n", insn->code);
  388. } else if (BPF_SRC(insn->code) == BPF_X) {
  389. verbose("(%02x) if r%d %s r%d goto pc%+d\n",
  390. insn->code, insn->dst_reg,
  391. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  392. insn->src_reg, insn->off);
  393. } else {
  394. verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
  395. insn->code, insn->dst_reg,
  396. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  397. insn->imm, insn->off);
  398. }
  399. } else {
  400. verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
  401. }
  402. }
  403. static int pop_stack(struct verifier_env *env, int *prev_insn_idx)
  404. {
  405. struct verifier_stack_elem *elem;
  406. int insn_idx;
  407. if (env->head == NULL)
  408. return -1;
  409. memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
  410. insn_idx = env->head->insn_idx;
  411. if (prev_insn_idx)
  412. *prev_insn_idx = env->head->prev_insn_idx;
  413. elem = env->head->next;
  414. kfree(env->head);
  415. env->head = elem;
  416. env->stack_size--;
  417. return insn_idx;
  418. }
  419. static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx,
  420. int prev_insn_idx)
  421. {
  422. struct verifier_stack_elem *elem;
  423. elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL);
  424. if (!elem)
  425. goto err;
  426. memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
  427. elem->insn_idx = insn_idx;
  428. elem->prev_insn_idx = prev_insn_idx;
  429. elem->next = env->head;
  430. env->head = elem;
  431. env->stack_size++;
  432. if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
  433. verbose("BPF program is too complex\n");
  434. goto err;
  435. }
  436. return &elem->st;
  437. err:
  438. /* pop all elements and return */
  439. while (pop_stack(env, NULL) >= 0);
  440. return NULL;
  441. }
  442. #define CALLER_SAVED_REGS 6
  443. static const int caller_saved[CALLER_SAVED_REGS] = {
  444. BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
  445. };
  446. static void init_reg_state(struct reg_state *regs)
  447. {
  448. int i;
  449. for (i = 0; i < MAX_BPF_REG; i++) {
  450. regs[i].type = NOT_INIT;
  451. regs[i].imm = 0;
  452. }
  453. /* frame pointer */
  454. regs[BPF_REG_FP].type = FRAME_PTR;
  455. /* 1st arg to a function */
  456. regs[BPF_REG_1].type = PTR_TO_CTX;
  457. }
  458. static void mark_reg_unknown_value(struct reg_state *regs, u32 regno)
  459. {
  460. BUG_ON(regno >= MAX_BPF_REG);
  461. regs[regno].type = UNKNOWN_VALUE;
  462. regs[regno].imm = 0;
  463. }
  464. enum reg_arg_type {
  465. SRC_OP, /* register is used as source operand */
  466. DST_OP, /* register is used as destination operand */
  467. DST_OP_NO_MARK /* same as above, check only, don't mark */
  468. };
  469. static int check_reg_arg(struct reg_state *regs, u32 regno,
  470. enum reg_arg_type t)
  471. {
  472. if (regno >= MAX_BPF_REG) {
  473. verbose("R%d is invalid\n", regno);
  474. return -EINVAL;
  475. }
  476. if (t == SRC_OP) {
  477. /* check whether register used as source operand can be read */
  478. if (regs[regno].type == NOT_INIT) {
  479. verbose("R%d !read_ok\n", regno);
  480. return -EACCES;
  481. }
  482. } else {
  483. /* check whether register used as dest operand can be written to */
  484. if (regno == BPF_REG_FP) {
  485. verbose("frame pointer is read only\n");
  486. return -EACCES;
  487. }
  488. if (t == DST_OP)
  489. mark_reg_unknown_value(regs, regno);
  490. }
  491. return 0;
  492. }
  493. static int bpf_size_to_bytes(int bpf_size)
  494. {
  495. if (bpf_size == BPF_W)
  496. return 4;
  497. else if (bpf_size == BPF_H)
  498. return 2;
  499. else if (bpf_size == BPF_B)
  500. return 1;
  501. else if (bpf_size == BPF_DW)
  502. return 8;
  503. else
  504. return -EINVAL;
  505. }
  506. static bool is_spillable_regtype(enum bpf_reg_type type)
  507. {
  508. switch (type) {
  509. case PTR_TO_MAP_VALUE:
  510. case PTR_TO_MAP_VALUE_OR_NULL:
  511. case PTR_TO_STACK:
  512. case PTR_TO_CTX:
  513. case FRAME_PTR:
  514. case CONST_PTR_TO_MAP:
  515. return true;
  516. default:
  517. return false;
  518. }
  519. }
  520. /* check_stack_read/write functions track spill/fill of registers,
  521. * stack boundary and alignment are checked in check_mem_access()
  522. */
  523. static int check_stack_write(struct verifier_state *state, int off, int size,
  524. int value_regno)
  525. {
  526. int i;
  527. /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
  528. * so it's aligned access and [off, off + size) are within stack limits
  529. */
  530. if (value_regno >= 0 &&
  531. is_spillable_regtype(state->regs[value_regno].type)) {
  532. /* register containing pointer is being spilled into stack */
  533. if (size != BPF_REG_SIZE) {
  534. verbose("invalid size of register spill\n");
  535. return -EACCES;
  536. }
  537. /* save register state */
  538. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  539. state->regs[value_regno];
  540. for (i = 0; i < BPF_REG_SIZE; i++)
  541. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
  542. } else {
  543. /* regular write of data into stack */
  544. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  545. (struct reg_state) {};
  546. for (i = 0; i < size; i++)
  547. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
  548. }
  549. return 0;
  550. }
  551. static int check_stack_read(struct verifier_state *state, int off, int size,
  552. int value_regno)
  553. {
  554. u8 *slot_type;
  555. int i;
  556. slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
  557. if (slot_type[0] == STACK_SPILL) {
  558. if (size != BPF_REG_SIZE) {
  559. verbose("invalid size of register spill\n");
  560. return -EACCES;
  561. }
  562. for (i = 1; i < BPF_REG_SIZE; i++) {
  563. if (slot_type[i] != STACK_SPILL) {
  564. verbose("corrupted spill memory\n");
  565. return -EACCES;
  566. }
  567. }
  568. if (value_regno >= 0)
  569. /* restore register state from stack */
  570. state->regs[value_regno] =
  571. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
  572. return 0;
  573. } else {
  574. for (i = 0; i < size; i++) {
  575. if (slot_type[i] != STACK_MISC) {
  576. verbose("invalid read from stack off %d+%d size %d\n",
  577. off, i, size);
  578. return -EACCES;
  579. }
  580. }
  581. if (value_regno >= 0)
  582. /* have read misc data from the stack */
  583. mark_reg_unknown_value(state->regs, value_regno);
  584. return 0;
  585. }
  586. }
  587. /* check read/write into map element returned by bpf_map_lookup_elem() */
  588. static int check_map_access(struct verifier_env *env, u32 regno, int off,
  589. int size)
  590. {
  591. struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
  592. if (off < 0 || off + size > map->value_size) {
  593. verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
  594. map->value_size, off, size);
  595. return -EACCES;
  596. }
  597. return 0;
  598. }
  599. /* check access to 'struct bpf_context' fields */
  600. static int check_ctx_access(struct verifier_env *env, int off, int size,
  601. enum bpf_access_type t)
  602. {
  603. if (env->prog->aux->ops->is_valid_access &&
  604. env->prog->aux->ops->is_valid_access(off, size, t)) {
  605. /* remember the offset of last byte accessed in ctx */
  606. if (env->prog->aux->max_ctx_offset < off + size)
  607. env->prog->aux->max_ctx_offset = off + size;
  608. return 0;
  609. }
  610. verbose("invalid bpf_context access off=%d size=%d\n", off, size);
  611. return -EACCES;
  612. }
  613. static bool is_pointer_value(struct verifier_env *env, int regno)
  614. {
  615. if (env->allow_ptr_leaks)
  616. return false;
  617. switch (env->cur_state.regs[regno].type) {
  618. case UNKNOWN_VALUE:
  619. case CONST_IMM:
  620. return false;
  621. default:
  622. return true;
  623. }
  624. }
  625. /* check whether memory at (regno + off) is accessible for t = (read | write)
  626. * if t==write, value_regno is a register which value is stored into memory
  627. * if t==read, value_regno is a register which will receive the value from memory
  628. * if t==write && value_regno==-1, some unknown value is stored into memory
  629. * if t==read && value_regno==-1, don't care what we read from memory
  630. */
  631. static int check_mem_access(struct verifier_env *env, u32 regno, int off,
  632. int bpf_size, enum bpf_access_type t,
  633. int value_regno)
  634. {
  635. struct verifier_state *state = &env->cur_state;
  636. int size, err = 0;
  637. if (state->regs[regno].type == PTR_TO_STACK)
  638. off += state->regs[regno].imm;
  639. size = bpf_size_to_bytes(bpf_size);
  640. if (size < 0)
  641. return size;
  642. if (off % size != 0) {
  643. verbose("misaligned access off %d size %d\n", off, size);
  644. return -EACCES;
  645. }
  646. if (state->regs[regno].type == PTR_TO_MAP_VALUE) {
  647. if (t == BPF_WRITE && value_regno >= 0 &&
  648. is_pointer_value(env, value_regno)) {
  649. verbose("R%d leaks addr into map\n", value_regno);
  650. return -EACCES;
  651. }
  652. err = check_map_access(env, regno, off, size);
  653. if (!err && t == BPF_READ && value_regno >= 0)
  654. mark_reg_unknown_value(state->regs, value_regno);
  655. } else if (state->regs[regno].type == PTR_TO_CTX) {
  656. if (t == BPF_WRITE && value_regno >= 0 &&
  657. is_pointer_value(env, value_regno)) {
  658. verbose("R%d leaks addr into ctx\n", value_regno);
  659. return -EACCES;
  660. }
  661. err = check_ctx_access(env, off, size, t);
  662. if (!err && t == BPF_READ && value_regno >= 0)
  663. mark_reg_unknown_value(state->regs, value_regno);
  664. } else if (state->regs[regno].type == FRAME_PTR ||
  665. state->regs[regno].type == PTR_TO_STACK) {
  666. if (off >= 0 || off < -MAX_BPF_STACK) {
  667. verbose("invalid stack off=%d size=%d\n", off, size);
  668. return -EACCES;
  669. }
  670. if (t == BPF_WRITE) {
  671. if (!env->allow_ptr_leaks &&
  672. state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
  673. size != BPF_REG_SIZE) {
  674. verbose("attempt to corrupt spilled pointer on stack\n");
  675. return -EACCES;
  676. }
  677. err = check_stack_write(state, off, size, value_regno);
  678. } else {
  679. err = check_stack_read(state, off, size, value_regno);
  680. }
  681. } else {
  682. verbose("R%d invalid mem access '%s'\n",
  683. regno, reg_type_str[state->regs[regno].type]);
  684. return -EACCES;
  685. }
  686. return err;
  687. }
  688. static int check_xadd(struct verifier_env *env, struct bpf_insn *insn)
  689. {
  690. struct reg_state *regs = env->cur_state.regs;
  691. int err;
  692. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  693. insn->imm != 0) {
  694. verbose("BPF_XADD uses reserved fields\n");
  695. return -EINVAL;
  696. }
  697. /* check src1 operand */
  698. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  699. if (err)
  700. return err;
  701. /* check src2 operand */
  702. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  703. if (err)
  704. return err;
  705. /* check whether atomic_add can read the memory */
  706. err = check_mem_access(env, insn->dst_reg, insn->off,
  707. BPF_SIZE(insn->code), BPF_READ, -1);
  708. if (err)
  709. return err;
  710. /* check whether atomic_add can write into the same memory */
  711. return check_mem_access(env, insn->dst_reg, insn->off,
  712. BPF_SIZE(insn->code), BPF_WRITE, -1);
  713. }
  714. /* when register 'regno' is passed into function that will read 'access_size'
  715. * bytes from that pointer, make sure that it's within stack boundary
  716. * and all elements of stack are initialized
  717. */
  718. static int check_stack_boundary(struct verifier_env *env, int regno,
  719. int access_size, bool zero_size_allowed,
  720. struct bpf_call_arg_meta *meta)
  721. {
  722. struct verifier_state *state = &env->cur_state;
  723. struct reg_state *regs = state->regs;
  724. int off, i;
  725. if (regs[regno].type != PTR_TO_STACK) {
  726. if (zero_size_allowed && access_size == 0 &&
  727. regs[regno].type == CONST_IMM &&
  728. regs[regno].imm == 0)
  729. return 0;
  730. verbose("R%d type=%s expected=%s\n", regno,
  731. reg_type_str[regs[regno].type],
  732. reg_type_str[PTR_TO_STACK]);
  733. return -EACCES;
  734. }
  735. off = regs[regno].imm;
  736. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  737. access_size <= 0) {
  738. verbose("invalid stack type R%d off=%d access_size=%d\n",
  739. regno, off, access_size);
  740. return -EACCES;
  741. }
  742. if (meta && meta->raw_mode) {
  743. meta->access_size = access_size;
  744. meta->regno = regno;
  745. return 0;
  746. }
  747. for (i = 0; i < access_size; i++) {
  748. if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
  749. verbose("invalid indirect read from stack off %d+%d size %d\n",
  750. off, i, access_size);
  751. return -EACCES;
  752. }
  753. }
  754. return 0;
  755. }
  756. static int check_func_arg(struct verifier_env *env, u32 regno,
  757. enum bpf_arg_type arg_type,
  758. struct bpf_call_arg_meta *meta)
  759. {
  760. struct reg_state *reg = env->cur_state.regs + regno;
  761. enum bpf_reg_type expected_type;
  762. int err = 0;
  763. if (arg_type == ARG_DONTCARE)
  764. return 0;
  765. if (reg->type == NOT_INIT) {
  766. verbose("R%d !read_ok\n", regno);
  767. return -EACCES;
  768. }
  769. if (arg_type == ARG_ANYTHING) {
  770. if (is_pointer_value(env, regno)) {
  771. verbose("R%d leaks addr into helper function\n", regno);
  772. return -EACCES;
  773. }
  774. return 0;
  775. }
  776. if (arg_type == ARG_PTR_TO_MAP_KEY ||
  777. arg_type == ARG_PTR_TO_MAP_VALUE) {
  778. expected_type = PTR_TO_STACK;
  779. } else if (arg_type == ARG_CONST_STACK_SIZE ||
  780. arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
  781. expected_type = CONST_IMM;
  782. } else if (arg_type == ARG_CONST_MAP_PTR) {
  783. expected_type = CONST_PTR_TO_MAP;
  784. } else if (arg_type == ARG_PTR_TO_CTX) {
  785. expected_type = PTR_TO_CTX;
  786. } else if (arg_type == ARG_PTR_TO_STACK ||
  787. arg_type == ARG_PTR_TO_RAW_STACK) {
  788. expected_type = PTR_TO_STACK;
  789. /* One exception here. In case function allows for NULL to be
  790. * passed in as argument, it's a CONST_IMM type. Final test
  791. * happens during stack boundary checking.
  792. */
  793. if (reg->type == CONST_IMM && reg->imm == 0)
  794. expected_type = CONST_IMM;
  795. meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK;
  796. } else {
  797. verbose("unsupported arg_type %d\n", arg_type);
  798. return -EFAULT;
  799. }
  800. if (reg->type != expected_type) {
  801. verbose("R%d type=%s expected=%s\n", regno,
  802. reg_type_str[reg->type], reg_type_str[expected_type]);
  803. return -EACCES;
  804. }
  805. if (arg_type == ARG_CONST_MAP_PTR) {
  806. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  807. meta->map_ptr = reg->map_ptr;
  808. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  809. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  810. * check that [key, key + map->key_size) are within
  811. * stack limits and initialized
  812. */
  813. if (!meta->map_ptr) {
  814. /* in function declaration map_ptr must come before
  815. * map_key, so that it's verified and known before
  816. * we have to check map_key here. Otherwise it means
  817. * that kernel subsystem misconfigured verifier
  818. */
  819. verbose("invalid map_ptr to access map->key\n");
  820. return -EACCES;
  821. }
  822. err = check_stack_boundary(env, regno, meta->map_ptr->key_size,
  823. false, NULL);
  824. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  825. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  826. * check [value, value + map->value_size) validity
  827. */
  828. if (!meta->map_ptr) {
  829. /* kernel subsystem misconfigured verifier */
  830. verbose("invalid map_ptr to access map->value\n");
  831. return -EACCES;
  832. }
  833. err = check_stack_boundary(env, regno,
  834. meta->map_ptr->value_size,
  835. false, NULL);
  836. } else if (arg_type == ARG_CONST_STACK_SIZE ||
  837. arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) {
  838. bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO);
  839. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  840. * from stack pointer 'buf'. Check it
  841. * note: regno == len, regno - 1 == buf
  842. */
  843. if (regno == 0) {
  844. /* kernel subsystem misconfigured verifier */
  845. verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
  846. return -EACCES;
  847. }
  848. err = check_stack_boundary(env, regno - 1, reg->imm,
  849. zero_size_allowed, meta);
  850. }
  851. return err;
  852. }
  853. static int check_map_func_compatibility(struct bpf_map *map, int func_id)
  854. {
  855. bool bool_map, bool_func;
  856. int i;
  857. if (!map)
  858. return 0;
  859. for (i = 0; i < ARRAY_SIZE(func_limit); i++) {
  860. bool_map = (map->map_type == func_limit[i].map_type);
  861. bool_func = (func_id == func_limit[i].func_id);
  862. /* only when map & func pair match it can continue.
  863. * don't allow any other map type to be passed into
  864. * the special func;
  865. */
  866. if (bool_func && bool_map != bool_func) {
  867. verbose("cannot pass map_type %d into func %d\n",
  868. map->map_type, func_id);
  869. return -EINVAL;
  870. }
  871. }
  872. return 0;
  873. }
  874. static int check_raw_mode(const struct bpf_func_proto *fn)
  875. {
  876. int count = 0;
  877. if (fn->arg1_type == ARG_PTR_TO_RAW_STACK)
  878. count++;
  879. if (fn->arg2_type == ARG_PTR_TO_RAW_STACK)
  880. count++;
  881. if (fn->arg3_type == ARG_PTR_TO_RAW_STACK)
  882. count++;
  883. if (fn->arg4_type == ARG_PTR_TO_RAW_STACK)
  884. count++;
  885. if (fn->arg5_type == ARG_PTR_TO_RAW_STACK)
  886. count++;
  887. return count > 1 ? -EINVAL : 0;
  888. }
  889. static int check_call(struct verifier_env *env, int func_id)
  890. {
  891. struct verifier_state *state = &env->cur_state;
  892. const struct bpf_func_proto *fn = NULL;
  893. struct reg_state *regs = state->regs;
  894. struct reg_state *reg;
  895. struct bpf_call_arg_meta meta;
  896. int i, err;
  897. /* find function prototype */
  898. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  899. verbose("invalid func %d\n", func_id);
  900. return -EINVAL;
  901. }
  902. if (env->prog->aux->ops->get_func_proto)
  903. fn = env->prog->aux->ops->get_func_proto(func_id);
  904. if (!fn) {
  905. verbose("unknown func %d\n", func_id);
  906. return -EINVAL;
  907. }
  908. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  909. if (!env->prog->gpl_compatible && fn->gpl_only) {
  910. verbose("cannot call GPL only function from proprietary program\n");
  911. return -EINVAL;
  912. }
  913. memset(&meta, 0, sizeof(meta));
  914. /* We only support one arg being in raw mode at the moment, which
  915. * is sufficient for the helper functions we have right now.
  916. */
  917. err = check_raw_mode(fn);
  918. if (err) {
  919. verbose("kernel subsystem misconfigured func %d\n", func_id);
  920. return err;
  921. }
  922. /* check args */
  923. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
  924. if (err)
  925. return err;
  926. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
  927. if (err)
  928. return err;
  929. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
  930. if (err)
  931. return err;
  932. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
  933. if (err)
  934. return err;
  935. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
  936. if (err)
  937. return err;
  938. /* Mark slots with STACK_MISC in case of raw mode, stack offset
  939. * is inferred from register state.
  940. */
  941. for (i = 0; i < meta.access_size; i++) {
  942. err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
  943. if (err)
  944. return err;
  945. }
  946. /* reset caller saved regs */
  947. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  948. reg = regs + caller_saved[i];
  949. reg->type = NOT_INIT;
  950. reg->imm = 0;
  951. }
  952. /* update return register */
  953. if (fn->ret_type == RET_INTEGER) {
  954. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  955. } else if (fn->ret_type == RET_VOID) {
  956. regs[BPF_REG_0].type = NOT_INIT;
  957. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  958. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  959. /* remember map_ptr, so that check_map_access()
  960. * can check 'value_size' boundary of memory access
  961. * to map element returned from bpf_map_lookup_elem()
  962. */
  963. if (meta.map_ptr == NULL) {
  964. verbose("kernel subsystem misconfigured verifier\n");
  965. return -EINVAL;
  966. }
  967. regs[BPF_REG_0].map_ptr = meta.map_ptr;
  968. } else {
  969. verbose("unknown return type %d of func %d\n",
  970. fn->ret_type, func_id);
  971. return -EINVAL;
  972. }
  973. err = check_map_func_compatibility(meta.map_ptr, func_id);
  974. if (err)
  975. return err;
  976. return 0;
  977. }
  978. /* check validity of 32-bit and 64-bit arithmetic operations */
  979. static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn)
  980. {
  981. struct reg_state *regs = env->cur_state.regs;
  982. u8 opcode = BPF_OP(insn->code);
  983. int err;
  984. if (opcode == BPF_END || opcode == BPF_NEG) {
  985. if (opcode == BPF_NEG) {
  986. if (BPF_SRC(insn->code) != 0 ||
  987. insn->src_reg != BPF_REG_0 ||
  988. insn->off != 0 || insn->imm != 0) {
  989. verbose("BPF_NEG uses reserved fields\n");
  990. return -EINVAL;
  991. }
  992. } else {
  993. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  994. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
  995. verbose("BPF_END uses reserved fields\n");
  996. return -EINVAL;
  997. }
  998. }
  999. /* check src operand */
  1000. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1001. if (err)
  1002. return err;
  1003. if (is_pointer_value(env, insn->dst_reg)) {
  1004. verbose("R%d pointer arithmetic prohibited\n",
  1005. insn->dst_reg);
  1006. return -EACCES;
  1007. }
  1008. /* check dest operand */
  1009. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1010. if (err)
  1011. return err;
  1012. } else if (opcode == BPF_MOV) {
  1013. if (BPF_SRC(insn->code) == BPF_X) {
  1014. if (insn->imm != 0 || insn->off != 0) {
  1015. verbose("BPF_MOV uses reserved fields\n");
  1016. return -EINVAL;
  1017. }
  1018. /* check src operand */
  1019. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1020. if (err)
  1021. return err;
  1022. } else {
  1023. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1024. verbose("BPF_MOV uses reserved fields\n");
  1025. return -EINVAL;
  1026. }
  1027. }
  1028. /* check dest operand */
  1029. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1030. if (err)
  1031. return err;
  1032. if (BPF_SRC(insn->code) == BPF_X) {
  1033. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  1034. /* case: R1 = R2
  1035. * copy register state to dest reg
  1036. */
  1037. regs[insn->dst_reg] = regs[insn->src_reg];
  1038. } else {
  1039. if (is_pointer_value(env, insn->src_reg)) {
  1040. verbose("R%d partial copy of pointer\n",
  1041. insn->src_reg);
  1042. return -EACCES;
  1043. }
  1044. regs[insn->dst_reg].type = UNKNOWN_VALUE;
  1045. regs[insn->dst_reg].map_ptr = NULL;
  1046. }
  1047. } else {
  1048. /* case: R = imm
  1049. * remember the value we stored into this reg
  1050. */
  1051. regs[insn->dst_reg].type = CONST_IMM;
  1052. regs[insn->dst_reg].imm = insn->imm;
  1053. }
  1054. } else if (opcode > BPF_END) {
  1055. verbose("invalid BPF_ALU opcode %x\n", opcode);
  1056. return -EINVAL;
  1057. } else { /* all other ALU ops: and, sub, xor, add, ... */
  1058. bool stack_relative = false;
  1059. if (BPF_SRC(insn->code) == BPF_X) {
  1060. if (insn->imm != 0 || insn->off != 0) {
  1061. verbose("BPF_ALU uses reserved fields\n");
  1062. return -EINVAL;
  1063. }
  1064. /* check src1 operand */
  1065. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1066. if (err)
  1067. return err;
  1068. } else {
  1069. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1070. verbose("BPF_ALU uses reserved fields\n");
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. /* check src2 operand */
  1075. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1076. if (err)
  1077. return err;
  1078. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  1079. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  1080. verbose("div by zero\n");
  1081. return -EINVAL;
  1082. }
  1083. if ((opcode == BPF_LSH || opcode == BPF_RSH ||
  1084. opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
  1085. int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
  1086. if (insn->imm < 0 || insn->imm >= size) {
  1087. verbose("invalid shift %d\n", insn->imm);
  1088. return -EINVAL;
  1089. }
  1090. }
  1091. /* pattern match 'bpf_add Rx, imm' instruction */
  1092. if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
  1093. regs[insn->dst_reg].type == FRAME_PTR &&
  1094. BPF_SRC(insn->code) == BPF_K) {
  1095. stack_relative = true;
  1096. } else if (is_pointer_value(env, insn->dst_reg)) {
  1097. verbose("R%d pointer arithmetic prohibited\n",
  1098. insn->dst_reg);
  1099. return -EACCES;
  1100. } else if (BPF_SRC(insn->code) == BPF_X &&
  1101. is_pointer_value(env, insn->src_reg)) {
  1102. verbose("R%d pointer arithmetic prohibited\n",
  1103. insn->src_reg);
  1104. return -EACCES;
  1105. }
  1106. /* check dest operand */
  1107. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1108. if (err)
  1109. return err;
  1110. if (stack_relative) {
  1111. regs[insn->dst_reg].type = PTR_TO_STACK;
  1112. regs[insn->dst_reg].imm = insn->imm;
  1113. }
  1114. }
  1115. return 0;
  1116. }
  1117. static int check_cond_jmp_op(struct verifier_env *env,
  1118. struct bpf_insn *insn, int *insn_idx)
  1119. {
  1120. struct reg_state *regs = env->cur_state.regs;
  1121. struct verifier_state *other_branch;
  1122. u8 opcode = BPF_OP(insn->code);
  1123. int err;
  1124. if (opcode > BPF_EXIT) {
  1125. verbose("invalid BPF_JMP opcode %x\n", opcode);
  1126. return -EINVAL;
  1127. }
  1128. if (BPF_SRC(insn->code) == BPF_X) {
  1129. if (insn->imm != 0) {
  1130. verbose("BPF_JMP uses reserved fields\n");
  1131. return -EINVAL;
  1132. }
  1133. /* check src1 operand */
  1134. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1135. if (err)
  1136. return err;
  1137. if (is_pointer_value(env, insn->src_reg)) {
  1138. verbose("R%d pointer comparison prohibited\n",
  1139. insn->src_reg);
  1140. return -EACCES;
  1141. }
  1142. } else {
  1143. if (insn->src_reg != BPF_REG_0) {
  1144. verbose("BPF_JMP uses reserved fields\n");
  1145. return -EINVAL;
  1146. }
  1147. }
  1148. /* check src2 operand */
  1149. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1150. if (err)
  1151. return err;
  1152. /* detect if R == 0 where R was initialized to zero earlier */
  1153. if (BPF_SRC(insn->code) == BPF_K &&
  1154. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  1155. regs[insn->dst_reg].type == CONST_IMM &&
  1156. regs[insn->dst_reg].imm == insn->imm) {
  1157. if (opcode == BPF_JEQ) {
  1158. /* if (imm == imm) goto pc+off;
  1159. * only follow the goto, ignore fall-through
  1160. */
  1161. *insn_idx += insn->off;
  1162. return 0;
  1163. } else {
  1164. /* if (imm != imm) goto pc+off;
  1165. * only follow fall-through branch, since
  1166. * that's where the program will go
  1167. */
  1168. return 0;
  1169. }
  1170. }
  1171. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  1172. if (!other_branch)
  1173. return -EFAULT;
  1174. /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */
  1175. if (BPF_SRC(insn->code) == BPF_K &&
  1176. insn->imm == 0 && (opcode == BPF_JEQ ||
  1177. opcode == BPF_JNE) &&
  1178. regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) {
  1179. if (opcode == BPF_JEQ) {
  1180. /* next fallthrough insn can access memory via
  1181. * this register
  1182. */
  1183. regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
  1184. /* branch targer cannot access it, since reg == 0 */
  1185. other_branch->regs[insn->dst_reg].type = CONST_IMM;
  1186. other_branch->regs[insn->dst_reg].imm = 0;
  1187. } else {
  1188. other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
  1189. regs[insn->dst_reg].type = CONST_IMM;
  1190. regs[insn->dst_reg].imm = 0;
  1191. }
  1192. } else if (is_pointer_value(env, insn->dst_reg)) {
  1193. verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
  1194. return -EACCES;
  1195. } else if (BPF_SRC(insn->code) == BPF_K &&
  1196. (opcode == BPF_JEQ || opcode == BPF_JNE)) {
  1197. if (opcode == BPF_JEQ) {
  1198. /* detect if (R == imm) goto
  1199. * and in the target state recognize that R = imm
  1200. */
  1201. other_branch->regs[insn->dst_reg].type = CONST_IMM;
  1202. other_branch->regs[insn->dst_reg].imm = insn->imm;
  1203. } else {
  1204. /* detect if (R != imm) goto
  1205. * and in the fall-through state recognize that R = imm
  1206. */
  1207. regs[insn->dst_reg].type = CONST_IMM;
  1208. regs[insn->dst_reg].imm = insn->imm;
  1209. }
  1210. }
  1211. if (log_level)
  1212. print_verifier_state(env);
  1213. return 0;
  1214. }
  1215. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  1216. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  1217. {
  1218. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  1219. return (struct bpf_map *) (unsigned long) imm64;
  1220. }
  1221. /* verify BPF_LD_IMM64 instruction */
  1222. static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)
  1223. {
  1224. struct reg_state *regs = env->cur_state.regs;
  1225. int err;
  1226. if (BPF_SIZE(insn->code) != BPF_DW) {
  1227. verbose("invalid BPF_LD_IMM insn\n");
  1228. return -EINVAL;
  1229. }
  1230. if (insn->off != 0) {
  1231. verbose("BPF_LD_IMM64 uses reserved fields\n");
  1232. return -EINVAL;
  1233. }
  1234. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1235. if (err)
  1236. return err;
  1237. if (insn->src_reg == 0)
  1238. /* generic move 64-bit immediate into a register */
  1239. return 0;
  1240. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  1241. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  1242. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  1243. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  1244. return 0;
  1245. }
  1246. static bool may_access_skb(enum bpf_prog_type type)
  1247. {
  1248. switch (type) {
  1249. case BPF_PROG_TYPE_SOCKET_FILTER:
  1250. case BPF_PROG_TYPE_SCHED_CLS:
  1251. case BPF_PROG_TYPE_SCHED_ACT:
  1252. return true;
  1253. default:
  1254. return false;
  1255. }
  1256. }
  1257. /* verify safety of LD_ABS|LD_IND instructions:
  1258. * - they can only appear in the programs where ctx == skb
  1259. * - since they are wrappers of function calls, they scratch R1-R5 registers,
  1260. * preserve R6-R9, and store return value into R0
  1261. *
  1262. * Implicit input:
  1263. * ctx == skb == R6 == CTX
  1264. *
  1265. * Explicit input:
  1266. * SRC == any register
  1267. * IMM == 32-bit immediate
  1268. *
  1269. * Output:
  1270. * R0 - 8/16/32-bit skb data converted to cpu endianness
  1271. */
  1272. static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn)
  1273. {
  1274. struct reg_state *regs = env->cur_state.regs;
  1275. u8 mode = BPF_MODE(insn->code);
  1276. struct reg_state *reg;
  1277. int i, err;
  1278. if (!may_access_skb(env->prog->type)) {
  1279. verbose("BPF_LD_ABS|IND instructions not allowed for this program type\n");
  1280. return -EINVAL;
  1281. }
  1282. if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
  1283. (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
  1284. verbose("BPF_LD_ABS uses reserved fields\n");
  1285. return -EINVAL;
  1286. }
  1287. /* check whether implicit source operand (register R6) is readable */
  1288. err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
  1289. if (err)
  1290. return err;
  1291. if (regs[BPF_REG_6].type != PTR_TO_CTX) {
  1292. verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
  1293. return -EINVAL;
  1294. }
  1295. if (mode == BPF_IND) {
  1296. /* check explicit source operand */
  1297. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1298. if (err)
  1299. return err;
  1300. }
  1301. /* reset caller saved regs to unreadable */
  1302. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  1303. reg = regs + caller_saved[i];
  1304. reg->type = NOT_INIT;
  1305. reg->imm = 0;
  1306. }
  1307. /* mark destination R0 register as readable, since it contains
  1308. * the value fetched from the packet
  1309. */
  1310. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  1311. return 0;
  1312. }
  1313. /* non-recursive DFS pseudo code
  1314. * 1 procedure DFS-iterative(G,v):
  1315. * 2 label v as discovered
  1316. * 3 let S be a stack
  1317. * 4 S.push(v)
  1318. * 5 while S is not empty
  1319. * 6 t <- S.pop()
  1320. * 7 if t is what we're looking for:
  1321. * 8 return t
  1322. * 9 for all edges e in G.adjacentEdges(t) do
  1323. * 10 if edge e is already labelled
  1324. * 11 continue with the next edge
  1325. * 12 w <- G.adjacentVertex(t,e)
  1326. * 13 if vertex w is not discovered and not explored
  1327. * 14 label e as tree-edge
  1328. * 15 label w as discovered
  1329. * 16 S.push(w)
  1330. * 17 continue at 5
  1331. * 18 else if vertex w is discovered
  1332. * 19 label e as back-edge
  1333. * 20 else
  1334. * 21 // vertex w is explored
  1335. * 22 label e as forward- or cross-edge
  1336. * 23 label t as explored
  1337. * 24 S.pop()
  1338. *
  1339. * convention:
  1340. * 0x10 - discovered
  1341. * 0x11 - discovered and fall-through edge labelled
  1342. * 0x12 - discovered and fall-through and branch edges labelled
  1343. * 0x20 - explored
  1344. */
  1345. enum {
  1346. DISCOVERED = 0x10,
  1347. EXPLORED = 0x20,
  1348. FALLTHROUGH = 1,
  1349. BRANCH = 2,
  1350. };
  1351. #define STATE_LIST_MARK ((struct verifier_state_list *) -1L)
  1352. static int *insn_stack; /* stack of insns to process */
  1353. static int cur_stack; /* current stack index */
  1354. static int *insn_state;
  1355. /* t, w, e - match pseudo-code above:
  1356. * t - index of current instruction
  1357. * w - next instruction
  1358. * e - edge
  1359. */
  1360. static int push_insn(int t, int w, int e, struct verifier_env *env)
  1361. {
  1362. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  1363. return 0;
  1364. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  1365. return 0;
  1366. if (w < 0 || w >= env->prog->len) {
  1367. verbose("jump out of range from insn %d to %d\n", t, w);
  1368. return -EINVAL;
  1369. }
  1370. if (e == BRANCH)
  1371. /* mark branch target for state pruning */
  1372. env->explored_states[w] = STATE_LIST_MARK;
  1373. if (insn_state[w] == 0) {
  1374. /* tree-edge */
  1375. insn_state[t] = DISCOVERED | e;
  1376. insn_state[w] = DISCOVERED;
  1377. if (cur_stack >= env->prog->len)
  1378. return -E2BIG;
  1379. insn_stack[cur_stack++] = w;
  1380. return 1;
  1381. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  1382. verbose("back-edge from insn %d to %d\n", t, w);
  1383. return -EINVAL;
  1384. } else if (insn_state[w] == EXPLORED) {
  1385. /* forward- or cross-edge */
  1386. insn_state[t] = DISCOVERED | e;
  1387. } else {
  1388. verbose("insn state internal bug\n");
  1389. return -EFAULT;
  1390. }
  1391. return 0;
  1392. }
  1393. /* non-recursive depth-first-search to detect loops in BPF program
  1394. * loop == back-edge in directed graph
  1395. */
  1396. static int check_cfg(struct verifier_env *env)
  1397. {
  1398. struct bpf_insn *insns = env->prog->insnsi;
  1399. int insn_cnt = env->prog->len;
  1400. int ret = 0;
  1401. int i, t;
  1402. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  1403. if (!insn_state)
  1404. return -ENOMEM;
  1405. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  1406. if (!insn_stack) {
  1407. kfree(insn_state);
  1408. return -ENOMEM;
  1409. }
  1410. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  1411. insn_stack[0] = 0; /* 0 is the first instruction */
  1412. cur_stack = 1;
  1413. peek_stack:
  1414. if (cur_stack == 0)
  1415. goto check_state;
  1416. t = insn_stack[cur_stack - 1];
  1417. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  1418. u8 opcode = BPF_OP(insns[t].code);
  1419. if (opcode == BPF_EXIT) {
  1420. goto mark_explored;
  1421. } else if (opcode == BPF_CALL) {
  1422. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1423. if (ret == 1)
  1424. goto peek_stack;
  1425. else if (ret < 0)
  1426. goto err_free;
  1427. if (t + 1 < insn_cnt)
  1428. env->explored_states[t + 1] = STATE_LIST_MARK;
  1429. } else if (opcode == BPF_JA) {
  1430. if (BPF_SRC(insns[t].code) != BPF_K) {
  1431. ret = -EINVAL;
  1432. goto err_free;
  1433. }
  1434. /* unconditional jump with single edge */
  1435. ret = push_insn(t, t + insns[t].off + 1,
  1436. FALLTHROUGH, env);
  1437. if (ret == 1)
  1438. goto peek_stack;
  1439. else if (ret < 0)
  1440. goto err_free;
  1441. /* tell verifier to check for equivalent states
  1442. * after every call and jump
  1443. */
  1444. if (t + 1 < insn_cnt)
  1445. env->explored_states[t + 1] = STATE_LIST_MARK;
  1446. } else {
  1447. /* conditional jump with two edges */
  1448. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1449. if (ret == 1)
  1450. goto peek_stack;
  1451. else if (ret < 0)
  1452. goto err_free;
  1453. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  1454. if (ret == 1)
  1455. goto peek_stack;
  1456. else if (ret < 0)
  1457. goto err_free;
  1458. }
  1459. } else {
  1460. /* all other non-branch instructions with single
  1461. * fall-through edge
  1462. */
  1463. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1464. if (ret == 1)
  1465. goto peek_stack;
  1466. else if (ret < 0)
  1467. goto err_free;
  1468. }
  1469. mark_explored:
  1470. insn_state[t] = EXPLORED;
  1471. if (cur_stack-- <= 0) {
  1472. verbose("pop stack internal bug\n");
  1473. ret = -EFAULT;
  1474. goto err_free;
  1475. }
  1476. goto peek_stack;
  1477. check_state:
  1478. for (i = 0; i < insn_cnt; i++) {
  1479. if (insn_state[i] != EXPLORED) {
  1480. verbose("unreachable insn %d\n", i);
  1481. ret = -EINVAL;
  1482. goto err_free;
  1483. }
  1484. }
  1485. ret = 0; /* cfg looks good */
  1486. err_free:
  1487. kfree(insn_state);
  1488. kfree(insn_stack);
  1489. return ret;
  1490. }
  1491. /* compare two verifier states
  1492. *
  1493. * all states stored in state_list are known to be valid, since
  1494. * verifier reached 'bpf_exit' instruction through them
  1495. *
  1496. * this function is called when verifier exploring different branches of
  1497. * execution popped from the state stack. If it sees an old state that has
  1498. * more strict register state and more strict stack state then this execution
  1499. * branch doesn't need to be explored further, since verifier already
  1500. * concluded that more strict state leads to valid finish.
  1501. *
  1502. * Therefore two states are equivalent if register state is more conservative
  1503. * and explored stack state is more conservative than the current one.
  1504. * Example:
  1505. * explored current
  1506. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  1507. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  1508. *
  1509. * In other words if current stack state (one being explored) has more
  1510. * valid slots than old one that already passed validation, it means
  1511. * the verifier can stop exploring and conclude that current state is valid too
  1512. *
  1513. * Similarly with registers. If explored state has register type as invalid
  1514. * whereas register type in current state is meaningful, it means that
  1515. * the current state will reach 'bpf_exit' instruction safely
  1516. */
  1517. static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
  1518. {
  1519. int i;
  1520. for (i = 0; i < MAX_BPF_REG; i++) {
  1521. if (memcmp(&old->regs[i], &cur->regs[i],
  1522. sizeof(old->regs[0])) != 0) {
  1523. if (old->regs[i].type == NOT_INIT ||
  1524. (old->regs[i].type == UNKNOWN_VALUE &&
  1525. cur->regs[i].type != NOT_INIT))
  1526. continue;
  1527. return false;
  1528. }
  1529. }
  1530. for (i = 0; i < MAX_BPF_STACK; i++) {
  1531. if (old->stack_slot_type[i] == STACK_INVALID)
  1532. continue;
  1533. if (old->stack_slot_type[i] != cur->stack_slot_type[i])
  1534. /* Ex: old explored (safe) state has STACK_SPILL in
  1535. * this stack slot, but current has has STACK_MISC ->
  1536. * this verifier states are not equivalent,
  1537. * return false to continue verification of this path
  1538. */
  1539. return false;
  1540. if (i % BPF_REG_SIZE)
  1541. continue;
  1542. if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
  1543. &cur->spilled_regs[i / BPF_REG_SIZE],
  1544. sizeof(old->spilled_regs[0])))
  1545. /* when explored and current stack slot types are
  1546. * the same, check that stored pointers types
  1547. * are the same as well.
  1548. * Ex: explored safe path could have stored
  1549. * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8}
  1550. * but current path has stored:
  1551. * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16}
  1552. * such verifier states are not equivalent.
  1553. * return false to continue verification of this path
  1554. */
  1555. return false;
  1556. else
  1557. continue;
  1558. }
  1559. return true;
  1560. }
  1561. static int is_state_visited(struct verifier_env *env, int insn_idx)
  1562. {
  1563. struct verifier_state_list *new_sl;
  1564. struct verifier_state_list *sl;
  1565. sl = env->explored_states[insn_idx];
  1566. if (!sl)
  1567. /* this 'insn_idx' instruction wasn't marked, so we will not
  1568. * be doing state search here
  1569. */
  1570. return 0;
  1571. while (sl != STATE_LIST_MARK) {
  1572. if (states_equal(&sl->state, &env->cur_state))
  1573. /* reached equivalent register/stack state,
  1574. * prune the search
  1575. */
  1576. return 1;
  1577. sl = sl->next;
  1578. }
  1579. /* there were no equivalent states, remember current one.
  1580. * technically the current state is not proven to be safe yet,
  1581. * but it will either reach bpf_exit (which means it's safe) or
  1582. * it will be rejected. Since there are no loops, we won't be
  1583. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  1584. */
  1585. new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER);
  1586. if (!new_sl)
  1587. return -ENOMEM;
  1588. /* add new state to the head of linked list */
  1589. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  1590. new_sl->next = env->explored_states[insn_idx];
  1591. env->explored_states[insn_idx] = new_sl;
  1592. return 0;
  1593. }
  1594. static int do_check(struct verifier_env *env)
  1595. {
  1596. struct verifier_state *state = &env->cur_state;
  1597. struct bpf_insn *insns = env->prog->insnsi;
  1598. struct reg_state *regs = state->regs;
  1599. int insn_cnt = env->prog->len;
  1600. int insn_idx, prev_insn_idx = 0;
  1601. int insn_processed = 0;
  1602. bool do_print_state = false;
  1603. init_reg_state(regs);
  1604. insn_idx = 0;
  1605. for (;;) {
  1606. struct bpf_insn *insn;
  1607. u8 class;
  1608. int err;
  1609. if (insn_idx >= insn_cnt) {
  1610. verbose("invalid insn idx %d insn_cnt %d\n",
  1611. insn_idx, insn_cnt);
  1612. return -EFAULT;
  1613. }
  1614. insn = &insns[insn_idx];
  1615. class = BPF_CLASS(insn->code);
  1616. if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
  1617. verbose("BPF program is too large. Proccessed %d insn\n",
  1618. insn_processed);
  1619. return -E2BIG;
  1620. }
  1621. err = is_state_visited(env, insn_idx);
  1622. if (err < 0)
  1623. return err;
  1624. if (err == 1) {
  1625. /* found equivalent state, can prune the search */
  1626. if (log_level) {
  1627. if (do_print_state)
  1628. verbose("\nfrom %d to %d: safe\n",
  1629. prev_insn_idx, insn_idx);
  1630. else
  1631. verbose("%d: safe\n", insn_idx);
  1632. }
  1633. goto process_bpf_exit;
  1634. }
  1635. if (log_level && do_print_state) {
  1636. verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
  1637. print_verifier_state(env);
  1638. do_print_state = false;
  1639. }
  1640. if (log_level) {
  1641. verbose("%d: ", insn_idx);
  1642. print_bpf_insn(insn);
  1643. }
  1644. if (class == BPF_ALU || class == BPF_ALU64) {
  1645. err = check_alu_op(env, insn);
  1646. if (err)
  1647. return err;
  1648. } else if (class == BPF_LDX) {
  1649. enum bpf_reg_type src_reg_type;
  1650. /* check for reserved fields is already done */
  1651. /* check src operand */
  1652. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1653. if (err)
  1654. return err;
  1655. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  1656. if (err)
  1657. return err;
  1658. src_reg_type = regs[insn->src_reg].type;
  1659. /* check that memory (src_reg + off) is readable,
  1660. * the state of dst_reg will be updated by this func
  1661. */
  1662. err = check_mem_access(env, insn->src_reg, insn->off,
  1663. BPF_SIZE(insn->code), BPF_READ,
  1664. insn->dst_reg);
  1665. if (err)
  1666. return err;
  1667. if (BPF_SIZE(insn->code) != BPF_W) {
  1668. insn_idx++;
  1669. continue;
  1670. }
  1671. if (insn->imm == 0) {
  1672. /* saw a valid insn
  1673. * dst_reg = *(u32 *)(src_reg + off)
  1674. * use reserved 'imm' field to mark this insn
  1675. */
  1676. insn->imm = src_reg_type;
  1677. } else if (src_reg_type != insn->imm &&
  1678. (src_reg_type == PTR_TO_CTX ||
  1679. insn->imm == PTR_TO_CTX)) {
  1680. /* ABuser program is trying to use the same insn
  1681. * dst_reg = *(u32*) (src_reg + off)
  1682. * with different pointer types:
  1683. * src_reg == ctx in one branch and
  1684. * src_reg == stack|map in some other branch.
  1685. * Reject it.
  1686. */
  1687. verbose("same insn cannot be used with different pointers\n");
  1688. return -EINVAL;
  1689. }
  1690. } else if (class == BPF_STX) {
  1691. enum bpf_reg_type dst_reg_type;
  1692. if (BPF_MODE(insn->code) == BPF_XADD) {
  1693. err = check_xadd(env, insn);
  1694. if (err)
  1695. return err;
  1696. insn_idx++;
  1697. continue;
  1698. }
  1699. /* check src1 operand */
  1700. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1701. if (err)
  1702. return err;
  1703. /* check src2 operand */
  1704. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1705. if (err)
  1706. return err;
  1707. dst_reg_type = regs[insn->dst_reg].type;
  1708. /* check that memory (dst_reg + off) is writeable */
  1709. err = check_mem_access(env, insn->dst_reg, insn->off,
  1710. BPF_SIZE(insn->code), BPF_WRITE,
  1711. insn->src_reg);
  1712. if (err)
  1713. return err;
  1714. if (insn->imm == 0) {
  1715. insn->imm = dst_reg_type;
  1716. } else if (dst_reg_type != insn->imm &&
  1717. (dst_reg_type == PTR_TO_CTX ||
  1718. insn->imm == PTR_TO_CTX)) {
  1719. verbose("same insn cannot be used with different pointers\n");
  1720. return -EINVAL;
  1721. }
  1722. } else if (class == BPF_ST) {
  1723. if (BPF_MODE(insn->code) != BPF_MEM ||
  1724. insn->src_reg != BPF_REG_0) {
  1725. verbose("BPF_ST uses reserved fields\n");
  1726. return -EINVAL;
  1727. }
  1728. /* check src operand */
  1729. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1730. if (err)
  1731. return err;
  1732. /* check that memory (dst_reg + off) is writeable */
  1733. err = check_mem_access(env, insn->dst_reg, insn->off,
  1734. BPF_SIZE(insn->code), BPF_WRITE,
  1735. -1);
  1736. if (err)
  1737. return err;
  1738. } else if (class == BPF_JMP) {
  1739. u8 opcode = BPF_OP(insn->code);
  1740. if (opcode == BPF_CALL) {
  1741. if (BPF_SRC(insn->code) != BPF_K ||
  1742. insn->off != 0 ||
  1743. insn->src_reg != BPF_REG_0 ||
  1744. insn->dst_reg != BPF_REG_0) {
  1745. verbose("BPF_CALL uses reserved fields\n");
  1746. return -EINVAL;
  1747. }
  1748. err = check_call(env, insn->imm);
  1749. if (err)
  1750. return err;
  1751. } else if (opcode == BPF_JA) {
  1752. if (BPF_SRC(insn->code) != BPF_K ||
  1753. insn->imm != 0 ||
  1754. insn->src_reg != BPF_REG_0 ||
  1755. insn->dst_reg != BPF_REG_0) {
  1756. verbose("BPF_JA uses reserved fields\n");
  1757. return -EINVAL;
  1758. }
  1759. insn_idx += insn->off + 1;
  1760. continue;
  1761. } else if (opcode == BPF_EXIT) {
  1762. if (BPF_SRC(insn->code) != BPF_K ||
  1763. insn->imm != 0 ||
  1764. insn->src_reg != BPF_REG_0 ||
  1765. insn->dst_reg != BPF_REG_0) {
  1766. verbose("BPF_EXIT uses reserved fields\n");
  1767. return -EINVAL;
  1768. }
  1769. /* eBPF calling convetion is such that R0 is used
  1770. * to return the value from eBPF program.
  1771. * Make sure that it's readable at this time
  1772. * of bpf_exit, which means that program wrote
  1773. * something into it earlier
  1774. */
  1775. err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
  1776. if (err)
  1777. return err;
  1778. if (is_pointer_value(env, BPF_REG_0)) {
  1779. verbose("R0 leaks addr as return value\n");
  1780. return -EACCES;
  1781. }
  1782. process_bpf_exit:
  1783. insn_idx = pop_stack(env, &prev_insn_idx);
  1784. if (insn_idx < 0) {
  1785. break;
  1786. } else {
  1787. do_print_state = true;
  1788. continue;
  1789. }
  1790. } else {
  1791. err = check_cond_jmp_op(env, insn, &insn_idx);
  1792. if (err)
  1793. return err;
  1794. }
  1795. } else if (class == BPF_LD) {
  1796. u8 mode = BPF_MODE(insn->code);
  1797. if (mode == BPF_ABS || mode == BPF_IND) {
  1798. err = check_ld_abs(env, insn);
  1799. if (err)
  1800. return err;
  1801. } else if (mode == BPF_IMM) {
  1802. err = check_ld_imm(env, insn);
  1803. if (err)
  1804. return err;
  1805. insn_idx++;
  1806. } else {
  1807. verbose("invalid BPF_LD mode\n");
  1808. return -EINVAL;
  1809. }
  1810. } else {
  1811. verbose("unknown insn class %d\n", class);
  1812. return -EINVAL;
  1813. }
  1814. insn_idx++;
  1815. }
  1816. return 0;
  1817. }
  1818. /* look for pseudo eBPF instructions that access map FDs and
  1819. * replace them with actual map pointers
  1820. */
  1821. static int replace_map_fd_with_map_ptr(struct verifier_env *env)
  1822. {
  1823. struct bpf_insn *insn = env->prog->insnsi;
  1824. int insn_cnt = env->prog->len;
  1825. int i, j;
  1826. for (i = 0; i < insn_cnt; i++, insn++) {
  1827. if (BPF_CLASS(insn->code) == BPF_LDX &&
  1828. (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
  1829. verbose("BPF_LDX uses reserved fields\n");
  1830. return -EINVAL;
  1831. }
  1832. if (BPF_CLASS(insn->code) == BPF_STX &&
  1833. ((BPF_MODE(insn->code) != BPF_MEM &&
  1834. BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
  1835. verbose("BPF_STX uses reserved fields\n");
  1836. return -EINVAL;
  1837. }
  1838. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  1839. struct bpf_map *map;
  1840. struct fd f;
  1841. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  1842. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  1843. insn[1].off != 0) {
  1844. verbose("invalid bpf_ld_imm64 insn\n");
  1845. return -EINVAL;
  1846. }
  1847. if (insn->src_reg == 0)
  1848. /* valid generic load 64-bit imm */
  1849. goto next_insn;
  1850. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  1851. verbose("unrecognized bpf_ld_imm64 insn\n");
  1852. return -EINVAL;
  1853. }
  1854. f = fdget(insn->imm);
  1855. map = __bpf_map_get(f);
  1856. if (IS_ERR(map)) {
  1857. verbose("fd %d is not pointing to valid bpf_map\n",
  1858. insn->imm);
  1859. fdput(f);
  1860. return PTR_ERR(map);
  1861. }
  1862. /* store map pointer inside BPF_LD_IMM64 instruction */
  1863. insn[0].imm = (u32) (unsigned long) map;
  1864. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  1865. /* check whether we recorded this map already */
  1866. for (j = 0; j < env->used_map_cnt; j++)
  1867. if (env->used_maps[j] == map) {
  1868. fdput(f);
  1869. goto next_insn;
  1870. }
  1871. if (env->used_map_cnt >= MAX_USED_MAPS) {
  1872. fdput(f);
  1873. return -E2BIG;
  1874. }
  1875. /* remember this map */
  1876. env->used_maps[env->used_map_cnt++] = map;
  1877. /* hold the map. If the program is rejected by verifier,
  1878. * the map will be released by release_maps() or it
  1879. * will be used by the valid program until it's unloaded
  1880. * and all maps are released in free_bpf_prog_info()
  1881. */
  1882. bpf_map_inc(map, false);
  1883. fdput(f);
  1884. next_insn:
  1885. insn++;
  1886. i++;
  1887. }
  1888. }
  1889. /* now all pseudo BPF_LD_IMM64 instructions load valid
  1890. * 'struct bpf_map *' into a register instead of user map_fd.
  1891. * These pointers will be used later by verifier to validate map access.
  1892. */
  1893. return 0;
  1894. }
  1895. /* drop refcnt of maps used by the rejected program */
  1896. static void release_maps(struct verifier_env *env)
  1897. {
  1898. int i;
  1899. for (i = 0; i < env->used_map_cnt; i++)
  1900. bpf_map_put(env->used_maps[i]);
  1901. }
  1902. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  1903. static void convert_pseudo_ld_imm64(struct verifier_env *env)
  1904. {
  1905. struct bpf_insn *insn = env->prog->insnsi;
  1906. int insn_cnt = env->prog->len;
  1907. int i;
  1908. for (i = 0; i < insn_cnt; i++, insn++)
  1909. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  1910. insn->src_reg = 0;
  1911. }
  1912. static void adjust_branches(struct bpf_prog *prog, int pos, int delta)
  1913. {
  1914. struct bpf_insn *insn = prog->insnsi;
  1915. int insn_cnt = prog->len;
  1916. int i;
  1917. for (i = 0; i < insn_cnt; i++, insn++) {
  1918. if (BPF_CLASS(insn->code) != BPF_JMP ||
  1919. BPF_OP(insn->code) == BPF_CALL ||
  1920. BPF_OP(insn->code) == BPF_EXIT)
  1921. continue;
  1922. /* adjust offset of jmps if necessary */
  1923. if (i < pos && i + insn->off + 1 > pos)
  1924. insn->off += delta;
  1925. else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
  1926. insn->off -= delta;
  1927. }
  1928. }
  1929. /* convert load instructions that access fields of 'struct __sk_buff'
  1930. * into sequence of instructions that access fields of 'struct sk_buff'
  1931. */
  1932. static int convert_ctx_accesses(struct verifier_env *env)
  1933. {
  1934. struct bpf_insn *insn = env->prog->insnsi;
  1935. int insn_cnt = env->prog->len;
  1936. struct bpf_insn insn_buf[16];
  1937. struct bpf_prog *new_prog;
  1938. u32 cnt;
  1939. int i;
  1940. enum bpf_access_type type;
  1941. if (!env->prog->aux->ops->convert_ctx_access)
  1942. return 0;
  1943. for (i = 0; i < insn_cnt; i++, insn++) {
  1944. if (insn->code == (BPF_LDX | BPF_MEM | BPF_W))
  1945. type = BPF_READ;
  1946. else if (insn->code == (BPF_STX | BPF_MEM | BPF_W))
  1947. type = BPF_WRITE;
  1948. else
  1949. continue;
  1950. if (insn->imm != PTR_TO_CTX) {
  1951. /* clear internal mark */
  1952. insn->imm = 0;
  1953. continue;
  1954. }
  1955. cnt = env->prog->aux->ops->
  1956. convert_ctx_access(type, insn->dst_reg, insn->src_reg,
  1957. insn->off, insn_buf, env->prog);
  1958. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
  1959. verbose("bpf verifier is misconfigured\n");
  1960. return -EINVAL;
  1961. }
  1962. if (cnt == 1) {
  1963. memcpy(insn, insn_buf, sizeof(*insn));
  1964. continue;
  1965. }
  1966. /* several new insns need to be inserted. Make room for them */
  1967. insn_cnt += cnt - 1;
  1968. new_prog = bpf_prog_realloc(env->prog,
  1969. bpf_prog_size(insn_cnt),
  1970. GFP_USER);
  1971. if (!new_prog)
  1972. return -ENOMEM;
  1973. new_prog->len = insn_cnt;
  1974. memmove(new_prog->insnsi + i + cnt, new_prog->insns + i + 1,
  1975. sizeof(*insn) * (insn_cnt - i - cnt));
  1976. /* copy substitute insns in place of load instruction */
  1977. memcpy(new_prog->insnsi + i, insn_buf, sizeof(*insn) * cnt);
  1978. /* adjust branches in the whole program */
  1979. adjust_branches(new_prog, i, cnt - 1);
  1980. /* keep walking new program and skip insns we just inserted */
  1981. env->prog = new_prog;
  1982. insn = new_prog->insnsi + i + cnt - 1;
  1983. i += cnt - 1;
  1984. }
  1985. return 0;
  1986. }
  1987. static void free_states(struct verifier_env *env)
  1988. {
  1989. struct verifier_state_list *sl, *sln;
  1990. int i;
  1991. if (!env->explored_states)
  1992. return;
  1993. for (i = 0; i < env->prog->len; i++) {
  1994. sl = env->explored_states[i];
  1995. if (sl)
  1996. while (sl != STATE_LIST_MARK) {
  1997. sln = sl->next;
  1998. kfree(sl);
  1999. sl = sln;
  2000. }
  2001. }
  2002. kfree(env->explored_states);
  2003. }
  2004. int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  2005. {
  2006. char __user *log_ubuf = NULL;
  2007. struct verifier_env *env;
  2008. int ret = -EINVAL;
  2009. if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
  2010. return -E2BIG;
  2011. /* 'struct verifier_env' can be global, but since it's not small,
  2012. * allocate/free it every time bpf_check() is called
  2013. */
  2014. env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL);
  2015. if (!env)
  2016. return -ENOMEM;
  2017. env->prog = *prog;
  2018. /* grab the mutex to protect few globals used by verifier */
  2019. mutex_lock(&bpf_verifier_lock);
  2020. if (attr->log_level || attr->log_buf || attr->log_size) {
  2021. /* user requested verbose verifier output
  2022. * and supplied buffer to store the verification trace
  2023. */
  2024. log_level = attr->log_level;
  2025. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  2026. log_size = attr->log_size;
  2027. log_len = 0;
  2028. ret = -EINVAL;
  2029. /* log_* values have to be sane */
  2030. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  2031. log_level == 0 || log_ubuf == NULL)
  2032. goto free_env;
  2033. ret = -ENOMEM;
  2034. log_buf = vmalloc(log_size);
  2035. if (!log_buf)
  2036. goto free_env;
  2037. } else {
  2038. log_level = 0;
  2039. }
  2040. ret = replace_map_fd_with_map_ptr(env);
  2041. if (ret < 0)
  2042. goto skip_full_check;
  2043. env->explored_states = kcalloc(env->prog->len,
  2044. sizeof(struct verifier_state_list *),
  2045. GFP_USER);
  2046. ret = -ENOMEM;
  2047. if (!env->explored_states)
  2048. goto skip_full_check;
  2049. ret = check_cfg(env);
  2050. if (ret < 0)
  2051. goto skip_full_check;
  2052. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  2053. ret = do_check(env);
  2054. skip_full_check:
  2055. while (pop_stack(env, NULL) >= 0);
  2056. free_states(env);
  2057. if (ret == 0)
  2058. /* program is valid, convert *(u32*)(ctx + off) accesses */
  2059. ret = convert_ctx_accesses(env);
  2060. if (log_level && log_len >= log_size - 1) {
  2061. BUG_ON(log_len >= log_size);
  2062. /* verifier log exceeded user supplied buffer */
  2063. ret = -ENOSPC;
  2064. /* fall through to return what was recorded */
  2065. }
  2066. /* copy verifier log back to user space including trailing zero */
  2067. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  2068. ret = -EFAULT;
  2069. goto free_log_buf;
  2070. }
  2071. if (ret == 0 && env->used_map_cnt) {
  2072. /* if program passed verifier, update used_maps in bpf_prog_info */
  2073. env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  2074. sizeof(env->used_maps[0]),
  2075. GFP_KERNEL);
  2076. if (!env->prog->aux->used_maps) {
  2077. ret = -ENOMEM;
  2078. goto free_log_buf;
  2079. }
  2080. memcpy(env->prog->aux->used_maps, env->used_maps,
  2081. sizeof(env->used_maps[0]) * env->used_map_cnt);
  2082. env->prog->aux->used_map_cnt = env->used_map_cnt;
  2083. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  2084. * bpf_ld_imm64 instructions
  2085. */
  2086. convert_pseudo_ld_imm64(env);
  2087. }
  2088. free_log_buf:
  2089. if (log_level)
  2090. vfree(log_buf);
  2091. free_env:
  2092. if (!env->prog->aux->used_maps)
  2093. /* if we didn't copy map pointers into bpf_prog_info, release
  2094. * them now. Otherwise free_bpf_prog_info() will release them.
  2095. */
  2096. release_maps(env);
  2097. *prog = env->prog;
  2098. kfree(env);
  2099. mutex_unlock(&bpf_verifier_lock);
  2100. return ret;
  2101. }