verifier.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. int 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, /* 1st byte of register spilled into stack */
  152. STACK_SPILL_PART, /* other 7 bytes of register spill */
  153. STACK_MISC /* BPF program wrote some data into this slot */
  154. };
  155. struct bpf_stack_slot {
  156. enum bpf_stack_slot_type stype;
  157. struct reg_state reg_st;
  158. };
  159. /* state of the program:
  160. * type of all registers and stack info
  161. */
  162. struct verifier_state {
  163. struct reg_state regs[MAX_BPF_REG];
  164. struct bpf_stack_slot stack[MAX_BPF_STACK];
  165. };
  166. /* linked list of verifier states used to prune search */
  167. struct verifier_state_list {
  168. struct verifier_state state;
  169. struct verifier_state_list *next;
  170. };
  171. /* verifier_state + insn_idx are pushed to stack when branch is encountered */
  172. struct verifier_stack_elem {
  173. /* verifer state is 'st'
  174. * before processing instruction 'insn_idx'
  175. * and after processing instruction 'prev_insn_idx'
  176. */
  177. struct verifier_state st;
  178. int insn_idx;
  179. int prev_insn_idx;
  180. struct verifier_stack_elem *next;
  181. };
  182. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  183. /* single container for all structs
  184. * one verifier_env per bpf_check() call
  185. */
  186. struct verifier_env {
  187. struct bpf_prog *prog; /* eBPF program being verified */
  188. struct verifier_stack_elem *head; /* stack of verifier states to be processed */
  189. int stack_size; /* number of states to be processed */
  190. struct verifier_state cur_state; /* current verifier state */
  191. struct verifier_state_list **explored_states; /* search pruning optimization */
  192. struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
  193. u32 used_map_cnt; /* number of used maps */
  194. };
  195. /* verbose verifier prints what it's seeing
  196. * bpf_check() is called under lock, so no race to access these global vars
  197. */
  198. static u32 log_level, log_size, log_len;
  199. static char *log_buf;
  200. static DEFINE_MUTEX(bpf_verifier_lock);
  201. /* log_level controls verbosity level of eBPF verifier.
  202. * verbose() is used to dump the verification trace to the log, so the user
  203. * can figure out what's wrong with the program
  204. */
  205. static void verbose(const char *fmt, ...)
  206. {
  207. va_list args;
  208. if (log_level == 0 || log_len >= log_size - 1)
  209. return;
  210. va_start(args, fmt);
  211. log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
  212. va_end(args);
  213. }
  214. /* string representation of 'enum bpf_reg_type' */
  215. static const char * const reg_type_str[] = {
  216. [NOT_INIT] = "?",
  217. [UNKNOWN_VALUE] = "inv",
  218. [PTR_TO_CTX] = "ctx",
  219. [CONST_PTR_TO_MAP] = "map_ptr",
  220. [PTR_TO_MAP_VALUE] = "map_value",
  221. [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
  222. [FRAME_PTR] = "fp",
  223. [PTR_TO_STACK] = "fp",
  224. [CONST_IMM] = "imm",
  225. };
  226. static void print_verifier_state(struct verifier_env *env)
  227. {
  228. enum bpf_reg_type t;
  229. int i;
  230. for (i = 0; i < MAX_BPF_REG; i++) {
  231. t = env->cur_state.regs[i].type;
  232. if (t == NOT_INIT)
  233. continue;
  234. verbose(" R%d=%s", i, reg_type_str[t]);
  235. if (t == CONST_IMM || t == PTR_TO_STACK)
  236. verbose("%d", env->cur_state.regs[i].imm);
  237. else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
  238. t == PTR_TO_MAP_VALUE_OR_NULL)
  239. verbose("(ks=%d,vs=%d)",
  240. env->cur_state.regs[i].map_ptr->key_size,
  241. env->cur_state.regs[i].map_ptr->value_size);
  242. }
  243. for (i = 0; i < MAX_BPF_STACK; i++) {
  244. if (env->cur_state.stack[i].stype == STACK_SPILL)
  245. verbose(" fp%d=%s", -MAX_BPF_STACK + i,
  246. reg_type_str[env->cur_state.stack[i].reg_st.type]);
  247. }
  248. verbose("\n");
  249. }
  250. static const char *const bpf_class_string[] = {
  251. [BPF_LD] = "ld",
  252. [BPF_LDX] = "ldx",
  253. [BPF_ST] = "st",
  254. [BPF_STX] = "stx",
  255. [BPF_ALU] = "alu",
  256. [BPF_JMP] = "jmp",
  257. [BPF_RET] = "BUG",
  258. [BPF_ALU64] = "alu64",
  259. };
  260. static const char *const bpf_alu_string[] = {
  261. [BPF_ADD >> 4] = "+=",
  262. [BPF_SUB >> 4] = "-=",
  263. [BPF_MUL >> 4] = "*=",
  264. [BPF_DIV >> 4] = "/=",
  265. [BPF_OR >> 4] = "|=",
  266. [BPF_AND >> 4] = "&=",
  267. [BPF_LSH >> 4] = "<<=",
  268. [BPF_RSH >> 4] = ">>=",
  269. [BPF_NEG >> 4] = "neg",
  270. [BPF_MOD >> 4] = "%=",
  271. [BPF_XOR >> 4] = "^=",
  272. [BPF_MOV >> 4] = "=",
  273. [BPF_ARSH >> 4] = "s>>=",
  274. [BPF_END >> 4] = "endian",
  275. };
  276. static const char *const bpf_ldst_string[] = {
  277. [BPF_W >> 3] = "u32",
  278. [BPF_H >> 3] = "u16",
  279. [BPF_B >> 3] = "u8",
  280. [BPF_DW >> 3] = "u64",
  281. };
  282. static const char *const bpf_jmp_string[] = {
  283. [BPF_JA >> 4] = "jmp",
  284. [BPF_JEQ >> 4] = "==",
  285. [BPF_JGT >> 4] = ">",
  286. [BPF_JGE >> 4] = ">=",
  287. [BPF_JSET >> 4] = "&",
  288. [BPF_JNE >> 4] = "!=",
  289. [BPF_JSGT >> 4] = "s>",
  290. [BPF_JSGE >> 4] = "s>=",
  291. [BPF_CALL >> 4] = "call",
  292. [BPF_EXIT >> 4] = "exit",
  293. };
  294. static void print_bpf_insn(const struct bpf_verifier_env *env,
  295. const struct bpf_insn *insn)
  296. {
  297. u8 class = BPF_CLASS(insn->code);
  298. if (class == BPF_ALU || class == BPF_ALU64) {
  299. if (BPF_SRC(insn->code) == BPF_X)
  300. verbose("(%02x) %sr%d %s %sr%d\n",
  301. insn->code, class == BPF_ALU ? "(u32) " : "",
  302. insn->dst_reg,
  303. bpf_alu_string[BPF_OP(insn->code) >> 4],
  304. class == BPF_ALU ? "(u32) " : "",
  305. insn->src_reg);
  306. else
  307. verbose("(%02x) %sr%d %s %s%d\n",
  308. insn->code, class == BPF_ALU ? "(u32) " : "",
  309. insn->dst_reg,
  310. bpf_alu_string[BPF_OP(insn->code) >> 4],
  311. class == BPF_ALU ? "(u32) " : "",
  312. insn->imm);
  313. } else if (class == BPF_STX) {
  314. if (BPF_MODE(insn->code) == BPF_MEM)
  315. verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
  316. insn->code,
  317. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  318. insn->dst_reg,
  319. insn->off, insn->src_reg);
  320. else if (BPF_MODE(insn->code) == BPF_XADD)
  321. verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
  322. insn->code,
  323. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  324. insn->dst_reg, insn->off,
  325. insn->src_reg);
  326. else
  327. verbose("BUG_%02x\n", insn->code);
  328. } else if (class == BPF_ST) {
  329. if (BPF_MODE(insn->code) != BPF_MEM) {
  330. verbose("BUG_st_%02x\n", insn->code);
  331. return;
  332. }
  333. verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
  334. insn->code,
  335. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  336. insn->dst_reg,
  337. insn->off, insn->imm);
  338. } else if (class == BPF_LDX) {
  339. if (BPF_MODE(insn->code) != BPF_MEM) {
  340. verbose("BUG_ldx_%02x\n", insn->code);
  341. return;
  342. }
  343. verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
  344. insn->code, insn->dst_reg,
  345. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  346. insn->src_reg, insn->off);
  347. } else if (class == BPF_LD) {
  348. if (BPF_MODE(insn->code) == BPF_ABS) {
  349. verbose("(%02x) r0 = *(%s *)skb[%d]\n",
  350. insn->code,
  351. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  352. insn->imm);
  353. } else if (BPF_MODE(insn->code) == BPF_IND) {
  354. verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
  355. insn->code,
  356. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  357. insn->src_reg, insn->imm);
  358. } else if (BPF_MODE(insn->code) == BPF_IMM &&
  359. BPF_SIZE(insn->code) == BPF_DW) {
  360. /* At this point, we already made sure that the second
  361. * part of the ldimm64 insn is accessible.
  362. */
  363. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  364. bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
  365. if (map_ptr && !env->allow_ptr_leaks)
  366. imm = 0;
  367. verbose("(%02x) r%d = 0x%llx\n", insn->code,
  368. insn->dst_reg, (unsigned long long)imm);
  369. } else {
  370. verbose("BUG_ld_%02x\n", insn->code);
  371. return;
  372. }
  373. } else if (class == BPF_JMP) {
  374. u8 opcode = BPF_OP(insn->code);
  375. if (opcode == BPF_CALL) {
  376. verbose("(%02x) call %d\n", insn->code, insn->imm);
  377. } else if (insn->code == (BPF_JMP | BPF_JA)) {
  378. verbose("(%02x) goto pc%+d\n",
  379. insn->code, insn->off);
  380. } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
  381. verbose("(%02x) exit\n", insn->code);
  382. } else if (BPF_SRC(insn->code) == BPF_X) {
  383. verbose("(%02x) if r%d %s r%d goto pc%+d\n",
  384. insn->code, insn->dst_reg,
  385. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  386. insn->src_reg, insn->off);
  387. } else {
  388. verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
  389. insn->code, insn->dst_reg,
  390. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  391. insn->imm, insn->off);
  392. }
  393. } else {
  394. verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
  395. }
  396. }
  397. static int pop_stack(struct verifier_env *env, int *prev_insn_idx)
  398. {
  399. struct verifier_stack_elem *elem;
  400. int insn_idx;
  401. if (env->head == NULL)
  402. return -1;
  403. memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
  404. insn_idx = env->head->insn_idx;
  405. if (prev_insn_idx)
  406. *prev_insn_idx = env->head->prev_insn_idx;
  407. elem = env->head->next;
  408. kfree(env->head);
  409. env->head = elem;
  410. env->stack_size--;
  411. return insn_idx;
  412. }
  413. static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx,
  414. int prev_insn_idx)
  415. {
  416. struct verifier_stack_elem *elem;
  417. elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL);
  418. if (!elem)
  419. goto err;
  420. memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
  421. elem->insn_idx = insn_idx;
  422. elem->prev_insn_idx = prev_insn_idx;
  423. elem->next = env->head;
  424. env->head = elem;
  425. env->stack_size++;
  426. if (env->stack_size > 1024) {
  427. verbose("BPF program is too complex\n");
  428. goto err;
  429. }
  430. return &elem->st;
  431. err:
  432. /* pop all elements and return */
  433. while (pop_stack(env, NULL) >= 0);
  434. return NULL;
  435. }
  436. #define CALLER_SAVED_REGS 6
  437. static const int caller_saved[CALLER_SAVED_REGS] = {
  438. BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
  439. };
  440. static void init_reg_state(struct reg_state *regs)
  441. {
  442. int i;
  443. for (i = 0; i < MAX_BPF_REG; i++) {
  444. regs[i].type = NOT_INIT;
  445. regs[i].imm = 0;
  446. regs[i].map_ptr = NULL;
  447. }
  448. /* frame pointer */
  449. regs[BPF_REG_FP].type = FRAME_PTR;
  450. /* 1st arg to a function */
  451. regs[BPF_REG_1].type = PTR_TO_CTX;
  452. }
  453. static void mark_reg_unknown_value(struct reg_state *regs, u32 regno)
  454. {
  455. BUG_ON(regno >= MAX_BPF_REG);
  456. regs[regno].type = UNKNOWN_VALUE;
  457. regs[regno].imm = 0;
  458. regs[regno].map_ptr = NULL;
  459. }
  460. enum reg_arg_type {
  461. SRC_OP, /* register is used as source operand */
  462. DST_OP, /* register is used as destination operand */
  463. DST_OP_NO_MARK /* same as above, check only, don't mark */
  464. };
  465. static int check_reg_arg(struct reg_state *regs, u32 regno,
  466. enum reg_arg_type t)
  467. {
  468. if (regno >= MAX_BPF_REG) {
  469. verbose("R%d is invalid\n", regno);
  470. return -EINVAL;
  471. }
  472. if (t == SRC_OP) {
  473. /* check whether register used as source operand can be read */
  474. if (regs[regno].type == NOT_INIT) {
  475. verbose("R%d !read_ok\n", regno);
  476. return -EACCES;
  477. }
  478. } else {
  479. /* check whether register used as dest operand can be written to */
  480. if (regno == BPF_REG_FP) {
  481. verbose("frame pointer is read only\n");
  482. return -EACCES;
  483. }
  484. if (t == DST_OP)
  485. mark_reg_unknown_value(regs, regno);
  486. }
  487. return 0;
  488. }
  489. static int bpf_size_to_bytes(int bpf_size)
  490. {
  491. if (bpf_size == BPF_W)
  492. return 4;
  493. else if (bpf_size == BPF_H)
  494. return 2;
  495. else if (bpf_size == BPF_B)
  496. return 1;
  497. else if (bpf_size == BPF_DW)
  498. return 8;
  499. else
  500. return -EINVAL;
  501. }
  502. /* check_stack_read/write functions track spill/fill of registers,
  503. * stack boundary and alignment are checked in check_mem_access()
  504. */
  505. static int check_stack_write(struct verifier_state *state, int off, int size,
  506. int value_regno)
  507. {
  508. struct bpf_stack_slot *slot;
  509. int i;
  510. if (value_regno >= 0 &&
  511. (state->regs[value_regno].type == PTR_TO_MAP_VALUE ||
  512. state->regs[value_regno].type == PTR_TO_STACK ||
  513. state->regs[value_regno].type == PTR_TO_CTX)) {
  514. /* register containing pointer is being spilled into stack */
  515. if (size != 8) {
  516. verbose("invalid size of register spill\n");
  517. return -EACCES;
  518. }
  519. slot = &state->stack[MAX_BPF_STACK + off];
  520. slot->stype = STACK_SPILL;
  521. /* save register state */
  522. slot->reg_st = state->regs[value_regno];
  523. for (i = 1; i < 8; i++) {
  524. slot = &state->stack[MAX_BPF_STACK + off + i];
  525. slot->stype = STACK_SPILL_PART;
  526. slot->reg_st.type = UNKNOWN_VALUE;
  527. slot->reg_st.map_ptr = NULL;
  528. }
  529. } else {
  530. /* regular write of data into stack */
  531. for (i = 0; i < size; i++) {
  532. slot = &state->stack[MAX_BPF_STACK + off + i];
  533. slot->stype = STACK_MISC;
  534. slot->reg_st.type = UNKNOWN_VALUE;
  535. slot->reg_st.map_ptr = NULL;
  536. }
  537. }
  538. return 0;
  539. }
  540. static int check_stack_read(struct verifier_state *state, int off, int size,
  541. int value_regno)
  542. {
  543. int i;
  544. struct bpf_stack_slot *slot;
  545. slot = &state->stack[MAX_BPF_STACK + off];
  546. if (slot->stype == STACK_SPILL) {
  547. if (size != 8) {
  548. verbose("invalid size of register spill\n");
  549. return -EACCES;
  550. }
  551. for (i = 1; i < 8; i++) {
  552. if (state->stack[MAX_BPF_STACK + off + i].stype !=
  553. STACK_SPILL_PART) {
  554. verbose("corrupted spill memory\n");
  555. return -EACCES;
  556. }
  557. }
  558. if (value_regno >= 0)
  559. /* restore register state from stack */
  560. state->regs[value_regno] = slot->reg_st;
  561. return 0;
  562. } else {
  563. for (i = 0; i < size; i++) {
  564. if (state->stack[MAX_BPF_STACK + off + i].stype !=
  565. STACK_MISC) {
  566. verbose("invalid read from stack off %d+%d size %d\n",
  567. off, i, size);
  568. return -EACCES;
  569. }
  570. }
  571. if (value_regno >= 0)
  572. /* have read misc data from the stack */
  573. mark_reg_unknown_value(state->regs, value_regno);
  574. return 0;
  575. }
  576. }
  577. /* check read/write into map element returned by bpf_map_lookup_elem() */
  578. static int check_map_access(struct verifier_env *env, u32 regno, int off,
  579. int size)
  580. {
  581. struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
  582. if (off < 0 || off + size > map->value_size) {
  583. verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
  584. map->value_size, off, size);
  585. return -EACCES;
  586. }
  587. return 0;
  588. }
  589. /* check access to 'struct bpf_context' fields */
  590. static int check_ctx_access(struct verifier_env *env, int off, int size,
  591. enum bpf_access_type t)
  592. {
  593. if (env->prog->aux->ops->is_valid_access &&
  594. env->prog->aux->ops->is_valid_access(off, size, t))
  595. return 0;
  596. verbose("invalid bpf_context access off=%d size=%d\n", off, size);
  597. return -EACCES;
  598. }
  599. /* check whether memory at (regno + off) is accessible for t = (read | write)
  600. * if t==write, value_regno is a register which value is stored into memory
  601. * if t==read, value_regno is a register which will receive the value from memory
  602. * if t==write && value_regno==-1, some unknown value is stored into memory
  603. * if t==read && value_regno==-1, don't care what we read from memory
  604. */
  605. static int check_mem_access(struct verifier_env *env, u32 regno, int off,
  606. int bpf_size, enum bpf_access_type t,
  607. int value_regno)
  608. {
  609. struct verifier_state *state = &env->cur_state;
  610. int size, err = 0;
  611. size = bpf_size_to_bytes(bpf_size);
  612. if (size < 0)
  613. return size;
  614. if (off % size != 0) {
  615. verbose("misaligned access off %d size %d\n", off, size);
  616. return -EACCES;
  617. }
  618. if (state->regs[regno].type == PTR_TO_MAP_VALUE) {
  619. err = check_map_access(env, regno, off, size);
  620. if (!err && t == BPF_READ && value_regno >= 0)
  621. mark_reg_unknown_value(state->regs, value_regno);
  622. } else if (state->regs[regno].type == PTR_TO_CTX) {
  623. err = check_ctx_access(env, off, size, t);
  624. if (!err && t == BPF_READ && value_regno >= 0)
  625. mark_reg_unknown_value(state->regs, value_regno);
  626. } else if (state->regs[regno].type == FRAME_PTR) {
  627. if (off >= 0 || off < -MAX_BPF_STACK) {
  628. verbose("invalid stack off=%d size=%d\n", off, size);
  629. return -EACCES;
  630. }
  631. if (t == BPF_WRITE)
  632. err = check_stack_write(state, off, size, value_regno);
  633. else
  634. err = check_stack_read(state, off, size, value_regno);
  635. } else {
  636. verbose("R%d invalid mem access '%s'\n",
  637. regno, reg_type_str[state->regs[regno].type]);
  638. return -EACCES;
  639. }
  640. return err;
  641. }
  642. static int check_xadd(struct verifier_env *env, struct bpf_insn *insn)
  643. {
  644. struct reg_state *regs = env->cur_state.regs;
  645. int err;
  646. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  647. insn->imm != 0) {
  648. verbose("BPF_XADD uses reserved fields\n");
  649. return -EINVAL;
  650. }
  651. /* check src1 operand */
  652. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  653. if (err)
  654. return err;
  655. /* check src2 operand */
  656. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  657. if (err)
  658. return err;
  659. /* check whether atomic_add can read the memory */
  660. err = check_mem_access(env, insn->dst_reg, insn->off,
  661. BPF_SIZE(insn->code), BPF_READ, -1);
  662. if (err)
  663. return err;
  664. /* check whether atomic_add can write into the same memory */
  665. return check_mem_access(env, insn->dst_reg, insn->off,
  666. BPF_SIZE(insn->code), BPF_WRITE, -1);
  667. }
  668. /* when register 'regno' is passed into function that will read 'access_size'
  669. * bytes from that pointer, make sure that it's within stack boundary
  670. * and all elements of stack are initialized
  671. */
  672. static int check_stack_boundary(struct verifier_env *env,
  673. int regno, int access_size)
  674. {
  675. struct verifier_state *state = &env->cur_state;
  676. struct reg_state *regs = state->regs;
  677. int off, i;
  678. if (regs[regno].type != PTR_TO_STACK)
  679. return -EACCES;
  680. off = regs[regno].imm;
  681. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  682. access_size <= 0) {
  683. verbose("invalid stack type R%d off=%d access_size=%d\n",
  684. regno, off, access_size);
  685. return -EACCES;
  686. }
  687. for (i = 0; i < access_size; i++) {
  688. if (state->stack[MAX_BPF_STACK + off + i].stype != STACK_MISC) {
  689. verbose("invalid indirect read from stack off %d+%d size %d\n",
  690. off, i, access_size);
  691. return -EACCES;
  692. }
  693. }
  694. return 0;
  695. }
  696. static int check_func_arg(struct verifier_env *env, u32 regno,
  697. enum bpf_arg_type arg_type, struct bpf_map **mapp)
  698. {
  699. struct reg_state *reg = env->cur_state.regs + regno;
  700. enum bpf_reg_type expected_type;
  701. int err = 0;
  702. if (arg_type == ARG_DONTCARE)
  703. return 0;
  704. if (reg->type == NOT_INIT) {
  705. verbose("R%d !read_ok\n", regno);
  706. return -EACCES;
  707. }
  708. if (arg_type == ARG_ANYTHING)
  709. return 0;
  710. if (arg_type == ARG_PTR_TO_STACK || arg_type == ARG_PTR_TO_MAP_KEY ||
  711. arg_type == ARG_PTR_TO_MAP_VALUE) {
  712. expected_type = PTR_TO_STACK;
  713. } else if (arg_type == ARG_CONST_STACK_SIZE) {
  714. expected_type = CONST_IMM;
  715. } else if (arg_type == ARG_CONST_MAP_PTR) {
  716. expected_type = CONST_PTR_TO_MAP;
  717. } else {
  718. verbose("unsupported arg_type %d\n", arg_type);
  719. return -EFAULT;
  720. }
  721. if (reg->type != expected_type) {
  722. verbose("R%d type=%s expected=%s\n", regno,
  723. reg_type_str[reg->type], reg_type_str[expected_type]);
  724. return -EACCES;
  725. }
  726. if (arg_type == ARG_CONST_MAP_PTR) {
  727. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  728. *mapp = reg->map_ptr;
  729. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  730. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  731. * check that [key, key + map->key_size) are within
  732. * stack limits and initialized
  733. */
  734. if (!*mapp) {
  735. /* in function declaration map_ptr must come before
  736. * map_key, so that it's verified and known before
  737. * we have to check map_key here. Otherwise it means
  738. * that kernel subsystem misconfigured verifier
  739. */
  740. verbose("invalid map_ptr to access map->key\n");
  741. return -EACCES;
  742. }
  743. err = check_stack_boundary(env, regno, (*mapp)->key_size);
  744. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  745. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  746. * check [value, value + map->value_size) validity
  747. */
  748. if (!*mapp) {
  749. /* kernel subsystem misconfigured verifier */
  750. verbose("invalid map_ptr to access map->value\n");
  751. return -EACCES;
  752. }
  753. err = check_stack_boundary(env, regno, (*mapp)->value_size);
  754. } else if (arg_type == ARG_CONST_STACK_SIZE) {
  755. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  756. * from stack pointer 'buf'. Check it
  757. * note: regno == len, regno - 1 == buf
  758. */
  759. if (regno == 0) {
  760. /* kernel subsystem misconfigured verifier */
  761. verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
  762. return -EACCES;
  763. }
  764. err = check_stack_boundary(env, regno - 1, reg->imm);
  765. }
  766. return err;
  767. }
  768. static int check_call(struct verifier_env *env, int func_id)
  769. {
  770. struct verifier_state *state = &env->cur_state;
  771. const struct bpf_func_proto *fn = NULL;
  772. struct reg_state *regs = state->regs;
  773. struct bpf_map *map = NULL;
  774. struct reg_state *reg;
  775. int i, err;
  776. /* find function prototype */
  777. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  778. verbose("invalid func %d\n", func_id);
  779. return -EINVAL;
  780. }
  781. if (env->prog->aux->ops->get_func_proto)
  782. fn = env->prog->aux->ops->get_func_proto(func_id);
  783. if (!fn) {
  784. verbose("unknown func %d\n", func_id);
  785. return -EINVAL;
  786. }
  787. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  788. if (!env->prog->aux->is_gpl_compatible && fn->gpl_only) {
  789. verbose("cannot call GPL only function from proprietary program\n");
  790. return -EINVAL;
  791. }
  792. /* check args */
  793. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &map);
  794. if (err)
  795. return err;
  796. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &map);
  797. if (err)
  798. return err;
  799. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &map);
  800. if (err)
  801. return err;
  802. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &map);
  803. if (err)
  804. return err;
  805. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &map);
  806. if (err)
  807. return err;
  808. /* reset caller saved regs */
  809. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  810. reg = regs + caller_saved[i];
  811. reg->type = NOT_INIT;
  812. reg->imm = 0;
  813. }
  814. /* update return register */
  815. if (fn->ret_type == RET_INTEGER) {
  816. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  817. } else if (fn->ret_type == RET_VOID) {
  818. regs[BPF_REG_0].type = NOT_INIT;
  819. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  820. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  821. /* remember map_ptr, so that check_map_access()
  822. * can check 'value_size' boundary of memory access
  823. * to map element returned from bpf_map_lookup_elem()
  824. */
  825. if (map == NULL) {
  826. verbose("kernel subsystem misconfigured verifier\n");
  827. return -EINVAL;
  828. }
  829. regs[BPF_REG_0].map_ptr = map;
  830. } else {
  831. verbose("unknown return type %d of func %d\n",
  832. fn->ret_type, func_id);
  833. return -EINVAL;
  834. }
  835. return 0;
  836. }
  837. /* check validity of 32-bit and 64-bit arithmetic operations */
  838. static int check_alu_op(struct reg_state *regs, struct bpf_insn *insn)
  839. {
  840. u8 opcode = BPF_OP(insn->code);
  841. int err;
  842. if (opcode == BPF_END || opcode == BPF_NEG) {
  843. if (opcode == BPF_NEG) {
  844. if (BPF_SRC(insn->code) != 0 ||
  845. insn->src_reg != BPF_REG_0 ||
  846. insn->off != 0 || insn->imm != 0) {
  847. verbose("BPF_NEG uses reserved fields\n");
  848. return -EINVAL;
  849. }
  850. } else {
  851. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  852. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
  853. verbose("BPF_END uses reserved fields\n");
  854. return -EINVAL;
  855. }
  856. }
  857. /* check src operand */
  858. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  859. if (err)
  860. return err;
  861. /* check dest operand */
  862. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  863. if (err)
  864. return err;
  865. } else if (opcode == BPF_MOV) {
  866. if (BPF_SRC(insn->code) == BPF_X) {
  867. if (insn->imm != 0 || insn->off != 0) {
  868. verbose("BPF_MOV uses reserved fields\n");
  869. return -EINVAL;
  870. }
  871. /* check src operand */
  872. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  873. if (err)
  874. return err;
  875. } else {
  876. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  877. verbose("BPF_MOV uses reserved fields\n");
  878. return -EINVAL;
  879. }
  880. }
  881. /* check dest operand */
  882. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  883. if (err)
  884. return err;
  885. if (BPF_SRC(insn->code) == BPF_X) {
  886. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  887. /* case: R1 = R2
  888. * copy register state to dest reg
  889. */
  890. regs[insn->dst_reg] = regs[insn->src_reg];
  891. } else {
  892. regs[insn->dst_reg].type = UNKNOWN_VALUE;
  893. regs[insn->dst_reg].map_ptr = NULL;
  894. }
  895. } else {
  896. /* case: R = imm
  897. * remember the value we stored into this reg
  898. */
  899. regs[insn->dst_reg].type = CONST_IMM;
  900. regs[insn->dst_reg].imm = insn->imm;
  901. }
  902. } else if (opcode > BPF_END) {
  903. verbose("invalid BPF_ALU opcode %x\n", opcode);
  904. return -EINVAL;
  905. } else { /* all other ALU ops: and, sub, xor, add, ... */
  906. bool stack_relative = false;
  907. if (BPF_SRC(insn->code) == BPF_X) {
  908. if (insn->imm != 0 || insn->off != 0) {
  909. verbose("BPF_ALU uses reserved fields\n");
  910. return -EINVAL;
  911. }
  912. /* check src1 operand */
  913. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  914. if (err)
  915. return err;
  916. } else {
  917. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  918. verbose("BPF_ALU uses reserved fields\n");
  919. return -EINVAL;
  920. }
  921. }
  922. /* check src2 operand */
  923. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  924. if (err)
  925. return err;
  926. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  927. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  928. verbose("div by zero\n");
  929. return -EINVAL;
  930. }
  931. /* pattern match 'bpf_add Rx, imm' instruction */
  932. if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
  933. regs[insn->dst_reg].type == FRAME_PTR &&
  934. BPF_SRC(insn->code) == BPF_K)
  935. stack_relative = true;
  936. /* check dest operand */
  937. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  938. if (err)
  939. return err;
  940. if (stack_relative) {
  941. regs[insn->dst_reg].type = PTR_TO_STACK;
  942. regs[insn->dst_reg].imm = insn->imm;
  943. }
  944. }
  945. return 0;
  946. }
  947. static int check_cond_jmp_op(struct verifier_env *env,
  948. struct bpf_insn *insn, int *insn_idx)
  949. {
  950. struct reg_state *regs = env->cur_state.regs;
  951. struct verifier_state *other_branch;
  952. u8 opcode = BPF_OP(insn->code);
  953. int err;
  954. if (opcode > BPF_EXIT) {
  955. verbose("invalid BPF_JMP opcode %x\n", opcode);
  956. return -EINVAL;
  957. }
  958. if (BPF_SRC(insn->code) == BPF_X) {
  959. if (insn->imm != 0) {
  960. verbose("BPF_JMP uses reserved fields\n");
  961. return -EINVAL;
  962. }
  963. /* check src1 operand */
  964. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  965. if (err)
  966. return err;
  967. } else {
  968. if (insn->src_reg != BPF_REG_0) {
  969. verbose("BPF_JMP uses reserved fields\n");
  970. return -EINVAL;
  971. }
  972. }
  973. /* check src2 operand */
  974. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  975. if (err)
  976. return err;
  977. /* detect if R == 0 where R was initialized to zero earlier */
  978. if (BPF_SRC(insn->code) == BPF_K &&
  979. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  980. regs[insn->dst_reg].type == CONST_IMM &&
  981. regs[insn->dst_reg].imm == insn->imm) {
  982. if (opcode == BPF_JEQ) {
  983. /* if (imm == imm) goto pc+off;
  984. * only follow the goto, ignore fall-through
  985. */
  986. *insn_idx += insn->off;
  987. return 0;
  988. } else {
  989. /* if (imm != imm) goto pc+off;
  990. * only follow fall-through branch, since
  991. * that's where the program will go
  992. */
  993. return 0;
  994. }
  995. }
  996. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  997. if (!other_branch)
  998. return -EFAULT;
  999. /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */
  1000. if (BPF_SRC(insn->code) == BPF_K &&
  1001. insn->imm == 0 && (opcode == BPF_JEQ ||
  1002. opcode == BPF_JNE) &&
  1003. regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) {
  1004. if (opcode == BPF_JEQ) {
  1005. /* next fallthrough insn can access memory via
  1006. * this register
  1007. */
  1008. regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
  1009. /* branch targer cannot access it, since reg == 0 */
  1010. other_branch->regs[insn->dst_reg].type = CONST_IMM;
  1011. other_branch->regs[insn->dst_reg].imm = 0;
  1012. } else {
  1013. other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
  1014. regs[insn->dst_reg].type = CONST_IMM;
  1015. regs[insn->dst_reg].imm = 0;
  1016. }
  1017. } else if (BPF_SRC(insn->code) == BPF_K &&
  1018. (opcode == BPF_JEQ || opcode == BPF_JNE)) {
  1019. if (opcode == BPF_JEQ) {
  1020. /* detect if (R == imm) goto
  1021. * and in the target state recognize that R = imm
  1022. */
  1023. other_branch->regs[insn->dst_reg].type = CONST_IMM;
  1024. other_branch->regs[insn->dst_reg].imm = insn->imm;
  1025. } else {
  1026. /* detect if (R != imm) goto
  1027. * and in the fall-through state recognize that R = imm
  1028. */
  1029. regs[insn->dst_reg].type = CONST_IMM;
  1030. regs[insn->dst_reg].imm = insn->imm;
  1031. }
  1032. }
  1033. if (log_level)
  1034. print_verifier_state(env);
  1035. return 0;
  1036. }
  1037. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  1038. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  1039. {
  1040. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  1041. return (struct bpf_map *) (unsigned long) imm64;
  1042. }
  1043. /* verify BPF_LD_IMM64 instruction */
  1044. static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)
  1045. {
  1046. struct reg_state *regs = env->cur_state.regs;
  1047. int err;
  1048. if (BPF_SIZE(insn->code) != BPF_DW) {
  1049. verbose("invalid BPF_LD_IMM insn\n");
  1050. return -EINVAL;
  1051. }
  1052. if (insn->off != 0) {
  1053. verbose("BPF_LD_IMM64 uses reserved fields\n");
  1054. return -EINVAL;
  1055. }
  1056. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1057. if (err)
  1058. return err;
  1059. if (insn->src_reg == 0)
  1060. /* generic move 64-bit immediate into a register */
  1061. return 0;
  1062. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  1063. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  1064. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  1065. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  1066. return 0;
  1067. }
  1068. /* non-recursive DFS pseudo code
  1069. * 1 procedure DFS-iterative(G,v):
  1070. * 2 label v as discovered
  1071. * 3 let S be a stack
  1072. * 4 S.push(v)
  1073. * 5 while S is not empty
  1074. * 6 t <- S.pop()
  1075. * 7 if t is what we're looking for:
  1076. * 8 return t
  1077. * 9 for all edges e in G.adjacentEdges(t) do
  1078. * 10 if edge e is already labelled
  1079. * 11 continue with the next edge
  1080. * 12 w <- G.adjacentVertex(t,e)
  1081. * 13 if vertex w is not discovered and not explored
  1082. * 14 label e as tree-edge
  1083. * 15 label w as discovered
  1084. * 16 S.push(w)
  1085. * 17 continue at 5
  1086. * 18 else if vertex w is discovered
  1087. * 19 label e as back-edge
  1088. * 20 else
  1089. * 21 // vertex w is explored
  1090. * 22 label e as forward- or cross-edge
  1091. * 23 label t as explored
  1092. * 24 S.pop()
  1093. *
  1094. * convention:
  1095. * 0x10 - discovered
  1096. * 0x11 - discovered and fall-through edge labelled
  1097. * 0x12 - discovered and fall-through and branch edges labelled
  1098. * 0x20 - explored
  1099. */
  1100. enum {
  1101. DISCOVERED = 0x10,
  1102. EXPLORED = 0x20,
  1103. FALLTHROUGH = 1,
  1104. BRANCH = 2,
  1105. };
  1106. #define STATE_LIST_MARK ((struct verifier_state_list *) -1L)
  1107. static int *insn_stack; /* stack of insns to process */
  1108. static int cur_stack; /* current stack index */
  1109. static int *insn_state;
  1110. /* t, w, e - match pseudo-code above:
  1111. * t - index of current instruction
  1112. * w - next instruction
  1113. * e - edge
  1114. */
  1115. static int push_insn(int t, int w, int e, struct verifier_env *env)
  1116. {
  1117. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  1118. return 0;
  1119. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  1120. return 0;
  1121. if (w < 0 || w >= env->prog->len) {
  1122. verbose("jump out of range from insn %d to %d\n", t, w);
  1123. return -EINVAL;
  1124. }
  1125. if (e == BRANCH)
  1126. /* mark branch target for state pruning */
  1127. env->explored_states[w] = STATE_LIST_MARK;
  1128. if (insn_state[w] == 0) {
  1129. /* tree-edge */
  1130. insn_state[t] = DISCOVERED | e;
  1131. insn_state[w] = DISCOVERED;
  1132. if (cur_stack >= env->prog->len)
  1133. return -E2BIG;
  1134. insn_stack[cur_stack++] = w;
  1135. return 1;
  1136. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  1137. verbose("back-edge from insn %d to %d\n", t, w);
  1138. return -EINVAL;
  1139. } else if (insn_state[w] == EXPLORED) {
  1140. /* forward- or cross-edge */
  1141. insn_state[t] = DISCOVERED | e;
  1142. } else {
  1143. verbose("insn state internal bug\n");
  1144. return -EFAULT;
  1145. }
  1146. return 0;
  1147. }
  1148. /* non-recursive depth-first-search to detect loops in BPF program
  1149. * loop == back-edge in directed graph
  1150. */
  1151. static int check_cfg(struct verifier_env *env)
  1152. {
  1153. struct bpf_insn *insns = env->prog->insnsi;
  1154. int insn_cnt = env->prog->len;
  1155. int ret = 0;
  1156. int i, t;
  1157. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  1158. if (!insn_state)
  1159. return -ENOMEM;
  1160. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  1161. if (!insn_stack) {
  1162. kfree(insn_state);
  1163. return -ENOMEM;
  1164. }
  1165. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  1166. insn_stack[0] = 0; /* 0 is the first instruction */
  1167. cur_stack = 1;
  1168. peek_stack:
  1169. if (cur_stack == 0)
  1170. goto check_state;
  1171. t = insn_stack[cur_stack - 1];
  1172. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  1173. u8 opcode = BPF_OP(insns[t].code);
  1174. if (opcode == BPF_EXIT) {
  1175. goto mark_explored;
  1176. } else if (opcode == BPF_CALL) {
  1177. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1178. if (ret == 1)
  1179. goto peek_stack;
  1180. else if (ret < 0)
  1181. goto err_free;
  1182. } else if (opcode == BPF_JA) {
  1183. if (BPF_SRC(insns[t].code) != BPF_K) {
  1184. ret = -EINVAL;
  1185. goto err_free;
  1186. }
  1187. /* unconditional jump with single edge */
  1188. ret = push_insn(t, t + insns[t].off + 1,
  1189. FALLTHROUGH, env);
  1190. if (ret == 1)
  1191. goto peek_stack;
  1192. else if (ret < 0)
  1193. goto err_free;
  1194. /* tell verifier to check for equivalent states
  1195. * after every call and jump
  1196. */
  1197. if (t + 1 < insn_cnt)
  1198. env->explored_states[t + 1] = STATE_LIST_MARK;
  1199. } else {
  1200. /* conditional jump with two edges */
  1201. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1202. if (ret == 1)
  1203. goto peek_stack;
  1204. else if (ret < 0)
  1205. goto err_free;
  1206. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  1207. if (ret == 1)
  1208. goto peek_stack;
  1209. else if (ret < 0)
  1210. goto err_free;
  1211. }
  1212. } else {
  1213. /* all other non-branch instructions with single
  1214. * fall-through edge
  1215. */
  1216. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  1217. if (ret == 1)
  1218. goto peek_stack;
  1219. else if (ret < 0)
  1220. goto err_free;
  1221. }
  1222. mark_explored:
  1223. insn_state[t] = EXPLORED;
  1224. if (cur_stack-- <= 0) {
  1225. verbose("pop stack internal bug\n");
  1226. ret = -EFAULT;
  1227. goto err_free;
  1228. }
  1229. goto peek_stack;
  1230. check_state:
  1231. for (i = 0; i < insn_cnt; i++) {
  1232. if (insn_state[i] != EXPLORED) {
  1233. verbose("unreachable insn %d\n", i);
  1234. ret = -EINVAL;
  1235. goto err_free;
  1236. }
  1237. }
  1238. ret = 0; /* cfg looks good */
  1239. err_free:
  1240. kfree(insn_state);
  1241. kfree(insn_stack);
  1242. return ret;
  1243. }
  1244. /* compare two verifier states
  1245. *
  1246. * all states stored in state_list are known to be valid, since
  1247. * verifier reached 'bpf_exit' instruction through them
  1248. *
  1249. * this function is called when verifier exploring different branches of
  1250. * execution popped from the state stack. If it sees an old state that has
  1251. * more strict register state and more strict stack state then this execution
  1252. * branch doesn't need to be explored further, since verifier already
  1253. * concluded that more strict state leads to valid finish.
  1254. *
  1255. * Therefore two states are equivalent if register state is more conservative
  1256. * and explored stack state is more conservative than the current one.
  1257. * Example:
  1258. * explored current
  1259. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  1260. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  1261. *
  1262. * In other words if current stack state (one being explored) has more
  1263. * valid slots than old one that already passed validation, it means
  1264. * the verifier can stop exploring and conclude that current state is valid too
  1265. *
  1266. * Similarly with registers. If explored state has register type as invalid
  1267. * whereas register type in current state is meaningful, it means that
  1268. * the current state will reach 'bpf_exit' instruction safely
  1269. */
  1270. static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
  1271. {
  1272. int i;
  1273. for (i = 0; i < MAX_BPF_REG; i++) {
  1274. if (memcmp(&old->regs[i], &cur->regs[i],
  1275. sizeof(old->regs[0])) != 0) {
  1276. if (old->regs[i].type == NOT_INIT ||
  1277. (old->regs[i].type == UNKNOWN_VALUE &&
  1278. cur->regs[i].type != NOT_INIT))
  1279. continue;
  1280. return false;
  1281. }
  1282. }
  1283. for (i = 0; i < MAX_BPF_STACK; i++) {
  1284. if (memcmp(&old->stack[i], &cur->stack[i],
  1285. sizeof(old->stack[0])) != 0) {
  1286. if (old->stack[i].stype == STACK_INVALID)
  1287. continue;
  1288. return false;
  1289. }
  1290. }
  1291. return true;
  1292. }
  1293. static int is_state_visited(struct verifier_env *env, int insn_idx)
  1294. {
  1295. struct verifier_state_list *new_sl;
  1296. struct verifier_state_list *sl;
  1297. sl = env->explored_states[insn_idx];
  1298. if (!sl)
  1299. /* this 'insn_idx' instruction wasn't marked, so we will not
  1300. * be doing state search here
  1301. */
  1302. return 0;
  1303. while (sl != STATE_LIST_MARK) {
  1304. if (states_equal(&sl->state, &env->cur_state))
  1305. /* reached equivalent register/stack state,
  1306. * prune the search
  1307. */
  1308. return 1;
  1309. sl = sl->next;
  1310. }
  1311. /* there were no equivalent states, remember current one.
  1312. * technically the current state is not proven to be safe yet,
  1313. * but it will either reach bpf_exit (which means it's safe) or
  1314. * it will be rejected. Since there are no loops, we won't be
  1315. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  1316. */
  1317. new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER);
  1318. if (!new_sl)
  1319. return -ENOMEM;
  1320. /* add new state to the head of linked list */
  1321. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  1322. new_sl->next = env->explored_states[insn_idx];
  1323. env->explored_states[insn_idx] = new_sl;
  1324. return 0;
  1325. }
  1326. static int do_check(struct verifier_env *env)
  1327. {
  1328. struct verifier_state *state = &env->cur_state;
  1329. struct bpf_insn *insns = env->prog->insnsi;
  1330. struct reg_state *regs = state->regs;
  1331. int insn_cnt = env->prog->len;
  1332. int insn_idx, prev_insn_idx = 0;
  1333. int insn_processed = 0;
  1334. bool do_print_state = false;
  1335. init_reg_state(regs);
  1336. insn_idx = 0;
  1337. for (;;) {
  1338. struct bpf_insn *insn;
  1339. u8 class;
  1340. int err;
  1341. if (insn_idx >= insn_cnt) {
  1342. verbose("invalid insn idx %d insn_cnt %d\n",
  1343. insn_idx, insn_cnt);
  1344. return -EFAULT;
  1345. }
  1346. insn = &insns[insn_idx];
  1347. class = BPF_CLASS(insn->code);
  1348. if (++insn_processed > 32768) {
  1349. verbose("BPF program is too large. Proccessed %d insn\n",
  1350. insn_processed);
  1351. return -E2BIG;
  1352. }
  1353. err = is_state_visited(env, insn_idx);
  1354. if (err < 0)
  1355. return err;
  1356. if (err == 1) {
  1357. /* found equivalent state, can prune the search */
  1358. if (log_level) {
  1359. if (do_print_state)
  1360. verbose("\nfrom %d to %d: safe\n",
  1361. prev_insn_idx, insn_idx);
  1362. else
  1363. verbose("%d: safe\n", insn_idx);
  1364. }
  1365. goto process_bpf_exit;
  1366. }
  1367. if (log_level && do_print_state) {
  1368. verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
  1369. print_verifier_state(env);
  1370. do_print_state = false;
  1371. }
  1372. if (log_level) {
  1373. verbose("%d: ", insn_idx);
  1374. print_bpf_insn(env, insn);
  1375. }
  1376. if (class == BPF_ALU || class == BPF_ALU64) {
  1377. err = check_alu_op(regs, insn);
  1378. if (err)
  1379. return err;
  1380. } else if (class == BPF_LDX) {
  1381. if (BPF_MODE(insn->code) != BPF_MEM ||
  1382. insn->imm != 0) {
  1383. verbose("BPF_LDX uses reserved fields\n");
  1384. return -EINVAL;
  1385. }
  1386. /* check src operand */
  1387. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1388. if (err)
  1389. return err;
  1390. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  1391. if (err)
  1392. return err;
  1393. /* check that memory (src_reg + off) is readable,
  1394. * the state of dst_reg will be updated by this func
  1395. */
  1396. err = check_mem_access(env, insn->src_reg, insn->off,
  1397. BPF_SIZE(insn->code), BPF_READ,
  1398. insn->dst_reg);
  1399. if (err)
  1400. return err;
  1401. } else if (class == BPF_STX) {
  1402. if (BPF_MODE(insn->code) == BPF_XADD) {
  1403. err = check_xadd(env, insn);
  1404. if (err)
  1405. return err;
  1406. insn_idx++;
  1407. continue;
  1408. }
  1409. if (BPF_MODE(insn->code) != BPF_MEM ||
  1410. insn->imm != 0) {
  1411. verbose("BPF_STX uses reserved fields\n");
  1412. return -EINVAL;
  1413. }
  1414. /* check src1 operand */
  1415. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1416. if (err)
  1417. return err;
  1418. /* check src2 operand */
  1419. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1420. if (err)
  1421. return err;
  1422. /* check that memory (dst_reg + off) is writeable */
  1423. err = check_mem_access(env, insn->dst_reg, insn->off,
  1424. BPF_SIZE(insn->code), BPF_WRITE,
  1425. insn->src_reg);
  1426. if (err)
  1427. return err;
  1428. } else if (class == BPF_ST) {
  1429. if (BPF_MODE(insn->code) != BPF_MEM ||
  1430. insn->src_reg != BPF_REG_0) {
  1431. verbose("BPF_ST uses reserved fields\n");
  1432. return -EINVAL;
  1433. }
  1434. /* check src operand */
  1435. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1436. if (err)
  1437. return err;
  1438. /* check that memory (dst_reg + off) is writeable */
  1439. err = check_mem_access(env, insn->dst_reg, insn->off,
  1440. BPF_SIZE(insn->code), BPF_WRITE,
  1441. -1);
  1442. if (err)
  1443. return err;
  1444. } else if (class == BPF_JMP) {
  1445. u8 opcode = BPF_OP(insn->code);
  1446. if (opcode == BPF_CALL) {
  1447. if (BPF_SRC(insn->code) != BPF_K ||
  1448. insn->off != 0 ||
  1449. insn->src_reg != BPF_REG_0 ||
  1450. insn->dst_reg != BPF_REG_0) {
  1451. verbose("BPF_CALL uses reserved fields\n");
  1452. return -EINVAL;
  1453. }
  1454. err = check_call(env, insn->imm);
  1455. if (err)
  1456. return err;
  1457. } else if (opcode == BPF_JA) {
  1458. if (BPF_SRC(insn->code) != BPF_K ||
  1459. insn->imm != 0 ||
  1460. insn->src_reg != BPF_REG_0 ||
  1461. insn->dst_reg != BPF_REG_0) {
  1462. verbose("BPF_JA uses reserved fields\n");
  1463. return -EINVAL;
  1464. }
  1465. insn_idx += insn->off + 1;
  1466. continue;
  1467. } else if (opcode == BPF_EXIT) {
  1468. if (BPF_SRC(insn->code) != BPF_K ||
  1469. insn->imm != 0 ||
  1470. insn->src_reg != BPF_REG_0 ||
  1471. insn->dst_reg != BPF_REG_0) {
  1472. verbose("BPF_EXIT uses reserved fields\n");
  1473. return -EINVAL;
  1474. }
  1475. /* eBPF calling convetion is such that R0 is used
  1476. * to return the value from eBPF program.
  1477. * Make sure that it's readable at this time
  1478. * of bpf_exit, which means that program wrote
  1479. * something into it earlier
  1480. */
  1481. err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
  1482. if (err)
  1483. return err;
  1484. process_bpf_exit:
  1485. insn_idx = pop_stack(env, &prev_insn_idx);
  1486. if (insn_idx < 0) {
  1487. break;
  1488. } else {
  1489. do_print_state = true;
  1490. continue;
  1491. }
  1492. } else {
  1493. err = check_cond_jmp_op(env, insn, &insn_idx);
  1494. if (err)
  1495. return err;
  1496. }
  1497. } else if (class == BPF_LD) {
  1498. u8 mode = BPF_MODE(insn->code);
  1499. if (mode == BPF_ABS || mode == BPF_IND) {
  1500. verbose("LD_ABS is not supported yet\n");
  1501. return -EINVAL;
  1502. } else if (mode == BPF_IMM) {
  1503. err = check_ld_imm(env, insn);
  1504. if (err)
  1505. return err;
  1506. insn_idx++;
  1507. } else {
  1508. verbose("invalid BPF_LD mode\n");
  1509. return -EINVAL;
  1510. }
  1511. } else {
  1512. verbose("unknown insn class %d\n", class);
  1513. return -EINVAL;
  1514. }
  1515. insn_idx++;
  1516. }
  1517. return 0;
  1518. }
  1519. /* look for pseudo eBPF instructions that access map FDs and
  1520. * replace them with actual map pointers
  1521. */
  1522. static int replace_map_fd_with_map_ptr(struct verifier_env *env)
  1523. {
  1524. struct bpf_insn *insn = env->prog->insnsi;
  1525. int insn_cnt = env->prog->len;
  1526. int i, j;
  1527. for (i = 0; i < insn_cnt; i++, insn++) {
  1528. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  1529. struct bpf_map *map;
  1530. struct fd f;
  1531. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  1532. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  1533. insn[1].off != 0) {
  1534. verbose("invalid bpf_ld_imm64 insn\n");
  1535. return -EINVAL;
  1536. }
  1537. if (insn->src_reg == 0)
  1538. /* valid generic load 64-bit imm */
  1539. goto next_insn;
  1540. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  1541. verbose("unrecognized bpf_ld_imm64 insn\n");
  1542. return -EINVAL;
  1543. }
  1544. f = fdget(insn->imm);
  1545. map = bpf_map_get(f);
  1546. if (IS_ERR(map)) {
  1547. verbose("fd %d is not pointing to valid bpf_map\n",
  1548. insn->imm);
  1549. fdput(f);
  1550. return PTR_ERR(map);
  1551. }
  1552. /* store map pointer inside BPF_LD_IMM64 instruction */
  1553. insn[0].imm = (u32) (unsigned long) map;
  1554. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  1555. /* check whether we recorded this map already */
  1556. for (j = 0; j < env->used_map_cnt; j++)
  1557. if (env->used_maps[j] == map) {
  1558. fdput(f);
  1559. goto next_insn;
  1560. }
  1561. if (env->used_map_cnt >= MAX_USED_MAPS) {
  1562. fdput(f);
  1563. return -E2BIG;
  1564. }
  1565. /* remember this map */
  1566. env->used_maps[env->used_map_cnt++] = map;
  1567. /* hold the map. If the program is rejected by verifier,
  1568. * the map will be released by release_maps() or it
  1569. * will be used by the valid program until it's unloaded
  1570. * and all maps are released in free_bpf_prog_info()
  1571. */
  1572. atomic_inc(&map->refcnt);
  1573. fdput(f);
  1574. next_insn:
  1575. insn++;
  1576. i++;
  1577. }
  1578. }
  1579. /* now all pseudo BPF_LD_IMM64 instructions load valid
  1580. * 'struct bpf_map *' into a register instead of user map_fd.
  1581. * These pointers will be used later by verifier to validate map access.
  1582. */
  1583. return 0;
  1584. }
  1585. /* drop refcnt of maps used by the rejected program */
  1586. static void release_maps(struct verifier_env *env)
  1587. {
  1588. int i;
  1589. for (i = 0; i < env->used_map_cnt; i++)
  1590. bpf_map_put(env->used_maps[i]);
  1591. }
  1592. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  1593. static void convert_pseudo_ld_imm64(struct verifier_env *env)
  1594. {
  1595. struct bpf_insn *insn = env->prog->insnsi;
  1596. int insn_cnt = env->prog->len;
  1597. int i;
  1598. for (i = 0; i < insn_cnt; i++, insn++)
  1599. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  1600. insn->src_reg = 0;
  1601. }
  1602. static void free_states(struct verifier_env *env)
  1603. {
  1604. struct verifier_state_list *sl, *sln;
  1605. int i;
  1606. if (!env->explored_states)
  1607. return;
  1608. for (i = 0; i < env->prog->len; i++) {
  1609. sl = env->explored_states[i];
  1610. if (sl)
  1611. while (sl != STATE_LIST_MARK) {
  1612. sln = sl->next;
  1613. kfree(sl);
  1614. sl = sln;
  1615. }
  1616. }
  1617. kfree(env->explored_states);
  1618. }
  1619. int bpf_check(struct bpf_prog *prog, union bpf_attr *attr)
  1620. {
  1621. char __user *log_ubuf = NULL;
  1622. struct verifier_env *env;
  1623. int ret = -EINVAL;
  1624. if (prog->len <= 0 || prog->len > BPF_MAXINSNS)
  1625. return -E2BIG;
  1626. /* 'struct verifier_env' can be global, but since it's not small,
  1627. * allocate/free it every time bpf_check() is called
  1628. */
  1629. env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL);
  1630. if (!env)
  1631. return -ENOMEM;
  1632. env->prog = prog;
  1633. /* grab the mutex to protect few globals used by verifier */
  1634. mutex_lock(&bpf_verifier_lock);
  1635. if (attr->log_level || attr->log_buf || attr->log_size) {
  1636. /* user requested verbose verifier output
  1637. * and supplied buffer to store the verification trace
  1638. */
  1639. log_level = attr->log_level;
  1640. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  1641. log_size = attr->log_size;
  1642. log_len = 0;
  1643. ret = -EINVAL;
  1644. /* log_* values have to be sane */
  1645. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  1646. log_level == 0 || log_ubuf == NULL)
  1647. goto free_env;
  1648. ret = -ENOMEM;
  1649. log_buf = vmalloc(log_size);
  1650. if (!log_buf)
  1651. goto free_env;
  1652. } else {
  1653. log_level = 0;
  1654. }
  1655. ret = replace_map_fd_with_map_ptr(env);
  1656. if (ret < 0)
  1657. goto skip_full_check;
  1658. env->explored_states = kcalloc(prog->len,
  1659. sizeof(struct verifier_state_list *),
  1660. GFP_USER);
  1661. ret = -ENOMEM;
  1662. if (!env->explored_states)
  1663. goto skip_full_check;
  1664. ret = check_cfg(env);
  1665. if (ret < 0)
  1666. goto skip_full_check;
  1667. ret = do_check(env);
  1668. skip_full_check:
  1669. while (pop_stack(env, NULL) >= 0);
  1670. free_states(env);
  1671. if (log_level && log_len >= log_size - 1) {
  1672. BUG_ON(log_len >= log_size);
  1673. /* verifier log exceeded user supplied buffer */
  1674. ret = -ENOSPC;
  1675. /* fall through to return what was recorded */
  1676. }
  1677. /* copy verifier log back to user space including trailing zero */
  1678. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  1679. ret = -EFAULT;
  1680. goto free_log_buf;
  1681. }
  1682. if (ret == 0 && env->used_map_cnt) {
  1683. /* if program passed verifier, update used_maps in bpf_prog_info */
  1684. prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  1685. sizeof(env->used_maps[0]),
  1686. GFP_KERNEL);
  1687. if (!prog->aux->used_maps) {
  1688. ret = -ENOMEM;
  1689. goto free_log_buf;
  1690. }
  1691. memcpy(prog->aux->used_maps, env->used_maps,
  1692. sizeof(env->used_maps[0]) * env->used_map_cnt);
  1693. prog->aux->used_map_cnt = env->used_map_cnt;
  1694. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  1695. * bpf_ld_imm64 instructions
  1696. */
  1697. convert_pseudo_ld_imm64(env);
  1698. }
  1699. free_log_buf:
  1700. if (log_level)
  1701. vfree(log_buf);
  1702. free_env:
  1703. if (!prog->aux->used_maps)
  1704. /* if we didn't copy map pointers into bpf_prog_info, release
  1705. * them now. Otherwise free_bpf_prog_info() will release them.
  1706. */
  1707. release_maps(env);
  1708. kfree(env);
  1709. mutex_unlock(&bpf_verifier_lock);
  1710. return ret;
  1711. }