syscall.c 38 KB

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