syscall.c 36 KB

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