syscall.c 49 KB

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