syscall.c 32 KB

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