syscall.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/bpf.h>
  13. #include <linux/bpf_trace.h>
  14. #include <linux/bpf_lirc.h>
  15. #include <linux/btf.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/mmzone.h>
  21. #include <linux/anon_inodes.h>
  22. #include <linux/fdtable.h>
  23. #include <linux/file.h>
  24. #include <linux/fs.h>
  25. #include <linux/license.h>
  26. #include <linux/filter.h>
  27. #include <linux/version.h>
  28. #include <linux/kernel.h>
  29. #include <linux/idr.h>
  30. #include <linux/cred.h>
  31. #include <linux/timekeeping.h>
  32. #include <linux/ctype.h>
  33. #include <linux/btf.h>
  34. #include <linux/nospec.h>
  35. #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
  36. (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
  37. (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
  38. (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  39. #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
  40. #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
  41. #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
  42. DEFINE_PER_CPU(int, bpf_prog_active);
  43. static DEFINE_IDR(prog_idr);
  44. static DEFINE_SPINLOCK(prog_idr_lock);
  45. static DEFINE_IDR(map_idr);
  46. static DEFINE_SPINLOCK(map_idr_lock);
  47. int sysctl_unprivileged_bpf_disabled __read_mostly;
  48. static const struct bpf_map_ops * const bpf_map_types[] = {
  49. #define BPF_PROG_TYPE(_id, _ops)
  50. #define BPF_MAP_TYPE(_id, _ops) \
  51. [_id] = &_ops,
  52. #include <linux/bpf_types.h>
  53. #undef BPF_PROG_TYPE
  54. #undef BPF_MAP_TYPE
  55. };
  56. /*
  57. * If we're handed a bigger struct than we know of, ensure all the unknown bits
  58. * are 0 - i.e. new user-space does not rely on any kernel feature extensions
  59. * we don't know about yet.
  60. *
  61. * There is a ToCToU between this function call and the following
  62. * copy_from_user() call. However, this is not a concern since this function is
  63. * meant to be a future-proofing of bits.
  64. */
  65. int bpf_check_uarg_tail_zero(void __user *uaddr,
  66. size_t expected_size,
  67. size_t actual_size)
  68. {
  69. unsigned char __user *addr;
  70. unsigned char __user *end;
  71. unsigned char val;
  72. int err;
  73. if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
  74. return -E2BIG;
  75. if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
  76. return -EFAULT;
  77. if (actual_size <= expected_size)
  78. return 0;
  79. addr = uaddr + expected_size;
  80. end = uaddr + actual_size;
  81. for (; addr < end; addr++) {
  82. err = get_user(val, addr);
  83. if (err)
  84. return err;
  85. if (val)
  86. return -E2BIG;
  87. }
  88. return 0;
  89. }
  90. const struct bpf_map_ops bpf_map_offload_ops = {
  91. .map_alloc = bpf_map_offload_map_alloc,
  92. .map_free = bpf_map_offload_map_free,
  93. .map_check_btf = map_check_no_btf,
  94. };
  95. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  96. {
  97. const struct bpf_map_ops *ops;
  98. u32 type = attr->map_type;
  99. struct bpf_map *map;
  100. int err;
  101. if (type >= ARRAY_SIZE(bpf_map_types))
  102. return ERR_PTR(-EINVAL);
  103. type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
  104. ops = bpf_map_types[type];
  105. if (!ops)
  106. return ERR_PTR(-EINVAL);
  107. if (ops->map_alloc_check) {
  108. err = ops->map_alloc_check(attr);
  109. if (err)
  110. return ERR_PTR(err);
  111. }
  112. if (attr->map_ifindex)
  113. ops = &bpf_map_offload_ops;
  114. map = ops->map_alloc(attr);
  115. if (IS_ERR(map))
  116. return map;
  117. map->ops = ops;
  118. map->map_type = type;
  119. return map;
  120. }
  121. void *bpf_map_area_alloc(size_t size, int numa_node)
  122. {
  123. /* We definitely need __GFP_NORETRY, so OOM killer doesn't
  124. * trigger under memory pressure as we really just want to
  125. * fail instead.
  126. */
  127. const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
  128. void *area;
  129. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  130. area = kmalloc_node(size, GFP_USER | flags, numa_node);
  131. if (area != NULL)
  132. return area;
  133. }
  134. return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
  135. __builtin_return_address(0));
  136. }
  137. void bpf_map_area_free(void *area)
  138. {
  139. kvfree(area);
  140. }
  141. void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
  142. {
  143. map->map_type = attr->map_type;
  144. map->key_size = attr->key_size;
  145. map->value_size = attr->value_size;
  146. map->max_entries = attr->max_entries;
  147. map->map_flags = attr->map_flags;
  148. map->numa_node = bpf_map_attr_numa_node(attr);
  149. }
  150. int bpf_map_precharge_memlock(u32 pages)
  151. {
  152. struct user_struct *user = get_current_user();
  153. unsigned long memlock_limit, cur;
  154. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  155. cur = atomic_long_read(&user->locked_vm);
  156. free_uid(user);
  157. if (cur + pages > memlock_limit)
  158. return -EPERM;
  159. return 0;
  160. }
  161. static int bpf_charge_memlock(struct user_struct *user, u32 pages)
  162. {
  163. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  164. if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
  165. atomic_long_sub(pages, &user->locked_vm);
  166. return -EPERM;
  167. }
  168. return 0;
  169. }
  170. static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
  171. {
  172. atomic_long_sub(pages, &user->locked_vm);
  173. }
  174. static int bpf_map_init_memlock(struct bpf_map *map)
  175. {
  176. struct user_struct *user = get_current_user();
  177. int ret;
  178. ret = bpf_charge_memlock(user, map->pages);
  179. if (ret) {
  180. free_uid(user);
  181. return ret;
  182. }
  183. map->user = user;
  184. return ret;
  185. }
  186. static void bpf_map_release_memlock(struct bpf_map *map)
  187. {
  188. struct user_struct *user = map->user;
  189. bpf_uncharge_memlock(user, map->pages);
  190. free_uid(user);
  191. }
  192. int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
  193. {
  194. int ret;
  195. ret = bpf_charge_memlock(map->user, pages);
  196. if (ret)
  197. return ret;
  198. map->pages += pages;
  199. return ret;
  200. }
  201. void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
  202. {
  203. bpf_uncharge_memlock(map->user, pages);
  204. map->pages -= pages;
  205. }
  206. static int bpf_map_alloc_id(struct bpf_map *map)
  207. {
  208. int id;
  209. idr_preload(GFP_KERNEL);
  210. spin_lock_bh(&map_idr_lock);
  211. id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
  212. if (id > 0)
  213. map->id = id;
  214. spin_unlock_bh(&map_idr_lock);
  215. idr_preload_end();
  216. if (WARN_ON_ONCE(!id))
  217. return -ENOSPC;
  218. return id > 0 ? 0 : id;
  219. }
  220. void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
  221. {
  222. unsigned long flags;
  223. /* Offloaded maps are removed from the IDR store when their device
  224. * disappears - even if someone holds an fd to them they are unusable,
  225. * the memory is gone, all ops will fail; they are simply waiting for
  226. * refcnt to drop to be freed.
  227. */
  228. if (!map->id)
  229. return;
  230. if (do_idr_lock)
  231. spin_lock_irqsave(&map_idr_lock, flags);
  232. else
  233. __acquire(&map_idr_lock);
  234. idr_remove(&map_idr, map->id);
  235. map->id = 0;
  236. if (do_idr_lock)
  237. spin_unlock_irqrestore(&map_idr_lock, flags);
  238. else
  239. __release(&map_idr_lock);
  240. }
  241. /* called from workqueue */
  242. static void bpf_map_free_deferred(struct work_struct *work)
  243. {
  244. struct bpf_map *map = container_of(work, struct bpf_map, work);
  245. bpf_map_release_memlock(map);
  246. security_bpf_map_free(map);
  247. /* implementation dependent freeing */
  248. map->ops->map_free(map);
  249. }
  250. static void bpf_map_put_uref(struct bpf_map *map)
  251. {
  252. if (atomic_dec_and_test(&map->usercnt)) {
  253. if (map->ops->map_release_uref)
  254. map->ops->map_release_uref(map);
  255. }
  256. }
  257. /* decrement map refcnt and schedule it for freeing via workqueue
  258. * (unrelying map implementation ops->map_free() might sleep)
  259. */
  260. static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
  261. {
  262. if (atomic_dec_and_test(&map->refcnt)) {
  263. /* bpf_map_free_id() must be called first */
  264. bpf_map_free_id(map, do_idr_lock);
  265. btf_put(map->btf);
  266. INIT_WORK(&map->work, bpf_map_free_deferred);
  267. schedule_work(&map->work);
  268. }
  269. }
  270. void bpf_map_put(struct bpf_map *map)
  271. {
  272. __bpf_map_put(map, true);
  273. }
  274. EXPORT_SYMBOL_GPL(bpf_map_put);
  275. void bpf_map_put_with_uref(struct bpf_map *map)
  276. {
  277. bpf_map_put_uref(map);
  278. bpf_map_put(map);
  279. }
  280. static int bpf_map_release(struct inode *inode, struct file *filp)
  281. {
  282. struct bpf_map *map = filp->private_data;
  283. if (map->ops->map_release)
  284. map->ops->map_release(map, filp);
  285. bpf_map_put_with_uref(map);
  286. return 0;
  287. }
  288. #ifdef CONFIG_PROC_FS
  289. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  290. {
  291. const struct bpf_map *map = filp->private_data;
  292. const struct bpf_array *array;
  293. u32 owner_prog_type = 0;
  294. u32 owner_jited = 0;
  295. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
  296. array = container_of(map, struct bpf_array, map);
  297. owner_prog_type = array->owner_prog_type;
  298. owner_jited = array->owner_jited;
  299. }
  300. seq_printf(m,
  301. "map_type:\t%u\n"
  302. "key_size:\t%u\n"
  303. "value_size:\t%u\n"
  304. "max_entries:\t%u\n"
  305. "map_flags:\t%#x\n"
  306. "memlock:\t%llu\n"
  307. "map_id:\t%u\n",
  308. map->map_type,
  309. map->key_size,
  310. map->value_size,
  311. map->max_entries,
  312. map->map_flags,
  313. map->pages * 1ULL << PAGE_SHIFT,
  314. map->id);
  315. if (owner_prog_type) {
  316. seq_printf(m, "owner_prog_type:\t%u\n",
  317. owner_prog_type);
  318. seq_printf(m, "owner_jited:\t%u\n",
  319. owner_jited);
  320. }
  321. }
  322. #endif
  323. static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
  324. loff_t *ppos)
  325. {
  326. /* We need this handler such that alloc_file() enables
  327. * f_mode with FMODE_CAN_READ.
  328. */
  329. return -EINVAL;
  330. }
  331. static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
  332. size_t siz, loff_t *ppos)
  333. {
  334. /* We need this handler such that alloc_file() enables
  335. * f_mode with FMODE_CAN_WRITE.
  336. */
  337. return -EINVAL;
  338. }
  339. const struct file_operations bpf_map_fops = {
  340. #ifdef CONFIG_PROC_FS
  341. .show_fdinfo = bpf_map_show_fdinfo,
  342. #endif
  343. .release = bpf_map_release,
  344. .read = bpf_dummy_read,
  345. .write = bpf_dummy_write,
  346. };
  347. int bpf_map_new_fd(struct bpf_map *map, int flags)
  348. {
  349. int ret;
  350. ret = security_bpf_map(map, OPEN_FMODE(flags));
  351. if (ret < 0)
  352. return ret;
  353. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  354. flags | O_CLOEXEC);
  355. }
  356. int bpf_get_file_flag(int flags)
  357. {
  358. if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
  359. return -EINVAL;
  360. if (flags & BPF_F_RDONLY)
  361. return O_RDONLY;
  362. if (flags & BPF_F_WRONLY)
  363. return O_WRONLY;
  364. return O_RDWR;
  365. }
  366. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  367. #define CHECK_ATTR(CMD) \
  368. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  369. sizeof(attr->CMD##_LAST_FIELD), 0, \
  370. sizeof(*attr) - \
  371. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  372. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  373. /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
  374. * Return 0 on success and < 0 on error.
  375. */
  376. static int bpf_obj_name_cpy(char *dst, const char *src)
  377. {
  378. const char *end = src + BPF_OBJ_NAME_LEN;
  379. memset(dst, 0, BPF_OBJ_NAME_LEN);
  380. /* Copy all isalnum() and '_' char */
  381. while (src < end && *src) {
  382. if (!isalnum(*src) && *src != '_')
  383. return -EINVAL;
  384. *dst++ = *src++;
  385. }
  386. /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
  387. if (src == end)
  388. return -EINVAL;
  389. return 0;
  390. }
  391. int map_check_no_btf(const struct bpf_map *map,
  392. const struct btf_type *key_type,
  393. const struct btf_type *value_type)
  394. {
  395. return -ENOTSUPP;
  396. }
  397. static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
  398. u32 btf_key_id, u32 btf_value_id)
  399. {
  400. const struct btf_type *key_type, *value_type;
  401. u32 key_size, value_size;
  402. int ret = 0;
  403. key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
  404. if (!key_type || key_size != map->key_size)
  405. return -EINVAL;
  406. value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
  407. if (!value_type || value_size != map->value_size)
  408. return -EINVAL;
  409. if (map->ops->map_check_btf)
  410. ret = map->ops->map_check_btf(map, key_type, value_type);
  411. return ret;
  412. }
  413. #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
  414. /* called via syscall */
  415. static int map_create(union bpf_attr *attr)
  416. {
  417. int numa_node = bpf_map_attr_numa_node(attr);
  418. struct bpf_map *map;
  419. int f_flags;
  420. int err;
  421. err = CHECK_ATTR(BPF_MAP_CREATE);
  422. if (err)
  423. return -EINVAL;
  424. f_flags = bpf_get_file_flag(attr->map_flags);
  425. if (f_flags < 0)
  426. return f_flags;
  427. if (numa_node != NUMA_NO_NODE &&
  428. ((unsigned int)numa_node >= nr_node_ids ||
  429. !node_online(numa_node)))
  430. return -EINVAL;
  431. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  432. map = find_and_alloc_map(attr);
  433. if (IS_ERR(map))
  434. return PTR_ERR(map);
  435. err = bpf_obj_name_cpy(map->name, attr->map_name);
  436. if (err)
  437. goto free_map_nouncharge;
  438. atomic_set(&map->refcnt, 1);
  439. atomic_set(&map->usercnt, 1);
  440. if (attr->btf_key_type_id || attr->btf_value_type_id) {
  441. struct btf *btf;
  442. if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
  443. err = -EINVAL;
  444. goto free_map_nouncharge;
  445. }
  446. btf = btf_get_by_fd(attr->btf_fd);
  447. if (IS_ERR(btf)) {
  448. err = PTR_ERR(btf);
  449. goto free_map_nouncharge;
  450. }
  451. err = map_check_btf(map, btf, attr->btf_key_type_id,
  452. attr->btf_value_type_id);
  453. if (err) {
  454. btf_put(btf);
  455. goto free_map_nouncharge;
  456. }
  457. map->btf = btf;
  458. map->btf_key_type_id = attr->btf_key_type_id;
  459. map->btf_value_type_id = attr->btf_value_type_id;
  460. }
  461. err = security_bpf_map_alloc(map);
  462. if (err)
  463. goto free_map_nouncharge;
  464. err = bpf_map_init_memlock(map);
  465. if (err)
  466. goto free_map_sec;
  467. err = bpf_map_alloc_id(map);
  468. if (err)
  469. goto free_map;
  470. err = bpf_map_new_fd(map, f_flags);
  471. if (err < 0) {
  472. /* failed to allocate fd.
  473. * bpf_map_put_with_uref() is needed because the above
  474. * bpf_map_alloc_id() has published the map
  475. * to the userspace and the userspace may
  476. * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
  477. */
  478. bpf_map_put_with_uref(map);
  479. return err;
  480. }
  481. return err;
  482. free_map:
  483. bpf_map_release_memlock(map);
  484. free_map_sec:
  485. security_bpf_map_free(map);
  486. free_map_nouncharge:
  487. btf_put(map->btf);
  488. map->ops->map_free(map);
  489. return err;
  490. }
  491. /* if error is returned, fd is released.
  492. * On success caller should complete fd access with matching fdput()
  493. */
  494. struct bpf_map *__bpf_map_get(struct fd f)
  495. {
  496. if (!f.file)
  497. return ERR_PTR(-EBADF);
  498. if (f.file->f_op != &bpf_map_fops) {
  499. fdput(f);
  500. return ERR_PTR(-EINVAL);
  501. }
  502. return f.file->private_data;
  503. }
  504. /* prog's and map's refcnt limit */
  505. #define BPF_MAX_REFCNT 32768
  506. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  507. {
  508. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  509. atomic_dec(&map->refcnt);
  510. return ERR_PTR(-EBUSY);
  511. }
  512. if (uref)
  513. atomic_inc(&map->usercnt);
  514. return map;
  515. }
  516. EXPORT_SYMBOL_GPL(bpf_map_inc);
  517. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  518. {
  519. struct fd f = fdget(ufd);
  520. struct bpf_map *map;
  521. map = __bpf_map_get(f);
  522. if (IS_ERR(map))
  523. return map;
  524. map = bpf_map_inc(map, true);
  525. fdput(f);
  526. return map;
  527. }
  528. /* map_idr_lock should have been held */
  529. static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
  530. bool uref)
  531. {
  532. int refold;
  533. refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
  534. if (refold >= BPF_MAX_REFCNT) {
  535. __bpf_map_put(map, false);
  536. return ERR_PTR(-EBUSY);
  537. }
  538. if (!refold)
  539. return ERR_PTR(-ENOENT);
  540. if (uref)
  541. atomic_inc(&map->usercnt);
  542. return map;
  543. }
  544. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  545. {
  546. return -ENOTSUPP;
  547. }
  548. /* last field in 'union bpf_attr' used by this command */
  549. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  550. static int map_lookup_elem(union bpf_attr *attr)
  551. {
  552. void __user *ukey = u64_to_user_ptr(attr->key);
  553. void __user *uvalue = u64_to_user_ptr(attr->value);
  554. int ufd = attr->map_fd;
  555. struct bpf_map *map;
  556. void *key, *value, *ptr;
  557. u32 value_size;
  558. struct fd f;
  559. int err;
  560. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  561. return -EINVAL;
  562. f = fdget(ufd);
  563. map = __bpf_map_get(f);
  564. if (IS_ERR(map))
  565. return PTR_ERR(map);
  566. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  567. err = -EPERM;
  568. goto err_put;
  569. }
  570. key = memdup_user(ukey, map->key_size);
  571. if (IS_ERR(key)) {
  572. err = PTR_ERR(key);
  573. goto err_put;
  574. }
  575. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  576. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  577. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  578. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  579. else if (IS_FD_MAP(map))
  580. value_size = sizeof(u32);
  581. else
  582. value_size = map->value_size;
  583. err = -ENOMEM;
  584. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  585. if (!value)
  586. goto free_key;
  587. if (bpf_map_is_dev_bound(map)) {
  588. err = bpf_map_offload_lookup_elem(map, key, value);
  589. goto done;
  590. }
  591. preempt_disable();
  592. this_cpu_inc(bpf_prog_active);
  593. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  594. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  595. err = bpf_percpu_hash_copy(map, key, value);
  596. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  597. err = bpf_percpu_array_copy(map, key, value);
  598. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  599. err = bpf_stackmap_copy(map, key, value);
  600. } else if (IS_FD_ARRAY(map)) {
  601. err = bpf_fd_array_map_lookup_elem(map, key, value);
  602. } else if (IS_FD_HASH(map)) {
  603. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  604. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  605. err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
  606. } else {
  607. rcu_read_lock();
  608. if (map->ops->map_lookup_elem_sys_only)
  609. ptr = map->ops->map_lookup_elem_sys_only(map, key);
  610. else
  611. ptr = map->ops->map_lookup_elem(map, key);
  612. if (ptr)
  613. memcpy(value, ptr, value_size);
  614. rcu_read_unlock();
  615. err = ptr ? 0 : -ENOENT;
  616. }
  617. this_cpu_dec(bpf_prog_active);
  618. preempt_enable();
  619. done:
  620. if (err)
  621. goto free_value;
  622. err = -EFAULT;
  623. if (copy_to_user(uvalue, value, value_size) != 0)
  624. goto free_value;
  625. err = 0;
  626. free_value:
  627. kfree(value);
  628. free_key:
  629. kfree(key);
  630. err_put:
  631. fdput(f);
  632. return err;
  633. }
  634. static void maybe_wait_bpf_programs(struct bpf_map *map)
  635. {
  636. /* Wait for any running BPF programs to complete so that
  637. * userspace, when we return to it, knows that all programs
  638. * that could be running use the new map value.
  639. */
  640. if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
  641. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  642. synchronize_rcu();
  643. }
  644. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  645. static int map_update_elem(union bpf_attr *attr)
  646. {
  647. void __user *ukey = u64_to_user_ptr(attr->key);
  648. void __user *uvalue = u64_to_user_ptr(attr->value);
  649. int ufd = attr->map_fd;
  650. struct bpf_map *map;
  651. void *key, *value;
  652. u32 value_size;
  653. struct fd f;
  654. int err;
  655. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  656. return -EINVAL;
  657. f = fdget(ufd);
  658. map = __bpf_map_get(f);
  659. if (IS_ERR(map))
  660. return PTR_ERR(map);
  661. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  662. err = -EPERM;
  663. goto err_put;
  664. }
  665. key = memdup_user(ukey, map->key_size);
  666. if (IS_ERR(key)) {
  667. err = PTR_ERR(key);
  668. goto err_put;
  669. }
  670. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  671. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  672. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  673. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  674. else
  675. value_size = map->value_size;
  676. err = -ENOMEM;
  677. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  678. if (!value)
  679. goto free_key;
  680. err = -EFAULT;
  681. if (copy_from_user(value, uvalue, value_size) != 0)
  682. goto free_value;
  683. /* Need to create a kthread, thus must support schedule */
  684. if (bpf_map_is_dev_bound(map)) {
  685. err = bpf_map_offload_update_elem(map, key, value, attr->flags);
  686. goto out;
  687. } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
  688. map->map_type == BPF_MAP_TYPE_SOCKHASH ||
  689. map->map_type == BPF_MAP_TYPE_SOCKMAP) {
  690. err = map->ops->map_update_elem(map, key, value, attr->flags);
  691. goto out;
  692. }
  693. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  694. * inside bpf map update or delete otherwise deadlocks are possible
  695. */
  696. preempt_disable();
  697. __this_cpu_inc(bpf_prog_active);
  698. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  699. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  700. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  701. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  702. err = bpf_percpu_array_update(map, key, value, attr->flags);
  703. } else if (IS_FD_ARRAY(map)) {
  704. rcu_read_lock();
  705. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  706. attr->flags);
  707. rcu_read_unlock();
  708. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  709. rcu_read_lock();
  710. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  711. attr->flags);
  712. rcu_read_unlock();
  713. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  714. /* rcu_read_lock() is not needed */
  715. err = bpf_fd_reuseport_array_update_elem(map, key, value,
  716. attr->flags);
  717. } else {
  718. rcu_read_lock();
  719. err = map->ops->map_update_elem(map, key, value, attr->flags);
  720. rcu_read_unlock();
  721. }
  722. __this_cpu_dec(bpf_prog_active);
  723. preempt_enable();
  724. maybe_wait_bpf_programs(map);
  725. out:
  726. free_value:
  727. kfree(value);
  728. free_key:
  729. kfree(key);
  730. err_put:
  731. fdput(f);
  732. return err;
  733. }
  734. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  735. static int map_delete_elem(union bpf_attr *attr)
  736. {
  737. void __user *ukey = u64_to_user_ptr(attr->key);
  738. int ufd = attr->map_fd;
  739. struct bpf_map *map;
  740. struct fd f;
  741. void *key;
  742. int err;
  743. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  744. return -EINVAL;
  745. f = fdget(ufd);
  746. map = __bpf_map_get(f);
  747. if (IS_ERR(map))
  748. return PTR_ERR(map);
  749. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  750. err = -EPERM;
  751. goto err_put;
  752. }
  753. key = memdup_user(ukey, map->key_size);
  754. if (IS_ERR(key)) {
  755. err = PTR_ERR(key);
  756. goto err_put;
  757. }
  758. if (bpf_map_is_dev_bound(map)) {
  759. err = bpf_map_offload_delete_elem(map, key);
  760. goto out;
  761. }
  762. preempt_disable();
  763. __this_cpu_inc(bpf_prog_active);
  764. rcu_read_lock();
  765. err = map->ops->map_delete_elem(map, key);
  766. rcu_read_unlock();
  767. __this_cpu_dec(bpf_prog_active);
  768. preempt_enable();
  769. maybe_wait_bpf_programs(map);
  770. out:
  771. kfree(key);
  772. err_put:
  773. fdput(f);
  774. return err;
  775. }
  776. /* last field in 'union bpf_attr' used by this command */
  777. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  778. static int map_get_next_key(union bpf_attr *attr)
  779. {
  780. void __user *ukey = u64_to_user_ptr(attr->key);
  781. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  782. int ufd = attr->map_fd;
  783. struct bpf_map *map;
  784. void *key, *next_key;
  785. struct fd f;
  786. int err;
  787. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  788. return -EINVAL;
  789. f = fdget(ufd);
  790. map = __bpf_map_get(f);
  791. if (IS_ERR(map))
  792. return PTR_ERR(map);
  793. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  794. err = -EPERM;
  795. goto err_put;
  796. }
  797. if (ukey) {
  798. key = memdup_user(ukey, map->key_size);
  799. if (IS_ERR(key)) {
  800. err = PTR_ERR(key);
  801. goto err_put;
  802. }
  803. } else {
  804. key = NULL;
  805. }
  806. err = -ENOMEM;
  807. next_key = kmalloc(map->key_size, GFP_USER);
  808. if (!next_key)
  809. goto free_key;
  810. if (bpf_map_is_dev_bound(map)) {
  811. err = bpf_map_offload_get_next_key(map, key, next_key);
  812. goto out;
  813. }
  814. rcu_read_lock();
  815. err = map->ops->map_get_next_key(map, key, next_key);
  816. rcu_read_unlock();
  817. out:
  818. if (err)
  819. goto free_next_key;
  820. err = -EFAULT;
  821. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  822. goto free_next_key;
  823. err = 0;
  824. free_next_key:
  825. kfree(next_key);
  826. free_key:
  827. kfree(key);
  828. err_put:
  829. fdput(f);
  830. return err;
  831. }
  832. static const struct bpf_prog_ops * const bpf_prog_types[] = {
  833. #define BPF_PROG_TYPE(_id, _name) \
  834. [_id] = & _name ## _prog_ops,
  835. #define BPF_MAP_TYPE(_id, _ops)
  836. #include <linux/bpf_types.h>
  837. #undef BPF_PROG_TYPE
  838. #undef BPF_MAP_TYPE
  839. };
  840. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  841. {
  842. const struct bpf_prog_ops *ops;
  843. if (type >= ARRAY_SIZE(bpf_prog_types))
  844. return -EINVAL;
  845. type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
  846. ops = bpf_prog_types[type];
  847. if (!ops)
  848. return -EINVAL;
  849. if (!bpf_prog_is_dev_bound(prog->aux))
  850. prog->aux->ops = ops;
  851. else
  852. prog->aux->ops = &bpf_offload_prog_ops;
  853. prog->type = type;
  854. return 0;
  855. }
  856. /* drop refcnt on maps used by eBPF program and free auxilary data */
  857. static void free_used_maps(struct bpf_prog_aux *aux)
  858. {
  859. int i;
  860. if (aux->cgroup_storage)
  861. bpf_cgroup_storage_release(aux->prog, aux->cgroup_storage);
  862. for (i = 0; i < aux->used_map_cnt; i++)
  863. bpf_map_put(aux->used_maps[i]);
  864. kfree(aux->used_maps);
  865. }
  866. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  867. {
  868. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  869. unsigned long user_bufs;
  870. if (user) {
  871. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  872. if (user_bufs > memlock_limit) {
  873. atomic_long_sub(pages, &user->locked_vm);
  874. return -EPERM;
  875. }
  876. }
  877. return 0;
  878. }
  879. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  880. {
  881. if (user)
  882. atomic_long_sub(pages, &user->locked_vm);
  883. }
  884. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  885. {
  886. struct user_struct *user = get_current_user();
  887. int ret;
  888. ret = __bpf_prog_charge(user, prog->pages);
  889. if (ret) {
  890. free_uid(user);
  891. return ret;
  892. }
  893. prog->aux->user = user;
  894. return 0;
  895. }
  896. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  897. {
  898. struct user_struct *user = prog->aux->user;
  899. __bpf_prog_uncharge(user, prog->pages);
  900. free_uid(user);
  901. }
  902. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  903. {
  904. int id;
  905. idr_preload(GFP_KERNEL);
  906. spin_lock_bh(&prog_idr_lock);
  907. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  908. if (id > 0)
  909. prog->aux->id = id;
  910. spin_unlock_bh(&prog_idr_lock);
  911. idr_preload_end();
  912. /* id is in [1, INT_MAX) */
  913. if (WARN_ON_ONCE(!id))
  914. return -ENOSPC;
  915. return id > 0 ? 0 : id;
  916. }
  917. void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  918. {
  919. /* cBPF to eBPF migrations are currently not in the idr store.
  920. * Offloaded programs are removed from the store when their device
  921. * disappears - even if someone grabs an fd to them they are unusable,
  922. * simply waiting for refcnt to drop to be freed.
  923. */
  924. if (!prog->aux->id)
  925. return;
  926. if (do_idr_lock)
  927. spin_lock_bh(&prog_idr_lock);
  928. else
  929. __acquire(&prog_idr_lock);
  930. idr_remove(&prog_idr, prog->aux->id);
  931. prog->aux->id = 0;
  932. if (do_idr_lock)
  933. spin_unlock_bh(&prog_idr_lock);
  934. else
  935. __release(&prog_idr_lock);
  936. }
  937. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  938. {
  939. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  940. free_used_maps(aux);
  941. bpf_prog_uncharge_memlock(aux->prog);
  942. security_bpf_prog_free(aux);
  943. bpf_prog_free(aux->prog);
  944. }
  945. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  946. {
  947. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  948. /* bpf_prog_free_id() must be called first */
  949. bpf_prog_free_id(prog, do_idr_lock);
  950. bpf_prog_kallsyms_del_all(prog);
  951. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  952. }
  953. }
  954. void bpf_prog_put(struct bpf_prog *prog)
  955. {
  956. __bpf_prog_put(prog, true);
  957. }
  958. EXPORT_SYMBOL_GPL(bpf_prog_put);
  959. static int bpf_prog_release(struct inode *inode, struct file *filp)
  960. {
  961. struct bpf_prog *prog = filp->private_data;
  962. bpf_prog_put(prog);
  963. return 0;
  964. }
  965. #ifdef CONFIG_PROC_FS
  966. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  967. {
  968. const struct bpf_prog *prog = filp->private_data;
  969. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  970. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  971. seq_printf(m,
  972. "prog_type:\t%u\n"
  973. "prog_jited:\t%u\n"
  974. "prog_tag:\t%s\n"
  975. "memlock:\t%llu\n"
  976. "prog_id:\t%u\n",
  977. prog->type,
  978. prog->jited,
  979. prog_tag,
  980. prog->pages * 1ULL << PAGE_SHIFT,
  981. prog->aux->id);
  982. }
  983. #endif
  984. const struct file_operations bpf_prog_fops = {
  985. #ifdef CONFIG_PROC_FS
  986. .show_fdinfo = bpf_prog_show_fdinfo,
  987. #endif
  988. .release = bpf_prog_release,
  989. .read = bpf_dummy_read,
  990. .write = bpf_dummy_write,
  991. };
  992. int bpf_prog_new_fd(struct bpf_prog *prog)
  993. {
  994. int ret;
  995. ret = security_bpf_prog(prog);
  996. if (ret < 0)
  997. return ret;
  998. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  999. O_RDWR | O_CLOEXEC);
  1000. }
  1001. static struct bpf_prog *____bpf_prog_get(struct fd f)
  1002. {
  1003. if (!f.file)
  1004. return ERR_PTR(-EBADF);
  1005. if (f.file->f_op != &bpf_prog_fops) {
  1006. fdput(f);
  1007. return ERR_PTR(-EINVAL);
  1008. }
  1009. return f.file->private_data;
  1010. }
  1011. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  1012. {
  1013. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  1014. atomic_sub(i, &prog->aux->refcnt);
  1015. return ERR_PTR(-EBUSY);
  1016. }
  1017. return prog;
  1018. }
  1019. EXPORT_SYMBOL_GPL(bpf_prog_add);
  1020. void bpf_prog_sub(struct bpf_prog *prog, int i)
  1021. {
  1022. /* Only to be used for undoing previous bpf_prog_add() in some
  1023. * error path. We still know that another entity in our call
  1024. * path holds a reference to the program, thus atomic_sub() can
  1025. * be safely used in such cases!
  1026. */
  1027. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  1028. }
  1029. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  1030. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  1031. {
  1032. return bpf_prog_add(prog, 1);
  1033. }
  1034. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  1035. /* prog_idr_lock should have been held */
  1036. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  1037. {
  1038. int refold;
  1039. refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
  1040. if (refold >= BPF_MAX_REFCNT) {
  1041. __bpf_prog_put(prog, false);
  1042. return ERR_PTR(-EBUSY);
  1043. }
  1044. if (!refold)
  1045. return ERR_PTR(-ENOENT);
  1046. return prog;
  1047. }
  1048. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  1049. bool bpf_prog_get_ok(struct bpf_prog *prog,
  1050. enum bpf_prog_type *attach_type, bool attach_drv)
  1051. {
  1052. /* not an attachment, just a refcount inc, always allow */
  1053. if (!attach_type)
  1054. return true;
  1055. if (prog->type != *attach_type)
  1056. return false;
  1057. if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
  1058. return false;
  1059. return true;
  1060. }
  1061. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
  1062. bool attach_drv)
  1063. {
  1064. struct fd f = fdget(ufd);
  1065. struct bpf_prog *prog;
  1066. prog = ____bpf_prog_get(f);
  1067. if (IS_ERR(prog))
  1068. return prog;
  1069. if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
  1070. prog = ERR_PTR(-EINVAL);
  1071. goto out;
  1072. }
  1073. prog = bpf_prog_inc(prog);
  1074. out:
  1075. fdput(f);
  1076. return prog;
  1077. }
  1078. struct bpf_prog *bpf_prog_get(u32 ufd)
  1079. {
  1080. return __bpf_prog_get(ufd, NULL, false);
  1081. }
  1082. struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
  1083. bool attach_drv)
  1084. {
  1085. return __bpf_prog_get(ufd, &type, attach_drv);
  1086. }
  1087. EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
  1088. /* Initially all BPF programs could be loaded w/o specifying
  1089. * expected_attach_type. Later for some of them specifying expected_attach_type
  1090. * at load time became required so that program could be validated properly.
  1091. * Programs of types that are allowed to be loaded both w/ and w/o (for
  1092. * backward compatibility) expected_attach_type, should have the default attach
  1093. * type assigned to expected_attach_type for the latter case, so that it can be
  1094. * validated later at attach time.
  1095. *
  1096. * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
  1097. * prog type requires it but has some attach types that have to be backward
  1098. * compatible.
  1099. */
  1100. static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
  1101. {
  1102. switch (attr->prog_type) {
  1103. case BPF_PROG_TYPE_CGROUP_SOCK:
  1104. /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
  1105. * exist so checking for non-zero is the way to go here.
  1106. */
  1107. if (!attr->expected_attach_type)
  1108. attr->expected_attach_type =
  1109. BPF_CGROUP_INET_SOCK_CREATE;
  1110. break;
  1111. }
  1112. }
  1113. static int
  1114. bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
  1115. enum bpf_attach_type expected_attach_type)
  1116. {
  1117. switch (prog_type) {
  1118. case BPF_PROG_TYPE_CGROUP_SOCK:
  1119. switch (expected_attach_type) {
  1120. case BPF_CGROUP_INET_SOCK_CREATE:
  1121. case BPF_CGROUP_INET4_POST_BIND:
  1122. case BPF_CGROUP_INET6_POST_BIND:
  1123. return 0;
  1124. default:
  1125. return -EINVAL;
  1126. }
  1127. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  1128. switch (expected_attach_type) {
  1129. case BPF_CGROUP_INET4_BIND:
  1130. case BPF_CGROUP_INET6_BIND:
  1131. case BPF_CGROUP_INET4_CONNECT:
  1132. case BPF_CGROUP_INET6_CONNECT:
  1133. case BPF_CGROUP_UDP4_SENDMSG:
  1134. case BPF_CGROUP_UDP6_SENDMSG:
  1135. case BPF_CGROUP_UDP4_RECVMSG:
  1136. case BPF_CGROUP_UDP6_RECVMSG:
  1137. return 0;
  1138. default:
  1139. return -EINVAL;
  1140. }
  1141. default:
  1142. return 0;
  1143. }
  1144. }
  1145. /* last field in 'union bpf_attr' used by this command */
  1146. #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
  1147. static int bpf_prog_load(union bpf_attr *attr)
  1148. {
  1149. enum bpf_prog_type type = attr->prog_type;
  1150. struct bpf_prog *prog;
  1151. int err;
  1152. char license[128];
  1153. bool is_gpl;
  1154. if (CHECK_ATTR(BPF_PROG_LOAD))
  1155. return -EINVAL;
  1156. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  1157. return -EINVAL;
  1158. /* copy eBPF program license from user space */
  1159. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  1160. sizeof(license) - 1) < 0)
  1161. return -EFAULT;
  1162. license[sizeof(license) - 1] = 0;
  1163. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1164. is_gpl = license_is_gpl_compatible(license);
  1165. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  1166. return -E2BIG;
  1167. if (type == BPF_PROG_TYPE_KPROBE &&
  1168. attr->kern_version != LINUX_VERSION_CODE)
  1169. return -EINVAL;
  1170. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  1171. type != BPF_PROG_TYPE_CGROUP_SKB &&
  1172. !capable(CAP_SYS_ADMIN))
  1173. return -EPERM;
  1174. bpf_prog_load_fixup_attach_type(attr);
  1175. if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
  1176. return -EINVAL;
  1177. /* plain bpf_prog allocation */
  1178. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  1179. if (!prog)
  1180. return -ENOMEM;
  1181. prog->expected_attach_type = attr->expected_attach_type;
  1182. prog->aux->offload_requested = !!attr->prog_ifindex;
  1183. err = security_bpf_prog_alloc(prog->aux);
  1184. if (err)
  1185. goto free_prog_nouncharge;
  1186. err = bpf_prog_charge_memlock(prog);
  1187. if (err)
  1188. goto free_prog_sec;
  1189. prog->len = attr->insn_cnt;
  1190. err = -EFAULT;
  1191. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  1192. bpf_prog_insn_size(prog)) != 0)
  1193. goto free_prog;
  1194. prog->orig_prog = NULL;
  1195. prog->jited = 0;
  1196. atomic_set(&prog->aux->refcnt, 1);
  1197. prog->gpl_compatible = is_gpl ? 1 : 0;
  1198. if (bpf_prog_is_dev_bound(prog->aux)) {
  1199. err = bpf_prog_offload_init(prog, attr);
  1200. if (err)
  1201. goto free_prog;
  1202. }
  1203. /* find program type: socket_filter vs tracing_filter */
  1204. err = find_prog_type(type, prog);
  1205. if (err < 0)
  1206. goto free_prog;
  1207. prog->aux->load_time = ktime_get_boot_ns();
  1208. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
  1209. if (err)
  1210. goto free_prog;
  1211. /* run eBPF verifier */
  1212. err = bpf_check(&prog, attr);
  1213. if (err < 0)
  1214. goto free_used_maps;
  1215. prog = bpf_prog_select_runtime(prog, &err);
  1216. if (err < 0)
  1217. goto free_used_maps;
  1218. err = bpf_prog_alloc_id(prog);
  1219. if (err)
  1220. goto free_used_maps;
  1221. /* Upon success of bpf_prog_alloc_id(), the BPF prog is
  1222. * effectively publicly exposed. However, retrieving via
  1223. * bpf_prog_get_fd_by_id() will take another reference,
  1224. * therefore it cannot be gone underneath us.
  1225. *
  1226. * Only for the time /after/ successful bpf_prog_new_fd()
  1227. * and before returning to userspace, we might just hold
  1228. * one reference and any parallel close on that fd could
  1229. * rip everything out. Hence, below notifications must
  1230. * happen before bpf_prog_new_fd().
  1231. *
  1232. * Also, any failure handling from this point onwards must
  1233. * be using bpf_prog_put() given the program is exposed.
  1234. */
  1235. bpf_prog_kallsyms_add(prog);
  1236. err = bpf_prog_new_fd(prog);
  1237. if (err < 0)
  1238. bpf_prog_put(prog);
  1239. return err;
  1240. free_used_maps:
  1241. bpf_prog_kallsyms_del_subprogs(prog);
  1242. free_used_maps(prog->aux);
  1243. free_prog:
  1244. bpf_prog_uncharge_memlock(prog);
  1245. free_prog_sec:
  1246. security_bpf_prog_free(prog->aux);
  1247. free_prog_nouncharge:
  1248. bpf_prog_free(prog);
  1249. return err;
  1250. }
  1251. #define BPF_OBJ_LAST_FIELD file_flags
  1252. static int bpf_obj_pin(const union bpf_attr *attr)
  1253. {
  1254. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  1255. return -EINVAL;
  1256. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  1257. }
  1258. static int bpf_obj_get(const union bpf_attr *attr)
  1259. {
  1260. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  1261. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  1262. return -EINVAL;
  1263. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  1264. attr->file_flags);
  1265. }
  1266. struct bpf_raw_tracepoint {
  1267. struct bpf_raw_event_map *btp;
  1268. struct bpf_prog *prog;
  1269. };
  1270. static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
  1271. {
  1272. struct bpf_raw_tracepoint *raw_tp = filp->private_data;
  1273. if (raw_tp->prog) {
  1274. bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
  1275. bpf_prog_put(raw_tp->prog);
  1276. }
  1277. kfree(raw_tp);
  1278. return 0;
  1279. }
  1280. static const struct file_operations bpf_raw_tp_fops = {
  1281. .release = bpf_raw_tracepoint_release,
  1282. .read = bpf_dummy_read,
  1283. .write = bpf_dummy_write,
  1284. };
  1285. #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
  1286. static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
  1287. {
  1288. struct bpf_raw_tracepoint *raw_tp;
  1289. struct bpf_raw_event_map *btp;
  1290. struct bpf_prog *prog;
  1291. char tp_name[128];
  1292. int tp_fd, err;
  1293. if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
  1294. sizeof(tp_name) - 1) < 0)
  1295. return -EFAULT;
  1296. tp_name[sizeof(tp_name) - 1] = 0;
  1297. btp = bpf_find_raw_tracepoint(tp_name);
  1298. if (!btp)
  1299. return -ENOENT;
  1300. raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
  1301. if (!raw_tp)
  1302. return -ENOMEM;
  1303. raw_tp->btp = btp;
  1304. prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
  1305. BPF_PROG_TYPE_RAW_TRACEPOINT);
  1306. if (IS_ERR(prog)) {
  1307. err = PTR_ERR(prog);
  1308. goto out_free_tp;
  1309. }
  1310. err = bpf_probe_register(raw_tp->btp, prog);
  1311. if (err)
  1312. goto out_put_prog;
  1313. raw_tp->prog = prog;
  1314. tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
  1315. O_CLOEXEC);
  1316. if (tp_fd < 0) {
  1317. bpf_probe_unregister(raw_tp->btp, prog);
  1318. err = tp_fd;
  1319. goto out_put_prog;
  1320. }
  1321. return tp_fd;
  1322. out_put_prog:
  1323. bpf_prog_put(prog);
  1324. out_free_tp:
  1325. kfree(raw_tp);
  1326. return err;
  1327. }
  1328. static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
  1329. enum bpf_attach_type attach_type)
  1330. {
  1331. switch (prog->type) {
  1332. case BPF_PROG_TYPE_CGROUP_SOCK:
  1333. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  1334. return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
  1335. default:
  1336. return 0;
  1337. }
  1338. }
  1339. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  1340. #define BPF_F_ATTACH_MASK \
  1341. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
  1342. static int bpf_prog_attach(const union bpf_attr *attr)
  1343. {
  1344. enum bpf_prog_type ptype;
  1345. struct bpf_prog *prog;
  1346. int ret;
  1347. if (!capable(CAP_NET_ADMIN))
  1348. return -EPERM;
  1349. if (CHECK_ATTR(BPF_PROG_ATTACH))
  1350. return -EINVAL;
  1351. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  1352. return -EINVAL;
  1353. switch (attr->attach_type) {
  1354. case BPF_CGROUP_INET_INGRESS:
  1355. case BPF_CGROUP_INET_EGRESS:
  1356. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1357. break;
  1358. case BPF_CGROUP_INET_SOCK_CREATE:
  1359. case BPF_CGROUP_INET4_POST_BIND:
  1360. case BPF_CGROUP_INET6_POST_BIND:
  1361. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1362. break;
  1363. case BPF_CGROUP_INET4_BIND:
  1364. case BPF_CGROUP_INET6_BIND:
  1365. case BPF_CGROUP_INET4_CONNECT:
  1366. case BPF_CGROUP_INET6_CONNECT:
  1367. case BPF_CGROUP_UDP4_SENDMSG:
  1368. case BPF_CGROUP_UDP6_SENDMSG:
  1369. case BPF_CGROUP_UDP4_RECVMSG:
  1370. case BPF_CGROUP_UDP6_RECVMSG:
  1371. ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
  1372. break;
  1373. case BPF_CGROUP_SOCK_OPS:
  1374. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1375. break;
  1376. case BPF_CGROUP_DEVICE:
  1377. ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
  1378. break;
  1379. case BPF_SK_MSG_VERDICT:
  1380. ptype = BPF_PROG_TYPE_SK_MSG;
  1381. break;
  1382. case BPF_SK_SKB_STREAM_PARSER:
  1383. case BPF_SK_SKB_STREAM_VERDICT:
  1384. ptype = BPF_PROG_TYPE_SK_SKB;
  1385. break;
  1386. case BPF_LIRC_MODE2:
  1387. ptype = BPF_PROG_TYPE_LIRC_MODE2;
  1388. break;
  1389. default:
  1390. return -EINVAL;
  1391. }
  1392. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1393. if (IS_ERR(prog))
  1394. return PTR_ERR(prog);
  1395. if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
  1396. bpf_prog_put(prog);
  1397. return -EINVAL;
  1398. }
  1399. switch (ptype) {
  1400. case BPF_PROG_TYPE_SK_SKB:
  1401. case BPF_PROG_TYPE_SK_MSG:
  1402. ret = sockmap_get_from_fd(attr, ptype, prog);
  1403. break;
  1404. case BPF_PROG_TYPE_LIRC_MODE2:
  1405. ret = lirc_prog_attach(attr, prog);
  1406. break;
  1407. default:
  1408. ret = cgroup_bpf_prog_attach(attr, ptype, prog);
  1409. }
  1410. if (ret)
  1411. bpf_prog_put(prog);
  1412. return ret;
  1413. }
  1414. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1415. static int bpf_prog_detach(const union bpf_attr *attr)
  1416. {
  1417. enum bpf_prog_type ptype;
  1418. if (!capable(CAP_NET_ADMIN))
  1419. return -EPERM;
  1420. if (CHECK_ATTR(BPF_PROG_DETACH))
  1421. return -EINVAL;
  1422. switch (attr->attach_type) {
  1423. case BPF_CGROUP_INET_INGRESS:
  1424. case BPF_CGROUP_INET_EGRESS:
  1425. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1426. break;
  1427. case BPF_CGROUP_INET_SOCK_CREATE:
  1428. case BPF_CGROUP_INET4_POST_BIND:
  1429. case BPF_CGROUP_INET6_POST_BIND:
  1430. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1431. break;
  1432. case BPF_CGROUP_INET4_BIND:
  1433. case BPF_CGROUP_INET6_BIND:
  1434. case BPF_CGROUP_INET4_CONNECT:
  1435. case BPF_CGROUP_INET6_CONNECT:
  1436. case BPF_CGROUP_UDP4_SENDMSG:
  1437. case BPF_CGROUP_UDP6_SENDMSG:
  1438. case BPF_CGROUP_UDP4_RECVMSG:
  1439. case BPF_CGROUP_UDP6_RECVMSG:
  1440. ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
  1441. break;
  1442. case BPF_CGROUP_SOCK_OPS:
  1443. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1444. break;
  1445. case BPF_CGROUP_DEVICE:
  1446. ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
  1447. break;
  1448. case BPF_SK_MSG_VERDICT:
  1449. return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, NULL);
  1450. case BPF_SK_SKB_STREAM_PARSER:
  1451. case BPF_SK_SKB_STREAM_VERDICT:
  1452. return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, NULL);
  1453. case BPF_LIRC_MODE2:
  1454. return lirc_prog_detach(attr);
  1455. default:
  1456. return -EINVAL;
  1457. }
  1458. return cgroup_bpf_prog_detach(attr, ptype);
  1459. }
  1460. #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
  1461. static int bpf_prog_query(const union bpf_attr *attr,
  1462. union bpf_attr __user *uattr)
  1463. {
  1464. if (!capable(CAP_NET_ADMIN))
  1465. return -EPERM;
  1466. if (CHECK_ATTR(BPF_PROG_QUERY))
  1467. return -EINVAL;
  1468. if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
  1469. return -EINVAL;
  1470. switch (attr->query.attach_type) {
  1471. case BPF_CGROUP_INET_INGRESS:
  1472. case BPF_CGROUP_INET_EGRESS:
  1473. case BPF_CGROUP_INET_SOCK_CREATE:
  1474. case BPF_CGROUP_INET4_BIND:
  1475. case BPF_CGROUP_INET6_BIND:
  1476. case BPF_CGROUP_INET4_POST_BIND:
  1477. case BPF_CGROUP_INET6_POST_BIND:
  1478. case BPF_CGROUP_INET4_CONNECT:
  1479. case BPF_CGROUP_INET6_CONNECT:
  1480. case BPF_CGROUP_UDP4_SENDMSG:
  1481. case BPF_CGROUP_UDP6_SENDMSG:
  1482. case BPF_CGROUP_UDP4_RECVMSG:
  1483. case BPF_CGROUP_UDP6_RECVMSG:
  1484. case BPF_CGROUP_SOCK_OPS:
  1485. case BPF_CGROUP_DEVICE:
  1486. break;
  1487. case BPF_LIRC_MODE2:
  1488. return lirc_prog_query(attr, uattr);
  1489. default:
  1490. return -EINVAL;
  1491. }
  1492. return cgroup_bpf_prog_query(attr, uattr);
  1493. }
  1494. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1495. static int bpf_prog_test_run(const union bpf_attr *attr,
  1496. union bpf_attr __user *uattr)
  1497. {
  1498. struct bpf_prog *prog;
  1499. int ret = -ENOTSUPP;
  1500. if (!capable(CAP_SYS_ADMIN))
  1501. return -EPERM;
  1502. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1503. return -EINVAL;
  1504. prog = bpf_prog_get(attr->test.prog_fd);
  1505. if (IS_ERR(prog))
  1506. return PTR_ERR(prog);
  1507. if (prog->aux->ops->test_run)
  1508. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1509. bpf_prog_put(prog);
  1510. return ret;
  1511. }
  1512. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1513. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1514. union bpf_attr __user *uattr,
  1515. struct idr *idr,
  1516. spinlock_t *lock)
  1517. {
  1518. u32 next_id = attr->start_id;
  1519. int err = 0;
  1520. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1521. return -EINVAL;
  1522. if (!capable(CAP_SYS_ADMIN))
  1523. return -EPERM;
  1524. next_id++;
  1525. spin_lock_bh(lock);
  1526. if (!idr_get_next(idr, &next_id))
  1527. err = -ENOENT;
  1528. spin_unlock_bh(lock);
  1529. if (!err)
  1530. err = put_user(next_id, &uattr->next_id);
  1531. return err;
  1532. }
  1533. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1534. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1535. {
  1536. struct bpf_prog *prog;
  1537. u32 id = attr->prog_id;
  1538. int fd;
  1539. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1540. return -EINVAL;
  1541. if (!capable(CAP_SYS_ADMIN))
  1542. return -EPERM;
  1543. spin_lock_bh(&prog_idr_lock);
  1544. prog = idr_find(&prog_idr, id);
  1545. if (prog)
  1546. prog = bpf_prog_inc_not_zero(prog);
  1547. else
  1548. prog = ERR_PTR(-ENOENT);
  1549. spin_unlock_bh(&prog_idr_lock);
  1550. if (IS_ERR(prog))
  1551. return PTR_ERR(prog);
  1552. fd = bpf_prog_new_fd(prog);
  1553. if (fd < 0)
  1554. bpf_prog_put(prog);
  1555. return fd;
  1556. }
  1557. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  1558. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1559. {
  1560. struct bpf_map *map;
  1561. u32 id = attr->map_id;
  1562. int f_flags;
  1563. int fd;
  1564. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  1565. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  1566. return -EINVAL;
  1567. if (!capable(CAP_SYS_ADMIN))
  1568. return -EPERM;
  1569. f_flags = bpf_get_file_flag(attr->open_flags);
  1570. if (f_flags < 0)
  1571. return f_flags;
  1572. spin_lock_bh(&map_idr_lock);
  1573. map = idr_find(&map_idr, id);
  1574. if (map)
  1575. map = bpf_map_inc_not_zero(map, true);
  1576. else
  1577. map = ERR_PTR(-ENOENT);
  1578. spin_unlock_bh(&map_idr_lock);
  1579. if (IS_ERR(map))
  1580. return PTR_ERR(map);
  1581. fd = bpf_map_new_fd(map, f_flags);
  1582. if (fd < 0)
  1583. bpf_map_put_with_uref(map);
  1584. return fd;
  1585. }
  1586. static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
  1587. unsigned long addr)
  1588. {
  1589. int i;
  1590. for (i = 0; i < prog->aux->used_map_cnt; i++)
  1591. if (prog->aux->used_maps[i] == (void *)addr)
  1592. return prog->aux->used_maps[i];
  1593. return NULL;
  1594. }
  1595. static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
  1596. {
  1597. const struct bpf_map *map;
  1598. struct bpf_insn *insns;
  1599. u64 imm;
  1600. int i;
  1601. insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
  1602. GFP_USER);
  1603. if (!insns)
  1604. return insns;
  1605. for (i = 0; i < prog->len; i++) {
  1606. if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
  1607. insns[i].code = BPF_JMP | BPF_CALL;
  1608. insns[i].imm = BPF_FUNC_tail_call;
  1609. /* fall-through */
  1610. }
  1611. if (insns[i].code == (BPF_JMP | BPF_CALL) ||
  1612. insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
  1613. if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
  1614. insns[i].code = BPF_JMP | BPF_CALL;
  1615. if (!bpf_dump_raw_ok())
  1616. insns[i].imm = 0;
  1617. continue;
  1618. }
  1619. if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
  1620. continue;
  1621. imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
  1622. map = bpf_map_from_imm(prog, imm);
  1623. if (map) {
  1624. insns[i].src_reg = BPF_PSEUDO_MAP_FD;
  1625. insns[i].imm = map->id;
  1626. insns[i + 1].imm = 0;
  1627. continue;
  1628. }
  1629. if (!bpf_dump_raw_ok() &&
  1630. imm == (unsigned long)prog->aux) {
  1631. insns[i].imm = 0;
  1632. insns[i + 1].imm = 0;
  1633. continue;
  1634. }
  1635. }
  1636. return insns;
  1637. }
  1638. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1639. const union bpf_attr *attr,
  1640. union bpf_attr __user *uattr)
  1641. {
  1642. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1643. struct bpf_prog_info info;
  1644. u32 info_len = attr->info.info_len;
  1645. char __user *uinsns;
  1646. u32 ulen;
  1647. int err;
  1648. err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1649. if (err)
  1650. return err;
  1651. info_len = min_t(u32, sizeof(info), info_len);
  1652. memset(&info, 0, sizeof(info));
  1653. if (copy_from_user(&info, uinfo, info_len))
  1654. return -EFAULT;
  1655. info.type = prog->type;
  1656. info.id = prog->aux->id;
  1657. info.load_time = prog->aux->load_time;
  1658. info.created_by_uid = from_kuid_munged(current_user_ns(),
  1659. prog->aux->user->uid);
  1660. info.gpl_compatible = prog->gpl_compatible;
  1661. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1662. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  1663. ulen = info.nr_map_ids;
  1664. info.nr_map_ids = prog->aux->used_map_cnt;
  1665. ulen = min_t(u32, info.nr_map_ids, ulen);
  1666. if (ulen) {
  1667. u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
  1668. u32 i;
  1669. for (i = 0; i < ulen; i++)
  1670. if (put_user(prog->aux->used_maps[i]->id,
  1671. &user_map_ids[i]))
  1672. return -EFAULT;
  1673. }
  1674. if (!capable(CAP_SYS_ADMIN)) {
  1675. info.jited_prog_len = 0;
  1676. info.xlated_prog_len = 0;
  1677. info.nr_jited_ksyms = 0;
  1678. info.nr_jited_func_lens = 0;
  1679. goto done;
  1680. }
  1681. ulen = info.xlated_prog_len;
  1682. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1683. if (info.xlated_prog_len && ulen) {
  1684. struct bpf_insn *insns_sanitized;
  1685. bool fault;
  1686. if (prog->blinded && !bpf_dump_raw_ok()) {
  1687. info.xlated_prog_insns = 0;
  1688. goto done;
  1689. }
  1690. insns_sanitized = bpf_insn_prepare_dump(prog);
  1691. if (!insns_sanitized)
  1692. return -ENOMEM;
  1693. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1694. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1695. fault = copy_to_user(uinsns, insns_sanitized, ulen);
  1696. kfree(insns_sanitized);
  1697. if (fault)
  1698. return -EFAULT;
  1699. }
  1700. if (bpf_prog_is_dev_bound(prog->aux)) {
  1701. err = bpf_prog_offload_info_fill(&info, prog);
  1702. if (err)
  1703. return err;
  1704. goto done;
  1705. }
  1706. /* NOTE: the following code is supposed to be skipped for offload.
  1707. * bpf_prog_offload_info_fill() is the place to fill similar fields
  1708. * for offload.
  1709. */
  1710. ulen = info.jited_prog_len;
  1711. if (prog->aux->func_cnt) {
  1712. u32 i;
  1713. info.jited_prog_len = 0;
  1714. for (i = 0; i < prog->aux->func_cnt; i++)
  1715. info.jited_prog_len += prog->aux->func[i]->jited_len;
  1716. } else {
  1717. info.jited_prog_len = prog->jited_len;
  1718. }
  1719. if (info.jited_prog_len && ulen) {
  1720. if (bpf_dump_raw_ok()) {
  1721. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1722. ulen = min_t(u32, info.jited_prog_len, ulen);
  1723. /* for multi-function programs, copy the JITed
  1724. * instructions for all the functions
  1725. */
  1726. if (prog->aux->func_cnt) {
  1727. u32 len, free, i;
  1728. u8 *img;
  1729. free = ulen;
  1730. for (i = 0; i < prog->aux->func_cnt; i++) {
  1731. len = prog->aux->func[i]->jited_len;
  1732. len = min_t(u32, len, free);
  1733. img = (u8 *) prog->aux->func[i]->bpf_func;
  1734. if (copy_to_user(uinsns, img, len))
  1735. return -EFAULT;
  1736. uinsns += len;
  1737. free -= len;
  1738. if (!free)
  1739. break;
  1740. }
  1741. } else {
  1742. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1743. return -EFAULT;
  1744. }
  1745. } else {
  1746. info.jited_prog_insns = 0;
  1747. }
  1748. }
  1749. ulen = info.nr_jited_ksyms;
  1750. info.nr_jited_ksyms = prog->aux->func_cnt;
  1751. if (info.nr_jited_ksyms && ulen) {
  1752. if (bpf_dump_raw_ok()) {
  1753. u64 __user *user_ksyms;
  1754. ulong ksym_addr;
  1755. u32 i;
  1756. /* copy the address of the kernel symbol
  1757. * corresponding to each function
  1758. */
  1759. ulen = min_t(u32, info.nr_jited_ksyms, ulen);
  1760. user_ksyms = u64_to_user_ptr(info.jited_ksyms);
  1761. for (i = 0; i < ulen; i++) {
  1762. ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
  1763. ksym_addr &= PAGE_MASK;
  1764. if (put_user((u64) ksym_addr, &user_ksyms[i]))
  1765. return -EFAULT;
  1766. }
  1767. } else {
  1768. info.jited_ksyms = 0;
  1769. }
  1770. }
  1771. ulen = info.nr_jited_func_lens;
  1772. info.nr_jited_func_lens = prog->aux->func_cnt;
  1773. if (info.nr_jited_func_lens && ulen) {
  1774. if (bpf_dump_raw_ok()) {
  1775. u32 __user *user_lens;
  1776. u32 func_len, i;
  1777. /* copy the JITed image lengths for each function */
  1778. ulen = min_t(u32, info.nr_jited_func_lens, ulen);
  1779. user_lens = u64_to_user_ptr(info.jited_func_lens);
  1780. for (i = 0; i < ulen; i++) {
  1781. func_len = prog->aux->func[i]->jited_len;
  1782. if (put_user(func_len, &user_lens[i]))
  1783. return -EFAULT;
  1784. }
  1785. } else {
  1786. info.jited_func_lens = 0;
  1787. }
  1788. }
  1789. done:
  1790. if (copy_to_user(uinfo, &info, info_len) ||
  1791. put_user(info_len, &uattr->info.info_len))
  1792. return -EFAULT;
  1793. return 0;
  1794. }
  1795. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1796. const union bpf_attr *attr,
  1797. union bpf_attr __user *uattr)
  1798. {
  1799. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1800. struct bpf_map_info info;
  1801. u32 info_len = attr->info.info_len;
  1802. int err;
  1803. err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1804. if (err)
  1805. return err;
  1806. info_len = min_t(u32, sizeof(info), info_len);
  1807. memset(&info, 0, sizeof(info));
  1808. info.type = map->map_type;
  1809. info.id = map->id;
  1810. info.key_size = map->key_size;
  1811. info.value_size = map->value_size;
  1812. info.max_entries = map->max_entries;
  1813. info.map_flags = map->map_flags;
  1814. memcpy(info.name, map->name, sizeof(map->name));
  1815. if (map->btf) {
  1816. info.btf_id = btf_id(map->btf);
  1817. info.btf_key_type_id = map->btf_key_type_id;
  1818. info.btf_value_type_id = map->btf_value_type_id;
  1819. }
  1820. if (bpf_map_is_dev_bound(map)) {
  1821. err = bpf_map_offload_info_fill(&info, map);
  1822. if (err)
  1823. return err;
  1824. }
  1825. if (copy_to_user(uinfo, &info, info_len) ||
  1826. put_user(info_len, &uattr->info.info_len))
  1827. return -EFAULT;
  1828. return 0;
  1829. }
  1830. static int bpf_btf_get_info_by_fd(struct btf *btf,
  1831. const union bpf_attr *attr,
  1832. union bpf_attr __user *uattr)
  1833. {
  1834. struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1835. u32 info_len = attr->info.info_len;
  1836. int err;
  1837. err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
  1838. if (err)
  1839. return err;
  1840. return btf_get_info_by_fd(btf, attr, uattr);
  1841. }
  1842. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1843. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1844. union bpf_attr __user *uattr)
  1845. {
  1846. int ufd = attr->info.bpf_fd;
  1847. struct fd f;
  1848. int err;
  1849. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1850. return -EINVAL;
  1851. f = fdget(ufd);
  1852. if (!f.file)
  1853. return -EBADFD;
  1854. if (f.file->f_op == &bpf_prog_fops)
  1855. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1856. uattr);
  1857. else if (f.file->f_op == &bpf_map_fops)
  1858. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1859. uattr);
  1860. else if (f.file->f_op == &btf_fops)
  1861. err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
  1862. else
  1863. err = -EINVAL;
  1864. fdput(f);
  1865. return err;
  1866. }
  1867. #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
  1868. static int bpf_btf_load(const union bpf_attr *attr)
  1869. {
  1870. if (CHECK_ATTR(BPF_BTF_LOAD))
  1871. return -EINVAL;
  1872. if (!capable(CAP_SYS_ADMIN))
  1873. return -EPERM;
  1874. return btf_new_fd(attr);
  1875. }
  1876. #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
  1877. static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
  1878. {
  1879. if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
  1880. return -EINVAL;
  1881. if (!capable(CAP_SYS_ADMIN))
  1882. return -EPERM;
  1883. return btf_get_fd_by_id(attr->btf_id);
  1884. }
  1885. static int bpf_task_fd_query_copy(const union bpf_attr *attr,
  1886. union bpf_attr __user *uattr,
  1887. u32 prog_id, u32 fd_type,
  1888. const char *buf, u64 probe_offset,
  1889. u64 probe_addr)
  1890. {
  1891. char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
  1892. u32 len = buf ? strlen(buf) : 0, input_len;
  1893. int err = 0;
  1894. if (put_user(len, &uattr->task_fd_query.buf_len))
  1895. return -EFAULT;
  1896. input_len = attr->task_fd_query.buf_len;
  1897. if (input_len && ubuf) {
  1898. if (!len) {
  1899. /* nothing to copy, just make ubuf NULL terminated */
  1900. char zero = '\0';
  1901. if (put_user(zero, ubuf))
  1902. return -EFAULT;
  1903. } else if (input_len >= len + 1) {
  1904. /* ubuf can hold the string with NULL terminator */
  1905. if (copy_to_user(ubuf, buf, len + 1))
  1906. return -EFAULT;
  1907. } else {
  1908. /* ubuf cannot hold the string with NULL terminator,
  1909. * do a partial copy with NULL terminator.
  1910. */
  1911. char zero = '\0';
  1912. err = -ENOSPC;
  1913. if (copy_to_user(ubuf, buf, input_len - 1))
  1914. return -EFAULT;
  1915. if (put_user(zero, ubuf + input_len - 1))
  1916. return -EFAULT;
  1917. }
  1918. }
  1919. if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
  1920. put_user(fd_type, &uattr->task_fd_query.fd_type) ||
  1921. put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
  1922. put_user(probe_addr, &uattr->task_fd_query.probe_addr))
  1923. return -EFAULT;
  1924. return err;
  1925. }
  1926. #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
  1927. static int bpf_task_fd_query(const union bpf_attr *attr,
  1928. union bpf_attr __user *uattr)
  1929. {
  1930. pid_t pid = attr->task_fd_query.pid;
  1931. u32 fd = attr->task_fd_query.fd;
  1932. const struct perf_event *event;
  1933. struct files_struct *files;
  1934. struct task_struct *task;
  1935. struct file *file;
  1936. int err;
  1937. if (CHECK_ATTR(BPF_TASK_FD_QUERY))
  1938. return -EINVAL;
  1939. if (!capable(CAP_SYS_ADMIN))
  1940. return -EPERM;
  1941. if (attr->task_fd_query.flags != 0)
  1942. return -EINVAL;
  1943. task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
  1944. if (!task)
  1945. return -ENOENT;
  1946. files = get_files_struct(task);
  1947. put_task_struct(task);
  1948. if (!files)
  1949. return -ENOENT;
  1950. err = 0;
  1951. spin_lock(&files->file_lock);
  1952. file = fcheck_files(files, fd);
  1953. if (!file)
  1954. err = -EBADF;
  1955. else
  1956. get_file(file);
  1957. spin_unlock(&files->file_lock);
  1958. put_files_struct(files);
  1959. if (err)
  1960. goto out;
  1961. if (file->f_op == &bpf_raw_tp_fops) {
  1962. struct bpf_raw_tracepoint *raw_tp = file->private_data;
  1963. struct bpf_raw_event_map *btp = raw_tp->btp;
  1964. err = bpf_task_fd_query_copy(attr, uattr,
  1965. raw_tp->prog->aux->id,
  1966. BPF_FD_TYPE_RAW_TRACEPOINT,
  1967. btp->tp->name, 0, 0);
  1968. goto put_file;
  1969. }
  1970. event = perf_get_event(file);
  1971. if (!IS_ERR(event)) {
  1972. u64 probe_offset, probe_addr;
  1973. u32 prog_id, fd_type;
  1974. const char *buf;
  1975. err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
  1976. &buf, &probe_offset,
  1977. &probe_addr);
  1978. if (!err)
  1979. err = bpf_task_fd_query_copy(attr, uattr, prog_id,
  1980. fd_type, buf,
  1981. probe_offset,
  1982. probe_addr);
  1983. goto put_file;
  1984. }
  1985. err = -ENOTSUPP;
  1986. put_file:
  1987. fput(file);
  1988. out:
  1989. return err;
  1990. }
  1991. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1992. {
  1993. union bpf_attr attr;
  1994. int err;
  1995. if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
  1996. return -EPERM;
  1997. if (kernel_is_locked_down("BPF"))
  1998. return -EPERM;
  1999. err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
  2000. if (err)
  2001. return err;
  2002. size = min_t(u32, size, sizeof(attr));
  2003. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  2004. memset(&attr, 0, sizeof(attr));
  2005. if (copy_from_user(&attr, uattr, size) != 0)
  2006. return -EFAULT;
  2007. err = security_bpf(cmd, &attr, size);
  2008. if (err < 0)
  2009. return err;
  2010. switch (cmd) {
  2011. case BPF_MAP_CREATE:
  2012. err = map_create(&attr);
  2013. break;
  2014. case BPF_MAP_LOOKUP_ELEM:
  2015. err = map_lookup_elem(&attr);
  2016. break;
  2017. case BPF_MAP_UPDATE_ELEM:
  2018. err = map_update_elem(&attr);
  2019. break;
  2020. case BPF_MAP_DELETE_ELEM:
  2021. err = map_delete_elem(&attr);
  2022. break;
  2023. case BPF_MAP_GET_NEXT_KEY:
  2024. err = map_get_next_key(&attr);
  2025. break;
  2026. case BPF_PROG_LOAD:
  2027. err = bpf_prog_load(&attr);
  2028. break;
  2029. case BPF_OBJ_PIN:
  2030. err = bpf_obj_pin(&attr);
  2031. break;
  2032. case BPF_OBJ_GET:
  2033. err = bpf_obj_get(&attr);
  2034. break;
  2035. case BPF_PROG_ATTACH:
  2036. err = bpf_prog_attach(&attr);
  2037. break;
  2038. case BPF_PROG_DETACH:
  2039. err = bpf_prog_detach(&attr);
  2040. break;
  2041. case BPF_PROG_QUERY:
  2042. err = bpf_prog_query(&attr, uattr);
  2043. break;
  2044. case BPF_PROG_TEST_RUN:
  2045. err = bpf_prog_test_run(&attr, uattr);
  2046. break;
  2047. case BPF_PROG_GET_NEXT_ID:
  2048. err = bpf_obj_get_next_id(&attr, uattr,
  2049. &prog_idr, &prog_idr_lock);
  2050. break;
  2051. case BPF_MAP_GET_NEXT_ID:
  2052. err = bpf_obj_get_next_id(&attr, uattr,
  2053. &map_idr, &map_idr_lock);
  2054. break;
  2055. case BPF_PROG_GET_FD_BY_ID:
  2056. err = bpf_prog_get_fd_by_id(&attr);
  2057. break;
  2058. case BPF_MAP_GET_FD_BY_ID:
  2059. err = bpf_map_get_fd_by_id(&attr);
  2060. break;
  2061. case BPF_OBJ_GET_INFO_BY_FD:
  2062. err = bpf_obj_get_info_by_fd(&attr, uattr);
  2063. break;
  2064. case BPF_RAW_TRACEPOINT_OPEN:
  2065. err = bpf_raw_tracepoint_open(&attr);
  2066. break;
  2067. case BPF_BTF_LOAD:
  2068. err = bpf_btf_load(&attr);
  2069. break;
  2070. case BPF_BTF_GET_FD_BY_ID:
  2071. err = bpf_btf_get_fd_by_id(&attr);
  2072. break;
  2073. case BPF_TASK_FD_QUERY:
  2074. err = bpf_task_fd_query(&attr, uattr);
  2075. break;
  2076. default:
  2077. err = -EINVAL;
  2078. break;
  2079. }
  2080. return err;
  2081. }