syscall.c 54 KB

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