syscall.c 59 KB

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