syscall.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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. /* Copy all isalnum() and '_' char */
  274. while (src < end && *src) {
  275. if (!isalnum(*src) && *src != '_')
  276. return -EINVAL;
  277. *dst++ = *src++;
  278. }
  279. /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
  280. if (src == end)
  281. return -EINVAL;
  282. /* '\0' terminates dst */
  283. *dst = 0;
  284. return 0;
  285. }
  286. #define BPF_MAP_CREATE_LAST_FIELD map_name
  287. /* called via syscall */
  288. static int map_create(union bpf_attr *attr)
  289. {
  290. int numa_node = bpf_map_attr_numa_node(attr);
  291. struct bpf_map *map;
  292. int err;
  293. err = CHECK_ATTR(BPF_MAP_CREATE);
  294. if (err)
  295. return -EINVAL;
  296. if (numa_node != NUMA_NO_NODE &&
  297. ((unsigned int)numa_node >= nr_node_ids ||
  298. !node_online(numa_node)))
  299. return -EINVAL;
  300. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  301. map = find_and_alloc_map(attr);
  302. if (IS_ERR(map))
  303. return PTR_ERR(map);
  304. err = bpf_obj_name_cpy(map->name, attr->map_name);
  305. if (err)
  306. goto free_map_nouncharge;
  307. atomic_set(&map->refcnt, 1);
  308. atomic_set(&map->usercnt, 1);
  309. err = bpf_map_charge_memlock(map);
  310. if (err)
  311. goto free_map_nouncharge;
  312. err = bpf_map_alloc_id(map);
  313. if (err)
  314. goto free_map;
  315. err = bpf_map_new_fd(map);
  316. if (err < 0) {
  317. /* failed to allocate fd.
  318. * bpf_map_put() is needed because the above
  319. * bpf_map_alloc_id() has published the map
  320. * to the userspace and the userspace may
  321. * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
  322. */
  323. bpf_map_put(map);
  324. return err;
  325. }
  326. trace_bpf_map_create(map, err);
  327. return err;
  328. free_map:
  329. bpf_map_uncharge_memlock(map);
  330. free_map_nouncharge:
  331. map->ops->map_free(map);
  332. return err;
  333. }
  334. /* if error is returned, fd is released.
  335. * On success caller should complete fd access with matching fdput()
  336. */
  337. struct bpf_map *__bpf_map_get(struct fd f)
  338. {
  339. if (!f.file)
  340. return ERR_PTR(-EBADF);
  341. if (f.file->f_op != &bpf_map_fops) {
  342. fdput(f);
  343. return ERR_PTR(-EINVAL);
  344. }
  345. return f.file->private_data;
  346. }
  347. /* prog's and map's refcnt limit */
  348. #define BPF_MAX_REFCNT 32768
  349. struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
  350. {
  351. if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
  352. atomic_dec(&map->refcnt);
  353. return ERR_PTR(-EBUSY);
  354. }
  355. if (uref)
  356. atomic_inc(&map->usercnt);
  357. return map;
  358. }
  359. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  360. {
  361. struct fd f = fdget(ufd);
  362. struct bpf_map *map;
  363. map = __bpf_map_get(f);
  364. if (IS_ERR(map))
  365. return map;
  366. map = bpf_map_inc(map, true);
  367. fdput(f);
  368. return map;
  369. }
  370. /* map_idr_lock should have been held */
  371. static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
  372. bool uref)
  373. {
  374. int refold;
  375. refold = __atomic_add_unless(&map->refcnt, 1, 0);
  376. if (refold >= BPF_MAX_REFCNT) {
  377. __bpf_map_put(map, false);
  378. return ERR_PTR(-EBUSY);
  379. }
  380. if (!refold)
  381. return ERR_PTR(-ENOENT);
  382. if (uref)
  383. atomic_inc(&map->usercnt);
  384. return map;
  385. }
  386. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  387. {
  388. return -ENOTSUPP;
  389. }
  390. /* last field in 'union bpf_attr' used by this command */
  391. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
  392. static int map_lookup_elem(union bpf_attr *attr)
  393. {
  394. void __user *ukey = u64_to_user_ptr(attr->key);
  395. void __user *uvalue = u64_to_user_ptr(attr->value);
  396. int ufd = attr->map_fd;
  397. struct bpf_map *map;
  398. void *key, *value, *ptr;
  399. u32 value_size;
  400. struct fd f;
  401. int err;
  402. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  403. return -EINVAL;
  404. f = fdget(ufd);
  405. map = __bpf_map_get(f);
  406. if (IS_ERR(map))
  407. return PTR_ERR(map);
  408. key = memdup_user(ukey, map->key_size);
  409. if (IS_ERR(key)) {
  410. err = PTR_ERR(key);
  411. goto err_put;
  412. }
  413. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  414. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  415. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  416. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  417. else if (IS_FD_MAP(map))
  418. value_size = sizeof(u32);
  419. else
  420. value_size = map->value_size;
  421. err = -ENOMEM;
  422. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  423. if (!value)
  424. goto free_key;
  425. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  426. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  427. err = bpf_percpu_hash_copy(map, key, value);
  428. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  429. err = bpf_percpu_array_copy(map, key, value);
  430. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  431. err = bpf_stackmap_copy(map, key, value);
  432. } else if (IS_FD_ARRAY(map)) {
  433. err = bpf_fd_array_map_lookup_elem(map, key, value);
  434. } else if (IS_FD_HASH(map)) {
  435. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  436. } else {
  437. rcu_read_lock();
  438. ptr = map->ops->map_lookup_elem(map, key);
  439. if (ptr)
  440. memcpy(value, ptr, value_size);
  441. rcu_read_unlock();
  442. err = ptr ? 0 : -ENOENT;
  443. }
  444. if (err)
  445. goto free_value;
  446. err = -EFAULT;
  447. if (copy_to_user(uvalue, value, value_size) != 0)
  448. goto free_value;
  449. trace_bpf_map_lookup_elem(map, ufd, key, value);
  450. err = 0;
  451. free_value:
  452. kfree(value);
  453. free_key:
  454. kfree(key);
  455. err_put:
  456. fdput(f);
  457. return err;
  458. }
  459. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  460. static int map_update_elem(union bpf_attr *attr)
  461. {
  462. void __user *ukey = u64_to_user_ptr(attr->key);
  463. void __user *uvalue = u64_to_user_ptr(attr->value);
  464. int ufd = attr->map_fd;
  465. struct bpf_map *map;
  466. void *key, *value;
  467. u32 value_size;
  468. struct fd f;
  469. int err;
  470. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  471. return -EINVAL;
  472. f = fdget(ufd);
  473. map = __bpf_map_get(f);
  474. if (IS_ERR(map))
  475. return PTR_ERR(map);
  476. key = memdup_user(ukey, map->key_size);
  477. if (IS_ERR(key)) {
  478. err = PTR_ERR(key);
  479. goto err_put;
  480. }
  481. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  482. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  483. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  484. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  485. else
  486. value_size = map->value_size;
  487. err = -ENOMEM;
  488. value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
  489. if (!value)
  490. goto free_key;
  491. err = -EFAULT;
  492. if (copy_from_user(value, uvalue, value_size) != 0)
  493. goto free_value;
  494. /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
  495. * inside bpf map update or delete otherwise deadlocks are possible
  496. */
  497. preempt_disable();
  498. __this_cpu_inc(bpf_prog_active);
  499. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  500. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  501. err = bpf_percpu_hash_update(map, key, value, attr->flags);
  502. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  503. err = bpf_percpu_array_update(map, key, value, attr->flags);
  504. } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
  505. map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
  506. map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
  507. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
  508. rcu_read_lock();
  509. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  510. attr->flags);
  511. rcu_read_unlock();
  512. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  513. rcu_read_lock();
  514. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  515. attr->flags);
  516. rcu_read_unlock();
  517. } else {
  518. rcu_read_lock();
  519. err = map->ops->map_update_elem(map, key, value, attr->flags);
  520. rcu_read_unlock();
  521. }
  522. __this_cpu_dec(bpf_prog_active);
  523. preempt_enable();
  524. if (!err)
  525. trace_bpf_map_update_elem(map, ufd, key, value);
  526. free_value:
  527. kfree(value);
  528. free_key:
  529. kfree(key);
  530. err_put:
  531. fdput(f);
  532. return err;
  533. }
  534. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  535. static int map_delete_elem(union bpf_attr *attr)
  536. {
  537. void __user *ukey = u64_to_user_ptr(attr->key);
  538. int ufd = attr->map_fd;
  539. struct bpf_map *map;
  540. struct fd f;
  541. void *key;
  542. int err;
  543. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  544. return -EINVAL;
  545. f = fdget(ufd);
  546. map = __bpf_map_get(f);
  547. if (IS_ERR(map))
  548. return PTR_ERR(map);
  549. key = memdup_user(ukey, map->key_size);
  550. if (IS_ERR(key)) {
  551. err = PTR_ERR(key);
  552. goto err_put;
  553. }
  554. preempt_disable();
  555. __this_cpu_inc(bpf_prog_active);
  556. rcu_read_lock();
  557. err = map->ops->map_delete_elem(map, key);
  558. rcu_read_unlock();
  559. __this_cpu_dec(bpf_prog_active);
  560. preempt_enable();
  561. if (!err)
  562. trace_bpf_map_delete_elem(map, ufd, key);
  563. kfree(key);
  564. err_put:
  565. fdput(f);
  566. return err;
  567. }
  568. /* last field in 'union bpf_attr' used by this command */
  569. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  570. static int map_get_next_key(union bpf_attr *attr)
  571. {
  572. void __user *ukey = u64_to_user_ptr(attr->key);
  573. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  574. int ufd = attr->map_fd;
  575. struct bpf_map *map;
  576. void *key, *next_key;
  577. struct fd f;
  578. int err;
  579. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  580. return -EINVAL;
  581. f = fdget(ufd);
  582. map = __bpf_map_get(f);
  583. if (IS_ERR(map))
  584. return PTR_ERR(map);
  585. if (ukey) {
  586. key = memdup_user(ukey, map->key_size);
  587. if (IS_ERR(key)) {
  588. err = PTR_ERR(key);
  589. goto err_put;
  590. }
  591. } else {
  592. key = NULL;
  593. }
  594. err = -ENOMEM;
  595. next_key = kmalloc(map->key_size, GFP_USER);
  596. if (!next_key)
  597. goto free_key;
  598. rcu_read_lock();
  599. err = map->ops->map_get_next_key(map, key, next_key);
  600. rcu_read_unlock();
  601. if (err)
  602. goto free_next_key;
  603. err = -EFAULT;
  604. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  605. goto free_next_key;
  606. trace_bpf_map_next_key(map, ufd, key, next_key);
  607. err = 0;
  608. free_next_key:
  609. kfree(next_key);
  610. free_key:
  611. kfree(key);
  612. err_put:
  613. fdput(f);
  614. return err;
  615. }
  616. static const struct bpf_verifier_ops * const bpf_prog_types[] = {
  617. #define BPF_PROG_TYPE(_id, _ops) \
  618. [_id] = &_ops,
  619. #define BPF_MAP_TYPE(_id, _ops)
  620. #include <linux/bpf_types.h>
  621. #undef BPF_PROG_TYPE
  622. #undef BPF_MAP_TYPE
  623. };
  624. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  625. {
  626. if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
  627. return -EINVAL;
  628. prog->aux->ops = bpf_prog_types[type];
  629. prog->type = type;
  630. return 0;
  631. }
  632. /* drop refcnt on maps used by eBPF program and free auxilary data */
  633. static void free_used_maps(struct bpf_prog_aux *aux)
  634. {
  635. int i;
  636. for (i = 0; i < aux->used_map_cnt; i++)
  637. bpf_map_put(aux->used_maps[i]);
  638. kfree(aux->used_maps);
  639. }
  640. int __bpf_prog_charge(struct user_struct *user, u32 pages)
  641. {
  642. unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  643. unsigned long user_bufs;
  644. if (user) {
  645. user_bufs = atomic_long_add_return(pages, &user->locked_vm);
  646. if (user_bufs > memlock_limit) {
  647. atomic_long_sub(pages, &user->locked_vm);
  648. return -EPERM;
  649. }
  650. }
  651. return 0;
  652. }
  653. void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  654. {
  655. if (user)
  656. atomic_long_sub(pages, &user->locked_vm);
  657. }
  658. static int bpf_prog_charge_memlock(struct bpf_prog *prog)
  659. {
  660. struct user_struct *user = get_current_user();
  661. int ret;
  662. ret = __bpf_prog_charge(user, prog->pages);
  663. if (ret) {
  664. free_uid(user);
  665. return ret;
  666. }
  667. prog->aux->user = user;
  668. return 0;
  669. }
  670. static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
  671. {
  672. struct user_struct *user = prog->aux->user;
  673. __bpf_prog_uncharge(user, prog->pages);
  674. free_uid(user);
  675. }
  676. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  677. {
  678. int id;
  679. spin_lock_bh(&prog_idr_lock);
  680. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  681. if (id > 0)
  682. prog->aux->id = id;
  683. spin_unlock_bh(&prog_idr_lock);
  684. /* id is in [1, INT_MAX) */
  685. if (WARN_ON_ONCE(!id))
  686. return -ENOSPC;
  687. return id > 0 ? 0 : id;
  688. }
  689. static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  690. {
  691. /* cBPF to eBPF migrations are currently not in the idr store. */
  692. if (!prog->aux->id)
  693. return;
  694. if (do_idr_lock)
  695. spin_lock_bh(&prog_idr_lock);
  696. else
  697. __acquire(&prog_idr_lock);
  698. idr_remove(&prog_idr, prog->aux->id);
  699. if (do_idr_lock)
  700. spin_unlock_bh(&prog_idr_lock);
  701. else
  702. __release(&prog_idr_lock);
  703. }
  704. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  705. {
  706. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  707. free_used_maps(aux);
  708. bpf_prog_uncharge_memlock(aux->prog);
  709. bpf_prog_free(aux->prog);
  710. }
  711. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  712. {
  713. if (atomic_dec_and_test(&prog->aux->refcnt)) {
  714. trace_bpf_prog_put_rcu(prog);
  715. /* bpf_prog_free_id() must be called first */
  716. bpf_prog_free_id(prog, do_idr_lock);
  717. bpf_prog_kallsyms_del(prog);
  718. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  719. }
  720. }
  721. void bpf_prog_put(struct bpf_prog *prog)
  722. {
  723. __bpf_prog_put(prog, true);
  724. }
  725. EXPORT_SYMBOL_GPL(bpf_prog_put);
  726. static int bpf_prog_release(struct inode *inode, struct file *filp)
  727. {
  728. struct bpf_prog *prog = filp->private_data;
  729. bpf_prog_put(prog);
  730. return 0;
  731. }
  732. #ifdef CONFIG_PROC_FS
  733. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  734. {
  735. const struct bpf_prog *prog = filp->private_data;
  736. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  737. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  738. seq_printf(m,
  739. "prog_type:\t%u\n"
  740. "prog_jited:\t%u\n"
  741. "prog_tag:\t%s\n"
  742. "memlock:\t%llu\n",
  743. prog->type,
  744. prog->jited,
  745. prog_tag,
  746. prog->pages * 1ULL << PAGE_SHIFT);
  747. }
  748. #endif
  749. static const struct file_operations bpf_prog_fops = {
  750. #ifdef CONFIG_PROC_FS
  751. .show_fdinfo = bpf_prog_show_fdinfo,
  752. #endif
  753. .release = bpf_prog_release,
  754. };
  755. int bpf_prog_new_fd(struct bpf_prog *prog)
  756. {
  757. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  758. O_RDWR | O_CLOEXEC);
  759. }
  760. static struct bpf_prog *____bpf_prog_get(struct fd f)
  761. {
  762. if (!f.file)
  763. return ERR_PTR(-EBADF);
  764. if (f.file->f_op != &bpf_prog_fops) {
  765. fdput(f);
  766. return ERR_PTR(-EINVAL);
  767. }
  768. return f.file->private_data;
  769. }
  770. struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
  771. {
  772. if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
  773. atomic_sub(i, &prog->aux->refcnt);
  774. return ERR_PTR(-EBUSY);
  775. }
  776. return prog;
  777. }
  778. EXPORT_SYMBOL_GPL(bpf_prog_add);
  779. void bpf_prog_sub(struct bpf_prog *prog, int i)
  780. {
  781. /* Only to be used for undoing previous bpf_prog_add() in some
  782. * error path. We still know that another entity in our call
  783. * path holds a reference to the program, thus atomic_sub() can
  784. * be safely used in such cases!
  785. */
  786. WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
  787. }
  788. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  789. struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
  790. {
  791. return bpf_prog_add(prog, 1);
  792. }
  793. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  794. /* prog_idr_lock should have been held */
  795. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  796. {
  797. int refold;
  798. refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
  799. if (refold >= BPF_MAX_REFCNT) {
  800. __bpf_prog_put(prog, false);
  801. return ERR_PTR(-EBUSY);
  802. }
  803. if (!refold)
  804. return ERR_PTR(-ENOENT);
  805. return prog;
  806. }
  807. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  808. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
  809. {
  810. struct fd f = fdget(ufd);
  811. struct bpf_prog *prog;
  812. prog = ____bpf_prog_get(f);
  813. if (IS_ERR(prog))
  814. return prog;
  815. if (type && prog->type != *type) {
  816. prog = ERR_PTR(-EINVAL);
  817. goto out;
  818. }
  819. prog = bpf_prog_inc(prog);
  820. out:
  821. fdput(f);
  822. return prog;
  823. }
  824. struct bpf_prog *bpf_prog_get(u32 ufd)
  825. {
  826. return __bpf_prog_get(ufd, NULL);
  827. }
  828. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
  829. {
  830. struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
  831. if (!IS_ERR(prog))
  832. trace_bpf_prog_get_type(prog);
  833. return prog;
  834. }
  835. EXPORT_SYMBOL_GPL(bpf_prog_get_type);
  836. /* last field in 'union bpf_attr' used by this command */
  837. #define BPF_PROG_LOAD_LAST_FIELD prog_name
  838. static int bpf_prog_load(union bpf_attr *attr)
  839. {
  840. enum bpf_prog_type type = attr->prog_type;
  841. struct bpf_prog *prog;
  842. int err;
  843. char license[128];
  844. bool is_gpl;
  845. if (CHECK_ATTR(BPF_PROG_LOAD))
  846. return -EINVAL;
  847. if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
  848. return -EINVAL;
  849. /* copy eBPF program license from user space */
  850. if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
  851. sizeof(license) - 1) < 0)
  852. return -EFAULT;
  853. license[sizeof(license) - 1] = 0;
  854. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  855. is_gpl = license_is_gpl_compatible(license);
  856. if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
  857. return -E2BIG;
  858. if (type == BPF_PROG_TYPE_KPROBE &&
  859. attr->kern_version != LINUX_VERSION_CODE)
  860. return -EINVAL;
  861. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  862. type != BPF_PROG_TYPE_CGROUP_SKB &&
  863. !capable(CAP_SYS_ADMIN))
  864. return -EPERM;
  865. /* plain bpf_prog allocation */
  866. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  867. if (!prog)
  868. return -ENOMEM;
  869. err = bpf_prog_charge_memlock(prog);
  870. if (err)
  871. goto free_prog_nouncharge;
  872. prog->len = attr->insn_cnt;
  873. err = -EFAULT;
  874. if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
  875. bpf_prog_insn_size(prog)) != 0)
  876. goto free_prog;
  877. prog->orig_prog = NULL;
  878. prog->jited = 0;
  879. atomic_set(&prog->aux->refcnt, 1);
  880. prog->gpl_compatible = is_gpl ? 1 : 0;
  881. /* find program type: socket_filter vs tracing_filter */
  882. err = find_prog_type(type, prog);
  883. if (err < 0)
  884. goto free_prog;
  885. prog->aux->load_time = ktime_get_boot_ns();
  886. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
  887. if (err)
  888. goto free_prog;
  889. /* run eBPF verifier */
  890. err = bpf_check(&prog, attr);
  891. if (err < 0)
  892. goto free_used_maps;
  893. /* eBPF program is ready to be JITed */
  894. prog = bpf_prog_select_runtime(prog, &err);
  895. if (err < 0)
  896. goto free_used_maps;
  897. err = bpf_prog_alloc_id(prog);
  898. if (err)
  899. goto free_used_maps;
  900. err = bpf_prog_new_fd(prog);
  901. if (err < 0) {
  902. /* failed to allocate fd.
  903. * bpf_prog_put() is needed because the above
  904. * bpf_prog_alloc_id() has published the prog
  905. * to the userspace and the userspace may
  906. * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
  907. */
  908. bpf_prog_put(prog);
  909. return err;
  910. }
  911. bpf_prog_kallsyms_add(prog);
  912. trace_bpf_prog_load(prog, err);
  913. return err;
  914. free_used_maps:
  915. free_used_maps(prog->aux);
  916. free_prog:
  917. bpf_prog_uncharge_memlock(prog);
  918. free_prog_nouncharge:
  919. bpf_prog_free(prog);
  920. return err;
  921. }
  922. #define BPF_OBJ_LAST_FIELD bpf_fd
  923. static int bpf_obj_pin(const union bpf_attr *attr)
  924. {
  925. if (CHECK_ATTR(BPF_OBJ))
  926. return -EINVAL;
  927. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  928. }
  929. static int bpf_obj_get(const union bpf_attr *attr)
  930. {
  931. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
  932. return -EINVAL;
  933. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
  934. }
  935. #ifdef CONFIG_CGROUP_BPF
  936. #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
  937. static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
  938. {
  939. struct bpf_prog *prog = NULL;
  940. int ufd = attr->target_fd;
  941. struct bpf_map *map;
  942. struct fd f;
  943. int err;
  944. f = fdget(ufd);
  945. map = __bpf_map_get(f);
  946. if (IS_ERR(map))
  947. return PTR_ERR(map);
  948. if (attach) {
  949. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  950. BPF_PROG_TYPE_SK_SKB);
  951. if (IS_ERR(prog)) {
  952. fdput(f);
  953. return PTR_ERR(prog);
  954. }
  955. }
  956. err = sock_map_prog(map, prog, attr->attach_type);
  957. if (err) {
  958. fdput(f);
  959. if (prog)
  960. bpf_prog_put(prog);
  961. return err;
  962. }
  963. fdput(f);
  964. return 0;
  965. }
  966. static int bpf_prog_attach(const union bpf_attr *attr)
  967. {
  968. enum bpf_prog_type ptype;
  969. struct bpf_prog *prog;
  970. struct cgroup *cgrp;
  971. int ret;
  972. if (!capable(CAP_NET_ADMIN))
  973. return -EPERM;
  974. if (CHECK_ATTR(BPF_PROG_ATTACH))
  975. return -EINVAL;
  976. if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
  977. return -EINVAL;
  978. switch (attr->attach_type) {
  979. case BPF_CGROUP_INET_INGRESS:
  980. case BPF_CGROUP_INET_EGRESS:
  981. ptype = BPF_PROG_TYPE_CGROUP_SKB;
  982. break;
  983. case BPF_CGROUP_INET_SOCK_CREATE:
  984. ptype = BPF_PROG_TYPE_CGROUP_SOCK;
  985. break;
  986. case BPF_CGROUP_SOCK_OPS:
  987. ptype = BPF_PROG_TYPE_SOCK_OPS;
  988. break;
  989. case BPF_SK_SKB_STREAM_PARSER:
  990. case BPF_SK_SKB_STREAM_VERDICT:
  991. return sockmap_get_from_fd(attr, true);
  992. default:
  993. return -EINVAL;
  994. }
  995. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  996. if (IS_ERR(prog))
  997. return PTR_ERR(prog);
  998. cgrp = cgroup_get_from_fd(attr->target_fd);
  999. if (IS_ERR(cgrp)) {
  1000. bpf_prog_put(prog);
  1001. return PTR_ERR(cgrp);
  1002. }
  1003. ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
  1004. attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
  1005. if (ret)
  1006. bpf_prog_put(prog);
  1007. cgroup_put(cgrp);
  1008. return ret;
  1009. }
  1010. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  1011. static int bpf_prog_detach(const union bpf_attr *attr)
  1012. {
  1013. struct cgroup *cgrp;
  1014. int ret;
  1015. if (!capable(CAP_NET_ADMIN))
  1016. return -EPERM;
  1017. if (CHECK_ATTR(BPF_PROG_DETACH))
  1018. return -EINVAL;
  1019. switch (attr->attach_type) {
  1020. case BPF_CGROUP_INET_INGRESS:
  1021. case BPF_CGROUP_INET_EGRESS:
  1022. case BPF_CGROUP_INET_SOCK_CREATE:
  1023. case BPF_CGROUP_SOCK_OPS:
  1024. cgrp = cgroup_get_from_fd(attr->target_fd);
  1025. if (IS_ERR(cgrp))
  1026. return PTR_ERR(cgrp);
  1027. ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
  1028. cgroup_put(cgrp);
  1029. break;
  1030. case BPF_SK_SKB_STREAM_PARSER:
  1031. case BPF_SK_SKB_STREAM_VERDICT:
  1032. ret = sockmap_get_from_fd(attr, false);
  1033. break;
  1034. default:
  1035. return -EINVAL;
  1036. }
  1037. return ret;
  1038. }
  1039. #endif /* CONFIG_CGROUP_BPF */
  1040. #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
  1041. static int bpf_prog_test_run(const union bpf_attr *attr,
  1042. union bpf_attr __user *uattr)
  1043. {
  1044. struct bpf_prog *prog;
  1045. int ret = -ENOTSUPP;
  1046. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  1047. return -EINVAL;
  1048. prog = bpf_prog_get(attr->test.prog_fd);
  1049. if (IS_ERR(prog))
  1050. return PTR_ERR(prog);
  1051. if (prog->aux->ops->test_run)
  1052. ret = prog->aux->ops->test_run(prog, attr, uattr);
  1053. bpf_prog_put(prog);
  1054. return ret;
  1055. }
  1056. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  1057. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  1058. union bpf_attr __user *uattr,
  1059. struct idr *idr,
  1060. spinlock_t *lock)
  1061. {
  1062. u32 next_id = attr->start_id;
  1063. int err = 0;
  1064. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  1065. return -EINVAL;
  1066. if (!capable(CAP_SYS_ADMIN))
  1067. return -EPERM;
  1068. next_id++;
  1069. spin_lock_bh(lock);
  1070. if (!idr_get_next(idr, &next_id))
  1071. err = -ENOENT;
  1072. spin_unlock_bh(lock);
  1073. if (!err)
  1074. err = put_user(next_id, &uattr->next_id);
  1075. return err;
  1076. }
  1077. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  1078. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  1079. {
  1080. struct bpf_prog *prog;
  1081. u32 id = attr->prog_id;
  1082. int fd;
  1083. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  1084. return -EINVAL;
  1085. if (!capable(CAP_SYS_ADMIN))
  1086. return -EPERM;
  1087. spin_lock_bh(&prog_idr_lock);
  1088. prog = idr_find(&prog_idr, id);
  1089. if (prog)
  1090. prog = bpf_prog_inc_not_zero(prog);
  1091. else
  1092. prog = ERR_PTR(-ENOENT);
  1093. spin_unlock_bh(&prog_idr_lock);
  1094. if (IS_ERR(prog))
  1095. return PTR_ERR(prog);
  1096. fd = bpf_prog_new_fd(prog);
  1097. if (fd < 0)
  1098. bpf_prog_put(prog);
  1099. return fd;
  1100. }
  1101. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
  1102. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  1103. {
  1104. struct bpf_map *map;
  1105. u32 id = attr->map_id;
  1106. int fd;
  1107. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
  1108. return -EINVAL;
  1109. if (!capable(CAP_SYS_ADMIN))
  1110. return -EPERM;
  1111. spin_lock_bh(&map_idr_lock);
  1112. map = idr_find(&map_idr, id);
  1113. if (map)
  1114. map = bpf_map_inc_not_zero(map, true);
  1115. else
  1116. map = ERR_PTR(-ENOENT);
  1117. spin_unlock_bh(&map_idr_lock);
  1118. if (IS_ERR(map))
  1119. return PTR_ERR(map);
  1120. fd = bpf_map_new_fd(map);
  1121. if (fd < 0)
  1122. bpf_map_put(map);
  1123. return fd;
  1124. }
  1125. static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
  1126. const union bpf_attr *attr,
  1127. union bpf_attr __user *uattr)
  1128. {
  1129. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1130. struct bpf_prog_info info = {};
  1131. u32 info_len = attr->info.info_len;
  1132. char __user *uinsns;
  1133. u32 ulen;
  1134. int err;
  1135. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1136. if (err)
  1137. return err;
  1138. info_len = min_t(u32, sizeof(info), info_len);
  1139. if (copy_from_user(&info, uinfo, info_len))
  1140. return -EFAULT;
  1141. info.type = prog->type;
  1142. info.id = prog->aux->id;
  1143. info.load_time = prog->aux->load_time;
  1144. info.created_by_uid = from_kuid_munged(current_user_ns(),
  1145. prog->aux->user->uid);
  1146. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  1147. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  1148. ulen = info.nr_map_ids;
  1149. info.nr_map_ids = prog->aux->used_map_cnt;
  1150. ulen = min_t(u32, info.nr_map_ids, ulen);
  1151. if (ulen) {
  1152. u32 *user_map_ids = (u32 *)info.map_ids;
  1153. u32 i;
  1154. for (i = 0; i < ulen; i++)
  1155. if (put_user(prog->aux->used_maps[i]->id,
  1156. &user_map_ids[i]))
  1157. return -EFAULT;
  1158. }
  1159. if (!capable(CAP_SYS_ADMIN)) {
  1160. info.jited_prog_len = 0;
  1161. info.xlated_prog_len = 0;
  1162. goto done;
  1163. }
  1164. ulen = info.jited_prog_len;
  1165. info.jited_prog_len = prog->jited_len;
  1166. if (info.jited_prog_len && ulen) {
  1167. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  1168. ulen = min_t(u32, info.jited_prog_len, ulen);
  1169. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  1170. return -EFAULT;
  1171. }
  1172. ulen = info.xlated_prog_len;
  1173. info.xlated_prog_len = bpf_prog_insn_size(prog);
  1174. if (info.xlated_prog_len && ulen) {
  1175. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  1176. ulen = min_t(u32, info.xlated_prog_len, ulen);
  1177. if (copy_to_user(uinsns, prog->insnsi, ulen))
  1178. return -EFAULT;
  1179. }
  1180. done:
  1181. if (copy_to_user(uinfo, &info, info_len) ||
  1182. put_user(info_len, &uattr->info.info_len))
  1183. return -EFAULT;
  1184. return 0;
  1185. }
  1186. static int bpf_map_get_info_by_fd(struct bpf_map *map,
  1187. const union bpf_attr *attr,
  1188. union bpf_attr __user *uattr)
  1189. {
  1190. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  1191. struct bpf_map_info info = {};
  1192. u32 info_len = attr->info.info_len;
  1193. int err;
  1194. err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
  1195. if (err)
  1196. return err;
  1197. info_len = min_t(u32, sizeof(info), info_len);
  1198. info.type = map->map_type;
  1199. info.id = map->id;
  1200. info.key_size = map->key_size;
  1201. info.value_size = map->value_size;
  1202. info.max_entries = map->max_entries;
  1203. info.map_flags = map->map_flags;
  1204. memcpy(info.name, map->name, sizeof(map->name));
  1205. if (copy_to_user(uinfo, &info, info_len) ||
  1206. put_user(info_len, &uattr->info.info_len))
  1207. return -EFAULT;
  1208. return 0;
  1209. }
  1210. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  1211. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  1212. union bpf_attr __user *uattr)
  1213. {
  1214. int ufd = attr->info.bpf_fd;
  1215. struct fd f;
  1216. int err;
  1217. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  1218. return -EINVAL;
  1219. f = fdget(ufd);
  1220. if (!f.file)
  1221. return -EBADFD;
  1222. if (f.file->f_op == &bpf_prog_fops)
  1223. err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
  1224. uattr);
  1225. else if (f.file->f_op == &bpf_map_fops)
  1226. err = bpf_map_get_info_by_fd(f.file->private_data, attr,
  1227. uattr);
  1228. else
  1229. err = -EINVAL;
  1230. fdput(f);
  1231. return err;
  1232. }
  1233. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  1234. {
  1235. union bpf_attr attr = {};
  1236. int err;
  1237. if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  1238. return -EPERM;
  1239. err = check_uarg_tail_zero(uattr, sizeof(attr), size);
  1240. if (err)
  1241. return err;
  1242. size = min_t(u32, size, sizeof(attr));
  1243. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  1244. if (copy_from_user(&attr, uattr, size) != 0)
  1245. return -EFAULT;
  1246. switch (cmd) {
  1247. case BPF_MAP_CREATE:
  1248. err = map_create(&attr);
  1249. break;
  1250. case BPF_MAP_LOOKUP_ELEM:
  1251. err = map_lookup_elem(&attr);
  1252. break;
  1253. case BPF_MAP_UPDATE_ELEM:
  1254. err = map_update_elem(&attr);
  1255. break;
  1256. case BPF_MAP_DELETE_ELEM:
  1257. err = map_delete_elem(&attr);
  1258. break;
  1259. case BPF_MAP_GET_NEXT_KEY:
  1260. err = map_get_next_key(&attr);
  1261. break;
  1262. case BPF_PROG_LOAD:
  1263. err = bpf_prog_load(&attr);
  1264. break;
  1265. case BPF_OBJ_PIN:
  1266. err = bpf_obj_pin(&attr);
  1267. break;
  1268. case BPF_OBJ_GET:
  1269. err = bpf_obj_get(&attr);
  1270. break;
  1271. #ifdef CONFIG_CGROUP_BPF
  1272. case BPF_PROG_ATTACH:
  1273. err = bpf_prog_attach(&attr);
  1274. break;
  1275. case BPF_PROG_DETACH:
  1276. err = bpf_prog_detach(&attr);
  1277. break;
  1278. #endif
  1279. case BPF_PROG_TEST_RUN:
  1280. err = bpf_prog_test_run(&attr, uattr);
  1281. break;
  1282. case BPF_PROG_GET_NEXT_ID:
  1283. err = bpf_obj_get_next_id(&attr, uattr,
  1284. &prog_idr, &prog_idr_lock);
  1285. break;
  1286. case BPF_MAP_GET_NEXT_ID:
  1287. err = bpf_obj_get_next_id(&attr, uattr,
  1288. &map_idr, &map_idr_lock);
  1289. break;
  1290. case BPF_PROG_GET_FD_BY_ID:
  1291. err = bpf_prog_get_fd_by_id(&attr);
  1292. break;
  1293. case BPF_MAP_GET_FD_BY_ID:
  1294. err = bpf_map_get_fd_by_id(&attr);
  1295. break;
  1296. case BPF_OBJ_GET_INFO_BY_FD:
  1297. err = bpf_obj_get_info_by_fd(&attr, uattr);
  1298. break;
  1299. default:
  1300. err = -EINVAL;
  1301. break;
  1302. }
  1303. return err;
  1304. }