syscall.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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/syscalls.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/file.h>
  21. #include <linux/license.h>
  22. #include <linux/filter.h>
  23. #include <linux/version.h>
  24. #include <linux/kernel.h>
  25. #include <linux/idr.h>
  26. #include <linux/cred.h>
  27. #include <linux/timekeeping.h>
  28. #include <linux/ctype.h>
  29. #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
  30. (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
  31. (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
  32. (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  33. #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
  34. #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
  35. #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
  36. DEFINE_PER_CPU(int, bpf_prog_active);
  37. static DEFINE_IDR(prog_idr);
  38. static DEFINE_SPINLOCK(prog_idr_lock);
  39. static DEFINE_IDR(map_idr);
  40. static DEFINE_SPINLOCK(map_idr_lock);
  41. int sysctl_unprivileged_bpf_disabled __read_mostly;
  42. static const struct bpf_map_ops * const bpf_map_types[] = {
  43. #define BPF_PROG_TYPE(_id, _ops)
  44. #define BPF_MAP_TYPE(_id, _ops) \
  45. [_id] = &_ops,
  46. #include <linux/bpf_types.h>
  47. #undef BPF_PROG_TYPE
  48. #undef BPF_MAP_TYPE
  49. };
  50. /*
  51. * If we're handed a bigger struct than we know of, ensure all the unknown bits
  52. * are 0 - i.e. new user-space does not rely on any kernel feature extensions
  53. * we don't know about yet.
  54. *
  55. * There is a ToCToU between this function call and the following
  56. * copy_from_user() call. However, this is not a concern since this function is
  57. * meant to be a future-proofing of bits.
  58. */
  59. static int check_uarg_tail_zero(void __user *uaddr,
  60. size_t expected_size,
  61. size_t actual_size)
  62. {
  63. unsigned char __user *addr;
  64. unsigned char __user *end;
  65. unsigned char val;
  66. int err;
  67. if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
  68. return -E2BIG;
  69. if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
  70. return -EFAULT;
  71. if (actual_size <= expected_size)
  72. return 0;
  73. addr = uaddr + expected_size;
  74. end = uaddr + actual_size;
  75. for (; addr < end; addr++) {
  76. err = get_user(val, addr);
  77. if (err)
  78. return err;
  79. if (val)
  80. return -E2BIG;
  81. }
  82. return 0;
  83. }
  84. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  85. {
  86. struct bpf_map *map;
  87. if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
  88. !bpf_map_types[attr->map_type])
  89. return ERR_PTR(-EINVAL);
  90. map = bpf_map_types[attr->map_type]->map_alloc(attr);
  91. if (IS_ERR(map))
  92. return map;
  93. map->ops = bpf_map_types[attr->map_type];
  94. map->map_type = attr->map_type;
  95. return map;
  96. }
  97. void *bpf_map_area_alloc(size_t size, int numa_node)
  98. {
  99. /* We definitely need __GFP_NORETRY, so OOM killer doesn't
  100. * trigger under memory pressure as we really just want to
  101. * fail instead.
  102. */
  103. const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
  104. void *area;
  105. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  106. area = kmalloc_node(size, GFP_USER | flags, numa_node);
  107. if (area != NULL)
  108. return area;
  109. }
  110. return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
  111. __builtin_return_address(0));
  112. }
  113. void bpf_map_area_free(void *area)
  114. {
  115. kvfree(area);
  116. }
  117. int bpf_map_precharge_memlock(u32 pages)
  118. {
  119. struct user_struct *user = get_current_user();
  120. unsigned long memlock_limit, cur;
  121. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  122. cur = atomic_long_read(&user->locked_vm);
  123. free_uid(user);
  124. if (cur + pages > memlock_limit)
  125. return -EPERM;
  126. return 0;
  127. }
  128. static int bpf_map_charge_memlock(struct bpf_map *map)
  129. {
  130. struct user_struct *user = get_current_user();
  131. unsigned long memlock_limit;
  132. memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  133. atomic_long_add(map->pages, &user->locked_vm);
  134. if (atomic_long_read(&user->locked_vm) > memlock_limit) {
  135. atomic_long_sub(map->pages, &user->locked_vm);
  136. free_uid(user);
  137. return -EPERM;
  138. }
  139. map->user = user;
  140. return 0;
  141. }
  142. static void bpf_map_uncharge_memlock(struct bpf_map *map)
  143. {
  144. struct user_struct *user = map->user;
  145. atomic_long_sub(map->pages, &user->locked_vm);
  146. free_uid(user);
  147. }
  148. static int bpf_map_alloc_id(struct bpf_map *map)
  149. {
  150. int id;
  151. spin_lock_bh(&map_idr_lock);
  152. id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
  153. if (id > 0)
  154. map->id = id;
  155. spin_unlock_bh(&map_idr_lock);
  156. if (WARN_ON_ONCE(!id))
  157. return -ENOSPC;
  158. return id > 0 ? 0 : id;
  159. }
  160. static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
  161. {
  162. unsigned long flags;
  163. if (do_idr_lock)
  164. spin_lock_irqsave(&map_idr_lock, flags);
  165. else
  166. __acquire(&map_idr_lock);
  167. idr_remove(&map_idr, map->id);
  168. if (do_idr_lock)
  169. spin_unlock_irqrestore(&map_idr_lock, flags);
  170. else
  171. __release(&map_idr_lock);
  172. }
  173. /* called from workqueue */
  174. static void bpf_map_free_deferred(struct work_struct *work)
  175. {
  176. struct bpf_map *map = container_of(work, struct bpf_map, work);
  177. bpf_map_uncharge_memlock(map);
  178. security_bpf_map_free(map);
  179. /* implementation dependent freeing */
  180. map->ops->map_free(map);
  181. }
  182. static void bpf_map_put_uref(struct bpf_map *map)
  183. {
  184. if (atomic_dec_and_test(&map->usercnt)) {
  185. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
  186. bpf_fd_array_map_clear(map);
  187. }
  188. }
  189. /* decrement map refcnt and schedule it for freeing via workqueue
  190. * (unrelying map implementation ops->map_free() might sleep)
  191. */
  192. static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
  193. {
  194. if (atomic_dec_and_test(&map->refcnt)) {
  195. /* bpf_map_free_id() must be called first */
  196. bpf_map_free_id(map, do_idr_lock);
  197. INIT_WORK(&map->work, bpf_map_free_deferred);
  198. schedule_work(&map->work);
  199. }
  200. }
  201. void bpf_map_put(struct bpf_map *map)
  202. {
  203. __bpf_map_put(map, true);
  204. }
  205. void bpf_map_put_with_uref(struct bpf_map *map)
  206. {
  207. bpf_map_put_uref(map);
  208. bpf_map_put(map);
  209. }
  210. static int bpf_map_release(struct inode *inode, struct file *filp)
  211. {
  212. struct bpf_map *map = filp->private_data;
  213. if (map->ops->map_release)
  214. map->ops->map_release(map, filp);
  215. bpf_map_put_with_uref(map);
  216. return 0;
  217. }
  218. #ifdef CONFIG_PROC_FS
  219. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  220. {
  221. const struct bpf_map *map = filp->private_data;
  222. const struct bpf_array *array;
  223. u32 owner_prog_type = 0;
  224. u32 owner_jited = 0;
  225. if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
  226. array = container_of(map, struct bpf_array, map);
  227. owner_prog_type = array->owner_prog_type;
  228. owner_jited = array->owner_jited;
  229. }
  230. seq_printf(m,
  231. "map_type:\t%u\n"
  232. "key_size:\t%u\n"
  233. "value_size:\t%u\n"
  234. "max_entries:\t%u\n"
  235. "map_flags:\t%#x\n"
  236. "memlock:\t%llu\n",
  237. map->map_type,
  238. map->key_size,
  239. map->value_size,
  240. map->max_entries,
  241. map->map_flags,
  242. map->pages * 1ULL << PAGE_SHIFT);
  243. if (owner_prog_type) {
  244. seq_printf(m, "owner_prog_type:\t%u\n",
  245. owner_prog_type);
  246. seq_printf(m, "owner_jited:\t%u\n",
  247. owner_jited);
  248. }
  249. }
  250. #endif
  251. static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
  252. loff_t *ppos)
  253. {
  254. /* We need this handler such that alloc_file() enables
  255. * f_mode with FMODE_CAN_READ.
  256. */
  257. return -EINVAL;
  258. }
  259. static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
  260. size_t siz, loff_t *ppos)
  261. {
  262. /* We need this handler such that alloc_file() enables
  263. * f_mode with FMODE_CAN_WRITE.
  264. */
  265. return -EINVAL;
  266. }
  267. const struct file_operations bpf_map_fops = {
  268. #ifdef CONFIG_PROC_FS
  269. .show_fdinfo = bpf_map_show_fdinfo,
  270. #endif
  271. .release = bpf_map_release,
  272. .read = bpf_dummy_read,
  273. .write = bpf_dummy_write,
  274. };
  275. int bpf_map_new_fd(struct bpf_map *map, int flags)
  276. {
  277. int ret;
  278. ret = security_bpf_map(map, OPEN_FMODE(flags));
  279. if (ret < 0)
  280. return ret;
  281. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  282. flags | O_CLOEXEC);
  283. }
  284. int bpf_get_file_flag(int flags)
  285. {
  286. if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
  287. return -EINVAL;
  288. if (flags & BPF_F_RDONLY)
  289. return O_RDONLY;
  290. if (flags & BPF_F_WRONLY)
  291. return O_WRONLY;
  292. return O_RDWR;
  293. }
  294. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  295. #define CHECK_ATTR(CMD) \
  296. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  297. sizeof(attr->CMD##_LAST_FIELD), 0, \
  298. sizeof(*attr) - \
  299. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  300. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  301. /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
  302. * Return 0 on success and < 0 on error.
  303. */
  304. static int bpf_obj_name_cpy(char *dst, const char *src)
  305. {
  306. const char *end = src + BPF_OBJ_NAME_LEN;
  307. memset(dst, 0, BPF_OBJ_NAME_LEN);
  308. /* Copy all isalnum() and '_' char */
  309. while (src < end && *src) {
  310. if (!isalnum(*src) && *src != '_')
  311. return -EINVAL;
  312. *dst++ = *src++;
  313. }
  314. /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
  315. if (src == end)
  316. return -EINVAL;
  317. return 0;
  318. }
  319. #define BPF_MAP_CREATE_LAST_FIELD map_name
  320. /* called via syscall */
  321. static int map_create(union bpf_attr *attr)
  322. {
  323. int numa_node = bpf_map_attr_numa_node(attr);
  324. struct bpf_map *map;
  325. int f_flags;
  326. int err;
  327. err = CHECK_ATTR(BPF_MAP_CREATE);
  328. if (err)
  329. return -EINVAL;
  330. f_flags = bpf_get_file_flag(attr->map_flags);
  331. if (f_flags < 0)
  332. return f_flags;
  333. if (numa_node != NUMA_NO_NODE &&
  334. ((unsigned int)numa_node >= nr_node_ids ||
  335. !node_online(numa_node)))
  336. return -EINVAL;
  337. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  338. map = find_and_alloc_map(attr);
  339. if (IS_ERR(map))
  340. return PTR_ERR(map);
  341. err = bpf_obj_name_cpy(map->name, attr->map_name);
  342. if (err)
  343. goto free_map_nouncharge;
  344. atomic_set(&map->refcnt, 1);
  345. atomic_set(&map->usercnt, 1);
  346. err = security_bpf_map_alloc(map);
  347. if (err)
  348. goto free_map_nouncharge;
  349. err = bpf_map_charge_memlock(map);
  350. if (err)
  351. goto free_map_sec;
  352. err = bpf_map_alloc_id(map);
  353. if (err)
  354. goto free_map;
  355. err = bpf_map_new_fd(map, f_flags);
  356. if (err < 0) {
  357. /* failed to allocate fd.
  358. * bpf_map_put() is needed because the above
  359. * bpf_map_alloc_id() has published the map
  360. * to the userspace and the userspace may
  361. * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
  362. */
  363. bpf_map_put(map);
  364. return err;
  365. }
  366. trace_bpf_map_create(map, err);
  367. return err;
  368. free_map:
  369. bpf_map_uncharge_memlock(map);
  370. free_map_sec:
  371. security_bpf_map_free(map);
  372. free_map_nouncharge:
  373. map->ops->map_free(map);
  374. return err;
  375. }
  376. /* if error is returned, fd is released.
  377. * On success caller should complete fd access with matching fdput()
  378. */
  379. struct bpf_map *__bpf_map_get(struct fd f)
  380. {
  381. if (!f.file)
  382. return ERR_PTR(-EBADF);
  383. if (f.file->f_op != &bpf_map_fops) {
  384. fdput(f);
  385. return ERR_PTR(-EINVAL);
  386. }
  387. return f.file->private_data;
  388. }
  389. /* prog's and map's refcnt limit */
  390. #define BPF_MAX_REFCNT 32768
  391. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  392. {
  393. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  394. atomic_dec(&map->refcnt);
  395. return ERR_PTR(-EBUSY);
  396. }
  397. if (uref)
  398. atomic_inc(&map->usercnt);
  399. return map;
  400. }
  401. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  402. {
  403. struct fd f = fdget(ufd);
  404. struct bpf_map *map;
  405. map = __bpf_map_get(f);
  406. if (IS_ERR(map))
  407. return map;
  408. map = bpf_map_inc(map, true);
  409. fdput(f);
  410. return map;
  411. }
  412. /* map_idr_lock should have been held */
  413. static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
  414. bool uref)
  415. {
  416. int refold;
  417. refold = __atomic_add_unless(&map->refcnt, 1, 0);
  418. if (refold >= BPF_MAX_REFCNT) {
  419. __bpf_map_put(map, false);
  420. return ERR_PTR(-EBUSY);
  421. }
  422. if (!refold)
  423. return ERR_PTR(-ENOENT);
  424. if (uref)
  425. atomic_inc(&map->usercnt);
  426. return map;
  427. }
  428. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  429. {
  430. return -ENOTSUPP;
  431. }
  432. /* last field in 'union bpf_attr' used by this command */
  433. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  434. static int map_lookup_elem(union bpf_attr *attr)
  435. {
  436. void __user *ukey = u64_to_user_ptr(attr->key);
  437. void __user *uvalue = u64_to_user_ptr(attr->value);
  438. int ufd = attr->map_fd;
  439. struct bpf_map *map;
  440. void *key, *value, *ptr;
  441. u32 value_size;
  442. struct fd f;
  443. int err;
  444. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  445. return -EINVAL;
  446. f = fdget(ufd);
  447. map = __bpf_map_get(f);
  448. if (IS_ERR(map))
  449. return PTR_ERR(map);
  450. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  451. err = -EPERM;
  452. goto err_put;
  453. }
  454. key = memdup_user(ukey, map->key_size);
  455. if (IS_ERR(key)) {
  456. err = PTR_ERR(key);
  457. goto err_put;
  458. }
  459. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  460. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  461. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  462. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  463. else if (IS_FD_MAP(map))
  464. value_size = sizeof(u32);
  465. else
  466. value_size = map->value_size;
  467. err = -ENOMEM;
  468. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  469. if (!value)
  470. goto free_key;
  471. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  472. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  473. err = bpf_percpu_hash_copy(map, key, value);
  474. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  475. err = bpf_percpu_array_copy(map, key, value);
  476. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  477. err = bpf_stackmap_copy(map, key, value);
  478. } else if (IS_FD_ARRAY(map)) {
  479. err = bpf_fd_array_map_lookup_elem(map, key, value);
  480. } else if (IS_FD_HASH(map)) {
  481. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  482. } else {
  483. rcu_read_lock();
  484. ptr = map->ops->map_lookup_elem(map, key);
  485. if (ptr)
  486. memcpy(value, ptr, value_size);
  487. rcu_read_unlock();
  488. err = ptr ? 0 : -ENOENT;
  489. }
  490. if (err)
  491. goto free_value;
  492. err = -EFAULT;
  493. if (copy_to_user(uvalue, value, value_size) != 0)
  494. goto free_value;
  495. trace_bpf_map_lookup_elem(map, ufd, key, value);
  496. err = 0;
  497. free_value:
  498. kfree(value);
  499. free_key:
  500. kfree(key);
  501. err_put:
  502. fdput(f);
  503. return err;
  504. }
  505. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  506. static int map_update_elem(union bpf_attr *attr)
  507. {
  508. void __user *ukey = u64_to_user_ptr(attr->key);
  509. void __user *uvalue = u64_to_user_ptr(attr->value);
  510. int ufd = attr->map_fd;
  511. struct bpf_map *map;
  512. void *key, *value;
  513. u32 value_size;
  514. struct fd f;
  515. int err;
  516. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  517. return -EINVAL;
  518. f = fdget(ufd);
  519. map = __bpf_map_get(f);
  520. if (IS_ERR(map))
  521. return PTR_ERR(map);
  522. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  523. err = -EPERM;
  524. goto err_put;
  525. }
  526. key = memdup_user(ukey, map->key_size);
  527. if (IS_ERR(key)) {
  528. err = PTR_ERR(key);
  529. goto err_put;
  530. }
  531. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  532. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  533. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  534. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  535. else
  536. value_size = map->value_size;
  537. err = -ENOMEM;
  538. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  539. if (!value)
  540. goto free_key;
  541. err = -EFAULT;
  542. if (copy_from_user(value, uvalue, value_size) != 0)
  543. goto free_value;
  544. /* Need to create a kthread, thus must support schedule */
  545. if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
  546. err = map->ops->map_update_elem(map, key, value, attr->flags);
  547. goto out;
  548. }
  549. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  550. * inside bpf map update or delete otherwise deadlocks are possible
  551. */
  552. preempt_disable();
  553. __this_cpu_inc(bpf_prog_active);
  554. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  555. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  556. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  557. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  558. err = bpf_percpu_array_update(map, key, value, attr->flags);
  559. } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
  560. map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  561. map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
  562. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
  563. rcu_read_lock();
  564. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  565. attr->flags);
  566. rcu_read_unlock();
  567. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  568. rcu_read_lock();
  569. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  570. attr->flags);
  571. rcu_read_unlock();
  572. } else {
  573. rcu_read_lock();
  574. err = map->ops->map_update_elem(map, key, value, attr->flags);
  575. rcu_read_unlock();
  576. }
  577. __this_cpu_dec(bpf_prog_active);
  578. preempt_enable();
  579. out:
  580. if (!err)
  581. trace_bpf_map_update_elem(map, ufd, key, value);
  582. free_value:
  583. kfree(value);
  584. free_key:
  585. kfree(key);
  586. err_put:
  587. fdput(f);
  588. return err;
  589. }
  590. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  591. static int map_delete_elem(union bpf_attr *attr)
  592. {
  593. void __user *ukey = u64_to_user_ptr(attr->key);
  594. int ufd = attr->map_fd;
  595. struct bpf_map *map;
  596. struct fd f;
  597. void *key;
  598. int err;
  599. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  600. return -EINVAL;
  601. f = fdget(ufd);
  602. map = __bpf_map_get(f);
  603. if (IS_ERR(map))
  604. return PTR_ERR(map);
  605. if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
  606. err = -EPERM;
  607. goto err_put;
  608. }
  609. key = memdup_user(ukey, map->key_size);
  610. if (IS_ERR(key)) {
  611. err = PTR_ERR(key);
  612. goto err_put;
  613. }
  614. preempt_disable();
  615. __this_cpu_inc(bpf_prog_active);
  616. rcu_read_lock();
  617. err = map->ops->map_delete_elem(map, key);
  618. rcu_read_unlock();
  619. __this_cpu_dec(bpf_prog_active);
  620. preempt_enable();
  621. if (!err)
  622. trace_bpf_map_delete_elem(map, ufd, key);
  623. kfree(key);
  624. err_put:
  625. fdput(f);
  626. return err;
  627. }
  628. /* last field in 'union bpf_attr' used by this command */
  629. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  630. static int map_get_next_key(union bpf_attr *attr)
  631. {
  632. void __user *ukey = u64_to_user_ptr(attr->key);
  633. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  634. int ufd = attr->map_fd;
  635. struct bpf_map *map;
  636. void *key, *next_key;
  637. struct fd f;
  638. int err;
  639. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  640. return -EINVAL;
  641. f = fdget(ufd);
  642. map = __bpf_map_get(f);
  643. if (IS_ERR(map))
  644. return PTR_ERR(map);
  645. if (!(f.file->f_mode & FMODE_CAN_READ)) {
  646. err = -EPERM;
  647. goto err_put;
  648. }
  649. if (ukey) {
  650. key = memdup_user(ukey, map->key_size);
  651. if (IS_ERR(key)) {
  652. err = PTR_ERR(key);
  653. goto err_put;
  654. }
  655. } else {
  656. key = NULL;
  657. }
  658. err = -ENOMEM;
  659. next_key = kmalloc(map->key_size, GFP_USER);
  660. if (!next_key)
  661. goto free_key;
  662. rcu_read_lock();
  663. err = map->ops->map_get_next_key(map, key, next_key);
  664. rcu_read_unlock();
  665. if (err)
  666. goto free_next_key;
  667. err = -EFAULT;
  668. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  669. goto free_next_key;
  670. trace_bpf_map_next_key(map, ufd, key, next_key);
  671. err = 0;
  672. free_next_key:
  673. kfree(next_key);
  674. free_key:
  675. kfree(key);
  676. err_put:
  677. fdput(f);
  678. return err;
  679. }
  680. static const struct bpf_prog_ops * const bpf_prog_types[] = {
  681. #define BPF_PROG_TYPE(_id, _name) \
  682. [_id] = & _name ## _prog_ops,
  683. #define BPF_MAP_TYPE(_id, _ops)
  684. #include <linux/bpf_types.h>
  685. #undef BPF_PROG_TYPE
  686. #undef BPF_MAP_TYPE
  687. };
  688. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  689. {
  690. if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
  691. return -EINVAL;
  692. if (!bpf_prog_is_dev_bound(prog->aux))
  693. prog->aux->ops = bpf_prog_types[type];
  694. else
  695. prog->aux->ops = &bpf_offload_prog_ops;
  696. prog->type = type;
  697. return 0;
  698. }
  699. /* drop refcnt on maps used by eBPF program and free auxilary data */
  700. static void free_used_maps(struct bpf_prog_aux *aux)
  701. {
  702. int i;
  703. for (i = 0; i < aux->used_map_cnt; i++)
  704. bpf_map_put(aux->used_maps[i]);
  705. kfree(aux->used_maps);
  706. }
  707. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  708. {
  709. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  710. unsigned long user_bufs;
  711. if (user) {
  712. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  713. if (user_bufs > memlock_limit) {
  714. atomic_long_sub(pages, &user->locked_vm);
  715. return -EPERM;
  716. }
  717. }
  718. return 0;
  719. }
  720. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  721. {
  722. if (user)
  723. atomic_long_sub(pages, &user->locked_vm);
  724. }
  725. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  726. {
  727. struct user_struct *user = get_current_user();
  728. int ret;
  729. ret = __bpf_prog_charge(user, prog->pages);
  730. if (ret) {
  731. free_uid(user);
  732. return ret;
  733. }
  734. prog->aux->user = user;
  735. return 0;
  736. }
  737. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  738. {
  739. struct user_struct *user = prog->aux->user;
  740. __bpf_prog_uncharge(user, prog->pages);
  741. free_uid(user);
  742. }
  743. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  744. {
  745. int id;
  746. spin_lock_bh(&prog_idr_lock);
  747. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  748. if (id > 0)
  749. prog->aux->id = id;
  750. spin_unlock_bh(&prog_idr_lock);
  751. /* id is in [1, INT_MAX) */
  752. if (WARN_ON_ONCE(!id))
  753. return -ENOSPC;
  754. return id > 0 ? 0 : id;
  755. }
  756. static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  757. {
  758. /* cBPF to eBPF migrations are currently not in the idr store. */
  759. if (!prog->aux->id)
  760. return;
  761. if (do_idr_lock)
  762. spin_lock_bh(&prog_idr_lock);
  763. else
  764. __acquire(&prog_idr_lock);
  765. idr_remove(&prog_idr, prog->aux->id);
  766. if (do_idr_lock)
  767. spin_unlock_bh(&prog_idr_lock);
  768. else
  769. __release(&prog_idr_lock);
  770. }
  771. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  772. {
  773. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  774. free_used_maps(aux);
  775. bpf_prog_uncharge_memlock(aux->prog);
  776. security_bpf_prog_free(aux);
  777. bpf_prog_free(aux->prog);
  778. }
  779. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  780. {
  781. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  782. trace_bpf_prog_put_rcu(prog);
  783. /* bpf_prog_free_id() must be called first */
  784. bpf_prog_free_id(prog, do_idr_lock);
  785. bpf_prog_kallsyms_del(prog);
  786. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  787. }
  788. }
  789. void bpf_prog_put(struct bpf_prog *prog)
  790. {
  791. __bpf_prog_put(prog, true);
  792. }
  793. EXPORT_SYMBOL_GPL(bpf_prog_put);
  794. static int bpf_prog_release(struct inode *inode, struct file *filp)
  795. {
  796. struct bpf_prog *prog = filp->private_data;
  797. bpf_prog_put(prog);
  798. return 0;
  799. }
  800. #ifdef CONFIG_PROC_FS
  801. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  802. {
  803. const struct bpf_prog *prog = filp->private_data;
  804. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  805. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  806. seq_printf(m,
  807. "prog_type:\t%u\n"
  808. "prog_jited:\t%u\n"
  809. "prog_tag:\t%s\n"
  810. "memlock:\t%llu\n",
  811. prog->type,
  812. prog->jited,
  813. prog_tag,
  814. prog->pages * 1ULL << PAGE_SHIFT);
  815. }
  816. #endif
  817. const struct file_operations bpf_prog_fops = {
  818. #ifdef CONFIG_PROC_FS
  819. .show_fdinfo = bpf_prog_show_fdinfo,
  820. #endif
  821. .release = bpf_prog_release,
  822. .read = bpf_dummy_read,
  823. .write = bpf_dummy_write,
  824. };
  825. int bpf_prog_new_fd(struct bpf_prog *prog)
  826. {
  827. int ret;
  828. ret = security_bpf_prog(prog);
  829. if (ret < 0)
  830. return ret;
  831. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  832. O_RDWR | O_CLOEXEC);
  833. }
  834. static struct bpf_prog *____bpf_prog_get(struct fd f)
  835. {
  836. if (!f.file)
  837. return ERR_PTR(-EBADF);
  838. if (f.file->f_op != &bpf_prog_fops) {
  839. fdput(f);
  840. return ERR_PTR(-EINVAL);
  841. }
  842. return f.file->private_data;
  843. }
  844. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  845. {
  846. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  847. atomic_sub(i, &prog->aux->refcnt);
  848. return ERR_PTR(-EBUSY);
  849. }
  850. return prog;
  851. }
  852. EXPORT_SYMBOL_GPL(bpf_prog_add);
  853. void bpf_prog_sub(struct bpf_prog *prog, int i)
  854. {
  855. /* Only to be used for undoing previous bpf_prog_add() in some
  856. * error path. We still know that another entity in our call
  857. * path holds a reference to the program, thus atomic_sub() can
  858. * be safely used in such cases!
  859. */
  860. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  861. }
  862. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  863. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  864. {
  865. return bpf_prog_add(prog, 1);
  866. }
  867. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  868. /* prog_idr_lock should have been held */
  869. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  870. {
  871. int refold;
  872. refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
  873. if (refold >= BPF_MAX_REFCNT) {
  874. __bpf_prog_put(prog, false);
  875. return ERR_PTR(-EBUSY);
  876. }
  877. if (!refold)
  878. return ERR_PTR(-ENOENT);
  879. return prog;
  880. }
  881. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  882. static bool bpf_prog_can_attach(struct bpf_prog *prog,
  883. enum bpf_prog_type *attach_type,
  884. struct net_device *netdev)
  885. {
  886. struct bpf_dev_offload *offload = prog->aux->offload;
  887. if (prog->type != *attach_type)
  888. return false;
  889. if (offload && offload->netdev != netdev)
  890. return false;
  891. return true;
  892. }
  893. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
  894. struct net_device *netdev)
  895. {
  896. struct fd f = fdget(ufd);
  897. struct bpf_prog *prog;
  898. prog = ____bpf_prog_get(f);
  899. if (IS_ERR(prog))
  900. return prog;
  901. if (attach_type && !bpf_prog_can_attach(prog, attach_type, netdev)) {
  902. prog = ERR_PTR(-EINVAL);
  903. goto out;
  904. }
  905. prog = bpf_prog_inc(prog);
  906. out:
  907. fdput(f);
  908. return prog;
  909. }
  910. struct bpf_prog *bpf_prog_get(u32 ufd)
  911. {
  912. return __bpf_prog_get(ufd, NULL, NULL);
  913. }
  914. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  915. {
  916. struct bpf_prog *prog = __bpf_prog_get(ufd, &type, NULL);
  917. if (!IS_ERR(prog))
  918. trace_bpf_prog_get_type(prog);
  919. return prog;
  920. }
  921. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  922. struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
  923. struct net_device *netdev)
  924. {
  925. struct bpf_prog *prog = __bpf_prog_get(ufd, &type, netdev);
  926. if (!IS_ERR(prog))
  927. trace_bpf_prog_get_type(prog);
  928. return prog;
  929. }
  930. EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
  931. /* last field in 'union bpf_attr' used by this command */
  932. #define BPF_PROG_LOAD_LAST_FIELD prog_target_ifindex
  933. static int bpf_prog_load(union bpf_attr *attr)
  934. {
  935. enum bpf_prog_type type = attr->prog_type;
  936. struct bpf_prog *prog;
  937. int err;
  938. char license[128];
  939. bool is_gpl;
  940. if (CHECK_ATTR(BPF_PROG_LOAD))
  941. return -EINVAL;
  942. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  943. return -EINVAL;
  944. /* copy eBPF program license from user space */
  945. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  946. sizeof(license) - 1) < 0)
  947. return -EFAULT;
  948. license[sizeof(license) - 1] = 0;
  949. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  950. is_gpl = license_is_gpl_compatible(license);
  951. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  952. return -E2BIG;
  953. if (type == BPF_PROG_TYPE_KPROBE &&
  954. attr->kern_version != LINUX_VERSION_CODE)
  955. return -EINVAL;
  956. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  957. type != BPF_PROG_TYPE_CGROUP_SKB &&
  958. !capable(CAP_SYS_ADMIN))
  959. return -EPERM;
  960. /* plain bpf_prog allocation */
  961. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  962. if (!prog)
  963. return -ENOMEM;
  964. err = security_bpf_prog_alloc(prog->aux);
  965. if (err)
  966. goto free_prog_nouncharge;
  967. err = bpf_prog_charge_memlock(prog);
  968. if (err)
  969. goto free_prog_sec;
  970. prog->len = attr->insn_cnt;
  971. err = -EFAULT;
  972. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  973. bpf_prog_insn_size(prog)) != 0)
  974. goto free_prog;
  975. prog->orig_prog = NULL;
  976. prog->jited = 0;
  977. atomic_set(&prog->aux->refcnt, 1);
  978. prog->gpl_compatible = is_gpl ? 1 : 0;
  979. if (attr->prog_target_ifindex) {
  980. err = bpf_prog_offload_init(prog, attr);
  981. if (err)
  982. goto free_prog;
  983. }
  984. /* find program type: socket_filter vs tracing_filter */
  985. err = find_prog_type(type, prog);
  986. if (err < 0)
  987. goto free_prog;
  988. prog->aux->load_time = ktime_get_boot_ns();
  989. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
  990. if (err)
  991. goto free_prog;
  992. /* run eBPF verifier */
  993. err = bpf_check(&prog, attr);
  994. if (err < 0)
  995. goto free_used_maps;
  996. /* eBPF program is ready to be JITed */
  997. prog = bpf_prog_select_runtime(prog, &err);
  998. if (err < 0)
  999. goto free_used_maps;
  1000. err = bpf_prog_alloc_id(prog);
  1001. if (err)
  1002. goto free_used_maps;
  1003. err = bpf_prog_new_fd(prog);
  1004. if (err < 0) {
  1005. /* failed to allocate fd.
  1006. * bpf_prog_put() is needed because the above
  1007. * bpf_prog_alloc_id() has published the prog
  1008. * to the userspace and the userspace may
  1009. * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
  1010. */
  1011. bpf_prog_put(prog);
  1012. return err;
  1013. }
  1014. bpf_prog_kallsyms_add(prog);
  1015. trace_bpf_prog_load(prog, err);
  1016. return err;
  1017. free_used_maps:
  1018. free_used_maps(prog->aux);
  1019. free_prog:
  1020. bpf_prog_uncharge_memlock(prog);
  1021. free_prog_sec:
  1022. security_bpf_prog_free(prog->aux);
  1023. free_prog_nouncharge:
  1024. bpf_prog_free(prog);
  1025. return err;
  1026. }
  1027. #define BPF_OBJ_LAST_FIELD file_flags
  1028. static int bpf_obj_pin(const union bpf_attr *attr)
  1029. {
  1030. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  1031. return -EINVAL;
  1032. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  1033. }
  1034. static int bpf_obj_get(const union bpf_attr *attr)
  1035. {
  1036. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  1037. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  1038. return -EINVAL;
  1039. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  1040. attr->file_flags);
  1041. }
  1042. #ifdef CONFIG_CGROUP_BPF
  1043. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  1044. static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
  1045. {
  1046. struct bpf_prog *prog = NULL;
  1047. int ufd = attr->target_fd;
  1048. struct bpf_map *map;
  1049. struct fd f;
  1050. int err;
  1051. f = fdget(ufd);
  1052. map = __bpf_map_get(f);
  1053. if (IS_ERR(map))
  1054. return PTR_ERR(map);
  1055. if (attach) {
  1056. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  1057. BPF_PROG_TYPE_SK_SKB);
  1058. if (IS_ERR(prog)) {
  1059. fdput(f);
  1060. return PTR_ERR(prog);
  1061. }
  1062. }
  1063. err = sock_map_prog(map, prog, attr->attach_type);
  1064. if (err) {
  1065. fdput(f);
  1066. if (prog)
  1067. bpf_prog_put(prog);
  1068. return err;
  1069. }
  1070. fdput(f);
  1071. return 0;
  1072. }
  1073. #define BPF_F_ATTACH_MASK \
  1074. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
  1075. static int bpf_prog_attach(const union bpf_attr *attr)
  1076. {
  1077. enum bpf_prog_type ptype;
  1078. struct bpf_prog *prog;
  1079. struct cgroup *cgrp;
  1080. int ret;
  1081. if (!capable(CAP_NET_ADMIN))
  1082. return -EPERM;
  1083. if (CHECK_ATTR(BPF_PROG_ATTACH))
  1084. return -EINVAL;
  1085. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  1086. return -EINVAL;
  1087. switch (attr->attach_type) {
  1088. case BPF_CGROUP_INET_INGRESS:
  1089. case BPF_CGROUP_INET_EGRESS:
  1090. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1091. break;
  1092. case BPF_CGROUP_INET_SOCK_CREATE:
  1093. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1094. break;
  1095. case BPF_CGROUP_SOCK_OPS:
  1096. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1097. break;
  1098. case BPF_SK_SKB_STREAM_PARSER:
  1099. case BPF_SK_SKB_STREAM_VERDICT:
  1100. return sockmap_get_from_fd(attr, true);
  1101. default:
  1102. return -EINVAL;
  1103. }
  1104. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1105. if (IS_ERR(prog))
  1106. return PTR_ERR(prog);
  1107. cgrp = cgroup_get_from_fd(attr->target_fd);
  1108. if (IS_ERR(cgrp)) {
  1109. bpf_prog_put(prog);
  1110. return PTR_ERR(cgrp);
  1111. }
  1112. ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
  1113. attr->attach_flags);
  1114. if (ret)
  1115. bpf_prog_put(prog);
  1116. cgroup_put(cgrp);
  1117. return ret;
  1118. }
  1119. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1120. static int bpf_prog_detach(const union bpf_attr *attr)
  1121. {
  1122. enum bpf_prog_type ptype;
  1123. struct bpf_prog *prog;
  1124. struct cgroup *cgrp;
  1125. int ret;
  1126. if (!capable(CAP_NET_ADMIN))
  1127. return -EPERM;
  1128. if (CHECK_ATTR(BPF_PROG_DETACH))
  1129. return -EINVAL;
  1130. switch (attr->attach_type) {
  1131. case BPF_CGROUP_INET_INGRESS:
  1132. case BPF_CGROUP_INET_EGRESS:
  1133. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  1134. break;
  1135. case BPF_CGROUP_INET_SOCK_CREATE:
  1136. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  1137. break;
  1138. case BPF_CGROUP_SOCK_OPS:
  1139. ptype = BPF_PROG_TYPE_SOCK_OPS;
  1140. break;
  1141. case BPF_SK_SKB_STREAM_PARSER:
  1142. case BPF_SK_SKB_STREAM_VERDICT:
  1143. return sockmap_get_from_fd(attr, false);
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. cgrp = cgroup_get_from_fd(attr->target_fd);
  1148. if (IS_ERR(cgrp))
  1149. return PTR_ERR(cgrp);
  1150. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  1151. if (IS_ERR(prog))
  1152. prog = NULL;
  1153. ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
  1154. if (prog)
  1155. bpf_prog_put(prog);
  1156. cgroup_put(cgrp);
  1157. return ret;
  1158. }
  1159. #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
  1160. static int bpf_prog_query(const union bpf_attr *attr,
  1161. union bpf_attr __user *uattr)
  1162. {
  1163. struct cgroup *cgrp;
  1164. int ret;
  1165. if (!capable(CAP_NET_ADMIN))
  1166. return -EPERM;
  1167. if (CHECK_ATTR(BPF_PROG_QUERY))
  1168. return -EINVAL;
  1169. if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
  1170. return -EINVAL;
  1171. switch (attr->query.attach_type) {
  1172. case BPF_CGROUP_INET_INGRESS:
  1173. case BPF_CGROUP_INET_EGRESS:
  1174. case BPF_CGROUP_INET_SOCK_CREATE:
  1175. case BPF_CGROUP_SOCK_OPS:
  1176. break;
  1177. default:
  1178. return -EINVAL;
  1179. }
  1180. cgrp = cgroup_get_from_fd(attr->query.target_fd);
  1181. if (IS_ERR(cgrp))
  1182. return PTR_ERR(cgrp);
  1183. ret = cgroup_bpf_query(cgrp, attr, uattr);
  1184. cgroup_put(cgrp);
  1185. return ret;
  1186. }
  1187. #endif /* CONFIG_CGROUP_BPF */
  1188. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1189. static int bpf_prog_test_run(const union bpf_attr *attr,
  1190. union bpf_attr __user *uattr)
  1191. {
  1192. struct bpf_prog *prog;
  1193. int ret = -ENOTSUPP;
  1194. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1195. return -EINVAL;
  1196. prog = bpf_prog_get(attr->test.prog_fd);
  1197. if (IS_ERR(prog))
  1198. return PTR_ERR(prog);
  1199. if (prog->aux->ops->test_run)
  1200. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1201. bpf_prog_put(prog);
  1202. return ret;
  1203. }
  1204. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1205. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1206. union bpf_attr __user *uattr,
  1207. struct idr *idr,
  1208. spinlock_t *lock)
  1209. {
  1210. u32 next_id = attr->start_id;
  1211. int err = 0;
  1212. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1213. return -EINVAL;
  1214. if (!capable(CAP_SYS_ADMIN))
  1215. return -EPERM;
  1216. next_id++;
  1217. spin_lock_bh(lock);
  1218. if (!idr_get_next(idr, &next_id))
  1219. err = -ENOENT;
  1220. spin_unlock_bh(lock);
  1221. if (!err)
  1222. err = put_user(next_id, &uattr->next_id);
  1223. return err;
  1224. }
  1225. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1226. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1227. {
  1228. struct bpf_prog *prog;
  1229. u32 id = attr->prog_id;
  1230. int fd;
  1231. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1232. return -EINVAL;
  1233. if (!capable(CAP_SYS_ADMIN))
  1234. return -EPERM;
  1235. spin_lock_bh(&prog_idr_lock);
  1236. prog = idr_find(&prog_idr, id);
  1237. if (prog)
  1238. prog = bpf_prog_inc_not_zero(prog);
  1239. else
  1240. prog = ERR_PTR(-ENOENT);
  1241. spin_unlock_bh(&prog_idr_lock);
  1242. if (IS_ERR(prog))
  1243. return PTR_ERR(prog);
  1244. fd = bpf_prog_new_fd(prog);
  1245. if (fd < 0)
  1246. bpf_prog_put(prog);
  1247. return fd;
  1248. }
  1249. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  1250. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1251. {
  1252. struct bpf_map *map;
  1253. u32 id = attr->map_id;
  1254. int f_flags;
  1255. int fd;
  1256. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  1257. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  1258. return -EINVAL;
  1259. if (!capable(CAP_SYS_ADMIN))
  1260. return -EPERM;
  1261. f_flags = bpf_get_file_flag(attr->open_flags);
  1262. if (f_flags < 0)
  1263. return f_flags;
  1264. spin_lock_bh(&map_idr_lock);
  1265. map = idr_find(&map_idr, id);
  1266. if (map)
  1267. map = bpf_map_inc_not_zero(map, true);
  1268. else
  1269. map = ERR_PTR(-ENOENT);
  1270. spin_unlock_bh(&map_idr_lock);
  1271. if (IS_ERR(map))
  1272. return PTR_ERR(map);
  1273. fd = bpf_map_new_fd(map, f_flags);
  1274. if (fd < 0)
  1275. bpf_map_put(map);
  1276. return fd;
  1277. }
  1278. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1279. const union bpf_attr *attr,
  1280. union bpf_attr __user *uattr)
  1281. {
  1282. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1283. struct bpf_prog_info info = {};
  1284. u32 info_len = attr->info.info_len;
  1285. char __user *uinsns;
  1286. u32 ulen;
  1287. int err;
  1288. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1289. if (err)
  1290. return err;
  1291. info_len = min_t(u32, sizeof(info), info_len);
  1292. if (copy_from_user(&info, uinfo, info_len))
  1293. return -EFAULT;
  1294. info.type = prog->type;
  1295. info.id = prog->aux->id;
  1296. info.load_time = prog->aux->load_time;
  1297. info.created_by_uid = from_kuid_munged(current_user_ns(),
  1298. prog->aux->user->uid);
  1299. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1300. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  1301. ulen = info.nr_map_ids;
  1302. info.nr_map_ids = prog->aux->used_map_cnt;
  1303. ulen = min_t(u32, info.nr_map_ids, ulen);
  1304. if (ulen) {
  1305. u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
  1306. u32 i;
  1307. for (i = 0; i < ulen; i++)
  1308. if (put_user(prog->aux->used_maps[i]->id,
  1309. &user_map_ids[i]))
  1310. return -EFAULT;
  1311. }
  1312. if (!capable(CAP_SYS_ADMIN)) {
  1313. info.jited_prog_len = 0;
  1314. info.xlated_prog_len = 0;
  1315. goto done;
  1316. }
  1317. ulen = info.jited_prog_len;
  1318. info.jited_prog_len = prog->jited_len;
  1319. if (info.jited_prog_len && ulen) {
  1320. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1321. ulen = min_t(u32, info.jited_prog_len, ulen);
  1322. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1323. return -EFAULT;
  1324. }
  1325. ulen = info.xlated_prog_len;
  1326. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1327. if (info.xlated_prog_len && ulen) {
  1328. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1329. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1330. if (copy_to_user(uinsns, prog->insnsi, ulen))
  1331. return -EFAULT;
  1332. }
  1333. if (bpf_prog_is_dev_bound(prog->aux)) {
  1334. info.status |= BPF_PROG_STATUS_DEV_BOUND;
  1335. info.ifindex = bpf_prog_offload_ifindex(prog);
  1336. }
  1337. done:
  1338. if (copy_to_user(uinfo, &info, info_len) ||
  1339. put_user(info_len, &uattr->info.info_len))
  1340. return -EFAULT;
  1341. return 0;
  1342. }
  1343. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1344. const union bpf_attr *attr,
  1345. union bpf_attr __user *uattr)
  1346. {
  1347. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1348. struct bpf_map_info info = {};
  1349. u32 info_len = attr->info.info_len;
  1350. int err;
  1351. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1352. if (err)
  1353. return err;
  1354. info_len = min_t(u32, sizeof(info), info_len);
  1355. info.type = map->map_type;
  1356. info.id = map->id;
  1357. info.key_size = map->key_size;
  1358. info.value_size = map->value_size;
  1359. info.max_entries = map->max_entries;
  1360. info.map_flags = map->map_flags;
  1361. memcpy(info.name, map->name, sizeof(map->name));
  1362. if (copy_to_user(uinfo, &info, info_len) ||
  1363. put_user(info_len, &uattr->info.info_len))
  1364. return -EFAULT;
  1365. return 0;
  1366. }
  1367. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1368. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1369. union bpf_attr __user *uattr)
  1370. {
  1371. int ufd = attr->info.bpf_fd;
  1372. struct fd f;
  1373. int err;
  1374. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1375. return -EINVAL;
  1376. f = fdget(ufd);
  1377. if (!f.file)
  1378. return -EBADFD;
  1379. if (f.file->f_op == &bpf_prog_fops)
  1380. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1381. uattr);
  1382. else if (f.file->f_op == &bpf_map_fops)
  1383. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1384. uattr);
  1385. else
  1386. err = -EINVAL;
  1387. fdput(f);
  1388. return err;
  1389. }
  1390. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1391. {
  1392. union bpf_attr attr = {};
  1393. int err;
  1394. if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  1395. return -EPERM;
  1396. err = check_uarg_tail_zero(uattr, sizeof(attr), size);
  1397. if (err)
  1398. return err;
  1399. size = min_t(u32, size, sizeof(attr));
  1400. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  1401. if (copy_from_user(&attr, uattr, size) != 0)
  1402. return -EFAULT;
  1403. err = security_bpf(cmd, &attr, size);
  1404. if (err < 0)
  1405. return err;
  1406. switch (cmd) {
  1407. case BPF_MAP_CREATE:
  1408. err = map_create(&attr);
  1409. break;
  1410. case BPF_MAP_LOOKUP_ELEM:
  1411. err = map_lookup_elem(&attr);
  1412. break;
  1413. case BPF_MAP_UPDATE_ELEM:
  1414. err = map_update_elem(&attr);
  1415. break;
  1416. case BPF_MAP_DELETE_ELEM:
  1417. err = map_delete_elem(&attr);
  1418. break;
  1419. case BPF_MAP_GET_NEXT_KEY:
  1420. err = map_get_next_key(&attr);
  1421. break;
  1422. case BPF_PROG_LOAD:
  1423. err = bpf_prog_load(&attr);
  1424. break;
  1425. case BPF_OBJ_PIN:
  1426. err = bpf_obj_pin(&attr);
  1427. break;
  1428. case BPF_OBJ_GET:
  1429. err = bpf_obj_get(&attr);
  1430. break;
  1431. #ifdef CONFIG_CGROUP_BPF
  1432. case BPF_PROG_ATTACH:
  1433. err = bpf_prog_attach(&attr);
  1434. break;
  1435. case BPF_PROG_DETACH:
  1436. err = bpf_prog_detach(&attr);
  1437. break;
  1438. case BPF_PROG_QUERY:
  1439. err = bpf_prog_query(&attr, uattr);
  1440. break;
  1441. #endif
  1442. case BPF_PROG_TEST_RUN:
  1443. err = bpf_prog_test_run(&attr, uattr);
  1444. break;
  1445. case BPF_PROG_GET_NEXT_ID:
  1446. err = bpf_obj_get_next_id(&attr, uattr,
  1447. &prog_idr, &prog_idr_lock);
  1448. break;
  1449. case BPF_MAP_GET_NEXT_ID:
  1450. err = bpf_obj_get_next_id(&attr, uattr,
  1451. &map_idr, &map_idr_lock);
  1452. break;
  1453. case BPF_PROG_GET_FD_BY_ID:
  1454. err = bpf_prog_get_fd_by_id(&attr);
  1455. break;
  1456. case BPF_MAP_GET_FD_BY_ID:
  1457. err = bpf_map_get_fd_by_id(&attr);
  1458. break;
  1459. case BPF_OBJ_GET_INFO_BY_FD:
  1460. err = bpf_obj_get_info_by_fd(&attr, uattr);
  1461. break;
  1462. default:
  1463. err = -EINVAL;
  1464. break;
  1465. }
  1466. return err;
  1467. }