verifier.c 53 KB

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