syscall.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  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. /* last field in 'union bpf_attr' used by this command */
  548. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  549. static int map_lookup_elem(union bpf_attr *attr)
  550. {
  551. void __user *ukey = u64_to_user_ptr(attr->key);
  552. void __user *uvalue = u64_to_user_ptr(attr->value);
  553. int ufd = attr->map_fd;
  554. struct bpf_map *map;
  555. void *key, *value, *ptr;
  556. u32 value_size;
  557. struct fd f;
  558. int err;
  559. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  560. return -EINVAL;
  561. f = fdget(ufd);
  562. map = __bpf_map_get(f);
  563. if (IS_ERR(map))
  564. return PTR_ERR(map);
  565. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  566. err = -EPERM;
  567. goto err_put;
  568. }
  569. key = memdup_user(ukey, map->key_size);
  570. if (IS_ERR(key)) {
  571. err = PTR_ERR(key);
  572. goto err_put;
  573. }
  574. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  575. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  576. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
  577. map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
  578. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  579. else if (IS_FD_MAP(map))
  580. value_size = sizeof(u32);
  581. else
  582. value_size = map->value_size;
  583. err = -ENOMEM;
  584. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  585. if (!value)
  586. goto free_key;
  587. if (bpf_map_is_dev_bound(map)) {
  588. err = bpf_map_offload_lookup_elem(map, key, value);
  589. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  590. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  591. err = bpf_percpu_hash_copy(map, key, value);
  592. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  593. err = bpf_percpu_array_copy(map, key, value);
  594. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
  595. err = bpf_percpu_cgroup_storage_copy(map, key, value);
  596. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  597. err = bpf_stackmap_copy(map, key, value);
  598. } else if (IS_FD_ARRAY(map)) {
  599. err = bpf_fd_array_map_lookup_elem(map, key, value);
  600. } else if (IS_FD_HASH(map)) {
  601. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  602. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  603. err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
  604. } else {
  605. rcu_read_lock();
  606. ptr = map->ops->map_lookup_elem(map, key);
  607. if (IS_ERR(ptr)) {
  608. err = PTR_ERR(ptr);
  609. } else if (!ptr) {
  610. err = -ENOENT;
  611. } else {
  612. err = 0;
  613. memcpy(value, ptr, value_size);
  614. }
  615. rcu_read_unlock();
  616. }
  617. if (err)
  618. goto free_value;
  619. err = -EFAULT;
  620. if (copy_to_user(uvalue, value, value_size) != 0)
  621. goto free_value;
  622. err = 0;
  623. free_value:
  624. kfree(value);
  625. free_key:
  626. kfree(key);
  627. err_put:
  628. fdput(f);
  629. return err;
  630. }
  631. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  632. static int map_update_elem(union bpf_attr *attr)
  633. {
  634. void __user *ukey = u64_to_user_ptr(attr->key);
  635. void __user *uvalue = u64_to_user_ptr(attr->value);
  636. int ufd = attr->map_fd;
  637. struct bpf_map *map;
  638. void *key, *value;
  639. u32 value_size;
  640. struct fd f;
  641. int err;
  642. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  643. return -EINVAL;
  644. f = fdget(ufd);
  645. map = __bpf_map_get(f);
  646. if (IS_ERR(map))
  647. return PTR_ERR(map);
  648. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  649. err = -EPERM;
  650. goto err_put;
  651. }
  652. key = memdup_user(ukey, map->key_size);
  653. if (IS_ERR(key)) {
  654. err = PTR_ERR(key);
  655. goto err_put;
  656. }
  657. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  658. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  659. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
  660. map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
  661. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  662. else
  663. value_size = map->value_size;
  664. err = -ENOMEM;
  665. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  666. if (!value)
  667. goto free_key;
  668. err = -EFAULT;
  669. if (copy_from_user(value, uvalue, value_size) != 0)
  670. goto free_value;
  671. /* Need to create a kthread, thus must support schedule */
  672. if (bpf_map_is_dev_bound(map)) {
  673. err = bpf_map_offload_update_elem(map, key, value, attr->flags);
  674. goto out;
  675. } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
  676. map->map_type == BPF_MAP_TYPE_SOCKHASH ||
  677. map->map_type == BPF_MAP_TYPE_SOCKMAP) {
  678. err = map->ops->map_update_elem(map, key, value, attr->flags);
  679. goto out;
  680. }
  681. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  682. * inside bpf map update or delete otherwise deadlocks are possible
  683. */
  684. preempt_disable();
  685. __this_cpu_inc(bpf_prog_active);
  686. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  687. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  688. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  689. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  690. err = bpf_percpu_array_update(map, key, value, attr->flags);
  691. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
  692. err = bpf_percpu_cgroup_storage_update(map, key, value,
  693. attr->flags);
  694. } else if (IS_FD_ARRAY(map)) {
  695. rcu_read_lock();
  696. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  697. attr->flags);
  698. rcu_read_unlock();
  699. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  700. rcu_read_lock();
  701. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  702. attr->flags);
  703. rcu_read_unlock();
  704. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  705. /* rcu_read_lock() is not needed */
  706. err = bpf_fd_reuseport_array_update_elem(map, key, value,
  707. attr->flags);
  708. } else {
  709. rcu_read_lock();
  710. err = map->ops->map_update_elem(map, key, value, attr->flags);
  711. rcu_read_unlock();
  712. }
  713. __this_cpu_dec(bpf_prog_active);
  714. preempt_enable();
  715. out:
  716. free_value:
  717. kfree(value);
  718. free_key:
  719. kfree(key);
  720. err_put:
  721. fdput(f);
  722. return err;
  723. }
  724. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  725. static int map_delete_elem(union bpf_attr *attr)
  726. {
  727. void __user *ukey = u64_to_user_ptr(attr->key);
  728. int ufd = attr->map_fd;
  729. struct bpf_map *map;
  730. struct fd f;
  731. void *key;
  732. int err;
  733. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  734. return -EINVAL;
  735. f = fdget(ufd);
  736. map = __bpf_map_get(f);
  737. if (IS_ERR(map))
  738. return PTR_ERR(map);
  739. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  740. err = -EPERM;
  741. goto err_put;
  742. }
  743. key = memdup_user(ukey, map->key_size);
  744. if (IS_ERR(key)) {
  745. err = PTR_ERR(key);
  746. goto err_put;
  747. }
  748. if (bpf_map_is_dev_bound(map)) {
  749. err = bpf_map_offload_delete_elem(map, key);
  750. goto out;
  751. }
  752. preempt_disable();
  753. __this_cpu_inc(bpf_prog_active);
  754. rcu_read_lock();
  755. err = map->ops->map_delete_elem(map, key);
  756. rcu_read_unlock();
  757. __this_cpu_dec(bpf_prog_active);
  758. preempt_enable();
  759. out:
  760. kfree(key);
  761. err_put:
  762. fdput(f);
  763. return err;
  764. }
  765. /* last field in 'union bpf_attr' used by this command */
  766. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  767. static int map_get_next_key(union bpf_attr *attr)
  768. {
  769. void __user *ukey = u64_to_user_ptr(attr->key);
  770. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  771. int ufd = attr->map_fd;
  772. struct bpf_map *map;
  773. void *key, *next_key;
  774. struct fd f;
  775. int err;
  776. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  777. return -EINVAL;
  778. f = fdget(ufd);
  779. map = __bpf_map_get(f);
  780. if (IS_ERR(map))
  781. return PTR_ERR(map);
  782. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  783. err = -EPERM;
  784. goto err_put;
  785. }
  786. if (ukey) {
  787. key = memdup_user(ukey, map->key_size);
  788. if (IS_ERR(key)) {
  789. err = PTR_ERR(key);
  790. goto err_put;
  791. }
  792. } else {
  793. key = NULL;
  794. }
  795. err = -ENOMEM;
  796. next_key = kmalloc(map->key_size, GFP_USER);
  797. if (!next_key)
  798. goto free_key;
  799. if (bpf_map_is_dev_bound(map)) {
  800. err = bpf_map_offload_get_next_key(map, key, next_key);
  801. goto out;
  802. }
  803. rcu_read_lock();
  804. err = map->ops->map_get_next_key(map, key, next_key);
  805. rcu_read_unlock();
  806. out:
  807. if (err)
  808. goto free_next_key;
  809. err = -EFAULT;
  810. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  811. goto free_next_key;
  812. err = 0;
  813. free_next_key:
  814. kfree(next_key);
  815. free_key:
  816. kfree(key);
  817. err_put:
  818. fdput(f);
  819. return err;
  820. }
  821. static const struct bpf_prog_ops * const bpf_prog_types[] = {
  822. #define BPF_PROG_TYPE(_id, _name) \
  823. [_id] = & _name ## _prog_ops,
  824. #define BPF_MAP_TYPE(_id, _ops)
  825. #include <linux/bpf_types.h>
  826. #undef BPF_PROG_TYPE
  827. #undef BPF_MAP_TYPE
  828. };
  829. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  830. {
  831. const struct bpf_prog_ops *ops;
  832. if (type >= ARRAY_SIZE(bpf_prog_types))
  833. return -EINVAL;
  834. type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
  835. ops = bpf_prog_types[type];
  836. if (!ops)
  837. return -EINVAL;
  838. if (!bpf_prog_is_dev_bound(prog->aux))
  839. prog->aux->ops = ops;
  840. else
  841. prog->aux->ops = &bpf_offload_prog_ops;
  842. prog->type = type;
  843. return 0;
  844. }
  845. /* drop refcnt on maps used by eBPF program and free auxilary data */
  846. static void free_used_maps(struct bpf_prog_aux *aux)
  847. {
  848. enum bpf_cgroup_storage_type stype;
  849. int i;
  850. for_each_cgroup_storage_type(stype) {
  851. if (!aux->cgroup_storage[stype])
  852. continue;
  853. bpf_cgroup_storage_release(aux->prog,
  854. aux->cgroup_storage[stype]);
  855. }
  856. for (i = 0; i < aux->used_map_cnt; i++)
  857. bpf_map_put(aux->used_maps[i]);
  858. kfree(aux->used_maps);
  859. }
  860. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  861. {
  862. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  863. unsigned long user_bufs;
  864. if (user) {
  865. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  866. if (user_bufs > memlock_limit) {
  867. atomic_long_sub(pages, &user->locked_vm);
  868. return -EPERM;
  869. }
  870. }
  871. return 0;
  872. }
  873. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  874. {
  875. if (user)
  876. atomic_long_sub(pages, &user->locked_vm);
  877. }
  878. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  879. {
  880. struct user_struct *user = get_current_user();
  881. int ret;
  882. ret = __bpf_prog_charge(user, prog->pages);
  883. if (ret) {
  884. free_uid(user);
  885. return ret;
  886. }
  887. prog->aux->user = user;
  888. return 0;
  889. }
  890. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  891. {
  892. struct user_struct *user = prog->aux->user;
  893. __bpf_prog_uncharge(user, prog->pages);
  894. free_uid(user);
  895. }
  896. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  897. {
  898. int id;
  899. idr_preload(GFP_KERNEL);
  900. spin_lock_bh(&prog_idr_lock);
  901. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  902. if (id > 0)
  903. prog->aux->id = id;
  904. spin_unlock_bh(&prog_idr_lock);
  905. idr_preload_end();
  906. /* id is in [1, INT_MAX) */
  907. if (WARN_ON_ONCE(!id))
  908. return -ENOSPC;
  909. return id > 0 ? 0 : id;
  910. }
  911. void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  912. {
  913. /* cBPF to eBPF migrations are currently not in the idr store.
  914. * Offloaded programs are removed from the store when their device
  915. * disappears - even if someone grabs an fd to them they are unusable,
  916. * simply waiting for refcnt to drop to be freed.
  917. */
  918. if (!prog->aux->id)
  919. return;
  920. if (do_idr_lock)
  921. spin_lock_bh(&prog_idr_lock);
  922. else
  923. __acquire(&prog_idr_lock);
  924. idr_remove(&prog_idr, prog->aux->id);
  925. prog->aux->id = 0;
  926. if (do_idr_lock)
  927. spin_unlock_bh(&prog_idr_lock);
  928. else
  929. __release(&prog_idr_lock);
  930. }
  931. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  932. {
  933. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  934. free_used_maps(aux);
  935. bpf_prog_uncharge_memlock(aux->prog);
  936. security_bpf_prog_free(aux);
  937. bpf_prog_free(aux->prog);
  938. }
  939. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  940. {
  941. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  942. /* bpf_prog_free_id() must be called first */
  943. bpf_prog_free_id(prog, do_idr_lock);
  944. bpf_prog_kallsyms_del_all(prog);
  945. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  946. }
  947. }
  948. void bpf_prog_put(struct bpf_prog *prog)
  949. {
  950. __bpf_prog_put(prog, true);
  951. }
  952. EXPORT_SYMBOL_GPL(bpf_prog_put);
  953. static int bpf_prog_release(struct inode *inode, struct file *filp)
  954. {
  955. struct bpf_prog *prog = filp->private_data;
  956. bpf_prog_put(prog);
  957. return 0;
  958. }
  959. #ifdef CONFIG_PROC_FS
  960. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  961. {
  962. const struct bpf_prog *prog = filp->private_data;
  963. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  964. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  965. seq_printf(m,
  966. "prog_type:\t%u\n"
  967. "prog_jited:\t%u\n"
  968. "prog_tag:\t%s\n"
  969. "memlock:\t%llu\n"
  970. "prog_id:\t%u\n",
  971. prog->type,
  972. prog->jited,
  973. prog_tag,
  974. prog->pages * 1ULL << PAGE_SHIFT,
  975. prog->aux->id);
  976. }
  977. #endif
  978. const struct file_operations bpf_prog_fops = {
  979. #ifdef CONFIG_PROC_FS
  980. .show_fdinfo = bpf_prog_show_fdinfo,
  981. #endif
  982. .release = bpf_prog_release,
  983. .read = bpf_dummy_read,
  984. .write = bpf_dummy_write,
  985. };
  986. int bpf_prog_new_fd(struct bpf_prog *prog)
  987. {
  988. int ret;
  989. ret = security_bpf_prog(prog);
  990. if (ret < 0)
  991. return ret;
  992. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  993. O_RDWR | O_CLOEXEC);
  994. }
  995. static struct bpf_prog *____bpf_prog_get(struct fd f)
  996. {
  997. if (!f.file)
  998. return ERR_PTR(-EBADF);
  999. if (f.file->f_op != &bpf_prog_fops) {
  1000. fdput(f);
  1001. return ERR_PTR(-EINVAL);
  1002. }
  1003. return f.file->private_data;
  1004. }
  1005. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  1006. {
  1007. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  1008. atomic_sub(i, &prog->aux->refcnt);
  1009. return ERR_PTR(-EBUSY);
  1010. }
  1011. return prog;
  1012. }
  1013. EXPORT_SYMBOL_GPL(bpf_prog_add);
  1014. void bpf_prog_sub(struct bpf_prog *prog, int i)
  1015. {
  1016. /* Only to be used for undoing previous bpf_prog_add() in some
  1017. * error path. We still know that another entity in our call
  1018. * path holds a reference to the program, thus atomic_sub() can
  1019. * be safely used in such cases!
  1020. */
  1021. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  1022. }
  1023. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  1024. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  1025. {
  1026. return bpf_prog_add(prog, 1);
  1027. }
  1028. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  1029. /* prog_idr_lock should have been held */
  1030. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  1031. {
  1032. int refold;
  1033. refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
  1034. if (refold >= BPF_MAX_REFCNT) {
  1035. __bpf_prog_put(prog, false);
  1036. return ERR_PTR(-EBUSY);
  1037. }
  1038. if (!refold)
  1039. return ERR_PTR(-ENOENT);
  1040. return prog;
  1041. }
  1042. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  1043. bool bpf_prog_get_ok(struct bpf_prog *prog,
  1044. enum bpf_prog_type *attach_type, bool attach_drv)
  1045. {
  1046. /* not an attachment, just a refcount inc, always allow */
  1047. if (!attach_type)
  1048. return true;
  1049. if (prog->type != *attach_type)
  1050. return false;
  1051. if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
  1052. return false;
  1053. return true;
  1054. }
  1055. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
  1056. bool attach_drv)
  1057. {
  1058. struct fd f = fdget(ufd);
  1059. struct bpf_prog *prog;
  1060. prog = ____bpf_prog_get(f);
  1061. if (IS_ERR(prog))
  1062. return prog;
  1063. if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
  1064. prog = ERR_PTR(-EINVAL);
  1065. goto out;
  1066. }
  1067. prog = bpf_prog_inc(prog);
  1068. out:
  1069. fdput(f);
  1070. return prog;
  1071. }
  1072. struct bpf_prog *bpf_prog_get(u32 ufd)
  1073. {
  1074. return __bpf_prog_get(ufd, NULL, false);
  1075. }
  1076. struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
  1077. bool attach_drv)
  1078. {
  1079. return __bpf_prog_get(ufd, &type, attach_drv);
  1080. }
  1081. EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
  1082. /* Initially all BPF programs could be loaded w/o specifying
  1083. * expected_attach_type. Later for some of them specifying expected_attach_type
  1084. * at load time became required so that program could be validated properly.
  1085. * Programs of types that are allowed to be loaded both w/ and w/o (for
  1086. * backward compatibility) expected_attach_type, should have the default attach
  1087. * type assigned to expected_attach_type for the latter case, so that it can be
  1088. * validated later at attach time.
  1089. *
  1090. * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
  1091. * prog type requires it but has some attach types that have to be backward
  1092. * compatible.
  1093. */
  1094. static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
  1095. {
  1096. switch (attr->prog_type) {
  1097. case BPF_PROG_TYPE_CGROUP_SOCK:
  1098. /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
  1099. * exist so checking for non-zero is the way to go here.
  1100. */
  1101. if (!attr->expected_attach_type)
  1102. attr->expected_attach_type =
  1103. BPF_CGROUP_INET_SOCK_CREATE;
  1104. break;
  1105. }
  1106. }
  1107. static int
  1108. bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
  1109. enum bpf_attach_type expected_attach_type)
  1110. {
  1111. switch (prog_type) {
  1112. case BPF_PROG_TYPE_CGROUP_SOCK:
  1113. switch (expected_attach_type) {
  1114. case BPF_CGROUP_INET_SOCK_CREATE:
  1115. case BPF_CGROUP_INET4_POST_BIND:
  1116. case BPF_CGROUP_INET6_POST_BIND:
  1117. return 0;
  1118. default:
  1119. return -EINVAL;
  1120. }
  1121. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  1122. switch (expected_attach_type) {
  1123. case BPF_CGROUP_INET4_BIND:
  1124. case BPF_CGROUP_INET6_BIND:
  1125. case BPF_CGROUP_INET4_CONNECT:
  1126. case BPF_CGROUP_INET6_CONNECT:
  1127. case BPF_CGROUP_UDP4_SENDMSG:
  1128. case BPF_CGROUP_UDP6_SENDMSG:
  1129. return 0;
  1130. default:
  1131. return -EINVAL;
  1132. }
  1133. default:
  1134. return 0;
  1135. }
  1136. }
  1137. /* last field in 'union bpf_attr' used by this command */
  1138. #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
  1139. static int bpf_prog_load(union bpf_attr *attr)
  1140. {
  1141. enum bpf_prog_type type = attr->prog_type;
  1142. struct bpf_prog *prog;
  1143. int err;
  1144. char license[128];
  1145. bool is_gpl;
  1146. if (CHECK_ATTR(BPF_PROG_LOAD))
  1147. return -EINVAL;
  1148. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  1149. return -EINVAL;
  1150. /* copy eBPF program license from user space */
  1151. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  1152. sizeof(license) - 1) < 0)
  1153. return -EFAULT;
  1154. license[sizeof(license) - 1] = 0;
  1155. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1156. is_gpl = license_is_gpl_compatible(license);
  1157. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  1158. return -E2BIG;
  1159. if (type == BPF_PROG_TYPE_KPROBE &&
  1160. attr->kern_version != LINUX_VERSION_CODE)
  1161. return -EINVAL;
  1162. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  1163. type != BPF_PROG_TYPE_CGROUP_SKB &&
  1164. !capable(CAP_SYS_ADMIN))
  1165. return -EPERM;
  1166. bpf_prog_load_fixup_attach_type(attr);
  1167. if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
  1168. return -EINVAL;
  1169. /* plain bpf_prog allocation */
  1170. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  1171. if (!prog)
  1172. return -ENOMEM;
  1173. prog->expected_attach_type = attr->expected_attach_type;
  1174. prog->aux->offload_requested = !!attr->prog_ifindex;
  1175. err = security_bpf_prog_alloc(prog->aux);
  1176. if (err)
  1177. goto free_prog_nouncharge;
  1178. err = bpf_prog_charge_memlock(prog);
  1179. if (err)
  1180. goto free_prog_sec;
  1181. prog->len = attr->insn_cnt;
  1182. err = -EFAULT;
  1183. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  1184. bpf_prog_insn_size(prog)) != 0)
  1185. goto free_prog;
  1186. prog->orig_prog = NULL;
  1187. prog->jited = 0;
  1188. atomic_set(&prog->aux->refcnt, 1);
  1189. prog->gpl_compatible = is_gpl ? 1 : 0;
  1190. if (bpf_prog_is_dev_bound(prog->aux)) {
  1191. err = bpf_prog_offload_init(prog, attr);
  1192. if (err)
  1193. goto free_prog;
  1194. }
  1195. /* find program type: socket_filter vs tracing_filter */
  1196. err = find_prog_type(type, prog);
  1197. if (err < 0)
  1198. goto free_prog;
  1199. prog->aux->load_time = ktime_get_boot_ns();
  1200. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
  1201. if (err)
  1202. goto free_prog;
  1203. /* run eBPF verifier */
  1204. err = bpf_check(&prog, attr);
  1205. if (err < 0)
  1206. goto free_used_maps;
  1207. prog = bpf_prog_select_runtime(prog, &err);
  1208. if (err < 0)
  1209. goto free_used_maps;
  1210. err = bpf_prog_alloc_id(prog);
  1211. if (err)
  1212. goto free_used_maps;
  1213. err = bpf_prog_new_fd(prog);
  1214. if (err < 0) {
  1215. /* failed to allocate fd.
  1216. * bpf_prog_put() is needed because the above
  1217. * bpf_prog_alloc_id() has published the prog
  1218. * to the userspace and the userspace may
  1219. * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
  1220. */
  1221. bpf_prog_put(prog);
  1222. return err;
  1223. }
  1224. bpf_prog_kallsyms_add(prog);
  1225. return err;
  1226. free_used_maps:
  1227. bpf_prog_kallsyms_del_subprogs(prog);
  1228. free_used_maps(prog->aux);
  1229. free_prog:
  1230. bpf_prog_uncharge_memlock(prog);
  1231. free_prog_sec:
  1232. security_bpf_prog_free(prog->aux);
  1233. free_prog_nouncharge:
  1234. bpf_prog_free(prog);
  1235. return err;
  1236. }
  1237. #define BPF_OBJ_LAST_FIELD file_flags
  1238. static int bpf_obj_pin(const union bpf_attr *attr)
  1239. {
  1240. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  1241. return -EINVAL;
  1242. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  1243. }
  1244. static int bpf_obj_get(const union bpf_attr *attr)
  1245. {
  1246. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  1247. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  1248. return -EINVAL;
  1249. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  1250. attr->file_flags);
  1251. }
  1252. struct bpf_raw_tracepoint {
  1253. struct bpf_raw_event_map *btp;
  1254. struct bpf_prog *prog;
  1255. };
  1256. static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
  1257. {
  1258. struct bpf_raw_tracepoint *raw_tp = filp->private_data;
  1259. if (raw_tp->prog) {
  1260. bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
  1261. bpf_prog_put(raw_tp->prog);
  1262. }
  1263. kfree(raw_tp);
  1264. return 0;
  1265. }
  1266. static const struct file_operations bpf_raw_tp_fops = {
  1267. .release = bpf_raw_tracepoint_release,
  1268. .read = bpf_dummy_read,
  1269. .write = bpf_dummy_write,
  1270. };
  1271. #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
  1272. static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
  1273. {
  1274. struct bpf_raw_tracepoint *raw_tp;
  1275. struct bpf_raw_event_map *btp;
  1276. struct bpf_prog *prog;
  1277. char tp_name[128];
  1278. int tp_fd, err;
  1279. if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
  1280. sizeof(tp_name) - 1) < 0)
  1281. return -EFAULT;
  1282. tp_name[sizeof(tp_name) - 1] = 0;
  1283. btp = bpf_find_raw_tracepoint(tp_name);
  1284. if (!btp)
  1285. return -ENOENT;
  1286. raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
  1287. if (!raw_tp)
  1288. return -ENOMEM;
  1289. raw_tp->btp = btp;
  1290. prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
  1291. BPF_PROG_TYPE_RAW_TRACEPOINT);
  1292. if (IS_ERR(prog)) {
  1293. err = PTR_ERR(prog);
  1294. goto out_free_tp;
  1295. }
  1296. err = bpf_probe_register(raw_tp->btp, prog);
  1297. if (err)
  1298. goto out_put_prog;
  1299. raw_tp->prog = prog;
  1300. tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
  1301. O_CLOEXEC);
  1302. if (tp_fd < 0) {
  1303. bpf_probe_unregister(raw_tp->btp, prog);
  1304. err = tp_fd;
  1305. goto out_put_prog;
  1306. }
  1307. return tp_fd;
  1308. out_put_prog:
  1309. bpf_prog_put(prog);
  1310. out_free_tp:
  1311. kfree(raw_tp);
  1312. return err;
  1313. }
  1314. static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
  1315. enum bpf_attach_type attach_type)
  1316. {
  1317. switch (prog->type) {
  1318. case BPF_PROG_TYPE_CGROUP_SOCK:
  1319. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  1320. return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
  1321. default:
  1322. return 0;
  1323. }
  1324. }
  1325. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  1326. #define BPF_F_ATTACH_MASK \
  1327. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
  1328. static int bpf_prog_attach(const union bpf_attr *attr)
  1329. {
  1330. enum bpf_prog_type ptype;
  1331. struct bpf_prog *prog;
  1332. int ret;
  1333. if (!capable(CAP_NET_ADMIN))
  1334. return -EPERM;
  1335. if (CHECK_ATTR(BPF_PROG_ATTACH))
  1336. return -EINVAL;
  1337. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  1338. return -EINVAL;
  1339. switch (attr->attach_type) {
  1340. case BPF_CGROUP_INET_INGRESS:
  1341. case BPF_CGROUP_INET_EGRESS:
  1342. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1343. break;
  1344. case BPF_CGROUP_INET_SOCK_CREATE:
  1345. case BPF_CGROUP_INET4_POST_BIND:
  1346. case BPF_CGROUP_INET6_POST_BIND:
  1347. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1348. break;
  1349. case BPF_CGROUP_INET4_BIND:
  1350. case BPF_CGROUP_INET6_BIND:
  1351. case BPF_CGROUP_INET4_CONNECT:
  1352. case BPF_CGROUP_INET6_CONNECT:
  1353. case BPF_CGROUP_UDP4_SENDMSG:
  1354. case BPF_CGROUP_UDP6_SENDMSG:
  1355. ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
  1356. break;
  1357. case BPF_CGROUP_SOCK_OPS:
  1358. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1359. break;
  1360. case BPF_CGROUP_DEVICE:
  1361. ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
  1362. break;
  1363. case BPF_SK_MSG_VERDICT:
  1364. ptype = BPF_PROG_TYPE_SK_MSG;
  1365. break;
  1366. case BPF_SK_SKB_STREAM_PARSER:
  1367. case BPF_SK_SKB_STREAM_VERDICT:
  1368. ptype = BPF_PROG_TYPE_SK_SKB;
  1369. break;
  1370. case BPF_LIRC_MODE2:
  1371. ptype = BPF_PROG_TYPE_LIRC_MODE2;
  1372. break;
  1373. case BPF_FLOW_DISSECTOR:
  1374. ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
  1375. break;
  1376. default:
  1377. return -EINVAL;
  1378. }
  1379. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1380. if (IS_ERR(prog))
  1381. return PTR_ERR(prog);
  1382. if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
  1383. bpf_prog_put(prog);
  1384. return -EINVAL;
  1385. }
  1386. switch (ptype) {
  1387. case BPF_PROG_TYPE_SK_SKB:
  1388. case BPF_PROG_TYPE_SK_MSG:
  1389. ret = sockmap_get_from_fd(attr, ptype, prog);
  1390. break;
  1391. case BPF_PROG_TYPE_LIRC_MODE2:
  1392. ret = lirc_prog_attach(attr, prog);
  1393. break;
  1394. case BPF_PROG_TYPE_FLOW_DISSECTOR:
  1395. ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
  1396. break;
  1397. default:
  1398. ret = cgroup_bpf_prog_attach(attr, ptype, prog);
  1399. }
  1400. if (ret)
  1401. bpf_prog_put(prog);
  1402. return ret;
  1403. }
  1404. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1405. static int bpf_prog_detach(const union bpf_attr *attr)
  1406. {
  1407. enum bpf_prog_type ptype;
  1408. if (!capable(CAP_NET_ADMIN))
  1409. return -EPERM;
  1410. if (CHECK_ATTR(BPF_PROG_DETACH))
  1411. return -EINVAL;
  1412. switch (attr->attach_type) {
  1413. case BPF_CGROUP_INET_INGRESS:
  1414. case BPF_CGROUP_INET_EGRESS:
  1415. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1416. break;
  1417. case BPF_CGROUP_INET_SOCK_CREATE:
  1418. case BPF_CGROUP_INET4_POST_BIND:
  1419. case BPF_CGROUP_INET6_POST_BIND:
  1420. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1421. break;
  1422. case BPF_CGROUP_INET4_BIND:
  1423. case BPF_CGROUP_INET6_BIND:
  1424. case BPF_CGROUP_INET4_CONNECT:
  1425. case BPF_CGROUP_INET6_CONNECT:
  1426. case BPF_CGROUP_UDP4_SENDMSG:
  1427. case BPF_CGROUP_UDP6_SENDMSG:
  1428. ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
  1429. break;
  1430. case BPF_CGROUP_SOCK_OPS:
  1431. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1432. break;
  1433. case BPF_CGROUP_DEVICE:
  1434. ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
  1435. break;
  1436. case BPF_SK_MSG_VERDICT:
  1437. return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, NULL);
  1438. case BPF_SK_SKB_STREAM_PARSER:
  1439. case BPF_SK_SKB_STREAM_VERDICT:
  1440. return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, NULL);
  1441. case BPF_LIRC_MODE2:
  1442. return lirc_prog_detach(attr);
  1443. case BPF_FLOW_DISSECTOR:
  1444. return skb_flow_dissector_bpf_prog_detach(attr);
  1445. default:
  1446. return -EINVAL;
  1447. }
  1448. return cgroup_bpf_prog_detach(attr, ptype);
  1449. }
  1450. #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
  1451. static int bpf_prog_query(const union bpf_attr *attr,
  1452. union bpf_attr __user *uattr)
  1453. {
  1454. if (!capable(CAP_NET_ADMIN))
  1455. return -EPERM;
  1456. if (CHECK_ATTR(BPF_PROG_QUERY))
  1457. return -EINVAL;
  1458. if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
  1459. return -EINVAL;
  1460. switch (attr->query.attach_type) {
  1461. case BPF_CGROUP_INET_INGRESS:
  1462. case BPF_CGROUP_INET_EGRESS:
  1463. case BPF_CGROUP_INET_SOCK_CREATE:
  1464. case BPF_CGROUP_INET4_BIND:
  1465. case BPF_CGROUP_INET6_BIND:
  1466. case BPF_CGROUP_INET4_POST_BIND:
  1467. case BPF_CGROUP_INET6_POST_BIND:
  1468. case BPF_CGROUP_INET4_CONNECT:
  1469. case BPF_CGROUP_INET6_CONNECT:
  1470. case BPF_CGROUP_UDP4_SENDMSG:
  1471. case BPF_CGROUP_UDP6_SENDMSG:
  1472. case BPF_CGROUP_SOCK_OPS:
  1473. case BPF_CGROUP_DEVICE:
  1474. break;
  1475. case BPF_LIRC_MODE2:
  1476. return lirc_prog_query(attr, uattr);
  1477. default:
  1478. return -EINVAL;
  1479. }
  1480. return cgroup_bpf_prog_query(attr, uattr);
  1481. }
  1482. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1483. static int bpf_prog_test_run(const union bpf_attr *attr,
  1484. union bpf_attr __user *uattr)
  1485. {
  1486. struct bpf_prog *prog;
  1487. int ret = -ENOTSUPP;
  1488. if (!capable(CAP_SYS_ADMIN))
  1489. return -EPERM;
  1490. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1491. return -EINVAL;
  1492. prog = bpf_prog_get(attr->test.prog_fd);
  1493. if (IS_ERR(prog))
  1494. return PTR_ERR(prog);
  1495. if (prog->aux->ops->test_run)
  1496. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1497. bpf_prog_put(prog);
  1498. return ret;
  1499. }
  1500. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1501. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1502. union bpf_attr __user *uattr,
  1503. struct idr *idr,
  1504. spinlock_t *lock)
  1505. {
  1506. u32 next_id = attr->start_id;
  1507. int err = 0;
  1508. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1509. return -EINVAL;
  1510. if (!capable(CAP_SYS_ADMIN))
  1511. return -EPERM;
  1512. next_id++;
  1513. spin_lock_bh(lock);
  1514. if (!idr_get_next(idr, &next_id))
  1515. err = -ENOENT;
  1516. spin_unlock_bh(lock);
  1517. if (!err)
  1518. err = put_user(next_id, &uattr->next_id);
  1519. return err;
  1520. }
  1521. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1522. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1523. {
  1524. struct bpf_prog *prog;
  1525. u32 id = attr->prog_id;
  1526. int fd;
  1527. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1528. return -EINVAL;
  1529. if (!capable(CAP_SYS_ADMIN))
  1530. return -EPERM;
  1531. spin_lock_bh(&prog_idr_lock);
  1532. prog = idr_find(&prog_idr, id);
  1533. if (prog)
  1534. prog = bpf_prog_inc_not_zero(prog);
  1535. else
  1536. prog = ERR_PTR(-ENOENT);
  1537. spin_unlock_bh(&prog_idr_lock);
  1538. if (IS_ERR(prog))
  1539. return PTR_ERR(prog);
  1540. fd = bpf_prog_new_fd(prog);
  1541. if (fd < 0)
  1542. bpf_prog_put(prog);
  1543. return fd;
  1544. }
  1545. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  1546. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1547. {
  1548. struct bpf_map *map;
  1549. u32 id = attr->map_id;
  1550. int f_flags;
  1551. int fd;
  1552. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  1553. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  1554. return -EINVAL;
  1555. if (!capable(CAP_SYS_ADMIN))
  1556. return -EPERM;
  1557. f_flags = bpf_get_file_flag(attr->open_flags);
  1558. if (f_flags < 0)
  1559. return f_flags;
  1560. spin_lock_bh(&map_idr_lock);
  1561. map = idr_find(&map_idr, id);
  1562. if (map)
  1563. map = bpf_map_inc_not_zero(map, true);
  1564. else
  1565. map = ERR_PTR(-ENOENT);
  1566. spin_unlock_bh(&map_idr_lock);
  1567. if (IS_ERR(map))
  1568. return PTR_ERR(map);
  1569. fd = bpf_map_new_fd(map, f_flags);
  1570. if (fd < 0)
  1571. bpf_map_put(map);
  1572. return fd;
  1573. }
  1574. static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
  1575. unsigned long addr)
  1576. {
  1577. int i;
  1578. for (i = 0; i < prog->aux->used_map_cnt; i++)
  1579. if (prog->aux->used_maps[i] == (void *)addr)
  1580. return prog->aux->used_maps[i];
  1581. return NULL;
  1582. }
  1583. static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
  1584. {
  1585. const struct bpf_map *map;
  1586. struct bpf_insn *insns;
  1587. u64 imm;
  1588. int i;
  1589. insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
  1590. GFP_USER);
  1591. if (!insns)
  1592. return insns;
  1593. for (i = 0; i < prog->len; i++) {
  1594. if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
  1595. insns[i].code = BPF_JMP | BPF_CALL;
  1596. insns[i].imm = BPF_FUNC_tail_call;
  1597. /* fall-through */
  1598. }
  1599. if (insns[i].code == (BPF_JMP | BPF_CALL) ||
  1600. insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
  1601. if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
  1602. insns[i].code = BPF_JMP | BPF_CALL;
  1603. if (!bpf_dump_raw_ok())
  1604. insns[i].imm = 0;
  1605. continue;
  1606. }
  1607. if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
  1608. continue;
  1609. imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
  1610. map = bpf_map_from_imm(prog, imm);
  1611. if (map) {
  1612. insns[i].src_reg = BPF_PSEUDO_MAP_FD;
  1613. insns[i].imm = map->id;
  1614. insns[i + 1].imm = 0;
  1615. continue;
  1616. }
  1617. if (!bpf_dump_raw_ok() &&
  1618. imm == (unsigned long)prog->aux) {
  1619. insns[i].imm = 0;
  1620. insns[i + 1].imm = 0;
  1621. continue;
  1622. }
  1623. }
  1624. return insns;
  1625. }
  1626. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1627. const union bpf_attr *attr,
  1628. union bpf_attr __user *uattr)
  1629. {
  1630. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1631. struct bpf_prog_info info = {};
  1632. u32 info_len = attr->info.info_len;
  1633. char __user *uinsns;
  1634. u32 ulen;
  1635. int err;
  1636. err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1637. if (err)
  1638. return err;
  1639. info_len = min_t(u32, sizeof(info), info_len);
  1640. if (copy_from_user(&info, uinfo, info_len))
  1641. return -EFAULT;
  1642. info.type = prog->type;
  1643. info.id = prog->aux->id;
  1644. info.load_time = prog->aux->load_time;
  1645. info.created_by_uid = from_kuid_munged(current_user_ns(),
  1646. prog->aux->user->uid);
  1647. info.gpl_compatible = prog->gpl_compatible;
  1648. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1649. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  1650. ulen = info.nr_map_ids;
  1651. info.nr_map_ids = prog->aux->used_map_cnt;
  1652. ulen = min_t(u32, info.nr_map_ids, ulen);
  1653. if (ulen) {
  1654. u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
  1655. u32 i;
  1656. for (i = 0; i < ulen; i++)
  1657. if (put_user(prog->aux->used_maps[i]->id,
  1658. &user_map_ids[i]))
  1659. return -EFAULT;
  1660. }
  1661. if (!capable(CAP_SYS_ADMIN)) {
  1662. info.jited_prog_len = 0;
  1663. info.xlated_prog_len = 0;
  1664. info.nr_jited_ksyms = 0;
  1665. goto done;
  1666. }
  1667. ulen = info.xlated_prog_len;
  1668. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1669. if (info.xlated_prog_len && ulen) {
  1670. struct bpf_insn *insns_sanitized;
  1671. bool fault;
  1672. if (prog->blinded && !bpf_dump_raw_ok()) {
  1673. info.xlated_prog_insns = 0;
  1674. goto done;
  1675. }
  1676. insns_sanitized = bpf_insn_prepare_dump(prog);
  1677. if (!insns_sanitized)
  1678. return -ENOMEM;
  1679. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1680. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1681. fault = copy_to_user(uinsns, insns_sanitized, ulen);
  1682. kfree(insns_sanitized);
  1683. if (fault)
  1684. return -EFAULT;
  1685. }
  1686. if (bpf_prog_is_dev_bound(prog->aux)) {
  1687. err = bpf_prog_offload_info_fill(&info, prog);
  1688. if (err)
  1689. return err;
  1690. goto done;
  1691. }
  1692. /* NOTE: the following code is supposed to be skipped for offload.
  1693. * bpf_prog_offload_info_fill() is the place to fill similar fields
  1694. * for offload.
  1695. */
  1696. ulen = info.jited_prog_len;
  1697. if (prog->aux->func_cnt) {
  1698. u32 i;
  1699. info.jited_prog_len = 0;
  1700. for (i = 0; i < prog->aux->func_cnt; i++)
  1701. info.jited_prog_len += prog->aux->func[i]->jited_len;
  1702. } else {
  1703. info.jited_prog_len = prog->jited_len;
  1704. }
  1705. if (info.jited_prog_len && ulen) {
  1706. if (bpf_dump_raw_ok()) {
  1707. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1708. ulen = min_t(u32, info.jited_prog_len, ulen);
  1709. /* for multi-function programs, copy the JITed
  1710. * instructions for all the functions
  1711. */
  1712. if (prog->aux->func_cnt) {
  1713. u32 len, free, i;
  1714. u8 *img;
  1715. free = ulen;
  1716. for (i = 0; i < prog->aux->func_cnt; i++) {
  1717. len = prog->aux->func[i]->jited_len;
  1718. len = min_t(u32, len, free);
  1719. img = (u8 *) prog->aux->func[i]->bpf_func;
  1720. if (copy_to_user(uinsns, img, len))
  1721. return -EFAULT;
  1722. uinsns += len;
  1723. free -= len;
  1724. if (!free)
  1725. break;
  1726. }
  1727. } else {
  1728. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1729. return -EFAULT;
  1730. }
  1731. } else {
  1732. info.jited_prog_insns = 0;
  1733. }
  1734. }
  1735. ulen = info.nr_jited_ksyms;
  1736. info.nr_jited_ksyms = prog->aux->func_cnt;
  1737. if (info.nr_jited_ksyms && ulen) {
  1738. if (bpf_dump_raw_ok()) {
  1739. u64 __user *user_ksyms;
  1740. ulong ksym_addr;
  1741. u32 i;
  1742. /* copy the address of the kernel symbol
  1743. * corresponding to each function
  1744. */
  1745. ulen = min_t(u32, info.nr_jited_ksyms, ulen);
  1746. user_ksyms = u64_to_user_ptr(info.jited_ksyms);
  1747. for (i = 0; i < ulen; i++) {
  1748. ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
  1749. ksym_addr &= PAGE_MASK;
  1750. if (put_user((u64) ksym_addr, &user_ksyms[i]))
  1751. return -EFAULT;
  1752. }
  1753. } else {
  1754. info.jited_ksyms = 0;
  1755. }
  1756. }
  1757. ulen = info.nr_jited_func_lens;
  1758. info.nr_jited_func_lens = prog->aux->func_cnt;
  1759. if (info.nr_jited_func_lens && ulen) {
  1760. if (bpf_dump_raw_ok()) {
  1761. u32 __user *user_lens;
  1762. u32 func_len, i;
  1763. /* copy the JITed image lengths for each function */
  1764. ulen = min_t(u32, info.nr_jited_func_lens, ulen);
  1765. user_lens = u64_to_user_ptr(info.jited_func_lens);
  1766. for (i = 0; i < ulen; i++) {
  1767. func_len = prog->aux->func[i]->jited_len;
  1768. if (put_user(func_len, &user_lens[i]))
  1769. return -EFAULT;
  1770. }
  1771. } else {
  1772. info.jited_func_lens = 0;
  1773. }
  1774. }
  1775. done:
  1776. if (copy_to_user(uinfo, &info, info_len) ||
  1777. put_user(info_len, &uattr->info.info_len))
  1778. return -EFAULT;
  1779. return 0;
  1780. }
  1781. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1782. const union bpf_attr *attr,
  1783. union bpf_attr __user *uattr)
  1784. {
  1785. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1786. struct bpf_map_info info = {};
  1787. u32 info_len = attr->info.info_len;
  1788. int err;
  1789. err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1790. if (err)
  1791. return err;
  1792. info_len = min_t(u32, sizeof(info), info_len);
  1793. info.type = map->map_type;
  1794. info.id = map->id;
  1795. info.key_size = map->key_size;
  1796. info.value_size = map->value_size;
  1797. info.max_entries = map->max_entries;
  1798. info.map_flags = map->map_flags;
  1799. memcpy(info.name, map->name, sizeof(map->name));
  1800. if (map->btf) {
  1801. info.btf_id = btf_id(map->btf);
  1802. info.btf_key_type_id = map->btf_key_type_id;
  1803. info.btf_value_type_id = map->btf_value_type_id;
  1804. }
  1805. if (bpf_map_is_dev_bound(map)) {
  1806. err = bpf_map_offload_info_fill(&info, map);
  1807. if (err)
  1808. return err;
  1809. }
  1810. if (copy_to_user(uinfo, &info, info_len) ||
  1811. put_user(info_len, &uattr->info.info_len))
  1812. return -EFAULT;
  1813. return 0;
  1814. }
  1815. static int bpf_btf_get_info_by_fd(struct btf *btf,
  1816. const union bpf_attr *attr,
  1817. union bpf_attr __user *uattr)
  1818. {
  1819. struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1820. u32 info_len = attr->info.info_len;
  1821. int err;
  1822. err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
  1823. if (err)
  1824. return err;
  1825. return btf_get_info_by_fd(btf, attr, uattr);
  1826. }
  1827. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1828. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1829. union bpf_attr __user *uattr)
  1830. {
  1831. int ufd = attr->info.bpf_fd;
  1832. struct fd f;
  1833. int err;
  1834. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1835. return -EINVAL;
  1836. f = fdget(ufd);
  1837. if (!f.file)
  1838. return -EBADFD;
  1839. if (f.file->f_op == &bpf_prog_fops)
  1840. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1841. uattr);
  1842. else if (f.file->f_op == &bpf_map_fops)
  1843. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1844. uattr);
  1845. else if (f.file->f_op == &btf_fops)
  1846. err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
  1847. else
  1848. err = -EINVAL;
  1849. fdput(f);
  1850. return err;
  1851. }
  1852. #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
  1853. static int bpf_btf_load(const union bpf_attr *attr)
  1854. {
  1855. if (CHECK_ATTR(BPF_BTF_LOAD))
  1856. return -EINVAL;
  1857. if (!capable(CAP_SYS_ADMIN))
  1858. return -EPERM;
  1859. return btf_new_fd(attr);
  1860. }
  1861. #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
  1862. static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
  1863. {
  1864. if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
  1865. return -EINVAL;
  1866. if (!capable(CAP_SYS_ADMIN))
  1867. return -EPERM;
  1868. return btf_get_fd_by_id(attr->btf_id);
  1869. }
  1870. static int bpf_task_fd_query_copy(const union bpf_attr *attr,
  1871. union bpf_attr __user *uattr,
  1872. u32 prog_id, u32 fd_type,
  1873. const char *buf, u64 probe_offset,
  1874. u64 probe_addr)
  1875. {
  1876. char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
  1877. u32 len = buf ? strlen(buf) : 0, input_len;
  1878. int err = 0;
  1879. if (put_user(len, &uattr->task_fd_query.buf_len))
  1880. return -EFAULT;
  1881. input_len = attr->task_fd_query.buf_len;
  1882. if (input_len && ubuf) {
  1883. if (!len) {
  1884. /* nothing to copy, just make ubuf NULL terminated */
  1885. char zero = '\0';
  1886. if (put_user(zero, ubuf))
  1887. return -EFAULT;
  1888. } else if (input_len >= len + 1) {
  1889. /* ubuf can hold the string with NULL terminator */
  1890. if (copy_to_user(ubuf, buf, len + 1))
  1891. return -EFAULT;
  1892. } else {
  1893. /* ubuf cannot hold the string with NULL terminator,
  1894. * do a partial copy with NULL terminator.
  1895. */
  1896. char zero = '\0';
  1897. err = -ENOSPC;
  1898. if (copy_to_user(ubuf, buf, input_len - 1))
  1899. return -EFAULT;
  1900. if (put_user(zero, ubuf + input_len - 1))
  1901. return -EFAULT;
  1902. }
  1903. }
  1904. if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
  1905. put_user(fd_type, &uattr->task_fd_query.fd_type) ||
  1906. put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
  1907. put_user(probe_addr, &uattr->task_fd_query.probe_addr))
  1908. return -EFAULT;
  1909. return err;
  1910. }
  1911. #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
  1912. static int bpf_task_fd_query(const union bpf_attr *attr,
  1913. union bpf_attr __user *uattr)
  1914. {
  1915. pid_t pid = attr->task_fd_query.pid;
  1916. u32 fd = attr->task_fd_query.fd;
  1917. const struct perf_event *event;
  1918. struct files_struct *files;
  1919. struct task_struct *task;
  1920. struct file *file;
  1921. int err;
  1922. if (CHECK_ATTR(BPF_TASK_FD_QUERY))
  1923. return -EINVAL;
  1924. if (!capable(CAP_SYS_ADMIN))
  1925. return -EPERM;
  1926. if (attr->task_fd_query.flags != 0)
  1927. return -EINVAL;
  1928. task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
  1929. if (!task)
  1930. return -ENOENT;
  1931. files = get_files_struct(task);
  1932. put_task_struct(task);
  1933. if (!files)
  1934. return -ENOENT;
  1935. err = 0;
  1936. spin_lock(&files->file_lock);
  1937. file = fcheck_files(files, fd);
  1938. if (!file)
  1939. err = -EBADF;
  1940. else
  1941. get_file(file);
  1942. spin_unlock(&files->file_lock);
  1943. put_files_struct(files);
  1944. if (err)
  1945. goto out;
  1946. if (file->f_op == &bpf_raw_tp_fops) {
  1947. struct bpf_raw_tracepoint *raw_tp = file->private_data;
  1948. struct bpf_raw_event_map *btp = raw_tp->btp;
  1949. err = bpf_task_fd_query_copy(attr, uattr,
  1950. raw_tp->prog->aux->id,
  1951. BPF_FD_TYPE_RAW_TRACEPOINT,
  1952. btp->tp->name, 0, 0);
  1953. goto put_file;
  1954. }
  1955. event = perf_get_event(file);
  1956. if (!IS_ERR(event)) {
  1957. u64 probe_offset, probe_addr;
  1958. u32 prog_id, fd_type;
  1959. const char *buf;
  1960. err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
  1961. &buf, &probe_offset,
  1962. &probe_addr);
  1963. if (!err)
  1964. err = bpf_task_fd_query_copy(attr, uattr, prog_id,
  1965. fd_type, buf,
  1966. probe_offset,
  1967. probe_addr);
  1968. goto put_file;
  1969. }
  1970. err = -ENOTSUPP;
  1971. put_file:
  1972. fput(file);
  1973. out:
  1974. return err;
  1975. }
  1976. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1977. {
  1978. union bpf_attr attr = {};
  1979. int err;
  1980. if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
  1981. return -EPERM;
  1982. err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
  1983. if (err)
  1984. return err;
  1985. size = min_t(u32, size, sizeof(attr));
  1986. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  1987. if (copy_from_user(&attr, uattr, size) != 0)
  1988. return -EFAULT;
  1989. err = security_bpf(cmd, &attr, size);
  1990. if (err < 0)
  1991. return err;
  1992. switch (cmd) {
  1993. case BPF_MAP_CREATE:
  1994. err = map_create(&attr);
  1995. break;
  1996. case BPF_MAP_LOOKUP_ELEM:
  1997. err = map_lookup_elem(&attr);
  1998. break;
  1999. case BPF_MAP_UPDATE_ELEM:
  2000. err = map_update_elem(&attr);
  2001. break;
  2002. case BPF_MAP_DELETE_ELEM:
  2003. err = map_delete_elem(&attr);
  2004. break;
  2005. case BPF_MAP_GET_NEXT_KEY:
  2006. err = map_get_next_key(&attr);
  2007. break;
  2008. case BPF_PROG_LOAD:
  2009. err = bpf_prog_load(&attr);
  2010. break;
  2011. case BPF_OBJ_PIN:
  2012. err = bpf_obj_pin(&attr);
  2013. break;
  2014. case BPF_OBJ_GET:
  2015. err = bpf_obj_get(&attr);
  2016. break;
  2017. case BPF_PROG_ATTACH:
  2018. err = bpf_prog_attach(&attr);
  2019. break;
  2020. case BPF_PROG_DETACH:
  2021. err = bpf_prog_detach(&attr);
  2022. break;
  2023. case BPF_PROG_QUERY:
  2024. err = bpf_prog_query(&attr, uattr);
  2025. break;
  2026. case BPF_PROG_TEST_RUN:
  2027. err = bpf_prog_test_run(&attr, uattr);
  2028. break;
  2029. case BPF_PROG_GET_NEXT_ID:
  2030. err = bpf_obj_get_next_id(&attr, uattr,
  2031. &prog_idr, &prog_idr_lock);
  2032. break;
  2033. case BPF_MAP_GET_NEXT_ID:
  2034. err = bpf_obj_get_next_id(&attr, uattr,
  2035. &map_idr, &map_idr_lock);
  2036. break;
  2037. case BPF_PROG_GET_FD_BY_ID:
  2038. err = bpf_prog_get_fd_by_id(&attr);
  2039. break;
  2040. case BPF_MAP_GET_FD_BY_ID:
  2041. err = bpf_map_get_fd_by_id(&attr);
  2042. break;
  2043. case BPF_OBJ_GET_INFO_BY_FD:
  2044. err = bpf_obj_get_info_by_fd(&attr, uattr);
  2045. break;
  2046. case BPF_RAW_TRACEPOINT_OPEN:
  2047. err = bpf_raw_tracepoint_open(&attr);
  2048. break;
  2049. case BPF_BTF_LOAD:
  2050. err = bpf_btf_load(&attr);
  2051. break;
  2052. case BPF_BTF_GET_FD_BY_ID:
  2053. err = bpf_btf_get_fd_by_id(&attr);
  2054. break;
  2055. case BPF_TASK_FD_QUERY:
  2056. err = bpf_task_fd_query(&attr, uattr);
  2057. break;
  2058. default:
  2059. err = -EINVAL;
  2060. break;
  2061. }
  2062. return err;
  2063. }