syscall.c 39 KB

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