syscall.c 36 KB

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