syscall.c 48 KB

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