btf.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Facebook */
  3. #include <uapi/linux/btf.h>
  4. #include <uapi/linux/types.h>
  5. #include <linux/seq_file.h>
  6. #include <linux/compiler.h>
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include <linux/anon_inodes.h>
  10. #include <linux/file.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/kernel.h>
  13. #include <linux/idr.h>
  14. #include <linux/bpf_verifier.h>
  15. #include <linux/btf.h>
  16. /* BTF (BPF Type Format) is the meta data format which describes
  17. * the data types of BPF program/map. Hence, it basically focus
  18. * on the C programming language which the modern BPF is primary
  19. * using.
  20. *
  21. * ELF Section:
  22. * ~~~~~~~~~~~
  23. * The BTF data is stored under the ".BTF" ELF section
  24. *
  25. * struct btf_type:
  26. * ~~~~~~~~~~~~~~~
  27. * Each 'struct btf_type' object describes a C data type.
  28. * Depending on the type it is describing, a 'struct btf_type'
  29. * object may be followed by more data. F.e.
  30. * To describe an array, 'struct btf_type' is followed by
  31. * 'struct btf_array'.
  32. *
  33. * 'struct btf_type' and any extra data following it are
  34. * 4 bytes aligned.
  35. *
  36. * Type section:
  37. * ~~~~~~~~~~~~~
  38. * The BTF type section contains a list of 'struct btf_type' objects.
  39. * Each one describes a C type. Recall from the above section
  40. * that a 'struct btf_type' object could be immediately followed by extra
  41. * data in order to desribe some particular C types.
  42. *
  43. * type_id:
  44. * ~~~~~~~
  45. * Each btf_type object is identified by a type_id. The type_id
  46. * is implicitly implied by the location of the btf_type object in
  47. * the BTF type section. The first one has type_id 1. The second
  48. * one has type_id 2...etc. Hence, an earlier btf_type has
  49. * a smaller type_id.
  50. *
  51. * A btf_type object may refer to another btf_type object by using
  52. * type_id (i.e. the "type" in the "struct btf_type").
  53. *
  54. * NOTE that we cannot assume any reference-order.
  55. * A btf_type object can refer to an earlier btf_type object
  56. * but it can also refer to a later btf_type object.
  57. *
  58. * For example, to describe "const void *". A btf_type
  59. * object describing "const" may refer to another btf_type
  60. * object describing "void *". This type-reference is done
  61. * by specifying type_id:
  62. *
  63. * [1] CONST (anon) type_id=2
  64. * [2] PTR (anon) type_id=0
  65. *
  66. * The above is the btf_verifier debug log:
  67. * - Each line started with "[?]" is a btf_type object
  68. * - [?] is the type_id of the btf_type object.
  69. * - CONST/PTR is the BTF_KIND_XXX
  70. * - "(anon)" is the name of the type. It just
  71. * happens that CONST and PTR has no name.
  72. * - type_id=XXX is the 'u32 type' in btf_type
  73. *
  74. * NOTE: "void" has type_id 0
  75. *
  76. * String section:
  77. * ~~~~~~~~~~~~~~
  78. * The BTF string section contains the names used by the type section.
  79. * Each string is referred by an "offset" from the beginning of the
  80. * string section.
  81. *
  82. * Each string is '\0' terminated.
  83. *
  84. * The first character in the string section must be '\0'
  85. * which is used to mean 'anonymous'. Some btf_type may not
  86. * have a name.
  87. */
  88. /* BTF verification:
  89. *
  90. * To verify BTF data, two passes are needed.
  91. *
  92. * Pass #1
  93. * ~~~~~~~
  94. * The first pass is to collect all btf_type objects to
  95. * an array: "btf->types".
  96. *
  97. * Depending on the C type that a btf_type is describing,
  98. * a btf_type may be followed by extra data. We don't know
  99. * how many btf_type is there, and more importantly we don't
  100. * know where each btf_type is located in the type section.
  101. *
  102. * Without knowing the location of each type_id, most verifications
  103. * cannot be done. e.g. an earlier btf_type may refer to a later
  104. * btf_type (recall the "const void *" above), so we cannot
  105. * check this type-reference in the first pass.
  106. *
  107. * In the first pass, it still does some verifications (e.g.
  108. * checking the name is a valid offset to the string section).
  109. *
  110. * Pass #2
  111. * ~~~~~~~
  112. * The main focus is to resolve a btf_type that is referring
  113. * to another type.
  114. *
  115. * We have to ensure the referring type:
  116. * 1) does exist in the BTF (i.e. in btf->types[])
  117. * 2) does not cause a loop:
  118. * struct A {
  119. * struct B b;
  120. * };
  121. *
  122. * struct B {
  123. * struct A a;
  124. * };
  125. *
  126. * btf_type_needs_resolve() decides if a btf_type needs
  127. * to be resolved.
  128. *
  129. * The needs_resolve type implements the "resolve()" ops which
  130. * essentially does a DFS and detects backedge.
  131. *
  132. * During resolve (or DFS), different C types have different
  133. * "RESOLVED" conditions.
  134. *
  135. * When resolving a BTF_KIND_STRUCT, we need to resolve all its
  136. * members because a member is always referring to another
  137. * type. A struct's member can be treated as "RESOLVED" if
  138. * it is referring to a BTF_KIND_PTR. Otherwise, the
  139. * following valid C struct would be rejected:
  140. *
  141. * struct A {
  142. * int m;
  143. * struct A *a;
  144. * };
  145. *
  146. * When resolving a BTF_KIND_PTR, it needs to keep resolving if
  147. * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
  148. * detect a pointer loop, e.g.:
  149. * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
  150. * ^ |
  151. * +-----------------------------------------+
  152. *
  153. */
  154. #define BITS_PER_U64 (sizeof(u64) * BITS_PER_BYTE)
  155. #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
  156. #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
  157. #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
  158. #define BITS_ROUNDUP_BYTES(bits) \
  159. (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
  160. /* 16MB for 64k structs and each has 16 members and
  161. * a few MB spaces for the string section.
  162. * The hard limit is S32_MAX.
  163. */
  164. #define BTF_MAX_SIZE (16 * 1024 * 1024)
  165. /* 64k. We can raise it later. The hard limit is S32_MAX. */
  166. #define BTF_MAX_NR_TYPES 65535
  167. #define for_each_member(i, struct_type, member) \
  168. for (i = 0, member = btf_type_member(struct_type); \
  169. i < btf_type_vlen(struct_type); \
  170. i++, member++)
  171. #define for_each_member_from(i, from, struct_type, member) \
  172. for (i = from, member = btf_type_member(struct_type) + from; \
  173. i < btf_type_vlen(struct_type); \
  174. i++, member++)
  175. static DEFINE_IDR(btf_idr);
  176. static DEFINE_SPINLOCK(btf_idr_lock);
  177. struct btf {
  178. union {
  179. struct btf_header *hdr;
  180. void *data;
  181. };
  182. struct btf_type **types;
  183. u32 *resolved_ids;
  184. u32 *resolved_sizes;
  185. const char *strings;
  186. void *nohdr_data;
  187. u32 nr_types;
  188. u32 types_size;
  189. u32 data_size;
  190. refcount_t refcnt;
  191. u32 id;
  192. struct rcu_head rcu;
  193. };
  194. enum verifier_phase {
  195. CHECK_META,
  196. CHECK_TYPE,
  197. };
  198. struct resolve_vertex {
  199. const struct btf_type *t;
  200. u32 type_id;
  201. u16 next_member;
  202. };
  203. enum visit_state {
  204. NOT_VISITED,
  205. VISITED,
  206. RESOLVED,
  207. };
  208. enum resolve_mode {
  209. RESOLVE_TBD, /* To Be Determined */
  210. RESOLVE_PTR, /* Resolving for Pointer */
  211. RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
  212. * or array
  213. */
  214. };
  215. #define MAX_RESOLVE_DEPTH 32
  216. struct btf_verifier_env {
  217. struct btf *btf;
  218. u8 *visit_states;
  219. struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
  220. struct bpf_verifier_log log;
  221. u32 log_type_id;
  222. u32 top_stack;
  223. enum verifier_phase phase;
  224. enum resolve_mode resolve_mode;
  225. };
  226. static const char * const btf_kind_str[NR_BTF_KINDS] = {
  227. [BTF_KIND_UNKN] = "UNKNOWN",
  228. [BTF_KIND_INT] = "INT",
  229. [BTF_KIND_PTR] = "PTR",
  230. [BTF_KIND_ARRAY] = "ARRAY",
  231. [BTF_KIND_STRUCT] = "STRUCT",
  232. [BTF_KIND_UNION] = "UNION",
  233. [BTF_KIND_ENUM] = "ENUM",
  234. [BTF_KIND_FWD] = "FWD",
  235. [BTF_KIND_TYPEDEF] = "TYPEDEF",
  236. [BTF_KIND_VOLATILE] = "VOLATILE",
  237. [BTF_KIND_CONST] = "CONST",
  238. [BTF_KIND_RESTRICT] = "RESTRICT",
  239. };
  240. struct btf_kind_operations {
  241. s32 (*check_meta)(struct btf_verifier_env *env,
  242. const struct btf_type *t,
  243. u32 meta_left);
  244. int (*resolve)(struct btf_verifier_env *env,
  245. const struct resolve_vertex *v);
  246. int (*check_member)(struct btf_verifier_env *env,
  247. const struct btf_type *struct_type,
  248. const struct btf_member *member,
  249. const struct btf_type *member_type);
  250. void (*log_details)(struct btf_verifier_env *env,
  251. const struct btf_type *t);
  252. void (*seq_show)(const struct btf *btf, const struct btf_type *t,
  253. u32 type_id, void *data, u8 bits_offsets,
  254. struct seq_file *m);
  255. };
  256. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
  257. static struct btf_type btf_void;
  258. static bool btf_type_is_modifier(const struct btf_type *t)
  259. {
  260. /* Some of them is not strictly a C modifier
  261. * but they are grouped into the same bucket
  262. * for BTF concern:
  263. * A type (t) that refers to another
  264. * type through t->type AND its size cannot
  265. * be determined without following the t->type.
  266. *
  267. * ptr does not fall into this bucket
  268. * because its size is always sizeof(void *).
  269. */
  270. switch (BTF_INFO_KIND(t->info)) {
  271. case BTF_KIND_TYPEDEF:
  272. case BTF_KIND_VOLATILE:
  273. case BTF_KIND_CONST:
  274. case BTF_KIND_RESTRICT:
  275. return true;
  276. }
  277. return false;
  278. }
  279. static bool btf_type_is_void(const struct btf_type *t)
  280. {
  281. /* void => no type and size info.
  282. * Hence, FWD is also treated as void.
  283. */
  284. return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
  285. }
  286. static bool btf_type_is_void_or_null(const struct btf_type *t)
  287. {
  288. return !t || btf_type_is_void(t);
  289. }
  290. /* union is only a special case of struct:
  291. * all its offsetof(member) == 0
  292. */
  293. static bool btf_type_is_struct(const struct btf_type *t)
  294. {
  295. u8 kind = BTF_INFO_KIND(t->info);
  296. return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  297. }
  298. static bool btf_type_is_array(const struct btf_type *t)
  299. {
  300. return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
  301. }
  302. static bool btf_type_is_ptr(const struct btf_type *t)
  303. {
  304. return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
  305. }
  306. static bool btf_type_is_int(const struct btf_type *t)
  307. {
  308. return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
  309. }
  310. /* What types need to be resolved?
  311. *
  312. * btf_type_is_modifier() is an obvious one.
  313. *
  314. * btf_type_is_struct() because its member refers to
  315. * another type (through member->type).
  316. * btf_type_is_array() because its element (array->type)
  317. * refers to another type. Array can be thought of a
  318. * special case of struct while array just has the same
  319. * member-type repeated by array->nelems of times.
  320. */
  321. static bool btf_type_needs_resolve(const struct btf_type *t)
  322. {
  323. return btf_type_is_modifier(t) ||
  324. btf_type_is_ptr(t) ||
  325. btf_type_is_struct(t) ||
  326. btf_type_is_array(t);
  327. }
  328. /* t->size can be used */
  329. static bool btf_type_has_size(const struct btf_type *t)
  330. {
  331. switch (BTF_INFO_KIND(t->info)) {
  332. case BTF_KIND_INT:
  333. case BTF_KIND_STRUCT:
  334. case BTF_KIND_UNION:
  335. case BTF_KIND_ENUM:
  336. return true;
  337. }
  338. return false;
  339. }
  340. static const char *btf_int_encoding_str(u8 encoding)
  341. {
  342. if (encoding == 0)
  343. return "(none)";
  344. else if (encoding == BTF_INT_SIGNED)
  345. return "SIGNED";
  346. else if (encoding == BTF_INT_CHAR)
  347. return "CHAR";
  348. else if (encoding == BTF_INT_BOOL)
  349. return "BOOL";
  350. else if (encoding == BTF_INT_VARARGS)
  351. return "VARARGS";
  352. else
  353. return "UNKN";
  354. }
  355. static u16 btf_type_vlen(const struct btf_type *t)
  356. {
  357. return BTF_INFO_VLEN(t->info);
  358. }
  359. static u32 btf_type_int(const struct btf_type *t)
  360. {
  361. return *(u32 *)(t + 1);
  362. }
  363. static const struct btf_array *btf_type_array(const struct btf_type *t)
  364. {
  365. return (const struct btf_array *)(t + 1);
  366. }
  367. static const struct btf_member *btf_type_member(const struct btf_type *t)
  368. {
  369. return (const struct btf_member *)(t + 1);
  370. }
  371. static const struct btf_enum *btf_type_enum(const struct btf_type *t)
  372. {
  373. return (const struct btf_enum *)(t + 1);
  374. }
  375. static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
  376. {
  377. return kind_ops[BTF_INFO_KIND(t->info)];
  378. }
  379. static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
  380. {
  381. return !BTF_STR_TBL_ELF_ID(offset) &&
  382. BTF_STR_OFFSET(offset) < btf->hdr->str_len;
  383. }
  384. static const char *btf_name_by_offset(const struct btf *btf, u32 offset)
  385. {
  386. if (!BTF_STR_OFFSET(offset))
  387. return "(anon)";
  388. else if (BTF_STR_OFFSET(offset) < btf->hdr->str_len)
  389. return &btf->strings[BTF_STR_OFFSET(offset)];
  390. else
  391. return "(invalid-name-offset)";
  392. }
  393. static const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
  394. {
  395. if (type_id > btf->nr_types)
  396. return NULL;
  397. return btf->types[type_id];
  398. }
  399. __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
  400. const char *fmt, ...)
  401. {
  402. va_list args;
  403. va_start(args, fmt);
  404. bpf_verifier_vlog(log, fmt, args);
  405. va_end(args);
  406. }
  407. __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
  408. const char *fmt, ...)
  409. {
  410. struct bpf_verifier_log *log = &env->log;
  411. va_list args;
  412. if (!bpf_verifier_log_needed(log))
  413. return;
  414. va_start(args, fmt);
  415. bpf_verifier_vlog(log, fmt, args);
  416. va_end(args);
  417. }
  418. __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
  419. const struct btf_type *t,
  420. bool log_details,
  421. const char *fmt, ...)
  422. {
  423. struct bpf_verifier_log *log = &env->log;
  424. u8 kind = BTF_INFO_KIND(t->info);
  425. struct btf *btf = env->btf;
  426. va_list args;
  427. if (!bpf_verifier_log_needed(log))
  428. return;
  429. __btf_verifier_log(log, "[%u] %s %s%s",
  430. env->log_type_id,
  431. btf_kind_str[kind],
  432. btf_name_by_offset(btf, t->name_off),
  433. log_details ? " " : "");
  434. if (log_details)
  435. btf_type_ops(t)->log_details(env, t);
  436. if (fmt && *fmt) {
  437. __btf_verifier_log(log, " ");
  438. va_start(args, fmt);
  439. bpf_verifier_vlog(log, fmt, args);
  440. va_end(args);
  441. }
  442. __btf_verifier_log(log, "\n");
  443. }
  444. #define btf_verifier_log_type(env, t, ...) \
  445. __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
  446. #define btf_verifier_log_basic(env, t, ...) \
  447. __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
  448. __printf(4, 5)
  449. static void btf_verifier_log_member(struct btf_verifier_env *env,
  450. const struct btf_type *struct_type,
  451. const struct btf_member *member,
  452. const char *fmt, ...)
  453. {
  454. struct bpf_verifier_log *log = &env->log;
  455. struct btf *btf = env->btf;
  456. va_list args;
  457. if (!bpf_verifier_log_needed(log))
  458. return;
  459. /* The CHECK_META phase already did a btf dump.
  460. *
  461. * If member is logged again, it must hit an error in
  462. * parsing this member. It is useful to print out which
  463. * struct this member belongs to.
  464. */
  465. if (env->phase != CHECK_META)
  466. btf_verifier_log_type(env, struct_type, NULL);
  467. __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
  468. btf_name_by_offset(btf, member->name_off),
  469. member->type, member->offset);
  470. if (fmt && *fmt) {
  471. __btf_verifier_log(log, " ");
  472. va_start(args, fmt);
  473. bpf_verifier_vlog(log, fmt, args);
  474. va_end(args);
  475. }
  476. __btf_verifier_log(log, "\n");
  477. }
  478. static void btf_verifier_log_hdr(struct btf_verifier_env *env)
  479. {
  480. struct bpf_verifier_log *log = &env->log;
  481. const struct btf *btf = env->btf;
  482. const struct btf_header *hdr;
  483. if (!bpf_verifier_log_needed(log))
  484. return;
  485. hdr = btf->hdr;
  486. __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
  487. __btf_verifier_log(log, "version: %u\n", hdr->version);
  488. __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
  489. __btf_verifier_log(log, "parent_label: %u\n", hdr->parent_label);
  490. __btf_verifier_log(log, "parent_name: %u\n", hdr->parent_name);
  491. __btf_verifier_log(log, "label_off: %u\n", hdr->label_off);
  492. __btf_verifier_log(log, "object_off: %u\n", hdr->object_off);
  493. __btf_verifier_log(log, "func_off: %u\n", hdr->func_off);
  494. __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
  495. __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
  496. __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
  497. __btf_verifier_log(log, "btf_total_size: %u\n", btf->data_size);
  498. }
  499. static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
  500. {
  501. struct btf *btf = env->btf;
  502. /* < 2 because +1 for btf_void which is always in btf->types[0].
  503. * btf_void is not accounted in btf->nr_types because btf_void
  504. * does not come from the BTF file.
  505. */
  506. if (btf->types_size - btf->nr_types < 2) {
  507. /* Expand 'types' array */
  508. struct btf_type **new_types;
  509. u32 expand_by, new_size;
  510. if (btf->types_size == BTF_MAX_NR_TYPES) {
  511. btf_verifier_log(env, "Exceeded max num of types");
  512. return -E2BIG;
  513. }
  514. expand_by = max_t(u32, btf->types_size >> 2, 16);
  515. new_size = min_t(u32, BTF_MAX_NR_TYPES,
  516. btf->types_size + expand_by);
  517. new_types = kvzalloc(new_size * sizeof(*new_types),
  518. GFP_KERNEL | __GFP_NOWARN);
  519. if (!new_types)
  520. return -ENOMEM;
  521. if (btf->nr_types == 0)
  522. new_types[0] = &btf_void;
  523. else
  524. memcpy(new_types, btf->types,
  525. sizeof(*btf->types) * (btf->nr_types + 1));
  526. kvfree(btf->types);
  527. btf->types = new_types;
  528. btf->types_size = new_size;
  529. }
  530. btf->types[++(btf->nr_types)] = t;
  531. return 0;
  532. }
  533. static int btf_alloc_id(struct btf *btf)
  534. {
  535. int id;
  536. idr_preload(GFP_KERNEL);
  537. spin_lock_bh(&btf_idr_lock);
  538. id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
  539. if (id > 0)
  540. btf->id = id;
  541. spin_unlock_bh(&btf_idr_lock);
  542. idr_preload_end();
  543. if (WARN_ON_ONCE(!id))
  544. return -ENOSPC;
  545. return id > 0 ? 0 : id;
  546. }
  547. static void btf_free_id(struct btf *btf)
  548. {
  549. unsigned long flags;
  550. /*
  551. * In map-in-map, calling map_delete_elem() on outer
  552. * map will call bpf_map_put on the inner map.
  553. * It will then eventually call btf_free_id()
  554. * on the inner map. Some of the map_delete_elem()
  555. * implementation may have irq disabled, so
  556. * we need to use the _irqsave() version instead
  557. * of the _bh() version.
  558. */
  559. spin_lock_irqsave(&btf_idr_lock, flags);
  560. idr_remove(&btf_idr, btf->id);
  561. spin_unlock_irqrestore(&btf_idr_lock, flags);
  562. }
  563. static void btf_free(struct btf *btf)
  564. {
  565. kvfree(btf->types);
  566. kvfree(btf->resolved_sizes);
  567. kvfree(btf->resolved_ids);
  568. kvfree(btf->data);
  569. kfree(btf);
  570. }
  571. static void btf_free_rcu(struct rcu_head *rcu)
  572. {
  573. struct btf *btf = container_of(rcu, struct btf, rcu);
  574. btf_free(btf);
  575. }
  576. void btf_put(struct btf *btf)
  577. {
  578. if (btf && refcount_dec_and_test(&btf->refcnt)) {
  579. btf_free_id(btf);
  580. call_rcu(&btf->rcu, btf_free_rcu);
  581. }
  582. }
  583. static int env_resolve_init(struct btf_verifier_env *env)
  584. {
  585. struct btf *btf = env->btf;
  586. u32 nr_types = btf->nr_types;
  587. u32 *resolved_sizes = NULL;
  588. u32 *resolved_ids = NULL;
  589. u8 *visit_states = NULL;
  590. /* +1 for btf_void */
  591. resolved_sizes = kvzalloc((nr_types + 1) * sizeof(*resolved_sizes),
  592. GFP_KERNEL | __GFP_NOWARN);
  593. if (!resolved_sizes)
  594. goto nomem;
  595. resolved_ids = kvzalloc((nr_types + 1) * sizeof(*resolved_ids),
  596. GFP_KERNEL | __GFP_NOWARN);
  597. if (!resolved_ids)
  598. goto nomem;
  599. visit_states = kvzalloc((nr_types + 1) * sizeof(*visit_states),
  600. GFP_KERNEL | __GFP_NOWARN);
  601. if (!visit_states)
  602. goto nomem;
  603. btf->resolved_sizes = resolved_sizes;
  604. btf->resolved_ids = resolved_ids;
  605. env->visit_states = visit_states;
  606. return 0;
  607. nomem:
  608. kvfree(resolved_sizes);
  609. kvfree(resolved_ids);
  610. kvfree(visit_states);
  611. return -ENOMEM;
  612. }
  613. static void btf_verifier_env_free(struct btf_verifier_env *env)
  614. {
  615. kvfree(env->visit_states);
  616. kfree(env);
  617. }
  618. static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
  619. const struct btf_type *next_type)
  620. {
  621. switch (env->resolve_mode) {
  622. case RESOLVE_TBD:
  623. /* int, enum or void is a sink */
  624. return !btf_type_needs_resolve(next_type);
  625. case RESOLVE_PTR:
  626. /* int, enum, void, struct or array is a sink for ptr */
  627. return !btf_type_is_modifier(next_type) &&
  628. !btf_type_is_ptr(next_type);
  629. case RESOLVE_STRUCT_OR_ARRAY:
  630. /* int, enum, void or ptr is a sink for struct and array */
  631. return !btf_type_is_modifier(next_type) &&
  632. !btf_type_is_array(next_type) &&
  633. !btf_type_is_struct(next_type);
  634. default:
  635. BUG_ON(1);
  636. }
  637. }
  638. static bool env_type_is_resolved(const struct btf_verifier_env *env,
  639. u32 type_id)
  640. {
  641. return env->visit_states[type_id] == RESOLVED;
  642. }
  643. static int env_stack_push(struct btf_verifier_env *env,
  644. const struct btf_type *t, u32 type_id)
  645. {
  646. struct resolve_vertex *v;
  647. if (env->top_stack == MAX_RESOLVE_DEPTH)
  648. return -E2BIG;
  649. if (env->visit_states[type_id] != NOT_VISITED)
  650. return -EEXIST;
  651. env->visit_states[type_id] = VISITED;
  652. v = &env->stack[env->top_stack++];
  653. v->t = t;
  654. v->type_id = type_id;
  655. v->next_member = 0;
  656. if (env->resolve_mode == RESOLVE_TBD) {
  657. if (btf_type_is_ptr(t))
  658. env->resolve_mode = RESOLVE_PTR;
  659. else if (btf_type_is_struct(t) || btf_type_is_array(t))
  660. env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
  661. }
  662. return 0;
  663. }
  664. static void env_stack_set_next_member(struct btf_verifier_env *env,
  665. u16 next_member)
  666. {
  667. env->stack[env->top_stack - 1].next_member = next_member;
  668. }
  669. static void env_stack_pop_resolved(struct btf_verifier_env *env,
  670. u32 resolved_type_id,
  671. u32 resolved_size)
  672. {
  673. u32 type_id = env->stack[--(env->top_stack)].type_id;
  674. struct btf *btf = env->btf;
  675. btf->resolved_sizes[type_id] = resolved_size;
  676. btf->resolved_ids[type_id] = resolved_type_id;
  677. env->visit_states[type_id] = RESOLVED;
  678. }
  679. static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
  680. {
  681. return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
  682. }
  683. /* The input param "type_id" must point to a needs_resolve type */
  684. static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
  685. u32 *type_id)
  686. {
  687. *type_id = btf->resolved_ids[*type_id];
  688. return btf_type_by_id(btf, *type_id);
  689. }
  690. const struct btf_type *btf_type_id_size(const struct btf *btf,
  691. u32 *type_id, u32 *ret_size)
  692. {
  693. const struct btf_type *size_type;
  694. u32 size_type_id = *type_id;
  695. u32 size = 0;
  696. size_type = btf_type_by_id(btf, size_type_id);
  697. if (btf_type_is_void_or_null(size_type))
  698. return NULL;
  699. if (btf_type_has_size(size_type)) {
  700. size = size_type->size;
  701. } else if (btf_type_is_array(size_type)) {
  702. size = btf->resolved_sizes[size_type_id];
  703. } else if (btf_type_is_ptr(size_type)) {
  704. size = sizeof(void *);
  705. } else {
  706. if (WARN_ON_ONCE(!btf_type_is_modifier(size_type)))
  707. return NULL;
  708. size = btf->resolved_sizes[size_type_id];
  709. size_type_id = btf->resolved_ids[size_type_id];
  710. size_type = btf_type_by_id(btf, size_type_id);
  711. if (btf_type_is_void(size_type))
  712. return NULL;
  713. }
  714. *type_id = size_type_id;
  715. if (ret_size)
  716. *ret_size = size;
  717. return size_type;
  718. }
  719. static int btf_df_check_member(struct btf_verifier_env *env,
  720. const struct btf_type *struct_type,
  721. const struct btf_member *member,
  722. const struct btf_type *member_type)
  723. {
  724. btf_verifier_log_basic(env, struct_type,
  725. "Unsupported check_member");
  726. return -EINVAL;
  727. }
  728. static int btf_df_resolve(struct btf_verifier_env *env,
  729. const struct resolve_vertex *v)
  730. {
  731. btf_verifier_log_basic(env, v->t, "Unsupported resolve");
  732. return -EINVAL;
  733. }
  734. static void btf_df_seq_show(const struct btf *btf, const struct btf_type *t,
  735. u32 type_id, void *data, u8 bits_offsets,
  736. struct seq_file *m)
  737. {
  738. seq_printf(m, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
  739. }
  740. static int btf_int_check_member(struct btf_verifier_env *env,
  741. const struct btf_type *struct_type,
  742. const struct btf_member *member,
  743. const struct btf_type *member_type)
  744. {
  745. u32 int_data = btf_type_int(member_type);
  746. u32 struct_bits_off = member->offset;
  747. u32 struct_size = struct_type->size;
  748. u32 nr_copy_bits;
  749. u32 bytes_offset;
  750. if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
  751. btf_verifier_log_member(env, struct_type, member,
  752. "bits_offset exceeds U32_MAX");
  753. return -EINVAL;
  754. }
  755. struct_bits_off += BTF_INT_OFFSET(int_data);
  756. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  757. nr_copy_bits = BTF_INT_BITS(int_data) +
  758. BITS_PER_BYTE_MASKED(struct_bits_off);
  759. if (nr_copy_bits > BITS_PER_U64) {
  760. btf_verifier_log_member(env, struct_type, member,
  761. "nr_copy_bits exceeds 64");
  762. return -EINVAL;
  763. }
  764. if (struct_size < bytes_offset ||
  765. struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
  766. btf_verifier_log_member(env, struct_type, member,
  767. "Member exceeds struct_size");
  768. return -EINVAL;
  769. }
  770. return 0;
  771. }
  772. static s32 btf_int_check_meta(struct btf_verifier_env *env,
  773. const struct btf_type *t,
  774. u32 meta_left)
  775. {
  776. u32 int_data, nr_bits, meta_needed = sizeof(int_data);
  777. u16 encoding;
  778. if (meta_left < meta_needed) {
  779. btf_verifier_log_basic(env, t,
  780. "meta_left:%u meta_needed:%u",
  781. meta_left, meta_needed);
  782. return -EINVAL;
  783. }
  784. if (btf_type_vlen(t)) {
  785. btf_verifier_log_type(env, t, "vlen != 0");
  786. return -EINVAL;
  787. }
  788. int_data = btf_type_int(t);
  789. nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
  790. if (nr_bits > BITS_PER_U64) {
  791. btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
  792. BITS_PER_U64);
  793. return -EINVAL;
  794. }
  795. if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
  796. btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
  797. return -EINVAL;
  798. }
  799. encoding = BTF_INT_ENCODING(int_data);
  800. if (encoding &&
  801. encoding != BTF_INT_SIGNED &&
  802. encoding != BTF_INT_CHAR &&
  803. encoding != BTF_INT_BOOL &&
  804. encoding != BTF_INT_VARARGS) {
  805. btf_verifier_log_type(env, t, "Unsupported encoding");
  806. return -ENOTSUPP;
  807. }
  808. btf_verifier_log_type(env, t, NULL);
  809. return meta_needed;
  810. }
  811. static void btf_int_log(struct btf_verifier_env *env,
  812. const struct btf_type *t)
  813. {
  814. int int_data = btf_type_int(t);
  815. btf_verifier_log(env,
  816. "size=%u bits_offset=%u nr_bits=%u encoding=%s",
  817. t->size, BTF_INT_OFFSET(int_data),
  818. BTF_INT_BITS(int_data),
  819. btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
  820. }
  821. static void btf_int_bits_seq_show(const struct btf *btf,
  822. const struct btf_type *t,
  823. void *data, u8 bits_offset,
  824. struct seq_file *m)
  825. {
  826. u32 int_data = btf_type_int(t);
  827. u16 nr_bits = BTF_INT_BITS(int_data);
  828. u16 total_bits_offset;
  829. u16 nr_copy_bytes;
  830. u16 nr_copy_bits;
  831. u8 nr_upper_bits;
  832. union {
  833. u64 u64_num;
  834. u8 u8_nums[8];
  835. } print_num;
  836. total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
  837. data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
  838. bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
  839. nr_copy_bits = nr_bits + bits_offset;
  840. nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
  841. print_num.u64_num = 0;
  842. memcpy(&print_num.u64_num, data, nr_copy_bytes);
  843. /* Ditch the higher order bits */
  844. nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
  845. if (nr_upper_bits) {
  846. /* We need to mask out some bits of the upper byte. */
  847. u8 mask = (1 << nr_upper_bits) - 1;
  848. print_num.u8_nums[nr_copy_bytes - 1] &= mask;
  849. }
  850. print_num.u64_num >>= bits_offset;
  851. seq_printf(m, "0x%llx", print_num.u64_num);
  852. }
  853. static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
  854. u32 type_id, void *data, u8 bits_offset,
  855. struct seq_file *m)
  856. {
  857. u32 int_data = btf_type_int(t);
  858. u8 encoding = BTF_INT_ENCODING(int_data);
  859. bool sign = encoding & BTF_INT_SIGNED;
  860. u32 nr_bits = BTF_INT_BITS(int_data);
  861. if (bits_offset || BTF_INT_OFFSET(int_data) ||
  862. BITS_PER_BYTE_MASKED(nr_bits)) {
  863. btf_int_bits_seq_show(btf, t, data, bits_offset, m);
  864. return;
  865. }
  866. switch (nr_bits) {
  867. case 64:
  868. if (sign)
  869. seq_printf(m, "%lld", *(s64 *)data);
  870. else
  871. seq_printf(m, "%llu", *(u64 *)data);
  872. break;
  873. case 32:
  874. if (sign)
  875. seq_printf(m, "%d", *(s32 *)data);
  876. else
  877. seq_printf(m, "%u", *(u32 *)data);
  878. break;
  879. case 16:
  880. if (sign)
  881. seq_printf(m, "%d", *(s16 *)data);
  882. else
  883. seq_printf(m, "%u", *(u16 *)data);
  884. break;
  885. case 8:
  886. if (sign)
  887. seq_printf(m, "%d", *(s8 *)data);
  888. else
  889. seq_printf(m, "%u", *(u8 *)data);
  890. break;
  891. default:
  892. btf_int_bits_seq_show(btf, t, data, bits_offset, m);
  893. }
  894. }
  895. static const struct btf_kind_operations int_ops = {
  896. .check_meta = btf_int_check_meta,
  897. .resolve = btf_df_resolve,
  898. .check_member = btf_int_check_member,
  899. .log_details = btf_int_log,
  900. .seq_show = btf_int_seq_show,
  901. };
  902. static int btf_modifier_check_member(struct btf_verifier_env *env,
  903. const struct btf_type *struct_type,
  904. const struct btf_member *member,
  905. const struct btf_type *member_type)
  906. {
  907. const struct btf_type *resolved_type;
  908. u32 resolved_type_id = member->type;
  909. struct btf_member resolved_member;
  910. struct btf *btf = env->btf;
  911. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  912. if (!resolved_type) {
  913. btf_verifier_log_member(env, struct_type, member,
  914. "Invalid member");
  915. return -EINVAL;
  916. }
  917. resolved_member = *member;
  918. resolved_member.type = resolved_type_id;
  919. return btf_type_ops(resolved_type)->check_member(env, struct_type,
  920. &resolved_member,
  921. resolved_type);
  922. }
  923. static int btf_ptr_check_member(struct btf_verifier_env *env,
  924. const struct btf_type *struct_type,
  925. const struct btf_member *member,
  926. const struct btf_type *member_type)
  927. {
  928. u32 struct_size, struct_bits_off, bytes_offset;
  929. struct_size = struct_type->size;
  930. struct_bits_off = member->offset;
  931. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  932. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  933. btf_verifier_log_member(env, struct_type, member,
  934. "Member is not byte aligned");
  935. return -EINVAL;
  936. }
  937. if (struct_size - bytes_offset < sizeof(void *)) {
  938. btf_verifier_log_member(env, struct_type, member,
  939. "Member exceeds struct_size");
  940. return -EINVAL;
  941. }
  942. return 0;
  943. }
  944. static int btf_ref_type_check_meta(struct btf_verifier_env *env,
  945. const struct btf_type *t,
  946. u32 meta_left)
  947. {
  948. if (btf_type_vlen(t)) {
  949. btf_verifier_log_type(env, t, "vlen != 0");
  950. return -EINVAL;
  951. }
  952. if (BTF_TYPE_PARENT(t->type)) {
  953. btf_verifier_log_type(env, t, "Invalid type_id");
  954. return -EINVAL;
  955. }
  956. btf_verifier_log_type(env, t, NULL);
  957. return 0;
  958. }
  959. static int btf_modifier_resolve(struct btf_verifier_env *env,
  960. const struct resolve_vertex *v)
  961. {
  962. const struct btf_type *t = v->t;
  963. const struct btf_type *next_type;
  964. u32 next_type_id = t->type;
  965. struct btf *btf = env->btf;
  966. u32 next_type_size = 0;
  967. next_type = btf_type_by_id(btf, next_type_id);
  968. if (!next_type) {
  969. btf_verifier_log_type(env, v->t, "Invalid type_id");
  970. return -EINVAL;
  971. }
  972. /* "typedef void new_void", "const void"...etc */
  973. if (btf_type_is_void(next_type))
  974. goto resolved;
  975. if (!env_type_is_resolve_sink(env, next_type) &&
  976. !env_type_is_resolved(env, next_type_id))
  977. return env_stack_push(env, next_type, next_type_id);
  978. /* Figure out the resolved next_type_id with size.
  979. * They will be stored in the current modifier's
  980. * resolved_ids and resolved_sizes such that it can
  981. * save us a few type-following when we use it later (e.g. in
  982. * pretty print).
  983. */
  984. if (!btf_type_id_size(btf, &next_type_id, &next_type_size) &&
  985. !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) {
  986. btf_verifier_log_type(env, v->t, "Invalid type_id");
  987. return -EINVAL;
  988. }
  989. resolved:
  990. env_stack_pop_resolved(env, next_type_id, next_type_size);
  991. return 0;
  992. }
  993. static int btf_ptr_resolve(struct btf_verifier_env *env,
  994. const struct resolve_vertex *v)
  995. {
  996. const struct btf_type *next_type;
  997. const struct btf_type *t = v->t;
  998. u32 next_type_id = t->type;
  999. struct btf *btf = env->btf;
  1000. u32 next_type_size = 0;
  1001. next_type = btf_type_by_id(btf, next_type_id);
  1002. if (!next_type) {
  1003. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1004. return -EINVAL;
  1005. }
  1006. /* "void *" */
  1007. if (btf_type_is_void(next_type))
  1008. goto resolved;
  1009. if (!env_type_is_resolve_sink(env, next_type) &&
  1010. !env_type_is_resolved(env, next_type_id))
  1011. return env_stack_push(env, next_type, next_type_id);
  1012. /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
  1013. * the modifier may have stopped resolving when it was resolved
  1014. * to a ptr (last-resolved-ptr).
  1015. *
  1016. * We now need to continue from the last-resolved-ptr to
  1017. * ensure the last-resolved-ptr will not referring back to
  1018. * the currenct ptr (t).
  1019. */
  1020. if (btf_type_is_modifier(next_type)) {
  1021. const struct btf_type *resolved_type;
  1022. u32 resolved_type_id;
  1023. resolved_type_id = next_type_id;
  1024. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  1025. if (btf_type_is_ptr(resolved_type) &&
  1026. !env_type_is_resolve_sink(env, resolved_type) &&
  1027. !env_type_is_resolved(env, resolved_type_id))
  1028. return env_stack_push(env, resolved_type,
  1029. resolved_type_id);
  1030. }
  1031. if (!btf_type_id_size(btf, &next_type_id, &next_type_size) &&
  1032. !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) {
  1033. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1034. return -EINVAL;
  1035. }
  1036. resolved:
  1037. env_stack_pop_resolved(env, next_type_id, 0);
  1038. return 0;
  1039. }
  1040. static void btf_modifier_seq_show(const struct btf *btf,
  1041. const struct btf_type *t,
  1042. u32 type_id, void *data,
  1043. u8 bits_offset, struct seq_file *m)
  1044. {
  1045. t = btf_type_id_resolve(btf, &type_id);
  1046. btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
  1047. }
  1048. static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
  1049. u32 type_id, void *data, u8 bits_offset,
  1050. struct seq_file *m)
  1051. {
  1052. /* It is a hashed value */
  1053. seq_printf(m, "%p", *(void **)data);
  1054. }
  1055. static void btf_ref_type_log(struct btf_verifier_env *env,
  1056. const struct btf_type *t)
  1057. {
  1058. btf_verifier_log(env, "type_id=%u", t->type);
  1059. }
  1060. static struct btf_kind_operations modifier_ops = {
  1061. .check_meta = btf_ref_type_check_meta,
  1062. .resolve = btf_modifier_resolve,
  1063. .check_member = btf_modifier_check_member,
  1064. .log_details = btf_ref_type_log,
  1065. .seq_show = btf_modifier_seq_show,
  1066. };
  1067. static struct btf_kind_operations ptr_ops = {
  1068. .check_meta = btf_ref_type_check_meta,
  1069. .resolve = btf_ptr_resolve,
  1070. .check_member = btf_ptr_check_member,
  1071. .log_details = btf_ref_type_log,
  1072. .seq_show = btf_ptr_seq_show,
  1073. };
  1074. static struct btf_kind_operations fwd_ops = {
  1075. .check_meta = btf_ref_type_check_meta,
  1076. .resolve = btf_df_resolve,
  1077. .check_member = btf_df_check_member,
  1078. .log_details = btf_ref_type_log,
  1079. .seq_show = btf_df_seq_show,
  1080. };
  1081. static int btf_array_check_member(struct btf_verifier_env *env,
  1082. const struct btf_type *struct_type,
  1083. const struct btf_member *member,
  1084. const struct btf_type *member_type)
  1085. {
  1086. u32 struct_bits_off = member->offset;
  1087. u32 struct_size, bytes_offset;
  1088. u32 array_type_id, array_size;
  1089. struct btf *btf = env->btf;
  1090. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1091. btf_verifier_log_member(env, struct_type, member,
  1092. "Member is not byte aligned");
  1093. return -EINVAL;
  1094. }
  1095. array_type_id = member->type;
  1096. btf_type_id_size(btf, &array_type_id, &array_size);
  1097. struct_size = struct_type->size;
  1098. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1099. if (struct_size - bytes_offset < array_size) {
  1100. btf_verifier_log_member(env, struct_type, member,
  1101. "Member exceeds struct_size");
  1102. return -EINVAL;
  1103. }
  1104. return 0;
  1105. }
  1106. static s32 btf_array_check_meta(struct btf_verifier_env *env,
  1107. const struct btf_type *t,
  1108. u32 meta_left)
  1109. {
  1110. const struct btf_array *array = btf_type_array(t);
  1111. u32 meta_needed = sizeof(*array);
  1112. if (meta_left < meta_needed) {
  1113. btf_verifier_log_basic(env, t,
  1114. "meta_left:%u meta_needed:%u",
  1115. meta_left, meta_needed);
  1116. return -EINVAL;
  1117. }
  1118. if (btf_type_vlen(t)) {
  1119. btf_verifier_log_type(env, t, "vlen != 0");
  1120. return -EINVAL;
  1121. }
  1122. /* We are a little forgiving on array->index_type since
  1123. * the kernel is not using it.
  1124. */
  1125. /* Array elem cannot be in type void,
  1126. * so !array->type is not allowed.
  1127. */
  1128. if (!array->type || BTF_TYPE_PARENT(array->type)) {
  1129. btf_verifier_log_type(env, t, "Invalid type_id");
  1130. return -EINVAL;
  1131. }
  1132. btf_verifier_log_type(env, t, NULL);
  1133. return meta_needed;
  1134. }
  1135. static int btf_array_resolve(struct btf_verifier_env *env,
  1136. const struct resolve_vertex *v)
  1137. {
  1138. const struct btf_array *array = btf_type_array(v->t);
  1139. const struct btf_type *elem_type;
  1140. u32 elem_type_id = array->type;
  1141. struct btf *btf = env->btf;
  1142. u32 elem_size;
  1143. elem_type = btf_type_by_id(btf, elem_type_id);
  1144. if (btf_type_is_void_or_null(elem_type)) {
  1145. btf_verifier_log_type(env, v->t,
  1146. "Invalid elem");
  1147. return -EINVAL;
  1148. }
  1149. if (!env_type_is_resolve_sink(env, elem_type) &&
  1150. !env_type_is_resolved(env, elem_type_id))
  1151. return env_stack_push(env, elem_type, elem_type_id);
  1152. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1153. if (!elem_type) {
  1154. btf_verifier_log_type(env, v->t, "Invalid elem");
  1155. return -EINVAL;
  1156. }
  1157. if (btf_type_is_int(elem_type)) {
  1158. int int_type_data = btf_type_int(elem_type);
  1159. u16 nr_bits = BTF_INT_BITS(int_type_data);
  1160. u16 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
  1161. /* Put more restriction on array of int. The int cannot
  1162. * be a bit field and it must be either u8/u16/u32/u64.
  1163. */
  1164. if (BITS_PER_BYTE_MASKED(nr_bits) ||
  1165. BTF_INT_OFFSET(int_type_data) ||
  1166. (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
  1167. nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64))) {
  1168. btf_verifier_log_type(env, v->t,
  1169. "Invalid array of int");
  1170. return -EINVAL;
  1171. }
  1172. }
  1173. if (array->nelems && elem_size > U32_MAX / array->nelems) {
  1174. btf_verifier_log_type(env, v->t,
  1175. "Array size overflows U32_MAX");
  1176. return -EINVAL;
  1177. }
  1178. env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
  1179. return 0;
  1180. }
  1181. static void btf_array_log(struct btf_verifier_env *env,
  1182. const struct btf_type *t)
  1183. {
  1184. const struct btf_array *array = btf_type_array(t);
  1185. btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
  1186. array->type, array->index_type, array->nelems);
  1187. }
  1188. static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
  1189. u32 type_id, void *data, u8 bits_offset,
  1190. struct seq_file *m)
  1191. {
  1192. const struct btf_array *array = btf_type_array(t);
  1193. const struct btf_kind_operations *elem_ops;
  1194. const struct btf_type *elem_type;
  1195. u32 i, elem_size, elem_type_id;
  1196. elem_type_id = array->type;
  1197. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1198. elem_ops = btf_type_ops(elem_type);
  1199. seq_puts(m, "[");
  1200. for (i = 0; i < array->nelems; i++) {
  1201. if (i)
  1202. seq_puts(m, ",");
  1203. elem_ops->seq_show(btf, elem_type, elem_type_id, data,
  1204. bits_offset, m);
  1205. data += elem_size;
  1206. }
  1207. seq_puts(m, "]");
  1208. }
  1209. static struct btf_kind_operations array_ops = {
  1210. .check_meta = btf_array_check_meta,
  1211. .resolve = btf_array_resolve,
  1212. .check_member = btf_array_check_member,
  1213. .log_details = btf_array_log,
  1214. .seq_show = btf_array_seq_show,
  1215. };
  1216. static int btf_struct_check_member(struct btf_verifier_env *env,
  1217. const struct btf_type *struct_type,
  1218. const struct btf_member *member,
  1219. const struct btf_type *member_type)
  1220. {
  1221. u32 struct_bits_off = member->offset;
  1222. u32 struct_size, bytes_offset;
  1223. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1224. btf_verifier_log_member(env, struct_type, member,
  1225. "Member is not byte aligned");
  1226. return -EINVAL;
  1227. }
  1228. struct_size = struct_type->size;
  1229. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1230. if (struct_size - bytes_offset < member_type->size) {
  1231. btf_verifier_log_member(env, struct_type, member,
  1232. "Member exceeds struct_size");
  1233. return -EINVAL;
  1234. }
  1235. return 0;
  1236. }
  1237. static s32 btf_struct_check_meta(struct btf_verifier_env *env,
  1238. const struct btf_type *t,
  1239. u32 meta_left)
  1240. {
  1241. bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
  1242. const struct btf_member *member;
  1243. struct btf *btf = env->btf;
  1244. u32 struct_size = t->size;
  1245. u32 meta_needed;
  1246. u16 i;
  1247. meta_needed = btf_type_vlen(t) * sizeof(*member);
  1248. if (meta_left < meta_needed) {
  1249. btf_verifier_log_basic(env, t,
  1250. "meta_left:%u meta_needed:%u",
  1251. meta_left, meta_needed);
  1252. return -EINVAL;
  1253. }
  1254. btf_verifier_log_type(env, t, NULL);
  1255. for_each_member(i, t, member) {
  1256. if (!btf_name_offset_valid(btf, member->name_off)) {
  1257. btf_verifier_log_member(env, t, member,
  1258. "Invalid member name_offset:%u",
  1259. member->name_off);
  1260. return -EINVAL;
  1261. }
  1262. /* A member cannot be in type void */
  1263. if (!member->type || BTF_TYPE_PARENT(member->type)) {
  1264. btf_verifier_log_member(env, t, member,
  1265. "Invalid type_id");
  1266. return -EINVAL;
  1267. }
  1268. if (is_union && member->offset) {
  1269. btf_verifier_log_member(env, t, member,
  1270. "Invalid member bits_offset");
  1271. return -EINVAL;
  1272. }
  1273. if (BITS_ROUNDUP_BYTES(member->offset) > struct_size) {
  1274. btf_verifier_log_member(env, t, member,
  1275. "Memmber bits_offset exceeds its struct size");
  1276. return -EINVAL;
  1277. }
  1278. btf_verifier_log_member(env, t, member, NULL);
  1279. }
  1280. return meta_needed;
  1281. }
  1282. static int btf_struct_resolve(struct btf_verifier_env *env,
  1283. const struct resolve_vertex *v)
  1284. {
  1285. const struct btf_member *member;
  1286. int err;
  1287. u16 i;
  1288. /* Before continue resolving the next_member,
  1289. * ensure the last member is indeed resolved to a
  1290. * type with size info.
  1291. */
  1292. if (v->next_member) {
  1293. const struct btf_type *last_member_type;
  1294. const struct btf_member *last_member;
  1295. u16 last_member_type_id;
  1296. last_member = btf_type_member(v->t) + v->next_member - 1;
  1297. last_member_type_id = last_member->type;
  1298. if (WARN_ON_ONCE(!env_type_is_resolved(env,
  1299. last_member_type_id)))
  1300. return -EINVAL;
  1301. last_member_type = btf_type_by_id(env->btf,
  1302. last_member_type_id);
  1303. err = btf_type_ops(last_member_type)->check_member(env, v->t,
  1304. last_member,
  1305. last_member_type);
  1306. if (err)
  1307. return err;
  1308. }
  1309. for_each_member_from(i, v->next_member, v->t, member) {
  1310. u32 member_type_id = member->type;
  1311. const struct btf_type *member_type = btf_type_by_id(env->btf,
  1312. member_type_id);
  1313. if (btf_type_is_void_or_null(member_type)) {
  1314. btf_verifier_log_member(env, v->t, member,
  1315. "Invalid member");
  1316. return -EINVAL;
  1317. }
  1318. if (!env_type_is_resolve_sink(env, member_type) &&
  1319. !env_type_is_resolved(env, member_type_id)) {
  1320. env_stack_set_next_member(env, i + 1);
  1321. return env_stack_push(env, member_type, member_type_id);
  1322. }
  1323. err = btf_type_ops(member_type)->check_member(env, v->t,
  1324. member,
  1325. member_type);
  1326. if (err)
  1327. return err;
  1328. }
  1329. env_stack_pop_resolved(env, 0, 0);
  1330. return 0;
  1331. }
  1332. static void btf_struct_log(struct btf_verifier_env *env,
  1333. const struct btf_type *t)
  1334. {
  1335. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  1336. }
  1337. static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
  1338. u32 type_id, void *data, u8 bits_offset,
  1339. struct seq_file *m)
  1340. {
  1341. const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
  1342. const struct btf_member *member;
  1343. u32 i;
  1344. seq_puts(m, "{");
  1345. for_each_member(i, t, member) {
  1346. const struct btf_type *member_type = btf_type_by_id(btf,
  1347. member->type);
  1348. u32 member_offset = member->offset;
  1349. u32 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
  1350. u8 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
  1351. const struct btf_kind_operations *ops;
  1352. if (i)
  1353. seq_puts(m, seq);
  1354. ops = btf_type_ops(member_type);
  1355. ops->seq_show(btf, member_type, member->type,
  1356. data + bytes_offset, bits8_offset, m);
  1357. }
  1358. seq_puts(m, "}");
  1359. }
  1360. static struct btf_kind_operations struct_ops = {
  1361. .check_meta = btf_struct_check_meta,
  1362. .resolve = btf_struct_resolve,
  1363. .check_member = btf_struct_check_member,
  1364. .log_details = btf_struct_log,
  1365. .seq_show = btf_struct_seq_show,
  1366. };
  1367. static int btf_enum_check_member(struct btf_verifier_env *env,
  1368. const struct btf_type *struct_type,
  1369. const struct btf_member *member,
  1370. const struct btf_type *member_type)
  1371. {
  1372. u32 struct_bits_off = member->offset;
  1373. u32 struct_size, bytes_offset;
  1374. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1375. btf_verifier_log_member(env, struct_type, member,
  1376. "Member is not byte aligned");
  1377. return -EINVAL;
  1378. }
  1379. struct_size = struct_type->size;
  1380. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1381. if (struct_size - bytes_offset < sizeof(int)) {
  1382. btf_verifier_log_member(env, struct_type, member,
  1383. "Member exceeds struct_size");
  1384. return -EINVAL;
  1385. }
  1386. return 0;
  1387. }
  1388. static s32 btf_enum_check_meta(struct btf_verifier_env *env,
  1389. const struct btf_type *t,
  1390. u32 meta_left)
  1391. {
  1392. const struct btf_enum *enums = btf_type_enum(t);
  1393. struct btf *btf = env->btf;
  1394. u16 i, nr_enums;
  1395. u32 meta_needed;
  1396. nr_enums = btf_type_vlen(t);
  1397. meta_needed = nr_enums * sizeof(*enums);
  1398. if (meta_left < meta_needed) {
  1399. btf_verifier_log_basic(env, t,
  1400. "meta_left:%u meta_needed:%u",
  1401. meta_left, meta_needed);
  1402. return -EINVAL;
  1403. }
  1404. if (t->size != sizeof(int)) {
  1405. btf_verifier_log_type(env, t, "Expected size:%zu",
  1406. sizeof(int));
  1407. return -EINVAL;
  1408. }
  1409. btf_verifier_log_type(env, t, NULL);
  1410. for (i = 0; i < nr_enums; i++) {
  1411. if (!btf_name_offset_valid(btf, enums[i].name_off)) {
  1412. btf_verifier_log(env, "\tInvalid name_offset:%u",
  1413. enums[i].name_off);
  1414. return -EINVAL;
  1415. }
  1416. btf_verifier_log(env, "\t%s val=%d\n",
  1417. btf_name_by_offset(btf, enums[i].name_off),
  1418. enums[i].val);
  1419. }
  1420. return meta_needed;
  1421. }
  1422. static void btf_enum_log(struct btf_verifier_env *env,
  1423. const struct btf_type *t)
  1424. {
  1425. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  1426. }
  1427. static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
  1428. u32 type_id, void *data, u8 bits_offset,
  1429. struct seq_file *m)
  1430. {
  1431. const struct btf_enum *enums = btf_type_enum(t);
  1432. u32 i, nr_enums = btf_type_vlen(t);
  1433. int v = *(int *)data;
  1434. for (i = 0; i < nr_enums; i++) {
  1435. if (v == enums[i].val) {
  1436. seq_printf(m, "%s",
  1437. btf_name_by_offset(btf, enums[i].name_off));
  1438. return;
  1439. }
  1440. }
  1441. seq_printf(m, "%d", v);
  1442. }
  1443. static struct btf_kind_operations enum_ops = {
  1444. .check_meta = btf_enum_check_meta,
  1445. .resolve = btf_df_resolve,
  1446. .check_member = btf_enum_check_member,
  1447. .log_details = btf_enum_log,
  1448. .seq_show = btf_enum_seq_show,
  1449. };
  1450. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
  1451. [BTF_KIND_INT] = &int_ops,
  1452. [BTF_KIND_PTR] = &ptr_ops,
  1453. [BTF_KIND_ARRAY] = &array_ops,
  1454. [BTF_KIND_STRUCT] = &struct_ops,
  1455. [BTF_KIND_UNION] = &struct_ops,
  1456. [BTF_KIND_ENUM] = &enum_ops,
  1457. [BTF_KIND_FWD] = &fwd_ops,
  1458. [BTF_KIND_TYPEDEF] = &modifier_ops,
  1459. [BTF_KIND_VOLATILE] = &modifier_ops,
  1460. [BTF_KIND_CONST] = &modifier_ops,
  1461. [BTF_KIND_RESTRICT] = &modifier_ops,
  1462. };
  1463. static s32 btf_check_meta(struct btf_verifier_env *env,
  1464. const struct btf_type *t,
  1465. u32 meta_left)
  1466. {
  1467. u32 saved_meta_left = meta_left;
  1468. s32 var_meta_size;
  1469. if (meta_left < sizeof(*t)) {
  1470. btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
  1471. env->log_type_id, meta_left, sizeof(*t));
  1472. return -EINVAL;
  1473. }
  1474. meta_left -= sizeof(*t);
  1475. if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
  1476. BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
  1477. btf_verifier_log(env, "[%u] Invalid kind:%u",
  1478. env->log_type_id, BTF_INFO_KIND(t->info));
  1479. return -EINVAL;
  1480. }
  1481. if (!btf_name_offset_valid(env->btf, t->name_off)) {
  1482. btf_verifier_log(env, "[%u] Invalid name_offset:%u",
  1483. env->log_type_id, t->name_off);
  1484. return -EINVAL;
  1485. }
  1486. var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
  1487. if (var_meta_size < 0)
  1488. return var_meta_size;
  1489. meta_left -= var_meta_size;
  1490. return saved_meta_left - meta_left;
  1491. }
  1492. static int btf_check_all_metas(struct btf_verifier_env *env)
  1493. {
  1494. struct btf *btf = env->btf;
  1495. struct btf_header *hdr;
  1496. void *cur, *end;
  1497. hdr = btf->hdr;
  1498. cur = btf->nohdr_data + hdr->type_off;
  1499. end = btf->nohdr_data + hdr->str_off;
  1500. env->log_type_id = 1;
  1501. while (cur < end) {
  1502. struct btf_type *t = cur;
  1503. s32 meta_size;
  1504. meta_size = btf_check_meta(env, t, end - cur);
  1505. if (meta_size < 0)
  1506. return meta_size;
  1507. btf_add_type(env, t);
  1508. cur += meta_size;
  1509. env->log_type_id++;
  1510. }
  1511. return 0;
  1512. }
  1513. static int btf_resolve(struct btf_verifier_env *env,
  1514. const struct btf_type *t, u32 type_id)
  1515. {
  1516. const struct resolve_vertex *v;
  1517. int err = 0;
  1518. env->resolve_mode = RESOLVE_TBD;
  1519. env_stack_push(env, t, type_id);
  1520. while (!err && (v = env_stack_peak(env))) {
  1521. env->log_type_id = v->type_id;
  1522. err = btf_type_ops(v->t)->resolve(env, v);
  1523. }
  1524. env->log_type_id = type_id;
  1525. if (err == -E2BIG)
  1526. btf_verifier_log_type(env, t,
  1527. "Exceeded max resolving depth:%u",
  1528. MAX_RESOLVE_DEPTH);
  1529. else if (err == -EEXIST)
  1530. btf_verifier_log_type(env, t, "Loop detected");
  1531. return err;
  1532. }
  1533. static bool btf_resolve_valid(struct btf_verifier_env *env,
  1534. const struct btf_type *t,
  1535. u32 type_id)
  1536. {
  1537. struct btf *btf = env->btf;
  1538. if (!env_type_is_resolved(env, type_id))
  1539. return false;
  1540. if (btf_type_is_struct(t))
  1541. return !btf->resolved_ids[type_id] &&
  1542. !btf->resolved_sizes[type_id];
  1543. if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) {
  1544. t = btf_type_id_resolve(btf, &type_id);
  1545. return t && !btf_type_is_modifier(t);
  1546. }
  1547. if (btf_type_is_array(t)) {
  1548. const struct btf_array *array = btf_type_array(t);
  1549. const struct btf_type *elem_type;
  1550. u32 elem_type_id = array->type;
  1551. u32 elem_size;
  1552. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1553. return elem_type && !btf_type_is_modifier(elem_type) &&
  1554. (array->nelems * elem_size ==
  1555. btf->resolved_sizes[type_id]);
  1556. }
  1557. return false;
  1558. }
  1559. static int btf_check_all_types(struct btf_verifier_env *env)
  1560. {
  1561. struct btf *btf = env->btf;
  1562. u32 type_id;
  1563. int err;
  1564. err = env_resolve_init(env);
  1565. if (err)
  1566. return err;
  1567. env->phase++;
  1568. for (type_id = 1; type_id <= btf->nr_types; type_id++) {
  1569. const struct btf_type *t = btf_type_by_id(btf, type_id);
  1570. env->log_type_id = type_id;
  1571. if (btf_type_needs_resolve(t) &&
  1572. !env_type_is_resolved(env, type_id)) {
  1573. err = btf_resolve(env, t, type_id);
  1574. if (err)
  1575. return err;
  1576. }
  1577. if (btf_type_needs_resolve(t) &&
  1578. !btf_resolve_valid(env, t, type_id)) {
  1579. btf_verifier_log_type(env, t, "Invalid resolve state");
  1580. return -EINVAL;
  1581. }
  1582. }
  1583. return 0;
  1584. }
  1585. static int btf_parse_type_sec(struct btf_verifier_env *env)
  1586. {
  1587. int err;
  1588. err = btf_check_all_metas(env);
  1589. if (err)
  1590. return err;
  1591. return btf_check_all_types(env);
  1592. }
  1593. static int btf_parse_str_sec(struct btf_verifier_env *env)
  1594. {
  1595. const struct btf_header *hdr;
  1596. struct btf *btf = env->btf;
  1597. const char *start, *end;
  1598. hdr = btf->hdr;
  1599. start = btf->nohdr_data + hdr->str_off;
  1600. end = start + hdr->str_len;
  1601. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
  1602. start[0] || end[-1]) {
  1603. btf_verifier_log(env, "Invalid string section");
  1604. return -EINVAL;
  1605. }
  1606. btf->strings = start;
  1607. return 0;
  1608. }
  1609. static int btf_parse_hdr(struct btf_verifier_env *env)
  1610. {
  1611. const struct btf_header *hdr;
  1612. struct btf *btf = env->btf;
  1613. u32 meta_left;
  1614. if (btf->data_size < sizeof(*hdr)) {
  1615. btf_verifier_log(env, "btf_header not found");
  1616. return -EINVAL;
  1617. }
  1618. btf_verifier_log_hdr(env);
  1619. hdr = btf->hdr;
  1620. if (hdr->magic != BTF_MAGIC) {
  1621. btf_verifier_log(env, "Invalid magic");
  1622. return -EINVAL;
  1623. }
  1624. if (hdr->version != BTF_VERSION) {
  1625. btf_verifier_log(env, "Unsupported version");
  1626. return -ENOTSUPP;
  1627. }
  1628. if (hdr->flags) {
  1629. btf_verifier_log(env, "Unsupported flags");
  1630. return -ENOTSUPP;
  1631. }
  1632. meta_left = btf->data_size - sizeof(*hdr);
  1633. if (!meta_left) {
  1634. btf_verifier_log(env, "No data");
  1635. return -EINVAL;
  1636. }
  1637. if (meta_left < hdr->type_off || hdr->str_off <= hdr->type_off ||
  1638. /* Type section must align to 4 bytes */
  1639. hdr->type_off & (sizeof(u32) - 1)) {
  1640. btf_verifier_log(env, "Invalid type_off");
  1641. return -EINVAL;
  1642. }
  1643. if (meta_left < hdr->str_off ||
  1644. meta_left - hdr->str_off < hdr->str_len) {
  1645. btf_verifier_log(env, "Invalid str_off or str_len");
  1646. return -EINVAL;
  1647. }
  1648. btf->nohdr_data = btf->hdr + 1;
  1649. return 0;
  1650. }
  1651. static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
  1652. u32 log_level, char __user *log_ubuf, u32 log_size)
  1653. {
  1654. struct btf_verifier_env *env = NULL;
  1655. struct bpf_verifier_log *log;
  1656. struct btf *btf = NULL;
  1657. u8 *data;
  1658. int err;
  1659. if (btf_data_size > BTF_MAX_SIZE)
  1660. return ERR_PTR(-E2BIG);
  1661. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  1662. if (!env)
  1663. return ERR_PTR(-ENOMEM);
  1664. log = &env->log;
  1665. if (log_level || log_ubuf || log_size) {
  1666. /* user requested verbose verifier output
  1667. * and supplied buffer to store the verification trace
  1668. */
  1669. log->level = log_level;
  1670. log->ubuf = log_ubuf;
  1671. log->len_total = log_size;
  1672. /* log attributes have to be sane */
  1673. if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
  1674. !log->level || !log->ubuf) {
  1675. err = -EINVAL;
  1676. goto errout;
  1677. }
  1678. }
  1679. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  1680. if (!btf) {
  1681. err = -ENOMEM;
  1682. goto errout;
  1683. }
  1684. data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
  1685. if (!data) {
  1686. err = -ENOMEM;
  1687. goto errout;
  1688. }
  1689. btf->data = data;
  1690. btf->data_size = btf_data_size;
  1691. if (copy_from_user(data, btf_data, btf_data_size)) {
  1692. err = -EFAULT;
  1693. goto errout;
  1694. }
  1695. env->btf = btf;
  1696. err = btf_parse_hdr(env);
  1697. if (err)
  1698. goto errout;
  1699. err = btf_parse_str_sec(env);
  1700. if (err)
  1701. goto errout;
  1702. err = btf_parse_type_sec(env);
  1703. if (err)
  1704. goto errout;
  1705. if (!err && log->level && bpf_verifier_log_full(log)) {
  1706. err = -ENOSPC;
  1707. goto errout;
  1708. }
  1709. if (!err) {
  1710. btf_verifier_env_free(env);
  1711. refcount_set(&btf->refcnt, 1);
  1712. return btf;
  1713. }
  1714. errout:
  1715. btf_verifier_env_free(env);
  1716. if (btf)
  1717. btf_free(btf);
  1718. return ERR_PTR(err);
  1719. }
  1720. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  1721. struct seq_file *m)
  1722. {
  1723. const struct btf_type *t = btf_type_by_id(btf, type_id);
  1724. btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
  1725. }
  1726. static int btf_release(struct inode *inode, struct file *filp)
  1727. {
  1728. btf_put(filp->private_data);
  1729. return 0;
  1730. }
  1731. const struct file_operations btf_fops = {
  1732. .release = btf_release,
  1733. };
  1734. static int __btf_new_fd(struct btf *btf)
  1735. {
  1736. return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
  1737. }
  1738. int btf_new_fd(const union bpf_attr *attr)
  1739. {
  1740. struct btf *btf;
  1741. int ret;
  1742. btf = btf_parse(u64_to_user_ptr(attr->btf),
  1743. attr->btf_size, attr->btf_log_level,
  1744. u64_to_user_ptr(attr->btf_log_buf),
  1745. attr->btf_log_size);
  1746. if (IS_ERR(btf))
  1747. return PTR_ERR(btf);
  1748. ret = btf_alloc_id(btf);
  1749. if (ret) {
  1750. btf_free(btf);
  1751. return ret;
  1752. }
  1753. /*
  1754. * The BTF ID is published to the userspace.
  1755. * All BTF free must go through call_rcu() from
  1756. * now on (i.e. free by calling btf_put()).
  1757. */
  1758. ret = __btf_new_fd(btf);
  1759. if (ret < 0)
  1760. btf_put(btf);
  1761. return ret;
  1762. }
  1763. struct btf *btf_get_by_fd(int fd)
  1764. {
  1765. struct btf *btf;
  1766. struct fd f;
  1767. f = fdget(fd);
  1768. if (!f.file)
  1769. return ERR_PTR(-EBADF);
  1770. if (f.file->f_op != &btf_fops) {
  1771. fdput(f);
  1772. return ERR_PTR(-EINVAL);
  1773. }
  1774. btf = f.file->private_data;
  1775. refcount_inc(&btf->refcnt);
  1776. fdput(f);
  1777. return btf;
  1778. }
  1779. int btf_get_info_by_fd(const struct btf *btf,
  1780. const union bpf_attr *attr,
  1781. union bpf_attr __user *uattr)
  1782. {
  1783. struct bpf_btf_info __user *uinfo;
  1784. struct bpf_btf_info info = {};
  1785. u32 info_copy, btf_copy;
  1786. void __user *ubtf;
  1787. u32 uinfo_len;
  1788. uinfo = u64_to_user_ptr(attr->info.info);
  1789. uinfo_len = attr->info.info_len;
  1790. info_copy = min_t(u32, uinfo_len, sizeof(info));
  1791. if (copy_from_user(&info, uinfo, info_copy))
  1792. return -EFAULT;
  1793. info.id = btf->id;
  1794. ubtf = u64_to_user_ptr(info.btf);
  1795. btf_copy = min_t(u32, btf->data_size, info.btf_size);
  1796. if (copy_to_user(ubtf, btf->data, btf_copy))
  1797. return -EFAULT;
  1798. info.btf_size = btf->data_size;
  1799. if (copy_to_user(uinfo, &info, info_copy) ||
  1800. put_user(info_copy, &uattr->info.info_len))
  1801. return -EFAULT;
  1802. return 0;
  1803. }
  1804. int btf_get_fd_by_id(u32 id)
  1805. {
  1806. struct btf *btf;
  1807. int fd;
  1808. rcu_read_lock();
  1809. btf = idr_find(&btf_idr, id);
  1810. if (!btf || !refcount_inc_not_zero(&btf->refcnt))
  1811. btf = ERR_PTR(-ENOENT);
  1812. rcu_read_unlock();
  1813. if (IS_ERR(btf))
  1814. return PTR_ERR(btf);
  1815. fd = __btf_new_fd(btf);
  1816. if (fd < 0)
  1817. btf_put(btf);
  1818. return fd;
  1819. }
  1820. u32 btf_id(const struct btf *btf)
  1821. {
  1822. return btf->id;
  1823. }