syscall.c 57 KB

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