features.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/filter.h>
  5. #include "bpf.h"
  6. #include "libbpf.h"
  7. #include "libbpf_common.h"
  8. #include "libbpf_internal.h"
  9. static inline __u64 ptr_to_u64(const void *ptr)
  10. {
  11. return (__u64)(unsigned long)ptr;
  12. }
  13. int probe_fd(int fd)
  14. {
  15. if (fd >= 0)
  16. close(fd);
  17. return fd >= 0;
  18. }
  19. static int probe_kern_prog_name(int token_fd)
  20. {
  21. const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
  22. struct bpf_insn insns[] = {
  23. BPF_MOV64_IMM(BPF_REG_0, 0),
  24. BPF_EXIT_INSN(),
  25. };
  26. union bpf_attr attr;
  27. int ret;
  28. memset(&attr, 0, attr_sz);
  29. attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  30. attr.license = ptr_to_u64("GPL");
  31. attr.insns = ptr_to_u64(insns);
  32. attr.insn_cnt = (__u32)ARRAY_SIZE(insns);
  33. attr.prog_token_fd = token_fd;
  34. if (token_fd)
  35. attr.prog_flags |= BPF_F_TOKEN_FD;
  36. libbpf_strlcpy(attr.prog_name, "libbpf_nametest", sizeof(attr.prog_name));
  37. /* make sure loading with name works */
  38. ret = sys_bpf_prog_load(&attr, attr_sz, PROG_LOAD_ATTEMPTS);
  39. return probe_fd(ret);
  40. }
  41. static int probe_kern_global_data(int token_fd)
  42. {
  43. struct bpf_insn insns[] = {
  44. BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
  45. BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
  46. BPF_MOV64_IMM(BPF_REG_0, 0),
  47. BPF_EXIT_INSN(),
  48. };
  49. LIBBPF_OPTS(bpf_map_create_opts, map_opts,
  50. .token_fd = token_fd,
  51. .map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  52. );
  53. LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
  54. .token_fd = token_fd,
  55. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  56. );
  57. int ret, map, insn_cnt = ARRAY_SIZE(insns);
  58. map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, &map_opts);
  59. if (map < 0) {
  60. ret = -errno;
  61. pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
  62. __func__, errstr(ret));
  63. return ret;
  64. }
  65. insns[0].imm = map;
  66. ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &prog_opts);
  67. close(map);
  68. return probe_fd(ret);
  69. }
  70. static int probe_kern_btf(int token_fd)
  71. {
  72. static const char strs[] = "\0int";
  73. __u32 types[] = {
  74. /* int */
  75. BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
  76. };
  77. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  78. strs, sizeof(strs), token_fd));
  79. }
  80. static int probe_kern_btf_func(int token_fd)
  81. {
  82. static const char strs[] = "\0int\0x\0a";
  83. /* void x(int a) {} */
  84. __u32 types[] = {
  85. /* int */
  86. BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  87. /* FUNC_PROTO */ /* [2] */
  88. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
  89. BTF_PARAM_ENC(7, 1),
  90. /* FUNC x */ /* [3] */
  91. BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
  92. };
  93. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  94. strs, sizeof(strs), token_fd));
  95. }
  96. static int probe_kern_btf_func_global(int token_fd)
  97. {
  98. static const char strs[] = "\0int\0x\0a";
  99. /* static void x(int a) {} */
  100. __u32 types[] = {
  101. /* int */
  102. BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  103. /* FUNC_PROTO */ /* [2] */
  104. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
  105. BTF_PARAM_ENC(7, 1),
  106. /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
  107. BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
  108. };
  109. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  110. strs, sizeof(strs), token_fd));
  111. }
  112. static int probe_kern_btf_datasec(int token_fd)
  113. {
  114. static const char strs[] = "\0x\0.data";
  115. /* static int a; */
  116. __u32 types[] = {
  117. /* int */
  118. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  119. /* VAR x */ /* [2] */
  120. BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
  121. BTF_VAR_STATIC,
  122. /* DATASEC val */ /* [3] */
  123. BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
  124. BTF_VAR_SECINFO_ENC(2, 0, 4),
  125. };
  126. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  127. strs, sizeof(strs), token_fd));
  128. }
  129. static int probe_kern_btf_qmark_datasec(int token_fd)
  130. {
  131. static const char strs[] = "\0x\0?.data";
  132. /* static int a; */
  133. __u32 types[] = {
  134. /* int */
  135. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  136. /* VAR x */ /* [2] */
  137. BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
  138. BTF_VAR_STATIC,
  139. /* DATASEC ?.data */ /* [3] */
  140. BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
  141. BTF_VAR_SECINFO_ENC(2, 0, 4),
  142. };
  143. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  144. strs, sizeof(strs), token_fd));
  145. }
  146. static int probe_kern_btf_float(int token_fd)
  147. {
  148. static const char strs[] = "\0float";
  149. __u32 types[] = {
  150. /* float */
  151. BTF_TYPE_FLOAT_ENC(1, 4),
  152. };
  153. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  154. strs, sizeof(strs), token_fd));
  155. }
  156. static int probe_kern_btf_decl_tag(int token_fd)
  157. {
  158. static const char strs[] = "\0tag";
  159. __u32 types[] = {
  160. /* int */
  161. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  162. /* VAR x */ /* [2] */
  163. BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
  164. BTF_VAR_STATIC,
  165. /* attr */
  166. BTF_TYPE_DECL_TAG_ENC(1, 2, -1),
  167. };
  168. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  169. strs, sizeof(strs), token_fd));
  170. }
  171. static int probe_kern_btf_type_tag(int token_fd)
  172. {
  173. static const char strs[] = "\0tag";
  174. __u32 types[] = {
  175. /* int */
  176. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  177. /* attr */
  178. BTF_TYPE_TYPE_TAG_ENC(1, 1), /* [2] */
  179. /* ptr */
  180. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2), /* [3] */
  181. };
  182. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  183. strs, sizeof(strs), token_fd));
  184. }
  185. static int probe_kern_array_mmap(int token_fd)
  186. {
  187. LIBBPF_OPTS(bpf_map_create_opts, opts,
  188. .map_flags = BPF_F_MMAPABLE | (token_fd ? BPF_F_TOKEN_FD : 0),
  189. .token_fd = token_fd,
  190. );
  191. int fd;
  192. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
  193. return probe_fd(fd);
  194. }
  195. static int probe_kern_exp_attach_type(int token_fd)
  196. {
  197. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  198. .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  199. .token_fd = token_fd,
  200. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  201. );
  202. struct bpf_insn insns[] = {
  203. BPF_MOV64_IMM(BPF_REG_0, 0),
  204. BPF_EXIT_INSN(),
  205. };
  206. int fd, insn_cnt = ARRAY_SIZE(insns);
  207. /* use any valid combination of program type and (optional)
  208. * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
  209. * to see if kernel supports expected_attach_type field for
  210. * BPF_PROG_LOAD command
  211. */
  212. fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns, insn_cnt, &opts);
  213. return probe_fd(fd);
  214. }
  215. static int probe_kern_probe_read_kernel(int token_fd)
  216. {
  217. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  218. .token_fd = token_fd,
  219. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  220. );
  221. struct bpf_insn insns[] = {
  222. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
  223. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
  224. BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */
  225. BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */
  226. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
  227. BPF_EXIT_INSN(),
  228. };
  229. int fd, insn_cnt = ARRAY_SIZE(insns);
  230. fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
  231. return probe_fd(fd);
  232. }
  233. static int probe_prog_bind_map(int token_fd)
  234. {
  235. struct bpf_insn insns[] = {
  236. BPF_MOV64_IMM(BPF_REG_0, 0),
  237. BPF_EXIT_INSN(),
  238. };
  239. LIBBPF_OPTS(bpf_map_create_opts, map_opts,
  240. .token_fd = token_fd,
  241. .map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  242. );
  243. LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
  244. .token_fd = token_fd,
  245. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  246. );
  247. int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
  248. map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, &map_opts);
  249. if (map < 0) {
  250. ret = -errno;
  251. pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
  252. __func__, errstr(ret));
  253. return ret;
  254. }
  255. prog = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &prog_opts);
  256. if (prog < 0) {
  257. close(map);
  258. return 0;
  259. }
  260. ret = bpf_prog_bind_map(prog, map, NULL);
  261. close(map);
  262. close(prog);
  263. return ret >= 0;
  264. }
  265. static int probe_module_btf(int token_fd)
  266. {
  267. static const char strs[] = "\0int";
  268. __u32 types[] = {
  269. /* int */
  270. BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
  271. };
  272. struct bpf_btf_info info;
  273. __u32 len = sizeof(info);
  274. char name[16];
  275. int fd, err;
  276. fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs), token_fd);
  277. if (fd < 0)
  278. return 0; /* BTF not supported at all */
  279. memset(&info, 0, sizeof(info));
  280. info.name = ptr_to_u64(name);
  281. info.name_len = sizeof(name);
  282. /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
  283. * kernel's module BTF support coincides with support for
  284. * name/name_len fields in struct bpf_btf_info.
  285. */
  286. err = bpf_btf_get_info_by_fd(fd, &info, &len);
  287. close(fd);
  288. return !err;
  289. }
  290. static int probe_perf_link(int token_fd)
  291. {
  292. struct bpf_insn insns[] = {
  293. BPF_MOV64_IMM(BPF_REG_0, 0),
  294. BPF_EXIT_INSN(),
  295. };
  296. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  297. .token_fd = token_fd,
  298. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  299. );
  300. int prog_fd, link_fd, err;
  301. prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL",
  302. insns, ARRAY_SIZE(insns), &opts);
  303. if (prog_fd < 0)
  304. return -errno;
  305. /* use invalid perf_event FD to get EBADF, if link is supported;
  306. * otherwise EINVAL should be returned
  307. */
  308. link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL);
  309. err = -errno; /* close() can clobber errno */
  310. if (link_fd >= 0)
  311. close(link_fd);
  312. close(prog_fd);
  313. return link_fd < 0 && err == -EBADF;
  314. }
  315. static int probe_uprobe_multi_link(int token_fd)
  316. {
  317. LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
  318. .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
  319. .token_fd = token_fd,
  320. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  321. );
  322. LIBBPF_OPTS(bpf_link_create_opts, link_opts);
  323. struct bpf_insn insns[] = {
  324. BPF_MOV64_IMM(BPF_REG_0, 0),
  325. BPF_EXIT_INSN(),
  326. };
  327. int prog_fd, link_fd, err;
  328. unsigned long offset = 0;
  329. prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
  330. insns, ARRAY_SIZE(insns), &load_opts);
  331. if (prog_fd < 0)
  332. return -errno;
  333. /* Creating uprobe in '/' binary should fail with -EBADF. */
  334. link_opts.uprobe_multi.path = "/";
  335. link_opts.uprobe_multi.offsets = &offset;
  336. link_opts.uprobe_multi.cnt = 1;
  337. link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts);
  338. err = -errno; /* close() can clobber errno */
  339. if (link_fd >= 0 || err != -EBADF) {
  340. if (link_fd >= 0)
  341. close(link_fd);
  342. close(prog_fd);
  343. return 0;
  344. }
  345. /* Initial multi-uprobe support in kernel didn't handle PID filtering
  346. * correctly (it was doing thread filtering, not process filtering).
  347. * So now we'll detect if PID filtering logic was fixed, and, if not,
  348. * we'll pretend multi-uprobes are not supported, if not.
  349. * Multi-uprobes are used in USDT attachment logic, and we need to be
  350. * conservative here, because multi-uprobe selection happens early at
  351. * load time, while the use of PID filtering is known late at
  352. * attachment time, at which point it's too late to undo multi-uprobe
  353. * selection.
  354. *
  355. * Creating uprobe with pid == -1 for (invalid) '/' binary will fail
  356. * early with -EINVAL on kernels with fixed PID filtering logic;
  357. * otherwise -ESRCH would be returned if passed correct binary path
  358. * (but we'll just get -BADF, of course).
  359. */
  360. link_opts.uprobe_multi.pid = -1; /* invalid PID */
  361. link_opts.uprobe_multi.path = "/"; /* invalid path */
  362. link_opts.uprobe_multi.offsets = &offset;
  363. link_opts.uprobe_multi.cnt = 1;
  364. link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts);
  365. err = -errno; /* close() can clobber errno */
  366. if (link_fd >= 0)
  367. close(link_fd);
  368. close(prog_fd);
  369. return link_fd < 0 && err == -EINVAL;
  370. }
  371. static int probe_kern_bpf_cookie(int token_fd)
  372. {
  373. struct bpf_insn insns[] = {
  374. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_attach_cookie),
  375. BPF_EXIT_INSN(),
  376. };
  377. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  378. .token_fd = token_fd,
  379. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  380. );
  381. int ret, insn_cnt = ARRAY_SIZE(insns);
  382. ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
  383. return probe_fd(ret);
  384. }
  385. static int probe_kern_btf_enum64(int token_fd)
  386. {
  387. static const char strs[] = "\0enum64";
  388. __u32 types[] = {
  389. BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
  390. };
  391. return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
  392. strs, sizeof(strs), token_fd));
  393. }
  394. static int probe_kern_arg_ctx_tag(int token_fd)
  395. {
  396. static const char strs[] = "\0a\0b\0arg:ctx\0";
  397. const __u32 types[] = {
  398. /* [1] INT */
  399. BTF_TYPE_INT_ENC(1 /* "a" */, BTF_INT_SIGNED, 0, 32, 4),
  400. /* [2] PTR -> VOID */
  401. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
  402. /* [3] FUNC_PROTO `int(void *a)` */
  403. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
  404. BTF_PARAM_ENC(1 /* "a" */, 2),
  405. /* [4] FUNC 'a' -> FUNC_PROTO (main prog) */
  406. BTF_TYPE_ENC(1 /* "a" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),
  407. /* [5] FUNC_PROTO `int(void *b __arg_ctx)` */
  408. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
  409. BTF_PARAM_ENC(3 /* "b" */, 2),
  410. /* [6] FUNC 'b' -> FUNC_PROTO (subprog) */
  411. BTF_TYPE_ENC(3 /* "b" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 5),
  412. /* [7] DECL_TAG 'arg:ctx' -> func 'b' arg 'b' */
  413. BTF_TYPE_DECL_TAG_ENC(5 /* "arg:ctx" */, 6, 0),
  414. };
  415. const struct bpf_insn insns[] = {
  416. /* main prog */
  417. BPF_CALL_REL(+1),
  418. BPF_EXIT_INSN(),
  419. /* global subprog */
  420. BPF_EMIT_CALL(BPF_FUNC_get_func_ip), /* needs PTR_TO_CTX */
  421. BPF_EXIT_INSN(),
  422. };
  423. const struct bpf_func_info_min func_infos[] = {
  424. { 0, 4 }, /* main prog -> FUNC 'a' */
  425. { 2, 6 }, /* subprog -> FUNC 'b' */
  426. };
  427. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  428. .token_fd = token_fd,
  429. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  430. );
  431. int prog_fd, btf_fd, insn_cnt = ARRAY_SIZE(insns);
  432. btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs), token_fd);
  433. if (btf_fd < 0)
  434. return 0;
  435. opts.prog_btf_fd = btf_fd;
  436. opts.func_info = &func_infos;
  437. opts.func_info_cnt = ARRAY_SIZE(func_infos);
  438. opts.func_info_rec_size = sizeof(func_infos[0]);
  439. prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, "det_arg_ctx",
  440. "GPL", insns, insn_cnt, &opts);
  441. close(btf_fd);
  442. return probe_fd(prog_fd);
  443. }
  444. static int probe_ldimm64_full_range_off(int token_fd)
  445. {
  446. char log_buf[1024];
  447. int prog_fd, map_fd;
  448. int ret;
  449. LIBBPF_OPTS(bpf_map_create_opts, map_opts,
  450. .token_fd = token_fd,
  451. .map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  452. );
  453. LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
  454. .token_fd = token_fd,
  455. .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
  456. .log_buf = log_buf,
  457. .log_size = sizeof(log_buf),
  458. );
  459. struct bpf_insn insns[] = {
  460. BPF_LD_MAP_VALUE(BPF_REG_1, 0, 1UL << 30),
  461. BPF_EXIT_INSN(),
  462. };
  463. int insn_cnt = ARRAY_SIZE(insns);
  464. map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr", sizeof(int), 1, 1, &map_opts);
  465. if (map_fd < 0) {
  466. ret = -errno;
  467. pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
  468. __func__, errstr(ret));
  469. return ret;
  470. }
  471. insns[0].imm = map_fd;
  472. log_buf[0] = '\0';
  473. prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "global_reloc", "GPL", insns, insn_cnt, &prog_opts);
  474. ret = -errno;
  475. close(map_fd);
  476. if (prog_fd >= 0) {
  477. pr_warn("Error in %s(): Program loading unexpectedly succeeded.\n", __func__);
  478. close(prog_fd);
  479. return -EINVAL;
  480. }
  481. /*
  482. * Feature is allowed if we're not failing with the error message
  483. * "direct value offset of %u is not allowed" removed in
  484. * 12a1fe6e12db ("bpf/verifier: Do not limit maximum direct offset into arena map").
  485. * We should instead fail with "invalid access to map value pointer".
  486. * Ensure we match with one of the two and we're not failing with a
  487. * different, unexpected message.
  488. */
  489. if (strstr(log_buf, "direct value offset of"))
  490. return 0;
  491. if (!strstr(log_buf, "invalid access to map value pointer")) {
  492. pr_warn("Error in %s(): Program unexpectedly failed with message: %s.\n",
  493. __func__, log_buf);
  494. return ret;
  495. }
  496. return 1;
  497. }
  498. typedef int (*feature_probe_fn)(int /* token_fd */);
  499. static struct kern_feature_cache feature_cache;
  500. static struct kern_feature_desc {
  501. const char *desc;
  502. feature_probe_fn probe;
  503. } feature_probes[__FEAT_CNT] = {
  504. [FEAT_PROG_NAME] = {
  505. "BPF program name", probe_kern_prog_name,
  506. },
  507. [FEAT_GLOBAL_DATA] = {
  508. "global variables", probe_kern_global_data,
  509. },
  510. [FEAT_BTF] = {
  511. "minimal BTF", probe_kern_btf,
  512. },
  513. [FEAT_BTF_FUNC] = {
  514. "BTF functions", probe_kern_btf_func,
  515. },
  516. [FEAT_BTF_GLOBAL_FUNC] = {
  517. "BTF global function", probe_kern_btf_func_global,
  518. },
  519. [FEAT_BTF_DATASEC] = {
  520. "BTF data section and variable", probe_kern_btf_datasec,
  521. },
  522. [FEAT_ARRAY_MMAP] = {
  523. "ARRAY map mmap()", probe_kern_array_mmap,
  524. },
  525. [FEAT_EXP_ATTACH_TYPE] = {
  526. "BPF_PROG_LOAD expected_attach_type attribute",
  527. probe_kern_exp_attach_type,
  528. },
  529. [FEAT_PROBE_READ_KERN] = {
  530. "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
  531. },
  532. [FEAT_PROG_BIND_MAP] = {
  533. "BPF_PROG_BIND_MAP support", probe_prog_bind_map,
  534. },
  535. [FEAT_MODULE_BTF] = {
  536. "module BTF support", probe_module_btf,
  537. },
  538. [FEAT_BTF_FLOAT] = {
  539. "BTF_KIND_FLOAT support", probe_kern_btf_float,
  540. },
  541. [FEAT_PERF_LINK] = {
  542. "BPF perf link support", probe_perf_link,
  543. },
  544. [FEAT_BTF_DECL_TAG] = {
  545. "BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag,
  546. },
  547. [FEAT_BTF_TYPE_TAG] = {
  548. "BTF_KIND_TYPE_TAG support", probe_kern_btf_type_tag,
  549. },
  550. [FEAT_MEMCG_ACCOUNT] = {
  551. "memcg-based memory accounting", probe_memcg_account,
  552. },
  553. [FEAT_BPF_COOKIE] = {
  554. "BPF cookie support", probe_kern_bpf_cookie,
  555. },
  556. [FEAT_BTF_ENUM64] = {
  557. "BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
  558. },
  559. [FEAT_SYSCALL_WRAPPER] = {
  560. "Kernel using syscall wrapper", probe_kern_syscall_wrapper,
  561. },
  562. [FEAT_UPROBE_MULTI_LINK] = {
  563. "BPF multi-uprobe link support", probe_uprobe_multi_link,
  564. },
  565. [FEAT_ARG_CTX_TAG] = {
  566. "kernel-side __arg_ctx tag", probe_kern_arg_ctx_tag,
  567. },
  568. [FEAT_BTF_QMARK_DATASEC] = {
  569. "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
  570. },
  571. [FEAT_LDIMM64_FULL_RANGE_OFF] = {
  572. "full range LDIMM64 support", probe_ldimm64_full_range_off,
  573. },
  574. };
  575. bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id)
  576. {
  577. struct kern_feature_desc *feat = &feature_probes[feat_id];
  578. int ret;
  579. /* assume global feature cache, unless custom one is provided */
  580. if (!cache)
  581. cache = &feature_cache;
  582. if (READ_ONCE(cache->res[feat_id]) == FEAT_UNKNOWN) {
  583. ret = feat->probe(cache->token_fd);
  584. if (ret > 0) {
  585. WRITE_ONCE(cache->res[feat_id], FEAT_SUPPORTED);
  586. } else if (ret == 0) {
  587. WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
  588. } else {
  589. pr_warn("Detection of kernel %s support failed: %s\n",
  590. feat->desc, errstr(ret));
  591. WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
  592. }
  593. }
  594. return READ_ONCE(cache->res[feat_id]) == FEAT_SUPPORTED;
  595. }