syscall.c 55 KB

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