syscall.c 54 KB

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