syscall.c 34 KB

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