syscall.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. prog->aux->ops = bpf_prog_types[type];
  693. prog->type = type;
  694. return 0;
  695. }
  696. /* drop refcnt on maps used by eBPF program and free auxilary data */
  697. static void free_used_maps(struct bpf_prog_aux *aux)
  698. {
  699. int i;
  700. for (i = 0; i < aux->used_map_cnt; i++)
  701. bpf_map_put(aux->used_maps[i]);
  702. kfree(aux->used_maps);
  703. }
  704. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  705. {
  706. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  707. unsigned long user_bufs;
  708. if (user) {
  709. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  710. if (user_bufs > memlock_limit) {
  711. atomic_long_sub(pages, &user->locked_vm);
  712. return -EPERM;
  713. }
  714. }
  715. return 0;
  716. }
  717. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  718. {
  719. if (user)
  720. atomic_long_sub(pages, &user->locked_vm);
  721. }
  722. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  723. {
  724. struct user_struct *user = get_current_user();
  725. int ret;
  726. ret = __bpf_prog_charge(user, prog->pages);
  727. if (ret) {
  728. free_uid(user);
  729. return ret;
  730. }
  731. prog->aux->user = user;
  732. return 0;
  733. }
  734. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  735. {
  736. struct user_struct *user = prog->aux->user;
  737. __bpf_prog_uncharge(user, prog->pages);
  738. free_uid(user);
  739. }
  740. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  741. {
  742. int id;
  743. spin_lock_bh(&prog_idr_lock);
  744. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  745. if (id > 0)
  746. prog->aux->id = id;
  747. spin_unlock_bh(&prog_idr_lock);
  748. /* id is in [1, INT_MAX) */
  749. if (WARN_ON_ONCE(!id))
  750. return -ENOSPC;
  751. return id > 0 ? 0 : id;
  752. }
  753. static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  754. {
  755. /* cBPF to eBPF migrations are currently not in the idr store. */
  756. if (!prog->aux->id)
  757. return;
  758. if (do_idr_lock)
  759. spin_lock_bh(&prog_idr_lock);
  760. else
  761. __acquire(&prog_idr_lock);
  762. idr_remove(&prog_idr, prog->aux->id);
  763. if (do_idr_lock)
  764. spin_unlock_bh(&prog_idr_lock);
  765. else
  766. __release(&prog_idr_lock);
  767. }
  768. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  769. {
  770. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  771. free_used_maps(aux);
  772. bpf_prog_uncharge_memlock(aux->prog);
  773. security_bpf_prog_free(aux);
  774. bpf_prog_free(aux->prog);
  775. }
  776. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  777. {
  778. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  779. trace_bpf_prog_put_rcu(prog);
  780. /* bpf_prog_free_id() must be called first */
  781. bpf_prog_free_id(prog, do_idr_lock);
  782. bpf_prog_kallsyms_del(prog);
  783. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  784. }
  785. }
  786. void bpf_prog_put(struct bpf_prog *prog)
  787. {
  788. __bpf_prog_put(prog, true);
  789. }
  790. EXPORT_SYMBOL_GPL(bpf_prog_put);
  791. static int bpf_prog_release(struct inode *inode, struct file *filp)
  792. {
  793. struct bpf_prog *prog = filp->private_data;
  794. bpf_prog_put(prog);
  795. return 0;
  796. }
  797. #ifdef CONFIG_PROC_FS
  798. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  799. {
  800. const struct bpf_prog *prog = filp->private_data;
  801. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  802. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  803. seq_printf(m,
  804. "prog_type:\t%u\n"
  805. "prog_jited:\t%u\n"
  806. "prog_tag:\t%s\n"
  807. "memlock:\t%llu\n",
  808. prog->type,
  809. prog->jited,
  810. prog_tag,
  811. prog->pages * 1ULL << PAGE_SHIFT);
  812. }
  813. #endif
  814. const struct file_operations bpf_prog_fops = {
  815. #ifdef CONFIG_PROC_FS
  816. .show_fdinfo = bpf_prog_show_fdinfo,
  817. #endif
  818. .release = bpf_prog_release,
  819. .read = bpf_dummy_read,
  820. .write = bpf_dummy_write,
  821. };
  822. int bpf_prog_new_fd(struct bpf_prog *prog)
  823. {
  824. int ret;
  825. ret = security_bpf_prog(prog);
  826. if (ret < 0)
  827. return ret;
  828. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  829. O_RDWR | O_CLOEXEC);
  830. }
  831. static struct bpf_prog *____bpf_prog_get(struct fd f)
  832. {
  833. if (!f.file)
  834. return ERR_PTR(-EBADF);
  835. if (f.file->f_op != &bpf_prog_fops) {
  836. fdput(f);
  837. return ERR_PTR(-EINVAL);
  838. }
  839. return f.file->private_data;
  840. }
  841. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  842. {
  843. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  844. atomic_sub(i, &prog->aux->refcnt);
  845. return ERR_PTR(-EBUSY);
  846. }
  847. return prog;
  848. }
  849. EXPORT_SYMBOL_GPL(bpf_prog_add);
  850. void bpf_prog_sub(struct bpf_prog *prog, int i)
  851. {
  852. /* Only to be used for undoing previous bpf_prog_add() in some
  853. * error path. We still know that another entity in our call
  854. * path holds a reference to the program, thus atomic_sub() can
  855. * be safely used in such cases!
  856. */
  857. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  858. }
  859. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  860. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  861. {
  862. return bpf_prog_add(prog, 1);
  863. }
  864. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  865. /* prog_idr_lock should have been held */
  866. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  867. {
  868. int refold;
  869. refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
  870. if (refold >= BPF_MAX_REFCNT) {
  871. __bpf_prog_put(prog, false);
  872. return ERR_PTR(-EBUSY);
  873. }
  874. if (!refold)
  875. return ERR_PTR(-ENOENT);
  876. return prog;
  877. }
  878. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  879. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
  880. {
  881. struct fd f = fdget(ufd);
  882. struct bpf_prog *prog;
  883. prog = ____bpf_prog_get(f);
  884. if (IS_ERR(prog))
  885. return prog;
  886. if (type && prog->type != *type) {
  887. prog = ERR_PTR(-EINVAL);
  888. goto out;
  889. }
  890. prog = bpf_prog_inc(prog);
  891. out:
  892. fdput(f);
  893. return prog;
  894. }
  895. struct bpf_prog *bpf_prog_get(u32 ufd)
  896. {
  897. return __bpf_prog_get(ufd, NULL);
  898. }
  899. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  900. {
  901. struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
  902. if (!IS_ERR(prog))
  903. trace_bpf_prog_get_type(prog);
  904. return prog;
  905. }
  906. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  907. /* last field in 'union bpf_attr' used by this command */
  908. #define BPF_PROG_LOAD_LAST_FIELD prog_name
  909. static int bpf_prog_load(union bpf_attr *attr)
  910. {
  911. enum bpf_prog_type type = attr->prog_type;
  912. struct bpf_prog *prog;
  913. int err;
  914. char license[128];
  915. bool is_gpl;
  916. if (CHECK_ATTR(BPF_PROG_LOAD))
  917. return -EINVAL;
  918. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  919. return -EINVAL;
  920. /* copy eBPF program license from user space */
  921. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  922. sizeof(license) - 1) < 0)
  923. return -EFAULT;
  924. license[sizeof(license) - 1] = 0;
  925. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  926. is_gpl = license_is_gpl_compatible(license);
  927. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  928. return -E2BIG;
  929. if (type == BPF_PROG_TYPE_KPROBE &&
  930. attr->kern_version != LINUX_VERSION_CODE)
  931. return -EINVAL;
  932. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  933. type != BPF_PROG_TYPE_CGROUP_SKB &&
  934. !capable(CAP_SYS_ADMIN))
  935. return -EPERM;
  936. /* plain bpf_prog allocation */
  937. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  938. if (!prog)
  939. return -ENOMEM;
  940. err = security_bpf_prog_alloc(prog->aux);
  941. if (err)
  942. goto free_prog_nouncharge;
  943. err = bpf_prog_charge_memlock(prog);
  944. if (err)
  945. goto free_prog_sec;
  946. prog->len = attr->insn_cnt;
  947. err = -EFAULT;
  948. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  949. bpf_prog_insn_size(prog)) != 0)
  950. goto free_prog;
  951. prog->orig_prog = NULL;
  952. prog->jited = 0;
  953. atomic_set(&prog->aux->refcnt, 1);
  954. prog->gpl_compatible = is_gpl ? 1 : 0;
  955. /* find program type: socket_filter vs tracing_filter */
  956. err = find_prog_type(type, prog);
  957. if (err < 0)
  958. goto free_prog;
  959. prog->aux->load_time = ktime_get_boot_ns();
  960. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
  961. if (err)
  962. goto free_prog;
  963. /* run eBPF verifier */
  964. err = bpf_check(&prog, attr);
  965. if (err < 0)
  966. goto free_used_maps;
  967. /* eBPF program is ready to be JITed */
  968. prog = bpf_prog_select_runtime(prog, &err);
  969. if (err < 0)
  970. goto free_used_maps;
  971. err = bpf_prog_alloc_id(prog);
  972. if (err)
  973. goto free_used_maps;
  974. err = bpf_prog_new_fd(prog);
  975. if (err < 0) {
  976. /* failed to allocate fd.
  977. * bpf_prog_put() is needed because the above
  978. * bpf_prog_alloc_id() has published the prog
  979. * to the userspace and the userspace may
  980. * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
  981. */
  982. bpf_prog_put(prog);
  983. return err;
  984. }
  985. bpf_prog_kallsyms_add(prog);
  986. trace_bpf_prog_load(prog, err);
  987. return err;
  988. free_used_maps:
  989. free_used_maps(prog->aux);
  990. free_prog:
  991. bpf_prog_uncharge_memlock(prog);
  992. free_prog_sec:
  993. security_bpf_prog_free(prog->aux);
  994. free_prog_nouncharge:
  995. bpf_prog_free(prog);
  996. return err;
  997. }
  998. #define BPF_OBJ_LAST_FIELD file_flags
  999. static int bpf_obj_pin(const union bpf_attr *attr)
  1000. {
  1001. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  1002. return -EINVAL;
  1003. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  1004. }
  1005. static int bpf_obj_get(const union bpf_attr *attr)
  1006. {
  1007. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  1008. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  1009. return -EINVAL;
  1010. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  1011. attr->file_flags);
  1012. }
  1013. #ifdef CONFIG_CGROUP_BPF
  1014. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  1015. static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
  1016. {
  1017. struct bpf_prog *prog = NULL;
  1018. int ufd = attr->target_fd;
  1019. struct bpf_map *map;
  1020. struct fd f;
  1021. int err;
  1022. f = fdget(ufd);
  1023. map = __bpf_map_get(f);
  1024. if (IS_ERR(map))
  1025. return PTR_ERR(map);
  1026. if (attach) {
  1027. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  1028. BPF_PROG_TYPE_SK_SKB);
  1029. if (IS_ERR(prog)) {
  1030. fdput(f);
  1031. return PTR_ERR(prog);
  1032. }
  1033. }
  1034. err = sock_map_prog(map, prog, attr->attach_type);
  1035. if (err) {
  1036. fdput(f);
  1037. if (prog)
  1038. bpf_prog_put(prog);
  1039. return err;
  1040. }
  1041. fdput(f);
  1042. return 0;
  1043. }
  1044. #define BPF_F_ATTACH_MASK \
  1045. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
  1046. static int bpf_prog_attach(const union bpf_attr *attr)
  1047. {
  1048. enum bpf_prog_type ptype;
  1049. struct bpf_prog *prog;
  1050. struct cgroup *cgrp;
  1051. int ret;
  1052. if (!capable(CAP_NET_ADMIN))
  1053. return -EPERM;
  1054. if (CHECK_ATTR(BPF_PROG_ATTACH))
  1055. return -EINVAL;
  1056. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  1057. return -EINVAL;
  1058. switch (attr->attach_type) {
  1059. case BPF_CGROUP_INET_INGRESS:
  1060. case BPF_CGROUP_INET_EGRESS:
  1061. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1062. break;
  1063. case BPF_CGROUP_INET_SOCK_CREATE:
  1064. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1065. break;
  1066. case BPF_CGROUP_SOCK_OPS:
  1067. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1068. break;
  1069. case BPF_SK_SKB_STREAM_PARSER:
  1070. case BPF_SK_SKB_STREAM_VERDICT:
  1071. return sockmap_get_from_fd(attr, true);
  1072. default:
  1073. return -EINVAL;
  1074. }
  1075. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1076. if (IS_ERR(prog))
  1077. return PTR_ERR(prog);
  1078. cgrp = cgroup_get_from_fd(attr->target_fd);
  1079. if (IS_ERR(cgrp)) {
  1080. bpf_prog_put(prog);
  1081. return PTR_ERR(cgrp);
  1082. }
  1083. ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
  1084. attr->attach_flags);
  1085. if (ret)
  1086. bpf_prog_put(prog);
  1087. cgroup_put(cgrp);
  1088. return ret;
  1089. }
  1090. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1091. static int bpf_prog_detach(const union bpf_attr *attr)
  1092. {
  1093. enum bpf_prog_type ptype;
  1094. struct bpf_prog *prog;
  1095. struct cgroup *cgrp;
  1096. int ret;
  1097. if (!capable(CAP_NET_ADMIN))
  1098. return -EPERM;
  1099. if (CHECK_ATTR(BPF_PROG_DETACH))
  1100. return -EINVAL;
  1101. switch (attr->attach_type) {
  1102. case BPF_CGROUP_INET_INGRESS:
  1103. case BPF_CGROUP_INET_EGRESS:
  1104. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1105. break;
  1106. case BPF_CGROUP_INET_SOCK_CREATE:
  1107. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1108. break;
  1109. case BPF_CGROUP_SOCK_OPS:
  1110. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1111. break;
  1112. case BPF_SK_SKB_STREAM_PARSER:
  1113. case BPF_SK_SKB_STREAM_VERDICT:
  1114. return sockmap_get_from_fd(attr, false);
  1115. default:
  1116. return -EINVAL;
  1117. }
  1118. cgrp = cgroup_get_from_fd(attr->target_fd);
  1119. if (IS_ERR(cgrp))
  1120. return PTR_ERR(cgrp);
  1121. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1122. if (IS_ERR(prog))
  1123. prog = NULL;
  1124. ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
  1125. if (prog)
  1126. bpf_prog_put(prog);
  1127. cgroup_put(cgrp);
  1128. return ret;
  1129. }
  1130. #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
  1131. static int bpf_prog_query(const union bpf_attr *attr,
  1132. union bpf_attr __user *uattr)
  1133. {
  1134. struct cgroup *cgrp;
  1135. int ret;
  1136. if (!capable(CAP_NET_ADMIN))
  1137. return -EPERM;
  1138. if (CHECK_ATTR(BPF_PROG_QUERY))
  1139. return -EINVAL;
  1140. if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
  1141. return -EINVAL;
  1142. switch (attr->query.attach_type) {
  1143. case BPF_CGROUP_INET_INGRESS:
  1144. case BPF_CGROUP_INET_EGRESS:
  1145. case BPF_CGROUP_INET_SOCK_CREATE:
  1146. case BPF_CGROUP_SOCK_OPS:
  1147. break;
  1148. default:
  1149. return -EINVAL;
  1150. }
  1151. cgrp = cgroup_get_from_fd(attr->query.target_fd);
  1152. if (IS_ERR(cgrp))
  1153. return PTR_ERR(cgrp);
  1154. ret = cgroup_bpf_query(cgrp, attr, uattr);
  1155. cgroup_put(cgrp);
  1156. return ret;
  1157. }
  1158. #endif /* CONFIG_CGROUP_BPF */
  1159. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1160. static int bpf_prog_test_run(const union bpf_attr *attr,
  1161. union bpf_attr __user *uattr)
  1162. {
  1163. struct bpf_prog *prog;
  1164. int ret = -ENOTSUPP;
  1165. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1166. return -EINVAL;
  1167. prog = bpf_prog_get(attr->test.prog_fd);
  1168. if (IS_ERR(prog))
  1169. return PTR_ERR(prog);
  1170. if (prog->aux->ops->test_run)
  1171. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1172. bpf_prog_put(prog);
  1173. return ret;
  1174. }
  1175. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1176. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1177. union bpf_attr __user *uattr,
  1178. struct idr *idr,
  1179. spinlock_t *lock)
  1180. {
  1181. u32 next_id = attr->start_id;
  1182. int err = 0;
  1183. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1184. return -EINVAL;
  1185. if (!capable(CAP_SYS_ADMIN))
  1186. return -EPERM;
  1187. next_id++;
  1188. spin_lock_bh(lock);
  1189. if (!idr_get_next(idr, &next_id))
  1190. err = -ENOENT;
  1191. spin_unlock_bh(lock);
  1192. if (!err)
  1193. err = put_user(next_id, &uattr->next_id);
  1194. return err;
  1195. }
  1196. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1197. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1198. {
  1199. struct bpf_prog *prog;
  1200. u32 id = attr->prog_id;
  1201. int fd;
  1202. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1203. return -EINVAL;
  1204. if (!capable(CAP_SYS_ADMIN))
  1205. return -EPERM;
  1206. spin_lock_bh(&prog_idr_lock);
  1207. prog = idr_find(&prog_idr, id);
  1208. if (prog)
  1209. prog = bpf_prog_inc_not_zero(prog);
  1210. else
  1211. prog = ERR_PTR(-ENOENT);
  1212. spin_unlock_bh(&prog_idr_lock);
  1213. if (IS_ERR(prog))
  1214. return PTR_ERR(prog);
  1215. fd = bpf_prog_new_fd(prog);
  1216. if (fd < 0)
  1217. bpf_prog_put(prog);
  1218. return fd;
  1219. }
  1220. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  1221. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1222. {
  1223. struct bpf_map *map;
  1224. u32 id = attr->map_id;
  1225. int f_flags;
  1226. int fd;
  1227. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  1228. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  1229. return -EINVAL;
  1230. if (!capable(CAP_SYS_ADMIN))
  1231. return -EPERM;
  1232. f_flags = bpf_get_file_flag(attr->open_flags);
  1233. if (f_flags < 0)
  1234. return f_flags;
  1235. spin_lock_bh(&map_idr_lock);
  1236. map = idr_find(&map_idr, id);
  1237. if (map)
  1238. map = bpf_map_inc_not_zero(map, true);
  1239. else
  1240. map = ERR_PTR(-ENOENT);
  1241. spin_unlock_bh(&map_idr_lock);
  1242. if (IS_ERR(map))
  1243. return PTR_ERR(map);
  1244. fd = bpf_map_new_fd(map, f_flags);
  1245. if (fd < 0)
  1246. bpf_map_put(map);
  1247. return fd;
  1248. }
  1249. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1250. const union bpf_attr *attr,
  1251. union bpf_attr __user *uattr)
  1252. {
  1253. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1254. struct bpf_prog_info info = {};
  1255. u32 info_len = attr->info.info_len;
  1256. char __user *uinsns;
  1257. u32 ulen;
  1258. int err;
  1259. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1260. if (err)
  1261. return err;
  1262. info_len = min_t(u32, sizeof(info), info_len);
  1263. if (copy_from_user(&info, uinfo, info_len))
  1264. return -EFAULT;
  1265. info.type = prog->type;
  1266. info.id = prog->aux->id;
  1267. info.load_time = prog->aux->load_time;
  1268. info.created_by_uid = from_kuid_munged(current_user_ns(),
  1269. prog->aux->user->uid);
  1270. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1271. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  1272. ulen = info.nr_map_ids;
  1273. info.nr_map_ids = prog->aux->used_map_cnt;
  1274. ulen = min_t(u32, info.nr_map_ids, ulen);
  1275. if (ulen) {
  1276. u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
  1277. u32 i;
  1278. for (i = 0; i < ulen; i++)
  1279. if (put_user(prog->aux->used_maps[i]->id,
  1280. &user_map_ids[i]))
  1281. return -EFAULT;
  1282. }
  1283. if (!capable(CAP_SYS_ADMIN)) {
  1284. info.jited_prog_len = 0;
  1285. info.xlated_prog_len = 0;
  1286. goto done;
  1287. }
  1288. ulen = info.jited_prog_len;
  1289. info.jited_prog_len = prog->jited_len;
  1290. if (info.jited_prog_len && ulen) {
  1291. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1292. ulen = min_t(u32, info.jited_prog_len, ulen);
  1293. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1294. return -EFAULT;
  1295. }
  1296. ulen = info.xlated_prog_len;
  1297. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1298. if (info.xlated_prog_len && ulen) {
  1299. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1300. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1301. if (copy_to_user(uinsns, prog->insnsi, ulen))
  1302. return -EFAULT;
  1303. }
  1304. done:
  1305. if (copy_to_user(uinfo, &info, info_len) ||
  1306. put_user(info_len, &uattr->info.info_len))
  1307. return -EFAULT;
  1308. return 0;
  1309. }
  1310. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1311. const union bpf_attr *attr,
  1312. union bpf_attr __user *uattr)
  1313. {
  1314. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1315. struct bpf_map_info info = {};
  1316. u32 info_len = attr->info.info_len;
  1317. int err;
  1318. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1319. if (err)
  1320. return err;
  1321. info_len = min_t(u32, sizeof(info), info_len);
  1322. info.type = map->map_type;
  1323. info.id = map->id;
  1324. info.key_size = map->key_size;
  1325. info.value_size = map->value_size;
  1326. info.max_entries = map->max_entries;
  1327. info.map_flags = map->map_flags;
  1328. memcpy(info.name, map->name, sizeof(map->name));
  1329. if (copy_to_user(uinfo, &info, info_len) ||
  1330. put_user(info_len, &uattr->info.info_len))
  1331. return -EFAULT;
  1332. return 0;
  1333. }
  1334. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1335. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1336. union bpf_attr __user *uattr)
  1337. {
  1338. int ufd = attr->info.bpf_fd;
  1339. struct fd f;
  1340. int err;
  1341. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1342. return -EINVAL;
  1343. f = fdget(ufd);
  1344. if (!f.file)
  1345. return -EBADFD;
  1346. if (f.file->f_op == &bpf_prog_fops)
  1347. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1348. uattr);
  1349. else if (f.file->f_op == &bpf_map_fops)
  1350. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1351. uattr);
  1352. else
  1353. err = -EINVAL;
  1354. fdput(f);
  1355. return err;
  1356. }
  1357. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1358. {
  1359. union bpf_attr attr = {};
  1360. int err;
  1361. if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  1362. return -EPERM;
  1363. err = check_uarg_tail_zero(uattr, sizeof(attr), size);
  1364. if (err)
  1365. return err;
  1366. size = min_t(u32, size, sizeof(attr));
  1367. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  1368. if (copy_from_user(&attr, uattr, size) != 0)
  1369. return -EFAULT;
  1370. err = security_bpf(cmd, &attr, size);
  1371. if (err < 0)
  1372. return err;
  1373. switch (cmd) {
  1374. case BPF_MAP_CREATE:
  1375. err = map_create(&attr);
  1376. break;
  1377. case BPF_MAP_LOOKUP_ELEM:
  1378. err = map_lookup_elem(&attr);
  1379. break;
  1380. case BPF_MAP_UPDATE_ELEM:
  1381. err = map_update_elem(&attr);
  1382. break;
  1383. case BPF_MAP_DELETE_ELEM:
  1384. err = map_delete_elem(&attr);
  1385. break;
  1386. case BPF_MAP_GET_NEXT_KEY:
  1387. err = map_get_next_key(&attr);
  1388. break;
  1389. case BPF_PROG_LOAD:
  1390. err = bpf_prog_load(&attr);
  1391. break;
  1392. case BPF_OBJ_PIN:
  1393. err = bpf_obj_pin(&attr);
  1394. break;
  1395. case BPF_OBJ_GET:
  1396. err = bpf_obj_get(&attr);
  1397. break;
  1398. #ifdef CONFIG_CGROUP_BPF
  1399. case BPF_PROG_ATTACH:
  1400. err = bpf_prog_attach(&attr);
  1401. break;
  1402. case BPF_PROG_DETACH:
  1403. err = bpf_prog_detach(&attr);
  1404. break;
  1405. case BPF_PROG_QUERY:
  1406. err = bpf_prog_query(&attr, uattr);
  1407. break;
  1408. #endif
  1409. case BPF_PROG_TEST_RUN:
  1410. err = bpf_prog_test_run(&attr, uattr);
  1411. break;
  1412. case BPF_PROG_GET_NEXT_ID:
  1413. err = bpf_obj_get_next_id(&attr, uattr,
  1414. &prog_idr, &prog_idr_lock);
  1415. break;
  1416. case BPF_MAP_GET_NEXT_ID:
  1417. err = bpf_obj_get_next_id(&attr, uattr,
  1418. &map_idr, &map_idr_lock);
  1419. break;
  1420. case BPF_PROG_GET_FD_BY_ID:
  1421. err = bpf_prog_get_fd_by_id(&attr);
  1422. break;
  1423. case BPF_MAP_GET_FD_BY_ID:
  1424. err = bpf_map_get_fd_by_id(&attr);
  1425. break;
  1426. case BPF_OBJ_GET_INFO_BY_FD:
  1427. err = bpf_obj_get_info_by_fd(&attr, uattr);
  1428. break;
  1429. default:
  1430. err = -EINVAL;
  1431. break;
  1432. }
  1433. return err;
  1434. }