btf.c 58 KB

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