btf.c 60 KB

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