syscall.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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/syscalls.h>
  14. #include <linux/slab.h>
  15. #include <linux/anon_inodes.h>
  16. #include <linux/file.h>
  17. #include <linux/license.h>
  18. #include <linux/filter.h>
  19. #include <linux/version.h>
  20. DEFINE_PER_CPU(int, bpf_prog_active);
  21. int sysctl_unprivileged_bpf_disabled __read_mostly;
  22. static LIST_HEAD(bpf_map_types);
  23. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  24. {
  25. struct bpf_map_type_list *tl;
  26. struct bpf_map *map;
  27. list_for_each_entry(tl, &bpf_map_types, list_node) {
  28. if (tl->type == attr->map_type) {
  29. map = tl->ops->map_alloc(attr);
  30. if (IS_ERR(map))
  31. return map;
  32. map->ops = tl->ops;
  33. map->map_type = attr->map_type;
  34. return map;
  35. }
  36. }
  37. return ERR_PTR(-EINVAL);
  38. }
  39. /* boot time registration of different map implementations */
  40. void bpf_register_map_type(struct bpf_map_type_list *tl)
  41. {
  42. list_add(&tl->list_node, &bpf_map_types);
  43. }
  44. int bpf_map_precharge_memlock(u32 pages)
  45. {
  46. struct user_struct *user = get_current_user();
  47. unsigned long memlock_limit, cur;
  48. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  49. cur = atomic_long_read(&user->locked_vm);
  50. free_uid(user);
  51. if (cur + pages > memlock_limit)
  52. return -EPERM;
  53. return 0;
  54. }
  55. static int bpf_map_charge_memlock(struct bpf_map *map)
  56. {
  57. struct user_struct *user = get_current_user();
  58. unsigned long memlock_limit;
  59. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  60. atomic_long_add(map->pages, &user->locked_vm);
  61. if (atomic_long_read(&user->locked_vm) > memlock_limit) {
  62. atomic_long_sub(map->pages, &user->locked_vm);
  63. free_uid(user);
  64. return -EPERM;
  65. }
  66. map->user = user;
  67. return 0;
  68. }
  69. static void bpf_map_uncharge_memlock(struct bpf_map *map)
  70. {
  71. struct user_struct *user = map->user;
  72. atomic_long_sub(map->pages, &user->locked_vm);
  73. free_uid(user);
  74. }
  75. /* called from workqueue */
  76. static void bpf_map_free_deferred(struct work_struct *work)
  77. {
  78. struct bpf_map *map = container_of(work, struct bpf_map, work);
  79. bpf_map_uncharge_memlock(map);
  80. /* implementation dependent freeing */
  81. map->ops->map_free(map);
  82. }
  83. static void bpf_map_put_uref(struct bpf_map *map)
  84. {
  85. if (atomic_dec_and_test(&map->usercnt)) {
  86. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
  87. bpf_fd_array_map_clear(map);
  88. }
  89. }
  90. /* decrement map refcnt and schedule it for freeing via workqueue
  91. * (unrelying map implementation ops->map_free() might sleep)
  92. */
  93. void bpf_map_put(struct bpf_map *map)
  94. {
  95. if (atomic_dec_and_test(&map->refcnt)) {
  96. INIT_WORK(&map->work, bpf_map_free_deferred);
  97. schedule_work(&map->work);
  98. }
  99. }
  100. void bpf_map_put_with_uref(struct bpf_map *map)
  101. {
  102. bpf_map_put_uref(map);
  103. bpf_map_put(map);
  104. }
  105. static int bpf_map_release(struct inode *inode, struct file *filp)
  106. {
  107. struct bpf_map *map = filp->private_data;
  108. if (map->ops->map_release)
  109. map->ops->map_release(map, filp);
  110. bpf_map_put_with_uref(map);
  111. return 0;
  112. }
  113. #ifdef CONFIG_PROC_FS
  114. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  115. {
  116. const struct bpf_map *map = filp->private_data;
  117. seq_printf(m,
  118. "map_type:\t%u\n"
  119. "key_size:\t%u\n"
  120. "value_size:\t%u\n"
  121. "max_entries:\t%u\n"
  122. "map_flags:\t%#x\n",
  123. map->map_type,
  124. map->key_size,
  125. map->value_size,
  126. map->max_entries,
  127. map->map_flags);
  128. }
  129. #endif
  130. static const struct file_operations bpf_map_fops = {
  131. #ifdef CONFIG_PROC_FS
  132. .show_fdinfo = bpf_map_show_fdinfo,
  133. #endif
  134. .release = bpf_map_release,
  135. };
  136. int bpf_map_new_fd(struct bpf_map *map)
  137. {
  138. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  139. O_RDWR | O_CLOEXEC);
  140. }
  141. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  142. #define CHECK_ATTR(CMD) \
  143. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  144. sizeof(attr->CMD##_LAST_FIELD), 0, \
  145. sizeof(*attr) - \
  146. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  147. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  148. #define BPF_MAP_CREATE_LAST_FIELD map_flags
  149. /* called via syscall */
  150. static int map_create(union bpf_attr *attr)
  151. {
  152. struct bpf_map *map;
  153. int err;
  154. err = CHECK_ATTR(BPF_MAP_CREATE);
  155. if (err)
  156. return -EINVAL;
  157. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  158. map = find_and_alloc_map(attr);
  159. if (IS_ERR(map))
  160. return PTR_ERR(map);
  161. atomic_set(&map->refcnt, 1);
  162. atomic_set(&map->usercnt, 1);
  163. err = bpf_map_charge_memlock(map);
  164. if (err)
  165. goto free_map_nouncharge;
  166. err = bpf_map_new_fd(map);
  167. if (err < 0)
  168. /* failed to allocate fd */
  169. goto free_map;
  170. return err;
  171. free_map:
  172. bpf_map_uncharge_memlock(map);
  173. free_map_nouncharge:
  174. map->ops->map_free(map);
  175. return err;
  176. }
  177. /* if error is returned, fd is released.
  178. * On success caller should complete fd access with matching fdput()
  179. */
  180. struct bpf_map *__bpf_map_get(struct fd f)
  181. {
  182. if (!f.file)
  183. return ERR_PTR(-EBADF);
  184. if (f.file->f_op != &bpf_map_fops) {
  185. fdput(f);
  186. return ERR_PTR(-EINVAL);
  187. }
  188. return f.file->private_data;
  189. }
  190. /* prog's and map's refcnt limit */
  191. #define BPF_MAX_REFCNT 32768
  192. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  193. {
  194. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  195. atomic_dec(&map->refcnt);
  196. return ERR_PTR(-EBUSY);
  197. }
  198. if (uref)
  199. atomic_inc(&map->usercnt);
  200. return map;
  201. }
  202. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  203. {
  204. struct fd f = fdget(ufd);
  205. struct bpf_map *map;
  206. map = __bpf_map_get(f);
  207. if (IS_ERR(map))
  208. return map;
  209. map = bpf_map_inc(map, true);
  210. fdput(f);
  211. return map;
  212. }
  213. /* helper to convert user pointers passed inside __aligned_u64 fields */
  214. static void __user *u64_to_ptr(__u64 val)
  215. {
  216. return (void __user *) (unsigned long) val;
  217. }
  218. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  219. {
  220. return -ENOTSUPP;
  221. }
  222. /* last field in 'union bpf_attr' used by this command */
  223. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  224. static int map_lookup_elem(union bpf_attr *attr)
  225. {
  226. void __user *ukey = u64_to_ptr(attr->key);
  227. void __user *uvalue = u64_to_ptr(attr->value);
  228. int ufd = attr->map_fd;
  229. struct bpf_map *map;
  230. void *key, *value, *ptr;
  231. u32 value_size;
  232. struct fd f;
  233. int err;
  234. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  235. return -EINVAL;
  236. f = fdget(ufd);
  237. map = __bpf_map_get(f);
  238. if (IS_ERR(map))
  239. return PTR_ERR(map);
  240. err = -ENOMEM;
  241. key = kmalloc(map->key_size, GFP_USER);
  242. if (!key)
  243. goto err_put;
  244. err = -EFAULT;
  245. if (copy_from_user(key, ukey, map->key_size) != 0)
  246. goto free_key;
  247. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  248. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  249. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  250. else
  251. value_size = map->value_size;
  252. err = -ENOMEM;
  253. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  254. if (!value)
  255. goto free_key;
  256. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
  257. err = bpf_percpu_hash_copy(map, key, value);
  258. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  259. err = bpf_percpu_array_copy(map, key, value);
  260. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  261. err = bpf_stackmap_copy(map, key, value);
  262. } else {
  263. rcu_read_lock();
  264. ptr = map->ops->map_lookup_elem(map, key);
  265. if (ptr)
  266. memcpy(value, ptr, value_size);
  267. rcu_read_unlock();
  268. err = ptr ? 0 : -ENOENT;
  269. }
  270. if (err)
  271. goto free_value;
  272. err = -EFAULT;
  273. if (copy_to_user(uvalue, value, value_size) != 0)
  274. goto free_value;
  275. err = 0;
  276. free_value:
  277. kfree(value);
  278. free_key:
  279. kfree(key);
  280. err_put:
  281. fdput(f);
  282. return err;
  283. }
  284. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  285. static int map_update_elem(union bpf_attr *attr)
  286. {
  287. void __user *ukey = u64_to_ptr(attr->key);
  288. void __user *uvalue = u64_to_ptr(attr->value);
  289. int ufd = attr->map_fd;
  290. struct bpf_map *map;
  291. void *key, *value;
  292. u32 value_size;
  293. struct fd f;
  294. int err;
  295. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  296. return -EINVAL;
  297. f = fdget(ufd);
  298. map = __bpf_map_get(f);
  299. if (IS_ERR(map))
  300. return PTR_ERR(map);
  301. err = -ENOMEM;
  302. key = kmalloc(map->key_size, GFP_USER);
  303. if (!key)
  304. goto err_put;
  305. err = -EFAULT;
  306. if (copy_from_user(key, ukey, map->key_size) != 0)
  307. goto free_key;
  308. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  309. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  310. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  311. else
  312. value_size = map->value_size;
  313. err = -ENOMEM;
  314. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  315. if (!value)
  316. goto free_key;
  317. err = -EFAULT;
  318. if (copy_from_user(value, uvalue, value_size) != 0)
  319. goto free_value;
  320. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  321. * inside bpf map update or delete otherwise deadlocks are possible
  322. */
  323. preempt_disable();
  324. __this_cpu_inc(bpf_prog_active);
  325. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
  326. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  327. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  328. err = bpf_percpu_array_update(map, key, value, attr->flags);
  329. } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
  330. map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  331. map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
  332. rcu_read_lock();
  333. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  334. attr->flags);
  335. rcu_read_unlock();
  336. } else {
  337. rcu_read_lock();
  338. err = map->ops->map_update_elem(map, key, value, attr->flags);
  339. rcu_read_unlock();
  340. }
  341. __this_cpu_dec(bpf_prog_active);
  342. preempt_enable();
  343. free_value:
  344. kfree(value);
  345. free_key:
  346. kfree(key);
  347. err_put:
  348. fdput(f);
  349. return err;
  350. }
  351. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  352. static int map_delete_elem(union bpf_attr *attr)
  353. {
  354. void __user *ukey = u64_to_ptr(attr->key);
  355. int ufd = attr->map_fd;
  356. struct bpf_map *map;
  357. struct fd f;
  358. void *key;
  359. int err;
  360. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  361. return -EINVAL;
  362. f = fdget(ufd);
  363. map = __bpf_map_get(f);
  364. if (IS_ERR(map))
  365. return PTR_ERR(map);
  366. err = -ENOMEM;
  367. key = kmalloc(map->key_size, GFP_USER);
  368. if (!key)
  369. goto err_put;
  370. err = -EFAULT;
  371. if (copy_from_user(key, ukey, map->key_size) != 0)
  372. goto free_key;
  373. preempt_disable();
  374. __this_cpu_inc(bpf_prog_active);
  375. rcu_read_lock();
  376. err = map->ops->map_delete_elem(map, key);
  377. rcu_read_unlock();
  378. __this_cpu_dec(bpf_prog_active);
  379. preempt_enable();
  380. free_key:
  381. kfree(key);
  382. err_put:
  383. fdput(f);
  384. return err;
  385. }
  386. /* last field in 'union bpf_attr' used by this command */
  387. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  388. static int map_get_next_key(union bpf_attr *attr)
  389. {
  390. void __user *ukey = u64_to_ptr(attr->key);
  391. void __user *unext_key = u64_to_ptr(attr->next_key);
  392. int ufd = attr->map_fd;
  393. struct bpf_map *map;
  394. void *key, *next_key;
  395. struct fd f;
  396. int err;
  397. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  398. return -EINVAL;
  399. f = fdget(ufd);
  400. map = __bpf_map_get(f);
  401. if (IS_ERR(map))
  402. return PTR_ERR(map);
  403. err = -ENOMEM;
  404. key = kmalloc(map->key_size, GFP_USER);
  405. if (!key)
  406. goto err_put;
  407. err = -EFAULT;
  408. if (copy_from_user(key, ukey, map->key_size) != 0)
  409. goto free_key;
  410. err = -ENOMEM;
  411. next_key = kmalloc(map->key_size, GFP_USER);
  412. if (!next_key)
  413. goto free_key;
  414. rcu_read_lock();
  415. err = map->ops->map_get_next_key(map, key, next_key);
  416. rcu_read_unlock();
  417. if (err)
  418. goto free_next_key;
  419. err = -EFAULT;
  420. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  421. goto free_next_key;
  422. err = 0;
  423. free_next_key:
  424. kfree(next_key);
  425. free_key:
  426. kfree(key);
  427. err_put:
  428. fdput(f);
  429. return err;
  430. }
  431. static LIST_HEAD(bpf_prog_types);
  432. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  433. {
  434. struct bpf_prog_type_list *tl;
  435. list_for_each_entry(tl, &bpf_prog_types, list_node) {
  436. if (tl->type == type) {
  437. prog->aux->ops = tl->ops;
  438. prog->type = type;
  439. return 0;
  440. }
  441. }
  442. return -EINVAL;
  443. }
  444. void bpf_register_prog_type(struct bpf_prog_type_list *tl)
  445. {
  446. list_add(&tl->list_node, &bpf_prog_types);
  447. }
  448. /* fixup insn->imm field of bpf_call instructions:
  449. * if (insn->imm == BPF_FUNC_map_lookup_elem)
  450. * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
  451. * else if (insn->imm == BPF_FUNC_map_update_elem)
  452. * insn->imm = bpf_map_update_elem - __bpf_call_base;
  453. * else ...
  454. *
  455. * this function is called after eBPF program passed verification
  456. */
  457. static void fixup_bpf_calls(struct bpf_prog *prog)
  458. {
  459. const struct bpf_func_proto *fn;
  460. int i;
  461. for (i = 0; i < prog->len; i++) {
  462. struct bpf_insn *insn = &prog->insnsi[i];
  463. if (insn->code == (BPF_JMP | BPF_CALL)) {
  464. /* we reach here when program has bpf_call instructions
  465. * and it passed bpf_check(), means that
  466. * ops->get_func_proto must have been supplied, check it
  467. */
  468. BUG_ON(!prog->aux->ops->get_func_proto);
  469. if (insn->imm == BPF_FUNC_get_route_realm)
  470. prog->dst_needed = 1;
  471. if (insn->imm == BPF_FUNC_get_prandom_u32)
  472. bpf_user_rnd_init_once();
  473. if (insn->imm == BPF_FUNC_tail_call) {
  474. /* mark bpf_tail_call as different opcode
  475. * to avoid conditional branch in
  476. * interpeter for every normal call
  477. * and to prevent accidental JITing by
  478. * JIT compiler that doesn't support
  479. * bpf_tail_call yet
  480. */
  481. insn->imm = 0;
  482. insn->code |= BPF_X;
  483. continue;
  484. }
  485. fn = prog->aux->ops->get_func_proto(insn->imm);
  486. /* all functions that have prototype and verifier allowed
  487. * programs to call them, must be real in-kernel functions
  488. */
  489. BUG_ON(!fn->func);
  490. insn->imm = fn->func - __bpf_call_base;
  491. }
  492. }
  493. }
  494. /* drop refcnt on maps used by eBPF program and free auxilary data */
  495. static void free_used_maps(struct bpf_prog_aux *aux)
  496. {
  497. int i;
  498. for (i = 0; i < aux->used_map_cnt; i++)
  499. bpf_map_put(aux->used_maps[i]);
  500. kfree(aux->used_maps);
  501. }
  502. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  503. {
  504. struct user_struct *user = get_current_user();
  505. unsigned long memlock_limit;
  506. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  507. atomic_long_add(prog->pages, &user->locked_vm);
  508. if (atomic_long_read(&user->locked_vm) > memlock_limit) {
  509. atomic_long_sub(prog->pages, &user->locked_vm);
  510. free_uid(user);
  511. return -EPERM;
  512. }
  513. prog->aux->user = user;
  514. return 0;
  515. }
  516. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  517. {
  518. struct user_struct *user = prog->aux->user;
  519. atomic_long_sub(prog->pages, &user->locked_vm);
  520. free_uid(user);
  521. }
  522. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  523. {
  524. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  525. free_used_maps(aux);
  526. bpf_prog_uncharge_memlock(aux->prog);
  527. bpf_prog_free(aux->prog);
  528. }
  529. void bpf_prog_put(struct bpf_prog *prog)
  530. {
  531. if (atomic_dec_and_test(&prog->aux->refcnt))
  532. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  533. }
  534. EXPORT_SYMBOL_GPL(bpf_prog_put);
  535. static int bpf_prog_release(struct inode *inode, struct file *filp)
  536. {
  537. struct bpf_prog *prog = filp->private_data;
  538. bpf_prog_put(prog);
  539. return 0;
  540. }
  541. static const struct file_operations bpf_prog_fops = {
  542. .release = bpf_prog_release,
  543. };
  544. int bpf_prog_new_fd(struct bpf_prog *prog)
  545. {
  546. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  547. O_RDWR | O_CLOEXEC);
  548. }
  549. static struct bpf_prog *____bpf_prog_get(struct fd f)
  550. {
  551. if (!f.file)
  552. return ERR_PTR(-EBADF);
  553. if (f.file->f_op != &bpf_prog_fops) {
  554. fdput(f);
  555. return ERR_PTR(-EINVAL);
  556. }
  557. return f.file->private_data;
  558. }
  559. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  560. {
  561. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  562. atomic_sub(i, &prog->aux->refcnt);
  563. return ERR_PTR(-EBUSY);
  564. }
  565. return prog;
  566. }
  567. EXPORT_SYMBOL_GPL(bpf_prog_add);
  568. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  569. {
  570. return bpf_prog_add(prog, 1);
  571. }
  572. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
  573. {
  574. struct fd f = fdget(ufd);
  575. struct bpf_prog *prog;
  576. prog = ____bpf_prog_get(f);
  577. if (IS_ERR(prog))
  578. return prog;
  579. if (type && prog->type != *type) {
  580. prog = ERR_PTR(-EINVAL);
  581. goto out;
  582. }
  583. prog = bpf_prog_inc(prog);
  584. out:
  585. fdput(f);
  586. return prog;
  587. }
  588. struct bpf_prog *bpf_prog_get(u32 ufd)
  589. {
  590. return __bpf_prog_get(ufd, NULL);
  591. }
  592. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  593. {
  594. return __bpf_prog_get(ufd, &type);
  595. }
  596. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  597. /* last field in 'union bpf_attr' used by this command */
  598. #define BPF_PROG_LOAD_LAST_FIELD kern_version
  599. static int bpf_prog_load(union bpf_attr *attr)
  600. {
  601. enum bpf_prog_type type = attr->prog_type;
  602. struct bpf_prog *prog;
  603. int err;
  604. char license[128];
  605. bool is_gpl;
  606. if (CHECK_ATTR(BPF_PROG_LOAD))
  607. return -EINVAL;
  608. /* copy eBPF program license from user space */
  609. if (strncpy_from_user(license, u64_to_ptr(attr->license),
  610. sizeof(license) - 1) < 0)
  611. return -EFAULT;
  612. license[sizeof(license) - 1] = 0;
  613. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  614. is_gpl = license_is_gpl_compatible(license);
  615. if (attr->insn_cnt >= BPF_MAXINSNS)
  616. return -EINVAL;
  617. if (type == BPF_PROG_TYPE_KPROBE &&
  618. attr->kern_version != LINUX_VERSION_CODE)
  619. return -EINVAL;
  620. if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
  621. return -EPERM;
  622. /* plain bpf_prog allocation */
  623. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  624. if (!prog)
  625. return -ENOMEM;
  626. err = bpf_prog_charge_memlock(prog);
  627. if (err)
  628. goto free_prog_nouncharge;
  629. prog->len = attr->insn_cnt;
  630. err = -EFAULT;
  631. if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
  632. prog->len * sizeof(struct bpf_insn)) != 0)
  633. goto free_prog;
  634. prog->orig_prog = NULL;
  635. prog->jited = 0;
  636. atomic_set(&prog->aux->refcnt, 1);
  637. prog->gpl_compatible = is_gpl ? 1 : 0;
  638. /* find program type: socket_filter vs tracing_filter */
  639. err = find_prog_type(type, prog);
  640. if (err < 0)
  641. goto free_prog;
  642. /* run eBPF verifier */
  643. err = bpf_check(&prog, attr);
  644. if (err < 0)
  645. goto free_used_maps;
  646. /* fixup BPF_CALL->imm field */
  647. fixup_bpf_calls(prog);
  648. /* eBPF program is ready to be JITed */
  649. prog = bpf_prog_select_runtime(prog, &err);
  650. if (err < 0)
  651. goto free_used_maps;
  652. err = bpf_prog_new_fd(prog);
  653. if (err < 0)
  654. /* failed to allocate fd */
  655. goto free_used_maps;
  656. return err;
  657. free_used_maps:
  658. free_used_maps(prog->aux);
  659. free_prog:
  660. bpf_prog_uncharge_memlock(prog);
  661. free_prog_nouncharge:
  662. bpf_prog_free(prog);
  663. return err;
  664. }
  665. #define BPF_OBJ_LAST_FIELD bpf_fd
  666. static int bpf_obj_pin(const union bpf_attr *attr)
  667. {
  668. if (CHECK_ATTR(BPF_OBJ))
  669. return -EINVAL;
  670. return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
  671. }
  672. static int bpf_obj_get(const union bpf_attr *attr)
  673. {
  674. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
  675. return -EINVAL;
  676. return bpf_obj_get_user(u64_to_ptr(attr->pathname));
  677. }
  678. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  679. {
  680. union bpf_attr attr = {};
  681. int err;
  682. if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  683. return -EPERM;
  684. if (!access_ok(VERIFY_READ, uattr, 1))
  685. return -EFAULT;
  686. if (size > PAGE_SIZE) /* silly large */
  687. return -E2BIG;
  688. /* If we're handed a bigger struct than we know of,
  689. * ensure all the unknown bits are 0 - i.e. new
  690. * user-space does not rely on any kernel feature
  691. * extensions we dont know about yet.
  692. */
  693. if (size > sizeof(attr)) {
  694. unsigned char __user *addr;
  695. unsigned char __user *end;
  696. unsigned char val;
  697. addr = (void __user *)uattr + sizeof(attr);
  698. end = (void __user *)uattr + size;
  699. for (; addr < end; addr++) {
  700. err = get_user(val, addr);
  701. if (err)
  702. return err;
  703. if (val)
  704. return -E2BIG;
  705. }
  706. size = sizeof(attr);
  707. }
  708. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  709. if (copy_from_user(&attr, uattr, size) != 0)
  710. return -EFAULT;
  711. switch (cmd) {
  712. case BPF_MAP_CREATE:
  713. err = map_create(&attr);
  714. break;
  715. case BPF_MAP_LOOKUP_ELEM:
  716. err = map_lookup_elem(&attr);
  717. break;
  718. case BPF_MAP_UPDATE_ELEM:
  719. err = map_update_elem(&attr);
  720. break;
  721. case BPF_MAP_DELETE_ELEM:
  722. err = map_delete_elem(&attr);
  723. break;
  724. case BPF_MAP_GET_NEXT_KEY:
  725. err = map_get_next_key(&attr);
  726. break;
  727. case BPF_PROG_LOAD:
  728. err = bpf_prog_load(&attr);
  729. break;
  730. case BPF_OBJ_PIN:
  731. err = bpf_obj_pin(&attr);
  732. break;
  733. case BPF_OBJ_GET:
  734. err = bpf_obj_get(&attr);
  735. break;
  736. default:
  737. err = -EINVAL;
  738. break;
  739. }
  740. return err;
  741. }