syscall.c 54 KB

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