syscall.c 58 KB

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