syscall.c 54 KB

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