syscall.c 17 KB

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