syscall.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  3. */
  4. #include <linux/bpf.h>
  5. #include <linux/bpf-cgroup.h>
  6. #include <linux/bpf_trace.h>
  7. #include <linux/bpf_lirc.h>
  8. #include <linux/bpf_verifier.h>
  9. #include <linux/bsearch.h>
  10. #include <linux/btf.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mmzone.h>
  16. #include <linux/anon_inodes.h>
  17. #include <linux/fdtable.h>
  18. #include <linux/file.h>
  19. #include <linux/fs.h>
  20. #include <linux/license.h>
  21. #include <linux/filter.h>
  22. #include <linux/kernel.h>
  23. #include <linux/idr.h>
  24. #include <linux/cred.h>
  25. #include <linux/timekeeping.h>
  26. #include <linux/ctype.h>
  27. #include <linux/nospec.h>
  28. #include <linux/audit.h>
  29. #include <uapi/linux/btf.h>
  30. #include <linux/pgtable.h>
  31. #include <linux/bpf_lsm.h>
  32. #include <linux/poll.h>
  33. #include <linux/sort.h>
  34. #include <linux/bpf-netns.h>
  35. #include <linux/rcupdate_trace.h>
  36. #include <linux/memcontrol.h>
  37. #include <linux/trace_events.h>
  38. #include <trace/hooks/syscall_check.h>
  39. #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
  40. (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
  41. (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  42. #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
  43. #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
  44. #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
  45. IS_FD_HASH(map))
  46. #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
  47. DEFINE_PER_CPU(int, bpf_prog_active);
  48. static DEFINE_IDR(prog_idr);
  49. static DEFINE_SPINLOCK(prog_idr_lock);
  50. static DEFINE_IDR(map_idr);
  51. static DEFINE_SPINLOCK(map_idr_lock);
  52. static DEFINE_IDR(link_idr);
  53. static DEFINE_SPINLOCK(link_idr_lock);
  54. int sysctl_unprivileged_bpf_disabled __read_mostly =
  55. IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
  56. static const struct bpf_map_ops * const bpf_map_types[] = {
  57. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
  58. #define BPF_MAP_TYPE(_id, _ops) \
  59. [_id] = &_ops,
  60. #define BPF_LINK_TYPE(_id, _name)
  61. #include <linux/bpf_types.h>
  62. #undef BPF_PROG_TYPE
  63. #undef BPF_MAP_TYPE
  64. #undef BPF_LINK_TYPE
  65. };
  66. /*
  67. * If we're handed a bigger struct than we know of, ensure all the unknown bits
  68. * are 0 - i.e. new user-space does not rely on any kernel feature extensions
  69. * we don't know about yet.
  70. *
  71. * There is a ToCToU between this function call and the following
  72. * copy_from_user() call. However, this is not a concern since this function is
  73. * meant to be a future-proofing of bits.
  74. */
  75. int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
  76. size_t expected_size,
  77. size_t actual_size)
  78. {
  79. int res;
  80. if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
  81. return -E2BIG;
  82. if (actual_size <= expected_size)
  83. return 0;
  84. if (uaddr.is_kernel)
  85. res = memchr_inv(uaddr.kernel + expected_size, 0,
  86. actual_size - expected_size) == NULL;
  87. else
  88. res = check_zeroed_user(uaddr.user + expected_size,
  89. actual_size - expected_size);
  90. if (res < 0)
  91. return res;
  92. return res ? 0 : -E2BIG;
  93. }
  94. const struct bpf_map_ops bpf_map_offload_ops = {
  95. .map_meta_equal = bpf_map_meta_equal,
  96. .map_alloc = bpf_map_offload_map_alloc,
  97. .map_free = bpf_map_offload_map_free,
  98. .map_check_btf = map_check_no_btf,
  99. };
  100. static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
  101. {
  102. const struct bpf_map_ops *ops;
  103. u32 type = attr->map_type;
  104. struct bpf_map *map;
  105. int err;
  106. if (type >= ARRAY_SIZE(bpf_map_types))
  107. return ERR_PTR(-EINVAL);
  108. type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
  109. ops = bpf_map_types[type];
  110. if (!ops)
  111. return ERR_PTR(-EINVAL);
  112. if (ops->map_alloc_check) {
  113. err = ops->map_alloc_check(attr);
  114. if (err)
  115. return ERR_PTR(err);
  116. }
  117. if (attr->map_ifindex)
  118. ops = &bpf_map_offload_ops;
  119. map = ops->map_alloc(attr);
  120. if (IS_ERR(map))
  121. return map;
  122. map->ops = ops;
  123. map->map_type = type;
  124. return map;
  125. }
  126. static void bpf_map_write_active_inc(struct bpf_map *map)
  127. {
  128. atomic64_inc(&map->writecnt);
  129. }
  130. static void bpf_map_write_active_dec(struct bpf_map *map)
  131. {
  132. atomic64_dec(&map->writecnt);
  133. }
  134. bool bpf_map_write_active(const struct bpf_map *map)
  135. {
  136. return atomic64_read(&map->writecnt) != 0;
  137. }
  138. static u32 bpf_map_value_size(const struct bpf_map *map)
  139. {
  140. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  141. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  142. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
  143. map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
  144. return round_up(map->value_size, 8) * num_possible_cpus();
  145. else if (IS_FD_MAP(map))
  146. return sizeof(u32);
  147. else
  148. return map->value_size;
  149. }
  150. static void maybe_wait_bpf_programs(struct bpf_map *map)
  151. {
  152. /* Wait for any running BPF programs to complete so that
  153. * userspace, when we return to it, knows that all programs
  154. * that could be running use the new map value.
  155. */
  156. if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
  157. map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
  158. synchronize_rcu();
  159. }
  160. static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
  161. void *value, __u64 flags)
  162. {
  163. int err;
  164. /* Need to create a kthread, thus must support schedule */
  165. if (bpf_map_is_dev_bound(map)) {
  166. return bpf_map_offload_update_elem(map, key, value, flags);
  167. } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
  168. map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
  169. return map->ops->map_update_elem(map, key, value, flags);
  170. } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
  171. map->map_type == BPF_MAP_TYPE_SOCKMAP) {
  172. return sock_map_update_elem_sys(map, key, value, flags);
  173. } else if (IS_FD_PROG_ARRAY(map)) {
  174. return bpf_fd_array_map_update_elem(map, f.file, key, value,
  175. flags);
  176. }
  177. bpf_disable_instrumentation();
  178. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  179. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  180. err = bpf_percpu_hash_update(map, key, value, flags);
  181. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  182. err = bpf_percpu_array_update(map, key, value, flags);
  183. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
  184. err = bpf_percpu_cgroup_storage_update(map, key, value,
  185. flags);
  186. } else if (IS_FD_ARRAY(map)) {
  187. rcu_read_lock();
  188. err = bpf_fd_array_map_update_elem(map, f.file, key, value,
  189. flags);
  190. rcu_read_unlock();
  191. } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  192. rcu_read_lock();
  193. err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
  194. flags);
  195. rcu_read_unlock();
  196. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  197. /* rcu_read_lock() is not needed */
  198. err = bpf_fd_reuseport_array_update_elem(map, key, value,
  199. flags);
  200. } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
  201. map->map_type == BPF_MAP_TYPE_STACK ||
  202. map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
  203. err = map->ops->map_push_elem(map, value, flags);
  204. } else {
  205. rcu_read_lock();
  206. err = map->ops->map_update_elem(map, key, value, flags);
  207. rcu_read_unlock();
  208. }
  209. bpf_enable_instrumentation();
  210. maybe_wait_bpf_programs(map);
  211. return err;
  212. }
  213. static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
  214. __u64 flags)
  215. {
  216. void *ptr;
  217. int err;
  218. if (bpf_map_is_dev_bound(map))
  219. return bpf_map_offload_lookup_elem(map, key, value);
  220. bpf_disable_instrumentation();
  221. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  222. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  223. err = bpf_percpu_hash_copy(map, key, value);
  224. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
  225. err = bpf_percpu_array_copy(map, key, value);
  226. } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
  227. err = bpf_percpu_cgroup_storage_copy(map, key, value);
  228. } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
  229. err = bpf_stackmap_copy(map, key, value);
  230. } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
  231. err = bpf_fd_array_map_lookup_elem(map, key, value);
  232. } else if (IS_FD_HASH(map)) {
  233. err = bpf_fd_htab_map_lookup_elem(map, key, value);
  234. } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
  235. err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
  236. } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
  237. map->map_type == BPF_MAP_TYPE_STACK ||
  238. map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
  239. err = map->ops->map_peek_elem(map, value);
  240. } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
  241. /* struct_ops map requires directly updating "value" */
  242. err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
  243. } else {
  244. rcu_read_lock();
  245. if (map->ops->map_lookup_elem_sys_only)
  246. ptr = map->ops->map_lookup_elem_sys_only(map, key);
  247. else
  248. ptr = map->ops->map_lookup_elem(map, key);
  249. if (IS_ERR(ptr)) {
  250. err = PTR_ERR(ptr);
  251. } else if (!ptr) {
  252. err = -ENOENT;
  253. } else {
  254. err = 0;
  255. if (flags & BPF_F_LOCK)
  256. /* lock 'ptr' and copy everything but lock */
  257. copy_map_value_locked(map, value, ptr, true);
  258. else
  259. copy_map_value(map, value, ptr);
  260. /* mask lock and timer, since value wasn't zero inited */
  261. check_and_init_map_value(map, value);
  262. }
  263. rcu_read_unlock();
  264. }
  265. bpf_enable_instrumentation();
  266. maybe_wait_bpf_programs(map);
  267. return err;
  268. }
  269. /* Please, do not use this function outside from the map creation path
  270. * (e.g. in map update path) without taking care of setting the active
  271. * memory cgroup (see at bpf_map_kmalloc_node() for example).
  272. */
  273. static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
  274. {
  275. /* We really just want to fail instead of triggering OOM killer
  276. * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
  277. * which is used for lower order allocation requests.
  278. *
  279. * It has been observed that higher order allocation requests done by
  280. * vmalloc with __GFP_NORETRY being set might fail due to not trying
  281. * to reclaim memory from the page cache, thus we set
  282. * __GFP_RETRY_MAYFAIL to avoid such situations.
  283. */
  284. const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
  285. unsigned int flags = 0;
  286. unsigned long align = 1;
  287. void *area;
  288. if (size >= SIZE_MAX)
  289. return NULL;
  290. /* kmalloc()'ed memory can't be mmap()'ed */
  291. if (mmapable) {
  292. BUG_ON(!PAGE_ALIGNED(size));
  293. align = SHMLBA;
  294. flags = VM_USERMAP;
  295. } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  296. area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
  297. numa_node);
  298. if (area != NULL)
  299. return area;
  300. }
  301. return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
  302. gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
  303. flags, numa_node, __builtin_return_address(0));
  304. }
  305. void *bpf_map_area_alloc(u64 size, int numa_node)
  306. {
  307. return __bpf_map_area_alloc(size, numa_node, false);
  308. }
  309. void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
  310. {
  311. return __bpf_map_area_alloc(size, numa_node, true);
  312. }
  313. void bpf_map_area_free(void *area)
  314. {
  315. kvfree(area);
  316. }
  317. static u32 bpf_map_flags_retain_permanent(u32 flags)
  318. {
  319. /* Some map creation flags are not tied to the map object but
  320. * rather to the map fd instead, so they have no meaning upon
  321. * map object inspection since multiple file descriptors with
  322. * different (access) properties can exist here. Thus, given
  323. * this has zero meaning for the map itself, lets clear these
  324. * from here.
  325. */
  326. return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
  327. }
  328. void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
  329. {
  330. map->map_type = attr->map_type;
  331. map->key_size = attr->key_size;
  332. map->value_size = attr->value_size;
  333. map->max_entries = attr->max_entries;
  334. map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
  335. map->numa_node = bpf_map_attr_numa_node(attr);
  336. map->map_extra = attr->map_extra;
  337. }
  338. static int bpf_map_alloc_id(struct bpf_map *map)
  339. {
  340. int id;
  341. idr_preload(GFP_KERNEL);
  342. spin_lock_bh(&map_idr_lock);
  343. id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
  344. if (id > 0)
  345. map->id = id;
  346. spin_unlock_bh(&map_idr_lock);
  347. idr_preload_end();
  348. if (WARN_ON_ONCE(!id))
  349. return -ENOSPC;
  350. return id > 0 ? 0 : id;
  351. }
  352. void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
  353. {
  354. unsigned long flags;
  355. /* Offloaded maps are removed from the IDR store when their device
  356. * disappears - even if someone holds an fd to them they are unusable,
  357. * the memory is gone, all ops will fail; they are simply waiting for
  358. * refcnt to drop to be freed.
  359. */
  360. if (!map->id)
  361. return;
  362. if (do_idr_lock)
  363. spin_lock_irqsave(&map_idr_lock, flags);
  364. else
  365. __acquire(&map_idr_lock);
  366. idr_remove(&map_idr, map->id);
  367. map->id = 0;
  368. if (do_idr_lock)
  369. spin_unlock_irqrestore(&map_idr_lock, flags);
  370. else
  371. __release(&map_idr_lock);
  372. }
  373. #ifdef CONFIG_MEMCG_KMEM
  374. static void bpf_map_save_memcg(struct bpf_map *map)
  375. {
  376. /* Currently if a map is created by a process belonging to the root
  377. * memory cgroup, get_obj_cgroup_from_current() will return NULL.
  378. * So we have to check map->objcg for being NULL each time it's
  379. * being used.
  380. */
  381. map->objcg = get_obj_cgroup_from_current();
  382. }
  383. static void bpf_map_release_memcg(struct bpf_map *map)
  384. {
  385. if (map->objcg)
  386. obj_cgroup_put(map->objcg);
  387. }
  388. static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
  389. {
  390. if (map->objcg)
  391. return get_mem_cgroup_from_objcg(map->objcg);
  392. return root_mem_cgroup;
  393. }
  394. void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
  395. int node)
  396. {
  397. struct mem_cgroup *memcg, *old_memcg;
  398. void *ptr;
  399. memcg = bpf_map_get_memcg(map);
  400. old_memcg = set_active_memcg(memcg);
  401. ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
  402. set_active_memcg(old_memcg);
  403. mem_cgroup_put(memcg);
  404. return ptr;
  405. }
  406. void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
  407. {
  408. struct mem_cgroup *memcg, *old_memcg;
  409. void *ptr;
  410. memcg = bpf_map_get_memcg(map);
  411. old_memcg = set_active_memcg(memcg);
  412. ptr = kzalloc(size, flags | __GFP_ACCOUNT);
  413. set_active_memcg(old_memcg);
  414. mem_cgroup_put(memcg);
  415. return ptr;
  416. }
  417. void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
  418. size_t align, gfp_t flags)
  419. {
  420. struct mem_cgroup *memcg, *old_memcg;
  421. void __percpu *ptr;
  422. memcg = bpf_map_get_memcg(map);
  423. old_memcg = set_active_memcg(memcg);
  424. ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
  425. set_active_memcg(old_memcg);
  426. mem_cgroup_put(memcg);
  427. return ptr;
  428. }
  429. #else
  430. static void bpf_map_save_memcg(struct bpf_map *map)
  431. {
  432. }
  433. static void bpf_map_release_memcg(struct bpf_map *map)
  434. {
  435. }
  436. #endif
  437. static int bpf_map_kptr_off_cmp(const void *a, const void *b)
  438. {
  439. const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;
  440. if (off_desc1->offset < off_desc2->offset)
  441. return -1;
  442. else if (off_desc1->offset > off_desc2->offset)
  443. return 1;
  444. return 0;
  445. }
  446. struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset)
  447. {
  448. /* Since members are iterated in btf_find_field in increasing order,
  449. * offsets appended to kptr_off_tab are in increasing order, so we can
  450. * do bsearch to find exact match.
  451. */
  452. struct bpf_map_value_off *tab;
  453. if (!map_value_has_kptrs(map))
  454. return NULL;
  455. tab = map->kptr_off_tab;
  456. return bsearch(&offset, tab->off, tab->nr_off, sizeof(tab->off[0]), bpf_map_kptr_off_cmp);
  457. }
  458. void bpf_map_free_kptr_off_tab(struct bpf_map *map)
  459. {
  460. struct bpf_map_value_off *tab = map->kptr_off_tab;
  461. int i;
  462. if (!map_value_has_kptrs(map))
  463. return;
  464. for (i = 0; i < tab->nr_off; i++) {
  465. if (tab->off[i].kptr.module)
  466. module_put(tab->off[i].kptr.module);
  467. btf_put(tab->off[i].kptr.btf);
  468. }
  469. kfree(tab);
  470. map->kptr_off_tab = NULL;
  471. }
  472. struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map)
  473. {
  474. struct bpf_map_value_off *tab = map->kptr_off_tab, *new_tab;
  475. int size, i;
  476. if (!map_value_has_kptrs(map))
  477. return ERR_PTR(-ENOENT);
  478. size = offsetof(struct bpf_map_value_off, off[tab->nr_off]);
  479. new_tab = kmemdup(tab, size, GFP_KERNEL | __GFP_NOWARN);
  480. if (!new_tab)
  481. return ERR_PTR(-ENOMEM);
  482. /* Do a deep copy of the kptr_off_tab */
  483. for (i = 0; i < tab->nr_off; i++) {
  484. btf_get(tab->off[i].kptr.btf);
  485. if (tab->off[i].kptr.module && !try_module_get(tab->off[i].kptr.module)) {
  486. while (i--) {
  487. if (tab->off[i].kptr.module)
  488. module_put(tab->off[i].kptr.module);
  489. btf_put(tab->off[i].kptr.btf);
  490. }
  491. kfree(new_tab);
  492. return ERR_PTR(-ENXIO);
  493. }
  494. }
  495. return new_tab;
  496. }
  497. bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b)
  498. {
  499. struct bpf_map_value_off *tab_a = map_a->kptr_off_tab, *tab_b = map_b->kptr_off_tab;
  500. bool a_has_kptr = map_value_has_kptrs(map_a), b_has_kptr = map_value_has_kptrs(map_b);
  501. int size;
  502. if (!a_has_kptr && !b_has_kptr)
  503. return true;
  504. if (a_has_kptr != b_has_kptr)
  505. return false;
  506. if (tab_a->nr_off != tab_b->nr_off)
  507. return false;
  508. size = offsetof(struct bpf_map_value_off, off[tab_a->nr_off]);
  509. return !memcmp(tab_a, tab_b, size);
  510. }
  511. /* Caller must ensure map_value_has_kptrs is true. Note that this function can
  512. * be called on a map value while the map_value is visible to BPF programs, as
  513. * it ensures the correct synchronization, and we already enforce the same using
  514. * the bpf_kptr_xchg helper on the BPF program side for referenced kptrs.
  515. */
  516. void bpf_map_free_kptrs(struct bpf_map *map, void *map_value)
  517. {
  518. struct bpf_map_value_off *tab = map->kptr_off_tab;
  519. unsigned long *btf_id_ptr;
  520. int i;
  521. for (i = 0; i < tab->nr_off; i++) {
  522. struct bpf_map_value_off_desc *off_desc = &tab->off[i];
  523. unsigned long old_ptr;
  524. btf_id_ptr = map_value + off_desc->offset;
  525. if (off_desc->type == BPF_KPTR_UNREF) {
  526. u64 *p = (u64 *)btf_id_ptr;
  527. WRITE_ONCE(*p, 0);
  528. continue;
  529. }
  530. old_ptr = xchg(btf_id_ptr, 0);
  531. off_desc->kptr.dtor((void *)old_ptr);
  532. }
  533. }
  534. /* called from workqueue */
  535. static void bpf_map_free_deferred(struct work_struct *work)
  536. {
  537. struct bpf_map *map = container_of(work, struct bpf_map, work);
  538. security_bpf_map_free(map);
  539. kfree(map->off_arr);
  540. bpf_map_release_memcg(map);
  541. /* implementation dependent freeing, map_free callback also does
  542. * bpf_map_free_kptr_off_tab, if needed.
  543. */
  544. map->ops->map_free(map);
  545. }
  546. static void bpf_map_put_uref(struct bpf_map *map)
  547. {
  548. if (atomic64_dec_and_test(&map->usercnt)) {
  549. if (map->ops->map_release_uref)
  550. map->ops->map_release_uref(map);
  551. }
  552. }
  553. /* decrement map refcnt and schedule it for freeing via workqueue
  554. * (unrelying map implementation ops->map_free() might sleep)
  555. */
  556. static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
  557. {
  558. if (atomic64_dec_and_test(&map->refcnt)) {
  559. /* bpf_map_free_id() must be called first */
  560. bpf_map_free_id(map, do_idr_lock);
  561. btf_put(map->btf);
  562. INIT_WORK(&map->work, bpf_map_free_deferred);
  563. /* Avoid spawning kworkers, since they all might contend
  564. * for the same mutex like slab_mutex.
  565. */
  566. queue_work(system_unbound_wq, &map->work);
  567. }
  568. }
  569. void bpf_map_put(struct bpf_map *map)
  570. {
  571. __bpf_map_put(map, true);
  572. }
  573. EXPORT_SYMBOL_GPL(bpf_map_put);
  574. void bpf_map_put_with_uref(struct bpf_map *map)
  575. {
  576. bpf_map_put_uref(map);
  577. bpf_map_put(map);
  578. }
  579. static int bpf_map_release(struct inode *inode, struct file *filp)
  580. {
  581. struct bpf_map *map = filp->private_data;
  582. if (map->ops->map_release)
  583. map->ops->map_release(map, filp);
  584. bpf_map_put_with_uref(map);
  585. return 0;
  586. }
  587. static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
  588. {
  589. fmode_t mode = f.file->f_mode;
  590. /* Our file permissions may have been overridden by global
  591. * map permissions facing syscall side.
  592. */
  593. if (READ_ONCE(map->frozen))
  594. mode &= ~FMODE_CAN_WRITE;
  595. return mode;
  596. }
  597. #ifdef CONFIG_PROC_FS
  598. /* Provides an approximation of the map's memory footprint.
  599. * Used only to provide a backward compatibility and display
  600. * a reasonable "memlock" info.
  601. */
  602. static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
  603. {
  604. unsigned long size;
  605. size = round_up(map->key_size + bpf_map_value_size(map), 8);
  606. return round_up(map->max_entries * size, PAGE_SIZE);
  607. }
  608. static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
  609. {
  610. struct bpf_map *map = filp->private_data;
  611. u32 type = 0, jited = 0;
  612. if (map_type_contains_progs(map)) {
  613. spin_lock(&map->owner.lock);
  614. type = map->owner.type;
  615. jited = map->owner.jited;
  616. spin_unlock(&map->owner.lock);
  617. }
  618. seq_printf(m,
  619. "map_type:\t%u\n"
  620. "key_size:\t%u\n"
  621. "value_size:\t%u\n"
  622. "max_entries:\t%u\n"
  623. "map_flags:\t%#x\n"
  624. "map_extra:\t%#llx\n"
  625. "memlock:\t%lu\n"
  626. "map_id:\t%u\n"
  627. "frozen:\t%u\n",
  628. map->map_type,
  629. map->key_size,
  630. map->value_size,
  631. map->max_entries,
  632. map->map_flags,
  633. (unsigned long long)map->map_extra,
  634. bpf_map_memory_footprint(map),
  635. map->id,
  636. READ_ONCE(map->frozen));
  637. if (type) {
  638. seq_printf(m, "owner_prog_type:\t%u\n", type);
  639. seq_printf(m, "owner_jited:\t%u\n", jited);
  640. }
  641. }
  642. #endif
  643. static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
  644. loff_t *ppos)
  645. {
  646. /* We need this handler such that alloc_file() enables
  647. * f_mode with FMODE_CAN_READ.
  648. */
  649. return -EINVAL;
  650. }
  651. static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
  652. size_t siz, loff_t *ppos)
  653. {
  654. /* We need this handler such that alloc_file() enables
  655. * f_mode with FMODE_CAN_WRITE.
  656. */
  657. return -EINVAL;
  658. }
  659. /* called for any extra memory-mapped regions (except initial) */
  660. static void bpf_map_mmap_open(struct vm_area_struct *vma)
  661. {
  662. struct bpf_map *map = vma->vm_file->private_data;
  663. if (vma->vm_flags & VM_MAYWRITE)
  664. bpf_map_write_active_inc(map);
  665. }
  666. /* called for all unmapped memory region (including initial) */
  667. static void bpf_map_mmap_close(struct vm_area_struct *vma)
  668. {
  669. struct bpf_map *map = vma->vm_file->private_data;
  670. if (vma->vm_flags & VM_MAYWRITE)
  671. bpf_map_write_active_dec(map);
  672. }
  673. static const struct vm_operations_struct bpf_map_default_vmops = {
  674. .open = bpf_map_mmap_open,
  675. .close = bpf_map_mmap_close,
  676. };
  677. static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
  678. {
  679. struct bpf_map *map = filp->private_data;
  680. int err;
  681. if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
  682. map_value_has_timer(map) || map_value_has_kptrs(map))
  683. return -ENOTSUPP;
  684. if (!(vma->vm_flags & VM_SHARED))
  685. return -EINVAL;
  686. mutex_lock(&map->freeze_mutex);
  687. if (vma->vm_flags & VM_WRITE) {
  688. if (map->frozen) {
  689. err = -EPERM;
  690. goto out;
  691. }
  692. /* map is meant to be read-only, so do not allow mapping as
  693. * writable, because it's possible to leak a writable page
  694. * reference and allows user-space to still modify it after
  695. * freezing, while verifier will assume contents do not change
  696. */
  697. if (map->map_flags & BPF_F_RDONLY_PROG) {
  698. err = -EACCES;
  699. goto out;
  700. }
  701. }
  702. /* set default open/close callbacks */
  703. vma->vm_ops = &bpf_map_default_vmops;
  704. vma->vm_private_data = map;
  705. vm_flags_clear(vma, VM_MAYEXEC);
  706. if (!(vma->vm_flags & VM_WRITE))
  707. /* disallow re-mapping with PROT_WRITE */
  708. vm_flags_clear(vma, VM_MAYWRITE);
  709. err = map->ops->map_mmap(map, vma);
  710. if (err)
  711. goto out;
  712. if (vma->vm_flags & VM_MAYWRITE)
  713. bpf_map_write_active_inc(map);
  714. out:
  715. mutex_unlock(&map->freeze_mutex);
  716. return err;
  717. }
  718. static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
  719. {
  720. struct bpf_map *map = filp->private_data;
  721. if (map->ops->map_poll)
  722. return map->ops->map_poll(map, filp, pts);
  723. return EPOLLERR;
  724. }
  725. const struct file_operations bpf_map_fops = {
  726. #ifdef CONFIG_PROC_FS
  727. .show_fdinfo = bpf_map_show_fdinfo,
  728. #endif
  729. .release = bpf_map_release,
  730. .read = bpf_dummy_read,
  731. .write = bpf_dummy_write,
  732. .mmap = bpf_map_mmap,
  733. .poll = bpf_map_poll,
  734. };
  735. int bpf_map_new_fd(struct bpf_map *map, int flags)
  736. {
  737. int ret;
  738. ret = security_bpf_map(map, OPEN_FMODE(flags));
  739. if (ret < 0)
  740. return ret;
  741. return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
  742. flags | O_CLOEXEC);
  743. }
  744. int bpf_get_file_flag(int flags)
  745. {
  746. if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
  747. return -EINVAL;
  748. if (flags & BPF_F_RDONLY)
  749. return O_RDONLY;
  750. if (flags & BPF_F_WRONLY)
  751. return O_WRONLY;
  752. return O_RDWR;
  753. }
  754. /* helper macro to check that unused fields 'union bpf_attr' are zero */
  755. #define CHECK_ATTR(CMD) \
  756. memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
  757. sizeof(attr->CMD##_LAST_FIELD), 0, \
  758. sizeof(*attr) - \
  759. offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
  760. sizeof(attr->CMD##_LAST_FIELD)) != NULL
  761. /* dst and src must have at least "size" number of bytes.
  762. * Return strlen on success and < 0 on error.
  763. */
  764. int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
  765. {
  766. const char *end = src + size;
  767. const char *orig_src = src;
  768. memset(dst, 0, size);
  769. /* Copy all isalnum(), '_' and '.' chars. */
  770. while (src < end && *src) {
  771. if (!isalnum(*src) &&
  772. *src != '_' && *src != '.')
  773. return -EINVAL;
  774. *dst++ = *src++;
  775. }
  776. /* No '\0' found in "size" number of bytes */
  777. if (src == end)
  778. return -EINVAL;
  779. return src - orig_src;
  780. }
  781. int map_check_no_btf(const struct bpf_map *map,
  782. const struct btf *btf,
  783. const struct btf_type *key_type,
  784. const struct btf_type *value_type)
  785. {
  786. return -ENOTSUPP;
  787. }
  788. static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
  789. {
  790. const u32 a = *(const u32 *)_a;
  791. const u32 b = *(const u32 *)_b;
  792. if (a < b)
  793. return -1;
  794. else if (a > b)
  795. return 1;
  796. return 0;
  797. }
  798. static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
  799. {
  800. struct bpf_map *map = (struct bpf_map *)priv;
  801. u32 *off_base = map->off_arr->field_off;
  802. u32 *a = _a, *b = _b;
  803. u8 *sz_a, *sz_b;
  804. sz_a = map->off_arr->field_sz + (a - off_base);
  805. sz_b = map->off_arr->field_sz + (b - off_base);
  806. swap(*a, *b);
  807. swap(*sz_a, *sz_b);
  808. }
  809. static int bpf_map_alloc_off_arr(struct bpf_map *map)
  810. {
  811. bool has_spin_lock = map_value_has_spin_lock(map);
  812. bool has_timer = map_value_has_timer(map);
  813. bool has_kptrs = map_value_has_kptrs(map);
  814. struct bpf_map_off_arr *off_arr;
  815. u32 i;
  816. if (!has_spin_lock && !has_timer && !has_kptrs) {
  817. map->off_arr = NULL;
  818. return 0;
  819. }
  820. off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
  821. if (!off_arr)
  822. return -ENOMEM;
  823. map->off_arr = off_arr;
  824. off_arr->cnt = 0;
  825. if (has_spin_lock) {
  826. i = off_arr->cnt;
  827. off_arr->field_off[i] = map->spin_lock_off;
  828. off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
  829. off_arr->cnt++;
  830. }
  831. if (has_timer) {
  832. i = off_arr->cnt;
  833. off_arr->field_off[i] = map->timer_off;
  834. off_arr->field_sz[i] = sizeof(struct bpf_timer);
  835. off_arr->cnt++;
  836. }
  837. if (has_kptrs) {
  838. struct bpf_map_value_off *tab = map->kptr_off_tab;
  839. u32 *off = &off_arr->field_off[off_arr->cnt];
  840. u8 *sz = &off_arr->field_sz[off_arr->cnt];
  841. for (i = 0; i < tab->nr_off; i++) {
  842. *off++ = tab->off[i].offset;
  843. *sz++ = sizeof(u64);
  844. }
  845. off_arr->cnt += tab->nr_off;
  846. }
  847. if (off_arr->cnt == 1)
  848. return 0;
  849. sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
  850. map_off_arr_cmp, map_off_arr_swap, map);
  851. return 0;
  852. }
  853. static int map_check_btf(struct bpf_map *map, const struct btf *btf,
  854. u32 btf_key_id, u32 btf_value_id)
  855. {
  856. const struct btf_type *key_type, *value_type;
  857. u32 key_size, value_size;
  858. int ret = 0;
  859. /* Some maps allow key to be unspecified. */
  860. if (btf_key_id) {
  861. key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
  862. if (!key_type || key_size != map->key_size)
  863. return -EINVAL;
  864. } else {
  865. key_type = btf_type_by_id(btf, 0);
  866. if (!map->ops->map_check_btf)
  867. return -EINVAL;
  868. }
  869. value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
  870. if (!value_type || value_size != map->value_size)
  871. return -EINVAL;
  872. map->spin_lock_off = btf_find_spin_lock(btf, value_type);
  873. if (map_value_has_spin_lock(map)) {
  874. if (map->map_flags & BPF_F_RDONLY_PROG)
  875. return -EACCES;
  876. if (map->map_type != BPF_MAP_TYPE_HASH &&
  877. map->map_type != BPF_MAP_TYPE_ARRAY &&
  878. map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
  879. map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
  880. map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
  881. map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
  882. return -ENOTSUPP;
  883. if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
  884. map->value_size) {
  885. WARN_ONCE(1,
  886. "verifier bug spin_lock_off %d value_size %d\n",
  887. map->spin_lock_off, map->value_size);
  888. return -EFAULT;
  889. }
  890. }
  891. map->timer_off = btf_find_timer(btf, value_type);
  892. if (map_value_has_timer(map)) {
  893. if (map->map_flags & BPF_F_RDONLY_PROG)
  894. return -EACCES;
  895. if (map->map_type != BPF_MAP_TYPE_HASH &&
  896. map->map_type != BPF_MAP_TYPE_LRU_HASH &&
  897. map->map_type != BPF_MAP_TYPE_ARRAY)
  898. return -EOPNOTSUPP;
  899. }
  900. map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
  901. if (map_value_has_kptrs(map)) {
  902. if (!bpf_capable()) {
  903. ret = -EPERM;
  904. goto free_map_tab;
  905. }
  906. if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
  907. ret = -EACCES;
  908. goto free_map_tab;
  909. }
  910. if (map->map_type != BPF_MAP_TYPE_HASH &&
  911. map->map_type != BPF_MAP_TYPE_LRU_HASH &&
  912. map->map_type != BPF_MAP_TYPE_ARRAY &&
  913. map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY) {
  914. ret = -EOPNOTSUPP;
  915. goto free_map_tab;
  916. }
  917. }
  918. if (map->ops->map_check_btf) {
  919. ret = map->ops->map_check_btf(map, btf, key_type, value_type);
  920. if (ret < 0)
  921. goto free_map_tab;
  922. }
  923. return ret;
  924. free_map_tab:
  925. bpf_map_free_kptr_off_tab(map);
  926. return ret;
  927. }
  928. #define BPF_MAP_CREATE_LAST_FIELD map_extra
  929. /* called via syscall */
  930. static int map_create(union bpf_attr *attr)
  931. {
  932. int numa_node = bpf_map_attr_numa_node(attr);
  933. struct bpf_map *map;
  934. int f_flags;
  935. int err;
  936. err = CHECK_ATTR(BPF_MAP_CREATE);
  937. if (err)
  938. return -EINVAL;
  939. if (attr->btf_vmlinux_value_type_id) {
  940. if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
  941. attr->btf_key_type_id || attr->btf_value_type_id)
  942. return -EINVAL;
  943. } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
  944. return -EINVAL;
  945. }
  946. if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
  947. attr->map_extra != 0)
  948. return -EINVAL;
  949. f_flags = bpf_get_file_flag(attr->map_flags);
  950. if (f_flags < 0)
  951. return f_flags;
  952. if (numa_node != NUMA_NO_NODE &&
  953. ((unsigned int)numa_node >= nr_node_ids ||
  954. !node_online(numa_node)))
  955. return -EINVAL;
  956. /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
  957. map = find_and_alloc_map(attr);
  958. if (IS_ERR(map))
  959. return PTR_ERR(map);
  960. err = bpf_obj_name_cpy(map->name, attr->map_name,
  961. sizeof(attr->map_name));
  962. if (err < 0)
  963. goto free_map;
  964. atomic64_set(&map->refcnt, 1);
  965. atomic64_set(&map->usercnt, 1);
  966. mutex_init(&map->freeze_mutex);
  967. spin_lock_init(&map->owner.lock);
  968. map->spin_lock_off = -EINVAL;
  969. map->timer_off = -EINVAL;
  970. if (attr->btf_key_type_id || attr->btf_value_type_id ||
  971. /* Even the map's value is a kernel's struct,
  972. * the bpf_prog.o must have BTF to begin with
  973. * to figure out the corresponding kernel's
  974. * counter part. Thus, attr->btf_fd has
  975. * to be valid also.
  976. */
  977. attr->btf_vmlinux_value_type_id) {
  978. struct btf *btf;
  979. btf = btf_get_by_fd(attr->btf_fd);
  980. if (IS_ERR(btf)) {
  981. err = PTR_ERR(btf);
  982. goto free_map;
  983. }
  984. if (btf_is_kernel(btf)) {
  985. btf_put(btf);
  986. err = -EACCES;
  987. goto free_map;
  988. }
  989. map->btf = btf;
  990. if (attr->btf_value_type_id) {
  991. err = map_check_btf(map, btf, attr->btf_key_type_id,
  992. attr->btf_value_type_id);
  993. if (err)
  994. goto free_map;
  995. }
  996. map->btf_key_type_id = attr->btf_key_type_id;
  997. map->btf_value_type_id = attr->btf_value_type_id;
  998. map->btf_vmlinux_value_type_id =
  999. attr->btf_vmlinux_value_type_id;
  1000. }
  1001. err = bpf_map_alloc_off_arr(map);
  1002. if (err)
  1003. goto free_map;
  1004. err = security_bpf_map_alloc(map);
  1005. if (err)
  1006. goto free_map_off_arr;
  1007. err = bpf_map_alloc_id(map);
  1008. if (err)
  1009. goto free_map_sec;
  1010. bpf_map_save_memcg(map);
  1011. err = bpf_map_new_fd(map, f_flags);
  1012. if (err < 0) {
  1013. /* failed to allocate fd.
  1014. * bpf_map_put_with_uref() is needed because the above
  1015. * bpf_map_alloc_id() has published the map
  1016. * to the userspace and the userspace may
  1017. * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
  1018. */
  1019. bpf_map_put_with_uref(map);
  1020. return err;
  1021. }
  1022. return err;
  1023. free_map_sec:
  1024. security_bpf_map_free(map);
  1025. free_map_off_arr:
  1026. kfree(map->off_arr);
  1027. free_map:
  1028. btf_put(map->btf);
  1029. map->ops->map_free(map);
  1030. return err;
  1031. }
  1032. /* if error is returned, fd is released.
  1033. * On success caller should complete fd access with matching fdput()
  1034. */
  1035. struct bpf_map *__bpf_map_get(struct fd f)
  1036. {
  1037. if (!f.file)
  1038. return ERR_PTR(-EBADF);
  1039. if (f.file->f_op != &bpf_map_fops) {
  1040. fdput(f);
  1041. return ERR_PTR(-EINVAL);
  1042. }
  1043. return f.file->private_data;
  1044. }
  1045. void bpf_map_inc(struct bpf_map *map)
  1046. {
  1047. atomic64_inc(&map->refcnt);
  1048. }
  1049. EXPORT_SYMBOL_GPL(bpf_map_inc);
  1050. void bpf_map_inc_with_uref(struct bpf_map *map)
  1051. {
  1052. atomic64_inc(&map->refcnt);
  1053. atomic64_inc(&map->usercnt);
  1054. }
  1055. EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
  1056. struct bpf_map *bpf_map_get(u32 ufd)
  1057. {
  1058. struct fd f = fdget(ufd);
  1059. struct bpf_map *map;
  1060. map = __bpf_map_get(f);
  1061. if (IS_ERR(map))
  1062. return map;
  1063. bpf_map_inc(map);
  1064. fdput(f);
  1065. return map;
  1066. }
  1067. EXPORT_SYMBOL(bpf_map_get);
  1068. struct bpf_map *bpf_map_get_with_uref(u32 ufd)
  1069. {
  1070. struct fd f = fdget(ufd);
  1071. struct bpf_map *map;
  1072. map = __bpf_map_get(f);
  1073. if (IS_ERR(map))
  1074. return map;
  1075. bpf_map_inc_with_uref(map);
  1076. fdput(f);
  1077. return map;
  1078. }
  1079. /* map_idr_lock should have been held */
  1080. static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
  1081. {
  1082. int refold;
  1083. refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
  1084. if (!refold)
  1085. return ERR_PTR(-ENOENT);
  1086. if (uref)
  1087. atomic64_inc(&map->usercnt);
  1088. return map;
  1089. }
  1090. struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
  1091. {
  1092. spin_lock_bh(&map_idr_lock);
  1093. map = __bpf_map_inc_not_zero(map, false);
  1094. spin_unlock_bh(&map_idr_lock);
  1095. return map;
  1096. }
  1097. EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
  1098. int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  1099. {
  1100. return -ENOTSUPP;
  1101. }
  1102. static void *__bpf_copy_key(void __user *ukey, u64 key_size)
  1103. {
  1104. if (key_size)
  1105. return vmemdup_user(ukey, key_size);
  1106. if (ukey)
  1107. return ERR_PTR(-EINVAL);
  1108. return NULL;
  1109. }
  1110. static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
  1111. {
  1112. if (key_size)
  1113. return kvmemdup_bpfptr(ukey, key_size);
  1114. if (!bpfptr_is_null(ukey))
  1115. return ERR_PTR(-EINVAL);
  1116. return NULL;
  1117. }
  1118. /* last field in 'union bpf_attr' used by this command */
  1119. #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
  1120. static int map_lookup_elem(union bpf_attr *attr)
  1121. {
  1122. void __user *ukey = u64_to_user_ptr(attr->key);
  1123. void __user *uvalue = u64_to_user_ptr(attr->value);
  1124. int ufd = attr->map_fd;
  1125. struct bpf_map *map;
  1126. void *key, *value;
  1127. u32 value_size;
  1128. struct fd f;
  1129. int err;
  1130. if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
  1131. return -EINVAL;
  1132. if (attr->flags & ~BPF_F_LOCK)
  1133. return -EINVAL;
  1134. f = fdget(ufd);
  1135. map = __bpf_map_get(f);
  1136. if (IS_ERR(map))
  1137. return PTR_ERR(map);
  1138. if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
  1139. err = -EPERM;
  1140. goto err_put;
  1141. }
  1142. if ((attr->flags & BPF_F_LOCK) &&
  1143. !map_value_has_spin_lock(map)) {
  1144. err = -EINVAL;
  1145. goto err_put;
  1146. }
  1147. key = __bpf_copy_key(ukey, map->key_size);
  1148. if (IS_ERR(key)) {
  1149. err = PTR_ERR(key);
  1150. goto err_put;
  1151. }
  1152. value_size = bpf_map_value_size(map);
  1153. err = -ENOMEM;
  1154. value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
  1155. if (!value)
  1156. goto free_key;
  1157. if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
  1158. if (copy_from_user(value, uvalue, value_size))
  1159. err = -EFAULT;
  1160. else
  1161. err = bpf_map_copy_value(map, key, value, attr->flags);
  1162. goto free_value;
  1163. }
  1164. err = bpf_map_copy_value(map, key, value, attr->flags);
  1165. if (err)
  1166. goto free_value;
  1167. err = -EFAULT;
  1168. if (copy_to_user(uvalue, value, value_size) != 0)
  1169. goto free_value;
  1170. err = 0;
  1171. free_value:
  1172. kvfree(value);
  1173. free_key:
  1174. kvfree(key);
  1175. err_put:
  1176. fdput(f);
  1177. return err;
  1178. }
  1179. #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
  1180. static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
  1181. {
  1182. bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
  1183. bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
  1184. int ufd = attr->map_fd;
  1185. struct bpf_map *map;
  1186. void *key, *value;
  1187. u32 value_size;
  1188. struct fd f;
  1189. int err;
  1190. if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
  1191. return -EINVAL;
  1192. f = fdget(ufd);
  1193. map = __bpf_map_get(f);
  1194. if (IS_ERR(map))
  1195. return PTR_ERR(map);
  1196. bpf_map_write_active_inc(map);
  1197. if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
  1198. err = -EPERM;
  1199. goto err_put;
  1200. }
  1201. if ((attr->flags & BPF_F_LOCK) &&
  1202. !map_value_has_spin_lock(map)) {
  1203. err = -EINVAL;
  1204. goto err_put;
  1205. }
  1206. key = ___bpf_copy_key(ukey, map->key_size);
  1207. if (IS_ERR(key)) {
  1208. err = PTR_ERR(key);
  1209. goto err_put;
  1210. }
  1211. value_size = bpf_map_value_size(map);
  1212. value = kvmemdup_bpfptr(uvalue, value_size);
  1213. if (IS_ERR(value)) {
  1214. err = PTR_ERR(value);
  1215. goto free_key;
  1216. }
  1217. err = bpf_map_update_value(map, f, key, value, attr->flags);
  1218. kvfree(value);
  1219. free_key:
  1220. kvfree(key);
  1221. err_put:
  1222. bpf_map_write_active_dec(map);
  1223. fdput(f);
  1224. return err;
  1225. }
  1226. #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
  1227. static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
  1228. {
  1229. bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
  1230. int ufd = attr->map_fd;
  1231. struct bpf_map *map;
  1232. struct fd f;
  1233. void *key;
  1234. int err;
  1235. if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
  1236. return -EINVAL;
  1237. f = fdget(ufd);
  1238. map = __bpf_map_get(f);
  1239. if (IS_ERR(map))
  1240. return PTR_ERR(map);
  1241. bpf_map_write_active_inc(map);
  1242. if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
  1243. err = -EPERM;
  1244. goto err_put;
  1245. }
  1246. key = ___bpf_copy_key(ukey, map->key_size);
  1247. if (IS_ERR(key)) {
  1248. err = PTR_ERR(key);
  1249. goto err_put;
  1250. }
  1251. if (bpf_map_is_dev_bound(map)) {
  1252. err = bpf_map_offload_delete_elem(map, key);
  1253. goto out;
  1254. } else if (IS_FD_PROG_ARRAY(map) ||
  1255. map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
  1256. /* These maps require sleepable context */
  1257. err = map->ops->map_delete_elem(map, key);
  1258. goto out;
  1259. }
  1260. bpf_disable_instrumentation();
  1261. rcu_read_lock();
  1262. err = map->ops->map_delete_elem(map, key);
  1263. rcu_read_unlock();
  1264. bpf_enable_instrumentation();
  1265. maybe_wait_bpf_programs(map);
  1266. out:
  1267. kvfree(key);
  1268. err_put:
  1269. bpf_map_write_active_dec(map);
  1270. fdput(f);
  1271. return err;
  1272. }
  1273. /* last field in 'union bpf_attr' used by this command */
  1274. #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
  1275. static int map_get_next_key(union bpf_attr *attr)
  1276. {
  1277. void __user *ukey = u64_to_user_ptr(attr->key);
  1278. void __user *unext_key = u64_to_user_ptr(attr->next_key);
  1279. int ufd = attr->map_fd;
  1280. struct bpf_map *map;
  1281. void *key, *next_key;
  1282. struct fd f;
  1283. int err;
  1284. if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
  1285. return -EINVAL;
  1286. f = fdget(ufd);
  1287. map = __bpf_map_get(f);
  1288. if (IS_ERR(map))
  1289. return PTR_ERR(map);
  1290. if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
  1291. err = -EPERM;
  1292. goto err_put;
  1293. }
  1294. if (ukey) {
  1295. key = __bpf_copy_key(ukey, map->key_size);
  1296. if (IS_ERR(key)) {
  1297. err = PTR_ERR(key);
  1298. goto err_put;
  1299. }
  1300. } else {
  1301. key = NULL;
  1302. }
  1303. err = -ENOMEM;
  1304. next_key = kvmalloc(map->key_size, GFP_USER);
  1305. if (!next_key)
  1306. goto free_key;
  1307. if (bpf_map_is_dev_bound(map)) {
  1308. err = bpf_map_offload_get_next_key(map, key, next_key);
  1309. goto out;
  1310. }
  1311. rcu_read_lock();
  1312. err = map->ops->map_get_next_key(map, key, next_key);
  1313. rcu_read_unlock();
  1314. out:
  1315. if (err)
  1316. goto free_next_key;
  1317. err = -EFAULT;
  1318. if (copy_to_user(unext_key, next_key, map->key_size) != 0)
  1319. goto free_next_key;
  1320. err = 0;
  1321. free_next_key:
  1322. kvfree(next_key);
  1323. free_key:
  1324. kvfree(key);
  1325. err_put:
  1326. fdput(f);
  1327. return err;
  1328. }
  1329. int generic_map_delete_batch(struct bpf_map *map,
  1330. const union bpf_attr *attr,
  1331. union bpf_attr __user *uattr)
  1332. {
  1333. void __user *keys = u64_to_user_ptr(attr->batch.keys);
  1334. u32 cp, max_count;
  1335. int err = 0;
  1336. void *key;
  1337. if (attr->batch.elem_flags & ~BPF_F_LOCK)
  1338. return -EINVAL;
  1339. if ((attr->batch.elem_flags & BPF_F_LOCK) &&
  1340. !map_value_has_spin_lock(map)) {
  1341. return -EINVAL;
  1342. }
  1343. max_count = attr->batch.count;
  1344. if (!max_count)
  1345. return 0;
  1346. key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
  1347. if (!key)
  1348. return -ENOMEM;
  1349. for (cp = 0; cp < max_count; cp++) {
  1350. err = -EFAULT;
  1351. if (copy_from_user(key, keys + cp * map->key_size,
  1352. map->key_size))
  1353. break;
  1354. if (bpf_map_is_dev_bound(map)) {
  1355. err = bpf_map_offload_delete_elem(map, key);
  1356. break;
  1357. }
  1358. bpf_disable_instrumentation();
  1359. rcu_read_lock();
  1360. err = map->ops->map_delete_elem(map, key);
  1361. rcu_read_unlock();
  1362. bpf_enable_instrumentation();
  1363. if (err)
  1364. break;
  1365. cond_resched();
  1366. }
  1367. if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
  1368. err = -EFAULT;
  1369. kvfree(key);
  1370. maybe_wait_bpf_programs(map);
  1371. return err;
  1372. }
  1373. int generic_map_update_batch(struct bpf_map *map,
  1374. const union bpf_attr *attr,
  1375. union bpf_attr __user *uattr)
  1376. {
  1377. void __user *values = u64_to_user_ptr(attr->batch.values);
  1378. void __user *keys = u64_to_user_ptr(attr->batch.keys);
  1379. u32 value_size, cp, max_count;
  1380. int ufd = attr->batch.map_fd;
  1381. void *key, *value;
  1382. struct fd f;
  1383. int err = 0;
  1384. if (attr->batch.elem_flags & ~BPF_F_LOCK)
  1385. return -EINVAL;
  1386. if ((attr->batch.elem_flags & BPF_F_LOCK) &&
  1387. !map_value_has_spin_lock(map)) {
  1388. return -EINVAL;
  1389. }
  1390. value_size = bpf_map_value_size(map);
  1391. max_count = attr->batch.count;
  1392. if (!max_count)
  1393. return 0;
  1394. key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
  1395. if (!key)
  1396. return -ENOMEM;
  1397. value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
  1398. if (!value) {
  1399. kvfree(key);
  1400. return -ENOMEM;
  1401. }
  1402. f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
  1403. for (cp = 0; cp < max_count; cp++) {
  1404. err = -EFAULT;
  1405. if (copy_from_user(key, keys + cp * map->key_size,
  1406. map->key_size) ||
  1407. copy_from_user(value, values + cp * value_size, value_size))
  1408. break;
  1409. err = bpf_map_update_value(map, f, key, value,
  1410. attr->batch.elem_flags);
  1411. if (err)
  1412. break;
  1413. cond_resched();
  1414. }
  1415. if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
  1416. err = -EFAULT;
  1417. kvfree(value);
  1418. kvfree(key);
  1419. fdput(f);
  1420. return err;
  1421. }
  1422. #define MAP_LOOKUP_RETRIES 3
  1423. int generic_map_lookup_batch(struct bpf_map *map,
  1424. const union bpf_attr *attr,
  1425. union bpf_attr __user *uattr)
  1426. {
  1427. void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
  1428. void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
  1429. void __user *values = u64_to_user_ptr(attr->batch.values);
  1430. void __user *keys = u64_to_user_ptr(attr->batch.keys);
  1431. void *buf, *buf_prevkey, *prev_key, *key, *value;
  1432. int err, retry = MAP_LOOKUP_RETRIES;
  1433. u32 value_size, cp, max_count;
  1434. if (attr->batch.elem_flags & ~BPF_F_LOCK)
  1435. return -EINVAL;
  1436. if ((attr->batch.elem_flags & BPF_F_LOCK) &&
  1437. !map_value_has_spin_lock(map))
  1438. return -EINVAL;
  1439. value_size = bpf_map_value_size(map);
  1440. max_count = attr->batch.count;
  1441. if (!max_count)
  1442. return 0;
  1443. if (put_user(0, &uattr->batch.count))
  1444. return -EFAULT;
  1445. buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
  1446. if (!buf_prevkey)
  1447. return -ENOMEM;
  1448. buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
  1449. if (!buf) {
  1450. kvfree(buf_prevkey);
  1451. return -ENOMEM;
  1452. }
  1453. err = -EFAULT;
  1454. prev_key = NULL;
  1455. if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
  1456. goto free_buf;
  1457. key = buf;
  1458. value = key + map->key_size;
  1459. if (ubatch)
  1460. prev_key = buf_prevkey;
  1461. for (cp = 0; cp < max_count;) {
  1462. rcu_read_lock();
  1463. err = map->ops->map_get_next_key(map, prev_key, key);
  1464. rcu_read_unlock();
  1465. if (err)
  1466. break;
  1467. err = bpf_map_copy_value(map, key, value,
  1468. attr->batch.elem_flags);
  1469. if (err == -ENOENT) {
  1470. if (retry) {
  1471. retry--;
  1472. continue;
  1473. }
  1474. err = -EINTR;
  1475. break;
  1476. }
  1477. if (err)
  1478. goto free_buf;
  1479. if (copy_to_user(keys + cp * map->key_size, key,
  1480. map->key_size)) {
  1481. err = -EFAULT;
  1482. goto free_buf;
  1483. }
  1484. if (copy_to_user(values + cp * value_size, value, value_size)) {
  1485. err = -EFAULT;
  1486. goto free_buf;
  1487. }
  1488. if (!prev_key)
  1489. prev_key = buf_prevkey;
  1490. swap(prev_key, key);
  1491. retry = MAP_LOOKUP_RETRIES;
  1492. cp++;
  1493. cond_resched();
  1494. }
  1495. if (err == -EFAULT)
  1496. goto free_buf;
  1497. if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
  1498. (cp && copy_to_user(uobatch, prev_key, map->key_size))))
  1499. err = -EFAULT;
  1500. free_buf:
  1501. kvfree(buf_prevkey);
  1502. kvfree(buf);
  1503. return err;
  1504. }
  1505. #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
  1506. static int map_lookup_and_delete_elem(union bpf_attr *attr)
  1507. {
  1508. void __user *ukey = u64_to_user_ptr(attr->key);
  1509. void __user *uvalue = u64_to_user_ptr(attr->value);
  1510. int ufd = attr->map_fd;
  1511. struct bpf_map *map;
  1512. void *key, *value;
  1513. u32 value_size;
  1514. struct fd f;
  1515. int err;
  1516. if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
  1517. return -EINVAL;
  1518. if (attr->flags & ~BPF_F_LOCK)
  1519. return -EINVAL;
  1520. f = fdget(ufd);
  1521. map = __bpf_map_get(f);
  1522. if (IS_ERR(map))
  1523. return PTR_ERR(map);
  1524. bpf_map_write_active_inc(map);
  1525. if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
  1526. !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
  1527. err = -EPERM;
  1528. goto err_put;
  1529. }
  1530. if (attr->flags &&
  1531. (map->map_type == BPF_MAP_TYPE_QUEUE ||
  1532. map->map_type == BPF_MAP_TYPE_STACK)) {
  1533. err = -EINVAL;
  1534. goto err_put;
  1535. }
  1536. if ((attr->flags & BPF_F_LOCK) &&
  1537. !map_value_has_spin_lock(map)) {
  1538. err = -EINVAL;
  1539. goto err_put;
  1540. }
  1541. key = __bpf_copy_key(ukey, map->key_size);
  1542. if (IS_ERR(key)) {
  1543. err = PTR_ERR(key);
  1544. goto err_put;
  1545. }
  1546. value_size = bpf_map_value_size(map);
  1547. err = -ENOMEM;
  1548. value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
  1549. if (!value)
  1550. goto free_key;
  1551. err = -ENOTSUPP;
  1552. if (map->map_type == BPF_MAP_TYPE_QUEUE ||
  1553. map->map_type == BPF_MAP_TYPE_STACK) {
  1554. err = map->ops->map_pop_elem(map, value);
  1555. } else if (map->map_type == BPF_MAP_TYPE_HASH ||
  1556. map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  1557. map->map_type == BPF_MAP_TYPE_LRU_HASH ||
  1558. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  1559. if (!bpf_map_is_dev_bound(map)) {
  1560. bpf_disable_instrumentation();
  1561. rcu_read_lock();
  1562. err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
  1563. rcu_read_unlock();
  1564. bpf_enable_instrumentation();
  1565. }
  1566. }
  1567. if (err)
  1568. goto free_value;
  1569. if (copy_to_user(uvalue, value, value_size) != 0) {
  1570. err = -EFAULT;
  1571. goto free_value;
  1572. }
  1573. err = 0;
  1574. free_value:
  1575. kvfree(value);
  1576. free_key:
  1577. kvfree(key);
  1578. err_put:
  1579. bpf_map_write_active_dec(map);
  1580. fdput(f);
  1581. return err;
  1582. }
  1583. #define BPF_MAP_FREEZE_LAST_FIELD map_fd
  1584. static int map_freeze(const union bpf_attr *attr)
  1585. {
  1586. int err = 0, ufd = attr->map_fd;
  1587. struct bpf_map *map;
  1588. struct fd f;
  1589. if (CHECK_ATTR(BPF_MAP_FREEZE))
  1590. return -EINVAL;
  1591. f = fdget(ufd);
  1592. map = __bpf_map_get(f);
  1593. if (IS_ERR(map))
  1594. return PTR_ERR(map);
  1595. if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
  1596. map_value_has_timer(map) || map_value_has_kptrs(map)) {
  1597. fdput(f);
  1598. return -ENOTSUPP;
  1599. }
  1600. mutex_lock(&map->freeze_mutex);
  1601. if (bpf_map_write_active(map)) {
  1602. err = -EBUSY;
  1603. goto err_put;
  1604. }
  1605. if (READ_ONCE(map->frozen)) {
  1606. err = -EBUSY;
  1607. goto err_put;
  1608. }
  1609. if (!bpf_capable()) {
  1610. err = -EPERM;
  1611. goto err_put;
  1612. }
  1613. WRITE_ONCE(map->frozen, true);
  1614. err_put:
  1615. mutex_unlock(&map->freeze_mutex);
  1616. fdput(f);
  1617. return err;
  1618. }
  1619. static const struct bpf_prog_ops * const bpf_prog_types[] = {
  1620. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  1621. [_id] = & _name ## _prog_ops,
  1622. #define BPF_MAP_TYPE(_id, _ops)
  1623. #define BPF_LINK_TYPE(_id, _name)
  1624. #include <linux/bpf_types.h>
  1625. #undef BPF_PROG_TYPE
  1626. #undef BPF_MAP_TYPE
  1627. #undef BPF_LINK_TYPE
  1628. };
  1629. static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
  1630. {
  1631. const struct bpf_prog_ops *ops;
  1632. if (type >= ARRAY_SIZE(bpf_prog_types))
  1633. return -EINVAL;
  1634. type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
  1635. ops = bpf_prog_types[type];
  1636. if (!ops)
  1637. return -EINVAL;
  1638. if (!bpf_prog_is_dev_bound(prog->aux))
  1639. prog->aux->ops = ops;
  1640. else
  1641. prog->aux->ops = &bpf_offload_prog_ops;
  1642. prog->type = type;
  1643. return 0;
  1644. }
  1645. enum bpf_audit {
  1646. BPF_AUDIT_LOAD,
  1647. BPF_AUDIT_UNLOAD,
  1648. BPF_AUDIT_MAX,
  1649. };
  1650. static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
  1651. [BPF_AUDIT_LOAD] = "LOAD",
  1652. [BPF_AUDIT_UNLOAD] = "UNLOAD",
  1653. };
  1654. static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
  1655. {
  1656. struct audit_context *ctx = NULL;
  1657. struct audit_buffer *ab;
  1658. if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
  1659. return;
  1660. if (audit_enabled == AUDIT_OFF)
  1661. return;
  1662. if (!in_irq() && !irqs_disabled())
  1663. ctx = audit_context();
  1664. ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
  1665. if (unlikely(!ab))
  1666. return;
  1667. audit_log_format(ab, "prog-id=%u op=%s",
  1668. prog->aux->id, bpf_audit_str[op]);
  1669. audit_log_end(ab);
  1670. }
  1671. static int bpf_prog_alloc_id(struct bpf_prog *prog)
  1672. {
  1673. int id;
  1674. idr_preload(GFP_KERNEL);
  1675. spin_lock_bh(&prog_idr_lock);
  1676. id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
  1677. if (id > 0)
  1678. prog->aux->id = id;
  1679. spin_unlock_bh(&prog_idr_lock);
  1680. idr_preload_end();
  1681. /* id is in [1, INT_MAX) */
  1682. if (WARN_ON_ONCE(!id))
  1683. return -ENOSPC;
  1684. return id > 0 ? 0 : id;
  1685. }
  1686. void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
  1687. {
  1688. unsigned long flags;
  1689. /* cBPF to eBPF migrations are currently not in the idr store.
  1690. * Offloaded programs are removed from the store when their device
  1691. * disappears - even if someone grabs an fd to them they are unusable,
  1692. * simply waiting for refcnt to drop to be freed.
  1693. */
  1694. if (!prog->aux->id)
  1695. return;
  1696. if (do_idr_lock)
  1697. spin_lock_irqsave(&prog_idr_lock, flags);
  1698. else
  1699. __acquire(&prog_idr_lock);
  1700. idr_remove(&prog_idr, prog->aux->id);
  1701. prog->aux->id = 0;
  1702. if (do_idr_lock)
  1703. spin_unlock_irqrestore(&prog_idr_lock, flags);
  1704. else
  1705. __release(&prog_idr_lock);
  1706. }
  1707. static void __bpf_prog_put_rcu(struct rcu_head *rcu)
  1708. {
  1709. struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
  1710. kvfree(aux->func_info);
  1711. kfree(aux->func_info_aux);
  1712. free_uid(aux->user);
  1713. security_bpf_prog_free(aux);
  1714. bpf_prog_free(aux->prog);
  1715. }
  1716. static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
  1717. {
  1718. bpf_prog_kallsyms_del_all(prog);
  1719. btf_put(prog->aux->btf);
  1720. kvfree(prog->aux->jited_linfo);
  1721. kvfree(prog->aux->linfo);
  1722. kfree(prog->aux->kfunc_tab);
  1723. if (prog->aux->attach_btf)
  1724. btf_put(prog->aux->attach_btf);
  1725. if (deferred) {
  1726. if (prog->aux->sleepable)
  1727. call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
  1728. else
  1729. call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
  1730. } else {
  1731. __bpf_prog_put_rcu(&prog->aux->rcu);
  1732. }
  1733. }
  1734. static void bpf_prog_put_deferred(struct work_struct *work)
  1735. {
  1736. struct bpf_prog_aux *aux;
  1737. struct bpf_prog *prog;
  1738. aux = container_of(work, struct bpf_prog_aux, work);
  1739. prog = aux->prog;
  1740. perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
  1741. bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
  1742. bpf_prog_free_id(prog, true);
  1743. __bpf_prog_put_noref(prog, true);
  1744. }
  1745. static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
  1746. {
  1747. struct bpf_prog_aux *aux = prog->aux;
  1748. if (atomic64_dec_and_test(&aux->refcnt)) {
  1749. if (in_irq() || irqs_disabled()) {
  1750. INIT_WORK(&aux->work, bpf_prog_put_deferred);
  1751. schedule_work(&aux->work);
  1752. } else {
  1753. bpf_prog_put_deferred(&aux->work);
  1754. }
  1755. }
  1756. }
  1757. void bpf_prog_put(struct bpf_prog *prog)
  1758. {
  1759. __bpf_prog_put(prog, true);
  1760. }
  1761. EXPORT_SYMBOL_GPL(bpf_prog_put);
  1762. static int bpf_prog_release(struct inode *inode, struct file *filp)
  1763. {
  1764. struct bpf_prog *prog = filp->private_data;
  1765. bpf_prog_put(prog);
  1766. return 0;
  1767. }
  1768. struct bpf_prog_kstats {
  1769. u64 nsecs;
  1770. u64 cnt;
  1771. u64 misses;
  1772. };
  1773. void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog)
  1774. {
  1775. struct bpf_prog_stats *stats;
  1776. unsigned int flags;
  1777. stats = this_cpu_ptr(prog->stats);
  1778. flags = u64_stats_update_begin_irqsave(&stats->syncp);
  1779. u64_stats_inc(&stats->misses);
  1780. u64_stats_update_end_irqrestore(&stats->syncp, flags);
  1781. }
  1782. static void bpf_prog_get_stats(const struct bpf_prog *prog,
  1783. struct bpf_prog_kstats *stats)
  1784. {
  1785. u64 nsecs = 0, cnt = 0, misses = 0;
  1786. int cpu;
  1787. for_each_possible_cpu(cpu) {
  1788. const struct bpf_prog_stats *st;
  1789. unsigned int start;
  1790. u64 tnsecs, tcnt, tmisses;
  1791. st = per_cpu_ptr(prog->stats, cpu);
  1792. do {
  1793. start = u64_stats_fetch_begin_irq(&st->syncp);
  1794. tnsecs = u64_stats_read(&st->nsecs);
  1795. tcnt = u64_stats_read(&st->cnt);
  1796. tmisses = u64_stats_read(&st->misses);
  1797. } while (u64_stats_fetch_retry_irq(&st->syncp, start));
  1798. nsecs += tnsecs;
  1799. cnt += tcnt;
  1800. misses += tmisses;
  1801. }
  1802. stats->nsecs = nsecs;
  1803. stats->cnt = cnt;
  1804. stats->misses = misses;
  1805. }
  1806. #ifdef CONFIG_PROC_FS
  1807. static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
  1808. {
  1809. const struct bpf_prog *prog = filp->private_data;
  1810. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  1811. struct bpf_prog_kstats stats;
  1812. bpf_prog_get_stats(prog, &stats);
  1813. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  1814. seq_printf(m,
  1815. "prog_type:\t%u\n"
  1816. "prog_jited:\t%u\n"
  1817. "prog_tag:\t%s\n"
  1818. "memlock:\t%llu\n"
  1819. "prog_id:\t%u\n"
  1820. "run_time_ns:\t%llu\n"
  1821. "run_cnt:\t%llu\n"
  1822. "recursion_misses:\t%llu\n"
  1823. "verified_insns:\t%u\n",
  1824. prog->type,
  1825. prog->jited,
  1826. prog_tag,
  1827. prog->pages * 1ULL << PAGE_SHIFT,
  1828. prog->aux->id,
  1829. stats.nsecs,
  1830. stats.cnt,
  1831. stats.misses,
  1832. prog->aux->verified_insns);
  1833. }
  1834. #endif
  1835. const struct file_operations bpf_prog_fops = {
  1836. #ifdef CONFIG_PROC_FS
  1837. .show_fdinfo = bpf_prog_show_fdinfo,
  1838. #endif
  1839. .release = bpf_prog_release,
  1840. .read = bpf_dummy_read,
  1841. .write = bpf_dummy_write,
  1842. };
  1843. int bpf_prog_new_fd(struct bpf_prog *prog)
  1844. {
  1845. int ret;
  1846. ret = security_bpf_prog(prog);
  1847. if (ret < 0)
  1848. return ret;
  1849. return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
  1850. O_RDWR | O_CLOEXEC);
  1851. }
  1852. static struct bpf_prog *____bpf_prog_get(struct fd f)
  1853. {
  1854. if (!f.file)
  1855. return ERR_PTR(-EBADF);
  1856. if (f.file->f_op != &bpf_prog_fops) {
  1857. fdput(f);
  1858. return ERR_PTR(-EINVAL);
  1859. }
  1860. return f.file->private_data;
  1861. }
  1862. void bpf_prog_add(struct bpf_prog *prog, int i)
  1863. {
  1864. atomic64_add(i, &prog->aux->refcnt);
  1865. }
  1866. EXPORT_SYMBOL_GPL(bpf_prog_add);
  1867. void bpf_prog_sub(struct bpf_prog *prog, int i)
  1868. {
  1869. /* Only to be used for undoing previous bpf_prog_add() in some
  1870. * error path. We still know that another entity in our call
  1871. * path holds a reference to the program, thus atomic_sub() can
  1872. * be safely used in such cases!
  1873. */
  1874. WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
  1875. }
  1876. EXPORT_SYMBOL_GPL(bpf_prog_sub);
  1877. void bpf_prog_inc(struct bpf_prog *prog)
  1878. {
  1879. atomic64_inc(&prog->aux->refcnt);
  1880. }
  1881. EXPORT_SYMBOL_GPL(bpf_prog_inc);
  1882. /* prog_idr_lock should have been held */
  1883. struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
  1884. {
  1885. int refold;
  1886. refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
  1887. if (!refold)
  1888. return ERR_PTR(-ENOENT);
  1889. return prog;
  1890. }
  1891. EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
  1892. bool bpf_prog_get_ok(struct bpf_prog *prog,
  1893. enum bpf_prog_type *attach_type, bool attach_drv)
  1894. {
  1895. /* not an attachment, just a refcount inc, always allow */
  1896. if (!attach_type)
  1897. return true;
  1898. if (prog->type != *attach_type)
  1899. return false;
  1900. if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
  1901. return false;
  1902. return true;
  1903. }
  1904. static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
  1905. bool attach_drv)
  1906. {
  1907. struct fd f = fdget(ufd);
  1908. struct bpf_prog *prog;
  1909. prog = ____bpf_prog_get(f);
  1910. if (IS_ERR(prog))
  1911. return prog;
  1912. if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
  1913. prog = ERR_PTR(-EINVAL);
  1914. goto out;
  1915. }
  1916. bpf_prog_inc(prog);
  1917. out:
  1918. fdput(f);
  1919. return prog;
  1920. }
  1921. struct bpf_prog *bpf_prog_get(u32 ufd)
  1922. {
  1923. return __bpf_prog_get(ufd, NULL, false);
  1924. }
  1925. struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
  1926. bool attach_drv)
  1927. {
  1928. return __bpf_prog_get(ufd, &type, attach_drv);
  1929. }
  1930. EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
  1931. /* Initially all BPF programs could be loaded w/o specifying
  1932. * expected_attach_type. Later for some of them specifying expected_attach_type
  1933. * at load time became required so that program could be validated properly.
  1934. * Programs of types that are allowed to be loaded both w/ and w/o (for
  1935. * backward compatibility) expected_attach_type, should have the default attach
  1936. * type assigned to expected_attach_type for the latter case, so that it can be
  1937. * validated later at attach time.
  1938. *
  1939. * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
  1940. * prog type requires it but has some attach types that have to be backward
  1941. * compatible.
  1942. */
  1943. static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
  1944. {
  1945. switch (attr->prog_type) {
  1946. case BPF_PROG_TYPE_CGROUP_SOCK:
  1947. /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
  1948. * exist so checking for non-zero is the way to go here.
  1949. */
  1950. if (!attr->expected_attach_type)
  1951. attr->expected_attach_type =
  1952. BPF_CGROUP_INET_SOCK_CREATE;
  1953. break;
  1954. case BPF_PROG_TYPE_SK_REUSEPORT:
  1955. if (!attr->expected_attach_type)
  1956. attr->expected_attach_type =
  1957. BPF_SK_REUSEPORT_SELECT;
  1958. break;
  1959. }
  1960. }
  1961. static int
  1962. bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
  1963. enum bpf_attach_type expected_attach_type,
  1964. struct btf *attach_btf, u32 btf_id,
  1965. struct bpf_prog *dst_prog)
  1966. {
  1967. if (btf_id) {
  1968. if (btf_id > BTF_MAX_TYPE)
  1969. return -EINVAL;
  1970. if (!attach_btf && !dst_prog)
  1971. return -EINVAL;
  1972. switch (prog_type) {
  1973. case BPF_PROG_TYPE_TRACING:
  1974. case BPF_PROG_TYPE_LSM:
  1975. case BPF_PROG_TYPE_STRUCT_OPS:
  1976. case BPF_PROG_TYPE_EXT:
  1977. break;
  1978. default:
  1979. return -EINVAL;
  1980. }
  1981. }
  1982. if (attach_btf && (!btf_id || dst_prog))
  1983. return -EINVAL;
  1984. if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
  1985. prog_type != BPF_PROG_TYPE_EXT)
  1986. return -EINVAL;
  1987. switch (prog_type) {
  1988. case BPF_PROG_TYPE_CGROUP_SOCK:
  1989. switch (expected_attach_type) {
  1990. case BPF_CGROUP_INET_SOCK_CREATE:
  1991. case BPF_CGROUP_INET_SOCK_RELEASE:
  1992. case BPF_CGROUP_INET4_POST_BIND:
  1993. case BPF_CGROUP_INET6_POST_BIND:
  1994. return 0;
  1995. default:
  1996. return -EINVAL;
  1997. }
  1998. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  1999. switch (expected_attach_type) {
  2000. case BPF_CGROUP_INET4_BIND:
  2001. case BPF_CGROUP_INET6_BIND:
  2002. case BPF_CGROUP_INET4_CONNECT:
  2003. case BPF_CGROUP_INET6_CONNECT:
  2004. case BPF_CGROUP_INET4_GETPEERNAME:
  2005. case BPF_CGROUP_INET6_GETPEERNAME:
  2006. case BPF_CGROUP_INET4_GETSOCKNAME:
  2007. case BPF_CGROUP_INET6_GETSOCKNAME:
  2008. case BPF_CGROUP_UDP4_SENDMSG:
  2009. case BPF_CGROUP_UDP6_SENDMSG:
  2010. case BPF_CGROUP_UDP4_RECVMSG:
  2011. case BPF_CGROUP_UDP6_RECVMSG:
  2012. return 0;
  2013. default:
  2014. return -EINVAL;
  2015. }
  2016. case BPF_PROG_TYPE_CGROUP_SKB:
  2017. switch (expected_attach_type) {
  2018. case BPF_CGROUP_INET_INGRESS:
  2019. case BPF_CGROUP_INET_EGRESS:
  2020. return 0;
  2021. default:
  2022. return -EINVAL;
  2023. }
  2024. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  2025. switch (expected_attach_type) {
  2026. case BPF_CGROUP_SETSOCKOPT:
  2027. case BPF_CGROUP_GETSOCKOPT:
  2028. return 0;
  2029. default:
  2030. return -EINVAL;
  2031. }
  2032. case BPF_PROG_TYPE_SK_LOOKUP:
  2033. if (expected_attach_type == BPF_SK_LOOKUP)
  2034. return 0;
  2035. return -EINVAL;
  2036. case BPF_PROG_TYPE_SK_REUSEPORT:
  2037. switch (expected_attach_type) {
  2038. case BPF_SK_REUSEPORT_SELECT:
  2039. case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
  2040. return 0;
  2041. default:
  2042. return -EINVAL;
  2043. }
  2044. case BPF_PROG_TYPE_SYSCALL:
  2045. case BPF_PROG_TYPE_EXT:
  2046. if (expected_attach_type)
  2047. return -EINVAL;
  2048. fallthrough;
  2049. default:
  2050. return 0;
  2051. }
  2052. }
  2053. static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
  2054. {
  2055. switch (prog_type) {
  2056. case BPF_PROG_TYPE_SCHED_CLS:
  2057. case BPF_PROG_TYPE_SCHED_ACT:
  2058. case BPF_PROG_TYPE_XDP:
  2059. case BPF_PROG_TYPE_LWT_IN:
  2060. case BPF_PROG_TYPE_LWT_OUT:
  2061. case BPF_PROG_TYPE_LWT_XMIT:
  2062. case BPF_PROG_TYPE_LWT_SEG6LOCAL:
  2063. case BPF_PROG_TYPE_SK_SKB:
  2064. case BPF_PROG_TYPE_SK_MSG:
  2065. case BPF_PROG_TYPE_LIRC_MODE2:
  2066. case BPF_PROG_TYPE_FLOW_DISSECTOR:
  2067. case BPF_PROG_TYPE_CGROUP_DEVICE:
  2068. case BPF_PROG_TYPE_CGROUP_SOCK:
  2069. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  2070. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  2071. case BPF_PROG_TYPE_CGROUP_SYSCTL:
  2072. case BPF_PROG_TYPE_SOCK_OPS:
  2073. case BPF_PROG_TYPE_EXT: /* extends any prog */
  2074. return true;
  2075. case BPF_PROG_TYPE_CGROUP_SKB:
  2076. /* always unpriv */
  2077. case BPF_PROG_TYPE_SK_REUSEPORT:
  2078. /* equivalent to SOCKET_FILTER. need CAP_BPF only */
  2079. default:
  2080. return false;
  2081. }
  2082. }
  2083. static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
  2084. {
  2085. switch (prog_type) {
  2086. case BPF_PROG_TYPE_KPROBE:
  2087. case BPF_PROG_TYPE_TRACEPOINT:
  2088. case BPF_PROG_TYPE_PERF_EVENT:
  2089. case BPF_PROG_TYPE_RAW_TRACEPOINT:
  2090. case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
  2091. case BPF_PROG_TYPE_TRACING:
  2092. case BPF_PROG_TYPE_LSM:
  2093. case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
  2094. case BPF_PROG_TYPE_EXT: /* extends any prog */
  2095. return true;
  2096. default:
  2097. return false;
  2098. }
  2099. }
  2100. /* last field in 'union bpf_attr' used by this command */
  2101. #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
  2102. static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
  2103. {
  2104. enum bpf_prog_type type = attr->prog_type;
  2105. struct bpf_prog *prog, *dst_prog = NULL;
  2106. struct btf *attach_btf = NULL;
  2107. int err;
  2108. char license[128];
  2109. bool is_gpl;
  2110. if (CHECK_ATTR(BPF_PROG_LOAD))
  2111. return -EINVAL;
  2112. if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
  2113. BPF_F_ANY_ALIGNMENT |
  2114. BPF_F_TEST_STATE_FREQ |
  2115. BPF_F_SLEEPABLE |
  2116. BPF_F_TEST_RND_HI32 |
  2117. BPF_F_XDP_HAS_FRAGS))
  2118. return -EINVAL;
  2119. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
  2120. (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
  2121. !bpf_capable())
  2122. return -EPERM;
  2123. /* copy eBPF program license from user space */
  2124. if (strncpy_from_bpfptr(license,
  2125. make_bpfptr(attr->license, uattr.is_kernel),
  2126. sizeof(license) - 1) < 0)
  2127. return -EFAULT;
  2128. license[sizeof(license) - 1] = 0;
  2129. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  2130. is_gpl = license_is_gpl_compatible(license);
  2131. if (attr->insn_cnt == 0 ||
  2132. attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
  2133. return -E2BIG;
  2134. if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
  2135. type != BPF_PROG_TYPE_CGROUP_SKB &&
  2136. !bpf_capable())
  2137. return -EPERM;
  2138. if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
  2139. return -EPERM;
  2140. if (is_perfmon_prog_type(type) && !perfmon_capable())
  2141. return -EPERM;
  2142. /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
  2143. * or btf, we need to check which one it is
  2144. */
  2145. if (attr->attach_prog_fd) {
  2146. dst_prog = bpf_prog_get(attr->attach_prog_fd);
  2147. if (IS_ERR(dst_prog)) {
  2148. dst_prog = NULL;
  2149. attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
  2150. if (IS_ERR(attach_btf))
  2151. return -EINVAL;
  2152. if (!btf_is_kernel(attach_btf)) {
  2153. /* attaching through specifying bpf_prog's BTF
  2154. * objects directly might be supported eventually
  2155. */
  2156. btf_put(attach_btf);
  2157. return -ENOTSUPP;
  2158. }
  2159. }
  2160. } else if (attr->attach_btf_id) {
  2161. /* fall back to vmlinux BTF, if BTF type ID is specified */
  2162. attach_btf = bpf_get_btf_vmlinux();
  2163. if (IS_ERR(attach_btf))
  2164. return PTR_ERR(attach_btf);
  2165. if (!attach_btf)
  2166. return -EINVAL;
  2167. btf_get(attach_btf);
  2168. }
  2169. bpf_prog_load_fixup_attach_type(attr);
  2170. if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
  2171. attach_btf, attr->attach_btf_id,
  2172. dst_prog)) {
  2173. if (dst_prog)
  2174. bpf_prog_put(dst_prog);
  2175. if (attach_btf)
  2176. btf_put(attach_btf);
  2177. return -EINVAL;
  2178. }
  2179. /* plain bpf_prog allocation */
  2180. prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
  2181. if (!prog) {
  2182. if (dst_prog)
  2183. bpf_prog_put(dst_prog);
  2184. if (attach_btf)
  2185. btf_put(attach_btf);
  2186. return -ENOMEM;
  2187. }
  2188. prog->expected_attach_type = attr->expected_attach_type;
  2189. prog->aux->attach_btf = attach_btf;
  2190. prog->aux->attach_btf_id = attr->attach_btf_id;
  2191. prog->aux->dst_prog = dst_prog;
  2192. prog->aux->offload_requested = !!attr->prog_ifindex;
  2193. prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
  2194. prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
  2195. err = security_bpf_prog_alloc(prog->aux);
  2196. if (err)
  2197. goto free_prog;
  2198. prog->aux->user = get_current_user();
  2199. prog->len = attr->insn_cnt;
  2200. err = -EFAULT;
  2201. if (copy_from_bpfptr(prog->insns,
  2202. make_bpfptr(attr->insns, uattr.is_kernel),
  2203. bpf_prog_insn_size(prog)) != 0)
  2204. goto free_prog_sec;
  2205. prog->orig_prog = NULL;
  2206. prog->jited = 0;
  2207. atomic64_set(&prog->aux->refcnt, 1);
  2208. prog->gpl_compatible = is_gpl ? 1 : 0;
  2209. if (bpf_prog_is_dev_bound(prog->aux)) {
  2210. err = bpf_prog_offload_init(prog, attr);
  2211. if (err)
  2212. goto free_prog_sec;
  2213. }
  2214. /* find program type: socket_filter vs tracing_filter */
  2215. err = find_prog_type(type, prog);
  2216. if (err < 0)
  2217. goto free_prog_sec;
  2218. prog->aux->load_time = ktime_get_boottime_ns();
  2219. err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
  2220. sizeof(attr->prog_name));
  2221. if (err < 0)
  2222. goto free_prog_sec;
  2223. /* run eBPF verifier */
  2224. err = bpf_check(&prog, attr, uattr);
  2225. if (err < 0)
  2226. goto free_used_maps;
  2227. prog = bpf_prog_select_runtime(prog, &err);
  2228. if (err < 0)
  2229. goto free_used_maps;
  2230. err = bpf_prog_alloc_id(prog);
  2231. if (err)
  2232. goto free_used_maps;
  2233. /* Upon success of bpf_prog_alloc_id(), the BPF prog is
  2234. * effectively publicly exposed. However, retrieving via
  2235. * bpf_prog_get_fd_by_id() will take another reference,
  2236. * therefore it cannot be gone underneath us.
  2237. *
  2238. * Only for the time /after/ successful bpf_prog_new_fd()
  2239. * and before returning to userspace, we might just hold
  2240. * one reference and any parallel close on that fd could
  2241. * rip everything out. Hence, below notifications must
  2242. * happen before bpf_prog_new_fd().
  2243. *
  2244. * Also, any failure handling from this point onwards must
  2245. * be using bpf_prog_put() given the program is exposed.
  2246. */
  2247. bpf_prog_kallsyms_add(prog);
  2248. perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
  2249. bpf_audit_prog(prog, BPF_AUDIT_LOAD);
  2250. err = bpf_prog_new_fd(prog);
  2251. if (err < 0)
  2252. bpf_prog_put(prog);
  2253. return err;
  2254. free_used_maps:
  2255. /* In case we have subprogs, we need to wait for a grace
  2256. * period before we can tear down JIT memory since symbols
  2257. * are already exposed under kallsyms.
  2258. */
  2259. __bpf_prog_put_noref(prog, prog->aux->func_cnt);
  2260. return err;
  2261. free_prog_sec:
  2262. free_uid(prog->aux->user);
  2263. security_bpf_prog_free(prog->aux);
  2264. free_prog:
  2265. if (prog->aux->attach_btf)
  2266. btf_put(prog->aux->attach_btf);
  2267. bpf_prog_free(prog);
  2268. return err;
  2269. }
  2270. #define BPF_OBJ_LAST_FIELD file_flags
  2271. static int bpf_obj_pin(const union bpf_attr *attr)
  2272. {
  2273. if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
  2274. return -EINVAL;
  2275. return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
  2276. }
  2277. static int bpf_obj_get(const union bpf_attr *attr)
  2278. {
  2279. if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
  2280. attr->file_flags & ~BPF_OBJ_FLAG_MASK)
  2281. return -EINVAL;
  2282. return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
  2283. attr->file_flags);
  2284. }
  2285. void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
  2286. const struct bpf_link_ops *ops, struct bpf_prog *prog)
  2287. {
  2288. atomic64_set(&link->refcnt, 1);
  2289. link->type = type;
  2290. link->id = 0;
  2291. link->ops = ops;
  2292. link->prog = prog;
  2293. }
  2294. static void bpf_link_free_id(int id)
  2295. {
  2296. if (!id)
  2297. return;
  2298. spin_lock_bh(&link_idr_lock);
  2299. idr_remove(&link_idr, id);
  2300. spin_unlock_bh(&link_idr_lock);
  2301. }
  2302. /* Clean up bpf_link and corresponding anon_inode file and FD. After
  2303. * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
  2304. * anon_inode's release() call. This helper marksbpf_link as
  2305. * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
  2306. * is not decremented, it's the responsibility of a calling code that failed
  2307. * to complete bpf_link initialization.
  2308. */
  2309. void bpf_link_cleanup(struct bpf_link_primer *primer)
  2310. {
  2311. primer->link->prog = NULL;
  2312. bpf_link_free_id(primer->id);
  2313. fput(primer->file);
  2314. put_unused_fd(primer->fd);
  2315. }
  2316. void bpf_link_inc(struct bpf_link *link)
  2317. {
  2318. atomic64_inc(&link->refcnt);
  2319. }
  2320. /* bpf_link_free is guaranteed to be called from process context */
  2321. static void bpf_link_free(struct bpf_link *link)
  2322. {
  2323. bpf_link_free_id(link->id);
  2324. if (link->prog) {
  2325. /* detach BPF program, clean up used resources */
  2326. link->ops->release(link);
  2327. bpf_prog_put(link->prog);
  2328. }
  2329. /* free bpf_link and its containing memory */
  2330. link->ops->dealloc(link);
  2331. }
  2332. static void bpf_link_put_deferred(struct work_struct *work)
  2333. {
  2334. struct bpf_link *link = container_of(work, struct bpf_link, work);
  2335. bpf_link_free(link);
  2336. }
  2337. /* bpf_link_put can be called from atomic context, but ensures that resources
  2338. * are freed from process context
  2339. */
  2340. void bpf_link_put(struct bpf_link *link)
  2341. {
  2342. if (!atomic64_dec_and_test(&link->refcnt))
  2343. return;
  2344. if (in_atomic()) {
  2345. INIT_WORK(&link->work, bpf_link_put_deferred);
  2346. schedule_work(&link->work);
  2347. } else {
  2348. bpf_link_free(link);
  2349. }
  2350. }
  2351. EXPORT_SYMBOL(bpf_link_put);
  2352. static int bpf_link_release(struct inode *inode, struct file *filp)
  2353. {
  2354. struct bpf_link *link = filp->private_data;
  2355. bpf_link_put(link);
  2356. return 0;
  2357. }
  2358. #ifdef CONFIG_PROC_FS
  2359. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
  2360. #define BPF_MAP_TYPE(_id, _ops)
  2361. #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
  2362. static const char *bpf_link_type_strs[] = {
  2363. [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
  2364. #include <linux/bpf_types.h>
  2365. };
  2366. #undef BPF_PROG_TYPE
  2367. #undef BPF_MAP_TYPE
  2368. #undef BPF_LINK_TYPE
  2369. static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
  2370. {
  2371. const struct bpf_link *link = filp->private_data;
  2372. const struct bpf_prog *prog = link->prog;
  2373. char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
  2374. bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
  2375. seq_printf(m,
  2376. "link_type:\t%s\n"
  2377. "link_id:\t%u\n"
  2378. "prog_tag:\t%s\n"
  2379. "prog_id:\t%u\n",
  2380. bpf_link_type_strs[link->type],
  2381. link->id,
  2382. prog_tag,
  2383. prog->aux->id);
  2384. if (link->ops->show_fdinfo)
  2385. link->ops->show_fdinfo(link, m);
  2386. }
  2387. #endif
  2388. static const struct file_operations bpf_link_fops = {
  2389. #ifdef CONFIG_PROC_FS
  2390. .show_fdinfo = bpf_link_show_fdinfo,
  2391. #endif
  2392. .release = bpf_link_release,
  2393. .read = bpf_dummy_read,
  2394. .write = bpf_dummy_write,
  2395. };
  2396. static int bpf_link_alloc_id(struct bpf_link *link)
  2397. {
  2398. int id;
  2399. idr_preload(GFP_KERNEL);
  2400. spin_lock_bh(&link_idr_lock);
  2401. id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
  2402. spin_unlock_bh(&link_idr_lock);
  2403. idr_preload_end();
  2404. return id;
  2405. }
  2406. /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
  2407. * reserving unused FD and allocating ID from link_idr. This is to be paired
  2408. * with bpf_link_settle() to install FD and ID and expose bpf_link to
  2409. * user-space, if bpf_link is successfully attached. If not, bpf_link and
  2410. * pre-allocated resources are to be freed with bpf_cleanup() call. All the
  2411. * transient state is passed around in struct bpf_link_primer.
  2412. * This is preferred way to create and initialize bpf_link, especially when
  2413. * there are complicated and expensive operations in between creating bpf_link
  2414. * itself and attaching it to BPF hook. By using bpf_link_prime() and
  2415. * bpf_link_settle() kernel code using bpf_link doesn't have to perform
  2416. * expensive (and potentially failing) roll back operations in a rare case
  2417. * that file, FD, or ID can't be allocated.
  2418. */
  2419. int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
  2420. {
  2421. struct file *file;
  2422. int fd, id;
  2423. fd = get_unused_fd_flags(O_CLOEXEC);
  2424. if (fd < 0)
  2425. return fd;
  2426. id = bpf_link_alloc_id(link);
  2427. if (id < 0) {
  2428. put_unused_fd(fd);
  2429. return id;
  2430. }
  2431. file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
  2432. if (IS_ERR(file)) {
  2433. bpf_link_free_id(id);
  2434. put_unused_fd(fd);
  2435. return PTR_ERR(file);
  2436. }
  2437. primer->link = link;
  2438. primer->file = file;
  2439. primer->fd = fd;
  2440. primer->id = id;
  2441. return 0;
  2442. }
  2443. int bpf_link_settle(struct bpf_link_primer *primer)
  2444. {
  2445. /* make bpf_link fetchable by ID */
  2446. spin_lock_bh(&link_idr_lock);
  2447. primer->link->id = primer->id;
  2448. spin_unlock_bh(&link_idr_lock);
  2449. /* make bpf_link fetchable by FD */
  2450. fd_install(primer->fd, primer->file);
  2451. /* pass through installed FD */
  2452. return primer->fd;
  2453. }
  2454. int bpf_link_new_fd(struct bpf_link *link)
  2455. {
  2456. return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
  2457. }
  2458. struct bpf_link *bpf_link_get_from_fd(u32 ufd)
  2459. {
  2460. struct fd f = fdget(ufd);
  2461. struct bpf_link *link;
  2462. if (!f.file)
  2463. return ERR_PTR(-EBADF);
  2464. if (f.file->f_op != &bpf_link_fops) {
  2465. fdput(f);
  2466. return ERR_PTR(-EINVAL);
  2467. }
  2468. link = f.file->private_data;
  2469. bpf_link_inc(link);
  2470. fdput(f);
  2471. return link;
  2472. }
  2473. EXPORT_SYMBOL(bpf_link_get_from_fd);
  2474. static void bpf_tracing_link_release(struct bpf_link *link)
  2475. {
  2476. struct bpf_tracing_link *tr_link =
  2477. container_of(link, struct bpf_tracing_link, link.link);
  2478. WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
  2479. tr_link->trampoline));
  2480. bpf_trampoline_put(tr_link->trampoline);
  2481. /* tgt_prog is NULL if target is a kernel function */
  2482. if (tr_link->tgt_prog)
  2483. bpf_prog_put(tr_link->tgt_prog);
  2484. }
  2485. static void bpf_tracing_link_dealloc(struct bpf_link *link)
  2486. {
  2487. struct bpf_tracing_link *tr_link =
  2488. container_of(link, struct bpf_tracing_link, link.link);
  2489. kfree(tr_link);
  2490. }
  2491. static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
  2492. struct seq_file *seq)
  2493. {
  2494. struct bpf_tracing_link *tr_link =
  2495. container_of(link, struct bpf_tracing_link, link.link);
  2496. seq_printf(seq,
  2497. "attach_type:\t%d\n",
  2498. tr_link->attach_type);
  2499. }
  2500. static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
  2501. struct bpf_link_info *info)
  2502. {
  2503. struct bpf_tracing_link *tr_link =
  2504. container_of(link, struct bpf_tracing_link, link.link);
  2505. info->tracing.attach_type = tr_link->attach_type;
  2506. bpf_trampoline_unpack_key(tr_link->trampoline->key,
  2507. &info->tracing.target_obj_id,
  2508. &info->tracing.target_btf_id);
  2509. return 0;
  2510. }
  2511. static const struct bpf_link_ops bpf_tracing_link_lops = {
  2512. .release = bpf_tracing_link_release,
  2513. .dealloc = bpf_tracing_link_dealloc,
  2514. .show_fdinfo = bpf_tracing_link_show_fdinfo,
  2515. .fill_link_info = bpf_tracing_link_fill_link_info,
  2516. };
  2517. static int bpf_tracing_prog_attach(struct bpf_prog *prog,
  2518. int tgt_prog_fd,
  2519. u32 btf_id,
  2520. u64 bpf_cookie)
  2521. {
  2522. struct bpf_link_primer link_primer;
  2523. struct bpf_prog *tgt_prog = NULL;
  2524. struct bpf_trampoline *tr = NULL;
  2525. struct bpf_tracing_link *link;
  2526. u64 key = 0;
  2527. int err;
  2528. switch (prog->type) {
  2529. case BPF_PROG_TYPE_TRACING:
  2530. if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
  2531. prog->expected_attach_type != BPF_TRACE_FEXIT &&
  2532. prog->expected_attach_type != BPF_MODIFY_RETURN) {
  2533. err = -EINVAL;
  2534. goto out_put_prog;
  2535. }
  2536. break;
  2537. case BPF_PROG_TYPE_EXT:
  2538. if (prog->expected_attach_type != 0) {
  2539. err = -EINVAL;
  2540. goto out_put_prog;
  2541. }
  2542. break;
  2543. case BPF_PROG_TYPE_LSM:
  2544. if (prog->expected_attach_type != BPF_LSM_MAC) {
  2545. err = -EINVAL;
  2546. goto out_put_prog;
  2547. }
  2548. break;
  2549. default:
  2550. err = -EINVAL;
  2551. goto out_put_prog;
  2552. }
  2553. if (!!tgt_prog_fd != !!btf_id) {
  2554. err = -EINVAL;
  2555. goto out_put_prog;
  2556. }
  2557. if (tgt_prog_fd) {
  2558. /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
  2559. if (prog->type != BPF_PROG_TYPE_EXT) {
  2560. err = -EINVAL;
  2561. goto out_put_prog;
  2562. }
  2563. tgt_prog = bpf_prog_get(tgt_prog_fd);
  2564. if (IS_ERR(tgt_prog)) {
  2565. err = PTR_ERR(tgt_prog);
  2566. tgt_prog = NULL;
  2567. goto out_put_prog;
  2568. }
  2569. key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
  2570. }
  2571. link = kzalloc(sizeof(*link), GFP_USER);
  2572. if (!link) {
  2573. err = -ENOMEM;
  2574. goto out_put_prog;
  2575. }
  2576. bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
  2577. &bpf_tracing_link_lops, prog);
  2578. link->attach_type = prog->expected_attach_type;
  2579. link->link.cookie = bpf_cookie;
  2580. mutex_lock(&prog->aux->dst_mutex);
  2581. /* There are a few possible cases here:
  2582. *
  2583. * - if prog->aux->dst_trampoline is set, the program was just loaded
  2584. * and not yet attached to anything, so we can use the values stored
  2585. * in prog->aux
  2586. *
  2587. * - if prog->aux->dst_trampoline is NULL, the program has already been
  2588. * attached to a target and its initial target was cleared (below)
  2589. *
  2590. * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
  2591. * target_btf_id using the link_create API.
  2592. *
  2593. * - if tgt_prog == NULL when this function was called using the old
  2594. * raw_tracepoint_open API, and we need a target from prog->aux
  2595. *
  2596. * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
  2597. * was detached and is going for re-attachment.
  2598. */
  2599. if (!prog->aux->dst_trampoline && !tgt_prog) {
  2600. /*
  2601. * Allow re-attach for TRACING and LSM programs. If it's
  2602. * currently linked, bpf_trampoline_link_prog will fail.
  2603. * EXT programs need to specify tgt_prog_fd, so they
  2604. * re-attach in separate code path.
  2605. */
  2606. if (prog->type != BPF_PROG_TYPE_TRACING &&
  2607. prog->type != BPF_PROG_TYPE_LSM) {
  2608. err = -EINVAL;
  2609. goto out_unlock;
  2610. }
  2611. btf_id = prog->aux->attach_btf_id;
  2612. key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
  2613. }
  2614. if (!prog->aux->dst_trampoline ||
  2615. (key && key != prog->aux->dst_trampoline->key)) {
  2616. /* If there is no saved target, or the specified target is
  2617. * different from the destination specified at load time, we
  2618. * need a new trampoline and a check for compatibility
  2619. */
  2620. struct bpf_attach_target_info tgt_info = {};
  2621. err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
  2622. &tgt_info);
  2623. if (err)
  2624. goto out_unlock;
  2625. tr = bpf_trampoline_get(key, &tgt_info);
  2626. if (!tr) {
  2627. err = -ENOMEM;
  2628. goto out_unlock;
  2629. }
  2630. } else {
  2631. /* The caller didn't specify a target, or the target was the
  2632. * same as the destination supplied during program load. This
  2633. * means we can reuse the trampoline and reference from program
  2634. * load time, and there is no need to allocate a new one. This
  2635. * can only happen once for any program, as the saved values in
  2636. * prog->aux are cleared below.
  2637. */
  2638. tr = prog->aux->dst_trampoline;
  2639. tgt_prog = prog->aux->dst_prog;
  2640. }
  2641. err = bpf_link_prime(&link->link.link, &link_primer);
  2642. if (err)
  2643. goto out_unlock;
  2644. err = bpf_trampoline_link_prog(&link->link, tr);
  2645. if (err) {
  2646. bpf_link_cleanup(&link_primer);
  2647. link = NULL;
  2648. goto out_unlock;
  2649. }
  2650. link->tgt_prog = tgt_prog;
  2651. link->trampoline = tr;
  2652. /* Always clear the trampoline and target prog from prog->aux to make
  2653. * sure the original attach destination is not kept alive after a
  2654. * program is (re-)attached to another target.
  2655. */
  2656. if (prog->aux->dst_prog &&
  2657. (tgt_prog_fd || tr != prog->aux->dst_trampoline))
  2658. /* got extra prog ref from syscall, or attaching to different prog */
  2659. bpf_prog_put(prog->aux->dst_prog);
  2660. if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
  2661. /* we allocated a new trampoline, so free the old one */
  2662. bpf_trampoline_put(prog->aux->dst_trampoline);
  2663. prog->aux->dst_prog = NULL;
  2664. prog->aux->dst_trampoline = NULL;
  2665. mutex_unlock(&prog->aux->dst_mutex);
  2666. return bpf_link_settle(&link_primer);
  2667. out_unlock:
  2668. if (tr && tr != prog->aux->dst_trampoline)
  2669. bpf_trampoline_put(tr);
  2670. mutex_unlock(&prog->aux->dst_mutex);
  2671. kfree(link);
  2672. out_put_prog:
  2673. if (tgt_prog_fd && tgt_prog)
  2674. bpf_prog_put(tgt_prog);
  2675. return err;
  2676. }
  2677. struct bpf_raw_tp_link {
  2678. struct bpf_link link;
  2679. struct bpf_raw_event_map *btp;
  2680. };
  2681. static void bpf_raw_tp_link_release(struct bpf_link *link)
  2682. {
  2683. struct bpf_raw_tp_link *raw_tp =
  2684. container_of(link, struct bpf_raw_tp_link, link);
  2685. bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
  2686. bpf_put_raw_tracepoint(raw_tp->btp);
  2687. }
  2688. static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
  2689. {
  2690. struct bpf_raw_tp_link *raw_tp =
  2691. container_of(link, struct bpf_raw_tp_link, link);
  2692. kfree(raw_tp);
  2693. }
  2694. static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
  2695. struct seq_file *seq)
  2696. {
  2697. struct bpf_raw_tp_link *raw_tp_link =
  2698. container_of(link, struct bpf_raw_tp_link, link);
  2699. seq_printf(seq,
  2700. "tp_name:\t%s\n",
  2701. raw_tp_link->btp->tp->name);
  2702. }
  2703. static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
  2704. struct bpf_link_info *info)
  2705. {
  2706. struct bpf_raw_tp_link *raw_tp_link =
  2707. container_of(link, struct bpf_raw_tp_link, link);
  2708. char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
  2709. const char *tp_name = raw_tp_link->btp->tp->name;
  2710. u32 ulen = info->raw_tracepoint.tp_name_len;
  2711. size_t tp_len = strlen(tp_name);
  2712. if (!ulen ^ !ubuf)
  2713. return -EINVAL;
  2714. info->raw_tracepoint.tp_name_len = tp_len + 1;
  2715. if (!ubuf)
  2716. return 0;
  2717. if (ulen >= tp_len + 1) {
  2718. if (copy_to_user(ubuf, tp_name, tp_len + 1))
  2719. return -EFAULT;
  2720. } else {
  2721. char zero = '\0';
  2722. if (copy_to_user(ubuf, tp_name, ulen - 1))
  2723. return -EFAULT;
  2724. if (put_user(zero, ubuf + ulen - 1))
  2725. return -EFAULT;
  2726. return -ENOSPC;
  2727. }
  2728. return 0;
  2729. }
  2730. static const struct bpf_link_ops bpf_raw_tp_link_lops = {
  2731. .release = bpf_raw_tp_link_release,
  2732. .dealloc = bpf_raw_tp_link_dealloc,
  2733. .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
  2734. .fill_link_info = bpf_raw_tp_link_fill_link_info,
  2735. };
  2736. #ifdef CONFIG_PERF_EVENTS
  2737. struct bpf_perf_link {
  2738. struct bpf_link link;
  2739. struct file *perf_file;
  2740. };
  2741. static void bpf_perf_link_release(struct bpf_link *link)
  2742. {
  2743. struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
  2744. struct perf_event *event = perf_link->perf_file->private_data;
  2745. perf_event_free_bpf_prog(event);
  2746. fput(perf_link->perf_file);
  2747. }
  2748. static void bpf_perf_link_dealloc(struct bpf_link *link)
  2749. {
  2750. struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
  2751. kfree(perf_link);
  2752. }
  2753. static const struct bpf_link_ops bpf_perf_link_lops = {
  2754. .release = bpf_perf_link_release,
  2755. .dealloc = bpf_perf_link_dealloc,
  2756. };
  2757. static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2758. {
  2759. struct bpf_link_primer link_primer;
  2760. struct bpf_perf_link *link;
  2761. struct perf_event *event;
  2762. struct file *perf_file;
  2763. int err;
  2764. if (attr->link_create.flags)
  2765. return -EINVAL;
  2766. perf_file = perf_event_get(attr->link_create.target_fd);
  2767. if (IS_ERR(perf_file))
  2768. return PTR_ERR(perf_file);
  2769. link = kzalloc(sizeof(*link), GFP_USER);
  2770. if (!link) {
  2771. err = -ENOMEM;
  2772. goto out_put_file;
  2773. }
  2774. bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
  2775. link->perf_file = perf_file;
  2776. err = bpf_link_prime(&link->link, &link_primer);
  2777. if (err) {
  2778. kfree(link);
  2779. goto out_put_file;
  2780. }
  2781. event = perf_file->private_data;
  2782. err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
  2783. if (err) {
  2784. bpf_link_cleanup(&link_primer);
  2785. goto out_put_file;
  2786. }
  2787. /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
  2788. bpf_prog_inc(prog);
  2789. return bpf_link_settle(&link_primer);
  2790. out_put_file:
  2791. fput(perf_file);
  2792. return err;
  2793. }
  2794. #else
  2795. static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  2796. {
  2797. return -EOPNOTSUPP;
  2798. }
  2799. #endif /* CONFIG_PERF_EVENTS */
  2800. static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
  2801. const char __user *user_tp_name)
  2802. {
  2803. struct bpf_link_primer link_primer;
  2804. struct bpf_raw_tp_link *link;
  2805. struct bpf_raw_event_map *btp;
  2806. const char *tp_name;
  2807. char buf[128];
  2808. int err;
  2809. switch (prog->type) {
  2810. case BPF_PROG_TYPE_TRACING:
  2811. case BPF_PROG_TYPE_EXT:
  2812. case BPF_PROG_TYPE_LSM:
  2813. if (user_tp_name)
  2814. /* The attach point for this category of programs
  2815. * should be specified via btf_id during program load.
  2816. */
  2817. return -EINVAL;
  2818. if (prog->type == BPF_PROG_TYPE_TRACING &&
  2819. prog->expected_attach_type == BPF_TRACE_RAW_TP) {
  2820. tp_name = prog->aux->attach_func_name;
  2821. break;
  2822. }
  2823. return bpf_tracing_prog_attach(prog, 0, 0, 0);
  2824. case BPF_PROG_TYPE_RAW_TRACEPOINT:
  2825. case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
  2826. if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
  2827. return -EFAULT;
  2828. buf[sizeof(buf) - 1] = 0;
  2829. tp_name = buf;
  2830. break;
  2831. default:
  2832. return -EINVAL;
  2833. }
  2834. btp = bpf_get_raw_tracepoint(tp_name);
  2835. if (!btp)
  2836. return -ENOENT;
  2837. link = kzalloc(sizeof(*link), GFP_USER);
  2838. if (!link) {
  2839. err = -ENOMEM;
  2840. goto out_put_btp;
  2841. }
  2842. bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
  2843. &bpf_raw_tp_link_lops, prog);
  2844. link->btp = btp;
  2845. err = bpf_link_prime(&link->link, &link_primer);
  2846. if (err) {
  2847. kfree(link);
  2848. goto out_put_btp;
  2849. }
  2850. err = bpf_probe_register(link->btp, prog);
  2851. if (err) {
  2852. bpf_link_cleanup(&link_primer);
  2853. goto out_put_btp;
  2854. }
  2855. return bpf_link_settle(&link_primer);
  2856. out_put_btp:
  2857. bpf_put_raw_tracepoint(btp);
  2858. return err;
  2859. }
  2860. #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
  2861. static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
  2862. {
  2863. struct bpf_prog *prog;
  2864. int fd;
  2865. if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
  2866. return -EINVAL;
  2867. prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
  2868. if (IS_ERR(prog))
  2869. return PTR_ERR(prog);
  2870. fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
  2871. if (fd < 0)
  2872. bpf_prog_put(prog);
  2873. return fd;
  2874. }
  2875. static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
  2876. enum bpf_attach_type attach_type)
  2877. {
  2878. switch (prog->type) {
  2879. case BPF_PROG_TYPE_CGROUP_SOCK:
  2880. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  2881. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  2882. case BPF_PROG_TYPE_SK_LOOKUP:
  2883. return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
  2884. case BPF_PROG_TYPE_CGROUP_SKB:
  2885. if (!capable(CAP_NET_ADMIN))
  2886. /* cg-skb progs can be loaded by unpriv user.
  2887. * check permissions at attach time.
  2888. */
  2889. return -EPERM;
  2890. return prog->enforce_expected_attach_type &&
  2891. prog->expected_attach_type != attach_type ?
  2892. -EINVAL : 0;
  2893. case BPF_PROG_TYPE_KPROBE:
  2894. if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
  2895. attach_type != BPF_TRACE_KPROBE_MULTI)
  2896. return -EINVAL;
  2897. return 0;
  2898. default:
  2899. return 0;
  2900. }
  2901. }
  2902. static enum bpf_prog_type
  2903. attach_type_to_prog_type(enum bpf_attach_type attach_type)
  2904. {
  2905. switch (attach_type) {
  2906. case BPF_CGROUP_INET_INGRESS:
  2907. case BPF_CGROUP_INET_EGRESS:
  2908. return BPF_PROG_TYPE_CGROUP_SKB;
  2909. case BPF_CGROUP_INET_SOCK_CREATE:
  2910. case BPF_CGROUP_INET_SOCK_RELEASE:
  2911. case BPF_CGROUP_INET4_POST_BIND:
  2912. case BPF_CGROUP_INET6_POST_BIND:
  2913. return BPF_PROG_TYPE_CGROUP_SOCK;
  2914. case BPF_CGROUP_INET4_BIND:
  2915. case BPF_CGROUP_INET6_BIND:
  2916. case BPF_CGROUP_INET4_CONNECT:
  2917. case BPF_CGROUP_INET6_CONNECT:
  2918. case BPF_CGROUP_INET4_GETPEERNAME:
  2919. case BPF_CGROUP_INET6_GETPEERNAME:
  2920. case BPF_CGROUP_INET4_GETSOCKNAME:
  2921. case BPF_CGROUP_INET6_GETSOCKNAME:
  2922. case BPF_CGROUP_UDP4_SENDMSG:
  2923. case BPF_CGROUP_UDP6_SENDMSG:
  2924. case BPF_CGROUP_UDP4_RECVMSG:
  2925. case BPF_CGROUP_UDP6_RECVMSG:
  2926. return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
  2927. case BPF_CGROUP_SOCK_OPS:
  2928. return BPF_PROG_TYPE_SOCK_OPS;
  2929. case BPF_CGROUP_DEVICE:
  2930. return BPF_PROG_TYPE_CGROUP_DEVICE;
  2931. case BPF_SK_MSG_VERDICT:
  2932. return BPF_PROG_TYPE_SK_MSG;
  2933. case BPF_SK_SKB_STREAM_PARSER:
  2934. case BPF_SK_SKB_STREAM_VERDICT:
  2935. case BPF_SK_SKB_VERDICT:
  2936. return BPF_PROG_TYPE_SK_SKB;
  2937. case BPF_LIRC_MODE2:
  2938. return BPF_PROG_TYPE_LIRC_MODE2;
  2939. case BPF_FLOW_DISSECTOR:
  2940. return BPF_PROG_TYPE_FLOW_DISSECTOR;
  2941. case BPF_CGROUP_SYSCTL:
  2942. return BPF_PROG_TYPE_CGROUP_SYSCTL;
  2943. case BPF_CGROUP_GETSOCKOPT:
  2944. case BPF_CGROUP_SETSOCKOPT:
  2945. return BPF_PROG_TYPE_CGROUP_SOCKOPT;
  2946. case BPF_TRACE_ITER:
  2947. case BPF_TRACE_RAW_TP:
  2948. case BPF_TRACE_FENTRY:
  2949. case BPF_TRACE_FEXIT:
  2950. case BPF_MODIFY_RETURN:
  2951. return BPF_PROG_TYPE_TRACING;
  2952. case BPF_LSM_MAC:
  2953. return BPF_PROG_TYPE_LSM;
  2954. case BPF_SK_LOOKUP:
  2955. return BPF_PROG_TYPE_SK_LOOKUP;
  2956. case BPF_XDP:
  2957. return BPF_PROG_TYPE_XDP;
  2958. case BPF_LSM_CGROUP:
  2959. return BPF_PROG_TYPE_LSM;
  2960. default:
  2961. return BPF_PROG_TYPE_UNSPEC;
  2962. }
  2963. }
  2964. #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
  2965. #define BPF_F_ATTACH_MASK \
  2966. (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
  2967. static int bpf_prog_attach(const union bpf_attr *attr)
  2968. {
  2969. enum bpf_prog_type ptype;
  2970. struct bpf_prog *prog;
  2971. int ret;
  2972. if (CHECK_ATTR(BPF_PROG_ATTACH))
  2973. return -EINVAL;
  2974. if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
  2975. return -EINVAL;
  2976. ptype = attach_type_to_prog_type(attr->attach_type);
  2977. if (ptype == BPF_PROG_TYPE_UNSPEC)
  2978. return -EINVAL;
  2979. prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
  2980. if (IS_ERR(prog))
  2981. return PTR_ERR(prog);
  2982. if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
  2983. bpf_prog_put(prog);
  2984. return -EINVAL;
  2985. }
  2986. switch (ptype) {
  2987. case BPF_PROG_TYPE_SK_SKB:
  2988. case BPF_PROG_TYPE_SK_MSG:
  2989. ret = sock_map_get_from_fd(attr, prog);
  2990. break;
  2991. case BPF_PROG_TYPE_LIRC_MODE2:
  2992. ret = lirc_prog_attach(attr, prog);
  2993. break;
  2994. case BPF_PROG_TYPE_FLOW_DISSECTOR:
  2995. ret = netns_bpf_prog_attach(attr, prog);
  2996. break;
  2997. case BPF_PROG_TYPE_CGROUP_DEVICE:
  2998. case BPF_PROG_TYPE_CGROUP_SKB:
  2999. case BPF_PROG_TYPE_CGROUP_SOCK:
  3000. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  3001. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  3002. case BPF_PROG_TYPE_CGROUP_SYSCTL:
  3003. case BPF_PROG_TYPE_SOCK_OPS:
  3004. case BPF_PROG_TYPE_LSM:
  3005. if (ptype == BPF_PROG_TYPE_LSM &&
  3006. prog->expected_attach_type != BPF_LSM_CGROUP)
  3007. ret = -EINVAL;
  3008. else
  3009. ret = cgroup_bpf_prog_attach(attr, ptype, prog);
  3010. break;
  3011. default:
  3012. ret = -EINVAL;
  3013. }
  3014. if (ret)
  3015. bpf_prog_put(prog);
  3016. return ret;
  3017. }
  3018. #define BPF_PROG_DETACH_LAST_FIELD attach_type
  3019. static int bpf_prog_detach(const union bpf_attr *attr)
  3020. {
  3021. enum bpf_prog_type ptype;
  3022. if (CHECK_ATTR(BPF_PROG_DETACH))
  3023. return -EINVAL;
  3024. ptype = attach_type_to_prog_type(attr->attach_type);
  3025. switch (ptype) {
  3026. case BPF_PROG_TYPE_SK_MSG:
  3027. case BPF_PROG_TYPE_SK_SKB:
  3028. return sock_map_prog_detach(attr, ptype);
  3029. case BPF_PROG_TYPE_LIRC_MODE2:
  3030. return lirc_prog_detach(attr);
  3031. case BPF_PROG_TYPE_FLOW_DISSECTOR:
  3032. return netns_bpf_prog_detach(attr, ptype);
  3033. case BPF_PROG_TYPE_CGROUP_DEVICE:
  3034. case BPF_PROG_TYPE_CGROUP_SKB:
  3035. case BPF_PROG_TYPE_CGROUP_SOCK:
  3036. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  3037. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  3038. case BPF_PROG_TYPE_CGROUP_SYSCTL:
  3039. case BPF_PROG_TYPE_SOCK_OPS:
  3040. case BPF_PROG_TYPE_LSM:
  3041. return cgroup_bpf_prog_detach(attr, ptype);
  3042. default:
  3043. return -EINVAL;
  3044. }
  3045. }
  3046. #define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
  3047. static int bpf_prog_query(const union bpf_attr *attr,
  3048. union bpf_attr __user *uattr)
  3049. {
  3050. if (!capable(CAP_NET_ADMIN))
  3051. return -EPERM;
  3052. if (CHECK_ATTR(BPF_PROG_QUERY))
  3053. return -EINVAL;
  3054. if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
  3055. return -EINVAL;
  3056. switch (attr->query.attach_type) {
  3057. case BPF_CGROUP_INET_INGRESS:
  3058. case BPF_CGROUP_INET_EGRESS:
  3059. case BPF_CGROUP_INET_SOCK_CREATE:
  3060. case BPF_CGROUP_INET_SOCK_RELEASE:
  3061. case BPF_CGROUP_INET4_BIND:
  3062. case BPF_CGROUP_INET6_BIND:
  3063. case BPF_CGROUP_INET4_POST_BIND:
  3064. case BPF_CGROUP_INET6_POST_BIND:
  3065. case BPF_CGROUP_INET4_CONNECT:
  3066. case BPF_CGROUP_INET6_CONNECT:
  3067. case BPF_CGROUP_INET4_GETPEERNAME:
  3068. case BPF_CGROUP_INET6_GETPEERNAME:
  3069. case BPF_CGROUP_INET4_GETSOCKNAME:
  3070. case BPF_CGROUP_INET6_GETSOCKNAME:
  3071. case BPF_CGROUP_UDP4_SENDMSG:
  3072. case BPF_CGROUP_UDP6_SENDMSG:
  3073. case BPF_CGROUP_UDP4_RECVMSG:
  3074. case BPF_CGROUP_UDP6_RECVMSG:
  3075. case BPF_CGROUP_SOCK_OPS:
  3076. case BPF_CGROUP_DEVICE:
  3077. case BPF_CGROUP_SYSCTL:
  3078. case BPF_CGROUP_GETSOCKOPT:
  3079. case BPF_CGROUP_SETSOCKOPT:
  3080. case BPF_LSM_CGROUP:
  3081. return cgroup_bpf_prog_query(attr, uattr);
  3082. case BPF_LIRC_MODE2:
  3083. return lirc_prog_query(attr, uattr);
  3084. case BPF_FLOW_DISSECTOR:
  3085. case BPF_SK_LOOKUP:
  3086. return netns_bpf_prog_query(attr, uattr);
  3087. case BPF_SK_SKB_STREAM_PARSER:
  3088. case BPF_SK_SKB_STREAM_VERDICT:
  3089. case BPF_SK_MSG_VERDICT:
  3090. case BPF_SK_SKB_VERDICT:
  3091. return sock_map_bpf_prog_query(attr, uattr);
  3092. default:
  3093. return -EINVAL;
  3094. }
  3095. }
  3096. #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
  3097. static int bpf_prog_test_run(const union bpf_attr *attr,
  3098. union bpf_attr __user *uattr)
  3099. {
  3100. struct bpf_prog *prog;
  3101. int ret = -ENOTSUPP;
  3102. if (CHECK_ATTR(BPF_PROG_TEST_RUN))
  3103. return -EINVAL;
  3104. if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
  3105. (!attr->test.ctx_size_in && attr->test.ctx_in))
  3106. return -EINVAL;
  3107. if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
  3108. (!attr->test.ctx_size_out && attr->test.ctx_out))
  3109. return -EINVAL;
  3110. prog = bpf_prog_get(attr->test.prog_fd);
  3111. if (IS_ERR(prog))
  3112. return PTR_ERR(prog);
  3113. if (prog->aux->ops->test_run)
  3114. ret = prog->aux->ops->test_run(prog, attr, uattr);
  3115. bpf_prog_put(prog);
  3116. return ret;
  3117. }
  3118. #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
  3119. static int bpf_obj_get_next_id(const union bpf_attr *attr,
  3120. union bpf_attr __user *uattr,
  3121. struct idr *idr,
  3122. spinlock_t *lock)
  3123. {
  3124. u32 next_id = attr->start_id;
  3125. int err = 0;
  3126. if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
  3127. return -EINVAL;
  3128. if (!capable(CAP_SYS_ADMIN))
  3129. return -EPERM;
  3130. next_id++;
  3131. spin_lock_bh(lock);
  3132. if (!idr_get_next(idr, &next_id))
  3133. err = -ENOENT;
  3134. spin_unlock_bh(lock);
  3135. if (!err)
  3136. err = put_user(next_id, &uattr->next_id);
  3137. return err;
  3138. }
  3139. struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
  3140. {
  3141. struct bpf_map *map;
  3142. spin_lock_bh(&map_idr_lock);
  3143. again:
  3144. map = idr_get_next(&map_idr, id);
  3145. if (map) {
  3146. map = __bpf_map_inc_not_zero(map, false);
  3147. if (IS_ERR(map)) {
  3148. (*id)++;
  3149. goto again;
  3150. }
  3151. }
  3152. spin_unlock_bh(&map_idr_lock);
  3153. return map;
  3154. }
  3155. struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
  3156. {
  3157. struct bpf_prog *prog;
  3158. spin_lock_bh(&prog_idr_lock);
  3159. again:
  3160. prog = idr_get_next(&prog_idr, id);
  3161. if (prog) {
  3162. prog = bpf_prog_inc_not_zero(prog);
  3163. if (IS_ERR(prog)) {
  3164. (*id)++;
  3165. goto again;
  3166. }
  3167. }
  3168. spin_unlock_bh(&prog_idr_lock);
  3169. return prog;
  3170. }
  3171. #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
  3172. struct bpf_prog *bpf_prog_by_id(u32 id)
  3173. {
  3174. struct bpf_prog *prog;
  3175. if (!id)
  3176. return ERR_PTR(-ENOENT);
  3177. spin_lock_bh(&prog_idr_lock);
  3178. prog = idr_find(&prog_idr, id);
  3179. if (prog)
  3180. prog = bpf_prog_inc_not_zero(prog);
  3181. else
  3182. prog = ERR_PTR(-ENOENT);
  3183. spin_unlock_bh(&prog_idr_lock);
  3184. return prog;
  3185. }
  3186. static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
  3187. {
  3188. struct bpf_prog *prog;
  3189. u32 id = attr->prog_id;
  3190. int fd;
  3191. if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
  3192. return -EINVAL;
  3193. if (!capable(CAP_SYS_ADMIN))
  3194. return -EPERM;
  3195. prog = bpf_prog_by_id(id);
  3196. if (IS_ERR(prog))
  3197. return PTR_ERR(prog);
  3198. fd = bpf_prog_new_fd(prog);
  3199. if (fd < 0)
  3200. bpf_prog_put(prog);
  3201. return fd;
  3202. }
  3203. #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
  3204. static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
  3205. {
  3206. struct bpf_map *map;
  3207. u32 id = attr->map_id;
  3208. int f_flags;
  3209. int fd;
  3210. if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
  3211. attr->open_flags & ~BPF_OBJ_FLAG_MASK)
  3212. return -EINVAL;
  3213. if (!capable(CAP_SYS_ADMIN))
  3214. return -EPERM;
  3215. f_flags = bpf_get_file_flag(attr->open_flags);
  3216. if (f_flags < 0)
  3217. return f_flags;
  3218. spin_lock_bh(&map_idr_lock);
  3219. map = idr_find(&map_idr, id);
  3220. if (map)
  3221. map = __bpf_map_inc_not_zero(map, true);
  3222. else
  3223. map = ERR_PTR(-ENOENT);
  3224. spin_unlock_bh(&map_idr_lock);
  3225. if (IS_ERR(map))
  3226. return PTR_ERR(map);
  3227. fd = bpf_map_new_fd(map, f_flags);
  3228. if (fd < 0)
  3229. bpf_map_put_with_uref(map);
  3230. return fd;
  3231. }
  3232. static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
  3233. unsigned long addr, u32 *off,
  3234. u32 *type)
  3235. {
  3236. const struct bpf_map *map;
  3237. int i;
  3238. mutex_lock(&prog->aux->used_maps_mutex);
  3239. for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
  3240. map = prog->aux->used_maps[i];
  3241. if (map == (void *)addr) {
  3242. *type = BPF_PSEUDO_MAP_FD;
  3243. goto out;
  3244. }
  3245. if (!map->ops->map_direct_value_meta)
  3246. continue;
  3247. if (!map->ops->map_direct_value_meta(map, addr, off)) {
  3248. *type = BPF_PSEUDO_MAP_VALUE;
  3249. goto out;
  3250. }
  3251. }
  3252. map = NULL;
  3253. out:
  3254. mutex_unlock(&prog->aux->used_maps_mutex);
  3255. return map;
  3256. }
  3257. static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
  3258. const struct cred *f_cred)
  3259. {
  3260. const struct bpf_map *map;
  3261. struct bpf_insn *insns;
  3262. u32 off, type;
  3263. u64 imm;
  3264. u8 code;
  3265. int i;
  3266. insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
  3267. GFP_USER);
  3268. if (!insns)
  3269. return insns;
  3270. for (i = 0; i < prog->len; i++) {
  3271. code = insns[i].code;
  3272. if (code == (BPF_JMP | BPF_TAIL_CALL)) {
  3273. insns[i].code = BPF_JMP | BPF_CALL;
  3274. insns[i].imm = BPF_FUNC_tail_call;
  3275. /* fall-through */
  3276. }
  3277. if (code == (BPF_JMP | BPF_CALL) ||
  3278. code == (BPF_JMP | BPF_CALL_ARGS)) {
  3279. if (code == (BPF_JMP | BPF_CALL_ARGS))
  3280. insns[i].code = BPF_JMP | BPF_CALL;
  3281. if (!bpf_dump_raw_ok(f_cred))
  3282. insns[i].imm = 0;
  3283. continue;
  3284. }
  3285. if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
  3286. insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
  3287. continue;
  3288. }
  3289. if (code != (BPF_LD | BPF_IMM | BPF_DW))
  3290. continue;
  3291. imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
  3292. map = bpf_map_from_imm(prog, imm, &off, &type);
  3293. if (map) {
  3294. insns[i].src_reg = type;
  3295. insns[i].imm = map->id;
  3296. insns[i + 1].imm = off;
  3297. continue;
  3298. }
  3299. }
  3300. return insns;
  3301. }
  3302. static int set_info_rec_size(struct bpf_prog_info *info)
  3303. {
  3304. /*
  3305. * Ensure info.*_rec_size is the same as kernel expected size
  3306. *
  3307. * or
  3308. *
  3309. * Only allow zero *_rec_size if both _rec_size and _cnt are
  3310. * zero. In this case, the kernel will set the expected
  3311. * _rec_size back to the info.
  3312. */
  3313. if ((info->nr_func_info || info->func_info_rec_size) &&
  3314. info->func_info_rec_size != sizeof(struct bpf_func_info))
  3315. return -EINVAL;
  3316. if ((info->nr_line_info || info->line_info_rec_size) &&
  3317. info->line_info_rec_size != sizeof(struct bpf_line_info))
  3318. return -EINVAL;
  3319. if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
  3320. info->jited_line_info_rec_size != sizeof(__u64))
  3321. return -EINVAL;
  3322. info->func_info_rec_size = sizeof(struct bpf_func_info);
  3323. info->line_info_rec_size = sizeof(struct bpf_line_info);
  3324. info->jited_line_info_rec_size = sizeof(__u64);
  3325. return 0;
  3326. }
  3327. static int bpf_prog_get_info_by_fd(struct file *file,
  3328. struct bpf_prog *prog,
  3329. const union bpf_attr *attr,
  3330. union bpf_attr __user *uattr)
  3331. {
  3332. struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  3333. struct btf *attach_btf = bpf_prog_get_target_btf(prog);
  3334. struct bpf_prog_info info;
  3335. u32 info_len = attr->info.info_len;
  3336. struct bpf_prog_kstats stats;
  3337. char __user *uinsns;
  3338. u32 ulen;
  3339. int err;
  3340. err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
  3341. if (err)
  3342. return err;
  3343. info_len = min_t(u32, sizeof(info), info_len);
  3344. memset(&info, 0, sizeof(info));
  3345. if (copy_from_user(&info, uinfo, info_len))
  3346. return -EFAULT;
  3347. info.type = prog->type;
  3348. info.id = prog->aux->id;
  3349. info.load_time = prog->aux->load_time;
  3350. info.created_by_uid = from_kuid_munged(current_user_ns(),
  3351. prog->aux->user->uid);
  3352. info.gpl_compatible = prog->gpl_compatible;
  3353. memcpy(info.tag, prog->tag, sizeof(prog->tag));
  3354. memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
  3355. mutex_lock(&prog->aux->used_maps_mutex);
  3356. ulen = info.nr_map_ids;
  3357. info.nr_map_ids = prog->aux->used_map_cnt;
  3358. ulen = min_t(u32, info.nr_map_ids, ulen);
  3359. if (ulen) {
  3360. u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
  3361. u32 i;
  3362. for (i = 0; i < ulen; i++)
  3363. if (put_user(prog->aux->used_maps[i]->id,
  3364. &user_map_ids[i])) {
  3365. mutex_unlock(&prog->aux->used_maps_mutex);
  3366. return -EFAULT;
  3367. }
  3368. }
  3369. mutex_unlock(&prog->aux->used_maps_mutex);
  3370. err = set_info_rec_size(&info);
  3371. if (err)
  3372. return err;
  3373. bpf_prog_get_stats(prog, &stats);
  3374. info.run_time_ns = stats.nsecs;
  3375. info.run_cnt = stats.cnt;
  3376. info.recursion_misses = stats.misses;
  3377. info.verified_insns = prog->aux->verified_insns;
  3378. if (!bpf_capable()) {
  3379. info.jited_prog_len = 0;
  3380. info.xlated_prog_len = 0;
  3381. info.nr_jited_ksyms = 0;
  3382. info.nr_jited_func_lens = 0;
  3383. info.nr_func_info = 0;
  3384. info.nr_line_info = 0;
  3385. info.nr_jited_line_info = 0;
  3386. goto done;
  3387. }
  3388. ulen = info.xlated_prog_len;
  3389. info.xlated_prog_len = bpf_prog_insn_size(prog);
  3390. if (info.xlated_prog_len && ulen) {
  3391. struct bpf_insn *insns_sanitized;
  3392. bool fault;
  3393. if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
  3394. info.xlated_prog_insns = 0;
  3395. goto done;
  3396. }
  3397. insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
  3398. if (!insns_sanitized)
  3399. return -ENOMEM;
  3400. uinsns = u64_to_user_ptr(info.xlated_prog_insns);
  3401. ulen = min_t(u32, info.xlated_prog_len, ulen);
  3402. fault = copy_to_user(uinsns, insns_sanitized, ulen);
  3403. kfree(insns_sanitized);
  3404. if (fault)
  3405. return -EFAULT;
  3406. }
  3407. if (bpf_prog_is_dev_bound(prog->aux)) {
  3408. err = bpf_prog_offload_info_fill(&info, prog);
  3409. if (err)
  3410. return err;
  3411. goto done;
  3412. }
  3413. /* NOTE: the following code is supposed to be skipped for offload.
  3414. * bpf_prog_offload_info_fill() is the place to fill similar fields
  3415. * for offload.
  3416. */
  3417. ulen = info.jited_prog_len;
  3418. if (prog->aux->func_cnt) {
  3419. u32 i;
  3420. info.jited_prog_len = 0;
  3421. for (i = 0; i < prog->aux->func_cnt; i++)
  3422. info.jited_prog_len += prog->aux->func[i]->jited_len;
  3423. } else {
  3424. info.jited_prog_len = prog->jited_len;
  3425. }
  3426. if (info.jited_prog_len && ulen) {
  3427. if (bpf_dump_raw_ok(file->f_cred)) {
  3428. uinsns = u64_to_user_ptr(info.jited_prog_insns);
  3429. ulen = min_t(u32, info.jited_prog_len, ulen);
  3430. /* for multi-function programs, copy the JITed
  3431. * instructions for all the functions
  3432. */
  3433. if (prog->aux->func_cnt) {
  3434. u32 len, free, i;
  3435. u8 *img;
  3436. free = ulen;
  3437. for (i = 0; i < prog->aux->func_cnt; i++) {
  3438. len = prog->aux->func[i]->jited_len;
  3439. len = min_t(u32, len, free);
  3440. img = (u8 *) prog->aux->func[i]->bpf_func;
  3441. if (copy_to_user(uinsns, img, len))
  3442. return -EFAULT;
  3443. uinsns += len;
  3444. free -= len;
  3445. if (!free)
  3446. break;
  3447. }
  3448. } else {
  3449. if (copy_to_user(uinsns, prog->bpf_func, ulen))
  3450. return -EFAULT;
  3451. }
  3452. } else {
  3453. info.jited_prog_insns = 0;
  3454. }
  3455. }
  3456. ulen = info.nr_jited_ksyms;
  3457. info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
  3458. if (ulen) {
  3459. if (bpf_dump_raw_ok(file->f_cred)) {
  3460. unsigned long ksym_addr;
  3461. u64 __user *user_ksyms;
  3462. u32 i;
  3463. /* copy the address of the kernel symbol
  3464. * corresponding to each function
  3465. */
  3466. ulen = min_t(u32, info.nr_jited_ksyms, ulen);
  3467. user_ksyms = u64_to_user_ptr(info.jited_ksyms);
  3468. if (prog->aux->func_cnt) {
  3469. for (i = 0; i < ulen; i++) {
  3470. ksym_addr = (unsigned long)
  3471. prog->aux->func[i]->bpf_func;
  3472. if (put_user((u64) ksym_addr,
  3473. &user_ksyms[i]))
  3474. return -EFAULT;
  3475. }
  3476. } else {
  3477. ksym_addr = (unsigned long) prog->bpf_func;
  3478. if (put_user((u64) ksym_addr, &user_ksyms[0]))
  3479. return -EFAULT;
  3480. }
  3481. } else {
  3482. info.jited_ksyms = 0;
  3483. }
  3484. }
  3485. ulen = info.nr_jited_func_lens;
  3486. info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
  3487. if (ulen) {
  3488. if (bpf_dump_raw_ok(file->f_cred)) {
  3489. u32 __user *user_lens;
  3490. u32 func_len, i;
  3491. /* copy the JITed image lengths for each function */
  3492. ulen = min_t(u32, info.nr_jited_func_lens, ulen);
  3493. user_lens = u64_to_user_ptr(info.jited_func_lens);
  3494. if (prog->aux->func_cnt) {
  3495. for (i = 0; i < ulen; i++) {
  3496. func_len =
  3497. prog->aux->func[i]->jited_len;
  3498. if (put_user(func_len, &user_lens[i]))
  3499. return -EFAULT;
  3500. }
  3501. } else {
  3502. func_len = prog->jited_len;
  3503. if (put_user(func_len, &user_lens[0]))
  3504. return -EFAULT;
  3505. }
  3506. } else {
  3507. info.jited_func_lens = 0;
  3508. }
  3509. }
  3510. if (prog->aux->btf)
  3511. info.btf_id = btf_obj_id(prog->aux->btf);
  3512. info.attach_btf_id = prog->aux->attach_btf_id;
  3513. if (attach_btf)
  3514. info.attach_btf_obj_id = btf_obj_id(attach_btf);
  3515. ulen = info.nr_func_info;
  3516. info.nr_func_info = prog->aux->func_info_cnt;
  3517. if (info.nr_func_info && ulen) {
  3518. char __user *user_finfo;
  3519. user_finfo = u64_to_user_ptr(info.func_info);
  3520. ulen = min_t(u32, info.nr_func_info, ulen);
  3521. if (copy_to_user(user_finfo, prog->aux->func_info,
  3522. info.func_info_rec_size * ulen))
  3523. return -EFAULT;
  3524. }
  3525. ulen = info.nr_line_info;
  3526. info.nr_line_info = prog->aux->nr_linfo;
  3527. if (info.nr_line_info && ulen) {
  3528. __u8 __user *user_linfo;
  3529. user_linfo = u64_to_user_ptr(info.line_info);
  3530. ulen = min_t(u32, info.nr_line_info, ulen);
  3531. if (copy_to_user(user_linfo, prog->aux->linfo,
  3532. info.line_info_rec_size * ulen))
  3533. return -EFAULT;
  3534. }
  3535. ulen = info.nr_jited_line_info;
  3536. if (prog->aux->jited_linfo)
  3537. info.nr_jited_line_info = prog->aux->nr_linfo;
  3538. else
  3539. info.nr_jited_line_info = 0;
  3540. if (info.nr_jited_line_info && ulen) {
  3541. if (bpf_dump_raw_ok(file->f_cred)) {
  3542. unsigned long line_addr;
  3543. __u64 __user *user_linfo;
  3544. u32 i;
  3545. user_linfo = u64_to_user_ptr(info.jited_line_info);
  3546. ulen = min_t(u32, info.nr_jited_line_info, ulen);
  3547. for (i = 0; i < ulen; i++) {
  3548. line_addr = (unsigned long)prog->aux->jited_linfo[i];
  3549. if (put_user((__u64)line_addr, &user_linfo[i]))
  3550. return -EFAULT;
  3551. }
  3552. } else {
  3553. info.jited_line_info = 0;
  3554. }
  3555. }
  3556. ulen = info.nr_prog_tags;
  3557. info.nr_prog_tags = prog->aux->func_cnt ? : 1;
  3558. if (ulen) {
  3559. __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
  3560. u32 i;
  3561. user_prog_tags = u64_to_user_ptr(info.prog_tags);
  3562. ulen = min_t(u32, info.nr_prog_tags, ulen);
  3563. if (prog->aux->func_cnt) {
  3564. for (i = 0; i < ulen; i++) {
  3565. if (copy_to_user(user_prog_tags[i],
  3566. prog->aux->func[i]->tag,
  3567. BPF_TAG_SIZE))
  3568. return -EFAULT;
  3569. }
  3570. } else {
  3571. if (copy_to_user(user_prog_tags[0],
  3572. prog->tag, BPF_TAG_SIZE))
  3573. return -EFAULT;
  3574. }
  3575. }
  3576. done:
  3577. if (copy_to_user(uinfo, &info, info_len) ||
  3578. put_user(info_len, &uattr->info.info_len))
  3579. return -EFAULT;
  3580. return 0;
  3581. }
  3582. static int bpf_map_get_info_by_fd(struct file *file,
  3583. struct bpf_map *map,
  3584. const union bpf_attr *attr,
  3585. union bpf_attr __user *uattr)
  3586. {
  3587. struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  3588. struct bpf_map_info info;
  3589. u32 info_len = attr->info.info_len;
  3590. int err;
  3591. err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
  3592. if (err)
  3593. return err;
  3594. info_len = min_t(u32, sizeof(info), info_len);
  3595. memset(&info, 0, sizeof(info));
  3596. info.type = map->map_type;
  3597. info.id = map->id;
  3598. info.key_size = map->key_size;
  3599. info.value_size = map->value_size;
  3600. info.max_entries = map->max_entries;
  3601. info.map_flags = map->map_flags;
  3602. info.map_extra = map->map_extra;
  3603. memcpy(info.name, map->name, sizeof(map->name));
  3604. if (map->btf) {
  3605. info.btf_id = btf_obj_id(map->btf);
  3606. info.btf_key_type_id = map->btf_key_type_id;
  3607. info.btf_value_type_id = map->btf_value_type_id;
  3608. }
  3609. info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
  3610. if (bpf_map_is_dev_bound(map)) {
  3611. err = bpf_map_offload_info_fill(&info, map);
  3612. if (err)
  3613. return err;
  3614. }
  3615. if (copy_to_user(uinfo, &info, info_len) ||
  3616. put_user(info_len, &uattr->info.info_len))
  3617. return -EFAULT;
  3618. return 0;
  3619. }
  3620. static int bpf_btf_get_info_by_fd(struct file *file,
  3621. struct btf *btf,
  3622. const union bpf_attr *attr,
  3623. union bpf_attr __user *uattr)
  3624. {
  3625. struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  3626. u32 info_len = attr->info.info_len;
  3627. int err;
  3628. err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
  3629. if (err)
  3630. return err;
  3631. return btf_get_info_by_fd(btf, attr, uattr);
  3632. }
  3633. static int bpf_link_get_info_by_fd(struct file *file,
  3634. struct bpf_link *link,
  3635. const union bpf_attr *attr,
  3636. union bpf_attr __user *uattr)
  3637. {
  3638. struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
  3639. struct bpf_link_info info;
  3640. u32 info_len = attr->info.info_len;
  3641. int err;
  3642. err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
  3643. if (err)
  3644. return err;
  3645. info_len = min_t(u32, sizeof(info), info_len);
  3646. memset(&info, 0, sizeof(info));
  3647. if (copy_from_user(&info, uinfo, info_len))
  3648. return -EFAULT;
  3649. info.type = link->type;
  3650. info.id = link->id;
  3651. info.prog_id = link->prog->aux->id;
  3652. if (link->ops->fill_link_info) {
  3653. err = link->ops->fill_link_info(link, &info);
  3654. if (err)
  3655. return err;
  3656. }
  3657. if (copy_to_user(uinfo, &info, info_len) ||
  3658. put_user(info_len, &uattr->info.info_len))
  3659. return -EFAULT;
  3660. return 0;
  3661. }
  3662. #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
  3663. static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
  3664. union bpf_attr __user *uattr)
  3665. {
  3666. int ufd = attr->info.bpf_fd;
  3667. struct fd f;
  3668. int err;
  3669. if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
  3670. return -EINVAL;
  3671. f = fdget(ufd);
  3672. if (!f.file)
  3673. return -EBADFD;
  3674. if (f.file->f_op == &bpf_prog_fops)
  3675. err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
  3676. uattr);
  3677. else if (f.file->f_op == &bpf_map_fops)
  3678. err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
  3679. uattr);
  3680. else if (f.file->f_op == &btf_fops)
  3681. err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
  3682. else if (f.file->f_op == &bpf_link_fops)
  3683. err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
  3684. attr, uattr);
  3685. else
  3686. err = -EINVAL;
  3687. fdput(f);
  3688. return err;
  3689. }
  3690. #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
  3691. static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
  3692. {
  3693. if (CHECK_ATTR(BPF_BTF_LOAD))
  3694. return -EINVAL;
  3695. if (!bpf_capable())
  3696. return -EPERM;
  3697. return btf_new_fd(attr, uattr);
  3698. }
  3699. #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
  3700. static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
  3701. {
  3702. if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
  3703. return -EINVAL;
  3704. if (!capable(CAP_SYS_ADMIN))
  3705. return -EPERM;
  3706. return btf_get_fd_by_id(attr->btf_id);
  3707. }
  3708. static int bpf_task_fd_query_copy(const union bpf_attr *attr,
  3709. union bpf_attr __user *uattr,
  3710. u32 prog_id, u32 fd_type,
  3711. const char *buf, u64 probe_offset,
  3712. u64 probe_addr)
  3713. {
  3714. char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
  3715. u32 len = buf ? strlen(buf) : 0, input_len;
  3716. int err = 0;
  3717. if (put_user(len, &uattr->task_fd_query.buf_len))
  3718. return -EFAULT;
  3719. input_len = attr->task_fd_query.buf_len;
  3720. if (input_len && ubuf) {
  3721. if (!len) {
  3722. /* nothing to copy, just make ubuf NULL terminated */
  3723. char zero = '\0';
  3724. if (put_user(zero, ubuf))
  3725. return -EFAULT;
  3726. } else if (input_len >= len + 1) {
  3727. /* ubuf can hold the string with NULL terminator */
  3728. if (copy_to_user(ubuf, buf, len + 1))
  3729. return -EFAULT;
  3730. } else {
  3731. /* ubuf cannot hold the string with NULL terminator,
  3732. * do a partial copy with NULL terminator.
  3733. */
  3734. char zero = '\0';
  3735. err = -ENOSPC;
  3736. if (copy_to_user(ubuf, buf, input_len - 1))
  3737. return -EFAULT;
  3738. if (put_user(zero, ubuf + input_len - 1))
  3739. return -EFAULT;
  3740. }
  3741. }
  3742. if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
  3743. put_user(fd_type, &uattr->task_fd_query.fd_type) ||
  3744. put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
  3745. put_user(probe_addr, &uattr->task_fd_query.probe_addr))
  3746. return -EFAULT;
  3747. return err;
  3748. }
  3749. #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
  3750. static int bpf_task_fd_query(const union bpf_attr *attr,
  3751. union bpf_attr __user *uattr)
  3752. {
  3753. pid_t pid = attr->task_fd_query.pid;
  3754. u32 fd = attr->task_fd_query.fd;
  3755. const struct perf_event *event;
  3756. struct task_struct *task;
  3757. struct file *file;
  3758. int err;
  3759. if (CHECK_ATTR(BPF_TASK_FD_QUERY))
  3760. return -EINVAL;
  3761. if (!capable(CAP_SYS_ADMIN))
  3762. return -EPERM;
  3763. if (attr->task_fd_query.flags != 0)
  3764. return -EINVAL;
  3765. rcu_read_lock();
  3766. task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
  3767. rcu_read_unlock();
  3768. if (!task)
  3769. return -ENOENT;
  3770. err = 0;
  3771. file = fget_task(task, fd);
  3772. put_task_struct(task);
  3773. if (!file)
  3774. return -EBADF;
  3775. if (file->f_op == &bpf_link_fops) {
  3776. struct bpf_link *link = file->private_data;
  3777. if (link->ops == &bpf_raw_tp_link_lops) {
  3778. struct bpf_raw_tp_link *raw_tp =
  3779. container_of(link, struct bpf_raw_tp_link, link);
  3780. struct bpf_raw_event_map *btp = raw_tp->btp;
  3781. err = bpf_task_fd_query_copy(attr, uattr,
  3782. raw_tp->link.prog->aux->id,
  3783. BPF_FD_TYPE_RAW_TRACEPOINT,
  3784. btp->tp->name, 0, 0);
  3785. goto put_file;
  3786. }
  3787. goto out_not_supp;
  3788. }
  3789. event = perf_get_event(file);
  3790. if (!IS_ERR(event)) {
  3791. u64 probe_offset, probe_addr;
  3792. u32 prog_id, fd_type;
  3793. const char *buf;
  3794. err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
  3795. &buf, &probe_offset,
  3796. &probe_addr);
  3797. if (!err)
  3798. err = bpf_task_fd_query_copy(attr, uattr, prog_id,
  3799. fd_type, buf,
  3800. probe_offset,
  3801. probe_addr);
  3802. goto put_file;
  3803. }
  3804. out_not_supp:
  3805. err = -ENOTSUPP;
  3806. put_file:
  3807. fput(file);
  3808. return err;
  3809. }
  3810. #define BPF_MAP_BATCH_LAST_FIELD batch.flags
  3811. #define BPF_DO_BATCH(fn) \
  3812. do { \
  3813. if (!fn) { \
  3814. err = -ENOTSUPP; \
  3815. goto err_put; \
  3816. } \
  3817. err = fn(map, attr, uattr); \
  3818. } while (0)
  3819. static int bpf_map_do_batch(const union bpf_attr *attr,
  3820. union bpf_attr __user *uattr,
  3821. int cmd)
  3822. {
  3823. bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
  3824. cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
  3825. bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
  3826. struct bpf_map *map;
  3827. int err, ufd;
  3828. struct fd f;
  3829. if (CHECK_ATTR(BPF_MAP_BATCH))
  3830. return -EINVAL;
  3831. ufd = attr->batch.map_fd;
  3832. f = fdget(ufd);
  3833. map = __bpf_map_get(f);
  3834. if (IS_ERR(map))
  3835. return PTR_ERR(map);
  3836. if (has_write)
  3837. bpf_map_write_active_inc(map);
  3838. if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
  3839. err = -EPERM;
  3840. goto err_put;
  3841. }
  3842. if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
  3843. err = -EPERM;
  3844. goto err_put;
  3845. }
  3846. if (cmd == BPF_MAP_LOOKUP_BATCH)
  3847. BPF_DO_BATCH(map->ops->map_lookup_batch);
  3848. else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
  3849. BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
  3850. else if (cmd == BPF_MAP_UPDATE_BATCH)
  3851. BPF_DO_BATCH(map->ops->map_update_batch);
  3852. else
  3853. BPF_DO_BATCH(map->ops->map_delete_batch);
  3854. err_put:
  3855. if (has_write)
  3856. bpf_map_write_active_dec(map);
  3857. fdput(f);
  3858. return err;
  3859. }
  3860. #define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
  3861. static int link_create(union bpf_attr *attr, bpfptr_t uattr)
  3862. {
  3863. enum bpf_prog_type ptype;
  3864. struct bpf_prog *prog;
  3865. int ret;
  3866. if (CHECK_ATTR(BPF_LINK_CREATE))
  3867. return -EINVAL;
  3868. prog = bpf_prog_get(attr->link_create.prog_fd);
  3869. if (IS_ERR(prog))
  3870. return PTR_ERR(prog);
  3871. ret = bpf_prog_attach_check_attach_type(prog,
  3872. attr->link_create.attach_type);
  3873. if (ret)
  3874. goto out;
  3875. switch (prog->type) {
  3876. case BPF_PROG_TYPE_EXT:
  3877. break;
  3878. case BPF_PROG_TYPE_PERF_EVENT:
  3879. case BPF_PROG_TYPE_TRACEPOINT:
  3880. if (attr->link_create.attach_type != BPF_PERF_EVENT) {
  3881. ret = -EINVAL;
  3882. goto out;
  3883. }
  3884. break;
  3885. case BPF_PROG_TYPE_KPROBE:
  3886. if (attr->link_create.attach_type != BPF_PERF_EVENT &&
  3887. attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
  3888. ret = -EINVAL;
  3889. goto out;
  3890. }
  3891. break;
  3892. default:
  3893. ptype = attach_type_to_prog_type(attr->link_create.attach_type);
  3894. if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
  3895. ret = -EINVAL;
  3896. goto out;
  3897. }
  3898. break;
  3899. }
  3900. switch (prog->type) {
  3901. case BPF_PROG_TYPE_CGROUP_SKB:
  3902. case BPF_PROG_TYPE_CGROUP_SOCK:
  3903. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  3904. case BPF_PROG_TYPE_SOCK_OPS:
  3905. case BPF_PROG_TYPE_CGROUP_DEVICE:
  3906. case BPF_PROG_TYPE_CGROUP_SYSCTL:
  3907. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  3908. ret = cgroup_bpf_link_attach(attr, prog);
  3909. break;
  3910. case BPF_PROG_TYPE_EXT:
  3911. ret = bpf_tracing_prog_attach(prog,
  3912. attr->link_create.target_fd,
  3913. attr->link_create.target_btf_id,
  3914. attr->link_create.tracing.cookie);
  3915. break;
  3916. case BPF_PROG_TYPE_LSM:
  3917. case BPF_PROG_TYPE_TRACING:
  3918. if (attr->link_create.attach_type != prog->expected_attach_type) {
  3919. ret = -EINVAL;
  3920. goto out;
  3921. }
  3922. if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
  3923. ret = bpf_raw_tp_link_attach(prog, NULL);
  3924. else if (prog->expected_attach_type == BPF_TRACE_ITER)
  3925. ret = bpf_iter_link_attach(attr, uattr, prog);
  3926. else if (prog->expected_attach_type == BPF_LSM_CGROUP)
  3927. ret = cgroup_bpf_link_attach(attr, prog);
  3928. else
  3929. ret = bpf_tracing_prog_attach(prog,
  3930. attr->link_create.target_fd,
  3931. attr->link_create.target_btf_id,
  3932. attr->link_create.tracing.cookie);
  3933. break;
  3934. case BPF_PROG_TYPE_FLOW_DISSECTOR:
  3935. case BPF_PROG_TYPE_SK_LOOKUP:
  3936. ret = netns_bpf_link_create(attr, prog);
  3937. break;
  3938. #ifdef CONFIG_NET
  3939. case BPF_PROG_TYPE_XDP:
  3940. ret = bpf_xdp_link_attach(attr, prog);
  3941. break;
  3942. #endif
  3943. case BPF_PROG_TYPE_PERF_EVENT:
  3944. case BPF_PROG_TYPE_TRACEPOINT:
  3945. ret = bpf_perf_link_attach(attr, prog);
  3946. break;
  3947. case BPF_PROG_TYPE_KPROBE:
  3948. if (attr->link_create.attach_type == BPF_PERF_EVENT)
  3949. ret = bpf_perf_link_attach(attr, prog);
  3950. else
  3951. ret = bpf_kprobe_multi_link_attach(attr, prog);
  3952. break;
  3953. default:
  3954. ret = -EINVAL;
  3955. }
  3956. out:
  3957. if (ret < 0)
  3958. bpf_prog_put(prog);
  3959. return ret;
  3960. }
  3961. #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
  3962. static int link_update(union bpf_attr *attr)
  3963. {
  3964. struct bpf_prog *old_prog = NULL, *new_prog;
  3965. struct bpf_link *link;
  3966. u32 flags;
  3967. int ret;
  3968. if (CHECK_ATTR(BPF_LINK_UPDATE))
  3969. return -EINVAL;
  3970. flags = attr->link_update.flags;
  3971. if (flags & ~BPF_F_REPLACE)
  3972. return -EINVAL;
  3973. link = bpf_link_get_from_fd(attr->link_update.link_fd);
  3974. if (IS_ERR(link))
  3975. return PTR_ERR(link);
  3976. new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
  3977. if (IS_ERR(new_prog)) {
  3978. ret = PTR_ERR(new_prog);
  3979. goto out_put_link;
  3980. }
  3981. if (flags & BPF_F_REPLACE) {
  3982. old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
  3983. if (IS_ERR(old_prog)) {
  3984. ret = PTR_ERR(old_prog);
  3985. old_prog = NULL;
  3986. goto out_put_progs;
  3987. }
  3988. } else if (attr->link_update.old_prog_fd) {
  3989. ret = -EINVAL;
  3990. goto out_put_progs;
  3991. }
  3992. if (link->ops->update_prog)
  3993. ret = link->ops->update_prog(link, new_prog, old_prog);
  3994. else
  3995. ret = -EINVAL;
  3996. out_put_progs:
  3997. if (old_prog)
  3998. bpf_prog_put(old_prog);
  3999. if (ret)
  4000. bpf_prog_put(new_prog);
  4001. out_put_link:
  4002. bpf_link_put(link);
  4003. return ret;
  4004. }
  4005. #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
  4006. static int link_detach(union bpf_attr *attr)
  4007. {
  4008. struct bpf_link *link;
  4009. int ret;
  4010. if (CHECK_ATTR(BPF_LINK_DETACH))
  4011. return -EINVAL;
  4012. link = bpf_link_get_from_fd(attr->link_detach.link_fd);
  4013. if (IS_ERR(link))
  4014. return PTR_ERR(link);
  4015. if (link->ops->detach)
  4016. ret = link->ops->detach(link);
  4017. else
  4018. ret = -EOPNOTSUPP;
  4019. bpf_link_put(link);
  4020. return ret;
  4021. }
  4022. static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
  4023. {
  4024. return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
  4025. }
  4026. struct bpf_link *bpf_link_by_id(u32 id)
  4027. {
  4028. struct bpf_link *link;
  4029. if (!id)
  4030. return ERR_PTR(-ENOENT);
  4031. spin_lock_bh(&link_idr_lock);
  4032. /* before link is "settled", ID is 0, pretend it doesn't exist yet */
  4033. link = idr_find(&link_idr, id);
  4034. if (link) {
  4035. if (link->id)
  4036. link = bpf_link_inc_not_zero(link);
  4037. else
  4038. link = ERR_PTR(-EAGAIN);
  4039. } else {
  4040. link = ERR_PTR(-ENOENT);
  4041. }
  4042. spin_unlock_bh(&link_idr_lock);
  4043. return link;
  4044. }
  4045. struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
  4046. {
  4047. struct bpf_link *link;
  4048. spin_lock_bh(&link_idr_lock);
  4049. again:
  4050. link = idr_get_next(&link_idr, id);
  4051. if (link) {
  4052. link = bpf_link_inc_not_zero(link);
  4053. if (IS_ERR(link)) {
  4054. (*id)++;
  4055. goto again;
  4056. }
  4057. }
  4058. spin_unlock_bh(&link_idr_lock);
  4059. return link;
  4060. }
  4061. #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
  4062. static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
  4063. {
  4064. struct bpf_link *link;
  4065. u32 id = attr->link_id;
  4066. int fd;
  4067. if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
  4068. return -EINVAL;
  4069. if (!capable(CAP_SYS_ADMIN))
  4070. return -EPERM;
  4071. link = bpf_link_by_id(id);
  4072. if (IS_ERR(link))
  4073. return PTR_ERR(link);
  4074. fd = bpf_link_new_fd(link);
  4075. if (fd < 0)
  4076. bpf_link_put(link);
  4077. return fd;
  4078. }
  4079. DEFINE_MUTEX(bpf_stats_enabled_mutex);
  4080. static int bpf_stats_release(struct inode *inode, struct file *file)
  4081. {
  4082. mutex_lock(&bpf_stats_enabled_mutex);
  4083. static_key_slow_dec(&bpf_stats_enabled_key.key);
  4084. mutex_unlock(&bpf_stats_enabled_mutex);
  4085. return 0;
  4086. }
  4087. static const struct file_operations bpf_stats_fops = {
  4088. .release = bpf_stats_release,
  4089. };
  4090. static int bpf_enable_runtime_stats(void)
  4091. {
  4092. int fd;
  4093. mutex_lock(&bpf_stats_enabled_mutex);
  4094. /* Set a very high limit to avoid overflow */
  4095. if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
  4096. mutex_unlock(&bpf_stats_enabled_mutex);
  4097. return -EBUSY;
  4098. }
  4099. fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
  4100. if (fd >= 0)
  4101. static_key_slow_inc(&bpf_stats_enabled_key.key);
  4102. mutex_unlock(&bpf_stats_enabled_mutex);
  4103. return fd;
  4104. }
  4105. #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
  4106. static int bpf_enable_stats(union bpf_attr *attr)
  4107. {
  4108. if (CHECK_ATTR(BPF_ENABLE_STATS))
  4109. return -EINVAL;
  4110. if (!capable(CAP_SYS_ADMIN))
  4111. return -EPERM;
  4112. switch (attr->enable_stats.type) {
  4113. case BPF_STATS_RUN_TIME:
  4114. return bpf_enable_runtime_stats();
  4115. default:
  4116. break;
  4117. }
  4118. return -EINVAL;
  4119. }
  4120. #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
  4121. static int bpf_iter_create(union bpf_attr *attr)
  4122. {
  4123. struct bpf_link *link;
  4124. int err;
  4125. if (CHECK_ATTR(BPF_ITER_CREATE))
  4126. return -EINVAL;
  4127. if (attr->iter_create.flags)
  4128. return -EINVAL;
  4129. link = bpf_link_get_from_fd(attr->iter_create.link_fd);
  4130. if (IS_ERR(link))
  4131. return PTR_ERR(link);
  4132. err = bpf_iter_new_fd(link);
  4133. bpf_link_put(link);
  4134. return err;
  4135. }
  4136. #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
  4137. static int bpf_prog_bind_map(union bpf_attr *attr)
  4138. {
  4139. struct bpf_prog *prog;
  4140. struct bpf_map *map;
  4141. struct bpf_map **used_maps_old, **used_maps_new;
  4142. int i, ret = 0;
  4143. if (CHECK_ATTR(BPF_PROG_BIND_MAP))
  4144. return -EINVAL;
  4145. if (attr->prog_bind_map.flags)
  4146. return -EINVAL;
  4147. prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
  4148. if (IS_ERR(prog))
  4149. return PTR_ERR(prog);
  4150. map = bpf_map_get(attr->prog_bind_map.map_fd);
  4151. if (IS_ERR(map)) {
  4152. ret = PTR_ERR(map);
  4153. goto out_prog_put;
  4154. }
  4155. mutex_lock(&prog->aux->used_maps_mutex);
  4156. used_maps_old = prog->aux->used_maps;
  4157. for (i = 0; i < prog->aux->used_map_cnt; i++)
  4158. if (used_maps_old[i] == map) {
  4159. bpf_map_put(map);
  4160. goto out_unlock;
  4161. }
  4162. used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
  4163. sizeof(used_maps_new[0]),
  4164. GFP_KERNEL);
  4165. if (!used_maps_new) {
  4166. ret = -ENOMEM;
  4167. goto out_unlock;
  4168. }
  4169. memcpy(used_maps_new, used_maps_old,
  4170. sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
  4171. used_maps_new[prog->aux->used_map_cnt] = map;
  4172. prog->aux->used_map_cnt++;
  4173. prog->aux->used_maps = used_maps_new;
  4174. kfree(used_maps_old);
  4175. out_unlock:
  4176. mutex_unlock(&prog->aux->used_maps_mutex);
  4177. if (ret)
  4178. bpf_map_put(map);
  4179. out_prog_put:
  4180. bpf_prog_put(prog);
  4181. return ret;
  4182. }
  4183. static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
  4184. {
  4185. union bpf_attr attr;
  4186. bool capable;
  4187. int err;
  4188. capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
  4189. /* Intent here is for unprivileged_bpf_disabled to block key object
  4190. * creation commands for unprivileged users; other actions depend
  4191. * of fd availability and access to bpffs, so are dependent on
  4192. * object creation success. Capabilities are later verified for
  4193. * operations such as load and map create, so even with unprivileged
  4194. * BPF disabled, capability checks are still carried out for these
  4195. * and other operations.
  4196. */
  4197. if (!capable &&
  4198. (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
  4199. return -EPERM;
  4200. err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
  4201. if (err)
  4202. return err;
  4203. size = min_t(u32, size, sizeof(attr));
  4204. /* copy attributes from user space, may be less than sizeof(bpf_attr) */
  4205. memset(&attr, 0, sizeof(attr));
  4206. if (copy_from_bpfptr(&attr, uattr, size) != 0)
  4207. return -EFAULT;
  4208. trace_android_vh_check_bpf_syscall(cmd, &attr, size);
  4209. err = security_bpf(cmd, &attr, size);
  4210. if (err < 0)
  4211. return err;
  4212. switch (cmd) {
  4213. case BPF_MAP_CREATE:
  4214. err = map_create(&attr);
  4215. break;
  4216. case BPF_MAP_LOOKUP_ELEM:
  4217. err = map_lookup_elem(&attr);
  4218. break;
  4219. case BPF_MAP_UPDATE_ELEM:
  4220. err = map_update_elem(&attr, uattr);
  4221. break;
  4222. case BPF_MAP_DELETE_ELEM:
  4223. err = map_delete_elem(&attr, uattr);
  4224. break;
  4225. case BPF_MAP_GET_NEXT_KEY:
  4226. err = map_get_next_key(&attr);
  4227. break;
  4228. case BPF_MAP_FREEZE:
  4229. err = map_freeze(&attr);
  4230. break;
  4231. case BPF_PROG_LOAD:
  4232. err = bpf_prog_load(&attr, uattr);
  4233. break;
  4234. case BPF_OBJ_PIN:
  4235. err = bpf_obj_pin(&attr);
  4236. break;
  4237. case BPF_OBJ_GET:
  4238. err = bpf_obj_get(&attr);
  4239. break;
  4240. case BPF_PROG_ATTACH:
  4241. err = bpf_prog_attach(&attr);
  4242. break;
  4243. case BPF_PROG_DETACH:
  4244. err = bpf_prog_detach(&attr);
  4245. break;
  4246. case BPF_PROG_QUERY:
  4247. err = bpf_prog_query(&attr, uattr.user);
  4248. break;
  4249. case BPF_PROG_TEST_RUN:
  4250. err = bpf_prog_test_run(&attr, uattr.user);
  4251. break;
  4252. case BPF_PROG_GET_NEXT_ID:
  4253. err = bpf_obj_get_next_id(&attr, uattr.user,
  4254. &prog_idr, &prog_idr_lock);
  4255. break;
  4256. case BPF_MAP_GET_NEXT_ID:
  4257. err = bpf_obj_get_next_id(&attr, uattr.user,
  4258. &map_idr, &map_idr_lock);
  4259. break;
  4260. case BPF_BTF_GET_NEXT_ID:
  4261. err = bpf_obj_get_next_id(&attr, uattr.user,
  4262. &btf_idr, &btf_idr_lock);
  4263. break;
  4264. case BPF_PROG_GET_FD_BY_ID:
  4265. err = bpf_prog_get_fd_by_id(&attr);
  4266. break;
  4267. case BPF_MAP_GET_FD_BY_ID:
  4268. err = bpf_map_get_fd_by_id(&attr);
  4269. break;
  4270. case BPF_OBJ_GET_INFO_BY_FD:
  4271. err = bpf_obj_get_info_by_fd(&attr, uattr.user);
  4272. break;
  4273. case BPF_RAW_TRACEPOINT_OPEN:
  4274. err = bpf_raw_tracepoint_open(&attr);
  4275. break;
  4276. case BPF_BTF_LOAD:
  4277. err = bpf_btf_load(&attr, uattr);
  4278. break;
  4279. case BPF_BTF_GET_FD_BY_ID:
  4280. err = bpf_btf_get_fd_by_id(&attr);
  4281. break;
  4282. case BPF_TASK_FD_QUERY:
  4283. err = bpf_task_fd_query(&attr, uattr.user);
  4284. break;
  4285. case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
  4286. err = map_lookup_and_delete_elem(&attr);
  4287. break;
  4288. case BPF_MAP_LOOKUP_BATCH:
  4289. err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
  4290. break;
  4291. case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
  4292. err = bpf_map_do_batch(&attr, uattr.user,
  4293. BPF_MAP_LOOKUP_AND_DELETE_BATCH);
  4294. break;
  4295. case BPF_MAP_UPDATE_BATCH:
  4296. err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
  4297. break;
  4298. case BPF_MAP_DELETE_BATCH:
  4299. err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
  4300. break;
  4301. case BPF_LINK_CREATE:
  4302. err = link_create(&attr, uattr);
  4303. break;
  4304. case BPF_LINK_UPDATE:
  4305. err = link_update(&attr);
  4306. break;
  4307. case BPF_LINK_GET_FD_BY_ID:
  4308. err = bpf_link_get_fd_by_id(&attr);
  4309. break;
  4310. case BPF_LINK_GET_NEXT_ID:
  4311. err = bpf_obj_get_next_id(&attr, uattr.user,
  4312. &link_idr, &link_idr_lock);
  4313. break;
  4314. case BPF_ENABLE_STATS:
  4315. err = bpf_enable_stats(&attr);
  4316. break;
  4317. case BPF_ITER_CREATE:
  4318. err = bpf_iter_create(&attr);
  4319. break;
  4320. case BPF_LINK_DETACH:
  4321. err = link_detach(&attr);
  4322. break;
  4323. case BPF_PROG_BIND_MAP:
  4324. err = bpf_prog_bind_map(&attr);
  4325. break;
  4326. default:
  4327. err = -EINVAL;
  4328. break;
  4329. }
  4330. return err;
  4331. }
  4332. SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
  4333. {
  4334. return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
  4335. }
  4336. static bool syscall_prog_is_valid_access(int off, int size,
  4337. enum bpf_access_type type,
  4338. const struct bpf_prog *prog,
  4339. struct bpf_insn_access_aux *info)
  4340. {
  4341. if (off < 0 || off >= U16_MAX)
  4342. return false;
  4343. if (off % size != 0)
  4344. return false;
  4345. return true;
  4346. }
  4347. BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
  4348. {
  4349. switch (cmd) {
  4350. case BPF_MAP_CREATE:
  4351. case BPF_MAP_DELETE_ELEM:
  4352. case BPF_MAP_UPDATE_ELEM:
  4353. case BPF_MAP_FREEZE:
  4354. case BPF_MAP_GET_FD_BY_ID:
  4355. case BPF_PROG_LOAD:
  4356. case BPF_BTF_LOAD:
  4357. case BPF_LINK_CREATE:
  4358. case BPF_RAW_TRACEPOINT_OPEN:
  4359. break;
  4360. default:
  4361. return -EINVAL;
  4362. }
  4363. return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
  4364. }
  4365. /* To shut up -Wmissing-prototypes.
  4366. * This function is used by the kernel light skeleton
  4367. * to load bpf programs when modules are loaded or during kernel boot.
  4368. * See tools/lib/bpf/skel_internal.h
  4369. */
  4370. int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
  4371. int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
  4372. {
  4373. struct bpf_prog * __maybe_unused prog;
  4374. struct bpf_tramp_run_ctx __maybe_unused run_ctx;
  4375. switch (cmd) {
  4376. #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
  4377. case BPF_PROG_TEST_RUN:
  4378. if (attr->test.data_in || attr->test.data_out ||
  4379. attr->test.ctx_out || attr->test.duration ||
  4380. attr->test.repeat || attr->test.flags)
  4381. return -EINVAL;
  4382. prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
  4383. if (IS_ERR(prog))
  4384. return PTR_ERR(prog);
  4385. if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
  4386. attr->test.ctx_size_in > U16_MAX) {
  4387. bpf_prog_put(prog);
  4388. return -EINVAL;
  4389. }
  4390. run_ctx.bpf_cookie = 0;
  4391. if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
  4392. /* recursion detected */
  4393. __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);
  4394. bpf_prog_put(prog);
  4395. return -EBUSY;
  4396. }
  4397. attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
  4398. __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
  4399. &run_ctx);
  4400. bpf_prog_put(prog);
  4401. return 0;
  4402. #endif
  4403. default:
  4404. return ____bpf_sys_bpf(cmd, attr, size);
  4405. }
  4406. }
  4407. EXPORT_SYMBOL(kern_sys_bpf);
  4408. static const struct bpf_func_proto bpf_sys_bpf_proto = {
  4409. .func = bpf_sys_bpf,
  4410. .gpl_only = false,
  4411. .ret_type = RET_INTEGER,
  4412. .arg1_type = ARG_ANYTHING,
  4413. .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  4414. .arg3_type = ARG_CONST_SIZE,
  4415. };
  4416. const struct bpf_func_proto * __weak
  4417. tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  4418. {
  4419. return bpf_base_func_proto(func_id);
  4420. }
  4421. BPF_CALL_1(bpf_sys_close, u32, fd)
  4422. {
  4423. /* When bpf program calls this helper there should not be
  4424. * an fdget() without matching completed fdput().
  4425. * This helper is allowed in the following callchain only:
  4426. * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
  4427. */
  4428. return close_fd(fd);
  4429. }
  4430. static const struct bpf_func_proto bpf_sys_close_proto = {
  4431. .func = bpf_sys_close,
  4432. .gpl_only = false,
  4433. .ret_type = RET_INTEGER,
  4434. .arg1_type = ARG_ANYTHING,
  4435. };
  4436. BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
  4437. {
  4438. if (flags)
  4439. return -EINVAL;
  4440. if (name_sz <= 1 || name[name_sz - 1])
  4441. return -EINVAL;
  4442. if (!bpf_dump_raw_ok(current_cred()))
  4443. return -EPERM;
  4444. *res = kallsyms_lookup_name(name);
  4445. return *res ? 0 : -ENOENT;
  4446. }
  4447. static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
  4448. .func = bpf_kallsyms_lookup_name,
  4449. .gpl_only = false,
  4450. .ret_type = RET_INTEGER,
  4451. .arg1_type = ARG_PTR_TO_MEM,
  4452. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  4453. .arg3_type = ARG_ANYTHING,
  4454. .arg4_type = ARG_PTR_TO_LONG,
  4455. };
  4456. static const struct bpf_func_proto *
  4457. syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  4458. {
  4459. switch (func_id) {
  4460. case BPF_FUNC_sys_bpf:
  4461. return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
  4462. case BPF_FUNC_btf_find_by_name_kind:
  4463. return &bpf_btf_find_by_name_kind_proto;
  4464. case BPF_FUNC_sys_close:
  4465. return &bpf_sys_close_proto;
  4466. case BPF_FUNC_kallsyms_lookup_name:
  4467. return &bpf_kallsyms_lookup_name_proto;
  4468. default:
  4469. return tracing_prog_func_proto(func_id, prog);
  4470. }
  4471. }
  4472. const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
  4473. .get_func_proto = syscall_prog_func_proto,
  4474. .is_valid_access = syscall_prog_is_valid_access,
  4475. };
  4476. const struct bpf_prog_ops bpf_syscall_prog_ops = {
  4477. .test_run = bpf_prog_test_run_syscall,
  4478. };
  4479. #ifdef CONFIG_SYSCTL
  4480. static int bpf_stats_handler(struct ctl_table *table, int write,
  4481. void *buffer, size_t *lenp, loff_t *ppos)
  4482. {
  4483. struct static_key *key = (struct static_key *)table->data;
  4484. static int saved_val;
  4485. int val, ret;
  4486. struct ctl_table tmp = {
  4487. .data = &val,
  4488. .maxlen = sizeof(val),
  4489. .mode = table->mode,
  4490. .extra1 = SYSCTL_ZERO,
  4491. .extra2 = SYSCTL_ONE,
  4492. };
  4493. if (write && !capable(CAP_SYS_ADMIN))
  4494. return -EPERM;
  4495. mutex_lock(&bpf_stats_enabled_mutex);
  4496. val = saved_val;
  4497. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  4498. if (write && !ret && val != saved_val) {
  4499. if (val)
  4500. static_key_slow_inc(key);
  4501. else
  4502. static_key_slow_dec(key);
  4503. saved_val = val;
  4504. }
  4505. mutex_unlock(&bpf_stats_enabled_mutex);
  4506. return ret;
  4507. }
  4508. void __weak unpriv_ebpf_notify(int new_state)
  4509. {
  4510. }
  4511. static int bpf_unpriv_handler(struct ctl_table *table, int write,
  4512. void *buffer, size_t *lenp, loff_t *ppos)
  4513. {
  4514. int ret, unpriv_enable = *(int *)table->data;
  4515. bool locked_state = unpriv_enable == 1;
  4516. struct ctl_table tmp = *table;
  4517. if (write && !capable(CAP_SYS_ADMIN))
  4518. return -EPERM;
  4519. tmp.data = &unpriv_enable;
  4520. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  4521. if (write && !ret) {
  4522. if (locked_state && unpriv_enable != 1)
  4523. return -EPERM;
  4524. *(int *)table->data = unpriv_enable;
  4525. }
  4526. if (write)
  4527. unpriv_ebpf_notify(unpriv_enable);
  4528. return ret;
  4529. }
  4530. static struct ctl_table bpf_syscall_table[] = {
  4531. {
  4532. .procname = "unprivileged_bpf_disabled",
  4533. .data = &sysctl_unprivileged_bpf_disabled,
  4534. .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
  4535. .mode = 0644,
  4536. .proc_handler = bpf_unpriv_handler,
  4537. .extra1 = SYSCTL_ZERO,
  4538. .extra2 = SYSCTL_TWO,
  4539. },
  4540. {
  4541. .procname = "bpf_stats_enabled",
  4542. .data = &bpf_stats_enabled_key.key,
  4543. .maxlen = sizeof(bpf_stats_enabled_key),
  4544. .mode = 0644,
  4545. .proc_handler = bpf_stats_handler,
  4546. },
  4547. { }
  4548. };
  4549. static int __init bpf_syscall_sysctl_init(void)
  4550. {
  4551. register_sysctl_init("kernel", bpf_syscall_table);
  4552. return 0;
  4553. }
  4554. late_initcall(bpf_syscall_sysctl_init);
  4555. #endif /* CONFIG_SYSCTL */