helpers.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  3. */
  4. #include <linux/bpf.h>
  5. #include <linux/btf.h>
  6. #include <linux/bpf-cgroup.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/random.h>
  9. #include <linux/smp.h>
  10. #include <linux/topology.h>
  11. #include <linux/ktime.h>
  12. #include <linux/sched.h>
  13. #include <linux/uidgid.h>
  14. #include <linux/filter.h>
  15. #include <linux/ctype.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/pid_namespace.h>
  18. #include <linux/poison.h>
  19. #include <linux/proc_ns.h>
  20. #include <linux/security.h>
  21. #include <linux/btf_ids.h>
  22. #include "../../lib/kstrtox.h"
  23. /* If kernel subsystem is allowing eBPF programs to call this function,
  24. * inside its own verifier_ops->get_func_proto() callback it should return
  25. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  26. *
  27. * Different map implementations will rely on rcu in map methods
  28. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  29. * if program is allowed to access maps, so check rcu_read_lock_held in
  30. * all three functions.
  31. */
  32. BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
  33. {
  34. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  35. return (unsigned long) map->ops->map_lookup_elem(map, key);
  36. }
  37. const struct bpf_func_proto bpf_map_lookup_elem_proto = {
  38. .func = bpf_map_lookup_elem,
  39. .gpl_only = false,
  40. .pkt_access = true,
  41. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  42. .arg1_type = ARG_CONST_MAP_PTR,
  43. .arg2_type = ARG_PTR_TO_MAP_KEY,
  44. };
  45. BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
  46. void *, value, u64, flags)
  47. {
  48. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  49. return map->ops->map_update_elem(map, key, value, flags);
  50. }
  51. const struct bpf_func_proto bpf_map_update_elem_proto = {
  52. .func = bpf_map_update_elem,
  53. .gpl_only = false,
  54. .pkt_access = true,
  55. .ret_type = RET_INTEGER,
  56. .arg1_type = ARG_CONST_MAP_PTR,
  57. .arg2_type = ARG_PTR_TO_MAP_KEY,
  58. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  59. .arg4_type = ARG_ANYTHING,
  60. };
  61. BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
  62. {
  63. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  64. return map->ops->map_delete_elem(map, key);
  65. }
  66. const struct bpf_func_proto bpf_map_delete_elem_proto = {
  67. .func = bpf_map_delete_elem,
  68. .gpl_only = false,
  69. .pkt_access = true,
  70. .ret_type = RET_INTEGER,
  71. .arg1_type = ARG_CONST_MAP_PTR,
  72. .arg2_type = ARG_PTR_TO_MAP_KEY,
  73. };
  74. BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
  75. {
  76. return map->ops->map_push_elem(map, value, flags);
  77. }
  78. const struct bpf_func_proto bpf_map_push_elem_proto = {
  79. .func = bpf_map_push_elem,
  80. .gpl_only = false,
  81. .pkt_access = true,
  82. .ret_type = RET_INTEGER,
  83. .arg1_type = ARG_CONST_MAP_PTR,
  84. .arg2_type = ARG_PTR_TO_MAP_VALUE,
  85. .arg3_type = ARG_ANYTHING,
  86. };
  87. BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
  88. {
  89. return map->ops->map_pop_elem(map, value);
  90. }
  91. const struct bpf_func_proto bpf_map_pop_elem_proto = {
  92. .func = bpf_map_pop_elem,
  93. .gpl_only = false,
  94. .ret_type = RET_INTEGER,
  95. .arg1_type = ARG_CONST_MAP_PTR,
  96. .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
  97. };
  98. BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
  99. {
  100. return map->ops->map_peek_elem(map, value);
  101. }
  102. const struct bpf_func_proto bpf_map_peek_elem_proto = {
  103. .func = bpf_map_peek_elem,
  104. .gpl_only = false,
  105. .ret_type = RET_INTEGER,
  106. .arg1_type = ARG_CONST_MAP_PTR,
  107. .arg2_type = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
  108. };
  109. BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu)
  110. {
  111. WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  112. return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu);
  113. }
  114. const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto = {
  115. .func = bpf_map_lookup_percpu_elem,
  116. .gpl_only = false,
  117. .pkt_access = true,
  118. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  119. .arg1_type = ARG_CONST_MAP_PTR,
  120. .arg2_type = ARG_PTR_TO_MAP_KEY,
  121. .arg3_type = ARG_ANYTHING,
  122. };
  123. const struct bpf_func_proto bpf_get_prandom_u32_proto = {
  124. .func = bpf_user_rnd_u32,
  125. .gpl_only = false,
  126. .ret_type = RET_INTEGER,
  127. };
  128. BPF_CALL_0(bpf_get_smp_processor_id)
  129. {
  130. return smp_processor_id();
  131. }
  132. const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
  133. .func = bpf_get_smp_processor_id,
  134. .gpl_only = false,
  135. .ret_type = RET_INTEGER,
  136. };
  137. BPF_CALL_0(bpf_get_numa_node_id)
  138. {
  139. return numa_node_id();
  140. }
  141. const struct bpf_func_proto bpf_get_numa_node_id_proto = {
  142. .func = bpf_get_numa_node_id,
  143. .gpl_only = false,
  144. .ret_type = RET_INTEGER,
  145. };
  146. BPF_CALL_0(bpf_ktime_get_ns)
  147. {
  148. /* NMI safe access to clock monotonic */
  149. return ktime_get_mono_fast_ns();
  150. }
  151. const struct bpf_func_proto bpf_ktime_get_ns_proto = {
  152. .func = bpf_ktime_get_ns,
  153. .gpl_only = false,
  154. .ret_type = RET_INTEGER,
  155. };
  156. BPF_CALL_0(bpf_ktime_get_boot_ns)
  157. {
  158. /* NMI safe access to clock boottime */
  159. return ktime_get_boot_fast_ns();
  160. }
  161. const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
  162. .func = bpf_ktime_get_boot_ns,
  163. .gpl_only = false,
  164. .ret_type = RET_INTEGER,
  165. };
  166. BPF_CALL_0(bpf_ktime_get_coarse_ns)
  167. {
  168. return ktime_get_coarse_ns();
  169. }
  170. const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
  171. .func = bpf_ktime_get_coarse_ns,
  172. .gpl_only = false,
  173. .ret_type = RET_INTEGER,
  174. };
  175. BPF_CALL_0(bpf_ktime_get_tai_ns)
  176. {
  177. /* NMI safe access to clock tai */
  178. return ktime_get_tai_fast_ns();
  179. }
  180. const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = {
  181. .func = bpf_ktime_get_tai_ns,
  182. .gpl_only = false,
  183. .ret_type = RET_INTEGER,
  184. };
  185. BPF_CALL_0(bpf_get_current_pid_tgid)
  186. {
  187. struct task_struct *task = current;
  188. if (unlikely(!task))
  189. return -EINVAL;
  190. return (u64) task->tgid << 32 | task->pid;
  191. }
  192. const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
  193. .func = bpf_get_current_pid_tgid,
  194. .gpl_only = false,
  195. .ret_type = RET_INTEGER,
  196. };
  197. BPF_CALL_0(bpf_get_current_uid_gid)
  198. {
  199. struct task_struct *task = current;
  200. kuid_t uid;
  201. kgid_t gid;
  202. if (unlikely(!task))
  203. return -EINVAL;
  204. current_uid_gid(&uid, &gid);
  205. return (u64) from_kgid(&init_user_ns, gid) << 32 |
  206. from_kuid(&init_user_ns, uid);
  207. }
  208. const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
  209. .func = bpf_get_current_uid_gid,
  210. .gpl_only = false,
  211. .ret_type = RET_INTEGER,
  212. };
  213. BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
  214. {
  215. struct task_struct *task = current;
  216. if (unlikely(!task))
  217. goto err_clear;
  218. /* Verifier guarantees that size > 0 */
  219. strscpy(buf, task->comm, size);
  220. return 0;
  221. err_clear:
  222. memset(buf, 0, size);
  223. return -EINVAL;
  224. }
  225. const struct bpf_func_proto bpf_get_current_comm_proto = {
  226. .func = bpf_get_current_comm,
  227. .gpl_only = false,
  228. .ret_type = RET_INTEGER,
  229. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  230. .arg2_type = ARG_CONST_SIZE,
  231. };
  232. #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
  233. static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
  234. {
  235. arch_spinlock_t *l = (void *)lock;
  236. union {
  237. __u32 val;
  238. arch_spinlock_t lock;
  239. } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
  240. compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
  241. BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
  242. BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
  243. arch_spin_lock(l);
  244. }
  245. static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
  246. {
  247. arch_spinlock_t *l = (void *)lock;
  248. arch_spin_unlock(l);
  249. }
  250. #else
  251. static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
  252. {
  253. atomic_t *l = (void *)lock;
  254. BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
  255. do {
  256. atomic_cond_read_relaxed(l, !VAL);
  257. } while (atomic_xchg(l, 1));
  258. }
  259. static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
  260. {
  261. atomic_t *l = (void *)lock;
  262. atomic_set_release(l, 0);
  263. }
  264. #endif
  265. static DEFINE_PER_CPU(unsigned long, irqsave_flags);
  266. static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock)
  267. {
  268. unsigned long flags;
  269. local_irq_save(flags);
  270. __bpf_spin_lock(lock);
  271. __this_cpu_write(irqsave_flags, flags);
  272. }
  273. notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
  274. {
  275. __bpf_spin_lock_irqsave(lock);
  276. return 0;
  277. }
  278. const struct bpf_func_proto bpf_spin_lock_proto = {
  279. .func = bpf_spin_lock,
  280. .gpl_only = false,
  281. .ret_type = RET_VOID,
  282. .arg1_type = ARG_PTR_TO_SPIN_LOCK,
  283. };
  284. static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock)
  285. {
  286. unsigned long flags;
  287. flags = __this_cpu_read(irqsave_flags);
  288. __bpf_spin_unlock(lock);
  289. local_irq_restore(flags);
  290. }
  291. notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
  292. {
  293. __bpf_spin_unlock_irqrestore(lock);
  294. return 0;
  295. }
  296. const struct bpf_func_proto bpf_spin_unlock_proto = {
  297. .func = bpf_spin_unlock,
  298. .gpl_only = false,
  299. .ret_type = RET_VOID,
  300. .arg1_type = ARG_PTR_TO_SPIN_LOCK,
  301. };
  302. void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
  303. bool lock_src)
  304. {
  305. struct bpf_spin_lock *lock;
  306. if (lock_src)
  307. lock = src + map->spin_lock_off;
  308. else
  309. lock = dst + map->spin_lock_off;
  310. preempt_disable();
  311. __bpf_spin_lock_irqsave(lock);
  312. copy_map_value(map, dst, src);
  313. __bpf_spin_unlock_irqrestore(lock);
  314. preempt_enable();
  315. }
  316. BPF_CALL_0(bpf_jiffies64)
  317. {
  318. return get_jiffies_64();
  319. }
  320. const struct bpf_func_proto bpf_jiffies64_proto = {
  321. .func = bpf_jiffies64,
  322. .gpl_only = false,
  323. .ret_type = RET_INTEGER,
  324. };
  325. #ifdef CONFIG_CGROUPS
  326. BPF_CALL_0(bpf_get_current_cgroup_id)
  327. {
  328. struct cgroup *cgrp;
  329. u64 cgrp_id;
  330. rcu_read_lock();
  331. cgrp = task_dfl_cgroup(current);
  332. cgrp_id = cgroup_id(cgrp);
  333. rcu_read_unlock();
  334. return cgrp_id;
  335. }
  336. const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
  337. .func = bpf_get_current_cgroup_id,
  338. .gpl_only = false,
  339. .ret_type = RET_INTEGER,
  340. };
  341. BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level)
  342. {
  343. struct cgroup *cgrp;
  344. struct cgroup *ancestor;
  345. u64 cgrp_id;
  346. rcu_read_lock();
  347. cgrp = task_dfl_cgroup(current);
  348. ancestor = cgroup_ancestor(cgrp, ancestor_level);
  349. cgrp_id = ancestor ? cgroup_id(ancestor) : 0;
  350. rcu_read_unlock();
  351. return cgrp_id;
  352. }
  353. const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = {
  354. .func = bpf_get_current_ancestor_cgroup_id,
  355. .gpl_only = false,
  356. .ret_type = RET_INTEGER,
  357. .arg1_type = ARG_ANYTHING,
  358. };
  359. #endif /* CONFIG_CGROUPS */
  360. #define BPF_STRTOX_BASE_MASK 0x1F
  361. static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
  362. unsigned long long *res, bool *is_negative)
  363. {
  364. unsigned int base = flags & BPF_STRTOX_BASE_MASK;
  365. const char *cur_buf = buf;
  366. size_t cur_len = buf_len;
  367. unsigned int consumed;
  368. size_t val_len;
  369. char str[64];
  370. if (!buf || !buf_len || !res || !is_negative)
  371. return -EINVAL;
  372. if (base != 0 && base != 8 && base != 10 && base != 16)
  373. return -EINVAL;
  374. if (flags & ~BPF_STRTOX_BASE_MASK)
  375. return -EINVAL;
  376. while (cur_buf < buf + buf_len && isspace(*cur_buf))
  377. ++cur_buf;
  378. *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
  379. if (*is_negative)
  380. ++cur_buf;
  381. consumed = cur_buf - buf;
  382. cur_len -= consumed;
  383. if (!cur_len)
  384. return -EINVAL;
  385. cur_len = min(cur_len, sizeof(str) - 1);
  386. memcpy(str, cur_buf, cur_len);
  387. str[cur_len] = '\0';
  388. cur_buf = str;
  389. cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
  390. val_len = _parse_integer(cur_buf, base, res);
  391. if (val_len & KSTRTOX_OVERFLOW)
  392. return -ERANGE;
  393. if (val_len == 0)
  394. return -EINVAL;
  395. cur_buf += val_len;
  396. consumed += cur_buf - str;
  397. return consumed;
  398. }
  399. static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
  400. long long *res)
  401. {
  402. unsigned long long _res;
  403. bool is_negative;
  404. int err;
  405. err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
  406. if (err < 0)
  407. return err;
  408. if (is_negative) {
  409. if ((long long)-_res > 0)
  410. return -ERANGE;
  411. *res = -_res;
  412. } else {
  413. if ((long long)_res < 0)
  414. return -ERANGE;
  415. *res = _res;
  416. }
  417. return err;
  418. }
  419. BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
  420. long *, res)
  421. {
  422. long long _res;
  423. int err;
  424. err = __bpf_strtoll(buf, buf_len, flags, &_res);
  425. if (err < 0)
  426. return err;
  427. if (_res != (long)_res)
  428. return -ERANGE;
  429. *res = _res;
  430. return err;
  431. }
  432. const struct bpf_func_proto bpf_strtol_proto = {
  433. .func = bpf_strtol,
  434. .gpl_only = false,
  435. .ret_type = RET_INTEGER,
  436. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  437. .arg2_type = ARG_CONST_SIZE,
  438. .arg3_type = ARG_ANYTHING,
  439. .arg4_type = ARG_PTR_TO_LONG,
  440. };
  441. BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
  442. unsigned long *, res)
  443. {
  444. unsigned long long _res;
  445. bool is_negative;
  446. int err;
  447. err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
  448. if (err < 0)
  449. return err;
  450. if (is_negative)
  451. return -EINVAL;
  452. if (_res != (unsigned long)_res)
  453. return -ERANGE;
  454. *res = _res;
  455. return err;
  456. }
  457. const struct bpf_func_proto bpf_strtoul_proto = {
  458. .func = bpf_strtoul,
  459. .gpl_only = false,
  460. .ret_type = RET_INTEGER,
  461. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  462. .arg2_type = ARG_CONST_SIZE,
  463. .arg3_type = ARG_ANYTHING,
  464. .arg4_type = ARG_PTR_TO_LONG,
  465. };
  466. BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
  467. {
  468. return strncmp(s1, s2, s1_sz);
  469. }
  470. static const struct bpf_func_proto bpf_strncmp_proto = {
  471. .func = bpf_strncmp,
  472. .gpl_only = false,
  473. .ret_type = RET_INTEGER,
  474. .arg1_type = ARG_PTR_TO_MEM,
  475. .arg2_type = ARG_CONST_SIZE,
  476. .arg3_type = ARG_PTR_TO_CONST_STR,
  477. };
  478. BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
  479. struct bpf_pidns_info *, nsdata, u32, size)
  480. {
  481. struct task_struct *task = current;
  482. struct pid_namespace *pidns;
  483. int err = -EINVAL;
  484. if (unlikely(size != sizeof(struct bpf_pidns_info)))
  485. goto clear;
  486. if (unlikely((u64)(dev_t)dev != dev))
  487. goto clear;
  488. if (unlikely(!task))
  489. goto clear;
  490. pidns = task_active_pid_ns(task);
  491. if (unlikely(!pidns)) {
  492. err = -ENOENT;
  493. goto clear;
  494. }
  495. if (!ns_match(&pidns->ns, (dev_t)dev, ino))
  496. goto clear;
  497. nsdata->pid = task_pid_nr_ns(task, pidns);
  498. nsdata->tgid = task_tgid_nr_ns(task, pidns);
  499. return 0;
  500. clear:
  501. memset((void *)nsdata, 0, (size_t) size);
  502. return err;
  503. }
  504. const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = {
  505. .func = bpf_get_ns_current_pid_tgid,
  506. .gpl_only = false,
  507. .ret_type = RET_INTEGER,
  508. .arg1_type = ARG_ANYTHING,
  509. .arg2_type = ARG_ANYTHING,
  510. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  511. .arg4_type = ARG_CONST_SIZE,
  512. };
  513. static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
  514. .func = bpf_get_raw_cpu_id,
  515. .gpl_only = false,
  516. .ret_type = RET_INTEGER,
  517. };
  518. BPF_CALL_5(bpf_event_output_data, void *, ctx, struct bpf_map *, map,
  519. u64, flags, void *, data, u64, size)
  520. {
  521. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  522. return -EINVAL;
  523. return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
  524. }
  525. const struct bpf_func_proto bpf_event_output_data_proto = {
  526. .func = bpf_event_output_data,
  527. .gpl_only = true,
  528. .ret_type = RET_INTEGER,
  529. .arg1_type = ARG_PTR_TO_CTX,
  530. .arg2_type = ARG_CONST_MAP_PTR,
  531. .arg3_type = ARG_ANYTHING,
  532. .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  533. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  534. };
  535. BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
  536. const void __user *, user_ptr)
  537. {
  538. int ret = copy_from_user(dst, user_ptr, size);
  539. if (unlikely(ret)) {
  540. memset(dst, 0, size);
  541. ret = -EFAULT;
  542. }
  543. return ret;
  544. }
  545. const struct bpf_func_proto bpf_copy_from_user_proto = {
  546. .func = bpf_copy_from_user,
  547. .gpl_only = false,
  548. .ret_type = RET_INTEGER,
  549. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  550. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  551. .arg3_type = ARG_ANYTHING,
  552. };
  553. BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
  554. const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
  555. {
  556. int ret;
  557. /* flags is not used yet */
  558. if (unlikely(flags))
  559. return -EINVAL;
  560. if (unlikely(!size))
  561. return 0;
  562. ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0);
  563. if (ret == size)
  564. return 0;
  565. memset(dst, 0, size);
  566. /* Return -EFAULT for partial read */
  567. return ret < 0 ? ret : -EFAULT;
  568. }
  569. const struct bpf_func_proto bpf_copy_from_user_task_proto = {
  570. .func = bpf_copy_from_user_task,
  571. .gpl_only = true,
  572. .ret_type = RET_INTEGER,
  573. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  574. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  575. .arg3_type = ARG_ANYTHING,
  576. .arg4_type = ARG_PTR_TO_BTF_ID,
  577. .arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
  578. .arg5_type = ARG_ANYTHING
  579. };
  580. BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
  581. {
  582. if (cpu >= nr_cpu_ids)
  583. return (unsigned long)NULL;
  584. return (unsigned long)per_cpu_ptr((const void __percpu *)ptr, cpu);
  585. }
  586. const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
  587. .func = bpf_per_cpu_ptr,
  588. .gpl_only = false,
  589. .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
  590. .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
  591. .arg2_type = ARG_ANYTHING,
  592. };
  593. BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)
  594. {
  595. return (unsigned long)this_cpu_ptr((const void __percpu *)percpu_ptr);
  596. }
  597. const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
  598. .func = bpf_this_cpu_ptr,
  599. .gpl_only = false,
  600. .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
  601. .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
  602. };
  603. static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
  604. size_t bufsz)
  605. {
  606. void __user *user_ptr = (__force void __user *)unsafe_ptr;
  607. buf[0] = 0;
  608. switch (fmt_ptype) {
  609. case 's':
  610. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  611. if ((unsigned long)unsafe_ptr < TASK_SIZE)
  612. return strncpy_from_user_nofault(buf, user_ptr, bufsz);
  613. fallthrough;
  614. #endif
  615. case 'k':
  616. return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
  617. case 'u':
  618. return strncpy_from_user_nofault(buf, user_ptr, bufsz);
  619. }
  620. return -EINVAL;
  621. }
  622. /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
  623. * arguments representation.
  624. */
  625. #define MAX_BPRINTF_BUF_LEN 512
  626. /* Support executing three nested bprintf helper calls on a given CPU */
  627. #define MAX_BPRINTF_NEST_LEVEL 3
  628. struct bpf_bprintf_buffers {
  629. char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
  630. };
  631. static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
  632. static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
  633. static int try_get_fmt_tmp_buf(char **tmp_buf)
  634. {
  635. struct bpf_bprintf_buffers *bufs;
  636. int nest_level;
  637. preempt_disable();
  638. nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
  639. if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
  640. this_cpu_dec(bpf_bprintf_nest_level);
  641. preempt_enable();
  642. return -EBUSY;
  643. }
  644. bufs = this_cpu_ptr(&bpf_bprintf_bufs);
  645. *tmp_buf = bufs->tmp_bufs[nest_level - 1];
  646. return 0;
  647. }
  648. void bpf_bprintf_cleanup(void)
  649. {
  650. if (this_cpu_read(bpf_bprintf_nest_level)) {
  651. this_cpu_dec(bpf_bprintf_nest_level);
  652. preempt_enable();
  653. }
  654. }
  655. /*
  656. * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers
  657. *
  658. * Returns a negative value if fmt is an invalid format string or 0 otherwise.
  659. *
  660. * This can be used in two ways:
  661. * - Format string verification only: when bin_args is NULL
  662. * - Arguments preparation: in addition to the above verification, it writes in
  663. * bin_args a binary representation of arguments usable by bstr_printf where
  664. * pointers from BPF have been sanitized.
  665. *
  666. * In argument preparation mode, if 0 is returned, safe temporary buffers are
  667. * allocated and bpf_bprintf_cleanup should be called to free them after use.
  668. */
  669. int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
  670. u32 **bin_args, u32 num_args)
  671. {
  672. char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end;
  673. size_t sizeof_cur_arg, sizeof_cur_ip;
  674. int err, i, num_spec = 0;
  675. u64 cur_arg;
  676. char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX";
  677. fmt_end = strnchr(fmt, fmt_size, 0);
  678. if (!fmt_end)
  679. return -EINVAL;
  680. fmt_size = fmt_end - fmt;
  681. if (bin_args) {
  682. if (num_args && try_get_fmt_tmp_buf(&tmp_buf))
  683. return -EBUSY;
  684. tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN;
  685. *bin_args = (u32 *)tmp_buf;
  686. }
  687. for (i = 0; i < fmt_size; i++) {
  688. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
  689. err = -EINVAL;
  690. goto out;
  691. }
  692. if (fmt[i] != '%')
  693. continue;
  694. if (fmt[i + 1] == '%') {
  695. i++;
  696. continue;
  697. }
  698. if (num_spec >= num_args) {
  699. err = -EINVAL;
  700. goto out;
  701. }
  702. /* The string is zero-terminated so if fmt[i] != 0, we can
  703. * always access fmt[i + 1], in the worst case it will be a 0
  704. */
  705. i++;
  706. /* skip optional "[0 +-][num]" width formatting field */
  707. while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
  708. fmt[i] == ' ')
  709. i++;
  710. if (fmt[i] >= '1' && fmt[i] <= '9') {
  711. i++;
  712. while (fmt[i] >= '0' && fmt[i] <= '9')
  713. i++;
  714. }
  715. if (fmt[i] == 'p') {
  716. sizeof_cur_arg = sizeof(long);
  717. if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') &&
  718. fmt[i + 2] == 's') {
  719. fmt_ptype = fmt[i + 1];
  720. i += 2;
  721. goto fmt_str;
  722. }
  723. if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
  724. ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' ||
  725. fmt[i + 1] == 'x' || fmt[i + 1] == 's' ||
  726. fmt[i + 1] == 'S') {
  727. /* just kernel pointers */
  728. if (tmp_buf)
  729. cur_arg = raw_args[num_spec];
  730. i++;
  731. goto nocopy_fmt;
  732. }
  733. if (fmt[i + 1] == 'B') {
  734. if (tmp_buf) {
  735. err = snprintf(tmp_buf,
  736. (tmp_buf_end - tmp_buf),
  737. "%pB",
  738. (void *)(long)raw_args[num_spec]);
  739. tmp_buf += (err + 1);
  740. }
  741. i++;
  742. num_spec++;
  743. continue;
  744. }
  745. /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
  746. if ((fmt[i + 1] != 'i' && fmt[i + 1] != 'I') ||
  747. (fmt[i + 2] != '4' && fmt[i + 2] != '6')) {
  748. err = -EINVAL;
  749. goto out;
  750. }
  751. i += 2;
  752. if (!tmp_buf)
  753. goto nocopy_fmt;
  754. sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
  755. if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
  756. err = -ENOSPC;
  757. goto out;
  758. }
  759. unsafe_ptr = (char *)(long)raw_args[num_spec];
  760. err = copy_from_kernel_nofault(cur_ip, unsafe_ptr,
  761. sizeof_cur_ip);
  762. if (err < 0)
  763. memset(cur_ip, 0, sizeof_cur_ip);
  764. /* hack: bstr_printf expects IP addresses to be
  765. * pre-formatted as strings, ironically, the easiest way
  766. * to do that is to call snprintf.
  767. */
  768. ip_spec[2] = fmt[i - 1];
  769. ip_spec[3] = fmt[i];
  770. err = snprintf(tmp_buf, tmp_buf_end - tmp_buf,
  771. ip_spec, &cur_ip);
  772. tmp_buf += err + 1;
  773. num_spec++;
  774. continue;
  775. } else if (fmt[i] == 's') {
  776. fmt_ptype = fmt[i];
  777. fmt_str:
  778. if (fmt[i + 1] != 0 &&
  779. !isspace(fmt[i + 1]) &&
  780. !ispunct(fmt[i + 1])) {
  781. err = -EINVAL;
  782. goto out;
  783. }
  784. if (!tmp_buf)
  785. goto nocopy_fmt;
  786. if (tmp_buf_end == tmp_buf) {
  787. err = -ENOSPC;
  788. goto out;
  789. }
  790. unsafe_ptr = (char *)(long)raw_args[num_spec];
  791. err = bpf_trace_copy_string(tmp_buf, unsafe_ptr,
  792. fmt_ptype,
  793. tmp_buf_end - tmp_buf);
  794. if (err < 0) {
  795. tmp_buf[0] = '\0';
  796. err = 1;
  797. }
  798. tmp_buf += err;
  799. num_spec++;
  800. continue;
  801. } else if (fmt[i] == 'c') {
  802. if (!tmp_buf)
  803. goto nocopy_fmt;
  804. if (tmp_buf_end == tmp_buf) {
  805. err = -ENOSPC;
  806. goto out;
  807. }
  808. *tmp_buf = raw_args[num_spec];
  809. tmp_buf++;
  810. num_spec++;
  811. continue;
  812. }
  813. sizeof_cur_arg = sizeof(int);
  814. if (fmt[i] == 'l') {
  815. sizeof_cur_arg = sizeof(long);
  816. i++;
  817. }
  818. if (fmt[i] == 'l') {
  819. sizeof_cur_arg = sizeof(long long);
  820. i++;
  821. }
  822. if (fmt[i] != 'i' && fmt[i] != 'd' && fmt[i] != 'u' &&
  823. fmt[i] != 'x' && fmt[i] != 'X') {
  824. err = -EINVAL;
  825. goto out;
  826. }
  827. if (tmp_buf)
  828. cur_arg = raw_args[num_spec];
  829. nocopy_fmt:
  830. if (tmp_buf) {
  831. tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
  832. if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
  833. err = -ENOSPC;
  834. goto out;
  835. }
  836. if (sizeof_cur_arg == 8) {
  837. *(u32 *)tmp_buf = *(u32 *)&cur_arg;
  838. *(u32 *)(tmp_buf + 4) = *((u32 *)&cur_arg + 1);
  839. } else {
  840. *(u32 *)tmp_buf = (u32)(long)cur_arg;
  841. }
  842. tmp_buf += sizeof_cur_arg;
  843. }
  844. num_spec++;
  845. }
  846. err = 0;
  847. out:
  848. if (err)
  849. bpf_bprintf_cleanup();
  850. return err;
  851. }
  852. BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt,
  853. const void *, data, u32, data_len)
  854. {
  855. int err, num_args;
  856. u32 *bin_args;
  857. if (data_len % 8 || data_len > MAX_BPRINTF_VARARGS * 8 ||
  858. (data_len && !data))
  859. return -EINVAL;
  860. num_args = data_len / 8;
  861. /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
  862. * can safely give an unbounded size.
  863. */
  864. err = bpf_bprintf_prepare(fmt, UINT_MAX, data, &bin_args, num_args);
  865. if (err < 0)
  866. return err;
  867. err = bstr_printf(str, str_size, fmt, bin_args);
  868. bpf_bprintf_cleanup();
  869. return err + 1;
  870. }
  871. const struct bpf_func_proto bpf_snprintf_proto = {
  872. .func = bpf_snprintf,
  873. .gpl_only = true,
  874. .ret_type = RET_INTEGER,
  875. .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
  876. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  877. .arg3_type = ARG_PTR_TO_CONST_STR,
  878. .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
  879. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  880. };
  881. /* BPF map elements can contain 'struct bpf_timer'.
  882. * Such map owns all of its BPF timers.
  883. * 'struct bpf_timer' is allocated as part of map element allocation
  884. * and it's zero initialized.
  885. * That space is used to keep 'struct bpf_timer_kern'.
  886. * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
  887. * remembers 'struct bpf_map *' pointer it's part of.
  888. * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
  889. * bpf_timer_start() arms the timer.
  890. * If user space reference to a map goes to zero at this point
  891. * ops->map_release_uref callback is responsible for cancelling the timers,
  892. * freeing their memory, and decrementing prog's refcnts.
  893. * bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
  894. * Inner maps can contain bpf timers as well. ops->map_release_uref is
  895. * freeing the timers when inner map is replaced or deleted by user space.
  896. */
  897. struct bpf_hrtimer {
  898. struct hrtimer timer;
  899. struct bpf_map *map;
  900. struct bpf_prog *prog;
  901. void __rcu *callback_fn;
  902. void *value;
  903. };
  904. /* the actual struct hidden inside uapi struct bpf_timer */
  905. struct bpf_timer_kern {
  906. struct bpf_hrtimer *timer;
  907. /* bpf_spin_lock is used here instead of spinlock_t to make
  908. * sure that it always fits into space reserved by struct bpf_timer
  909. * regardless of LOCKDEP and spinlock debug flags.
  910. */
  911. struct bpf_spin_lock lock;
  912. } __attribute__((aligned(8)));
  913. static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
  914. static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
  915. {
  916. struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
  917. struct bpf_map *map = t->map;
  918. void *value = t->value;
  919. bpf_callback_t callback_fn;
  920. void *key;
  921. u32 idx;
  922. BTF_TYPE_EMIT(struct bpf_timer);
  923. callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held());
  924. if (!callback_fn)
  925. goto out;
  926. /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
  927. * cannot be preempted by another bpf_timer_cb() on the same cpu.
  928. * Remember the timer this callback is servicing to prevent
  929. * deadlock if callback_fn() calls bpf_timer_cancel() or
  930. * bpf_map_delete_elem() on the same timer.
  931. */
  932. this_cpu_write(hrtimer_running, t);
  933. if (map->map_type == BPF_MAP_TYPE_ARRAY) {
  934. struct bpf_array *array = container_of(map, struct bpf_array, map);
  935. /* compute the key */
  936. idx = ((char *)value - array->value) / array->elem_size;
  937. key = &idx;
  938. } else { /* hash or lru */
  939. key = value - round_up(map->key_size, 8);
  940. }
  941. callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
  942. /* The verifier checked that return value is zero. */
  943. this_cpu_write(hrtimer_running, NULL);
  944. out:
  945. return HRTIMER_NORESTART;
  946. }
  947. BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map,
  948. u64, flags)
  949. {
  950. clockid_t clockid = flags & (MAX_CLOCKS - 1);
  951. struct bpf_hrtimer *t;
  952. int ret = 0;
  953. BUILD_BUG_ON(MAX_CLOCKS != 16);
  954. BUILD_BUG_ON(sizeof(struct bpf_timer_kern) > sizeof(struct bpf_timer));
  955. BUILD_BUG_ON(__alignof__(struct bpf_timer_kern) != __alignof__(struct bpf_timer));
  956. if (in_nmi())
  957. return -EOPNOTSUPP;
  958. if (flags >= MAX_CLOCKS ||
  959. /* similar to timerfd except _ALARM variants are not supported */
  960. (clockid != CLOCK_MONOTONIC &&
  961. clockid != CLOCK_REALTIME &&
  962. clockid != CLOCK_BOOTTIME))
  963. return -EINVAL;
  964. __bpf_spin_lock_irqsave(&timer->lock);
  965. t = timer->timer;
  966. if (t) {
  967. ret = -EBUSY;
  968. goto out;
  969. }
  970. /* allocate hrtimer via map_kmalloc to use memcg accounting */
  971. t = bpf_map_kmalloc_node(map, sizeof(*t), GFP_ATOMIC, map->numa_node);
  972. if (!t) {
  973. ret = -ENOMEM;
  974. goto out;
  975. }
  976. t->value = (void *)timer - map->timer_off;
  977. t->map = map;
  978. t->prog = NULL;
  979. rcu_assign_pointer(t->callback_fn, NULL);
  980. hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT);
  981. t->timer.function = bpf_timer_cb;
  982. WRITE_ONCE(timer->timer, t);
  983. /* Guarantee the order between timer->timer and map->usercnt. So
  984. * when there are concurrent uref release and bpf timer init, either
  985. * bpf_timer_cancel_and_free() called by uref release reads a no-NULL
  986. * timer or atomic64_read() below returns a zero usercnt.
  987. */
  988. smp_mb();
  989. if (!atomic64_read(&map->usercnt)) {
  990. /* maps with timers must be either held by user space
  991. * or pinned in bpffs.
  992. */
  993. WRITE_ONCE(timer->timer, NULL);
  994. kfree(t);
  995. ret = -EPERM;
  996. }
  997. out:
  998. __bpf_spin_unlock_irqrestore(&timer->lock);
  999. return ret;
  1000. }
  1001. static const struct bpf_func_proto bpf_timer_init_proto = {
  1002. .func = bpf_timer_init,
  1003. .gpl_only = true,
  1004. .ret_type = RET_INTEGER,
  1005. .arg1_type = ARG_PTR_TO_TIMER,
  1006. .arg2_type = ARG_CONST_MAP_PTR,
  1007. .arg3_type = ARG_ANYTHING,
  1008. };
  1009. BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn,
  1010. struct bpf_prog_aux *, aux)
  1011. {
  1012. struct bpf_prog *prev, *prog = aux->prog;
  1013. struct bpf_hrtimer *t;
  1014. int ret = 0;
  1015. if (in_nmi())
  1016. return -EOPNOTSUPP;
  1017. __bpf_spin_lock_irqsave(&timer->lock);
  1018. t = timer->timer;
  1019. if (!t) {
  1020. ret = -EINVAL;
  1021. goto out;
  1022. }
  1023. if (!atomic64_read(&t->map->usercnt)) {
  1024. /* maps with timers must be either held by user space
  1025. * or pinned in bpffs. Otherwise timer might still be
  1026. * running even when bpf prog is detached and user space
  1027. * is gone, since map_release_uref won't ever be called.
  1028. */
  1029. ret = -EPERM;
  1030. goto out;
  1031. }
  1032. prev = t->prog;
  1033. if (prev != prog) {
  1034. /* Bump prog refcnt once. Every bpf_timer_set_callback()
  1035. * can pick different callback_fn-s within the same prog.
  1036. */
  1037. prog = bpf_prog_inc_not_zero(prog);
  1038. if (IS_ERR(prog)) {
  1039. ret = PTR_ERR(prog);
  1040. goto out;
  1041. }
  1042. if (prev)
  1043. /* Drop prev prog refcnt when swapping with new prog */
  1044. bpf_prog_put(prev);
  1045. t->prog = prog;
  1046. }
  1047. rcu_assign_pointer(t->callback_fn, callback_fn);
  1048. out:
  1049. __bpf_spin_unlock_irqrestore(&timer->lock);
  1050. return ret;
  1051. }
  1052. static const struct bpf_func_proto bpf_timer_set_callback_proto = {
  1053. .func = bpf_timer_set_callback,
  1054. .gpl_only = true,
  1055. .ret_type = RET_INTEGER,
  1056. .arg1_type = ARG_PTR_TO_TIMER,
  1057. .arg2_type = ARG_PTR_TO_FUNC,
  1058. };
  1059. BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, flags)
  1060. {
  1061. struct bpf_hrtimer *t;
  1062. int ret = 0;
  1063. if (in_nmi())
  1064. return -EOPNOTSUPP;
  1065. if (flags)
  1066. return -EINVAL;
  1067. __bpf_spin_lock_irqsave(&timer->lock);
  1068. t = timer->timer;
  1069. if (!t || !t->prog) {
  1070. ret = -EINVAL;
  1071. goto out;
  1072. }
  1073. hrtimer_start(&t->timer, ns_to_ktime(nsecs), HRTIMER_MODE_REL_SOFT);
  1074. out:
  1075. __bpf_spin_unlock_irqrestore(&timer->lock);
  1076. return ret;
  1077. }
  1078. static const struct bpf_func_proto bpf_timer_start_proto = {
  1079. .func = bpf_timer_start,
  1080. .gpl_only = true,
  1081. .ret_type = RET_INTEGER,
  1082. .arg1_type = ARG_PTR_TO_TIMER,
  1083. .arg2_type = ARG_ANYTHING,
  1084. .arg3_type = ARG_ANYTHING,
  1085. };
  1086. static void drop_prog_refcnt(struct bpf_hrtimer *t)
  1087. {
  1088. struct bpf_prog *prog = t->prog;
  1089. if (prog) {
  1090. bpf_prog_put(prog);
  1091. t->prog = NULL;
  1092. rcu_assign_pointer(t->callback_fn, NULL);
  1093. }
  1094. }
  1095. BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer)
  1096. {
  1097. struct bpf_hrtimer *t;
  1098. int ret = 0;
  1099. if (in_nmi())
  1100. return -EOPNOTSUPP;
  1101. __bpf_spin_lock_irqsave(&timer->lock);
  1102. t = timer->timer;
  1103. if (!t) {
  1104. ret = -EINVAL;
  1105. goto out;
  1106. }
  1107. if (this_cpu_read(hrtimer_running) == t) {
  1108. /* If bpf callback_fn is trying to bpf_timer_cancel()
  1109. * its own timer the hrtimer_cancel() will deadlock
  1110. * since it waits for callback_fn to finish
  1111. */
  1112. ret = -EDEADLK;
  1113. goto out;
  1114. }
  1115. drop_prog_refcnt(t);
  1116. out:
  1117. __bpf_spin_unlock_irqrestore(&timer->lock);
  1118. /* Cancel the timer and wait for associated callback to finish
  1119. * if it was running.
  1120. */
  1121. ret = ret ?: hrtimer_cancel(&t->timer);
  1122. return ret;
  1123. }
  1124. static const struct bpf_func_proto bpf_timer_cancel_proto = {
  1125. .func = bpf_timer_cancel,
  1126. .gpl_only = true,
  1127. .ret_type = RET_INTEGER,
  1128. .arg1_type = ARG_PTR_TO_TIMER,
  1129. };
  1130. /* This function is called by map_delete/update_elem for individual element and
  1131. * by ops->map_release_uref when the user space reference to a map reaches zero.
  1132. */
  1133. void bpf_timer_cancel_and_free(void *val)
  1134. {
  1135. struct bpf_timer_kern *timer = val;
  1136. struct bpf_hrtimer *t;
  1137. /* Performance optimization: read timer->timer without lock first. */
  1138. if (!READ_ONCE(timer->timer))
  1139. return;
  1140. __bpf_spin_lock_irqsave(&timer->lock);
  1141. /* re-read it under lock */
  1142. t = timer->timer;
  1143. if (!t)
  1144. goto out;
  1145. drop_prog_refcnt(t);
  1146. /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
  1147. * this timer, since it won't be initialized.
  1148. */
  1149. WRITE_ONCE(timer->timer, NULL);
  1150. out:
  1151. __bpf_spin_unlock_irqrestore(&timer->lock);
  1152. if (!t)
  1153. return;
  1154. /* Cancel the timer and wait for callback to complete if it was running.
  1155. * If hrtimer_cancel() can be safely called it's safe to call kfree(t)
  1156. * right after for both preallocated and non-preallocated maps.
  1157. * The timer->timer = NULL was already done and no code path can
  1158. * see address 't' anymore.
  1159. *
  1160. * Check that bpf_map_delete/update_elem() wasn't called from timer
  1161. * callback_fn. In such case don't call hrtimer_cancel() (since it will
  1162. * deadlock) and don't call hrtimer_try_to_cancel() (since it will just
  1163. * return -1). Though callback_fn is still running on this cpu it's
  1164. * safe to do kfree(t) because bpf_timer_cb() read everything it needed
  1165. * from 't'. The bpf subprog callback_fn won't be able to access 't',
  1166. * since timer->timer = NULL was already done. The timer will be
  1167. * effectively cancelled because bpf_timer_cb() will return
  1168. * HRTIMER_NORESTART.
  1169. */
  1170. if (this_cpu_read(hrtimer_running) != t)
  1171. hrtimer_cancel(&t->timer);
  1172. kfree(t);
  1173. }
  1174. BPF_CALL_2(bpf_kptr_xchg, void *, map_value, void *, ptr)
  1175. {
  1176. unsigned long *kptr = map_value;
  1177. return xchg(kptr, (unsigned long)ptr);
  1178. }
  1179. /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg()
  1180. * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to
  1181. * denote type that verifier will determine.
  1182. */
  1183. static const struct bpf_func_proto bpf_kptr_xchg_proto = {
  1184. .func = bpf_kptr_xchg,
  1185. .gpl_only = false,
  1186. .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
  1187. .ret_btf_id = BPF_PTR_POISON,
  1188. .arg1_type = ARG_PTR_TO_KPTR,
  1189. .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL | OBJ_RELEASE,
  1190. .arg2_btf_id = BPF_PTR_POISON,
  1191. };
  1192. /* Since the upper 8 bits of dynptr->size is reserved, the
  1193. * maximum supported size is 2^24 - 1.
  1194. */
  1195. #define DYNPTR_MAX_SIZE ((1UL << 24) - 1)
  1196. #define DYNPTR_TYPE_SHIFT 28
  1197. #define DYNPTR_SIZE_MASK 0xFFFFFF
  1198. #define DYNPTR_RDONLY_BIT BIT(31)
  1199. static bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
  1200. {
  1201. return ptr->size & DYNPTR_RDONLY_BIT;
  1202. }
  1203. static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type)
  1204. {
  1205. ptr->size |= type << DYNPTR_TYPE_SHIFT;
  1206. }
  1207. u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
  1208. {
  1209. return ptr->size & DYNPTR_SIZE_MASK;
  1210. }
  1211. int bpf_dynptr_check_size(u32 size)
  1212. {
  1213. return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
  1214. }
  1215. void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
  1216. enum bpf_dynptr_type type, u32 offset, u32 size)
  1217. {
  1218. ptr->data = data;
  1219. ptr->offset = offset;
  1220. ptr->size = size;
  1221. bpf_dynptr_set_type(ptr, type);
  1222. }
  1223. void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
  1224. {
  1225. memset(ptr, 0, sizeof(*ptr));
  1226. }
  1227. static int bpf_dynptr_check_off_len(struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
  1228. {
  1229. u32 size = bpf_dynptr_get_size(ptr);
  1230. if (len > size || offset > size - len)
  1231. return -E2BIG;
  1232. return 0;
  1233. }
  1234. BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr)
  1235. {
  1236. int err;
  1237. BTF_TYPE_EMIT(struct bpf_dynptr);
  1238. err = bpf_dynptr_check_size(size);
  1239. if (err)
  1240. goto error;
  1241. /* flags is currently unsupported */
  1242. if (flags) {
  1243. err = -EINVAL;
  1244. goto error;
  1245. }
  1246. bpf_dynptr_init(ptr, data, BPF_DYNPTR_TYPE_LOCAL, 0, size);
  1247. return 0;
  1248. error:
  1249. bpf_dynptr_set_null(ptr);
  1250. return err;
  1251. }
  1252. static const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
  1253. .func = bpf_dynptr_from_mem,
  1254. .gpl_only = false,
  1255. .ret_type = RET_INTEGER,
  1256. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  1257. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  1258. .arg3_type = ARG_ANYTHING,
  1259. .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
  1260. };
  1261. BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src,
  1262. u32, offset, u64, flags)
  1263. {
  1264. int err;
  1265. if (!src->data || flags)
  1266. return -EINVAL;
  1267. err = bpf_dynptr_check_off_len(src, offset, len);
  1268. if (err)
  1269. return err;
  1270. memcpy(dst, src->data + src->offset + offset, len);
  1271. return 0;
  1272. }
  1273. static const struct bpf_func_proto bpf_dynptr_read_proto = {
  1274. .func = bpf_dynptr_read,
  1275. .gpl_only = false,
  1276. .ret_type = RET_INTEGER,
  1277. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  1278. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  1279. .arg3_type = ARG_PTR_TO_DYNPTR,
  1280. .arg4_type = ARG_ANYTHING,
  1281. .arg5_type = ARG_ANYTHING,
  1282. };
  1283. BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
  1284. u32, len, u64, flags)
  1285. {
  1286. int err;
  1287. if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
  1288. return -EINVAL;
  1289. err = bpf_dynptr_check_off_len(dst, offset, len);
  1290. if (err)
  1291. return err;
  1292. memcpy(dst->data + dst->offset + offset, src, len);
  1293. return 0;
  1294. }
  1295. static const struct bpf_func_proto bpf_dynptr_write_proto = {
  1296. .func = bpf_dynptr_write,
  1297. .gpl_only = false,
  1298. .ret_type = RET_INTEGER,
  1299. .arg1_type = ARG_PTR_TO_DYNPTR,
  1300. .arg2_type = ARG_ANYTHING,
  1301. .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1302. .arg4_type = ARG_CONST_SIZE_OR_ZERO,
  1303. .arg5_type = ARG_ANYTHING,
  1304. };
  1305. BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len)
  1306. {
  1307. int err;
  1308. if (!ptr->data)
  1309. return 0;
  1310. err = bpf_dynptr_check_off_len(ptr, offset, len);
  1311. if (err)
  1312. return 0;
  1313. if (bpf_dynptr_is_rdonly(ptr))
  1314. return 0;
  1315. return (unsigned long)(ptr->data + ptr->offset + offset);
  1316. }
  1317. static const struct bpf_func_proto bpf_dynptr_data_proto = {
  1318. .func = bpf_dynptr_data,
  1319. .gpl_only = false,
  1320. .ret_type = RET_PTR_TO_DYNPTR_MEM_OR_NULL,
  1321. .arg1_type = ARG_PTR_TO_DYNPTR,
  1322. .arg2_type = ARG_ANYTHING,
  1323. .arg3_type = ARG_CONST_ALLOC_SIZE_OR_ZERO,
  1324. };
  1325. const struct bpf_func_proto bpf_get_current_task_proto __weak;
  1326. const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
  1327. const struct bpf_func_proto bpf_probe_read_user_proto __weak;
  1328. const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
  1329. const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
  1330. const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
  1331. const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
  1332. const struct bpf_func_proto *
  1333. bpf_base_func_proto(enum bpf_func_id func_id)
  1334. {
  1335. switch (func_id) {
  1336. case BPF_FUNC_map_lookup_elem:
  1337. return &bpf_map_lookup_elem_proto;
  1338. case BPF_FUNC_map_update_elem:
  1339. return &bpf_map_update_elem_proto;
  1340. case BPF_FUNC_map_delete_elem:
  1341. return &bpf_map_delete_elem_proto;
  1342. case BPF_FUNC_map_push_elem:
  1343. return &bpf_map_push_elem_proto;
  1344. case BPF_FUNC_map_pop_elem:
  1345. return &bpf_map_pop_elem_proto;
  1346. case BPF_FUNC_map_peek_elem:
  1347. return &bpf_map_peek_elem_proto;
  1348. case BPF_FUNC_map_lookup_percpu_elem:
  1349. return &bpf_map_lookup_percpu_elem_proto;
  1350. case BPF_FUNC_get_prandom_u32:
  1351. return &bpf_get_prandom_u32_proto;
  1352. case BPF_FUNC_get_smp_processor_id:
  1353. return &bpf_get_raw_smp_processor_id_proto;
  1354. case BPF_FUNC_get_numa_node_id:
  1355. return &bpf_get_numa_node_id_proto;
  1356. case BPF_FUNC_tail_call:
  1357. return &bpf_tail_call_proto;
  1358. case BPF_FUNC_ktime_get_ns:
  1359. return &bpf_ktime_get_ns_proto;
  1360. case BPF_FUNC_ktime_get_boot_ns:
  1361. return &bpf_ktime_get_boot_ns_proto;
  1362. case BPF_FUNC_ktime_get_tai_ns:
  1363. return &bpf_ktime_get_tai_ns_proto;
  1364. case BPF_FUNC_ringbuf_output:
  1365. return &bpf_ringbuf_output_proto;
  1366. case BPF_FUNC_ringbuf_reserve:
  1367. return &bpf_ringbuf_reserve_proto;
  1368. case BPF_FUNC_ringbuf_submit:
  1369. return &bpf_ringbuf_submit_proto;
  1370. case BPF_FUNC_ringbuf_discard:
  1371. return &bpf_ringbuf_discard_proto;
  1372. case BPF_FUNC_ringbuf_query:
  1373. return &bpf_ringbuf_query_proto;
  1374. case BPF_FUNC_strncmp:
  1375. return &bpf_strncmp_proto;
  1376. case BPF_FUNC_strtol:
  1377. return &bpf_strtol_proto;
  1378. case BPF_FUNC_strtoul:
  1379. return &bpf_strtoul_proto;
  1380. default:
  1381. break;
  1382. }
  1383. if (!bpf_capable())
  1384. return NULL;
  1385. switch (func_id) {
  1386. case BPF_FUNC_spin_lock:
  1387. return &bpf_spin_lock_proto;
  1388. case BPF_FUNC_spin_unlock:
  1389. return &bpf_spin_unlock_proto;
  1390. case BPF_FUNC_jiffies64:
  1391. return &bpf_jiffies64_proto;
  1392. case BPF_FUNC_per_cpu_ptr:
  1393. return &bpf_per_cpu_ptr_proto;
  1394. case BPF_FUNC_this_cpu_ptr:
  1395. return &bpf_this_cpu_ptr_proto;
  1396. case BPF_FUNC_timer_init:
  1397. return &bpf_timer_init_proto;
  1398. case BPF_FUNC_timer_set_callback:
  1399. return &bpf_timer_set_callback_proto;
  1400. case BPF_FUNC_timer_start:
  1401. return &bpf_timer_start_proto;
  1402. case BPF_FUNC_timer_cancel:
  1403. return &bpf_timer_cancel_proto;
  1404. case BPF_FUNC_kptr_xchg:
  1405. return &bpf_kptr_xchg_proto;
  1406. case BPF_FUNC_for_each_map_elem:
  1407. return &bpf_for_each_map_elem_proto;
  1408. case BPF_FUNC_loop:
  1409. return &bpf_loop_proto;
  1410. case BPF_FUNC_user_ringbuf_drain:
  1411. return &bpf_user_ringbuf_drain_proto;
  1412. case BPF_FUNC_ringbuf_reserve_dynptr:
  1413. return &bpf_ringbuf_reserve_dynptr_proto;
  1414. case BPF_FUNC_ringbuf_submit_dynptr:
  1415. return &bpf_ringbuf_submit_dynptr_proto;
  1416. case BPF_FUNC_ringbuf_discard_dynptr:
  1417. return &bpf_ringbuf_discard_dynptr_proto;
  1418. case BPF_FUNC_dynptr_from_mem:
  1419. return &bpf_dynptr_from_mem_proto;
  1420. case BPF_FUNC_dynptr_read:
  1421. return &bpf_dynptr_read_proto;
  1422. case BPF_FUNC_dynptr_write:
  1423. return &bpf_dynptr_write_proto;
  1424. case BPF_FUNC_dynptr_data:
  1425. return &bpf_dynptr_data_proto;
  1426. default:
  1427. break;
  1428. }
  1429. if (!perfmon_capable())
  1430. return NULL;
  1431. switch (func_id) {
  1432. case BPF_FUNC_trace_printk:
  1433. return bpf_get_trace_printk_proto();
  1434. case BPF_FUNC_get_current_task:
  1435. return &bpf_get_current_task_proto;
  1436. case BPF_FUNC_get_current_task_btf:
  1437. return &bpf_get_current_task_btf_proto;
  1438. case BPF_FUNC_probe_read_user:
  1439. return &bpf_probe_read_user_proto;
  1440. case BPF_FUNC_probe_read_kernel:
  1441. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1442. NULL : &bpf_probe_read_kernel_proto;
  1443. case BPF_FUNC_probe_read_user_str:
  1444. return &bpf_probe_read_user_str_proto;
  1445. case BPF_FUNC_probe_read_kernel_str:
  1446. return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
  1447. NULL : &bpf_probe_read_kernel_str_proto;
  1448. case BPF_FUNC_snprintf_btf:
  1449. return &bpf_snprintf_btf_proto;
  1450. case BPF_FUNC_snprintf:
  1451. return &bpf_snprintf_proto;
  1452. case BPF_FUNC_task_pt_regs:
  1453. return &bpf_task_pt_regs_proto;
  1454. case BPF_FUNC_trace_vprintk:
  1455. return bpf_get_trace_vprintk_proto();
  1456. default:
  1457. return NULL;
  1458. }
  1459. }
  1460. BTF_SET8_START(tracing_btf_ids)
  1461. #ifdef CONFIG_KEXEC_CORE
  1462. BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
  1463. #endif
  1464. BTF_SET8_END(tracing_btf_ids)
  1465. static const struct btf_kfunc_id_set tracing_kfunc_set = {
  1466. .owner = THIS_MODULE,
  1467. .set = &tracing_btf_ids,
  1468. };
  1469. static int __init kfunc_init(void)
  1470. {
  1471. return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
  1472. }
  1473. late_initcall(kfunc_init);