btf.c 60 KB

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