btf.c 58 KB

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