cgroup.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Functions to manage eBPF programs attached to cgroups
  4. *
  5. * Copyright (c) 2016 Daniel Mack
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/atomic.h>
  9. #include <linux/cgroup.h>
  10. #include <linux/filter.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysctl.h>
  13. #include <linux/string.h>
  14. #include <linux/bpf.h>
  15. #include <linux/bpf-cgroup.h>
  16. #include <linux/bpf_lsm.h>
  17. #include <linux/bpf_verifier.h>
  18. #include <net/sock.h>
  19. #include <net/bpf_sk_storage.h>
  20. #include "../cgroup/cgroup-internal.h"
  21. DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE);
  22. EXPORT_SYMBOL(cgroup_bpf_enabled_key);
  23. /* __always_inline is necessary to prevent indirect call through run_prog
  24. * function pointer.
  25. */
  26. static __always_inline int
  27. bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
  28. enum cgroup_bpf_attach_type atype,
  29. const void *ctx, bpf_prog_run_fn run_prog,
  30. int retval, u32 *ret_flags)
  31. {
  32. const struct bpf_prog_array_item *item;
  33. const struct bpf_prog *prog;
  34. const struct bpf_prog_array *array;
  35. struct bpf_run_ctx *old_run_ctx;
  36. struct bpf_cg_run_ctx run_ctx;
  37. u32 func_ret;
  38. run_ctx.retval = retval;
  39. migrate_disable();
  40. rcu_read_lock();
  41. array = rcu_dereference(cgrp->effective[atype]);
  42. item = &array->items[0];
  43. old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  44. while ((prog = READ_ONCE(item->prog))) {
  45. run_ctx.prog_item = item;
  46. func_ret = run_prog(prog, ctx);
  47. if (ret_flags) {
  48. *(ret_flags) |= (func_ret >> 1);
  49. func_ret &= 1;
  50. }
  51. if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
  52. run_ctx.retval = -EPERM;
  53. item++;
  54. }
  55. bpf_reset_run_ctx(old_run_ctx);
  56. rcu_read_unlock();
  57. migrate_enable();
  58. return run_ctx.retval;
  59. }
  60. unsigned int __cgroup_bpf_run_lsm_sock(const void *ctx,
  61. const struct bpf_insn *insn)
  62. {
  63. const struct bpf_prog *shim_prog;
  64. struct sock *sk;
  65. struct cgroup *cgrp;
  66. int ret = 0;
  67. u64 *args;
  68. args = (u64 *)ctx;
  69. sk = (void *)(unsigned long)args[0];
  70. /*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
  71. shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
  72. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  73. if (likely(cgrp))
  74. ret = bpf_prog_run_array_cg(&cgrp->bpf,
  75. shim_prog->aux->cgroup_atype,
  76. ctx, bpf_prog_run, 0, NULL);
  77. return ret;
  78. }
  79. unsigned int __cgroup_bpf_run_lsm_socket(const void *ctx,
  80. const struct bpf_insn *insn)
  81. {
  82. const struct bpf_prog *shim_prog;
  83. struct socket *sock;
  84. struct cgroup *cgrp;
  85. int ret = 0;
  86. u64 *args;
  87. args = (u64 *)ctx;
  88. sock = (void *)(unsigned long)args[0];
  89. /*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
  90. shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
  91. cgrp = sock_cgroup_ptr(&sock->sk->sk_cgrp_data);
  92. if (likely(cgrp))
  93. ret = bpf_prog_run_array_cg(&cgrp->bpf,
  94. shim_prog->aux->cgroup_atype,
  95. ctx, bpf_prog_run, 0, NULL);
  96. return ret;
  97. }
  98. unsigned int __cgroup_bpf_run_lsm_current(const void *ctx,
  99. const struct bpf_insn *insn)
  100. {
  101. const struct bpf_prog *shim_prog;
  102. struct cgroup *cgrp;
  103. int ret = 0;
  104. /*shim_prog = container_of(insn, struct bpf_prog, insnsi);*/
  105. shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi));
  106. /* We rely on trampoline's __bpf_prog_enter_lsm_cgroup to grab RCU read lock. */
  107. cgrp = task_dfl_cgroup(current);
  108. if (likely(cgrp))
  109. ret = bpf_prog_run_array_cg(&cgrp->bpf,
  110. shim_prog->aux->cgroup_atype,
  111. ctx, bpf_prog_run, 0, NULL);
  112. return ret;
  113. }
  114. #ifdef CONFIG_BPF_LSM
  115. struct cgroup_lsm_atype {
  116. u32 attach_btf_id;
  117. int refcnt;
  118. };
  119. static struct cgroup_lsm_atype cgroup_lsm_atype[CGROUP_LSM_NUM];
  120. static enum cgroup_bpf_attach_type
  121. bpf_cgroup_atype_find(enum bpf_attach_type attach_type, u32 attach_btf_id)
  122. {
  123. int i;
  124. lockdep_assert_held(&cgroup_mutex);
  125. if (attach_type != BPF_LSM_CGROUP)
  126. return to_cgroup_bpf_attach_type(attach_type);
  127. for (i = 0; i < ARRAY_SIZE(cgroup_lsm_atype); i++)
  128. if (cgroup_lsm_atype[i].attach_btf_id == attach_btf_id)
  129. return CGROUP_LSM_START + i;
  130. for (i = 0; i < ARRAY_SIZE(cgroup_lsm_atype); i++)
  131. if (cgroup_lsm_atype[i].attach_btf_id == 0)
  132. return CGROUP_LSM_START + i;
  133. return -E2BIG;
  134. }
  135. void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype)
  136. {
  137. int i = cgroup_atype - CGROUP_LSM_START;
  138. lockdep_assert_held(&cgroup_mutex);
  139. WARN_ON_ONCE(cgroup_lsm_atype[i].attach_btf_id &&
  140. cgroup_lsm_atype[i].attach_btf_id != attach_btf_id);
  141. cgroup_lsm_atype[i].attach_btf_id = attach_btf_id;
  142. cgroup_lsm_atype[i].refcnt++;
  143. }
  144. void bpf_cgroup_atype_put(int cgroup_atype)
  145. {
  146. int i = cgroup_atype - CGROUP_LSM_START;
  147. cgroup_lock();
  148. if (--cgroup_lsm_atype[i].refcnt <= 0)
  149. cgroup_lsm_atype[i].attach_btf_id = 0;
  150. WARN_ON_ONCE(cgroup_lsm_atype[i].refcnt < 0);
  151. cgroup_unlock();
  152. }
  153. #else
  154. static enum cgroup_bpf_attach_type
  155. bpf_cgroup_atype_find(enum bpf_attach_type attach_type, u32 attach_btf_id)
  156. {
  157. if (attach_type != BPF_LSM_CGROUP)
  158. return to_cgroup_bpf_attach_type(attach_type);
  159. return -EOPNOTSUPP;
  160. }
  161. #endif /* CONFIG_BPF_LSM */
  162. void cgroup_bpf_offline(struct cgroup *cgrp)
  163. {
  164. cgroup_get(cgrp);
  165. percpu_ref_kill(&cgrp->bpf.refcnt);
  166. }
  167. static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[])
  168. {
  169. enum bpf_cgroup_storage_type stype;
  170. for_each_cgroup_storage_type(stype)
  171. bpf_cgroup_storage_free(storages[stype]);
  172. }
  173. static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[],
  174. struct bpf_cgroup_storage *new_storages[],
  175. enum bpf_attach_type type,
  176. struct bpf_prog *prog,
  177. struct cgroup *cgrp)
  178. {
  179. enum bpf_cgroup_storage_type stype;
  180. struct bpf_cgroup_storage_key key;
  181. struct bpf_map *map;
  182. key.cgroup_inode_id = cgroup_id(cgrp);
  183. key.attach_type = type;
  184. for_each_cgroup_storage_type(stype) {
  185. map = prog->aux->cgroup_storage[stype];
  186. if (!map)
  187. continue;
  188. storages[stype] = cgroup_storage_lookup((void *)map, &key, false);
  189. if (storages[stype])
  190. continue;
  191. storages[stype] = bpf_cgroup_storage_alloc(prog, stype);
  192. if (IS_ERR(storages[stype])) {
  193. bpf_cgroup_storages_free(new_storages);
  194. return -ENOMEM;
  195. }
  196. new_storages[stype] = storages[stype];
  197. }
  198. return 0;
  199. }
  200. static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[],
  201. struct bpf_cgroup_storage *src[])
  202. {
  203. enum bpf_cgroup_storage_type stype;
  204. for_each_cgroup_storage_type(stype)
  205. dst[stype] = src[stype];
  206. }
  207. static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
  208. struct cgroup *cgrp,
  209. enum bpf_attach_type attach_type)
  210. {
  211. enum bpf_cgroup_storage_type stype;
  212. for_each_cgroup_storage_type(stype)
  213. bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
  214. }
  215. /* Called when bpf_cgroup_link is auto-detached from dying cgroup.
  216. * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
  217. * doesn't free link memory, which will eventually be done by bpf_link's
  218. * release() callback, when its last FD is closed.
  219. */
  220. static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
  221. {
  222. cgroup_put(link->cgroup);
  223. link->cgroup = NULL;
  224. }
  225. /**
  226. * cgroup_bpf_release() - put references of all bpf programs and
  227. * release all cgroup bpf data
  228. * @work: work structure embedded into the cgroup to modify
  229. */
  230. static void cgroup_bpf_release(struct work_struct *work)
  231. {
  232. struct cgroup *p, *cgrp = container_of(work, struct cgroup,
  233. bpf.release_work);
  234. struct bpf_prog_array *old_array;
  235. struct list_head *storages = &cgrp->bpf.storages;
  236. struct bpf_cgroup_storage *storage, *stmp;
  237. unsigned int atype;
  238. cgroup_lock();
  239. for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) {
  240. struct hlist_head *progs = &cgrp->bpf.progs[atype];
  241. struct bpf_prog_list *pl;
  242. struct hlist_node *pltmp;
  243. hlist_for_each_entry_safe(pl, pltmp, progs, node) {
  244. hlist_del(&pl->node);
  245. if (pl->prog) {
  246. if (pl->prog->expected_attach_type == BPF_LSM_CGROUP)
  247. bpf_trampoline_unlink_cgroup_shim(pl->prog);
  248. bpf_prog_put(pl->prog);
  249. }
  250. if (pl->link) {
  251. if (pl->link->link.prog->expected_attach_type == BPF_LSM_CGROUP)
  252. bpf_trampoline_unlink_cgroup_shim(pl->link->link.prog);
  253. bpf_cgroup_link_auto_detach(pl->link);
  254. }
  255. kfree(pl);
  256. static_branch_dec(&cgroup_bpf_enabled_key[atype]);
  257. }
  258. old_array = rcu_dereference_protected(
  259. cgrp->bpf.effective[atype],
  260. lockdep_is_held(&cgroup_mutex));
  261. bpf_prog_array_free(old_array);
  262. }
  263. list_for_each_entry_safe(storage, stmp, storages, list_cg) {
  264. bpf_cgroup_storage_unlink(storage);
  265. bpf_cgroup_storage_free(storage);
  266. }
  267. cgroup_unlock();
  268. for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
  269. cgroup_bpf_put(p);
  270. percpu_ref_exit(&cgrp->bpf.refcnt);
  271. cgroup_put(cgrp);
  272. }
  273. /**
  274. * cgroup_bpf_release_fn() - callback used to schedule releasing
  275. * of bpf cgroup data
  276. * @ref: percpu ref counter structure
  277. */
  278. static void cgroup_bpf_release_fn(struct percpu_ref *ref)
  279. {
  280. struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
  281. INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
  282. queue_work(system_wq, &cgrp->bpf.release_work);
  283. }
  284. /* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through
  285. * link or direct prog.
  286. */
  287. static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
  288. {
  289. if (pl->prog)
  290. return pl->prog;
  291. if (pl->link)
  292. return pl->link->link.prog;
  293. return NULL;
  294. }
  295. /* count number of elements in the list.
  296. * it's slow but the list cannot be long
  297. */
  298. static u32 prog_list_length(struct hlist_head *head)
  299. {
  300. struct bpf_prog_list *pl;
  301. u32 cnt = 0;
  302. hlist_for_each_entry(pl, head, node) {
  303. if (!prog_list_prog(pl))
  304. continue;
  305. cnt++;
  306. }
  307. return cnt;
  308. }
  309. /* if parent has non-overridable prog attached,
  310. * disallow attaching new programs to the descendent cgroup.
  311. * if parent has overridable or multi-prog, allow attaching
  312. */
  313. static bool hierarchy_allows_attach(struct cgroup *cgrp,
  314. enum cgroup_bpf_attach_type atype)
  315. {
  316. struct cgroup *p;
  317. p = cgroup_parent(cgrp);
  318. if (!p)
  319. return true;
  320. do {
  321. u32 flags = p->bpf.flags[atype];
  322. u32 cnt;
  323. if (flags & BPF_F_ALLOW_MULTI)
  324. return true;
  325. cnt = prog_list_length(&p->bpf.progs[atype]);
  326. WARN_ON_ONCE(cnt > 1);
  327. if (cnt == 1)
  328. return !!(flags & BPF_F_ALLOW_OVERRIDE);
  329. p = cgroup_parent(p);
  330. } while (p);
  331. return true;
  332. }
  333. /* compute a chain of effective programs for a given cgroup:
  334. * start from the list of programs in this cgroup and add
  335. * all parent programs.
  336. * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
  337. * to programs in this cgroup
  338. */
  339. static int compute_effective_progs(struct cgroup *cgrp,
  340. enum cgroup_bpf_attach_type atype,
  341. struct bpf_prog_array **array)
  342. {
  343. struct bpf_prog_array_item *item;
  344. struct bpf_prog_array *progs;
  345. struct bpf_prog_list *pl;
  346. struct cgroup *p = cgrp;
  347. int cnt = 0;
  348. /* count number of effective programs by walking parents */
  349. do {
  350. if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
  351. cnt += prog_list_length(&p->bpf.progs[atype]);
  352. p = cgroup_parent(p);
  353. } while (p);
  354. progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
  355. if (!progs)
  356. return -ENOMEM;
  357. /* populate the array with effective progs */
  358. cnt = 0;
  359. p = cgrp;
  360. do {
  361. if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
  362. continue;
  363. hlist_for_each_entry(pl, &p->bpf.progs[atype], node) {
  364. if (!prog_list_prog(pl))
  365. continue;
  366. item = &progs->items[cnt];
  367. item->prog = prog_list_prog(pl);
  368. bpf_cgroup_storages_assign(item->cgroup_storage,
  369. pl->storage);
  370. cnt++;
  371. }
  372. } while ((p = cgroup_parent(p)));
  373. *array = progs;
  374. return 0;
  375. }
  376. static void activate_effective_progs(struct cgroup *cgrp,
  377. enum cgroup_bpf_attach_type atype,
  378. struct bpf_prog_array *old_array)
  379. {
  380. old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array,
  381. lockdep_is_held(&cgroup_mutex));
  382. /* free prog array after grace period, since __cgroup_bpf_run_*()
  383. * might be still walking the array
  384. */
  385. bpf_prog_array_free(old_array);
  386. }
  387. /**
  388. * cgroup_bpf_inherit() - inherit effective programs from parent
  389. * @cgrp: the cgroup to modify
  390. */
  391. int cgroup_bpf_inherit(struct cgroup *cgrp)
  392. {
  393. /* has to use marco instead of const int, since compiler thinks
  394. * that array below is variable length
  395. */
  396. #define NR ARRAY_SIZE(cgrp->bpf.effective)
  397. struct bpf_prog_array *arrays[NR] = {};
  398. struct cgroup *p;
  399. int ret, i;
  400. ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
  401. GFP_KERNEL);
  402. if (ret)
  403. return ret;
  404. for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
  405. cgroup_bpf_get(p);
  406. for (i = 0; i < NR; i++)
  407. INIT_HLIST_HEAD(&cgrp->bpf.progs[i]);
  408. INIT_LIST_HEAD(&cgrp->bpf.storages);
  409. for (i = 0; i < NR; i++)
  410. if (compute_effective_progs(cgrp, i, &arrays[i]))
  411. goto cleanup;
  412. for (i = 0; i < NR; i++)
  413. activate_effective_progs(cgrp, i, arrays[i]);
  414. return 0;
  415. cleanup:
  416. for (i = 0; i < NR; i++)
  417. bpf_prog_array_free(arrays[i]);
  418. for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
  419. cgroup_bpf_put(p);
  420. percpu_ref_exit(&cgrp->bpf.refcnt);
  421. return -ENOMEM;
  422. }
  423. static int update_effective_progs(struct cgroup *cgrp,
  424. enum cgroup_bpf_attach_type atype)
  425. {
  426. struct cgroup_subsys_state *css;
  427. int err;
  428. /* allocate and recompute effective prog arrays */
  429. css_for_each_descendant_pre(css, &cgrp->self) {
  430. struct cgroup *desc = container_of(css, struct cgroup, self);
  431. if (percpu_ref_is_zero(&desc->bpf.refcnt))
  432. continue;
  433. err = compute_effective_progs(desc, atype, &desc->bpf.inactive);
  434. if (err)
  435. goto cleanup;
  436. }
  437. /* all allocations were successful. Activate all prog arrays */
  438. css_for_each_descendant_pre(css, &cgrp->self) {
  439. struct cgroup *desc = container_of(css, struct cgroup, self);
  440. if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
  441. if (unlikely(desc->bpf.inactive)) {
  442. bpf_prog_array_free(desc->bpf.inactive);
  443. desc->bpf.inactive = NULL;
  444. }
  445. continue;
  446. }
  447. activate_effective_progs(desc, atype, desc->bpf.inactive);
  448. desc->bpf.inactive = NULL;
  449. }
  450. return 0;
  451. cleanup:
  452. /* oom while computing effective. Free all computed effective arrays
  453. * since they were not activated
  454. */
  455. css_for_each_descendant_pre(css, &cgrp->self) {
  456. struct cgroup *desc = container_of(css, struct cgroup, self);
  457. bpf_prog_array_free(desc->bpf.inactive);
  458. desc->bpf.inactive = NULL;
  459. }
  460. return err;
  461. }
  462. #define BPF_CGROUP_MAX_PROGS 64
  463. static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,
  464. struct bpf_prog *prog,
  465. struct bpf_cgroup_link *link,
  466. struct bpf_prog *replace_prog,
  467. bool allow_multi)
  468. {
  469. struct bpf_prog_list *pl;
  470. /* single-attach case */
  471. if (!allow_multi) {
  472. if (hlist_empty(progs))
  473. return NULL;
  474. return hlist_entry(progs->first, typeof(*pl), node);
  475. }
  476. hlist_for_each_entry(pl, progs, node) {
  477. if (prog && pl->prog == prog && prog != replace_prog)
  478. /* disallow attaching the same prog twice */
  479. return ERR_PTR(-EINVAL);
  480. if (link && pl->link == link)
  481. /* disallow attaching the same link twice */
  482. return ERR_PTR(-EINVAL);
  483. }
  484. /* direct prog multi-attach w/ replacement case */
  485. if (replace_prog) {
  486. hlist_for_each_entry(pl, progs, node) {
  487. if (pl->prog == replace_prog)
  488. /* a match found */
  489. return pl;
  490. }
  491. /* prog to replace not found for cgroup */
  492. return ERR_PTR(-ENOENT);
  493. }
  494. return NULL;
  495. }
  496. /**
  497. * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
  498. * propagate the change to descendants
  499. * @cgrp: The cgroup which descendants to traverse
  500. * @prog: A program to attach
  501. * @link: A link to attach
  502. * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
  503. * @type: Type of attach operation
  504. * @flags: Option flags
  505. *
  506. * Exactly one of @prog or @link can be non-null.
  507. * Must be called with cgroup_mutex held.
  508. */
  509. static int __cgroup_bpf_attach(struct cgroup *cgrp,
  510. struct bpf_prog *prog, struct bpf_prog *replace_prog,
  511. struct bpf_cgroup_link *link,
  512. enum bpf_attach_type type, u32 flags)
  513. {
  514. u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
  515. struct bpf_prog *old_prog = NULL;
  516. struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
  517. struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
  518. struct bpf_prog *new_prog = prog ? : link->link.prog;
  519. enum cgroup_bpf_attach_type atype;
  520. struct bpf_prog_list *pl;
  521. struct hlist_head *progs;
  522. int err;
  523. if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
  524. ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
  525. /* invalid combination */
  526. return -EINVAL;
  527. if (link && (prog || replace_prog))
  528. /* only either link or prog/replace_prog can be specified */
  529. return -EINVAL;
  530. if (!!replace_prog != !!(flags & BPF_F_REPLACE))
  531. /* replace_prog implies BPF_F_REPLACE, and vice versa */
  532. return -EINVAL;
  533. atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
  534. if (atype < 0)
  535. return -EINVAL;
  536. progs = &cgrp->bpf.progs[atype];
  537. if (!hierarchy_allows_attach(cgrp, atype))
  538. return -EPERM;
  539. if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags)
  540. /* Disallow attaching non-overridable on top
  541. * of existing overridable in this cgroup.
  542. * Disallow attaching multi-prog if overridable or none
  543. */
  544. return -EPERM;
  545. if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
  546. return -E2BIG;
  547. pl = find_attach_entry(progs, prog, link, replace_prog,
  548. flags & BPF_F_ALLOW_MULTI);
  549. if (IS_ERR(pl))
  550. return PTR_ERR(pl);
  551. if (bpf_cgroup_storages_alloc(storage, new_storage, type,
  552. prog ? : link->link.prog, cgrp))
  553. return -ENOMEM;
  554. if (pl) {
  555. old_prog = pl->prog;
  556. } else {
  557. struct hlist_node *last = NULL;
  558. pl = kmalloc(sizeof(*pl), GFP_KERNEL);
  559. if (!pl) {
  560. bpf_cgroup_storages_free(new_storage);
  561. return -ENOMEM;
  562. }
  563. if (hlist_empty(progs))
  564. hlist_add_head(&pl->node, progs);
  565. else
  566. hlist_for_each(last, progs) {
  567. if (last->next)
  568. continue;
  569. hlist_add_behind(&pl->node, last);
  570. break;
  571. }
  572. }
  573. pl->prog = prog;
  574. pl->link = link;
  575. bpf_cgroup_storages_assign(pl->storage, storage);
  576. cgrp->bpf.flags[atype] = saved_flags;
  577. if (type == BPF_LSM_CGROUP) {
  578. err = bpf_trampoline_link_cgroup_shim(new_prog, atype);
  579. if (err)
  580. goto cleanup;
  581. }
  582. err = update_effective_progs(cgrp, atype);
  583. if (err)
  584. goto cleanup_trampoline;
  585. if (old_prog) {
  586. if (type == BPF_LSM_CGROUP)
  587. bpf_trampoline_unlink_cgroup_shim(old_prog);
  588. bpf_prog_put(old_prog);
  589. } else {
  590. static_branch_inc(&cgroup_bpf_enabled_key[atype]);
  591. }
  592. bpf_cgroup_storages_link(new_storage, cgrp, type);
  593. return 0;
  594. cleanup_trampoline:
  595. if (type == BPF_LSM_CGROUP)
  596. bpf_trampoline_unlink_cgroup_shim(new_prog);
  597. cleanup:
  598. if (old_prog) {
  599. pl->prog = old_prog;
  600. pl->link = NULL;
  601. }
  602. bpf_cgroup_storages_free(new_storage);
  603. if (!old_prog) {
  604. hlist_del(&pl->node);
  605. kfree(pl);
  606. }
  607. return err;
  608. }
  609. static int cgroup_bpf_attach(struct cgroup *cgrp,
  610. struct bpf_prog *prog, struct bpf_prog *replace_prog,
  611. struct bpf_cgroup_link *link,
  612. enum bpf_attach_type type,
  613. u32 flags)
  614. {
  615. int ret;
  616. cgroup_lock();
  617. ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags);
  618. cgroup_unlock();
  619. return ret;
  620. }
  621. /* Swap updated BPF program for given link in effective program arrays across
  622. * all descendant cgroups. This function is guaranteed to succeed.
  623. */
  624. static void replace_effective_prog(struct cgroup *cgrp,
  625. enum cgroup_bpf_attach_type atype,
  626. struct bpf_cgroup_link *link)
  627. {
  628. struct bpf_prog_array_item *item;
  629. struct cgroup_subsys_state *css;
  630. struct bpf_prog_array *progs;
  631. struct bpf_prog_list *pl;
  632. struct hlist_head *head;
  633. struct cgroup *cg;
  634. int pos;
  635. css_for_each_descendant_pre(css, &cgrp->self) {
  636. struct cgroup *desc = container_of(css, struct cgroup, self);
  637. if (percpu_ref_is_zero(&desc->bpf.refcnt))
  638. continue;
  639. /* find position of link in effective progs array */
  640. for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
  641. if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
  642. continue;
  643. head = &cg->bpf.progs[atype];
  644. hlist_for_each_entry(pl, head, node) {
  645. if (!prog_list_prog(pl))
  646. continue;
  647. if (pl->link == link)
  648. goto found;
  649. pos++;
  650. }
  651. }
  652. found:
  653. BUG_ON(!cg);
  654. progs = rcu_dereference_protected(
  655. desc->bpf.effective[atype],
  656. lockdep_is_held(&cgroup_mutex));
  657. item = &progs->items[pos];
  658. WRITE_ONCE(item->prog, link->link.prog);
  659. }
  660. }
  661. /**
  662. * __cgroup_bpf_replace() - Replace link's program and propagate the change
  663. * to descendants
  664. * @cgrp: The cgroup which descendants to traverse
  665. * @link: A link for which to replace BPF program
  666. * @type: Type of attach operation
  667. *
  668. * Must be called with cgroup_mutex held.
  669. */
  670. static int __cgroup_bpf_replace(struct cgroup *cgrp,
  671. struct bpf_cgroup_link *link,
  672. struct bpf_prog *new_prog)
  673. {
  674. enum cgroup_bpf_attach_type atype;
  675. struct bpf_prog *old_prog;
  676. struct bpf_prog_list *pl;
  677. struct hlist_head *progs;
  678. bool found = false;
  679. atype = bpf_cgroup_atype_find(link->type, new_prog->aux->attach_btf_id);
  680. if (atype < 0)
  681. return -EINVAL;
  682. progs = &cgrp->bpf.progs[atype];
  683. if (link->link.prog->type != new_prog->type)
  684. return -EINVAL;
  685. hlist_for_each_entry(pl, progs, node) {
  686. if (pl->link == link) {
  687. found = true;
  688. break;
  689. }
  690. }
  691. if (!found)
  692. return -ENOENT;
  693. old_prog = xchg(&link->link.prog, new_prog);
  694. replace_effective_prog(cgrp, atype, link);
  695. bpf_prog_put(old_prog);
  696. return 0;
  697. }
  698. static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
  699. struct bpf_prog *old_prog)
  700. {
  701. struct bpf_cgroup_link *cg_link;
  702. int ret;
  703. cg_link = container_of(link, struct bpf_cgroup_link, link);
  704. cgroup_lock();
  705. /* link might have been auto-released by dying cgroup, so fail */
  706. if (!cg_link->cgroup) {
  707. ret = -ENOLINK;
  708. goto out_unlock;
  709. }
  710. if (old_prog && link->prog != old_prog) {
  711. ret = -EPERM;
  712. goto out_unlock;
  713. }
  714. ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
  715. out_unlock:
  716. cgroup_unlock();
  717. return ret;
  718. }
  719. static struct bpf_prog_list *find_detach_entry(struct hlist_head *progs,
  720. struct bpf_prog *prog,
  721. struct bpf_cgroup_link *link,
  722. bool allow_multi)
  723. {
  724. struct bpf_prog_list *pl;
  725. if (!allow_multi) {
  726. if (hlist_empty(progs))
  727. /* report error when trying to detach and nothing is attached */
  728. return ERR_PTR(-ENOENT);
  729. /* to maintain backward compatibility NONE and OVERRIDE cgroups
  730. * allow detaching with invalid FD (prog==NULL) in legacy mode
  731. */
  732. return hlist_entry(progs->first, typeof(*pl), node);
  733. }
  734. if (!prog && !link)
  735. /* to detach MULTI prog the user has to specify valid FD
  736. * of the program or link to be detached
  737. */
  738. return ERR_PTR(-EINVAL);
  739. /* find the prog or link and detach it */
  740. hlist_for_each_entry(pl, progs, node) {
  741. if (pl->prog == prog && pl->link == link)
  742. return pl;
  743. }
  744. return ERR_PTR(-ENOENT);
  745. }
  746. /**
  747. * purge_effective_progs() - After compute_effective_progs fails to alloc new
  748. * cgrp->bpf.inactive table we can recover by
  749. * recomputing the array in place.
  750. *
  751. * @cgrp: The cgroup which descendants to travers
  752. * @prog: A program to detach or NULL
  753. * @link: A link to detach or NULL
  754. * @atype: Type of detach operation
  755. */
  756. static void purge_effective_progs(struct cgroup *cgrp, struct bpf_prog *prog,
  757. struct bpf_cgroup_link *link,
  758. enum cgroup_bpf_attach_type atype)
  759. {
  760. struct cgroup_subsys_state *css;
  761. struct bpf_prog_array *progs;
  762. struct bpf_prog_list *pl;
  763. struct hlist_head *head;
  764. struct cgroup *cg;
  765. int pos;
  766. /* recompute effective prog array in place */
  767. css_for_each_descendant_pre(css, &cgrp->self) {
  768. struct cgroup *desc = container_of(css, struct cgroup, self);
  769. if (percpu_ref_is_zero(&desc->bpf.refcnt))
  770. continue;
  771. /* find position of link or prog in effective progs array */
  772. for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
  773. if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
  774. continue;
  775. head = &cg->bpf.progs[atype];
  776. hlist_for_each_entry(pl, head, node) {
  777. if (!prog_list_prog(pl))
  778. continue;
  779. if (pl->prog == prog && pl->link == link)
  780. goto found;
  781. pos++;
  782. }
  783. }
  784. /* no link or prog match, skip the cgroup of this layer */
  785. continue;
  786. found:
  787. progs = rcu_dereference_protected(
  788. desc->bpf.effective[atype],
  789. lockdep_is_held(&cgroup_mutex));
  790. /* Remove the program from the array */
  791. WARN_ONCE(bpf_prog_array_delete_safe_at(progs, pos),
  792. "Failed to purge a prog from array at index %d", pos);
  793. }
  794. }
  795. /**
  796. * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
  797. * propagate the change to descendants
  798. * @cgrp: The cgroup which descendants to traverse
  799. * @prog: A program to detach or NULL
  800. * @link: A link to detach or NULL
  801. * @type: Type of detach operation
  802. *
  803. * At most one of @prog or @link can be non-NULL.
  804. * Must be called with cgroup_mutex held.
  805. */
  806. static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
  807. struct bpf_cgroup_link *link, enum bpf_attach_type type)
  808. {
  809. enum cgroup_bpf_attach_type atype;
  810. struct bpf_prog *old_prog;
  811. struct bpf_prog_list *pl;
  812. struct hlist_head *progs;
  813. u32 attach_btf_id = 0;
  814. u32 flags;
  815. if (prog)
  816. attach_btf_id = prog->aux->attach_btf_id;
  817. if (link)
  818. attach_btf_id = link->link.prog->aux->attach_btf_id;
  819. atype = bpf_cgroup_atype_find(type, attach_btf_id);
  820. if (atype < 0)
  821. return -EINVAL;
  822. progs = &cgrp->bpf.progs[atype];
  823. flags = cgrp->bpf.flags[atype];
  824. if (prog && link)
  825. /* only one of prog or link can be specified */
  826. return -EINVAL;
  827. pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
  828. if (IS_ERR(pl))
  829. return PTR_ERR(pl);
  830. /* mark it deleted, so it's ignored while recomputing effective */
  831. old_prog = pl->prog;
  832. pl->prog = NULL;
  833. pl->link = NULL;
  834. if (update_effective_progs(cgrp, atype)) {
  835. /* if update effective array failed replace the prog with a dummy prog*/
  836. pl->prog = old_prog;
  837. pl->link = link;
  838. purge_effective_progs(cgrp, old_prog, link, atype);
  839. }
  840. /* now can actually delete it from this cgroup list */
  841. hlist_del(&pl->node);
  842. kfree(pl);
  843. if (hlist_empty(progs))
  844. /* last program was detached, reset flags to zero */
  845. cgrp->bpf.flags[atype] = 0;
  846. if (old_prog) {
  847. if (type == BPF_LSM_CGROUP)
  848. bpf_trampoline_unlink_cgroup_shim(old_prog);
  849. bpf_prog_put(old_prog);
  850. }
  851. static_branch_dec(&cgroup_bpf_enabled_key[atype]);
  852. return 0;
  853. }
  854. static int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
  855. enum bpf_attach_type type)
  856. {
  857. int ret;
  858. cgroup_lock();
  859. ret = __cgroup_bpf_detach(cgrp, prog, NULL, type);
  860. cgroup_unlock();
  861. return ret;
  862. }
  863. /* Must be called with cgroup_mutex held to avoid races. */
  864. static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
  865. union bpf_attr __user *uattr)
  866. {
  867. __u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags);
  868. bool effective_query = attr->query.query_flags & BPF_F_QUERY_EFFECTIVE;
  869. __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
  870. enum bpf_attach_type type = attr->query.attach_type;
  871. enum cgroup_bpf_attach_type from_atype, to_atype;
  872. enum cgroup_bpf_attach_type atype;
  873. struct bpf_prog_array *effective;
  874. int cnt, ret = 0, i;
  875. int total_cnt = 0;
  876. u32 flags;
  877. if (effective_query && prog_attach_flags)
  878. return -EINVAL;
  879. if (type == BPF_LSM_CGROUP) {
  880. if (!effective_query && attr->query.prog_cnt &&
  881. prog_ids && !prog_attach_flags)
  882. return -EINVAL;
  883. from_atype = CGROUP_LSM_START;
  884. to_atype = CGROUP_LSM_END;
  885. flags = 0;
  886. } else {
  887. from_atype = to_cgroup_bpf_attach_type(type);
  888. if (from_atype < 0)
  889. return -EINVAL;
  890. to_atype = from_atype;
  891. flags = cgrp->bpf.flags[from_atype];
  892. }
  893. for (atype = from_atype; atype <= to_atype; atype++) {
  894. if (effective_query) {
  895. effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
  896. lockdep_is_held(&cgroup_mutex));
  897. total_cnt += bpf_prog_array_length(effective);
  898. } else {
  899. total_cnt += prog_list_length(&cgrp->bpf.progs[atype]);
  900. }
  901. }
  902. /* always output uattr->query.attach_flags as 0 during effective query */
  903. flags = effective_query ? 0 : flags;
  904. if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
  905. return -EFAULT;
  906. if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt)))
  907. return -EFAULT;
  908. if (attr->query.prog_cnt == 0 || !prog_ids || !total_cnt)
  909. /* return early if user requested only program count + flags */
  910. return 0;
  911. if (attr->query.prog_cnt < total_cnt) {
  912. total_cnt = attr->query.prog_cnt;
  913. ret = -ENOSPC;
  914. }
  915. for (atype = from_atype; atype <= to_atype && total_cnt; atype++) {
  916. if (effective_query) {
  917. effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
  918. lockdep_is_held(&cgroup_mutex));
  919. cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
  920. ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
  921. } else {
  922. struct hlist_head *progs;
  923. struct bpf_prog_list *pl;
  924. struct bpf_prog *prog;
  925. u32 id;
  926. progs = &cgrp->bpf.progs[atype];
  927. cnt = min_t(int, prog_list_length(progs), total_cnt);
  928. i = 0;
  929. hlist_for_each_entry(pl, progs, node) {
  930. prog = prog_list_prog(pl);
  931. id = prog->aux->id;
  932. if (copy_to_user(prog_ids + i, &id, sizeof(id)))
  933. return -EFAULT;
  934. if (++i == cnt)
  935. break;
  936. }
  937. if (prog_attach_flags) {
  938. flags = cgrp->bpf.flags[atype];
  939. for (i = 0; i < cnt; i++)
  940. if (copy_to_user(prog_attach_flags + i,
  941. &flags, sizeof(flags)))
  942. return -EFAULT;
  943. prog_attach_flags += cnt;
  944. }
  945. }
  946. prog_ids += cnt;
  947. total_cnt -= cnt;
  948. }
  949. return ret;
  950. }
  951. static int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
  952. union bpf_attr __user *uattr)
  953. {
  954. int ret;
  955. cgroup_lock();
  956. ret = __cgroup_bpf_query(cgrp, attr, uattr);
  957. cgroup_unlock();
  958. return ret;
  959. }
  960. int cgroup_bpf_prog_attach(const union bpf_attr *attr,
  961. enum bpf_prog_type ptype, struct bpf_prog *prog)
  962. {
  963. struct bpf_prog *replace_prog = NULL;
  964. struct cgroup *cgrp;
  965. int ret;
  966. cgrp = cgroup_get_from_fd(attr->target_fd);
  967. if (IS_ERR(cgrp))
  968. return PTR_ERR(cgrp);
  969. if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
  970. (attr->attach_flags & BPF_F_REPLACE)) {
  971. replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
  972. if (IS_ERR(replace_prog)) {
  973. cgroup_put(cgrp);
  974. return PTR_ERR(replace_prog);
  975. }
  976. }
  977. ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
  978. attr->attach_type, attr->attach_flags);
  979. if (replace_prog)
  980. bpf_prog_put(replace_prog);
  981. cgroup_put(cgrp);
  982. return ret;
  983. }
  984. int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
  985. {
  986. struct bpf_prog *prog;
  987. struct cgroup *cgrp;
  988. int ret;
  989. cgrp = cgroup_get_from_fd(attr->target_fd);
  990. if (IS_ERR(cgrp))
  991. return PTR_ERR(cgrp);
  992. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  993. if (IS_ERR(prog))
  994. prog = NULL;
  995. ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
  996. if (prog)
  997. bpf_prog_put(prog);
  998. cgroup_put(cgrp);
  999. return ret;
  1000. }
  1001. static void bpf_cgroup_link_release(struct bpf_link *link)
  1002. {
  1003. struct bpf_cgroup_link *cg_link =
  1004. container_of(link, struct bpf_cgroup_link, link);
  1005. struct cgroup *cg;
  1006. /* link might have been auto-detached by dying cgroup already,
  1007. * in that case our work is done here
  1008. */
  1009. if (!cg_link->cgroup)
  1010. return;
  1011. cgroup_lock();
  1012. /* re-check cgroup under lock again */
  1013. if (!cg_link->cgroup) {
  1014. cgroup_unlock();
  1015. return;
  1016. }
  1017. WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
  1018. cg_link->type));
  1019. if (cg_link->type == BPF_LSM_CGROUP)
  1020. bpf_trampoline_unlink_cgroup_shim(cg_link->link.prog);
  1021. cg = cg_link->cgroup;
  1022. cg_link->cgroup = NULL;
  1023. cgroup_unlock();
  1024. cgroup_put(cg);
  1025. }
  1026. static void bpf_cgroup_link_dealloc(struct bpf_link *link)
  1027. {
  1028. struct bpf_cgroup_link *cg_link =
  1029. container_of(link, struct bpf_cgroup_link, link);
  1030. kfree(cg_link);
  1031. }
  1032. static int bpf_cgroup_link_detach(struct bpf_link *link)
  1033. {
  1034. bpf_cgroup_link_release(link);
  1035. return 0;
  1036. }
  1037. static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
  1038. struct seq_file *seq)
  1039. {
  1040. struct bpf_cgroup_link *cg_link =
  1041. container_of(link, struct bpf_cgroup_link, link);
  1042. u64 cg_id = 0;
  1043. cgroup_lock();
  1044. if (cg_link->cgroup)
  1045. cg_id = cgroup_id(cg_link->cgroup);
  1046. cgroup_unlock();
  1047. seq_printf(seq,
  1048. "cgroup_id:\t%llu\n"
  1049. "attach_type:\t%d\n",
  1050. cg_id,
  1051. cg_link->type);
  1052. }
  1053. static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
  1054. struct bpf_link_info *info)
  1055. {
  1056. struct bpf_cgroup_link *cg_link =
  1057. container_of(link, struct bpf_cgroup_link, link);
  1058. u64 cg_id = 0;
  1059. cgroup_lock();
  1060. if (cg_link->cgroup)
  1061. cg_id = cgroup_id(cg_link->cgroup);
  1062. cgroup_unlock();
  1063. info->cgroup.cgroup_id = cg_id;
  1064. info->cgroup.attach_type = cg_link->type;
  1065. return 0;
  1066. }
  1067. static const struct bpf_link_ops bpf_cgroup_link_lops = {
  1068. .release = bpf_cgroup_link_release,
  1069. .dealloc = bpf_cgroup_link_dealloc,
  1070. .detach = bpf_cgroup_link_detach,
  1071. .update_prog = cgroup_bpf_replace,
  1072. .show_fdinfo = bpf_cgroup_link_show_fdinfo,
  1073. .fill_link_info = bpf_cgroup_link_fill_link_info,
  1074. };
  1075. int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  1076. {
  1077. struct bpf_link_primer link_primer;
  1078. struct bpf_cgroup_link *link;
  1079. struct cgroup *cgrp;
  1080. int err;
  1081. if (attr->link_create.flags)
  1082. return -EINVAL;
  1083. cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
  1084. if (IS_ERR(cgrp))
  1085. return PTR_ERR(cgrp);
  1086. link = kzalloc(sizeof(*link), GFP_USER);
  1087. if (!link) {
  1088. err = -ENOMEM;
  1089. goto out_put_cgroup;
  1090. }
  1091. bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
  1092. prog);
  1093. link->cgroup = cgrp;
  1094. link->type = attr->link_create.attach_type;
  1095. err = bpf_link_prime(&link->link, &link_primer);
  1096. if (err) {
  1097. kfree(link);
  1098. goto out_put_cgroup;
  1099. }
  1100. err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
  1101. link->type, BPF_F_ALLOW_MULTI);
  1102. if (err) {
  1103. bpf_link_cleanup(&link_primer);
  1104. goto out_put_cgroup;
  1105. }
  1106. return bpf_link_settle(&link_primer);
  1107. out_put_cgroup:
  1108. cgroup_put(cgrp);
  1109. return err;
  1110. }
  1111. int cgroup_bpf_prog_query(const union bpf_attr *attr,
  1112. union bpf_attr __user *uattr)
  1113. {
  1114. struct cgroup *cgrp;
  1115. int ret;
  1116. cgrp = cgroup_get_from_fd(attr->query.target_fd);
  1117. if (IS_ERR(cgrp))
  1118. return PTR_ERR(cgrp);
  1119. ret = cgroup_bpf_query(cgrp, attr, uattr);
  1120. cgroup_put(cgrp);
  1121. return ret;
  1122. }
  1123. /**
  1124. * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
  1125. * @sk: The socket sending or receiving traffic
  1126. * @skb: The skb that is being sent or received
  1127. * @type: The type of program to be executed
  1128. *
  1129. * If no socket is passed, or the socket is not of type INET or INET6,
  1130. * this function does nothing and returns 0.
  1131. *
  1132. * The program type passed in via @type must be suitable for network
  1133. * filtering. No further check is performed to assert that.
  1134. *
  1135. * For egress packets, this function can return:
  1136. * NET_XMIT_SUCCESS (0) - continue with packet output
  1137. * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
  1138. * NET_XMIT_CN (2) - continue with packet output and notify TCP
  1139. * to call cwr
  1140. * -err - drop packet
  1141. *
  1142. * For ingress packets, this function will return -EPERM if any
  1143. * attached program was found and if it returned != 1 during execution.
  1144. * Otherwise 0 is returned.
  1145. */
  1146. int __cgroup_bpf_run_filter_skb(struct sock *sk,
  1147. struct sk_buff *skb,
  1148. enum cgroup_bpf_attach_type atype)
  1149. {
  1150. unsigned int offset = skb->data - skb_network_header(skb);
  1151. struct sock *save_sk;
  1152. void *saved_data_end;
  1153. struct cgroup *cgrp;
  1154. int ret;
  1155. if (!sk || !sk_fullsock(sk))
  1156. return 0;
  1157. if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
  1158. return 0;
  1159. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1160. save_sk = skb->sk;
  1161. skb->sk = sk;
  1162. __skb_push(skb, offset);
  1163. /* compute pointers for the bpf prog */
  1164. bpf_compute_and_save_data_end(skb, &saved_data_end);
  1165. if (atype == CGROUP_INET_EGRESS) {
  1166. u32 flags = 0;
  1167. bool cn;
  1168. ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, skb,
  1169. __bpf_prog_run_save_cb, 0, &flags);
  1170. /* Return values of CGROUP EGRESS BPF programs are:
  1171. * 0: drop packet
  1172. * 1: keep packet
  1173. * 2: drop packet and cn
  1174. * 3: keep packet and cn
  1175. *
  1176. * The returned value is then converted to one of the NET_XMIT
  1177. * or an error code that is then interpreted as drop packet
  1178. * (and no cn):
  1179. * 0: NET_XMIT_SUCCESS skb should be transmitted
  1180. * 1: NET_XMIT_DROP skb should be dropped and cn
  1181. * 2: NET_XMIT_CN skb should be transmitted and cn
  1182. * 3: -err skb should be dropped
  1183. */
  1184. cn = flags & BPF_RET_SET_CN;
  1185. if (ret && !IS_ERR_VALUE((long)ret))
  1186. ret = -EFAULT;
  1187. if (!ret)
  1188. ret = (cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);
  1189. else
  1190. ret = (cn ? NET_XMIT_DROP : ret);
  1191. } else {
  1192. ret = bpf_prog_run_array_cg(&cgrp->bpf, atype,
  1193. skb, __bpf_prog_run_save_cb, 0,
  1194. NULL);
  1195. if (ret && !IS_ERR_VALUE((long)ret))
  1196. ret = -EFAULT;
  1197. }
  1198. bpf_restore_data_end(skb, saved_data_end);
  1199. __skb_pull(skb, offset);
  1200. skb->sk = save_sk;
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
  1204. /**
  1205. * __cgroup_bpf_run_filter_sk() - Run a program on a sock
  1206. * @sk: sock structure to manipulate
  1207. * @type: The type of program to be executed
  1208. *
  1209. * socket is passed is expected to be of type INET or INET6.
  1210. *
  1211. * The program type passed in via @type must be suitable for sock
  1212. * filtering. No further check is performed to assert that.
  1213. *
  1214. * This function will return %-EPERM if any if an attached program was found
  1215. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  1216. */
  1217. int __cgroup_bpf_run_filter_sk(struct sock *sk,
  1218. enum cgroup_bpf_attach_type atype)
  1219. {
  1220. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1221. return bpf_prog_run_array_cg(&cgrp->bpf, atype, sk, bpf_prog_run, 0,
  1222. NULL);
  1223. }
  1224. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
  1225. /**
  1226. * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
  1227. * provided by user sockaddr
  1228. * @sk: sock struct that will use sockaddr
  1229. * @uaddr: sockaddr struct provided by user
  1230. * @type: The type of program to be executed
  1231. * @t_ctx: Pointer to attach type specific context
  1232. * @flags: Pointer to u32 which contains higher bits of BPF program
  1233. * return value (OR'ed together).
  1234. *
  1235. * socket is expected to be of type INET or INET6.
  1236. *
  1237. * This function will return %-EPERM if an attached program is found and
  1238. * returned value != 1 during execution. In all other cases, 0 is returned.
  1239. */
  1240. int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
  1241. struct sockaddr *uaddr,
  1242. enum cgroup_bpf_attach_type atype,
  1243. void *t_ctx,
  1244. u32 *flags)
  1245. {
  1246. struct bpf_sock_addr_kern ctx = {
  1247. .sk = sk,
  1248. .uaddr = uaddr,
  1249. .t_ctx = t_ctx,
  1250. };
  1251. struct sockaddr_storage unspec;
  1252. struct cgroup *cgrp;
  1253. /* Check socket family since not all sockets represent network
  1254. * endpoint (e.g. AF_UNIX).
  1255. */
  1256. if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
  1257. return 0;
  1258. if (!ctx.uaddr) {
  1259. memset(&unspec, 0, sizeof(unspec));
  1260. ctx.uaddr = (struct sockaddr *)&unspec;
  1261. }
  1262. cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1263. return bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run,
  1264. 0, flags);
  1265. }
  1266. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
  1267. /**
  1268. * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
  1269. * @sk: socket to get cgroup from
  1270. * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
  1271. * sk with connection information (IP addresses, etc.) May not contain
  1272. * cgroup info if it is a req sock.
  1273. * @type: The type of program to be executed
  1274. *
  1275. * socket passed is expected to be of type INET or INET6.
  1276. *
  1277. * The program type passed in via @type must be suitable for sock_ops
  1278. * filtering. No further check is performed to assert that.
  1279. *
  1280. * This function will return %-EPERM if any if an attached program was found
  1281. * and if it returned != 1 during execution. In all other cases, 0 is returned.
  1282. */
  1283. int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
  1284. struct bpf_sock_ops_kern *sock_ops,
  1285. enum cgroup_bpf_attach_type atype)
  1286. {
  1287. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1288. return bpf_prog_run_array_cg(&cgrp->bpf, atype, sock_ops, bpf_prog_run,
  1289. 0, NULL);
  1290. }
  1291. EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
  1292. int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
  1293. short access, enum cgroup_bpf_attach_type atype)
  1294. {
  1295. struct cgroup *cgrp;
  1296. struct bpf_cgroup_dev_ctx ctx = {
  1297. .access_type = (access << 16) | dev_type,
  1298. .major = major,
  1299. .minor = minor,
  1300. };
  1301. int ret;
  1302. rcu_read_lock();
  1303. cgrp = task_dfl_cgroup(current);
  1304. ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
  1305. NULL);
  1306. rcu_read_unlock();
  1307. return ret;
  1308. }
  1309. BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
  1310. {
  1311. /* flags argument is not used now,
  1312. * but provides an ability to extend the API.
  1313. * verifier checks that its value is correct.
  1314. */
  1315. enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
  1316. struct bpf_cgroup_storage *storage;
  1317. struct bpf_cg_run_ctx *ctx;
  1318. void *ptr;
  1319. /* get current cgroup storage from BPF run context */
  1320. ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
  1321. storage = ctx->prog_item->cgroup_storage[stype];
  1322. if (stype == BPF_CGROUP_STORAGE_SHARED)
  1323. ptr = &READ_ONCE(storage->buf)->data[0];
  1324. else
  1325. ptr = this_cpu_ptr(storage->percpu_buf);
  1326. return (unsigned long)ptr;
  1327. }
  1328. const struct bpf_func_proto bpf_get_local_storage_proto = {
  1329. .func = bpf_get_local_storage,
  1330. .gpl_only = false,
  1331. .ret_type = RET_PTR_TO_MAP_VALUE,
  1332. .arg1_type = ARG_CONST_MAP_PTR,
  1333. .arg2_type = ARG_ANYTHING,
  1334. };
  1335. BPF_CALL_0(bpf_get_retval)
  1336. {
  1337. struct bpf_cg_run_ctx *ctx =
  1338. container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
  1339. return ctx->retval;
  1340. }
  1341. const struct bpf_func_proto bpf_get_retval_proto = {
  1342. .func = bpf_get_retval,
  1343. .gpl_only = false,
  1344. .ret_type = RET_INTEGER,
  1345. };
  1346. BPF_CALL_1(bpf_set_retval, int, retval)
  1347. {
  1348. struct bpf_cg_run_ctx *ctx =
  1349. container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
  1350. ctx->retval = retval;
  1351. return 0;
  1352. }
  1353. const struct bpf_func_proto bpf_set_retval_proto = {
  1354. .func = bpf_set_retval,
  1355. .gpl_only = false,
  1356. .ret_type = RET_INTEGER,
  1357. .arg1_type = ARG_ANYTHING,
  1358. };
  1359. static const struct bpf_func_proto *
  1360. cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1361. {
  1362. const struct bpf_func_proto *func_proto;
  1363. func_proto = cgroup_common_func_proto(func_id, prog);
  1364. if (func_proto)
  1365. return func_proto;
  1366. func_proto = cgroup_current_func_proto(func_id, prog);
  1367. if (func_proto)
  1368. return func_proto;
  1369. switch (func_id) {
  1370. case BPF_FUNC_perf_event_output:
  1371. return &bpf_event_output_data_proto;
  1372. default:
  1373. return bpf_base_func_proto(func_id);
  1374. }
  1375. }
  1376. static bool cgroup_dev_is_valid_access(int off, int size,
  1377. enum bpf_access_type type,
  1378. const struct bpf_prog *prog,
  1379. struct bpf_insn_access_aux *info)
  1380. {
  1381. const int size_default = sizeof(__u32);
  1382. if (type == BPF_WRITE)
  1383. return false;
  1384. if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
  1385. return false;
  1386. /* The verifier guarantees that size > 0. */
  1387. if (off % size != 0)
  1388. return false;
  1389. switch (off) {
  1390. case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
  1391. bpf_ctx_record_field_size(info, size_default);
  1392. if (!bpf_ctx_narrow_access_ok(off, size, size_default))
  1393. return false;
  1394. break;
  1395. default:
  1396. if (size != size_default)
  1397. return false;
  1398. }
  1399. return true;
  1400. }
  1401. const struct bpf_prog_ops cg_dev_prog_ops = {
  1402. };
  1403. const struct bpf_verifier_ops cg_dev_verifier_ops = {
  1404. .get_func_proto = cgroup_dev_func_proto,
  1405. .is_valid_access = cgroup_dev_is_valid_access,
  1406. };
  1407. /**
  1408. * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
  1409. *
  1410. * @head: sysctl table header
  1411. * @table: sysctl table
  1412. * @write: sysctl is being read (= 0) or written (= 1)
  1413. * @buf: pointer to buffer (in and out)
  1414. * @pcount: value-result argument: value is size of buffer pointed to by @buf,
  1415. * result is size of @new_buf if program set new value, initial value
  1416. * otherwise
  1417. * @ppos: value-result argument: value is position at which read from or write
  1418. * to sysctl is happening, result is new position if program overrode it,
  1419. * initial value otherwise
  1420. * @type: type of program to be executed
  1421. *
  1422. * Program is run when sysctl is being accessed, either read or written, and
  1423. * can allow or deny such access.
  1424. *
  1425. * This function will return %-EPERM if an attached program is found and
  1426. * returned value != 1 during execution. In all other cases 0 is returned.
  1427. */
  1428. int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
  1429. struct ctl_table *table, int write,
  1430. char **buf, size_t *pcount, loff_t *ppos,
  1431. enum cgroup_bpf_attach_type atype)
  1432. {
  1433. struct bpf_sysctl_kern ctx = {
  1434. .head = head,
  1435. .table = table,
  1436. .write = write,
  1437. .ppos = ppos,
  1438. .cur_val = NULL,
  1439. .cur_len = PAGE_SIZE,
  1440. .new_val = NULL,
  1441. .new_len = 0,
  1442. .new_updated = 0,
  1443. };
  1444. struct cgroup *cgrp;
  1445. loff_t pos = 0;
  1446. int ret;
  1447. ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
  1448. if (!ctx.cur_val ||
  1449. table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
  1450. /* Let BPF program decide how to proceed. */
  1451. ctx.cur_len = 0;
  1452. }
  1453. if (write && *buf && *pcount) {
  1454. /* BPF program should be able to override new value with a
  1455. * buffer bigger than provided by user.
  1456. */
  1457. ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
  1458. ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
  1459. if (ctx.new_val) {
  1460. memcpy(ctx.new_val, *buf, ctx.new_len);
  1461. } else {
  1462. /* Let BPF program decide how to proceed. */
  1463. ctx.new_len = 0;
  1464. }
  1465. }
  1466. rcu_read_lock();
  1467. cgrp = task_dfl_cgroup(current);
  1468. ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
  1469. NULL);
  1470. rcu_read_unlock();
  1471. kfree(ctx.cur_val);
  1472. if (ret == 1 && ctx.new_updated) {
  1473. kfree(*buf);
  1474. *buf = ctx.new_val;
  1475. *pcount = ctx.new_len;
  1476. } else {
  1477. kfree(ctx.new_val);
  1478. }
  1479. return ret;
  1480. }
  1481. #ifdef CONFIG_NET
  1482. static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
  1483. struct bpf_sockopt_buf *buf)
  1484. {
  1485. if (unlikely(max_optlen < 0))
  1486. return -EINVAL;
  1487. if (unlikely(max_optlen > PAGE_SIZE)) {
  1488. /* We don't expose optvals that are greater than PAGE_SIZE
  1489. * to the BPF program.
  1490. */
  1491. max_optlen = PAGE_SIZE;
  1492. }
  1493. if (max_optlen <= sizeof(buf->data)) {
  1494. /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
  1495. * bytes avoid the cost of kzalloc.
  1496. */
  1497. ctx->optval = buf->data;
  1498. ctx->optval_end = ctx->optval + max_optlen;
  1499. return max_optlen;
  1500. }
  1501. ctx->optval = kzalloc(max_optlen, GFP_USER);
  1502. if (!ctx->optval)
  1503. return -ENOMEM;
  1504. ctx->optval_end = ctx->optval + max_optlen;
  1505. return max_optlen;
  1506. }
  1507. static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
  1508. struct bpf_sockopt_buf *buf)
  1509. {
  1510. if (ctx->optval == buf->data)
  1511. return;
  1512. kfree(ctx->optval);
  1513. }
  1514. static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
  1515. struct bpf_sockopt_buf *buf)
  1516. {
  1517. return ctx->optval != buf->data;
  1518. }
  1519. int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  1520. int *optname, char __user *optval,
  1521. int *optlen, char **kernel_optval)
  1522. {
  1523. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1524. struct bpf_sockopt_buf buf = {};
  1525. struct bpf_sockopt_kern ctx = {
  1526. .sk = sk,
  1527. .level = *level,
  1528. .optname = *optname,
  1529. };
  1530. int ret, max_optlen;
  1531. /* Allocate a bit more than the initial user buffer for
  1532. * BPF program. The canonical use case is overriding
  1533. * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
  1534. */
  1535. max_optlen = max_t(int, 16, *optlen);
  1536. max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
  1537. if (max_optlen < 0)
  1538. return max_optlen;
  1539. ctx.optlen = *optlen;
  1540. if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
  1541. ret = -EFAULT;
  1542. goto out;
  1543. }
  1544. lock_sock(sk);
  1545. ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
  1546. &ctx, bpf_prog_run, 0, NULL);
  1547. release_sock(sk);
  1548. if (ret)
  1549. goto out;
  1550. if (ctx.optlen == -1) {
  1551. /* optlen set to -1, bypass kernel */
  1552. ret = 1;
  1553. } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
  1554. /* optlen is out of bounds */
  1555. if (*optlen > PAGE_SIZE && ctx.optlen >= 0) {
  1556. pr_info_once("bpf setsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
  1557. ctx.optlen, max_optlen);
  1558. ret = 0;
  1559. goto out;
  1560. }
  1561. ret = -EFAULT;
  1562. } else {
  1563. /* optlen within bounds, run kernel handler */
  1564. ret = 0;
  1565. /* export any potential modifications */
  1566. *level = ctx.level;
  1567. *optname = ctx.optname;
  1568. /* optlen == 0 from BPF indicates that we should
  1569. * use original userspace data.
  1570. */
  1571. if (ctx.optlen != 0) {
  1572. *optlen = ctx.optlen;
  1573. /* We've used bpf_sockopt_kern->buf as an intermediary
  1574. * storage, but the BPF program indicates that we need
  1575. * to pass this data to the kernel setsockopt handler.
  1576. * No way to export on-stack buf, have to allocate a
  1577. * new buffer.
  1578. */
  1579. if (!sockopt_buf_allocated(&ctx, &buf)) {
  1580. void *p = kmalloc(ctx.optlen, GFP_USER);
  1581. if (!p) {
  1582. ret = -ENOMEM;
  1583. goto out;
  1584. }
  1585. memcpy(p, ctx.optval, ctx.optlen);
  1586. *kernel_optval = p;
  1587. } else {
  1588. *kernel_optval = ctx.optval;
  1589. }
  1590. /* export and don't free sockopt buf */
  1591. return 0;
  1592. }
  1593. }
  1594. out:
  1595. sockopt_free_buf(&ctx, &buf);
  1596. return ret;
  1597. }
  1598. int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  1599. int optname, char __user *optval,
  1600. int __user *optlen, int max_optlen,
  1601. int retval)
  1602. {
  1603. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1604. struct bpf_sockopt_buf buf = {};
  1605. struct bpf_sockopt_kern ctx = {
  1606. .sk = sk,
  1607. .level = level,
  1608. .optname = optname,
  1609. .current_task = current,
  1610. };
  1611. int orig_optlen;
  1612. int ret;
  1613. orig_optlen = max_optlen;
  1614. ctx.optlen = max_optlen;
  1615. max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
  1616. if (max_optlen < 0)
  1617. return max_optlen;
  1618. if (!retval) {
  1619. /* If kernel getsockopt finished successfully,
  1620. * copy whatever was returned to the user back
  1621. * into our temporary buffer. Set optlen to the
  1622. * one that kernel returned as well to let
  1623. * BPF programs inspect the value.
  1624. */
  1625. if (get_user(ctx.optlen, optlen)) {
  1626. ret = -EFAULT;
  1627. goto out;
  1628. }
  1629. if (ctx.optlen < 0) {
  1630. ret = -EFAULT;
  1631. goto out;
  1632. }
  1633. orig_optlen = ctx.optlen;
  1634. if (copy_from_user(ctx.optval, optval,
  1635. min(ctx.optlen, max_optlen)) != 0) {
  1636. ret = -EFAULT;
  1637. goto out;
  1638. }
  1639. }
  1640. lock_sock(sk);
  1641. ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
  1642. &ctx, bpf_prog_run, retval, NULL);
  1643. release_sock(sk);
  1644. if (ret < 0)
  1645. goto out;
  1646. if (optval && (ctx.optlen > max_optlen || ctx.optlen < 0)) {
  1647. if (orig_optlen > PAGE_SIZE && ctx.optlen >= 0) {
  1648. pr_info_once("bpf getsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
  1649. ctx.optlen, max_optlen);
  1650. ret = retval;
  1651. goto out;
  1652. }
  1653. ret = -EFAULT;
  1654. goto out;
  1655. }
  1656. if (ctx.optlen != 0) {
  1657. if (optval && copy_to_user(optval, ctx.optval, ctx.optlen)) {
  1658. ret = -EFAULT;
  1659. goto out;
  1660. }
  1661. if (put_user(ctx.optlen, optlen)) {
  1662. ret = -EFAULT;
  1663. goto out;
  1664. }
  1665. }
  1666. out:
  1667. sockopt_free_buf(&ctx, &buf);
  1668. return ret;
  1669. }
  1670. int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
  1671. int optname, void *optval,
  1672. int *optlen, int retval)
  1673. {
  1674. struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
  1675. struct bpf_sockopt_kern ctx = {
  1676. .sk = sk,
  1677. .level = level,
  1678. .optname = optname,
  1679. .optlen = *optlen,
  1680. .optval = optval,
  1681. .optval_end = optval + *optlen,
  1682. .current_task = current,
  1683. };
  1684. int ret;
  1685. /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
  1686. * user data back into BPF buffer when reval != 0. This is
  1687. * done as an optimization to avoid extra copy, assuming
  1688. * kernel won't populate the data in case of an error.
  1689. * Here we always pass the data and memset() should
  1690. * be called if that data shouldn't be "exported".
  1691. */
  1692. ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
  1693. &ctx, bpf_prog_run, retval, NULL);
  1694. if (ret < 0)
  1695. return ret;
  1696. if (ctx.optlen > *optlen)
  1697. return -EFAULT;
  1698. /* BPF programs can shrink the buffer, export the modifications.
  1699. */
  1700. if (ctx.optlen != 0)
  1701. *optlen = ctx.optlen;
  1702. return ret;
  1703. }
  1704. #endif
  1705. static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
  1706. size_t *lenp)
  1707. {
  1708. ssize_t tmp_ret = 0, ret;
  1709. if (dir->header.parent) {
  1710. tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
  1711. if (tmp_ret < 0)
  1712. return tmp_ret;
  1713. }
  1714. ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
  1715. if (ret < 0)
  1716. return ret;
  1717. *bufp += ret;
  1718. *lenp -= ret;
  1719. ret += tmp_ret;
  1720. /* Avoid leading slash. */
  1721. if (!ret)
  1722. return ret;
  1723. tmp_ret = strscpy(*bufp, "/", *lenp);
  1724. if (tmp_ret < 0)
  1725. return tmp_ret;
  1726. *bufp += tmp_ret;
  1727. *lenp -= tmp_ret;
  1728. return ret + tmp_ret;
  1729. }
  1730. BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
  1731. size_t, buf_len, u64, flags)
  1732. {
  1733. ssize_t tmp_ret = 0, ret;
  1734. if (!buf)
  1735. return -EINVAL;
  1736. if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
  1737. if (!ctx->head)
  1738. return -EINVAL;
  1739. tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
  1740. if (tmp_ret < 0)
  1741. return tmp_ret;
  1742. }
  1743. ret = strscpy(buf, ctx->table->procname, buf_len);
  1744. return ret < 0 ? ret : tmp_ret + ret;
  1745. }
  1746. static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
  1747. .func = bpf_sysctl_get_name,
  1748. .gpl_only = false,
  1749. .ret_type = RET_INTEGER,
  1750. .arg1_type = ARG_PTR_TO_CTX,
  1751. .arg2_type = ARG_PTR_TO_MEM,
  1752. .arg3_type = ARG_CONST_SIZE,
  1753. .arg4_type = ARG_ANYTHING,
  1754. };
  1755. static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
  1756. size_t src_len)
  1757. {
  1758. if (!dst)
  1759. return -EINVAL;
  1760. if (!dst_len)
  1761. return -E2BIG;
  1762. if (!src || !src_len) {
  1763. memset(dst, 0, dst_len);
  1764. return -EINVAL;
  1765. }
  1766. memcpy(dst, src, min(dst_len, src_len));
  1767. if (dst_len > src_len) {
  1768. memset(dst + src_len, '\0', dst_len - src_len);
  1769. return src_len;
  1770. }
  1771. dst[dst_len - 1] = '\0';
  1772. return -E2BIG;
  1773. }
  1774. BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
  1775. char *, buf, size_t, buf_len)
  1776. {
  1777. return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
  1778. }
  1779. static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
  1780. .func = bpf_sysctl_get_current_value,
  1781. .gpl_only = false,
  1782. .ret_type = RET_INTEGER,
  1783. .arg1_type = ARG_PTR_TO_CTX,
  1784. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1785. .arg3_type = ARG_CONST_SIZE,
  1786. };
  1787. BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
  1788. size_t, buf_len)
  1789. {
  1790. if (!ctx->write) {
  1791. if (buf && buf_len)
  1792. memset(buf, '\0', buf_len);
  1793. return -EINVAL;
  1794. }
  1795. return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
  1796. }
  1797. static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
  1798. .func = bpf_sysctl_get_new_value,
  1799. .gpl_only = false,
  1800. .ret_type = RET_INTEGER,
  1801. .arg1_type = ARG_PTR_TO_CTX,
  1802. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  1803. .arg3_type = ARG_CONST_SIZE,
  1804. };
  1805. BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
  1806. const char *, buf, size_t, buf_len)
  1807. {
  1808. if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
  1809. return -EINVAL;
  1810. if (buf_len > PAGE_SIZE - 1)
  1811. return -E2BIG;
  1812. memcpy(ctx->new_val, buf, buf_len);
  1813. ctx->new_len = buf_len;
  1814. ctx->new_updated = 1;
  1815. return 0;
  1816. }
  1817. static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
  1818. .func = bpf_sysctl_set_new_value,
  1819. .gpl_only = false,
  1820. .ret_type = RET_INTEGER,
  1821. .arg1_type = ARG_PTR_TO_CTX,
  1822. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  1823. .arg3_type = ARG_CONST_SIZE,
  1824. };
  1825. static const struct bpf_func_proto *
  1826. sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1827. {
  1828. const struct bpf_func_proto *func_proto;
  1829. func_proto = cgroup_common_func_proto(func_id, prog);
  1830. if (func_proto)
  1831. return func_proto;
  1832. func_proto = cgroup_current_func_proto(func_id, prog);
  1833. if (func_proto)
  1834. return func_proto;
  1835. switch (func_id) {
  1836. case BPF_FUNC_sysctl_get_name:
  1837. return &bpf_sysctl_get_name_proto;
  1838. case BPF_FUNC_sysctl_get_current_value:
  1839. return &bpf_sysctl_get_current_value_proto;
  1840. case BPF_FUNC_sysctl_get_new_value:
  1841. return &bpf_sysctl_get_new_value_proto;
  1842. case BPF_FUNC_sysctl_set_new_value:
  1843. return &bpf_sysctl_set_new_value_proto;
  1844. case BPF_FUNC_ktime_get_coarse_ns:
  1845. return &bpf_ktime_get_coarse_ns_proto;
  1846. case BPF_FUNC_perf_event_output:
  1847. return &bpf_event_output_data_proto;
  1848. default:
  1849. return bpf_base_func_proto(func_id);
  1850. }
  1851. }
  1852. static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
  1853. const struct bpf_prog *prog,
  1854. struct bpf_insn_access_aux *info)
  1855. {
  1856. const int size_default = sizeof(__u32);
  1857. if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
  1858. return false;
  1859. switch (off) {
  1860. case bpf_ctx_range(struct bpf_sysctl, write):
  1861. if (type != BPF_READ)
  1862. return false;
  1863. bpf_ctx_record_field_size(info, size_default);
  1864. return bpf_ctx_narrow_access_ok(off, size, size_default);
  1865. case bpf_ctx_range(struct bpf_sysctl, file_pos):
  1866. if (type == BPF_READ) {
  1867. bpf_ctx_record_field_size(info, size_default);
  1868. return bpf_ctx_narrow_access_ok(off, size, size_default);
  1869. } else {
  1870. return size == size_default;
  1871. }
  1872. default:
  1873. return false;
  1874. }
  1875. }
  1876. static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
  1877. const struct bpf_insn *si,
  1878. struct bpf_insn *insn_buf,
  1879. struct bpf_prog *prog, u32 *target_size)
  1880. {
  1881. struct bpf_insn *insn = insn_buf;
  1882. u32 read_size;
  1883. switch (si->off) {
  1884. case offsetof(struct bpf_sysctl, write):
  1885. *insn++ = BPF_LDX_MEM(
  1886. BPF_SIZE(si->code), si->dst_reg, si->src_reg,
  1887. bpf_target_off(struct bpf_sysctl_kern, write,
  1888. sizeof_field(struct bpf_sysctl_kern,
  1889. write),
  1890. target_size));
  1891. break;
  1892. case offsetof(struct bpf_sysctl, file_pos):
  1893. /* ppos is a pointer so it should be accessed via indirect
  1894. * loads and stores. Also for stores additional temporary
  1895. * register is used since neither src_reg nor dst_reg can be
  1896. * overridden.
  1897. */
  1898. if (type == BPF_WRITE) {
  1899. int treg = BPF_REG_9;
  1900. if (si->src_reg == treg || si->dst_reg == treg)
  1901. --treg;
  1902. if (si->src_reg == treg || si->dst_reg == treg)
  1903. --treg;
  1904. *insn++ = BPF_STX_MEM(
  1905. BPF_DW, si->dst_reg, treg,
  1906. offsetof(struct bpf_sysctl_kern, tmp_reg));
  1907. *insn++ = BPF_LDX_MEM(
  1908. BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
  1909. treg, si->dst_reg,
  1910. offsetof(struct bpf_sysctl_kern, ppos));
  1911. *insn++ = BPF_STX_MEM(
  1912. BPF_SIZEOF(u32), treg, si->src_reg,
  1913. bpf_ctx_narrow_access_offset(
  1914. 0, sizeof(u32), sizeof(loff_t)));
  1915. *insn++ = BPF_LDX_MEM(
  1916. BPF_DW, treg, si->dst_reg,
  1917. offsetof(struct bpf_sysctl_kern, tmp_reg));
  1918. } else {
  1919. *insn++ = BPF_LDX_MEM(
  1920. BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
  1921. si->dst_reg, si->src_reg,
  1922. offsetof(struct bpf_sysctl_kern, ppos));
  1923. read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
  1924. *insn++ = BPF_LDX_MEM(
  1925. BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
  1926. bpf_ctx_narrow_access_offset(
  1927. 0, read_size, sizeof(loff_t)));
  1928. }
  1929. *target_size = sizeof(u32);
  1930. break;
  1931. }
  1932. return insn - insn_buf;
  1933. }
  1934. const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
  1935. .get_func_proto = sysctl_func_proto,
  1936. .is_valid_access = sysctl_is_valid_access,
  1937. .convert_ctx_access = sysctl_convert_ctx_access,
  1938. };
  1939. const struct bpf_prog_ops cg_sysctl_prog_ops = {
  1940. };
  1941. #ifdef CONFIG_NET
  1942. BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx)
  1943. {
  1944. const struct net *net = ctx ? sock_net(ctx->sk) : &init_net;
  1945. return net->net_cookie;
  1946. }
  1947. static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = {
  1948. .func = bpf_get_netns_cookie_sockopt,
  1949. .gpl_only = false,
  1950. .ret_type = RET_INTEGER,
  1951. .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
  1952. };
  1953. #endif
  1954. static const struct bpf_func_proto *
  1955. cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1956. {
  1957. const struct bpf_func_proto *func_proto;
  1958. func_proto = cgroup_common_func_proto(func_id, prog);
  1959. if (func_proto)
  1960. return func_proto;
  1961. func_proto = cgroup_current_func_proto(func_id, prog);
  1962. if (func_proto)
  1963. return func_proto;
  1964. switch (func_id) {
  1965. #ifdef CONFIG_NET
  1966. case BPF_FUNC_get_netns_cookie:
  1967. return &bpf_get_netns_cookie_sockopt_proto;
  1968. case BPF_FUNC_sk_storage_get:
  1969. return &bpf_sk_storage_get_proto;
  1970. case BPF_FUNC_sk_storage_delete:
  1971. return &bpf_sk_storage_delete_proto;
  1972. case BPF_FUNC_setsockopt:
  1973. if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
  1974. return &bpf_sk_setsockopt_proto;
  1975. return NULL;
  1976. case BPF_FUNC_getsockopt:
  1977. if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
  1978. return &bpf_sk_getsockopt_proto;
  1979. return NULL;
  1980. #endif
  1981. #ifdef CONFIG_INET
  1982. case BPF_FUNC_tcp_sock:
  1983. return &bpf_tcp_sock_proto;
  1984. #endif
  1985. case BPF_FUNC_perf_event_output:
  1986. return &bpf_event_output_data_proto;
  1987. default:
  1988. return bpf_base_func_proto(func_id);
  1989. }
  1990. }
  1991. static bool cg_sockopt_is_valid_access(int off, int size,
  1992. enum bpf_access_type type,
  1993. const struct bpf_prog *prog,
  1994. struct bpf_insn_access_aux *info)
  1995. {
  1996. const int size_default = sizeof(__u32);
  1997. if (off < 0 || off >= sizeof(struct bpf_sockopt))
  1998. return false;
  1999. if (off % size != 0)
  2000. return false;
  2001. if (type == BPF_WRITE) {
  2002. switch (off) {
  2003. case offsetof(struct bpf_sockopt, retval):
  2004. if (size != size_default)
  2005. return false;
  2006. return prog->expected_attach_type ==
  2007. BPF_CGROUP_GETSOCKOPT;
  2008. case offsetof(struct bpf_sockopt, optname):
  2009. fallthrough;
  2010. case offsetof(struct bpf_sockopt, level):
  2011. if (size != size_default)
  2012. return false;
  2013. return prog->expected_attach_type ==
  2014. BPF_CGROUP_SETSOCKOPT;
  2015. case offsetof(struct bpf_sockopt, optlen):
  2016. return size == size_default;
  2017. default:
  2018. return false;
  2019. }
  2020. }
  2021. switch (off) {
  2022. case offsetof(struct bpf_sockopt, sk):
  2023. if (size != sizeof(__u64))
  2024. return false;
  2025. info->reg_type = PTR_TO_SOCKET;
  2026. break;
  2027. case offsetof(struct bpf_sockopt, optval):
  2028. if (size != sizeof(__u64))
  2029. return false;
  2030. info->reg_type = PTR_TO_PACKET;
  2031. break;
  2032. case offsetof(struct bpf_sockopt, optval_end):
  2033. if (size != sizeof(__u64))
  2034. return false;
  2035. info->reg_type = PTR_TO_PACKET_END;
  2036. break;
  2037. case offsetof(struct bpf_sockopt, retval):
  2038. if (size != size_default)
  2039. return false;
  2040. return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
  2041. default:
  2042. if (size != size_default)
  2043. return false;
  2044. break;
  2045. }
  2046. return true;
  2047. }
  2048. #define CG_SOCKOPT_ACCESS_FIELD(T, F) \
  2049. T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
  2050. si->dst_reg, si->src_reg, \
  2051. offsetof(struct bpf_sockopt_kern, F))
  2052. static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
  2053. const struct bpf_insn *si,
  2054. struct bpf_insn *insn_buf,
  2055. struct bpf_prog *prog,
  2056. u32 *target_size)
  2057. {
  2058. struct bpf_insn *insn = insn_buf;
  2059. switch (si->off) {
  2060. case offsetof(struct bpf_sockopt, sk):
  2061. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
  2062. break;
  2063. case offsetof(struct bpf_sockopt, level):
  2064. if (type == BPF_WRITE)
  2065. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
  2066. else
  2067. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
  2068. break;
  2069. case offsetof(struct bpf_sockopt, optname):
  2070. if (type == BPF_WRITE)
  2071. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
  2072. else
  2073. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
  2074. break;
  2075. case offsetof(struct bpf_sockopt, optlen):
  2076. if (type == BPF_WRITE)
  2077. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
  2078. else
  2079. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
  2080. break;
  2081. case offsetof(struct bpf_sockopt, retval):
  2082. BUILD_BUG_ON(offsetof(struct bpf_cg_run_ctx, run_ctx) != 0);
  2083. if (type == BPF_WRITE) {
  2084. int treg = BPF_REG_9;
  2085. if (si->src_reg == treg || si->dst_reg == treg)
  2086. --treg;
  2087. if (si->src_reg == treg || si->dst_reg == treg)
  2088. --treg;
  2089. *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, treg,
  2090. offsetof(struct bpf_sockopt_kern, tmp_reg));
  2091. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
  2092. treg, si->dst_reg,
  2093. offsetof(struct bpf_sockopt_kern, current_task));
  2094. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
  2095. treg, treg,
  2096. offsetof(struct task_struct, bpf_ctx));
  2097. *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
  2098. treg, si->src_reg,
  2099. offsetof(struct bpf_cg_run_ctx, retval));
  2100. *insn++ = BPF_LDX_MEM(BPF_DW, treg, si->dst_reg,
  2101. offsetof(struct bpf_sockopt_kern, tmp_reg));
  2102. } else {
  2103. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
  2104. si->dst_reg, si->src_reg,
  2105. offsetof(struct bpf_sockopt_kern, current_task));
  2106. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
  2107. si->dst_reg, si->dst_reg,
  2108. offsetof(struct task_struct, bpf_ctx));
  2109. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
  2110. si->dst_reg, si->dst_reg,
  2111. offsetof(struct bpf_cg_run_ctx, retval));
  2112. }
  2113. break;
  2114. case offsetof(struct bpf_sockopt, optval):
  2115. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
  2116. break;
  2117. case offsetof(struct bpf_sockopt, optval_end):
  2118. *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
  2119. break;
  2120. }
  2121. return insn - insn_buf;
  2122. }
  2123. static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
  2124. bool direct_write,
  2125. const struct bpf_prog *prog)
  2126. {
  2127. /* Nothing to do for sockopt argument. The data is kzalloc'ated.
  2128. */
  2129. return 0;
  2130. }
  2131. const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
  2132. .get_func_proto = cg_sockopt_func_proto,
  2133. .is_valid_access = cg_sockopt_is_valid_access,
  2134. .convert_ctx_access = cg_sockopt_convert_ctx_access,
  2135. .gen_prologue = cg_sockopt_get_prologue,
  2136. };
  2137. const struct bpf_prog_ops cg_sockopt_prog_ops = {
  2138. };
  2139. /* Common helpers for cgroup hooks. */
  2140. const struct bpf_func_proto *
  2141. cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  2142. {
  2143. switch (func_id) {
  2144. case BPF_FUNC_get_local_storage:
  2145. return &bpf_get_local_storage_proto;
  2146. case BPF_FUNC_get_retval:
  2147. switch (prog->expected_attach_type) {
  2148. case BPF_CGROUP_INET_INGRESS:
  2149. case BPF_CGROUP_INET_EGRESS:
  2150. case BPF_CGROUP_SOCK_OPS:
  2151. case BPF_CGROUP_UDP4_RECVMSG:
  2152. case BPF_CGROUP_UDP6_RECVMSG:
  2153. case BPF_CGROUP_INET4_GETPEERNAME:
  2154. case BPF_CGROUP_INET6_GETPEERNAME:
  2155. case BPF_CGROUP_INET4_GETSOCKNAME:
  2156. case BPF_CGROUP_INET6_GETSOCKNAME:
  2157. return NULL;
  2158. default:
  2159. return &bpf_get_retval_proto;
  2160. }
  2161. case BPF_FUNC_set_retval:
  2162. switch (prog->expected_attach_type) {
  2163. case BPF_CGROUP_INET_INGRESS:
  2164. case BPF_CGROUP_INET_EGRESS:
  2165. case BPF_CGROUP_SOCK_OPS:
  2166. case BPF_CGROUP_UDP4_RECVMSG:
  2167. case BPF_CGROUP_UDP6_RECVMSG:
  2168. case BPF_CGROUP_INET4_GETPEERNAME:
  2169. case BPF_CGROUP_INET6_GETPEERNAME:
  2170. case BPF_CGROUP_INET4_GETSOCKNAME:
  2171. case BPF_CGROUP_INET6_GETSOCKNAME:
  2172. return NULL;
  2173. default:
  2174. return &bpf_set_retval_proto;
  2175. }
  2176. default:
  2177. return NULL;
  2178. }
  2179. }
  2180. /* Common helpers for cgroup hooks with valid process context. */
  2181. const struct bpf_func_proto *
  2182. cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  2183. {
  2184. switch (func_id) {
  2185. case BPF_FUNC_get_current_uid_gid:
  2186. return &bpf_get_current_uid_gid_proto;
  2187. case BPF_FUNC_get_current_pid_tgid:
  2188. return &bpf_get_current_pid_tgid_proto;
  2189. case BPF_FUNC_get_current_comm:
  2190. return &bpf_get_current_comm_proto;
  2191. case BPF_FUNC_get_current_cgroup_id:
  2192. return &bpf_get_current_cgroup_id_proto;
  2193. case BPF_FUNC_get_current_ancestor_cgroup_id:
  2194. return &bpf_get_current_ancestor_cgroup_id_proto;
  2195. #ifdef CONFIG_CGROUP_NET_CLASSID
  2196. case BPF_FUNC_get_cgroup_classid:
  2197. return &bpf_get_cgroup_classid_curr_proto;
  2198. #endif
  2199. default:
  2200. return NULL;
  2201. }
  2202. }