btf.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  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. u16 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. u32 int_data = btf_type_int(t);
  857. u16 nr_bits = BTF_INT_BITS(int_data);
  858. u16 total_bits_offset;
  859. u16 nr_copy_bytes;
  860. u16 nr_copy_bits;
  861. u8 nr_upper_bits;
  862. union {
  863. u64 u64_num;
  864. u8 u8_nums[8];
  865. } print_num;
  866. total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
  867. data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
  868. bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
  869. nr_copy_bits = nr_bits + bits_offset;
  870. nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
  871. print_num.u64_num = 0;
  872. memcpy(&print_num.u64_num, data, nr_copy_bytes);
  873. /* Ditch the higher order bits */
  874. nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
  875. if (nr_upper_bits) {
  876. /* We need to mask out some bits of the upper byte. */
  877. u8 mask = (1 << nr_upper_bits) - 1;
  878. print_num.u8_nums[nr_copy_bytes - 1] &= mask;
  879. }
  880. print_num.u64_num >>= bits_offset;
  881. seq_printf(m, "0x%llx", print_num.u64_num);
  882. }
  883. static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,
  884. u32 type_id, void *data, u8 bits_offset,
  885. struct seq_file *m)
  886. {
  887. u32 int_data = btf_type_int(t);
  888. u8 encoding = BTF_INT_ENCODING(int_data);
  889. bool sign = encoding & BTF_INT_SIGNED;
  890. u32 nr_bits = BTF_INT_BITS(int_data);
  891. if (bits_offset || BTF_INT_OFFSET(int_data) ||
  892. BITS_PER_BYTE_MASKED(nr_bits)) {
  893. btf_int_bits_seq_show(btf, t, data, bits_offset, m);
  894. return;
  895. }
  896. switch (nr_bits) {
  897. case 64:
  898. if (sign)
  899. seq_printf(m, "%lld", *(s64 *)data);
  900. else
  901. seq_printf(m, "%llu", *(u64 *)data);
  902. break;
  903. case 32:
  904. if (sign)
  905. seq_printf(m, "%d", *(s32 *)data);
  906. else
  907. seq_printf(m, "%u", *(u32 *)data);
  908. break;
  909. case 16:
  910. if (sign)
  911. seq_printf(m, "%d", *(s16 *)data);
  912. else
  913. seq_printf(m, "%u", *(u16 *)data);
  914. break;
  915. case 8:
  916. if (sign)
  917. seq_printf(m, "%d", *(s8 *)data);
  918. else
  919. seq_printf(m, "%u", *(u8 *)data);
  920. break;
  921. default:
  922. btf_int_bits_seq_show(btf, t, data, bits_offset, m);
  923. }
  924. }
  925. static const struct btf_kind_operations int_ops = {
  926. .check_meta = btf_int_check_meta,
  927. .resolve = btf_df_resolve,
  928. .check_member = btf_int_check_member,
  929. .log_details = btf_int_log,
  930. .seq_show = btf_int_seq_show,
  931. };
  932. static int btf_modifier_check_member(struct btf_verifier_env *env,
  933. const struct btf_type *struct_type,
  934. const struct btf_member *member,
  935. const struct btf_type *member_type)
  936. {
  937. const struct btf_type *resolved_type;
  938. u32 resolved_type_id = member->type;
  939. struct btf_member resolved_member;
  940. struct btf *btf = env->btf;
  941. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  942. if (!resolved_type) {
  943. btf_verifier_log_member(env, struct_type, member,
  944. "Invalid member");
  945. return -EINVAL;
  946. }
  947. resolved_member = *member;
  948. resolved_member.type = resolved_type_id;
  949. return btf_type_ops(resolved_type)->check_member(env, struct_type,
  950. &resolved_member,
  951. resolved_type);
  952. }
  953. static int btf_ptr_check_member(struct btf_verifier_env *env,
  954. const struct btf_type *struct_type,
  955. const struct btf_member *member,
  956. const struct btf_type *member_type)
  957. {
  958. u32 struct_size, struct_bits_off, bytes_offset;
  959. struct_size = struct_type->size;
  960. struct_bits_off = member->offset;
  961. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  962. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  963. btf_verifier_log_member(env, struct_type, member,
  964. "Member is not byte aligned");
  965. return -EINVAL;
  966. }
  967. if (struct_size - bytes_offset < sizeof(void *)) {
  968. btf_verifier_log_member(env, struct_type, member,
  969. "Member exceeds struct_size");
  970. return -EINVAL;
  971. }
  972. return 0;
  973. }
  974. static int btf_ref_type_check_meta(struct btf_verifier_env *env,
  975. const struct btf_type *t,
  976. u32 meta_left)
  977. {
  978. if (btf_type_vlen(t)) {
  979. btf_verifier_log_type(env, t, "vlen != 0");
  980. return -EINVAL;
  981. }
  982. if (!BTF_TYPE_ID_VALID(t->type)) {
  983. btf_verifier_log_type(env, t, "Invalid type_id");
  984. return -EINVAL;
  985. }
  986. btf_verifier_log_type(env, t, NULL);
  987. return 0;
  988. }
  989. static int btf_modifier_resolve(struct btf_verifier_env *env,
  990. const struct resolve_vertex *v)
  991. {
  992. const struct btf_type *t = v->t;
  993. const struct btf_type *next_type;
  994. u32 next_type_id = t->type;
  995. struct btf *btf = env->btf;
  996. u32 next_type_size = 0;
  997. next_type = btf_type_by_id(btf, next_type_id);
  998. if (!next_type) {
  999. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1000. return -EINVAL;
  1001. }
  1002. /* "typedef void new_void", "const void"...etc */
  1003. if (btf_type_is_void(next_type))
  1004. goto resolved;
  1005. if (!env_type_is_resolve_sink(env, next_type) &&
  1006. !env_type_is_resolved(env, next_type_id))
  1007. return env_stack_push(env, next_type, next_type_id);
  1008. /* Figure out the resolved next_type_id with size.
  1009. * They will be stored in the current modifier's
  1010. * resolved_ids and resolved_sizes such that it can
  1011. * save us a few type-following when we use it later (e.g. in
  1012. * pretty print).
  1013. */
  1014. if (!btf_type_id_size(btf, &next_type_id, &next_type_size) &&
  1015. !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) {
  1016. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1017. return -EINVAL;
  1018. }
  1019. resolved:
  1020. env_stack_pop_resolved(env, next_type_id, next_type_size);
  1021. return 0;
  1022. }
  1023. static int btf_ptr_resolve(struct btf_verifier_env *env,
  1024. const struct resolve_vertex *v)
  1025. {
  1026. const struct btf_type *next_type;
  1027. const struct btf_type *t = v->t;
  1028. u32 next_type_id = t->type;
  1029. struct btf *btf = env->btf;
  1030. u32 next_type_size = 0;
  1031. next_type = btf_type_by_id(btf, next_type_id);
  1032. if (!next_type) {
  1033. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1034. return -EINVAL;
  1035. }
  1036. /* "void *" */
  1037. if (btf_type_is_void(next_type))
  1038. goto resolved;
  1039. if (!env_type_is_resolve_sink(env, next_type) &&
  1040. !env_type_is_resolved(env, next_type_id))
  1041. return env_stack_push(env, next_type, next_type_id);
  1042. /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
  1043. * the modifier may have stopped resolving when it was resolved
  1044. * to a ptr (last-resolved-ptr).
  1045. *
  1046. * We now need to continue from the last-resolved-ptr to
  1047. * ensure the last-resolved-ptr will not referring back to
  1048. * the currenct ptr (t).
  1049. */
  1050. if (btf_type_is_modifier(next_type)) {
  1051. const struct btf_type *resolved_type;
  1052. u32 resolved_type_id;
  1053. resolved_type_id = next_type_id;
  1054. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  1055. if (btf_type_is_ptr(resolved_type) &&
  1056. !env_type_is_resolve_sink(env, resolved_type) &&
  1057. !env_type_is_resolved(env, resolved_type_id))
  1058. return env_stack_push(env, resolved_type,
  1059. resolved_type_id);
  1060. }
  1061. if (!btf_type_id_size(btf, &next_type_id, &next_type_size) &&
  1062. !btf_type_is_void(btf_type_id_resolve(btf, &next_type_id))) {
  1063. btf_verifier_log_type(env, v->t, "Invalid type_id");
  1064. return -EINVAL;
  1065. }
  1066. resolved:
  1067. env_stack_pop_resolved(env, next_type_id, 0);
  1068. return 0;
  1069. }
  1070. static void btf_modifier_seq_show(const struct btf *btf,
  1071. const struct btf_type *t,
  1072. u32 type_id, void *data,
  1073. u8 bits_offset, struct seq_file *m)
  1074. {
  1075. t = btf_type_id_resolve(btf, &type_id);
  1076. btf_type_ops(t)->seq_show(btf, t, type_id, data, bits_offset, m);
  1077. }
  1078. static void btf_ptr_seq_show(const struct btf *btf, const struct btf_type *t,
  1079. u32 type_id, void *data, u8 bits_offset,
  1080. struct seq_file *m)
  1081. {
  1082. /* It is a hashed value */
  1083. seq_printf(m, "%p", *(void **)data);
  1084. }
  1085. static void btf_ref_type_log(struct btf_verifier_env *env,
  1086. const struct btf_type *t)
  1087. {
  1088. btf_verifier_log(env, "type_id=%u", t->type);
  1089. }
  1090. static struct btf_kind_operations modifier_ops = {
  1091. .check_meta = btf_ref_type_check_meta,
  1092. .resolve = btf_modifier_resolve,
  1093. .check_member = btf_modifier_check_member,
  1094. .log_details = btf_ref_type_log,
  1095. .seq_show = btf_modifier_seq_show,
  1096. };
  1097. static struct btf_kind_operations ptr_ops = {
  1098. .check_meta = btf_ref_type_check_meta,
  1099. .resolve = btf_ptr_resolve,
  1100. .check_member = btf_ptr_check_member,
  1101. .log_details = btf_ref_type_log,
  1102. .seq_show = btf_ptr_seq_show,
  1103. };
  1104. static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
  1105. const struct btf_type *t,
  1106. u32 meta_left)
  1107. {
  1108. if (btf_type_vlen(t)) {
  1109. btf_verifier_log_type(env, t, "vlen != 0");
  1110. return -EINVAL;
  1111. }
  1112. if (t->type) {
  1113. btf_verifier_log_type(env, t, "type != 0");
  1114. return -EINVAL;
  1115. }
  1116. btf_verifier_log_type(env, t, NULL);
  1117. return 0;
  1118. }
  1119. static struct btf_kind_operations fwd_ops = {
  1120. .check_meta = btf_fwd_check_meta,
  1121. .resolve = btf_df_resolve,
  1122. .check_member = btf_df_check_member,
  1123. .log_details = btf_ref_type_log,
  1124. .seq_show = btf_df_seq_show,
  1125. };
  1126. static int btf_array_check_member(struct btf_verifier_env *env,
  1127. const struct btf_type *struct_type,
  1128. const struct btf_member *member,
  1129. const struct btf_type *member_type)
  1130. {
  1131. u32 struct_bits_off = member->offset;
  1132. u32 struct_size, bytes_offset;
  1133. u32 array_type_id, array_size;
  1134. struct btf *btf = env->btf;
  1135. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1136. btf_verifier_log_member(env, struct_type, member,
  1137. "Member is not byte aligned");
  1138. return -EINVAL;
  1139. }
  1140. array_type_id = member->type;
  1141. btf_type_id_size(btf, &array_type_id, &array_size);
  1142. struct_size = struct_type->size;
  1143. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1144. if (struct_size - bytes_offset < array_size) {
  1145. btf_verifier_log_member(env, struct_type, member,
  1146. "Member exceeds struct_size");
  1147. return -EINVAL;
  1148. }
  1149. return 0;
  1150. }
  1151. static s32 btf_array_check_meta(struct btf_verifier_env *env,
  1152. const struct btf_type *t,
  1153. u32 meta_left)
  1154. {
  1155. const struct btf_array *array = btf_type_array(t);
  1156. u32 meta_needed = sizeof(*array);
  1157. if (meta_left < meta_needed) {
  1158. btf_verifier_log_basic(env, t,
  1159. "meta_left:%u meta_needed:%u",
  1160. meta_left, meta_needed);
  1161. return -EINVAL;
  1162. }
  1163. if (btf_type_vlen(t)) {
  1164. btf_verifier_log_type(env, t, "vlen != 0");
  1165. return -EINVAL;
  1166. }
  1167. if (t->size) {
  1168. btf_verifier_log_type(env, t, "size != 0");
  1169. return -EINVAL;
  1170. }
  1171. /* Array elem type and index type cannot be in type void,
  1172. * so !array->type and !array->index_type are not allowed.
  1173. */
  1174. if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
  1175. btf_verifier_log_type(env, t, "Invalid elem");
  1176. return -EINVAL;
  1177. }
  1178. if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
  1179. btf_verifier_log_type(env, t, "Invalid index");
  1180. return -EINVAL;
  1181. }
  1182. btf_verifier_log_type(env, t, NULL);
  1183. return meta_needed;
  1184. }
  1185. static int btf_array_resolve(struct btf_verifier_env *env,
  1186. const struct resolve_vertex *v)
  1187. {
  1188. const struct btf_array *array = btf_type_array(v->t);
  1189. const struct btf_type *elem_type, *index_type;
  1190. u32 elem_type_id, index_type_id;
  1191. struct btf *btf = env->btf;
  1192. u32 elem_size;
  1193. /* Check array->index_type */
  1194. index_type_id = array->index_type;
  1195. index_type = btf_type_by_id(btf, index_type_id);
  1196. if (btf_type_is_void_or_null(index_type)) {
  1197. btf_verifier_log_type(env, v->t, "Invalid index");
  1198. return -EINVAL;
  1199. }
  1200. if (!env_type_is_resolve_sink(env, index_type) &&
  1201. !env_type_is_resolved(env, index_type_id))
  1202. return env_stack_push(env, index_type, index_type_id);
  1203. index_type = btf_type_id_size(btf, &index_type_id, NULL);
  1204. if (!index_type || !btf_type_is_int(index_type) ||
  1205. !btf_type_int_is_regular(index_type)) {
  1206. btf_verifier_log_type(env, v->t, "Invalid index");
  1207. return -EINVAL;
  1208. }
  1209. /* Check array->type */
  1210. elem_type_id = array->type;
  1211. elem_type = btf_type_by_id(btf, elem_type_id);
  1212. if (btf_type_is_void_or_null(elem_type)) {
  1213. btf_verifier_log_type(env, v->t,
  1214. "Invalid elem");
  1215. return -EINVAL;
  1216. }
  1217. if (!env_type_is_resolve_sink(env, elem_type) &&
  1218. !env_type_is_resolved(env, elem_type_id))
  1219. return env_stack_push(env, elem_type, elem_type_id);
  1220. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1221. if (!elem_type) {
  1222. btf_verifier_log_type(env, v->t, "Invalid elem");
  1223. return -EINVAL;
  1224. }
  1225. if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
  1226. btf_verifier_log_type(env, v->t, "Invalid array of int");
  1227. return -EINVAL;
  1228. }
  1229. if (array->nelems && elem_size > U32_MAX / array->nelems) {
  1230. btf_verifier_log_type(env, v->t,
  1231. "Array size overflows U32_MAX");
  1232. return -EINVAL;
  1233. }
  1234. env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
  1235. return 0;
  1236. }
  1237. static void btf_array_log(struct btf_verifier_env *env,
  1238. const struct btf_type *t)
  1239. {
  1240. const struct btf_array *array = btf_type_array(t);
  1241. btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
  1242. array->type, array->index_type, array->nelems);
  1243. }
  1244. static void btf_array_seq_show(const struct btf *btf, const struct btf_type *t,
  1245. u32 type_id, void *data, u8 bits_offset,
  1246. struct seq_file *m)
  1247. {
  1248. const struct btf_array *array = btf_type_array(t);
  1249. const struct btf_kind_operations *elem_ops;
  1250. const struct btf_type *elem_type;
  1251. u32 i, elem_size, elem_type_id;
  1252. elem_type_id = array->type;
  1253. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1254. elem_ops = btf_type_ops(elem_type);
  1255. seq_puts(m, "[");
  1256. for (i = 0; i < array->nelems; i++) {
  1257. if (i)
  1258. seq_puts(m, ",");
  1259. elem_ops->seq_show(btf, elem_type, elem_type_id, data,
  1260. bits_offset, m);
  1261. data += elem_size;
  1262. }
  1263. seq_puts(m, "]");
  1264. }
  1265. static struct btf_kind_operations array_ops = {
  1266. .check_meta = btf_array_check_meta,
  1267. .resolve = btf_array_resolve,
  1268. .check_member = btf_array_check_member,
  1269. .log_details = btf_array_log,
  1270. .seq_show = btf_array_seq_show,
  1271. };
  1272. static int btf_struct_check_member(struct btf_verifier_env *env,
  1273. const struct btf_type *struct_type,
  1274. const struct btf_member *member,
  1275. const struct btf_type *member_type)
  1276. {
  1277. u32 struct_bits_off = member->offset;
  1278. u32 struct_size, bytes_offset;
  1279. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1280. btf_verifier_log_member(env, struct_type, member,
  1281. "Member is not byte aligned");
  1282. return -EINVAL;
  1283. }
  1284. struct_size = struct_type->size;
  1285. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1286. if (struct_size - bytes_offset < member_type->size) {
  1287. btf_verifier_log_member(env, struct_type, member,
  1288. "Member exceeds struct_size");
  1289. return -EINVAL;
  1290. }
  1291. return 0;
  1292. }
  1293. static s32 btf_struct_check_meta(struct btf_verifier_env *env,
  1294. const struct btf_type *t,
  1295. u32 meta_left)
  1296. {
  1297. bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
  1298. const struct btf_member *member;
  1299. struct btf *btf = env->btf;
  1300. u32 struct_size = t->size;
  1301. u32 meta_needed;
  1302. u16 i;
  1303. meta_needed = btf_type_vlen(t) * sizeof(*member);
  1304. if (meta_left < meta_needed) {
  1305. btf_verifier_log_basic(env, t,
  1306. "meta_left:%u meta_needed:%u",
  1307. meta_left, meta_needed);
  1308. return -EINVAL;
  1309. }
  1310. btf_verifier_log_type(env, t, NULL);
  1311. for_each_member(i, t, member) {
  1312. if (!btf_name_offset_valid(btf, member->name_off)) {
  1313. btf_verifier_log_member(env, t, member,
  1314. "Invalid member name_offset:%u",
  1315. member->name_off);
  1316. return -EINVAL;
  1317. }
  1318. /* A member cannot be in type void */
  1319. if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
  1320. btf_verifier_log_member(env, t, member,
  1321. "Invalid type_id");
  1322. return -EINVAL;
  1323. }
  1324. if (is_union && member->offset) {
  1325. btf_verifier_log_member(env, t, member,
  1326. "Invalid member bits_offset");
  1327. return -EINVAL;
  1328. }
  1329. if (BITS_ROUNDUP_BYTES(member->offset) > struct_size) {
  1330. btf_verifier_log_member(env, t, member,
  1331. "Memmber bits_offset exceeds its struct size");
  1332. return -EINVAL;
  1333. }
  1334. btf_verifier_log_member(env, t, member, NULL);
  1335. }
  1336. return meta_needed;
  1337. }
  1338. static int btf_struct_resolve(struct btf_verifier_env *env,
  1339. const struct resolve_vertex *v)
  1340. {
  1341. const struct btf_member *member;
  1342. int err;
  1343. u16 i;
  1344. /* Before continue resolving the next_member,
  1345. * ensure the last member is indeed resolved to a
  1346. * type with size info.
  1347. */
  1348. if (v->next_member) {
  1349. const struct btf_type *last_member_type;
  1350. const struct btf_member *last_member;
  1351. u16 last_member_type_id;
  1352. last_member = btf_type_member(v->t) + v->next_member - 1;
  1353. last_member_type_id = last_member->type;
  1354. if (WARN_ON_ONCE(!env_type_is_resolved(env,
  1355. last_member_type_id)))
  1356. return -EINVAL;
  1357. last_member_type = btf_type_by_id(env->btf,
  1358. last_member_type_id);
  1359. err = btf_type_ops(last_member_type)->check_member(env, v->t,
  1360. last_member,
  1361. last_member_type);
  1362. if (err)
  1363. return err;
  1364. }
  1365. for_each_member_from(i, v->next_member, v->t, member) {
  1366. u32 member_type_id = member->type;
  1367. const struct btf_type *member_type = btf_type_by_id(env->btf,
  1368. member_type_id);
  1369. if (btf_type_is_void_or_null(member_type)) {
  1370. btf_verifier_log_member(env, v->t, member,
  1371. "Invalid member");
  1372. return -EINVAL;
  1373. }
  1374. if (!env_type_is_resolve_sink(env, member_type) &&
  1375. !env_type_is_resolved(env, member_type_id)) {
  1376. env_stack_set_next_member(env, i + 1);
  1377. return env_stack_push(env, member_type, member_type_id);
  1378. }
  1379. err = btf_type_ops(member_type)->check_member(env, v->t,
  1380. member,
  1381. member_type);
  1382. if (err)
  1383. return err;
  1384. }
  1385. env_stack_pop_resolved(env, 0, 0);
  1386. return 0;
  1387. }
  1388. static void btf_struct_log(struct btf_verifier_env *env,
  1389. const struct btf_type *t)
  1390. {
  1391. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  1392. }
  1393. static void btf_struct_seq_show(const struct btf *btf, const struct btf_type *t,
  1394. u32 type_id, void *data, u8 bits_offset,
  1395. struct seq_file *m)
  1396. {
  1397. const char *seq = BTF_INFO_KIND(t->info) == BTF_KIND_UNION ? "|" : ",";
  1398. const struct btf_member *member;
  1399. u32 i;
  1400. seq_puts(m, "{");
  1401. for_each_member(i, t, member) {
  1402. const struct btf_type *member_type = btf_type_by_id(btf,
  1403. member->type);
  1404. u32 member_offset = member->offset;
  1405. u32 bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
  1406. u8 bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
  1407. const struct btf_kind_operations *ops;
  1408. if (i)
  1409. seq_puts(m, seq);
  1410. ops = btf_type_ops(member_type);
  1411. ops->seq_show(btf, member_type, member->type,
  1412. data + bytes_offset, bits8_offset, m);
  1413. }
  1414. seq_puts(m, "}");
  1415. }
  1416. static struct btf_kind_operations struct_ops = {
  1417. .check_meta = btf_struct_check_meta,
  1418. .resolve = btf_struct_resolve,
  1419. .check_member = btf_struct_check_member,
  1420. .log_details = btf_struct_log,
  1421. .seq_show = btf_struct_seq_show,
  1422. };
  1423. static int btf_enum_check_member(struct btf_verifier_env *env,
  1424. const struct btf_type *struct_type,
  1425. const struct btf_member *member,
  1426. const struct btf_type *member_type)
  1427. {
  1428. u32 struct_bits_off = member->offset;
  1429. u32 struct_size, bytes_offset;
  1430. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1431. btf_verifier_log_member(env, struct_type, member,
  1432. "Member is not byte aligned");
  1433. return -EINVAL;
  1434. }
  1435. struct_size = struct_type->size;
  1436. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1437. if (struct_size - bytes_offset < sizeof(int)) {
  1438. btf_verifier_log_member(env, struct_type, member,
  1439. "Member exceeds struct_size");
  1440. return -EINVAL;
  1441. }
  1442. return 0;
  1443. }
  1444. static s32 btf_enum_check_meta(struct btf_verifier_env *env,
  1445. const struct btf_type *t,
  1446. u32 meta_left)
  1447. {
  1448. const struct btf_enum *enums = btf_type_enum(t);
  1449. struct btf *btf = env->btf;
  1450. u16 i, nr_enums;
  1451. u32 meta_needed;
  1452. nr_enums = btf_type_vlen(t);
  1453. meta_needed = nr_enums * sizeof(*enums);
  1454. if (meta_left < meta_needed) {
  1455. btf_verifier_log_basic(env, t,
  1456. "meta_left:%u meta_needed:%u",
  1457. meta_left, meta_needed);
  1458. return -EINVAL;
  1459. }
  1460. if (t->size != sizeof(int)) {
  1461. btf_verifier_log_type(env, t, "Expected size:%zu",
  1462. sizeof(int));
  1463. return -EINVAL;
  1464. }
  1465. btf_verifier_log_type(env, t, NULL);
  1466. for (i = 0; i < nr_enums; i++) {
  1467. if (!btf_name_offset_valid(btf, enums[i].name_off)) {
  1468. btf_verifier_log(env, "\tInvalid name_offset:%u",
  1469. enums[i].name_off);
  1470. return -EINVAL;
  1471. }
  1472. btf_verifier_log(env, "\t%s val=%d\n",
  1473. btf_name_by_offset(btf, enums[i].name_off),
  1474. enums[i].val);
  1475. }
  1476. return meta_needed;
  1477. }
  1478. static void btf_enum_log(struct btf_verifier_env *env,
  1479. const struct btf_type *t)
  1480. {
  1481. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  1482. }
  1483. static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
  1484. u32 type_id, void *data, u8 bits_offset,
  1485. struct seq_file *m)
  1486. {
  1487. const struct btf_enum *enums = btf_type_enum(t);
  1488. u32 i, nr_enums = btf_type_vlen(t);
  1489. int v = *(int *)data;
  1490. for (i = 0; i < nr_enums; i++) {
  1491. if (v == enums[i].val) {
  1492. seq_printf(m, "%s",
  1493. btf_name_by_offset(btf, enums[i].name_off));
  1494. return;
  1495. }
  1496. }
  1497. seq_printf(m, "%d", v);
  1498. }
  1499. static struct btf_kind_operations enum_ops = {
  1500. .check_meta = btf_enum_check_meta,
  1501. .resolve = btf_df_resolve,
  1502. .check_member = btf_enum_check_member,
  1503. .log_details = btf_enum_log,
  1504. .seq_show = btf_enum_seq_show,
  1505. };
  1506. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
  1507. [BTF_KIND_INT] = &int_ops,
  1508. [BTF_KIND_PTR] = &ptr_ops,
  1509. [BTF_KIND_ARRAY] = &array_ops,
  1510. [BTF_KIND_STRUCT] = &struct_ops,
  1511. [BTF_KIND_UNION] = &struct_ops,
  1512. [BTF_KIND_ENUM] = &enum_ops,
  1513. [BTF_KIND_FWD] = &fwd_ops,
  1514. [BTF_KIND_TYPEDEF] = &modifier_ops,
  1515. [BTF_KIND_VOLATILE] = &modifier_ops,
  1516. [BTF_KIND_CONST] = &modifier_ops,
  1517. [BTF_KIND_RESTRICT] = &modifier_ops,
  1518. };
  1519. static s32 btf_check_meta(struct btf_verifier_env *env,
  1520. const struct btf_type *t,
  1521. u32 meta_left)
  1522. {
  1523. u32 saved_meta_left = meta_left;
  1524. s32 var_meta_size;
  1525. if (meta_left < sizeof(*t)) {
  1526. btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
  1527. env->log_type_id, meta_left, sizeof(*t));
  1528. return -EINVAL;
  1529. }
  1530. meta_left -= sizeof(*t);
  1531. if (t->info & ~BTF_INFO_MASK) {
  1532. btf_verifier_log(env, "[%u] Invalid btf_info:%x",
  1533. env->log_type_id, t->info);
  1534. return -EINVAL;
  1535. }
  1536. if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
  1537. BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
  1538. btf_verifier_log(env, "[%u] Invalid kind:%u",
  1539. env->log_type_id, BTF_INFO_KIND(t->info));
  1540. return -EINVAL;
  1541. }
  1542. if (!btf_name_offset_valid(env->btf, t->name_off)) {
  1543. btf_verifier_log(env, "[%u] Invalid name_offset:%u",
  1544. env->log_type_id, t->name_off);
  1545. return -EINVAL;
  1546. }
  1547. var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
  1548. if (var_meta_size < 0)
  1549. return var_meta_size;
  1550. meta_left -= var_meta_size;
  1551. return saved_meta_left - meta_left;
  1552. }
  1553. static int btf_check_all_metas(struct btf_verifier_env *env)
  1554. {
  1555. struct btf *btf = env->btf;
  1556. struct btf_header *hdr;
  1557. void *cur, *end;
  1558. hdr = &btf->hdr;
  1559. cur = btf->nohdr_data + hdr->type_off;
  1560. end = btf->nohdr_data + hdr->type_len;
  1561. env->log_type_id = 1;
  1562. while (cur < end) {
  1563. struct btf_type *t = cur;
  1564. s32 meta_size;
  1565. meta_size = btf_check_meta(env, t, end - cur);
  1566. if (meta_size < 0)
  1567. return meta_size;
  1568. btf_add_type(env, t);
  1569. cur += meta_size;
  1570. env->log_type_id++;
  1571. }
  1572. return 0;
  1573. }
  1574. static int btf_resolve(struct btf_verifier_env *env,
  1575. const struct btf_type *t, u32 type_id)
  1576. {
  1577. const struct resolve_vertex *v;
  1578. int err = 0;
  1579. env->resolve_mode = RESOLVE_TBD;
  1580. env_stack_push(env, t, type_id);
  1581. while (!err && (v = env_stack_peak(env))) {
  1582. env->log_type_id = v->type_id;
  1583. err = btf_type_ops(v->t)->resolve(env, v);
  1584. }
  1585. env->log_type_id = type_id;
  1586. if (err == -E2BIG)
  1587. btf_verifier_log_type(env, t,
  1588. "Exceeded max resolving depth:%u",
  1589. MAX_RESOLVE_DEPTH);
  1590. else if (err == -EEXIST)
  1591. btf_verifier_log_type(env, t, "Loop detected");
  1592. return err;
  1593. }
  1594. static bool btf_resolve_valid(struct btf_verifier_env *env,
  1595. const struct btf_type *t,
  1596. u32 type_id)
  1597. {
  1598. struct btf *btf = env->btf;
  1599. if (!env_type_is_resolved(env, type_id))
  1600. return false;
  1601. if (btf_type_is_struct(t))
  1602. return !btf->resolved_ids[type_id] &&
  1603. !btf->resolved_sizes[type_id];
  1604. if (btf_type_is_modifier(t) || btf_type_is_ptr(t)) {
  1605. t = btf_type_id_resolve(btf, &type_id);
  1606. return t && !btf_type_is_modifier(t);
  1607. }
  1608. if (btf_type_is_array(t)) {
  1609. const struct btf_array *array = btf_type_array(t);
  1610. const struct btf_type *elem_type;
  1611. u32 elem_type_id = array->type;
  1612. u32 elem_size;
  1613. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  1614. return elem_type && !btf_type_is_modifier(elem_type) &&
  1615. (array->nelems * elem_size ==
  1616. btf->resolved_sizes[type_id]);
  1617. }
  1618. return false;
  1619. }
  1620. static int btf_check_all_types(struct btf_verifier_env *env)
  1621. {
  1622. struct btf *btf = env->btf;
  1623. u32 type_id;
  1624. int err;
  1625. err = env_resolve_init(env);
  1626. if (err)
  1627. return err;
  1628. env->phase++;
  1629. for (type_id = 1; type_id <= btf->nr_types; type_id++) {
  1630. const struct btf_type *t = btf_type_by_id(btf, type_id);
  1631. env->log_type_id = type_id;
  1632. if (btf_type_needs_resolve(t) &&
  1633. !env_type_is_resolved(env, type_id)) {
  1634. err = btf_resolve(env, t, type_id);
  1635. if (err)
  1636. return err;
  1637. }
  1638. if (btf_type_needs_resolve(t) &&
  1639. !btf_resolve_valid(env, t, type_id)) {
  1640. btf_verifier_log_type(env, t, "Invalid resolve state");
  1641. return -EINVAL;
  1642. }
  1643. }
  1644. return 0;
  1645. }
  1646. static int btf_parse_type_sec(struct btf_verifier_env *env)
  1647. {
  1648. const struct btf_header *hdr = &env->btf->hdr;
  1649. int err;
  1650. /* Type section must align to 4 bytes */
  1651. if (hdr->type_off & (sizeof(u32) - 1)) {
  1652. btf_verifier_log(env, "Unaligned type_off");
  1653. return -EINVAL;
  1654. }
  1655. if (!hdr->type_len) {
  1656. btf_verifier_log(env, "No type found");
  1657. return -EINVAL;
  1658. }
  1659. err = btf_check_all_metas(env);
  1660. if (err)
  1661. return err;
  1662. return btf_check_all_types(env);
  1663. }
  1664. static int btf_parse_str_sec(struct btf_verifier_env *env)
  1665. {
  1666. const struct btf_header *hdr;
  1667. struct btf *btf = env->btf;
  1668. const char *start, *end;
  1669. hdr = &btf->hdr;
  1670. start = btf->nohdr_data + hdr->str_off;
  1671. end = start + hdr->str_len;
  1672. if (end != btf->data + btf->data_size) {
  1673. btf_verifier_log(env, "String section is not at the end");
  1674. return -EINVAL;
  1675. }
  1676. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
  1677. start[0] || end[-1]) {
  1678. btf_verifier_log(env, "Invalid string section");
  1679. return -EINVAL;
  1680. }
  1681. btf->strings = start;
  1682. return 0;
  1683. }
  1684. static const size_t btf_sec_info_offset[] = {
  1685. offsetof(struct btf_header, type_off),
  1686. offsetof(struct btf_header, str_off),
  1687. };
  1688. static int btf_sec_info_cmp(const void *a, const void *b)
  1689. {
  1690. const struct btf_sec_info *x = a;
  1691. const struct btf_sec_info *y = b;
  1692. return (int)(x->off - y->off) ? : (int)(x->len - y->len);
  1693. }
  1694. static int btf_check_sec_info(struct btf_verifier_env *env,
  1695. u32 btf_data_size)
  1696. {
  1697. struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
  1698. u32 total, expected_total, i;
  1699. const struct btf_header *hdr;
  1700. const struct btf *btf;
  1701. btf = env->btf;
  1702. hdr = &btf->hdr;
  1703. /* Populate the secs from hdr */
  1704. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
  1705. secs[i] = *(struct btf_sec_info *)((void *)hdr +
  1706. btf_sec_info_offset[i]);
  1707. sort(secs, ARRAY_SIZE(btf_sec_info_offset),
  1708. sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
  1709. /* Check for gaps and overlap among sections */
  1710. total = 0;
  1711. expected_total = btf_data_size - hdr->hdr_len;
  1712. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
  1713. if (expected_total < secs[i].off) {
  1714. btf_verifier_log(env, "Invalid section offset");
  1715. return -EINVAL;
  1716. }
  1717. if (total < secs[i].off) {
  1718. /* gap */
  1719. btf_verifier_log(env, "Unsupported section found");
  1720. return -EINVAL;
  1721. }
  1722. if (total > secs[i].off) {
  1723. btf_verifier_log(env, "Section overlap found");
  1724. return -EINVAL;
  1725. }
  1726. if (expected_total - total < secs[i].len) {
  1727. btf_verifier_log(env,
  1728. "Total section length too long");
  1729. return -EINVAL;
  1730. }
  1731. total += secs[i].len;
  1732. }
  1733. /* There is data other than hdr and known sections */
  1734. if (expected_total != total) {
  1735. btf_verifier_log(env, "Unsupported section found");
  1736. return -EINVAL;
  1737. }
  1738. return 0;
  1739. }
  1740. static int btf_parse_hdr(struct btf_verifier_env *env, void __user *btf_data,
  1741. u32 btf_data_size)
  1742. {
  1743. const struct btf_header *hdr;
  1744. u32 hdr_len, hdr_copy;
  1745. /*
  1746. * Minimal part of the "struct btf_header" that
  1747. * contains the hdr_len.
  1748. */
  1749. struct btf_min_header {
  1750. u16 magic;
  1751. u8 version;
  1752. u8 flags;
  1753. u32 hdr_len;
  1754. } __user *min_hdr;
  1755. struct btf *btf;
  1756. int err;
  1757. btf = env->btf;
  1758. min_hdr = btf_data;
  1759. if (btf_data_size < sizeof(*min_hdr)) {
  1760. btf_verifier_log(env, "hdr_len not found");
  1761. return -EINVAL;
  1762. }
  1763. if (get_user(hdr_len, &min_hdr->hdr_len))
  1764. return -EFAULT;
  1765. if (btf_data_size < hdr_len) {
  1766. btf_verifier_log(env, "btf_header not found");
  1767. return -EINVAL;
  1768. }
  1769. err = bpf_check_uarg_tail_zero(btf_data, sizeof(btf->hdr), hdr_len);
  1770. if (err) {
  1771. if (err == -E2BIG)
  1772. btf_verifier_log(env, "Unsupported btf_header");
  1773. return err;
  1774. }
  1775. hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
  1776. if (copy_from_user(&btf->hdr, btf_data, hdr_copy))
  1777. return -EFAULT;
  1778. hdr = &btf->hdr;
  1779. btf_verifier_log_hdr(env, btf_data_size);
  1780. if (hdr->magic != BTF_MAGIC) {
  1781. btf_verifier_log(env, "Invalid magic");
  1782. return -EINVAL;
  1783. }
  1784. if (hdr->version != BTF_VERSION) {
  1785. btf_verifier_log(env, "Unsupported version");
  1786. return -ENOTSUPP;
  1787. }
  1788. if (hdr->flags) {
  1789. btf_verifier_log(env, "Unsupported flags");
  1790. return -ENOTSUPP;
  1791. }
  1792. if (btf_data_size == hdr->hdr_len) {
  1793. btf_verifier_log(env, "No data");
  1794. return -EINVAL;
  1795. }
  1796. err = btf_check_sec_info(env, btf_data_size);
  1797. if (err)
  1798. return err;
  1799. return 0;
  1800. }
  1801. static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
  1802. u32 log_level, char __user *log_ubuf, u32 log_size)
  1803. {
  1804. struct btf_verifier_env *env = NULL;
  1805. struct bpf_verifier_log *log;
  1806. struct btf *btf = NULL;
  1807. u8 *data;
  1808. int err;
  1809. if (btf_data_size > BTF_MAX_SIZE)
  1810. return ERR_PTR(-E2BIG);
  1811. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  1812. if (!env)
  1813. return ERR_PTR(-ENOMEM);
  1814. log = &env->log;
  1815. if (log_level || log_ubuf || log_size) {
  1816. /* user requested verbose verifier output
  1817. * and supplied buffer to store the verification trace
  1818. */
  1819. log->level = log_level;
  1820. log->ubuf = log_ubuf;
  1821. log->len_total = log_size;
  1822. /* log attributes have to be sane */
  1823. if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
  1824. !log->level || !log->ubuf) {
  1825. err = -EINVAL;
  1826. goto errout;
  1827. }
  1828. }
  1829. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  1830. if (!btf) {
  1831. err = -ENOMEM;
  1832. goto errout;
  1833. }
  1834. env->btf = btf;
  1835. err = btf_parse_hdr(env, btf_data, btf_data_size);
  1836. if (err)
  1837. goto errout;
  1838. data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
  1839. if (!data) {
  1840. err = -ENOMEM;
  1841. goto errout;
  1842. }
  1843. btf->data = data;
  1844. btf->data_size = btf_data_size;
  1845. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  1846. if (copy_from_user(data, btf_data, btf_data_size)) {
  1847. err = -EFAULT;
  1848. goto errout;
  1849. }
  1850. err = btf_parse_str_sec(env);
  1851. if (err)
  1852. goto errout;
  1853. err = btf_parse_type_sec(env);
  1854. if (err)
  1855. goto errout;
  1856. if (log->level && bpf_verifier_log_full(log)) {
  1857. err = -ENOSPC;
  1858. goto errout;
  1859. }
  1860. btf_verifier_env_free(env);
  1861. refcount_set(&btf->refcnt, 1);
  1862. return btf;
  1863. errout:
  1864. btf_verifier_env_free(env);
  1865. if (btf)
  1866. btf_free(btf);
  1867. return ERR_PTR(err);
  1868. }
  1869. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  1870. struct seq_file *m)
  1871. {
  1872. const struct btf_type *t = btf_type_by_id(btf, type_id);
  1873. btf_type_ops(t)->seq_show(btf, t, type_id, obj, 0, m);
  1874. }
  1875. static int btf_release(struct inode *inode, struct file *filp)
  1876. {
  1877. btf_put(filp->private_data);
  1878. return 0;
  1879. }
  1880. const struct file_operations btf_fops = {
  1881. .release = btf_release,
  1882. };
  1883. static int __btf_new_fd(struct btf *btf)
  1884. {
  1885. return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
  1886. }
  1887. int btf_new_fd(const union bpf_attr *attr)
  1888. {
  1889. struct btf *btf;
  1890. int ret;
  1891. btf = btf_parse(u64_to_user_ptr(attr->btf),
  1892. attr->btf_size, attr->btf_log_level,
  1893. u64_to_user_ptr(attr->btf_log_buf),
  1894. attr->btf_log_size);
  1895. if (IS_ERR(btf))
  1896. return PTR_ERR(btf);
  1897. ret = btf_alloc_id(btf);
  1898. if (ret) {
  1899. btf_free(btf);
  1900. return ret;
  1901. }
  1902. /*
  1903. * The BTF ID is published to the userspace.
  1904. * All BTF free must go through call_rcu() from
  1905. * now on (i.e. free by calling btf_put()).
  1906. */
  1907. ret = __btf_new_fd(btf);
  1908. if (ret < 0)
  1909. btf_put(btf);
  1910. return ret;
  1911. }
  1912. struct btf *btf_get_by_fd(int fd)
  1913. {
  1914. struct btf *btf;
  1915. struct fd f;
  1916. f = fdget(fd);
  1917. if (!f.file)
  1918. return ERR_PTR(-EBADF);
  1919. if (f.file->f_op != &btf_fops) {
  1920. fdput(f);
  1921. return ERR_PTR(-EINVAL);
  1922. }
  1923. btf = f.file->private_data;
  1924. refcount_inc(&btf->refcnt);
  1925. fdput(f);
  1926. return btf;
  1927. }
  1928. int btf_get_info_by_fd(const struct btf *btf,
  1929. const union bpf_attr *attr,
  1930. union bpf_attr __user *uattr)
  1931. {
  1932. struct bpf_btf_info __user *uinfo;
  1933. struct bpf_btf_info info = {};
  1934. u32 info_copy, btf_copy;
  1935. void __user *ubtf;
  1936. u32 uinfo_len;
  1937. uinfo = u64_to_user_ptr(attr->info.info);
  1938. uinfo_len = attr->info.info_len;
  1939. info_copy = min_t(u32, uinfo_len, sizeof(info));
  1940. if (copy_from_user(&info, uinfo, info_copy))
  1941. return -EFAULT;
  1942. info.id = btf->id;
  1943. ubtf = u64_to_user_ptr(info.btf);
  1944. btf_copy = min_t(u32, btf->data_size, info.btf_size);
  1945. if (copy_to_user(ubtf, btf->data, btf_copy))
  1946. return -EFAULT;
  1947. info.btf_size = btf->data_size;
  1948. if (copy_to_user(uinfo, &info, info_copy) ||
  1949. put_user(info_copy, &uattr->info.info_len))
  1950. return -EFAULT;
  1951. return 0;
  1952. }
  1953. int btf_get_fd_by_id(u32 id)
  1954. {
  1955. struct btf *btf;
  1956. int fd;
  1957. rcu_read_lock();
  1958. btf = idr_find(&btf_idr, id);
  1959. if (!btf || !refcount_inc_not_zero(&btf->refcnt))
  1960. btf = ERR_PTR(-ENOENT);
  1961. rcu_read_unlock();
  1962. if (IS_ERR(btf))
  1963. return PTR_ERR(btf);
  1964. fd = __btf_new_fd(btf);
  1965. if (fd < 0)
  1966. btf_put(btf);
  1967. return fd;
  1968. }
  1969. u32 btf_id(const struct btf *btf)
  1970. {
  1971. return btf->id;
  1972. }