syscall.c 43 KB

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