syscall.c 33 KB

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