syscall.c 17 KB

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