syscall.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. DEFINE_PER_CPU(int, bpf_prog_active);
  26. int sysctl_unprivileged_bpf_disabled __read_mostly;
  27. static const struct bpf_map_ops * const bpf_map_types[] = {
  28. #define BPF_PROG_TYPE(_id, _ops)
  29. #define BPF_MAP_TYPE(_id, _ops) \
  30. [_id] = &_ops,
  31. #include <linux/bpf_types.h>
  32. #undef BPF_PROG_TYPE
  33. #undef BPF_MAP_TYPE
  34. };
  35. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  36. {
  37. struct bpf_map *map;
  38. if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
  39. !bpf_map_types[attr->map_type])
  40. return ERR_PTR(-EINVAL);
  41. map = bpf_map_types[attr->map_type]->map_alloc(attr);
  42. if (IS_ERR(map))
  43. return map;
  44. map->ops = bpf_map_types[attr->map_type];
  45. map->map_type = attr->map_type;
  46. return map;
  47. }
  48. void *bpf_map_area_alloc(size_t size)
  49. {
  50. /* We definitely need __GFP_NORETRY, so OOM killer doesn't
  51. * trigger under memory pressure as we really just want to
  52. * fail instead.
  53. */
  54. const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
  55. void *area;
  56. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  57. area = kmalloc(size, GFP_USER | flags);
  58. if (area != NULL)
  59. return area;
  60. }
  61. return __vmalloc(size, GFP_KERNEL | flags, PAGE_KERNEL);
  62. }
  63. void bpf_map_area_free(void *area)
  64. {
  65. kvfree(area);
  66. }
  67. int bpf_map_precharge_memlock(u32 pages)
  68. {
  69. struct user_struct *user = get_current_user();
  70. unsigned long memlock_limit, cur;
  71. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  72. cur = atomic_long_read(&user->locked_vm);
  73. free_uid(user);
  74. if (cur + pages > memlock_limit)
  75. return -EPERM;
  76. return 0;
  77. }
  78. static int bpf_map_charge_memlock(struct bpf_map *map)
  79. {
  80. struct user_struct *user = get_current_user();
  81. unsigned long memlock_limit;
  82. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  83. atomic_long_add(map->pages, &user->locked_vm);
  84. if (atomic_long_read(&user->locked_vm) > memlock_limit) {
  85. atomic_long_sub(map->pages, &user->locked_vm);
  86. free_uid(user);
  87. return -EPERM;
  88. }
  89. map->user = user;
  90. return 0;
  91. }
  92. static void bpf_map_uncharge_memlock(struct bpf_map *map)
  93. {
  94. struct user_struct *user = map->user;
  95. atomic_long_sub(map->pages, &user->locked_vm);
  96. free_uid(user);
  97. }
  98. /* called from workqueue */
  99. static void bpf_map_free_deferred(struct work_struct *work)
  100. {
  101. struct bpf_map *map = container_of(work, struct bpf_map, work);
  102. bpf_map_uncharge_memlock(map);
  103. /* implementation dependent freeing */
  104. map->ops->map_free(map);
  105. }
  106. static void bpf_map_put_uref(struct bpf_map *map)
  107. {
  108. if (atomic_dec_and_test(&map->usercnt)) {
  109. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
  110. bpf_fd_array_map_clear(map);
  111. }
  112. }
  113. /* decrement map refcnt and schedule it for freeing via workqueue
  114. * (unrelying map implementation ops->map_free() might sleep)
  115. */
  116. void bpf_map_put(struct bpf_map *map)
  117. {
  118. if (atomic_dec_and_test(&map->refcnt)) {
  119. INIT_WORK(&map->work, bpf_map_free_deferred);
  120. schedule_work(&map->work);
  121. }
  122. }
  123. void bpf_map_put_with_uref(struct bpf_map *map)
  124. {
  125. bpf_map_put_uref(map);
  126. bpf_map_put(map);
  127. }
  128. static int bpf_map_release(struct inode *inode, struct file *filp)
  129. {
  130. struct bpf_map *map = filp->private_data;
  131. if (map->ops->map_release)
  132. map->ops->map_release(map, filp);
  133. bpf_map_put_with_uref(map);
  134. return 0;
  135. }
  136. #ifdef CONFIG_PROC_FS
  137. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  138. {
  139. const struct bpf_map *map = filp->private_data;
  140. const struct bpf_array *array;
  141. u32 owner_prog_type = 0;
  142. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
  143. array = container_of(map, struct bpf_array, map);
  144. owner_prog_type = array->owner_prog_type;
  145. }
  146. seq_printf(m,
  147. "map_type:\t%u\n"
  148. "key_size:\t%u\n"
  149. "value_size:\t%u\n"
  150. "max_entries:\t%u\n"
  151. "map_flags:\t%#x\n"
  152. "memlock:\t%llu\n",
  153. map->map_type,
  154. map->key_size,
  155. map->value_size,
  156. map->max_entries,
  157. map->map_flags,
  158. map->pages * 1ULL << PAGE_SHIFT);
  159. if (owner_prog_type)
  160. seq_printf(m, "owner_prog_type:\t%u\n",
  161. owner_prog_type);
  162. }
  163. #endif
  164. static const struct file_operations bpf_map_fops = {
  165. #ifdef CONFIG_PROC_FS
  166. .show_fdinfo = bpf_map_show_fdinfo,
  167. #endif
  168. .release = bpf_map_release,
  169. };
  170. int bpf_map_new_fd(struct bpf_map *map)
  171. {
  172. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  173. O_RDWR | O_CLOEXEC);
  174. }
  175. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  176. #define CHECK_ATTR(CMD) \
  177. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  178. sizeof(attr->CMD##_LAST_FIELD), 0, \
  179. sizeof(*attr) - \
  180. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  181. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  182. #define BPF_MAP_CREATE_LAST_FIELD inner_map_fd
  183. /* called via syscall */
  184. static int map_create(union bpf_attr *attr)
  185. {
  186. struct bpf_map *map;
  187. int err;
  188. err = CHECK_ATTR(BPF_MAP_CREATE);
  189. if (err)
  190. return -EINVAL;
  191. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  192. map = find_and_alloc_map(attr);
  193. if (IS_ERR(map))
  194. return PTR_ERR(map);
  195. atomic_set(&map->refcnt, 1);
  196. atomic_set(&map->usercnt, 1);
  197. err = bpf_map_charge_memlock(map);
  198. if (err)
  199. goto free_map_nouncharge;
  200. err = bpf_map_new_fd(map);
  201. if (err < 0)
  202. /* failed to allocate fd */
  203. goto free_map;
  204. trace_bpf_map_create(map, err);
  205. return err;
  206. free_map:
  207. bpf_map_uncharge_memlock(map);
  208. free_map_nouncharge:
  209. map->ops->map_free(map);
  210. return err;
  211. }
  212. /* if error is returned, fd is released.
  213. * On success caller should complete fd access with matching fdput()
  214. */
  215. struct bpf_map *__bpf_map_get(struct fd f)
  216. {
  217. if (!f.file)
  218. return ERR_PTR(-EBADF);
  219. if (f.file->f_op != &bpf_map_fops) {
  220. fdput(f);
  221. return ERR_PTR(-EINVAL);
  222. }
  223. return f.file->private_data;
  224. }
  225. /* prog's and map's refcnt limit */
  226. #define BPF_MAX_REFCNT 32768
  227. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  228. {
  229. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  230. atomic_dec(&map->refcnt);
  231. return ERR_PTR(-EBUSY);
  232. }
  233. if (uref)
  234. atomic_inc(&map->usercnt);
  235. return map;
  236. }
  237. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  238. {
  239. struct fd f = fdget(ufd);
  240. struct bpf_map *map;
  241. map = __bpf_map_get(f);
  242. if (IS_ERR(map))
  243. return map;
  244. map = bpf_map_inc(map, true);
  245. fdput(f);
  246. return map;
  247. }
  248. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  249. {
  250. return -ENOTSUPP;
  251. }
  252. /* last field in 'union bpf_attr' used by this command */
  253. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  254. static int map_lookup_elem(union bpf_attr *attr)
  255. {
  256. void __user *ukey = u64_to_user_ptr(attr->key);
  257. void __user *uvalue = u64_to_user_ptr(attr->value);
  258. int ufd = attr->map_fd;
  259. struct bpf_map *map;
  260. void *key, *value, *ptr;
  261. u32 value_size;
  262. struct fd f;
  263. int err;
  264. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  265. return -EINVAL;
  266. f = fdget(ufd);
  267. map = __bpf_map_get(f);
  268. if (IS_ERR(map))
  269. return PTR_ERR(map);
  270. err = -ENOMEM;
  271. key = kmalloc(map->key_size, GFP_USER);
  272. if (!key)
  273. goto err_put;
  274. err = -EFAULT;
  275. if (copy_from_user(key, ukey, map->key_size) != 0)
  276. goto free_key;
  277. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  278. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  279. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  280. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  281. else
  282. value_size = map->value_size;
  283. err = -ENOMEM;
  284. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  285. if (!value)
  286. goto free_key;
  287. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  288. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  289. err = bpf_percpu_hash_copy(map, key, value);
  290. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  291. err = bpf_percpu_array_copy(map, key, value);
  292. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  293. err = bpf_stackmap_copy(map, key, value);
  294. } else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
  295. map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  296. err = -ENOTSUPP;
  297. } else {
  298. rcu_read_lock();
  299. ptr = map->ops->map_lookup_elem(map, key);
  300. if (ptr)
  301. memcpy(value, ptr, value_size);
  302. rcu_read_unlock();
  303. err = ptr ? 0 : -ENOENT;
  304. }
  305. if (err)
  306. goto free_value;
  307. err = -EFAULT;
  308. if (copy_to_user(uvalue, value, value_size) != 0)
  309. goto free_value;
  310. trace_bpf_map_lookup_elem(map, ufd, key, value);
  311. err = 0;
  312. free_value:
  313. kfree(value);
  314. free_key:
  315. kfree(key);
  316. err_put:
  317. fdput(f);
  318. return err;
  319. }
  320. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  321. static int map_update_elem(union bpf_attr *attr)
  322. {
  323. void __user *ukey = u64_to_user_ptr(attr->key);
  324. void __user *uvalue = u64_to_user_ptr(attr->value);
  325. int ufd = attr->map_fd;
  326. struct bpf_map *map;
  327. void *key, *value;
  328. u32 value_size;
  329. struct fd f;
  330. int err;
  331. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  332. return -EINVAL;
  333. f = fdget(ufd);
  334. map = __bpf_map_get(f);
  335. if (IS_ERR(map))
  336. return PTR_ERR(map);
  337. err = -ENOMEM;
  338. key = kmalloc(map->key_size, GFP_USER);
  339. if (!key)
  340. goto err_put;
  341. err = -EFAULT;
  342. if (copy_from_user(key, ukey, map->key_size) != 0)
  343. goto free_key;
  344. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  345. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  346. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  347. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  348. else
  349. value_size = map->value_size;
  350. err = -ENOMEM;
  351. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  352. if (!value)
  353. goto free_key;
  354. err = -EFAULT;
  355. if (copy_from_user(value, uvalue, value_size) != 0)
  356. goto free_value;
  357. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  358. * inside bpf map update or delete otherwise deadlocks are possible
  359. */
  360. preempt_disable();
  361. __this_cpu_inc(bpf_prog_active);
  362. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  363. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  364. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  365. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  366. err = bpf_percpu_array_update(map, key, value, attr->flags);
  367. } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
  368. map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  369. map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
  370. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
  371. rcu_read_lock();
  372. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  373. attr->flags);
  374. rcu_read_unlock();
  375. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  376. rcu_read_lock();
  377. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  378. attr->flags);
  379. rcu_read_unlock();
  380. } else {
  381. rcu_read_lock();
  382. err = map->ops->map_update_elem(map, key, value, attr->flags);
  383. rcu_read_unlock();
  384. }
  385. __this_cpu_dec(bpf_prog_active);
  386. preempt_enable();
  387. if (!err)
  388. trace_bpf_map_update_elem(map, ufd, key, value);
  389. free_value:
  390. kfree(value);
  391. free_key:
  392. kfree(key);
  393. err_put:
  394. fdput(f);
  395. return err;
  396. }
  397. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  398. static int map_delete_elem(union bpf_attr *attr)
  399. {
  400. void __user *ukey = u64_to_user_ptr(attr->key);
  401. int ufd = attr->map_fd;
  402. struct bpf_map *map;
  403. struct fd f;
  404. void *key;
  405. int err;
  406. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  407. return -EINVAL;
  408. f = fdget(ufd);
  409. map = __bpf_map_get(f);
  410. if (IS_ERR(map))
  411. return PTR_ERR(map);
  412. err = -ENOMEM;
  413. key = kmalloc(map->key_size, GFP_USER);
  414. if (!key)
  415. goto err_put;
  416. err = -EFAULT;
  417. if (copy_from_user(key, ukey, map->key_size) != 0)
  418. goto free_key;
  419. preempt_disable();
  420. __this_cpu_inc(bpf_prog_active);
  421. rcu_read_lock();
  422. err = map->ops->map_delete_elem(map, key);
  423. rcu_read_unlock();
  424. __this_cpu_dec(bpf_prog_active);
  425. preempt_enable();
  426. if (!err)
  427. trace_bpf_map_delete_elem(map, ufd, key);
  428. free_key:
  429. kfree(key);
  430. err_put:
  431. fdput(f);
  432. return err;
  433. }
  434. /* last field in 'union bpf_attr' used by this command */
  435. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  436. static int map_get_next_key(union bpf_attr *attr)
  437. {
  438. void __user *ukey = u64_to_user_ptr(attr->key);
  439. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  440. int ufd = attr->map_fd;
  441. struct bpf_map *map;
  442. void *key, *next_key;
  443. struct fd f;
  444. int err;
  445. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  446. return -EINVAL;
  447. f = fdget(ufd);
  448. map = __bpf_map_get(f);
  449. if (IS_ERR(map))
  450. return PTR_ERR(map);
  451. if (ukey) {
  452. err = -ENOMEM;
  453. key = kmalloc(map->key_size, GFP_USER);
  454. if (!key)
  455. goto err_put;
  456. err = -EFAULT;
  457. if (copy_from_user(key, ukey, map->key_size) != 0)
  458. goto free_key;
  459. } else {
  460. key = NULL;
  461. }
  462. err = -ENOMEM;
  463. next_key = kmalloc(map->key_size, GFP_USER);
  464. if (!next_key)
  465. goto free_key;
  466. rcu_read_lock();
  467. err = map->ops->map_get_next_key(map, key, next_key);
  468. rcu_read_unlock();
  469. if (err)
  470. goto free_next_key;
  471. err = -EFAULT;
  472. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  473. goto free_next_key;
  474. trace_bpf_map_next_key(map, ufd, key, next_key);
  475. err = 0;
  476. free_next_key:
  477. kfree(next_key);
  478. free_key:
  479. kfree(key);
  480. err_put:
  481. fdput(f);
  482. return err;
  483. }
  484. static const struct bpf_verifier_ops * const bpf_prog_types[] = {
  485. #define BPF_PROG_TYPE(_id, _ops) \
  486. [_id] = &_ops,
  487. #define BPF_MAP_TYPE(_id, _ops)
  488. #include <linux/bpf_types.h>
  489. #undef BPF_PROG_TYPE
  490. #undef BPF_MAP_TYPE
  491. };
  492. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  493. {
  494. if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
  495. return -EINVAL;
  496. prog->aux->ops = bpf_prog_types[type];
  497. prog->type = type;
  498. return 0;
  499. }
  500. /* drop refcnt on maps used by eBPF program and free auxilary data */
  501. static void free_used_maps(struct bpf_prog_aux *aux)
  502. {
  503. int i;
  504. for (i = 0; i < aux->used_map_cnt; i++)
  505. bpf_map_put(aux->used_maps[i]);
  506. kfree(aux->used_maps);
  507. }
  508. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  509. {
  510. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  511. unsigned long user_bufs;
  512. if (user) {
  513. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  514. if (user_bufs > memlock_limit) {
  515. atomic_long_sub(pages, &user->locked_vm);
  516. return -EPERM;
  517. }
  518. }
  519. return 0;
  520. }
  521. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  522. {
  523. if (user)
  524. atomic_long_sub(pages, &user->locked_vm);
  525. }
  526. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  527. {
  528. struct user_struct *user = get_current_user();
  529. int ret;
  530. ret = __bpf_prog_charge(user, prog->pages);
  531. if (ret) {
  532. free_uid(user);
  533. return ret;
  534. }
  535. prog->aux->user = user;
  536. return 0;
  537. }
  538. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  539. {
  540. struct user_struct *user = prog->aux->user;
  541. __bpf_prog_uncharge(user, prog->pages);
  542. free_uid(user);
  543. }
  544. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  545. {
  546. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  547. free_used_maps(aux);
  548. bpf_prog_uncharge_memlock(aux->prog);
  549. bpf_prog_free(aux->prog);
  550. }
  551. void bpf_prog_put(struct bpf_prog *prog)
  552. {
  553. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  554. trace_bpf_prog_put_rcu(prog);
  555. bpf_prog_kallsyms_del(prog);
  556. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  557. }
  558. }
  559. EXPORT_SYMBOL_GPL(bpf_prog_put);
  560. static int bpf_prog_release(struct inode *inode, struct file *filp)
  561. {
  562. struct bpf_prog *prog = filp->private_data;
  563. bpf_prog_put(prog);
  564. return 0;
  565. }
  566. #ifdef CONFIG_PROC_FS
  567. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  568. {
  569. const struct bpf_prog *prog = filp->private_data;
  570. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  571. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  572. seq_printf(m,
  573. "prog_type:\t%u\n"
  574. "prog_jited:\t%u\n"
  575. "prog_tag:\t%s\n"
  576. "memlock:\t%llu\n",
  577. prog->type,
  578. prog->jited,
  579. prog_tag,
  580. prog->pages * 1ULL << PAGE_SHIFT);
  581. }
  582. #endif
  583. static const struct file_operations bpf_prog_fops = {
  584. #ifdef CONFIG_PROC_FS
  585. .show_fdinfo = bpf_prog_show_fdinfo,
  586. #endif
  587. .release = bpf_prog_release,
  588. };
  589. int bpf_prog_new_fd(struct bpf_prog *prog)
  590. {
  591. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  592. O_RDWR | O_CLOEXEC);
  593. }
  594. static struct bpf_prog *____bpf_prog_get(struct fd f)
  595. {
  596. if (!f.file)
  597. return ERR_PTR(-EBADF);
  598. if (f.file->f_op != &bpf_prog_fops) {
  599. fdput(f);
  600. return ERR_PTR(-EINVAL);
  601. }
  602. return f.file->private_data;
  603. }
  604. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  605. {
  606. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  607. atomic_sub(i, &prog->aux->refcnt);
  608. return ERR_PTR(-EBUSY);
  609. }
  610. return prog;
  611. }
  612. EXPORT_SYMBOL_GPL(bpf_prog_add);
  613. void bpf_prog_sub(struct bpf_prog *prog, int i)
  614. {
  615. /* Only to be used for undoing previous bpf_prog_add() in some
  616. * error path. We still know that another entity in our call
  617. * path holds a reference to the program, thus atomic_sub() can
  618. * be safely used in such cases!
  619. */
  620. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  621. }
  622. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  623. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  624. {
  625. return bpf_prog_add(prog, 1);
  626. }
  627. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  628. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
  629. {
  630. struct fd f = fdget(ufd);
  631. struct bpf_prog *prog;
  632. prog = ____bpf_prog_get(f);
  633. if (IS_ERR(prog))
  634. return prog;
  635. if (type && prog->type != *type) {
  636. prog = ERR_PTR(-EINVAL);
  637. goto out;
  638. }
  639. prog = bpf_prog_inc(prog);
  640. out:
  641. fdput(f);
  642. return prog;
  643. }
  644. struct bpf_prog *bpf_prog_get(u32 ufd)
  645. {
  646. return __bpf_prog_get(ufd, NULL);
  647. }
  648. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  649. {
  650. struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
  651. if (!IS_ERR(prog))
  652. trace_bpf_prog_get_type(prog);
  653. return prog;
  654. }
  655. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  656. /* last field in 'union bpf_attr' used by this command */
  657. #define BPF_PROG_LOAD_LAST_FIELD prog_flags
  658. static int bpf_prog_load(union bpf_attr *attr)
  659. {
  660. enum bpf_prog_type type = attr->prog_type;
  661. struct bpf_prog *prog;
  662. int err;
  663. char license[128];
  664. bool is_gpl;
  665. if (CHECK_ATTR(BPF_PROG_LOAD))
  666. return -EINVAL;
  667. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  668. return -EINVAL;
  669. /* copy eBPF program license from user space */
  670. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  671. sizeof(license) - 1) < 0)
  672. return -EFAULT;
  673. license[sizeof(license) - 1] = 0;
  674. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  675. is_gpl = license_is_gpl_compatible(license);
  676. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  677. return -E2BIG;
  678. if (type == BPF_PROG_TYPE_KPROBE &&
  679. attr->kern_version != LINUX_VERSION_CODE)
  680. return -EINVAL;
  681. if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
  682. return -EPERM;
  683. /* plain bpf_prog allocation */
  684. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  685. if (!prog)
  686. return -ENOMEM;
  687. err = bpf_prog_charge_memlock(prog);
  688. if (err)
  689. goto free_prog_nouncharge;
  690. prog->len = attr->insn_cnt;
  691. err = -EFAULT;
  692. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  693. bpf_prog_insn_size(prog)) != 0)
  694. goto free_prog;
  695. prog->orig_prog = NULL;
  696. prog->jited = 0;
  697. atomic_set(&prog->aux->refcnt, 1);
  698. prog->gpl_compatible = is_gpl ? 1 : 0;
  699. /* find program type: socket_filter vs tracing_filter */
  700. err = find_prog_type(type, prog);
  701. if (err < 0)
  702. goto free_prog;
  703. /* run eBPF verifier */
  704. err = bpf_check(&prog, attr);
  705. if (err < 0)
  706. goto free_used_maps;
  707. /* eBPF program is ready to be JITed */
  708. prog = bpf_prog_select_runtime(prog, &err);
  709. if (err < 0)
  710. goto free_used_maps;
  711. err = bpf_prog_new_fd(prog);
  712. if (err < 0)
  713. /* failed to allocate fd */
  714. goto free_used_maps;
  715. bpf_prog_kallsyms_add(prog);
  716. trace_bpf_prog_load(prog, err);
  717. return err;
  718. free_used_maps:
  719. free_used_maps(prog->aux);
  720. free_prog:
  721. bpf_prog_uncharge_memlock(prog);
  722. free_prog_nouncharge:
  723. bpf_prog_free(prog);
  724. return err;
  725. }
  726. #define BPF_OBJ_LAST_FIELD bpf_fd
  727. static int bpf_obj_pin(const union bpf_attr *attr)
  728. {
  729. if (CHECK_ATTR(BPF_OBJ))
  730. return -EINVAL;
  731. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  732. }
  733. static int bpf_obj_get(const union bpf_attr *attr)
  734. {
  735. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
  736. return -EINVAL;
  737. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
  738. }
  739. #ifdef CONFIG_CGROUP_BPF
  740. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  741. static int bpf_prog_attach(const union bpf_attr *attr)
  742. {
  743. enum bpf_prog_type ptype;
  744. struct bpf_prog *prog;
  745. struct cgroup *cgrp;
  746. int ret;
  747. if (!capable(CAP_NET_ADMIN))
  748. return -EPERM;
  749. if (CHECK_ATTR(BPF_PROG_ATTACH))
  750. return -EINVAL;
  751. if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
  752. return -EINVAL;
  753. switch (attr->attach_type) {
  754. case BPF_CGROUP_INET_INGRESS:
  755. case BPF_CGROUP_INET_EGRESS:
  756. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  757. break;
  758. case BPF_CGROUP_INET_SOCK_CREATE:
  759. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  760. break;
  761. default:
  762. return -EINVAL;
  763. }
  764. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  765. if (IS_ERR(prog))
  766. return PTR_ERR(prog);
  767. cgrp = cgroup_get_from_fd(attr->target_fd);
  768. if (IS_ERR(cgrp)) {
  769. bpf_prog_put(prog);
  770. return PTR_ERR(cgrp);
  771. }
  772. ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
  773. attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
  774. if (ret)
  775. bpf_prog_put(prog);
  776. cgroup_put(cgrp);
  777. return ret;
  778. }
  779. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  780. static int bpf_prog_detach(const union bpf_attr *attr)
  781. {
  782. struct cgroup *cgrp;
  783. int ret;
  784. if (!capable(CAP_NET_ADMIN))
  785. return -EPERM;
  786. if (CHECK_ATTR(BPF_PROG_DETACH))
  787. return -EINVAL;
  788. switch (attr->attach_type) {
  789. case BPF_CGROUP_INET_INGRESS:
  790. case BPF_CGROUP_INET_EGRESS:
  791. case BPF_CGROUP_INET_SOCK_CREATE:
  792. cgrp = cgroup_get_from_fd(attr->target_fd);
  793. if (IS_ERR(cgrp))
  794. return PTR_ERR(cgrp);
  795. ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
  796. cgroup_put(cgrp);
  797. break;
  798. default:
  799. return -EINVAL;
  800. }
  801. return ret;
  802. }
  803. #endif /* CONFIG_CGROUP_BPF */
  804. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  805. static int bpf_prog_test_run(const union bpf_attr *attr,
  806. union bpf_attr __user *uattr)
  807. {
  808. struct bpf_prog *prog;
  809. int ret = -ENOTSUPP;
  810. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  811. return -EINVAL;
  812. prog = bpf_prog_get(attr->test.prog_fd);
  813. if (IS_ERR(prog))
  814. return PTR_ERR(prog);
  815. if (prog->aux->ops->test_run)
  816. ret = prog->aux->ops->test_run(prog, attr, uattr);
  817. bpf_prog_put(prog);
  818. return ret;
  819. }
  820. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  821. {
  822. union bpf_attr attr = {};
  823. int err;
  824. if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  825. return -EPERM;
  826. if (!access_ok(VERIFY_READ, uattr, 1))
  827. return -EFAULT;
  828. if (size > PAGE_SIZE) /* silly large */
  829. return -E2BIG;
  830. /* If we're handed a bigger struct than we know of,
  831. * ensure all the unknown bits are 0 - i.e. new
  832. * user-space does not rely on any kernel feature
  833. * extensions we dont know about yet.
  834. */
  835. if (size > sizeof(attr)) {
  836. unsigned char __user *addr;
  837. unsigned char __user *end;
  838. unsigned char val;
  839. addr = (void __user *)uattr + sizeof(attr);
  840. end = (void __user *)uattr + size;
  841. for (; addr < end; addr++) {
  842. err = get_user(val, addr);
  843. if (err)
  844. return err;
  845. if (val)
  846. return -E2BIG;
  847. }
  848. size = sizeof(attr);
  849. }
  850. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  851. if (copy_from_user(&attr, uattr, size) != 0)
  852. return -EFAULT;
  853. switch (cmd) {
  854. case BPF_MAP_CREATE:
  855. err = map_create(&attr);
  856. break;
  857. case BPF_MAP_LOOKUP_ELEM:
  858. err = map_lookup_elem(&attr);
  859. break;
  860. case BPF_MAP_UPDATE_ELEM:
  861. err = map_update_elem(&attr);
  862. break;
  863. case BPF_MAP_DELETE_ELEM:
  864. err = map_delete_elem(&attr);
  865. break;
  866. case BPF_MAP_GET_NEXT_KEY:
  867. err = map_get_next_key(&attr);
  868. break;
  869. case BPF_PROG_LOAD:
  870. err = bpf_prog_load(&attr);
  871. break;
  872. case BPF_OBJ_PIN:
  873. err = bpf_obj_pin(&attr);
  874. break;
  875. case BPF_OBJ_GET:
  876. err = bpf_obj_get(&attr);
  877. break;
  878. #ifdef CONFIG_CGROUP_BPF
  879. case BPF_PROG_ATTACH:
  880. err = bpf_prog_attach(&attr);
  881. break;
  882. case BPF_PROG_DETACH:
  883. err = bpf_prog_detach(&attr);
  884. break;
  885. #endif
  886. case BPF_PROG_TEST_RUN:
  887. err = bpf_prog_test_run(&attr, uattr);
  888. break;
  889. default:
  890. err = -EINVAL;
  891. break;
  892. }
  893. return err;
  894. }