syscall.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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/syscalls.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/file.h>
  21. #include <linux/license.h>
  22. #include <linux/filter.h>
  23. #include <linux/version.h>
  24. #include <linux/kernel.h>
  25. #include <linux/idr.h>
  26. #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
  27. (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
  28. (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
  29. (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  30. #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
  31. #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
  32. #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
  33. DEFINE_PER_CPU(int, bpf_prog_active);
  34. static DEFINE_IDR(prog_idr);
  35. static DEFINE_SPINLOCK(prog_idr_lock);
  36. static DEFINE_IDR(map_idr);
  37. static DEFINE_SPINLOCK(map_idr_lock);
  38. int sysctl_unprivileged_bpf_disabled __read_mostly;
  39. static const struct bpf_map_ops * const bpf_map_types[] = {
  40. #define BPF_PROG_TYPE(_id, _ops)
  41. #define BPF_MAP_TYPE(_id, _ops) \
  42. [_id] = &_ops,
  43. #include <linux/bpf_types.h>
  44. #undef BPF_PROG_TYPE
  45. #undef BPF_MAP_TYPE
  46. };
  47. /*
  48. * If we're handed a bigger struct than we know of, ensure all the unknown bits
  49. * are 0 - i.e. new user-space does not rely on any kernel feature extensions
  50. * we don't know about yet.
  51. *
  52. * There is a ToCToU between this function call and the following
  53. * copy_from_user() call. However, this is not a concern since this function is
  54. * meant to be a future-proofing of bits.
  55. */
  56. static int check_uarg_tail_zero(void __user *uaddr,
  57. size_t expected_size,
  58. size_t actual_size)
  59. {
  60. unsigned char __user *addr;
  61. unsigned char __user *end;
  62. unsigned char val;
  63. int err;
  64. if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
  65. return -E2BIG;
  66. if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
  67. return -EFAULT;
  68. if (actual_size <= expected_size)
  69. return 0;
  70. addr = uaddr + expected_size;
  71. end = uaddr + actual_size;
  72. for (; addr < end; addr++) {
  73. err = get_user(val, addr);
  74. if (err)
  75. return err;
  76. if (val)
  77. return -E2BIG;
  78. }
  79. return 0;
  80. }
  81. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  82. {
  83. struct bpf_map *map;
  84. if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
  85. !bpf_map_types[attr->map_type])
  86. return ERR_PTR(-EINVAL);
  87. map = bpf_map_types[attr->map_type]->map_alloc(attr);
  88. if (IS_ERR(map))
  89. return map;
  90. map->ops = bpf_map_types[attr->map_type];
  91. map->map_type = attr->map_type;
  92. return map;
  93. }
  94. void *bpf_map_area_alloc(size_t size, int numa_node)
  95. {
  96. /* We definitely need __GFP_NORETRY, so OOM killer doesn't
  97. * trigger under memory pressure as we really just want to
  98. * fail instead.
  99. */
  100. const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
  101. void *area;
  102. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  103. area = kmalloc_node(size, GFP_USER | flags, numa_node);
  104. if (area != NULL)
  105. return area;
  106. }
  107. return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
  108. __builtin_return_address(0));
  109. }
  110. void bpf_map_area_free(void *area)
  111. {
  112. kvfree(area);
  113. }
  114. int bpf_map_precharge_memlock(u32 pages)
  115. {
  116. struct user_struct *user = get_current_user();
  117. unsigned long memlock_limit, cur;
  118. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  119. cur = atomic_long_read(&user->locked_vm);
  120. free_uid(user);
  121. if (cur + pages > memlock_limit)
  122. return -EPERM;
  123. return 0;
  124. }
  125. static int bpf_map_charge_memlock(struct bpf_map *map)
  126. {
  127. struct user_struct *user = get_current_user();
  128. unsigned long memlock_limit;
  129. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  130. atomic_long_add(map->pages, &user->locked_vm);
  131. if (atomic_long_read(&user->locked_vm) > memlock_limit) {
  132. atomic_long_sub(map->pages, &user->locked_vm);
  133. free_uid(user);
  134. return -EPERM;
  135. }
  136. map->user = user;
  137. return 0;
  138. }
  139. static void bpf_map_uncharge_memlock(struct bpf_map *map)
  140. {
  141. struct user_struct *user = map->user;
  142. atomic_long_sub(map->pages, &user->locked_vm);
  143. free_uid(user);
  144. }
  145. static int bpf_map_alloc_id(struct bpf_map *map)
  146. {
  147. int id;
  148. spin_lock_bh(&map_idr_lock);
  149. id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
  150. if (id > 0)
  151. map->id = id;
  152. spin_unlock_bh(&map_idr_lock);
  153. if (WARN_ON_ONCE(!id))
  154. return -ENOSPC;
  155. return id > 0 ? 0 : id;
  156. }
  157. static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
  158. {
  159. unsigned long flags;
  160. if (do_idr_lock)
  161. spin_lock_irqsave(&map_idr_lock, flags);
  162. else
  163. __acquire(&map_idr_lock);
  164. idr_remove(&map_idr, map->id);
  165. if (do_idr_lock)
  166. spin_unlock_irqrestore(&map_idr_lock, flags);
  167. else
  168. __release(&map_idr_lock);
  169. }
  170. /* called from workqueue */
  171. static void bpf_map_free_deferred(struct work_struct *work)
  172. {
  173. struct bpf_map *map = container_of(work, struct bpf_map, work);
  174. bpf_map_uncharge_memlock(map);
  175. security_bpf_map_free(map);
  176. /* implementation dependent freeing */
  177. map->ops->map_free(map);
  178. }
  179. static void bpf_map_put_uref(struct bpf_map *map)
  180. {
  181. if (atomic_dec_and_test(&map->usercnt)) {
  182. if (map->ops->map_release_uref)
  183. map->ops->map_release_uref(map);
  184. }
  185. }
  186. /* decrement map refcnt and schedule it for freeing via workqueue
  187. * (unrelying map implementation ops->map_free() might sleep)
  188. */
  189. static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
  190. {
  191. if (atomic_dec_and_test(&map->refcnt)) {
  192. /* bpf_map_free_id() must be called first */
  193. bpf_map_free_id(map, do_idr_lock);
  194. INIT_WORK(&map->work, bpf_map_free_deferred);
  195. schedule_work(&map->work);
  196. }
  197. }
  198. void bpf_map_put(struct bpf_map *map)
  199. {
  200. __bpf_map_put(map, true);
  201. }
  202. void bpf_map_put_with_uref(struct bpf_map *map)
  203. {
  204. bpf_map_put_uref(map);
  205. bpf_map_put(map);
  206. }
  207. static int bpf_map_release(struct inode *inode, struct file *filp)
  208. {
  209. struct bpf_map *map = filp->private_data;
  210. if (map->ops->map_release)
  211. map->ops->map_release(map, filp);
  212. bpf_map_put_with_uref(map);
  213. return 0;
  214. }
  215. #ifdef CONFIG_PROC_FS
  216. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  217. {
  218. const struct bpf_map *map = filp->private_data;
  219. const struct bpf_array *array;
  220. u32 owner_prog_type = 0;
  221. u32 owner_jited = 0;
  222. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
  223. array = container_of(map, struct bpf_array, map);
  224. owner_prog_type = array->owner_prog_type;
  225. owner_jited = array->owner_jited;
  226. }
  227. seq_printf(m,
  228. "map_type:\t%u\n"
  229. "key_size:\t%u\n"
  230. "value_size:\t%u\n"
  231. "max_entries:\t%u\n"
  232. "map_flags:\t%#x\n"
  233. "memlock:\t%llu\n",
  234. map->map_type,
  235. map->key_size,
  236. map->value_size,
  237. map->max_entries,
  238. map->map_flags,
  239. map->pages * 1ULL << PAGE_SHIFT);
  240. if (owner_prog_type) {
  241. seq_printf(m, "owner_prog_type:\t%u\n",
  242. owner_prog_type);
  243. seq_printf(m, "owner_jited:\t%u\n",
  244. owner_jited);
  245. }
  246. }
  247. #endif
  248. static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
  249. loff_t *ppos)
  250. {
  251. /* We need this handler such that alloc_file() enables
  252. * f_mode with FMODE_CAN_READ.
  253. */
  254. return -EINVAL;
  255. }
  256. static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
  257. size_t siz, loff_t *ppos)
  258. {
  259. /* We need this handler such that alloc_file() enables
  260. * f_mode with FMODE_CAN_WRITE.
  261. */
  262. return -EINVAL;
  263. }
  264. const struct file_operations bpf_map_fops = {
  265. #ifdef CONFIG_PROC_FS
  266. .show_fdinfo = bpf_map_show_fdinfo,
  267. #endif
  268. .release = bpf_map_release,
  269. .read = bpf_dummy_read,
  270. .write = bpf_dummy_write,
  271. };
  272. int bpf_map_new_fd(struct bpf_map *map, int flags)
  273. {
  274. int ret;
  275. ret = security_bpf_map(map, OPEN_FMODE(flags));
  276. if (ret < 0)
  277. return ret;
  278. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  279. flags | O_CLOEXEC);
  280. }
  281. int bpf_get_file_flag(int flags)
  282. {
  283. if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
  284. return -EINVAL;
  285. if (flags & BPF_F_RDONLY)
  286. return O_RDONLY;
  287. if (flags & BPF_F_WRONLY)
  288. return O_WRONLY;
  289. return O_RDWR;
  290. }
  291. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  292. #define CHECK_ATTR(CMD) \
  293. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  294. sizeof(attr->CMD##_LAST_FIELD), 0, \
  295. sizeof(*attr) - \
  296. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  297. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  298. #define BPF_MAP_CREATE_LAST_FIELD numa_node
  299. /* called via syscall */
  300. static int map_create(union bpf_attr *attr)
  301. {
  302. int numa_node = bpf_map_attr_numa_node(attr);
  303. struct bpf_map *map;
  304. int f_flags;
  305. int err;
  306. err = CHECK_ATTR(BPF_MAP_CREATE);
  307. if (err)
  308. return -EINVAL;
  309. f_flags = bpf_get_file_flag(attr->map_flags);
  310. if (f_flags < 0)
  311. return f_flags;
  312. if (numa_node != NUMA_NO_NODE &&
  313. ((unsigned int)numa_node >= nr_node_ids ||
  314. !node_online(numa_node)))
  315. return -EINVAL;
  316. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  317. map = find_and_alloc_map(attr);
  318. if (IS_ERR(map))
  319. return PTR_ERR(map);
  320. atomic_set(&map->refcnt, 1);
  321. atomic_set(&map->usercnt, 1);
  322. err = security_bpf_map_alloc(map);
  323. if (err)
  324. goto free_map_nouncharge;
  325. err = bpf_map_charge_memlock(map);
  326. if (err)
  327. goto free_map_sec;
  328. err = bpf_map_alloc_id(map);
  329. if (err)
  330. goto free_map;
  331. err = bpf_map_new_fd(map, f_flags);
  332. if (err < 0) {
  333. /* failed to allocate fd.
  334. * bpf_map_put_with_uref() is needed because the above
  335. * bpf_map_alloc_id() has published the map
  336. * to the userspace and the userspace may
  337. * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
  338. */
  339. bpf_map_put_with_uref(map);
  340. return err;
  341. }
  342. trace_bpf_map_create(map, err);
  343. return err;
  344. free_map:
  345. bpf_map_uncharge_memlock(map);
  346. free_map_sec:
  347. security_bpf_map_free(map);
  348. free_map_nouncharge:
  349. map->ops->map_free(map);
  350. return err;
  351. }
  352. /* if error is returned, fd is released.
  353. * On success caller should complete fd access with matching fdput()
  354. */
  355. struct bpf_map *__bpf_map_get(struct fd f)
  356. {
  357. if (!f.file)
  358. return ERR_PTR(-EBADF);
  359. if (f.file->f_op != &bpf_map_fops) {
  360. fdput(f);
  361. return ERR_PTR(-EINVAL);
  362. }
  363. return f.file->private_data;
  364. }
  365. /* prog's and map's refcnt limit */
  366. #define BPF_MAX_REFCNT 32768
  367. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  368. {
  369. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  370. atomic_dec(&map->refcnt);
  371. return ERR_PTR(-EBUSY);
  372. }
  373. if (uref)
  374. atomic_inc(&map->usercnt);
  375. return map;
  376. }
  377. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  378. {
  379. struct fd f = fdget(ufd);
  380. struct bpf_map *map;
  381. map = __bpf_map_get(f);
  382. if (IS_ERR(map))
  383. return map;
  384. map = bpf_map_inc(map, true);
  385. fdput(f);
  386. return map;
  387. }
  388. /* map_idr_lock should have been held */
  389. static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
  390. bool uref)
  391. {
  392. int refold;
  393. refold = __atomic_add_unless(&map->refcnt, 1, 0);
  394. if (refold >= BPF_MAX_REFCNT) {
  395. __bpf_map_put(map, false);
  396. return ERR_PTR(-EBUSY);
  397. }
  398. if (!refold)
  399. return ERR_PTR(-ENOENT);
  400. if (uref)
  401. atomic_inc(&map->usercnt);
  402. return map;
  403. }
  404. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  405. {
  406. return -ENOTSUPP;
  407. }
  408. /* last field in 'union bpf_attr' used by this command */
  409. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  410. static int map_lookup_elem(union bpf_attr *attr)
  411. {
  412. void __user *ukey = u64_to_user_ptr(attr->key);
  413. void __user *uvalue = u64_to_user_ptr(attr->value);
  414. int ufd = attr->map_fd;
  415. struct bpf_map *map;
  416. void *key, *value, *ptr;
  417. u8 key_onstack[SZ_16] __aligned(sizeof(long));
  418. u8 value_onstack[SZ_64] __aligned(sizeof(long));
  419. u32 value_size;
  420. struct fd f;
  421. int err;
  422. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  423. return -EINVAL;
  424. f = fdget(ufd);
  425. map = __bpf_map_get(f);
  426. if (IS_ERR(map))
  427. return PTR_ERR(map);
  428. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  429. err = -EPERM;
  430. goto err_put;
  431. }
  432. if (map->key_size <= sizeof(key_onstack)) {
  433. key = key_onstack;
  434. if (copy_from_user(key, ukey, map->key_size)) {
  435. err = -EFAULT;
  436. goto err_put;
  437. }
  438. } else {
  439. key = memdup_user(ukey, map->key_size);
  440. if (IS_ERR(key)) {
  441. err = PTR_ERR(key);
  442. goto err_put;
  443. }
  444. }
  445. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  446. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  447. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  448. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  449. else if (IS_FD_MAP(map))
  450. value_size = sizeof(u32);
  451. else
  452. value_size = map->value_size;
  453. err = -ENOMEM;
  454. if (value_size <= sizeof(value_onstack)) {
  455. value = value_onstack;
  456. } else {
  457. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  458. if (!value)
  459. goto free_key;
  460. }
  461. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  462. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  463. err = bpf_percpu_hash_copy(map, key, value);
  464. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  465. err = bpf_percpu_array_copy(map, key, value);
  466. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  467. err = bpf_stackmap_copy(map, key, value);
  468. } else if (IS_FD_ARRAY(map)) {
  469. err = bpf_fd_array_map_lookup_elem(map, key, value);
  470. } else if (IS_FD_HASH(map)) {
  471. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  472. } else {
  473. rcu_read_lock();
  474. if (map->ops->map_lookup_elem_sys_only)
  475. ptr = map->ops->map_lookup_elem_sys_only(map, key);
  476. else
  477. ptr = map->ops->map_lookup_elem(map, key);
  478. if (ptr)
  479. memcpy(value, ptr, value_size);
  480. rcu_read_unlock();
  481. err = ptr ? 0 : -ENOENT;
  482. }
  483. if (err)
  484. goto free_value;
  485. err = -EFAULT;
  486. if (copy_to_user(uvalue, value, value_size) != 0)
  487. goto free_value;
  488. trace_bpf_map_lookup_elem(map, ufd, key, value);
  489. err = 0;
  490. free_value:
  491. if (value != value_onstack)
  492. kfree(value);
  493. free_key:
  494. if (key != key_onstack)
  495. kfree(key);
  496. err_put:
  497. fdput(f);
  498. return err;
  499. }
  500. static void maybe_wait_bpf_programs(struct bpf_map *map)
  501. {
  502. /* Wait for any running BPF programs to complete so that
  503. * userspace, when we return to it, knows that all programs
  504. * that could be running use the new map value.
  505. */
  506. if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
  507. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  508. synchronize_rcu();
  509. }
  510. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  511. static int map_update_elem(union bpf_attr *attr)
  512. {
  513. void __user *ukey = u64_to_user_ptr(attr->key);
  514. void __user *uvalue = u64_to_user_ptr(attr->value);
  515. int ufd = attr->map_fd;
  516. struct bpf_map *map;
  517. void *key, *value;
  518. u8 key_onstack[SZ_16] __aligned(sizeof(long));
  519. u8 value_onstack[SZ_64] __aligned(sizeof(long));
  520. u32 value_size;
  521. struct fd f;
  522. int err;
  523. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  524. return -EINVAL;
  525. f = fdget(ufd);
  526. map = __bpf_map_get(f);
  527. if (IS_ERR(map))
  528. return PTR_ERR(map);
  529. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  530. err = -EPERM;
  531. goto err_put;
  532. }
  533. if (map->key_size <= sizeof(key_onstack)) {
  534. key = key_onstack;
  535. if (copy_from_user(key, ukey, map->key_size)) {
  536. err = -EFAULT;
  537. goto err_put;
  538. }
  539. } else {
  540. key = memdup_user(ukey, map->key_size);
  541. if (IS_ERR(key)) {
  542. err = PTR_ERR(key);
  543. goto err_put;
  544. }
  545. }
  546. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  547. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  548. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  549. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  550. else
  551. value_size = map->value_size;
  552. if (value_size <= sizeof(value_onstack)) {
  553. value = value_onstack;
  554. } else {
  555. err = -ENOMEM;
  556. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  557. if (!value)
  558. goto free_key;
  559. }
  560. err = -EFAULT;
  561. if (copy_from_user(value, uvalue, value_size) != 0)
  562. goto free_value;
  563. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  564. * inside bpf map update or delete otherwise deadlocks are possible
  565. */
  566. preempt_disable();
  567. __this_cpu_inc(bpf_prog_active);
  568. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  569. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  570. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  571. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  572. err = bpf_percpu_array_update(map, key, value, attr->flags);
  573. } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
  574. map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  575. map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
  576. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
  577. rcu_read_lock();
  578. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  579. attr->flags);
  580. rcu_read_unlock();
  581. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  582. rcu_read_lock();
  583. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  584. attr->flags);
  585. rcu_read_unlock();
  586. } else {
  587. rcu_read_lock();
  588. err = map->ops->map_update_elem(map, key, value, attr->flags);
  589. rcu_read_unlock();
  590. }
  591. __this_cpu_dec(bpf_prog_active);
  592. preempt_enable();
  593. maybe_wait_bpf_programs(map);
  594. if (!err)
  595. trace_bpf_map_update_elem(map, ufd, key, value);
  596. free_value:
  597. if (value != value_onstack)
  598. kfree(value);
  599. free_key:
  600. if (key != key_onstack)
  601. kfree(key);
  602. err_put:
  603. fdput(f);
  604. return err;
  605. }
  606. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  607. static int map_delete_elem(union bpf_attr *attr)
  608. {
  609. void __user *ukey = u64_to_user_ptr(attr->key);
  610. int ufd = attr->map_fd;
  611. struct bpf_map *map;
  612. struct fd f;
  613. void *key;
  614. u8 key_onstack[SZ_16] __aligned(sizeof(long));
  615. int err;
  616. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  617. return -EINVAL;
  618. f = fdget(ufd);
  619. map = __bpf_map_get(f);
  620. if (IS_ERR(map))
  621. return PTR_ERR(map);
  622. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  623. err = -EPERM;
  624. goto err_put;
  625. }
  626. if (map->key_size <= sizeof(key_onstack)) {
  627. key = key_onstack;
  628. if (copy_from_user(key, ukey, map->key_size)) {
  629. err = -EFAULT;
  630. goto err_put;
  631. }
  632. } else {
  633. key = memdup_user(ukey, map->key_size);
  634. if (IS_ERR(key)) {
  635. err = PTR_ERR(key);
  636. goto err_put;
  637. }
  638. }
  639. preempt_disable();
  640. __this_cpu_inc(bpf_prog_active);
  641. rcu_read_lock();
  642. err = map->ops->map_delete_elem(map, key);
  643. rcu_read_unlock();
  644. __this_cpu_dec(bpf_prog_active);
  645. preempt_enable();
  646. maybe_wait_bpf_programs(map);
  647. if (!err)
  648. trace_bpf_map_delete_elem(map, ufd, key);
  649. if (key != key_onstack)
  650. kfree(key);
  651. err_put:
  652. fdput(f);
  653. return err;
  654. }
  655. /* last field in 'union bpf_attr' used by this command */
  656. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  657. static int map_get_next_key(union bpf_attr *attr)
  658. {
  659. void __user *ukey = u64_to_user_ptr(attr->key);
  660. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  661. int ufd = attr->map_fd;
  662. struct bpf_map *map;
  663. void *key, *next_key;
  664. u8 key_onstack[SZ_16] __aligned(sizeof(long));
  665. u8 next_key_onstack[SZ_64] __aligned(sizeof(long));
  666. struct fd f;
  667. int err;
  668. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  669. return -EINVAL;
  670. f = fdget(ufd);
  671. map = __bpf_map_get(f);
  672. if (IS_ERR(map))
  673. return PTR_ERR(map);
  674. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  675. err = -EPERM;
  676. goto err_put;
  677. }
  678. if (ukey) {
  679. if (map->key_size <= sizeof(key_onstack)) {
  680. key = key_onstack;
  681. if (copy_from_user(key, ukey, map->key_size)) {
  682. err = -EFAULT;
  683. goto err_put;
  684. }
  685. } else {
  686. key = memdup_user(ukey, map->key_size);
  687. if (IS_ERR(key)) {
  688. err = PTR_ERR(key);
  689. goto err_put;
  690. }
  691. }
  692. } else {
  693. key = NULL;
  694. }
  695. err = -ENOMEM;
  696. if (map->key_size <= sizeof(next_key_onstack)) {
  697. next_key = next_key_onstack;
  698. } else {
  699. next_key = kmalloc(map->key_size, GFP_USER);
  700. if (!next_key)
  701. goto free_key;
  702. }
  703. rcu_read_lock();
  704. err = map->ops->map_get_next_key(map, key, next_key);
  705. rcu_read_unlock();
  706. if (err)
  707. goto free_next_key;
  708. err = -EFAULT;
  709. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  710. goto free_next_key;
  711. trace_bpf_map_next_key(map, ufd, key, next_key);
  712. err = 0;
  713. free_next_key:
  714. if (next_key != next_key_onstack)
  715. kfree(next_key);
  716. free_key:
  717. if (key != key_onstack)
  718. kfree(key);
  719. err_put:
  720. fdput(f);
  721. return err;
  722. }
  723. static const struct bpf_verifier_ops * const bpf_prog_types[] = {
  724. #define BPF_PROG_TYPE(_id, _ops) \
  725. [_id] = &_ops,
  726. #define BPF_MAP_TYPE(_id, _ops)
  727. #include <linux/bpf_types.h>
  728. #undef BPF_PROG_TYPE
  729. #undef BPF_MAP_TYPE
  730. };
  731. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  732. {
  733. if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
  734. return -EINVAL;
  735. prog->aux->ops = bpf_prog_types[type];
  736. prog->type = type;
  737. return 0;
  738. }
  739. /* drop refcnt on maps used by eBPF program and free auxilary data */
  740. static void free_used_maps(struct bpf_prog_aux *aux)
  741. {
  742. int i;
  743. for (i = 0; i < aux->used_map_cnt; i++)
  744. bpf_map_put(aux->used_maps[i]);
  745. kfree(aux->used_maps);
  746. }
  747. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  748. {
  749. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  750. unsigned long user_bufs;
  751. if (user) {
  752. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  753. if (user_bufs > memlock_limit) {
  754. atomic_long_sub(pages, &user->locked_vm);
  755. return -EPERM;
  756. }
  757. }
  758. return 0;
  759. }
  760. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  761. {
  762. if (user)
  763. atomic_long_sub(pages, &user->locked_vm);
  764. }
  765. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  766. {
  767. struct user_struct *user = get_current_user();
  768. int ret;
  769. ret = __bpf_prog_charge(user, prog->pages);
  770. if (ret) {
  771. free_uid(user);
  772. return ret;
  773. }
  774. prog->aux->user = user;
  775. return 0;
  776. }
  777. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  778. {
  779. struct user_struct *user = prog->aux->user;
  780. __bpf_prog_uncharge(user, prog->pages);
  781. free_uid(user);
  782. }
  783. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  784. {
  785. int id;
  786. spin_lock_bh(&prog_idr_lock);
  787. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  788. if (id > 0)
  789. prog->aux->id = id;
  790. spin_unlock_bh(&prog_idr_lock);
  791. /* id is in [1, INT_MAX) */
  792. if (WARN_ON_ONCE(!id))
  793. return -ENOSPC;
  794. return id > 0 ? 0 : id;
  795. }
  796. static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  797. {
  798. /* cBPF to eBPF migrations are currently not in the idr store. */
  799. if (!prog->aux->id)
  800. return;
  801. if (do_idr_lock)
  802. spin_lock_bh(&prog_idr_lock);
  803. else
  804. __acquire(&prog_idr_lock);
  805. idr_remove(&prog_idr, prog->aux->id);
  806. if (do_idr_lock)
  807. spin_unlock_bh(&prog_idr_lock);
  808. else
  809. __release(&prog_idr_lock);
  810. }
  811. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  812. {
  813. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  814. free_used_maps(aux);
  815. bpf_prog_uncharge_memlock(aux->prog);
  816. security_bpf_prog_free(aux);
  817. bpf_prog_free(aux->prog);
  818. }
  819. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  820. {
  821. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  822. trace_bpf_prog_put_rcu(prog);
  823. /* bpf_prog_free_id() must be called first */
  824. bpf_prog_free_id(prog, do_idr_lock);
  825. bpf_prog_kallsyms_del(prog);
  826. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  827. }
  828. }
  829. void bpf_prog_put(struct bpf_prog *prog)
  830. {
  831. __bpf_prog_put(prog, true);
  832. }
  833. EXPORT_SYMBOL_GPL(bpf_prog_put);
  834. static int bpf_prog_release(struct inode *inode, struct file *filp)
  835. {
  836. struct bpf_prog *prog = filp->private_data;
  837. bpf_prog_put(prog);
  838. return 0;
  839. }
  840. #ifdef CONFIG_PROC_FS
  841. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  842. {
  843. const struct bpf_prog *prog = filp->private_data;
  844. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  845. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  846. seq_printf(m,
  847. "prog_type:\t%u\n"
  848. "prog_jited:\t%u\n"
  849. "prog_tag:\t%s\n"
  850. "memlock:\t%llu\n",
  851. prog->type,
  852. prog->jited,
  853. prog_tag,
  854. prog->pages * 1ULL << PAGE_SHIFT);
  855. }
  856. #endif
  857. const struct file_operations bpf_prog_fops = {
  858. #ifdef CONFIG_PROC_FS
  859. .show_fdinfo = bpf_prog_show_fdinfo,
  860. #endif
  861. .release = bpf_prog_release,
  862. .read = bpf_dummy_read,
  863. .write = bpf_dummy_write,
  864. };
  865. int bpf_prog_new_fd(struct bpf_prog *prog)
  866. {
  867. int ret;
  868. ret = security_bpf_prog(prog);
  869. if (ret < 0)
  870. return ret;
  871. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  872. O_RDWR | O_CLOEXEC);
  873. }
  874. static struct bpf_prog *____bpf_prog_get(struct fd f)
  875. {
  876. if (!f.file)
  877. return ERR_PTR(-EBADF);
  878. if (f.file->f_op != &bpf_prog_fops) {
  879. fdput(f);
  880. return ERR_PTR(-EINVAL);
  881. }
  882. return f.file->private_data;
  883. }
  884. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  885. {
  886. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  887. atomic_sub(i, &prog->aux->refcnt);
  888. return ERR_PTR(-EBUSY);
  889. }
  890. return prog;
  891. }
  892. EXPORT_SYMBOL_GPL(bpf_prog_add);
  893. void bpf_prog_sub(struct bpf_prog *prog, int i)
  894. {
  895. /* Only to be used for undoing previous bpf_prog_add() in some
  896. * error path. We still know that another entity in our call
  897. * path holds a reference to the program, thus atomic_sub() can
  898. * be safely used in such cases!
  899. */
  900. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  901. }
  902. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  903. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  904. {
  905. return bpf_prog_add(prog, 1);
  906. }
  907. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  908. /* prog_idr_lock should have been held */
  909. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  910. {
  911. int refold;
  912. refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
  913. if (refold >= BPF_MAX_REFCNT) {
  914. __bpf_prog_put(prog, false);
  915. return ERR_PTR(-EBUSY);
  916. }
  917. if (!refold)
  918. return ERR_PTR(-ENOENT);
  919. return prog;
  920. }
  921. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  922. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
  923. {
  924. struct fd f = fdget(ufd);
  925. struct bpf_prog *prog;
  926. prog = ____bpf_prog_get(f);
  927. if (IS_ERR(prog))
  928. return prog;
  929. if (type && prog->type != *type) {
  930. prog = ERR_PTR(-EINVAL);
  931. goto out;
  932. }
  933. prog = bpf_prog_inc(prog);
  934. out:
  935. fdput(f);
  936. return prog;
  937. }
  938. struct bpf_prog *bpf_prog_get(u32 ufd)
  939. {
  940. return __bpf_prog_get(ufd, NULL);
  941. }
  942. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  943. {
  944. struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
  945. if (!IS_ERR(prog))
  946. trace_bpf_prog_get_type(prog);
  947. return prog;
  948. }
  949. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  950. /* last field in 'union bpf_attr' used by this command */
  951. #define BPF_PROG_LOAD_LAST_FIELD prog_flags
  952. static int bpf_prog_load(union bpf_attr *attr)
  953. {
  954. enum bpf_prog_type type = attr->prog_type;
  955. struct bpf_prog *prog;
  956. int err;
  957. char license[128];
  958. bool is_gpl;
  959. if (CHECK_ATTR(BPF_PROG_LOAD))
  960. return -EINVAL;
  961. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  962. return -EINVAL;
  963. /* copy eBPF program license from user space */
  964. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  965. sizeof(license) - 1) < 0)
  966. return -EFAULT;
  967. license[sizeof(license) - 1] = 0;
  968. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  969. is_gpl = license_is_gpl_compatible(license);
  970. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  971. return -E2BIG;
  972. if (type == BPF_PROG_TYPE_KPROBE &&
  973. attr->kern_version != LINUX_VERSION_CODE)
  974. return -EINVAL;
  975. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  976. type != BPF_PROG_TYPE_CGROUP_SKB &&
  977. !capable(CAP_SYS_ADMIN))
  978. return -EPERM;
  979. /* plain bpf_prog allocation */
  980. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  981. if (!prog)
  982. return -ENOMEM;
  983. err = security_bpf_prog_alloc(prog->aux);
  984. if (err)
  985. goto free_prog_nouncharge;
  986. err = bpf_prog_charge_memlock(prog);
  987. if (err)
  988. goto free_prog_sec;
  989. prog->len = attr->insn_cnt;
  990. err = -EFAULT;
  991. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  992. bpf_prog_insn_size(prog)) != 0)
  993. goto free_prog;
  994. prog->orig_prog = NULL;
  995. prog->jited = 0;
  996. atomic_set(&prog->aux->refcnt, 1);
  997. prog->gpl_compatible = is_gpl ? 1 : 0;
  998. /* find program type: socket_filter vs tracing_filter */
  999. err = find_prog_type(type, prog);
  1000. if (err < 0)
  1001. goto free_prog;
  1002. /* run eBPF verifier */
  1003. err = bpf_check(&prog, attr);
  1004. if (err < 0)
  1005. goto free_used_maps;
  1006. /* eBPF program is ready to be JITed */
  1007. prog = bpf_prog_select_runtime(prog, &err);
  1008. if (err < 0)
  1009. goto free_used_maps;
  1010. err = bpf_prog_alloc_id(prog);
  1011. if (err)
  1012. goto free_used_maps;
  1013. /* Upon success of bpf_prog_alloc_id(), the BPF prog is
  1014. * effectively publicly exposed. However, retrieving via
  1015. * bpf_prog_get_fd_by_id() will take another reference,
  1016. * therefore it cannot be gone underneath us.
  1017. *
  1018. * Only for the time /after/ successful bpf_prog_new_fd()
  1019. * and before returning to userspace, we might just hold
  1020. * one reference and any parallel close on that fd could
  1021. * rip everything out. Hence, below notifications must
  1022. * happen before bpf_prog_new_fd().
  1023. *
  1024. * Also, any failure handling from this point onwards must
  1025. * be using bpf_prog_put() given the program is exposed.
  1026. */
  1027. bpf_prog_kallsyms_add(prog);
  1028. trace_bpf_prog_load(prog, err);
  1029. err = bpf_prog_new_fd(prog);
  1030. if (err < 0)
  1031. bpf_prog_put(prog);
  1032. return err;
  1033. free_used_maps:
  1034. free_used_maps(prog->aux);
  1035. free_prog:
  1036. bpf_prog_uncharge_memlock(prog);
  1037. free_prog_sec:
  1038. security_bpf_prog_free(prog->aux);
  1039. free_prog_nouncharge:
  1040. bpf_prog_free(prog);
  1041. return err;
  1042. }
  1043. #define BPF_OBJ_LAST_FIELD file_flags
  1044. static int bpf_obj_pin(const union bpf_attr *attr)
  1045. {
  1046. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  1047. return -EINVAL;
  1048. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  1049. }
  1050. static int bpf_obj_get(const union bpf_attr *attr)
  1051. {
  1052. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  1053. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  1054. return -EINVAL;
  1055. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  1056. attr->file_flags);
  1057. }
  1058. #ifdef CONFIG_CGROUP_BPF
  1059. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  1060. static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
  1061. {
  1062. struct bpf_prog *prog = NULL;
  1063. int ufd = attr->target_fd;
  1064. struct bpf_map *map;
  1065. struct fd f;
  1066. int err;
  1067. f = fdget(ufd);
  1068. map = __bpf_map_get(f);
  1069. if (IS_ERR(map))
  1070. return PTR_ERR(map);
  1071. if (attach) {
  1072. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  1073. BPF_PROG_TYPE_SK_SKB);
  1074. if (IS_ERR(prog)) {
  1075. fdput(f);
  1076. return PTR_ERR(prog);
  1077. }
  1078. }
  1079. err = sock_map_prog(map, prog, attr->attach_type);
  1080. if (err) {
  1081. fdput(f);
  1082. if (prog)
  1083. bpf_prog_put(prog);
  1084. return err;
  1085. }
  1086. fdput(f);
  1087. return 0;
  1088. }
  1089. #define BPF_F_ATTACH_MASK \
  1090. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
  1091. static int bpf_prog_attach(const union bpf_attr *attr)
  1092. {
  1093. enum bpf_prog_type ptype;
  1094. struct bpf_prog *prog;
  1095. struct cgroup *cgrp;
  1096. int ret;
  1097. if (!capable(CAP_NET_ADMIN))
  1098. return -EPERM;
  1099. if (CHECK_ATTR(BPF_PROG_ATTACH))
  1100. return -EINVAL;
  1101. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  1102. return -EINVAL;
  1103. switch (attr->attach_type) {
  1104. case BPF_CGROUP_INET_INGRESS:
  1105. case BPF_CGROUP_INET_EGRESS:
  1106. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1107. break;
  1108. case BPF_CGROUP_INET_SOCK_CREATE:
  1109. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1110. break;
  1111. case BPF_CGROUP_SOCK_OPS:
  1112. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1113. break;
  1114. case BPF_SK_SKB_STREAM_PARSER:
  1115. case BPF_SK_SKB_STREAM_VERDICT:
  1116. return sockmap_get_from_fd(attr, true);
  1117. default:
  1118. return -EINVAL;
  1119. }
  1120. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1121. if (IS_ERR(prog))
  1122. return PTR_ERR(prog);
  1123. cgrp = cgroup_get_from_fd(attr->target_fd);
  1124. if (IS_ERR(cgrp)) {
  1125. bpf_prog_put(prog);
  1126. return PTR_ERR(cgrp);
  1127. }
  1128. ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
  1129. attr->attach_flags);
  1130. if (ret)
  1131. bpf_prog_put(prog);
  1132. cgroup_put(cgrp);
  1133. return ret;
  1134. }
  1135. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1136. static int bpf_prog_detach(const union bpf_attr *attr)
  1137. {
  1138. enum bpf_prog_type ptype;
  1139. struct bpf_prog *prog;
  1140. struct cgroup *cgrp;
  1141. int ret;
  1142. if (!capable(CAP_NET_ADMIN))
  1143. return -EPERM;
  1144. if (CHECK_ATTR(BPF_PROG_DETACH))
  1145. return -EINVAL;
  1146. switch (attr->attach_type) {
  1147. case BPF_CGROUP_INET_INGRESS:
  1148. case BPF_CGROUP_INET_EGRESS:
  1149. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1150. break;
  1151. case BPF_CGROUP_INET_SOCK_CREATE:
  1152. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1153. break;
  1154. case BPF_CGROUP_SOCK_OPS:
  1155. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1156. break;
  1157. case BPF_SK_SKB_STREAM_PARSER:
  1158. case BPF_SK_SKB_STREAM_VERDICT:
  1159. return sockmap_get_from_fd(attr, false);
  1160. default:
  1161. return -EINVAL;
  1162. }
  1163. cgrp = cgroup_get_from_fd(attr->target_fd);
  1164. if (IS_ERR(cgrp))
  1165. return PTR_ERR(cgrp);
  1166. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1167. if (IS_ERR(prog))
  1168. prog = NULL;
  1169. ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
  1170. if (prog)
  1171. bpf_prog_put(prog);
  1172. cgroup_put(cgrp);
  1173. return ret;
  1174. }
  1175. #endif /* CONFIG_CGROUP_BPF */
  1176. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1177. static int bpf_prog_test_run(const union bpf_attr *attr,
  1178. union bpf_attr __user *uattr)
  1179. {
  1180. struct bpf_prog *prog;
  1181. int ret = -ENOTSUPP;
  1182. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1183. return -EINVAL;
  1184. prog = bpf_prog_get(attr->test.prog_fd);
  1185. if (IS_ERR(prog))
  1186. return PTR_ERR(prog);
  1187. if (prog->aux->ops->test_run)
  1188. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1189. bpf_prog_put(prog);
  1190. return ret;
  1191. }
  1192. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1193. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1194. union bpf_attr __user *uattr,
  1195. struct idr *idr,
  1196. spinlock_t *lock)
  1197. {
  1198. u32 next_id = attr->start_id;
  1199. int err = 0;
  1200. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1201. return -EINVAL;
  1202. if (!capable(CAP_SYS_ADMIN))
  1203. return -EPERM;
  1204. next_id++;
  1205. spin_lock_bh(lock);
  1206. if (!idr_get_next(idr, &next_id))
  1207. err = -ENOENT;
  1208. spin_unlock_bh(lock);
  1209. if (!err)
  1210. err = put_user(next_id, &uattr->next_id);
  1211. return err;
  1212. }
  1213. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1214. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1215. {
  1216. struct bpf_prog *prog;
  1217. u32 id = attr->prog_id;
  1218. int fd;
  1219. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1220. return -EINVAL;
  1221. if (!capable(CAP_SYS_ADMIN))
  1222. return -EPERM;
  1223. spin_lock_bh(&prog_idr_lock);
  1224. prog = idr_find(&prog_idr, id);
  1225. if (prog)
  1226. prog = bpf_prog_inc_not_zero(prog);
  1227. else
  1228. prog = ERR_PTR(-ENOENT);
  1229. spin_unlock_bh(&prog_idr_lock);
  1230. if (IS_ERR(prog))
  1231. return PTR_ERR(prog);
  1232. fd = bpf_prog_new_fd(prog);
  1233. if (fd < 0)
  1234. bpf_prog_put(prog);
  1235. return fd;
  1236. }
  1237. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  1238. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1239. {
  1240. struct bpf_map *map;
  1241. u32 id = attr->map_id;
  1242. int f_flags;
  1243. int fd;
  1244. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  1245. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  1246. return -EINVAL;
  1247. if (!capable(CAP_SYS_ADMIN))
  1248. return -EPERM;
  1249. f_flags = bpf_get_file_flag(attr->open_flags);
  1250. if (f_flags < 0)
  1251. return f_flags;
  1252. spin_lock_bh(&map_idr_lock);
  1253. map = idr_find(&map_idr, id);
  1254. if (map)
  1255. map = bpf_map_inc_not_zero(map, true);
  1256. else
  1257. map = ERR_PTR(-ENOENT);
  1258. spin_unlock_bh(&map_idr_lock);
  1259. if (IS_ERR(map))
  1260. return PTR_ERR(map);
  1261. fd = bpf_map_new_fd(map, f_flags);
  1262. if (fd < 0)
  1263. bpf_map_put_with_uref(map);
  1264. return fd;
  1265. }
  1266. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1267. const union bpf_attr *attr,
  1268. union bpf_attr __user *uattr)
  1269. {
  1270. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1271. struct bpf_prog_info info;
  1272. u32 info_len = attr->info.info_len;
  1273. char __user *uinsns;
  1274. u32 ulen;
  1275. int err;
  1276. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1277. if (err)
  1278. return err;
  1279. info_len = min_t(u32, sizeof(info), info_len);
  1280. memset(&info, 0, sizeof(info));
  1281. if (copy_from_user(&info, uinfo, info_len))
  1282. return -EFAULT;
  1283. info.type = prog->type;
  1284. info.id = prog->aux->id;
  1285. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1286. if (!capable(CAP_SYS_ADMIN)) {
  1287. info.jited_prog_len = 0;
  1288. info.xlated_prog_len = 0;
  1289. goto done;
  1290. }
  1291. ulen = info.jited_prog_len;
  1292. info.jited_prog_len = prog->jited_len;
  1293. if (info.jited_prog_len && ulen) {
  1294. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1295. ulen = min_t(u32, info.jited_prog_len, ulen);
  1296. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1297. return -EFAULT;
  1298. }
  1299. ulen = info.xlated_prog_len;
  1300. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1301. if (info.xlated_prog_len && ulen) {
  1302. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1303. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1304. if (copy_to_user(uinsns, prog->insnsi, ulen))
  1305. return -EFAULT;
  1306. }
  1307. done:
  1308. if (copy_to_user(uinfo, &info, info_len) ||
  1309. put_user(info_len, &uattr->info.info_len))
  1310. return -EFAULT;
  1311. return 0;
  1312. }
  1313. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1314. const union bpf_attr *attr,
  1315. union bpf_attr __user *uattr)
  1316. {
  1317. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1318. struct bpf_map_info info;
  1319. u32 info_len = attr->info.info_len;
  1320. int err;
  1321. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1322. if (err)
  1323. return err;
  1324. info_len = min_t(u32, sizeof(info), info_len);
  1325. memset(&info, 0, sizeof(info));
  1326. info.type = map->map_type;
  1327. info.id = map->id;
  1328. info.key_size = map->key_size;
  1329. info.value_size = map->value_size;
  1330. info.max_entries = map->max_entries;
  1331. info.map_flags = map->map_flags;
  1332. if (copy_to_user(uinfo, &info, info_len) ||
  1333. put_user(info_len, &uattr->info.info_len))
  1334. return -EFAULT;
  1335. return 0;
  1336. }
  1337. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1338. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1339. union bpf_attr __user *uattr)
  1340. {
  1341. int ufd = attr->info.bpf_fd;
  1342. struct fd f;
  1343. int err;
  1344. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1345. return -EINVAL;
  1346. f = fdget(ufd);
  1347. if (!f.file)
  1348. return -EBADFD;
  1349. if (f.file->f_op == &bpf_prog_fops)
  1350. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1351. uattr);
  1352. else if (f.file->f_op == &bpf_map_fops)
  1353. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1354. uattr);
  1355. else
  1356. err = -EINVAL;
  1357. fdput(f);
  1358. return err;
  1359. }
  1360. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1361. {
  1362. union bpf_attr attr;
  1363. int err;
  1364. if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
  1365. return -EPERM;
  1366. err = check_uarg_tail_zero(uattr, sizeof(attr), size);
  1367. if (err)
  1368. return err;
  1369. size = min_t(u32, size, sizeof(attr));
  1370. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  1371. memset(&attr, 0, sizeof(attr));
  1372. if (copy_from_user(&attr, uattr, size) != 0)
  1373. return -EFAULT;
  1374. err = security_bpf(cmd, &attr, size);
  1375. if (err < 0)
  1376. return err;
  1377. switch (cmd) {
  1378. case BPF_MAP_CREATE:
  1379. err = map_create(&attr);
  1380. break;
  1381. case BPF_MAP_LOOKUP_ELEM:
  1382. err = map_lookup_elem(&attr);
  1383. break;
  1384. case BPF_MAP_UPDATE_ELEM:
  1385. err = map_update_elem(&attr);
  1386. break;
  1387. case BPF_MAP_DELETE_ELEM:
  1388. err = map_delete_elem(&attr);
  1389. break;
  1390. case BPF_MAP_GET_NEXT_KEY:
  1391. err = map_get_next_key(&attr);
  1392. break;
  1393. case BPF_PROG_LOAD:
  1394. err = bpf_prog_load(&attr);
  1395. break;
  1396. case BPF_OBJ_PIN:
  1397. err = bpf_obj_pin(&attr);
  1398. break;
  1399. case BPF_OBJ_GET:
  1400. err = bpf_obj_get(&attr);
  1401. break;
  1402. #ifdef CONFIG_CGROUP_BPF
  1403. case BPF_PROG_ATTACH:
  1404. err = bpf_prog_attach(&attr);
  1405. break;
  1406. case BPF_PROG_DETACH:
  1407. err = bpf_prog_detach(&attr);
  1408. break;
  1409. #endif
  1410. case BPF_PROG_TEST_RUN:
  1411. err = bpf_prog_test_run(&attr, uattr);
  1412. break;
  1413. case BPF_PROG_GET_NEXT_ID:
  1414. err = bpf_obj_get_next_id(&attr, uattr,
  1415. &prog_idr, &prog_idr_lock);
  1416. break;
  1417. case BPF_MAP_GET_NEXT_ID:
  1418. err = bpf_obj_get_next_id(&attr, uattr,
  1419. &map_idr, &map_idr_lock);
  1420. break;
  1421. case BPF_PROG_GET_FD_BY_ID:
  1422. err = bpf_prog_get_fd_by_id(&attr);
  1423. break;
  1424. case BPF_MAP_GET_FD_BY_ID:
  1425. err = bpf_map_get_fd_by_id(&attr);
  1426. break;
  1427. case BPF_OBJ_GET_INFO_BY_FD:
  1428. err = bpf_obj_get_info_by_fd(&attr, uattr);
  1429. break;
  1430. default:
  1431. err = -EINVAL;
  1432. break;
  1433. }
  1434. return err;
  1435. }