security.c 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Security plug functions
  4. *
  5. * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
  6. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  7. * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
  8. * Copyright (C) 2016 Mellanox Technologies
  9. * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com>
  10. */
  11. #define pr_fmt(fmt) "LSM: " fmt
  12. #include <linux/bpf.h>
  13. #include <linux/capability.h>
  14. #include <linux/dcache.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kernel_read_file.h>
  19. #include <linux/lsm_hooks.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/mman.h>
  22. #include <linux/mount.h>
  23. #include <linux/personality.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/string.h>
  26. #include <linux/xattr.h>
  27. #include <linux/msg.h>
  28. #include <linux/overflow.h>
  29. #include <linux/perf_event.h>
  30. #include <linux/fs.h>
  31. #include <net/flow.h>
  32. #include <net/sock.h>
  33. #define SECURITY_HOOK_ACTIVE_KEY(HOOK, IDX) security_hook_active_##HOOK##_##IDX
  34. /*
  35. * Identifier for the LSM static calls.
  36. * HOOK is an LSM hook as defined in linux/lsm_hookdefs.h
  37. * IDX is the index of the static call. 0 <= NUM < MAX_LSM_COUNT
  38. */
  39. #define LSM_STATIC_CALL(HOOK, IDX) lsm_static_call_##HOOK##_##IDX
  40. /*
  41. * Call the macro M for each LSM hook MAX_LSM_COUNT times.
  42. */
  43. #define LSM_LOOP_UNROLL(M, ...) \
  44. do { \
  45. UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__) \
  46. } while (0)
  47. #define LSM_DEFINE_UNROLL(M, ...) UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__)
  48. /*
  49. * These are descriptions of the reasons that can be passed to the
  50. * security_locked_down() LSM hook. Placing this array here allows
  51. * all security modules to use the same descriptions for auditing
  52. * purposes.
  53. */
  54. const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
  55. [LOCKDOWN_NONE] = "none",
  56. [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
  57. [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
  58. [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
  59. [LOCKDOWN_KEXEC] = "kexec of unsigned images",
  60. [LOCKDOWN_HIBERNATION] = "hibernation",
  61. [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
  62. [LOCKDOWN_IOPORT] = "raw io port access",
  63. [LOCKDOWN_MSR] = "raw MSR access",
  64. [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
  65. [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
  66. [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
  67. [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
  68. [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
  69. [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
  70. [LOCKDOWN_DEBUGFS] = "debugfs access",
  71. [LOCKDOWN_XMON_WR] = "xmon write access",
  72. [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
  73. [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
  74. [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
  75. [LOCKDOWN_INTEGRITY_MAX] = "integrity",
  76. [LOCKDOWN_KCORE] = "/proc/kcore access",
  77. [LOCKDOWN_KPROBES] = "use of kprobes",
  78. [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
  79. [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
  80. [LOCKDOWN_PERF] = "unsafe use of perf",
  81. [LOCKDOWN_TRACEFS] = "use of tracefs",
  82. [LOCKDOWN_XMON_RW] = "xmon read and write access",
  83. [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
  84. [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
  85. };
  86. static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
  87. static struct kmem_cache *lsm_file_cache;
  88. static struct kmem_cache *lsm_inode_cache;
  89. char *lsm_names;
  90. static struct lsm_blob_sizes blob_sizes __ro_after_init;
  91. /* Boot-time LSM user choice */
  92. static __initdata const char *chosen_lsm_order;
  93. static __initdata const char *chosen_major_lsm;
  94. static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
  95. /* Ordered list of LSMs to initialize. */
  96. static __initdata struct lsm_info *ordered_lsms[MAX_LSM_COUNT + 1];
  97. static __initdata struct lsm_info *exclusive;
  98. #ifdef CONFIG_HAVE_STATIC_CALL
  99. #define LSM_HOOK_TRAMP(NAME, NUM) \
  100. &STATIC_CALL_TRAMP(LSM_STATIC_CALL(NAME, NUM))
  101. #else
  102. #define LSM_HOOK_TRAMP(NAME, NUM) NULL
  103. #endif
  104. /*
  105. * Define static calls and static keys for each LSM hook.
  106. */
  107. #define DEFINE_LSM_STATIC_CALL(NUM, NAME, RET, ...) \
  108. DEFINE_STATIC_CALL_NULL(LSM_STATIC_CALL(NAME, NUM), \
  109. *((RET(*)(__VA_ARGS__))NULL)); \
  110. DEFINE_STATIC_KEY_FALSE(SECURITY_HOOK_ACTIVE_KEY(NAME, NUM));
  111. #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
  112. LSM_DEFINE_UNROLL(DEFINE_LSM_STATIC_CALL, NAME, RET, __VA_ARGS__)
  113. #include <linux/lsm_hook_defs.h>
  114. #undef LSM_HOOK
  115. #undef DEFINE_LSM_STATIC_CALL
  116. /*
  117. * Initialise a table of static calls for each LSM hook.
  118. * DEFINE_STATIC_CALL_NULL invocation above generates a key (STATIC_CALL_KEY)
  119. * and a trampoline (STATIC_CALL_TRAMP) which are used to call
  120. * __static_call_update when updating the static call.
  121. *
  122. * The static calls table is used by early LSMs, some architectures can fault on
  123. * unaligned accesses and the fault handling code may not be ready by then.
  124. * Thus, the static calls table should be aligned to avoid any unhandled faults
  125. * in early init.
  126. */
  127. struct lsm_static_calls_table
  128. static_calls_table __ro_after_init __aligned(sizeof(u64)) = {
  129. #define INIT_LSM_STATIC_CALL(NUM, NAME) \
  130. (struct lsm_static_call) { \
  131. .key = &STATIC_CALL_KEY(LSM_STATIC_CALL(NAME, NUM)), \
  132. .trampoline = LSM_HOOK_TRAMP(NAME, NUM), \
  133. .active = &SECURITY_HOOK_ACTIVE_KEY(NAME, NUM), \
  134. },
  135. #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
  136. .NAME = { \
  137. LSM_DEFINE_UNROLL(INIT_LSM_STATIC_CALL, NAME) \
  138. },
  139. #include <linux/lsm_hook_defs.h>
  140. #undef LSM_HOOK
  141. #undef INIT_LSM_STATIC_CALL
  142. };
  143. static __initdata bool debug;
  144. #define init_debug(...) \
  145. do { \
  146. if (debug) \
  147. pr_info(__VA_ARGS__); \
  148. } while (0)
  149. static bool __init is_enabled(struct lsm_info *lsm)
  150. {
  151. if (!lsm->enabled)
  152. return false;
  153. return *lsm->enabled;
  154. }
  155. /* Mark an LSM's enabled flag. */
  156. static int lsm_enabled_true __initdata = 1;
  157. static int lsm_enabled_false __initdata = 0;
  158. static void __init set_enabled(struct lsm_info *lsm, bool enabled)
  159. {
  160. /*
  161. * When an LSM hasn't configured an enable variable, we can use
  162. * a hard-coded location for storing the default enabled state.
  163. */
  164. if (!lsm->enabled) {
  165. if (enabled)
  166. lsm->enabled = &lsm_enabled_true;
  167. else
  168. lsm->enabled = &lsm_enabled_false;
  169. } else if (lsm->enabled == &lsm_enabled_true) {
  170. if (!enabled)
  171. lsm->enabled = &lsm_enabled_false;
  172. } else if (lsm->enabled == &lsm_enabled_false) {
  173. if (enabled)
  174. lsm->enabled = &lsm_enabled_true;
  175. } else {
  176. *lsm->enabled = enabled;
  177. }
  178. }
  179. /* Is an LSM already listed in the ordered LSMs list? */
  180. static bool __init exists_ordered_lsm(struct lsm_info *lsm)
  181. {
  182. struct lsm_info **check;
  183. for (check = ordered_lsms; *check; check++)
  184. if (*check == lsm)
  185. return true;
  186. return false;
  187. }
  188. /* Append an LSM to the list of ordered LSMs to initialize. */
  189. static int last_lsm __initdata;
  190. static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
  191. {
  192. /* Ignore duplicate selections. */
  193. if (exists_ordered_lsm(lsm))
  194. return;
  195. if (WARN(last_lsm == MAX_LSM_COUNT, "%s: out of LSM static calls!?\n", from))
  196. return;
  197. /* Enable this LSM, if it is not already set. */
  198. if (!lsm->enabled)
  199. lsm->enabled = &lsm_enabled_true;
  200. ordered_lsms[last_lsm++] = lsm;
  201. init_debug("%s ordered: %s (%s)\n", from, lsm->name,
  202. is_enabled(lsm) ? "enabled" : "disabled");
  203. }
  204. /* Is an LSM allowed to be initialized? */
  205. static bool __init lsm_allowed(struct lsm_info *lsm)
  206. {
  207. /* Skip if the LSM is disabled. */
  208. if (!is_enabled(lsm))
  209. return false;
  210. /* Not allowed if another exclusive LSM already initialized. */
  211. if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
  212. init_debug("exclusive disabled: %s\n", lsm->name);
  213. return false;
  214. }
  215. return true;
  216. }
  217. static void __init lsm_set_blob_size(int *need, int *lbs)
  218. {
  219. int offset;
  220. if (*need <= 0)
  221. return;
  222. offset = ALIGN(*lbs, sizeof(void *));
  223. *lbs = offset + *need;
  224. *need = offset;
  225. }
  226. static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
  227. {
  228. if (!needed)
  229. return;
  230. lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
  231. lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
  232. lsm_set_blob_size(&needed->lbs_ib, &blob_sizes.lbs_ib);
  233. /*
  234. * The inode blob gets an rcu_head in addition to
  235. * what the modules might need.
  236. */
  237. if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
  238. blob_sizes.lbs_inode = sizeof(struct rcu_head);
  239. lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
  240. lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
  241. lsm_set_blob_size(&needed->lbs_key, &blob_sizes.lbs_key);
  242. lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
  243. lsm_set_blob_size(&needed->lbs_perf_event, &blob_sizes.lbs_perf_event);
  244. lsm_set_blob_size(&needed->lbs_sock, &blob_sizes.lbs_sock);
  245. lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
  246. lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
  247. lsm_set_blob_size(&needed->lbs_tun_dev, &blob_sizes.lbs_tun_dev);
  248. lsm_set_blob_size(&needed->lbs_xattr_count,
  249. &blob_sizes.lbs_xattr_count);
  250. lsm_set_blob_size(&needed->lbs_bdev, &blob_sizes.lbs_bdev);
  251. }
  252. /* Prepare LSM for initialization. */
  253. static void __init prepare_lsm(struct lsm_info *lsm)
  254. {
  255. int enabled = lsm_allowed(lsm);
  256. /* Record enablement (to handle any following exclusive LSMs). */
  257. set_enabled(lsm, enabled);
  258. /* If enabled, do pre-initialization work. */
  259. if (enabled) {
  260. if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
  261. exclusive = lsm;
  262. init_debug("exclusive chosen: %s\n", lsm->name);
  263. }
  264. lsm_set_blob_sizes(lsm->blobs);
  265. }
  266. }
  267. /* Initialize a given LSM, if it is enabled. */
  268. static void __init initialize_lsm(struct lsm_info *lsm)
  269. {
  270. if (is_enabled(lsm)) {
  271. int ret;
  272. init_debug("initializing %s\n", lsm->name);
  273. ret = lsm->init();
  274. WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
  275. }
  276. }
  277. /*
  278. * Current index to use while initializing the lsm id list.
  279. */
  280. u32 lsm_active_cnt __ro_after_init;
  281. const struct lsm_id *lsm_idlist[MAX_LSM_COUNT];
  282. /* Populate ordered LSMs list from comma-separated LSM name list. */
  283. static void __init ordered_lsm_parse(const char *order, const char *origin)
  284. {
  285. struct lsm_info *lsm;
  286. char *sep, *name, *next;
  287. /* LSM_ORDER_FIRST is always first. */
  288. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  289. if (lsm->order == LSM_ORDER_FIRST)
  290. append_ordered_lsm(lsm, " first");
  291. }
  292. /* Process "security=", if given. */
  293. if (chosen_major_lsm) {
  294. struct lsm_info *major;
  295. /*
  296. * To match the original "security=" behavior, this
  297. * explicitly does NOT fallback to another Legacy Major
  298. * if the selected one was separately disabled: disable
  299. * all non-matching Legacy Major LSMs.
  300. */
  301. for (major = __start_lsm_info; major < __end_lsm_info;
  302. major++) {
  303. if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
  304. strcmp(major->name, chosen_major_lsm) != 0) {
  305. set_enabled(major, false);
  306. init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
  307. chosen_major_lsm, major->name);
  308. }
  309. }
  310. }
  311. sep = kstrdup(order, GFP_KERNEL);
  312. next = sep;
  313. /* Walk the list, looking for matching LSMs. */
  314. while ((name = strsep(&next, ",")) != NULL) {
  315. bool found = false;
  316. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  317. if (strcmp(lsm->name, name) == 0) {
  318. if (lsm->order == LSM_ORDER_MUTABLE)
  319. append_ordered_lsm(lsm, origin);
  320. found = true;
  321. }
  322. }
  323. if (!found)
  324. init_debug("%s ignored: %s (not built into kernel)\n",
  325. origin, name);
  326. }
  327. /* Process "security=", if given. */
  328. if (chosen_major_lsm) {
  329. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  330. if (exists_ordered_lsm(lsm))
  331. continue;
  332. if (strcmp(lsm->name, chosen_major_lsm) == 0)
  333. append_ordered_lsm(lsm, "security=");
  334. }
  335. }
  336. /* LSM_ORDER_LAST is always last. */
  337. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  338. if (lsm->order == LSM_ORDER_LAST)
  339. append_ordered_lsm(lsm, " last");
  340. }
  341. /* Disable all LSMs not in the ordered list. */
  342. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  343. if (exists_ordered_lsm(lsm))
  344. continue;
  345. set_enabled(lsm, false);
  346. init_debug("%s skipped: %s (not in requested order)\n",
  347. origin, lsm->name);
  348. }
  349. kfree(sep);
  350. }
  351. static void __init lsm_static_call_init(struct security_hook_list *hl)
  352. {
  353. struct lsm_static_call *scall = hl->scalls;
  354. int i;
  355. for (i = 0; i < MAX_LSM_COUNT; i++) {
  356. /* Update the first static call that is not used yet */
  357. if (!scall->hl) {
  358. __static_call_update(scall->key, scall->trampoline,
  359. hl->hook.lsm_func_addr);
  360. scall->hl = hl;
  361. static_branch_enable(scall->active);
  362. return;
  363. }
  364. scall++;
  365. }
  366. panic("%s - Ran out of static slots.\n", __func__);
  367. }
  368. static void __init lsm_early_cred(struct cred *cred);
  369. static void __init lsm_early_task(struct task_struct *task);
  370. static int lsm_append(const char *new, char **result);
  371. static void __init report_lsm_order(void)
  372. {
  373. struct lsm_info **lsm, *early;
  374. int first = 0;
  375. pr_info("initializing lsm=");
  376. /* Report each enabled LSM name, comma separated. */
  377. for (early = __start_early_lsm_info;
  378. early < __end_early_lsm_info; early++)
  379. if (is_enabled(early))
  380. pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
  381. for (lsm = ordered_lsms; *lsm; lsm++)
  382. if (is_enabled(*lsm))
  383. pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
  384. pr_cont("\n");
  385. }
  386. static void __init ordered_lsm_init(void)
  387. {
  388. struct lsm_info **lsm;
  389. if (chosen_lsm_order) {
  390. if (chosen_major_lsm) {
  391. pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
  392. chosen_major_lsm, chosen_lsm_order);
  393. chosen_major_lsm = NULL;
  394. }
  395. ordered_lsm_parse(chosen_lsm_order, "cmdline");
  396. } else
  397. ordered_lsm_parse(builtin_lsm_order, "builtin");
  398. for (lsm = ordered_lsms; *lsm; lsm++)
  399. prepare_lsm(*lsm);
  400. report_lsm_order();
  401. init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
  402. init_debug("file blob size = %d\n", blob_sizes.lbs_file);
  403. init_debug("ib blob size = %d\n", blob_sizes.lbs_ib);
  404. init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
  405. init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
  406. #ifdef CONFIG_KEYS
  407. init_debug("key blob size = %d\n", blob_sizes.lbs_key);
  408. #endif /* CONFIG_KEYS */
  409. init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
  410. init_debug("sock blob size = %d\n", blob_sizes.lbs_sock);
  411. init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
  412. init_debug("perf event blob size = %d\n", blob_sizes.lbs_perf_event);
  413. init_debug("task blob size = %d\n", blob_sizes.lbs_task);
  414. init_debug("tun device blob size = %d\n", blob_sizes.lbs_tun_dev);
  415. init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
  416. init_debug("bdev blob size = %d\n", blob_sizes.lbs_bdev);
  417. /*
  418. * Create any kmem_caches needed for blobs
  419. */
  420. if (blob_sizes.lbs_file)
  421. lsm_file_cache = kmem_cache_create("lsm_file_cache",
  422. blob_sizes.lbs_file, 0,
  423. SLAB_PANIC, NULL);
  424. if (blob_sizes.lbs_inode)
  425. lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
  426. blob_sizes.lbs_inode, 0,
  427. SLAB_PANIC, NULL);
  428. lsm_early_cred((struct cred *) current->cred);
  429. lsm_early_task(current);
  430. for (lsm = ordered_lsms; *lsm; lsm++)
  431. initialize_lsm(*lsm);
  432. }
  433. int __init early_security_init(void)
  434. {
  435. struct lsm_info *lsm;
  436. for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
  437. if (!lsm->enabled)
  438. lsm->enabled = &lsm_enabled_true;
  439. prepare_lsm(lsm);
  440. initialize_lsm(lsm);
  441. }
  442. return 0;
  443. }
  444. /**
  445. * security_init - initializes the security framework
  446. *
  447. * This should be called early in the kernel initialization sequence.
  448. */
  449. int __init security_init(void)
  450. {
  451. struct lsm_info *lsm;
  452. init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
  453. init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
  454. init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
  455. /*
  456. * Append the names of the early LSM modules now that kmalloc() is
  457. * available
  458. */
  459. for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
  460. init_debug(" early started: %s (%s)\n", lsm->name,
  461. is_enabled(lsm) ? "enabled" : "disabled");
  462. if (lsm->enabled)
  463. lsm_append(lsm->name, &lsm_names);
  464. }
  465. /* Load LSMs in specified order. */
  466. ordered_lsm_init();
  467. return 0;
  468. }
  469. /* Save user chosen LSM */
  470. static int __init choose_major_lsm(char *str)
  471. {
  472. chosen_major_lsm = str;
  473. return 1;
  474. }
  475. __setup("security=", choose_major_lsm);
  476. /* Explicitly choose LSM initialization order. */
  477. static int __init choose_lsm_order(char *str)
  478. {
  479. chosen_lsm_order = str;
  480. return 1;
  481. }
  482. __setup("lsm=", choose_lsm_order);
  483. /* Enable LSM order debugging. */
  484. static int __init enable_debug(char *str)
  485. {
  486. debug = true;
  487. return 1;
  488. }
  489. __setup("lsm.debug", enable_debug);
  490. static bool match_last_lsm(const char *list, const char *lsm)
  491. {
  492. const char *last;
  493. if (WARN_ON(!list || !lsm))
  494. return false;
  495. last = strrchr(list, ',');
  496. if (last)
  497. /* Pass the comma, strcmp() will check for '\0' */
  498. last++;
  499. else
  500. last = list;
  501. return !strcmp(last, lsm);
  502. }
  503. static int lsm_append(const char *new, char **result)
  504. {
  505. char *cp;
  506. if (*result == NULL) {
  507. *result = kstrdup(new, GFP_KERNEL);
  508. if (*result == NULL)
  509. return -ENOMEM;
  510. } else {
  511. /* Check if it is the last registered name */
  512. if (match_last_lsm(*result, new))
  513. return 0;
  514. cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
  515. if (cp == NULL)
  516. return -ENOMEM;
  517. kfree(*result);
  518. *result = cp;
  519. }
  520. return 0;
  521. }
  522. /**
  523. * security_add_hooks - Add a modules hooks to the hook lists.
  524. * @hooks: the hooks to add
  525. * @count: the number of hooks to add
  526. * @lsmid: the identification information for the security module
  527. *
  528. * Each LSM has to register its hooks with the infrastructure.
  529. */
  530. void __init security_add_hooks(struct security_hook_list *hooks, int count,
  531. const struct lsm_id *lsmid)
  532. {
  533. int i;
  534. /*
  535. * A security module may call security_add_hooks() more
  536. * than once during initialization, and LSM initialization
  537. * is serialized. Landlock is one such case.
  538. * Look at the previous entry, if there is one, for duplication.
  539. */
  540. if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
  541. if (lsm_active_cnt >= MAX_LSM_COUNT)
  542. panic("%s Too many LSMs registered.\n", __func__);
  543. lsm_idlist[lsm_active_cnt++] = lsmid;
  544. }
  545. for (i = 0; i < count; i++) {
  546. hooks[i].lsmid = lsmid;
  547. lsm_static_call_init(&hooks[i]);
  548. }
  549. /*
  550. * Don't try to append during early_security_init(), we'll come back
  551. * and fix this up afterwards.
  552. */
  553. if (slab_is_available()) {
  554. if (lsm_append(lsmid->name, &lsm_names) < 0)
  555. panic("%s - Cannot get early memory.\n", __func__);
  556. }
  557. }
  558. int call_blocking_lsm_notifier(enum lsm_event event, void *data)
  559. {
  560. return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
  561. event, data);
  562. }
  563. EXPORT_SYMBOL(call_blocking_lsm_notifier);
  564. int register_blocking_lsm_notifier(struct notifier_block *nb)
  565. {
  566. return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
  567. nb);
  568. }
  569. EXPORT_SYMBOL(register_blocking_lsm_notifier);
  570. int unregister_blocking_lsm_notifier(struct notifier_block *nb)
  571. {
  572. return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
  573. nb);
  574. }
  575. EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
  576. /**
  577. * lsm_blob_alloc - allocate a composite blob
  578. * @dest: the destination for the blob
  579. * @size: the size of the blob
  580. * @gfp: allocation type
  581. *
  582. * Allocate a blob for all the modules
  583. *
  584. * Returns 0, or -ENOMEM if memory can't be allocated.
  585. */
  586. static int lsm_blob_alloc(void **dest, size_t size, gfp_t gfp)
  587. {
  588. if (size == 0) {
  589. *dest = NULL;
  590. return 0;
  591. }
  592. *dest = kzalloc(size, gfp);
  593. if (*dest == NULL)
  594. return -ENOMEM;
  595. return 0;
  596. }
  597. /**
  598. * lsm_cred_alloc - allocate a composite cred blob
  599. * @cred: the cred that needs a blob
  600. * @gfp: allocation type
  601. *
  602. * Allocate the cred blob for all the modules
  603. *
  604. * Returns 0, or -ENOMEM if memory can't be allocated.
  605. */
  606. static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
  607. {
  608. return lsm_blob_alloc(&cred->security, blob_sizes.lbs_cred, gfp);
  609. }
  610. /**
  611. * lsm_early_cred - during initialization allocate a composite cred blob
  612. * @cred: the cred that needs a blob
  613. *
  614. * Allocate the cred blob for all the modules
  615. */
  616. static void __init lsm_early_cred(struct cred *cred)
  617. {
  618. int rc = lsm_cred_alloc(cred, GFP_KERNEL);
  619. if (rc)
  620. panic("%s: Early cred alloc failed.\n", __func__);
  621. }
  622. /**
  623. * lsm_file_alloc - allocate a composite file blob
  624. * @file: the file that needs a blob
  625. *
  626. * Allocate the file blob for all the modules
  627. *
  628. * Returns 0, or -ENOMEM if memory can't be allocated.
  629. */
  630. static int lsm_file_alloc(struct file *file)
  631. {
  632. if (!lsm_file_cache) {
  633. file->f_security = NULL;
  634. return 0;
  635. }
  636. file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
  637. if (file->f_security == NULL)
  638. return -ENOMEM;
  639. return 0;
  640. }
  641. /**
  642. * lsm_inode_alloc - allocate a composite inode blob
  643. * @inode: the inode that needs a blob
  644. * @gfp: allocation flags
  645. *
  646. * Allocate the inode blob for all the modules
  647. *
  648. * Returns 0, or -ENOMEM if memory can't be allocated.
  649. */
  650. static int lsm_inode_alloc(struct inode *inode, gfp_t gfp)
  651. {
  652. if (!lsm_inode_cache) {
  653. inode->i_security = NULL;
  654. return 0;
  655. }
  656. inode->i_security = kmem_cache_zalloc(lsm_inode_cache, gfp);
  657. if (inode->i_security == NULL)
  658. return -ENOMEM;
  659. return 0;
  660. }
  661. /**
  662. * lsm_task_alloc - allocate a composite task blob
  663. * @task: the task that needs a blob
  664. *
  665. * Allocate the task blob for all the modules
  666. *
  667. * Returns 0, or -ENOMEM if memory can't be allocated.
  668. */
  669. static int lsm_task_alloc(struct task_struct *task)
  670. {
  671. return lsm_blob_alloc(&task->security, blob_sizes.lbs_task, GFP_KERNEL);
  672. }
  673. /**
  674. * lsm_ipc_alloc - allocate a composite ipc blob
  675. * @kip: the ipc that needs a blob
  676. *
  677. * Allocate the ipc blob for all the modules
  678. *
  679. * Returns 0, or -ENOMEM if memory can't be allocated.
  680. */
  681. static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
  682. {
  683. return lsm_blob_alloc(&kip->security, blob_sizes.lbs_ipc, GFP_KERNEL);
  684. }
  685. #ifdef CONFIG_KEYS
  686. /**
  687. * lsm_key_alloc - allocate a composite key blob
  688. * @key: the key that needs a blob
  689. *
  690. * Allocate the key blob for all the modules
  691. *
  692. * Returns 0, or -ENOMEM if memory can't be allocated.
  693. */
  694. static int lsm_key_alloc(struct key *key)
  695. {
  696. return lsm_blob_alloc(&key->security, blob_sizes.lbs_key, GFP_KERNEL);
  697. }
  698. #endif /* CONFIG_KEYS */
  699. /**
  700. * lsm_msg_msg_alloc - allocate a composite msg_msg blob
  701. * @mp: the msg_msg that needs a blob
  702. *
  703. * Allocate the ipc blob for all the modules
  704. *
  705. * Returns 0, or -ENOMEM if memory can't be allocated.
  706. */
  707. static int lsm_msg_msg_alloc(struct msg_msg *mp)
  708. {
  709. return lsm_blob_alloc(&mp->security, blob_sizes.lbs_msg_msg,
  710. GFP_KERNEL);
  711. }
  712. /**
  713. * lsm_bdev_alloc - allocate a composite block_device blob
  714. * @bdev: the block_device that needs a blob
  715. *
  716. * Allocate the block_device blob for all the modules
  717. *
  718. * Returns 0, or -ENOMEM if memory can't be allocated.
  719. */
  720. static int lsm_bdev_alloc(struct block_device *bdev)
  721. {
  722. if (blob_sizes.lbs_bdev == 0) {
  723. bdev->bd_security = NULL;
  724. return 0;
  725. }
  726. bdev->bd_security = kzalloc(blob_sizes.lbs_bdev, GFP_KERNEL);
  727. if (!bdev->bd_security)
  728. return -ENOMEM;
  729. return 0;
  730. }
  731. /**
  732. * lsm_early_task - during initialization allocate a composite task blob
  733. * @task: the task that needs a blob
  734. *
  735. * Allocate the task blob for all the modules
  736. */
  737. static void __init lsm_early_task(struct task_struct *task)
  738. {
  739. int rc = lsm_task_alloc(task);
  740. if (rc)
  741. panic("%s: Early task alloc failed.\n", __func__);
  742. }
  743. /**
  744. * lsm_superblock_alloc - allocate a composite superblock blob
  745. * @sb: the superblock that needs a blob
  746. *
  747. * Allocate the superblock blob for all the modules
  748. *
  749. * Returns 0, or -ENOMEM if memory can't be allocated.
  750. */
  751. static int lsm_superblock_alloc(struct super_block *sb)
  752. {
  753. return lsm_blob_alloc(&sb->s_security, blob_sizes.lbs_superblock,
  754. GFP_KERNEL);
  755. }
  756. /**
  757. * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
  758. * @uctx: a userspace LSM context to be filled
  759. * @uctx_len: available uctx size (input), used uctx size (output)
  760. * @val: the new LSM context value
  761. * @val_len: the size of the new LSM context value
  762. * @id: LSM id
  763. * @flags: LSM defined flags
  764. *
  765. * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL
  766. * simply calculate the required size to output via @utc_len and return
  767. * success.
  768. *
  769. * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
  770. * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
  771. */
  772. int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
  773. void *val, size_t val_len,
  774. u64 id, u64 flags)
  775. {
  776. struct lsm_ctx *nctx = NULL;
  777. size_t nctx_len;
  778. int rc = 0;
  779. nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
  780. if (nctx_len > *uctx_len) {
  781. rc = -E2BIG;
  782. goto out;
  783. }
  784. /* no buffer - return success/0 and set @uctx_len to the req size */
  785. if (!uctx)
  786. goto out;
  787. nctx = kzalloc(nctx_len, GFP_KERNEL);
  788. if (nctx == NULL) {
  789. rc = -ENOMEM;
  790. goto out;
  791. }
  792. nctx->id = id;
  793. nctx->flags = flags;
  794. nctx->len = nctx_len;
  795. nctx->ctx_len = val_len;
  796. memcpy(nctx->ctx, val, val_len);
  797. if (copy_to_user(uctx, nctx, nctx_len))
  798. rc = -EFAULT;
  799. out:
  800. kfree(nctx);
  801. *uctx_len = nctx_len;
  802. return rc;
  803. }
  804. /*
  805. * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
  806. * can be accessed with:
  807. *
  808. * LSM_RET_DEFAULT(<hook_name>)
  809. *
  810. * The macros below define static constants for the default value of each
  811. * LSM hook.
  812. */
  813. #define LSM_RET_DEFAULT(NAME) (NAME##_default)
  814. #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
  815. #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
  816. static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
  817. #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
  818. DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
  819. #include <linux/lsm_hook_defs.h>
  820. #undef LSM_HOOK
  821. /*
  822. * Hook list operation macros.
  823. *
  824. * call_void_hook:
  825. * This is a hook that does not return a value.
  826. *
  827. * call_int_hook:
  828. * This is a hook that returns a value.
  829. */
  830. #define __CALL_STATIC_VOID(NUM, HOOK, ...) \
  831. do { \
  832. if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \
  833. static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \
  834. } \
  835. } while (0);
  836. #define call_void_hook(HOOK, ...) \
  837. do { \
  838. LSM_LOOP_UNROLL(__CALL_STATIC_VOID, HOOK, __VA_ARGS__); \
  839. } while (0)
  840. #define __CALL_STATIC_INT(NUM, R, HOOK, LABEL, ...) \
  841. do { \
  842. if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \
  843. R = static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \
  844. if (R != LSM_RET_DEFAULT(HOOK)) \
  845. goto LABEL; \
  846. } \
  847. } while (0);
  848. #define call_int_hook(HOOK, ...) \
  849. ({ \
  850. __label__ OUT; \
  851. int RC = LSM_RET_DEFAULT(HOOK); \
  852. \
  853. LSM_LOOP_UNROLL(__CALL_STATIC_INT, RC, HOOK, OUT, __VA_ARGS__); \
  854. OUT: \
  855. RC; \
  856. })
  857. #define lsm_for_each_hook(scall, NAME) \
  858. for (scall = static_calls_table.NAME; \
  859. scall - static_calls_table.NAME < MAX_LSM_COUNT; scall++) \
  860. if (static_key_enabled(&scall->active->key))
  861. /* Security operations */
  862. /**
  863. * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
  864. * @mgr: task credentials of current binder process
  865. *
  866. * Check whether @mgr is allowed to be the binder context manager.
  867. *
  868. * Return: Return 0 if permission is granted.
  869. */
  870. int security_binder_set_context_mgr(const struct cred *mgr)
  871. {
  872. return call_int_hook(binder_set_context_mgr, mgr);
  873. }
  874. /**
  875. * security_binder_transaction() - Check if a binder transaction is allowed
  876. * @from: sending process
  877. * @to: receiving process
  878. *
  879. * Check whether @from is allowed to invoke a binder transaction call to @to.
  880. *
  881. * Return: Returns 0 if permission is granted.
  882. */
  883. int security_binder_transaction(const struct cred *from,
  884. const struct cred *to)
  885. {
  886. return call_int_hook(binder_transaction, from, to);
  887. }
  888. /**
  889. * security_binder_transfer_binder() - Check if a binder transfer is allowed
  890. * @from: sending process
  891. * @to: receiving process
  892. *
  893. * Check whether @from is allowed to transfer a binder reference to @to.
  894. *
  895. * Return: Returns 0 if permission is granted.
  896. */
  897. int security_binder_transfer_binder(const struct cred *from,
  898. const struct cred *to)
  899. {
  900. return call_int_hook(binder_transfer_binder, from, to);
  901. }
  902. /**
  903. * security_binder_transfer_file() - Check if a binder file xfer is allowed
  904. * @from: sending process
  905. * @to: receiving process
  906. * @file: file being transferred
  907. *
  908. * Check whether @from is allowed to transfer @file to @to.
  909. *
  910. * Return: Returns 0 if permission is granted.
  911. */
  912. int security_binder_transfer_file(const struct cred *from,
  913. const struct cred *to, const struct file *file)
  914. {
  915. return call_int_hook(binder_transfer_file, from, to, file);
  916. }
  917. /**
  918. * security_ptrace_access_check() - Check if tracing is allowed
  919. * @child: target process
  920. * @mode: PTRACE_MODE flags
  921. *
  922. * Check permission before allowing the current process to trace the @child
  923. * process. Security modules may also want to perform a process tracing check
  924. * during an execve in the set_security or apply_creds hooks of tracing check
  925. * during an execve in the bprm_set_creds hook of binprm_security_ops if the
  926. * process is being traced and its security attributes would be changed by the
  927. * execve.
  928. *
  929. * Return: Returns 0 if permission is granted.
  930. */
  931. int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
  932. {
  933. return call_int_hook(ptrace_access_check, child, mode);
  934. }
  935. /**
  936. * security_ptrace_traceme() - Check if tracing is allowed
  937. * @parent: tracing process
  938. *
  939. * Check that the @parent process has sufficient permission to trace the
  940. * current process before allowing the current process to present itself to the
  941. * @parent process for tracing.
  942. *
  943. * Return: Returns 0 if permission is granted.
  944. */
  945. int security_ptrace_traceme(struct task_struct *parent)
  946. {
  947. return call_int_hook(ptrace_traceme, parent);
  948. }
  949. /**
  950. * security_capget() - Get the capability sets for a process
  951. * @target: target process
  952. * @effective: effective capability set
  953. * @inheritable: inheritable capability set
  954. * @permitted: permitted capability set
  955. *
  956. * Get the @effective, @inheritable, and @permitted capability sets for the
  957. * @target process. The hook may also perform permission checking to determine
  958. * if the current process is allowed to see the capability sets of the @target
  959. * process.
  960. *
  961. * Return: Returns 0 if the capability sets were successfully obtained.
  962. */
  963. int security_capget(const struct task_struct *target,
  964. kernel_cap_t *effective,
  965. kernel_cap_t *inheritable,
  966. kernel_cap_t *permitted)
  967. {
  968. return call_int_hook(capget, target, effective, inheritable, permitted);
  969. }
  970. /**
  971. * security_capset() - Set the capability sets for a process
  972. * @new: new credentials for the target process
  973. * @old: current credentials of the target process
  974. * @effective: effective capability set
  975. * @inheritable: inheritable capability set
  976. * @permitted: permitted capability set
  977. *
  978. * Set the @effective, @inheritable, and @permitted capability sets for the
  979. * current process.
  980. *
  981. * Return: Returns 0 and update @new if permission is granted.
  982. */
  983. int security_capset(struct cred *new, const struct cred *old,
  984. const kernel_cap_t *effective,
  985. const kernel_cap_t *inheritable,
  986. const kernel_cap_t *permitted)
  987. {
  988. return call_int_hook(capset, new, old, effective, inheritable,
  989. permitted);
  990. }
  991. /**
  992. * security_capable() - Check if a process has the necessary capability
  993. * @cred: credentials to examine
  994. * @ns: user namespace
  995. * @cap: capability requested
  996. * @opts: capability check options
  997. *
  998. * Check whether the @tsk process has the @cap capability in the indicated
  999. * credentials. @cap contains the capability <include/linux/capability.h>.
  1000. * @opts contains options for the capable check <include/linux/security.h>.
  1001. *
  1002. * Return: Returns 0 if the capability is granted.
  1003. */
  1004. int security_capable(const struct cred *cred,
  1005. struct user_namespace *ns,
  1006. int cap,
  1007. unsigned int opts)
  1008. {
  1009. return call_int_hook(capable, cred, ns, cap, opts);
  1010. }
  1011. /**
  1012. * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
  1013. * @cmds: commands
  1014. * @type: type
  1015. * @id: id
  1016. * @sb: filesystem
  1017. *
  1018. * Check whether the quotactl syscall is allowed for this @sb.
  1019. *
  1020. * Return: Returns 0 if permission is granted.
  1021. */
  1022. int security_quotactl(int cmds, int type, int id, const struct super_block *sb)
  1023. {
  1024. return call_int_hook(quotactl, cmds, type, id, sb);
  1025. }
  1026. /**
  1027. * security_quota_on() - Check if QUOTAON is allowed for a dentry
  1028. * @dentry: dentry
  1029. *
  1030. * Check whether QUOTAON is allowed for @dentry.
  1031. *
  1032. * Return: Returns 0 if permission is granted.
  1033. */
  1034. int security_quota_on(struct dentry *dentry)
  1035. {
  1036. return call_int_hook(quota_on, dentry);
  1037. }
  1038. /**
  1039. * security_syslog() - Check if accessing the kernel message ring is allowed
  1040. * @type: SYSLOG_ACTION_* type
  1041. *
  1042. * Check permission before accessing the kernel message ring or changing
  1043. * logging to the console. See the syslog(2) manual page for an explanation of
  1044. * the @type values.
  1045. *
  1046. * Return: Return 0 if permission is granted.
  1047. */
  1048. int security_syslog(int type)
  1049. {
  1050. return call_int_hook(syslog, type);
  1051. }
  1052. /**
  1053. * security_settime64() - Check if changing the system time is allowed
  1054. * @ts: new time
  1055. * @tz: timezone
  1056. *
  1057. * Check permission to change the system time, struct timespec64 is defined in
  1058. * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
  1059. *
  1060. * Return: Returns 0 if permission is granted.
  1061. */
  1062. int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
  1063. {
  1064. return call_int_hook(settime, ts, tz);
  1065. }
  1066. /**
  1067. * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
  1068. * @mm: mm struct
  1069. * @pages: number of pages
  1070. *
  1071. * Check permissions for allocating a new virtual mapping. If all LSMs return
  1072. * a positive value, __vm_enough_memory() will be called with cap_sys_admin
  1073. * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
  1074. * called with cap_sys_admin cleared.
  1075. *
  1076. * Return: Returns 0 if permission is granted by the LSM infrastructure to the
  1077. * caller.
  1078. */
  1079. int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
  1080. {
  1081. struct lsm_static_call *scall;
  1082. int cap_sys_admin = 1;
  1083. int rc;
  1084. /*
  1085. * The module will respond with 0 if it thinks the __vm_enough_memory()
  1086. * call should be made with the cap_sys_admin set. If all of the modules
  1087. * agree that it should be set it will. If any module thinks it should
  1088. * not be set it won't.
  1089. */
  1090. lsm_for_each_hook(scall, vm_enough_memory) {
  1091. rc = scall->hl->hook.vm_enough_memory(mm, pages);
  1092. if (rc < 0) {
  1093. cap_sys_admin = 0;
  1094. break;
  1095. }
  1096. }
  1097. return __vm_enough_memory(mm, pages, cap_sys_admin);
  1098. }
  1099. /**
  1100. * security_bprm_creds_for_exec() - Prepare the credentials for exec()
  1101. * @bprm: binary program information
  1102. *
  1103. * If the setup in prepare_exec_creds did not setup @bprm->cred->security
  1104. * properly for executing @bprm->file, update the LSM's portion of
  1105. * @bprm->cred->security to be what commit_creds needs to install for the new
  1106. * program. This hook may also optionally check permissions (e.g. for
  1107. * transitions between security domains). The hook must set @bprm->secureexec
  1108. * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
  1109. * contains the linux_binprm structure.
  1110. *
  1111. * Return: Returns 0 if the hook is successful and permission is granted.
  1112. */
  1113. int security_bprm_creds_for_exec(struct linux_binprm *bprm)
  1114. {
  1115. return call_int_hook(bprm_creds_for_exec, bprm);
  1116. }
  1117. /**
  1118. * security_bprm_creds_from_file() - Update linux_binprm creds based on file
  1119. * @bprm: binary program information
  1120. * @file: associated file
  1121. *
  1122. * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
  1123. * exec, update @bprm->cred to reflect that change. This is called after
  1124. * finding the binary that will be executed without an interpreter. This
  1125. * ensures that the credentials will not be derived from a script that the
  1126. * binary will need to reopen, which when reopend may end up being a completely
  1127. * different file. This hook may also optionally check permissions (e.g. for
  1128. * transitions between security domains). The hook must set @bprm->secureexec
  1129. * to 1 if AT_SECURE should be set to request libc enable secure mode. The
  1130. * hook must add to @bprm->per_clear any personality flags that should be
  1131. * cleared from current->personality. @bprm contains the linux_binprm
  1132. * structure.
  1133. *
  1134. * Return: Returns 0 if the hook is successful and permission is granted.
  1135. */
  1136. int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file)
  1137. {
  1138. return call_int_hook(bprm_creds_from_file, bprm, file);
  1139. }
  1140. /**
  1141. * security_bprm_check() - Mediate binary handler search
  1142. * @bprm: binary program information
  1143. *
  1144. * This hook mediates the point when a search for a binary handler will begin.
  1145. * It allows a check against the @bprm->cred->security value which was set in
  1146. * the preceding creds_for_exec call. The argv list and envp list are reliably
  1147. * available in @bprm. This hook may be called multiple times during a single
  1148. * execve. @bprm contains the linux_binprm structure.
  1149. *
  1150. * Return: Returns 0 if the hook is successful and permission is granted.
  1151. */
  1152. int security_bprm_check(struct linux_binprm *bprm)
  1153. {
  1154. return call_int_hook(bprm_check_security, bprm);
  1155. }
  1156. /**
  1157. * security_bprm_committing_creds() - Install creds for a process during exec()
  1158. * @bprm: binary program information
  1159. *
  1160. * Prepare to install the new security attributes of a process being
  1161. * transformed by an execve operation, based on the old credentials pointed to
  1162. * by @current->cred and the information set in @bprm->cred by the
  1163. * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
  1164. * hook is a good place to perform state changes on the process such as closing
  1165. * open file descriptors to which access will no longer be granted when the
  1166. * attributes are changed. This is called immediately before commit_creds().
  1167. */
  1168. void security_bprm_committing_creds(const struct linux_binprm *bprm)
  1169. {
  1170. call_void_hook(bprm_committing_creds, bprm);
  1171. }
  1172. /**
  1173. * security_bprm_committed_creds() - Tidy up after cred install during exec()
  1174. * @bprm: binary program information
  1175. *
  1176. * Tidy up after the installation of the new security attributes of a process
  1177. * being transformed by an execve operation. The new credentials have, by this
  1178. * point, been set to @current->cred. @bprm points to the linux_binprm
  1179. * structure. This hook is a good place to perform state changes on the
  1180. * process such as clearing out non-inheritable signal state. This is called
  1181. * immediately after commit_creds().
  1182. */
  1183. void security_bprm_committed_creds(const struct linux_binprm *bprm)
  1184. {
  1185. call_void_hook(bprm_committed_creds, bprm);
  1186. }
  1187. /**
  1188. * security_fs_context_submount() - Initialise fc->security
  1189. * @fc: new filesystem context
  1190. * @reference: dentry reference for submount/remount
  1191. *
  1192. * Fill out the ->security field for a new fs_context.
  1193. *
  1194. * Return: Returns 0 on success or negative error code on failure.
  1195. */
  1196. int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
  1197. {
  1198. return call_int_hook(fs_context_submount, fc, reference);
  1199. }
  1200. /**
  1201. * security_fs_context_dup() - Duplicate a fs_context LSM blob
  1202. * @fc: destination filesystem context
  1203. * @src_fc: source filesystem context
  1204. *
  1205. * Allocate and attach a security structure to sc->security. This pointer is
  1206. * initialised to NULL by the caller. @fc indicates the new filesystem context.
  1207. * @src_fc indicates the original filesystem context.
  1208. *
  1209. * Return: Returns 0 on success or a negative error code on failure.
  1210. */
  1211. int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
  1212. {
  1213. return call_int_hook(fs_context_dup, fc, src_fc);
  1214. }
  1215. /**
  1216. * security_fs_context_parse_param() - Configure a filesystem context
  1217. * @fc: filesystem context
  1218. * @param: filesystem parameter
  1219. *
  1220. * Userspace provided a parameter to configure a superblock. The LSM can
  1221. * consume the parameter or return it to the caller for use elsewhere.
  1222. *
  1223. * Return: If the parameter is used by the LSM it should return 0, if it is
  1224. * returned to the caller -ENOPARAM is returned, otherwise a negative
  1225. * error code is returned.
  1226. */
  1227. int security_fs_context_parse_param(struct fs_context *fc,
  1228. struct fs_parameter *param)
  1229. {
  1230. struct lsm_static_call *scall;
  1231. int trc;
  1232. int rc = -ENOPARAM;
  1233. lsm_for_each_hook(scall, fs_context_parse_param) {
  1234. trc = scall->hl->hook.fs_context_parse_param(fc, param);
  1235. if (trc == 0)
  1236. rc = 0;
  1237. else if (trc != -ENOPARAM)
  1238. return trc;
  1239. }
  1240. return rc;
  1241. }
  1242. /**
  1243. * security_sb_alloc() - Allocate a super_block LSM blob
  1244. * @sb: filesystem superblock
  1245. *
  1246. * Allocate and attach a security structure to the sb->s_security field. The
  1247. * s_security field is initialized to NULL when the structure is allocated.
  1248. * @sb contains the super_block structure to be modified.
  1249. *
  1250. * Return: Returns 0 if operation was successful.
  1251. */
  1252. int security_sb_alloc(struct super_block *sb)
  1253. {
  1254. int rc = lsm_superblock_alloc(sb);
  1255. if (unlikely(rc))
  1256. return rc;
  1257. rc = call_int_hook(sb_alloc_security, sb);
  1258. if (unlikely(rc))
  1259. security_sb_free(sb);
  1260. return rc;
  1261. }
  1262. /**
  1263. * security_sb_delete() - Release super_block LSM associated objects
  1264. * @sb: filesystem superblock
  1265. *
  1266. * Release objects tied to a superblock (e.g. inodes). @sb contains the
  1267. * super_block structure being released.
  1268. */
  1269. void security_sb_delete(struct super_block *sb)
  1270. {
  1271. call_void_hook(sb_delete, sb);
  1272. }
  1273. /**
  1274. * security_sb_free() - Free a super_block LSM blob
  1275. * @sb: filesystem superblock
  1276. *
  1277. * Deallocate and clear the sb->s_security field. @sb contains the super_block
  1278. * structure to be modified.
  1279. */
  1280. void security_sb_free(struct super_block *sb)
  1281. {
  1282. call_void_hook(sb_free_security, sb);
  1283. kfree(sb->s_security);
  1284. sb->s_security = NULL;
  1285. }
  1286. /**
  1287. * security_free_mnt_opts() - Free memory associated with mount options
  1288. * @mnt_opts: LSM processed mount options
  1289. *
  1290. * Free memory associated with @mnt_ops.
  1291. */
  1292. void security_free_mnt_opts(void **mnt_opts)
  1293. {
  1294. if (!*mnt_opts)
  1295. return;
  1296. call_void_hook(sb_free_mnt_opts, *mnt_opts);
  1297. *mnt_opts = NULL;
  1298. }
  1299. EXPORT_SYMBOL(security_free_mnt_opts);
  1300. /**
  1301. * security_sb_eat_lsm_opts() - Consume LSM mount options
  1302. * @options: mount options
  1303. * @mnt_opts: LSM processed mount options
  1304. *
  1305. * Eat (scan @options) and save them in @mnt_opts.
  1306. *
  1307. * Return: Returns 0 on success, negative values on failure.
  1308. */
  1309. int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
  1310. {
  1311. return call_int_hook(sb_eat_lsm_opts, options, mnt_opts);
  1312. }
  1313. EXPORT_SYMBOL(security_sb_eat_lsm_opts);
  1314. /**
  1315. * security_sb_mnt_opts_compat() - Check if new mount options are allowed
  1316. * @sb: filesystem superblock
  1317. * @mnt_opts: new mount options
  1318. *
  1319. * Determine if the new mount options in @mnt_opts are allowed given the
  1320. * existing mounted filesystem at @sb. @sb superblock being compared.
  1321. *
  1322. * Return: Returns 0 if options are compatible.
  1323. */
  1324. int security_sb_mnt_opts_compat(struct super_block *sb,
  1325. void *mnt_opts)
  1326. {
  1327. return call_int_hook(sb_mnt_opts_compat, sb, mnt_opts);
  1328. }
  1329. EXPORT_SYMBOL(security_sb_mnt_opts_compat);
  1330. /**
  1331. * security_sb_remount() - Verify no incompatible mount changes during remount
  1332. * @sb: filesystem superblock
  1333. * @mnt_opts: (re)mount options
  1334. *
  1335. * Extracts security system specific mount options and verifies no changes are
  1336. * being made to those options.
  1337. *
  1338. * Return: Returns 0 if permission is granted.
  1339. */
  1340. int security_sb_remount(struct super_block *sb,
  1341. void *mnt_opts)
  1342. {
  1343. return call_int_hook(sb_remount, sb, mnt_opts);
  1344. }
  1345. EXPORT_SYMBOL(security_sb_remount);
  1346. /**
  1347. * security_sb_kern_mount() - Check if a kernel mount is allowed
  1348. * @sb: filesystem superblock
  1349. *
  1350. * Mount this @sb if allowed by permissions.
  1351. *
  1352. * Return: Returns 0 if permission is granted.
  1353. */
  1354. int security_sb_kern_mount(const struct super_block *sb)
  1355. {
  1356. return call_int_hook(sb_kern_mount, sb);
  1357. }
  1358. /**
  1359. * security_sb_show_options() - Output the mount options for a superblock
  1360. * @m: output file
  1361. * @sb: filesystem superblock
  1362. *
  1363. * Show (print on @m) mount options for this @sb.
  1364. *
  1365. * Return: Returns 0 on success, negative values on failure.
  1366. */
  1367. int security_sb_show_options(struct seq_file *m, struct super_block *sb)
  1368. {
  1369. return call_int_hook(sb_show_options, m, sb);
  1370. }
  1371. /**
  1372. * security_sb_statfs() - Check if accessing fs stats is allowed
  1373. * @dentry: superblock handle
  1374. *
  1375. * Check permission before obtaining filesystem statistics for the @mnt
  1376. * mountpoint. @dentry is a handle on the superblock for the filesystem.
  1377. *
  1378. * Return: Returns 0 if permission is granted.
  1379. */
  1380. int security_sb_statfs(struct dentry *dentry)
  1381. {
  1382. return call_int_hook(sb_statfs, dentry);
  1383. }
  1384. /**
  1385. * security_sb_mount() - Check permission for mounting a filesystem
  1386. * @dev_name: filesystem backing device
  1387. * @path: mount point
  1388. * @type: filesystem type
  1389. * @flags: mount flags
  1390. * @data: filesystem specific data
  1391. *
  1392. * Check permission before an object specified by @dev_name is mounted on the
  1393. * mount point named by @nd. For an ordinary mount, @dev_name identifies a
  1394. * device if the file system type requires a device. For a remount
  1395. * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
  1396. * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
  1397. * mounted.
  1398. *
  1399. * Return: Returns 0 if permission is granted.
  1400. */
  1401. int security_sb_mount(const char *dev_name, const struct path *path,
  1402. const char *type, unsigned long flags, void *data)
  1403. {
  1404. return call_int_hook(sb_mount, dev_name, path, type, flags, data);
  1405. }
  1406. /**
  1407. * security_sb_umount() - Check permission for unmounting a filesystem
  1408. * @mnt: mounted filesystem
  1409. * @flags: unmount flags
  1410. *
  1411. * Check permission before the @mnt file system is unmounted.
  1412. *
  1413. * Return: Returns 0 if permission is granted.
  1414. */
  1415. int security_sb_umount(struct vfsmount *mnt, int flags)
  1416. {
  1417. return call_int_hook(sb_umount, mnt, flags);
  1418. }
  1419. /**
  1420. * security_sb_pivotroot() - Check permissions for pivoting the rootfs
  1421. * @old_path: new location for current rootfs
  1422. * @new_path: location of the new rootfs
  1423. *
  1424. * Check permission before pivoting the root filesystem.
  1425. *
  1426. * Return: Returns 0 if permission is granted.
  1427. */
  1428. int security_sb_pivotroot(const struct path *old_path,
  1429. const struct path *new_path)
  1430. {
  1431. return call_int_hook(sb_pivotroot, old_path, new_path);
  1432. }
  1433. /**
  1434. * security_sb_set_mnt_opts() - Set the mount options for a filesystem
  1435. * @sb: filesystem superblock
  1436. * @mnt_opts: binary mount options
  1437. * @kern_flags: kernel flags (in)
  1438. * @set_kern_flags: kernel flags (out)
  1439. *
  1440. * Set the security relevant mount options used for a superblock.
  1441. *
  1442. * Return: Returns 0 on success, error on failure.
  1443. */
  1444. int security_sb_set_mnt_opts(struct super_block *sb,
  1445. void *mnt_opts,
  1446. unsigned long kern_flags,
  1447. unsigned long *set_kern_flags)
  1448. {
  1449. struct lsm_static_call *scall;
  1450. int rc = mnt_opts ? -EOPNOTSUPP : LSM_RET_DEFAULT(sb_set_mnt_opts);
  1451. lsm_for_each_hook(scall, sb_set_mnt_opts) {
  1452. rc = scall->hl->hook.sb_set_mnt_opts(sb, mnt_opts, kern_flags,
  1453. set_kern_flags);
  1454. if (rc != LSM_RET_DEFAULT(sb_set_mnt_opts))
  1455. break;
  1456. }
  1457. return rc;
  1458. }
  1459. EXPORT_SYMBOL(security_sb_set_mnt_opts);
  1460. /**
  1461. * security_sb_clone_mnt_opts() - Duplicate superblock mount options
  1462. * @oldsb: source superblock
  1463. * @newsb: destination superblock
  1464. * @kern_flags: kernel flags (in)
  1465. * @set_kern_flags: kernel flags (out)
  1466. *
  1467. * Copy all security options from a given superblock to another.
  1468. *
  1469. * Return: Returns 0 on success, error on failure.
  1470. */
  1471. int security_sb_clone_mnt_opts(const struct super_block *oldsb,
  1472. struct super_block *newsb,
  1473. unsigned long kern_flags,
  1474. unsigned long *set_kern_flags)
  1475. {
  1476. return call_int_hook(sb_clone_mnt_opts, oldsb, newsb,
  1477. kern_flags, set_kern_flags);
  1478. }
  1479. EXPORT_SYMBOL(security_sb_clone_mnt_opts);
  1480. /**
  1481. * security_move_mount() - Check permissions for moving a mount
  1482. * @from_path: source mount point
  1483. * @to_path: destination mount point
  1484. *
  1485. * Check permission before a mount is moved.
  1486. *
  1487. * Return: Returns 0 if permission is granted.
  1488. */
  1489. int security_move_mount(const struct path *from_path,
  1490. const struct path *to_path)
  1491. {
  1492. return call_int_hook(move_mount, from_path, to_path);
  1493. }
  1494. /**
  1495. * security_path_notify() - Check if setting a watch is allowed
  1496. * @path: file path
  1497. * @mask: event mask
  1498. * @obj_type: file path type
  1499. *
  1500. * Check permissions before setting a watch on events as defined by @mask, on
  1501. * an object at @path, whose type is defined by @obj_type.
  1502. *
  1503. * Return: Returns 0 if permission is granted.
  1504. */
  1505. int security_path_notify(const struct path *path, u64 mask,
  1506. unsigned int obj_type)
  1507. {
  1508. return call_int_hook(path_notify, path, mask, obj_type);
  1509. }
  1510. /**
  1511. * security_inode_alloc() - Allocate an inode LSM blob
  1512. * @inode: the inode
  1513. * @gfp: allocation flags
  1514. *
  1515. * Allocate and attach a security structure to @inode->i_security. The
  1516. * i_security field is initialized to NULL when the inode structure is
  1517. * allocated.
  1518. *
  1519. * Return: Return 0 if operation was successful.
  1520. */
  1521. int security_inode_alloc(struct inode *inode, gfp_t gfp)
  1522. {
  1523. int rc = lsm_inode_alloc(inode, gfp);
  1524. if (unlikely(rc))
  1525. return rc;
  1526. rc = call_int_hook(inode_alloc_security, inode);
  1527. if (unlikely(rc))
  1528. security_inode_free(inode);
  1529. return rc;
  1530. }
  1531. static void inode_free_by_rcu(struct rcu_head *head)
  1532. {
  1533. /* The rcu head is at the start of the inode blob */
  1534. call_void_hook(inode_free_security_rcu, head);
  1535. kmem_cache_free(lsm_inode_cache, head);
  1536. }
  1537. /**
  1538. * security_inode_free() - Free an inode's LSM blob
  1539. * @inode: the inode
  1540. *
  1541. * Release any LSM resources associated with @inode, although due to the
  1542. * inode's RCU protections it is possible that the resources will not be
  1543. * fully released until after the current RCU grace period has elapsed.
  1544. *
  1545. * It is important for LSMs to note that despite being present in a call to
  1546. * security_inode_free(), @inode may still be referenced in a VFS path walk
  1547. * and calls to security_inode_permission() may be made during, or after,
  1548. * a call to security_inode_free(). For this reason the inode->i_security
  1549. * field is released via a call_rcu() callback and any LSMs which need to
  1550. * retain inode state for use in security_inode_permission() should only
  1551. * release that state in the inode_free_security_rcu() LSM hook callback.
  1552. */
  1553. void security_inode_free(struct inode *inode)
  1554. {
  1555. call_void_hook(inode_free_security, inode);
  1556. if (!inode->i_security)
  1557. return;
  1558. call_rcu((struct rcu_head *)inode->i_security, inode_free_by_rcu);
  1559. }
  1560. /**
  1561. * security_dentry_init_security() - Perform dentry initialization
  1562. * @dentry: the dentry to initialize
  1563. * @mode: mode used to determine resource type
  1564. * @name: name of the last path component
  1565. * @xattr_name: name of the security/LSM xattr
  1566. * @ctx: pointer to the resulting LSM context
  1567. * @ctxlen: length of @ctx
  1568. *
  1569. * Compute a context for a dentry as the inode is not yet available since NFSv4
  1570. * has no label backed by an EA anyway. It is important to note that
  1571. * @xattr_name does not need to be free'd by the caller, it is a static string.
  1572. *
  1573. * Return: Returns 0 on success, negative values on failure.
  1574. */
  1575. int security_dentry_init_security(struct dentry *dentry, int mode,
  1576. const struct qstr *name,
  1577. const char **xattr_name, void **ctx,
  1578. u32 *ctxlen)
  1579. {
  1580. return call_int_hook(dentry_init_security, dentry, mode, name,
  1581. xattr_name, ctx, ctxlen);
  1582. }
  1583. EXPORT_SYMBOL(security_dentry_init_security);
  1584. /**
  1585. * security_dentry_create_files_as() - Perform dentry initialization
  1586. * @dentry: the dentry to initialize
  1587. * @mode: mode used to determine resource type
  1588. * @name: name of the last path component
  1589. * @old: creds to use for LSM context calculations
  1590. * @new: creds to modify
  1591. *
  1592. * Compute a context for a dentry as the inode is not yet available and set
  1593. * that context in passed in creds so that new files are created using that
  1594. * context. Context is calculated using the passed in creds and not the creds
  1595. * of the caller.
  1596. *
  1597. * Return: Returns 0 on success, error on failure.
  1598. */
  1599. int security_dentry_create_files_as(struct dentry *dentry, int mode,
  1600. struct qstr *name,
  1601. const struct cred *old, struct cred *new)
  1602. {
  1603. return call_int_hook(dentry_create_files_as, dentry, mode,
  1604. name, old, new);
  1605. }
  1606. EXPORT_SYMBOL(security_dentry_create_files_as);
  1607. /**
  1608. * security_inode_init_security() - Initialize an inode's LSM context
  1609. * @inode: the inode
  1610. * @dir: parent directory
  1611. * @qstr: last component of the pathname
  1612. * @initxattrs: callback function to write xattrs
  1613. * @fs_data: filesystem specific data
  1614. *
  1615. * Obtain the security attribute name suffix and value to set on a newly
  1616. * created inode and set up the incore security field for the new inode. This
  1617. * hook is called by the fs code as part of the inode creation transaction and
  1618. * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
  1619. * hooks called by the VFS.
  1620. *
  1621. * The hook function is expected to populate the xattrs array, by calling
  1622. * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
  1623. * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
  1624. * slot, the hook function should set ->name to the attribute name suffix
  1625. * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
  1626. * to the attribute value, to set ->value_len to the length of the value. If
  1627. * the security module does not use security attributes or does not wish to put
  1628. * a security attribute on this particular inode, then it should return
  1629. * -EOPNOTSUPP to skip this processing.
  1630. *
  1631. * Return: Returns 0 if the LSM successfully initialized all of the inode
  1632. * security attributes that are required, negative values otherwise.
  1633. */
  1634. int security_inode_init_security(struct inode *inode, struct inode *dir,
  1635. const struct qstr *qstr,
  1636. const initxattrs initxattrs, void *fs_data)
  1637. {
  1638. struct lsm_static_call *scall;
  1639. struct xattr *new_xattrs = NULL;
  1640. int ret = -EOPNOTSUPP, xattr_count = 0;
  1641. if (unlikely(IS_PRIVATE(inode)))
  1642. return 0;
  1643. if (!blob_sizes.lbs_xattr_count)
  1644. return 0;
  1645. if (initxattrs) {
  1646. /* Allocate +1 as terminator. */
  1647. new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 1,
  1648. sizeof(*new_xattrs), GFP_NOFS);
  1649. if (!new_xattrs)
  1650. return -ENOMEM;
  1651. }
  1652. lsm_for_each_hook(scall, inode_init_security) {
  1653. ret = scall->hl->hook.inode_init_security(inode, dir, qstr, new_xattrs,
  1654. &xattr_count);
  1655. if (ret && ret != -EOPNOTSUPP)
  1656. goto out;
  1657. /*
  1658. * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
  1659. * means that the LSM is not willing to provide an xattr, not
  1660. * that it wants to signal an error. Thus, continue to invoke
  1661. * the remaining LSMs.
  1662. */
  1663. }
  1664. /* If initxattrs() is NULL, xattr_count is zero, skip the call. */
  1665. if (!xattr_count)
  1666. goto out;
  1667. ret = initxattrs(inode, new_xattrs, fs_data);
  1668. out:
  1669. for (; xattr_count > 0; xattr_count--)
  1670. kfree(new_xattrs[xattr_count - 1].value);
  1671. kfree(new_xattrs);
  1672. return (ret == -EOPNOTSUPP) ? 0 : ret;
  1673. }
  1674. EXPORT_SYMBOL(security_inode_init_security);
  1675. /**
  1676. * security_inode_init_security_anon() - Initialize an anonymous inode
  1677. * @inode: the inode
  1678. * @name: the anonymous inode class
  1679. * @context_inode: an optional related inode
  1680. *
  1681. * Set up the incore security field for the new anonymous inode and return
  1682. * whether the inode creation is permitted by the security module or not.
  1683. *
  1684. * Return: Returns 0 on success, -EACCES if the security module denies the
  1685. * creation of this inode, or another -errno upon other errors.
  1686. */
  1687. int security_inode_init_security_anon(struct inode *inode,
  1688. const struct qstr *name,
  1689. const struct inode *context_inode)
  1690. {
  1691. return call_int_hook(inode_init_security_anon, inode, name,
  1692. context_inode);
  1693. }
  1694. #ifdef CONFIG_SECURITY_PATH
  1695. /**
  1696. * security_path_mknod() - Check if creating a special file is allowed
  1697. * @dir: parent directory
  1698. * @dentry: new file
  1699. * @mode: new file mode
  1700. * @dev: device number
  1701. *
  1702. * Check permissions when creating a file. Note that this hook is called even
  1703. * if mknod operation is being done for a regular file.
  1704. *
  1705. * Return: Returns 0 if permission is granted.
  1706. */
  1707. int security_path_mknod(const struct path *dir, struct dentry *dentry,
  1708. umode_t mode, unsigned int dev)
  1709. {
  1710. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  1711. return 0;
  1712. return call_int_hook(path_mknod, dir, dentry, mode, dev);
  1713. }
  1714. EXPORT_SYMBOL(security_path_mknod);
  1715. /**
  1716. * security_path_post_mknod() - Update inode security after reg file creation
  1717. * @idmap: idmap of the mount
  1718. * @dentry: new file
  1719. *
  1720. * Update inode security field after a regular file has been created.
  1721. */
  1722. void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
  1723. {
  1724. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1725. return;
  1726. call_void_hook(path_post_mknod, idmap, dentry);
  1727. }
  1728. /**
  1729. * security_path_mkdir() - Check if creating a new directory is allowed
  1730. * @dir: parent directory
  1731. * @dentry: new directory
  1732. * @mode: new directory mode
  1733. *
  1734. * Check permissions to create a new directory in the existing directory.
  1735. *
  1736. * Return: Returns 0 if permission is granted.
  1737. */
  1738. int security_path_mkdir(const struct path *dir, struct dentry *dentry,
  1739. umode_t mode)
  1740. {
  1741. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  1742. return 0;
  1743. return call_int_hook(path_mkdir, dir, dentry, mode);
  1744. }
  1745. EXPORT_SYMBOL(security_path_mkdir);
  1746. /**
  1747. * security_path_rmdir() - Check if removing a directory is allowed
  1748. * @dir: parent directory
  1749. * @dentry: directory to remove
  1750. *
  1751. * Check the permission to remove a directory.
  1752. *
  1753. * Return: Returns 0 if permission is granted.
  1754. */
  1755. int security_path_rmdir(const struct path *dir, struct dentry *dentry)
  1756. {
  1757. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  1758. return 0;
  1759. return call_int_hook(path_rmdir, dir, dentry);
  1760. }
  1761. /**
  1762. * security_path_unlink() - Check if removing a hard link is allowed
  1763. * @dir: parent directory
  1764. * @dentry: file
  1765. *
  1766. * Check the permission to remove a hard link to a file.
  1767. *
  1768. * Return: Returns 0 if permission is granted.
  1769. */
  1770. int security_path_unlink(const struct path *dir, struct dentry *dentry)
  1771. {
  1772. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  1773. return 0;
  1774. return call_int_hook(path_unlink, dir, dentry);
  1775. }
  1776. EXPORT_SYMBOL(security_path_unlink);
  1777. /**
  1778. * security_path_symlink() - Check if creating a symbolic link is allowed
  1779. * @dir: parent directory
  1780. * @dentry: symbolic link
  1781. * @old_name: file pathname
  1782. *
  1783. * Check the permission to create a symbolic link to a file.
  1784. *
  1785. * Return: Returns 0 if permission is granted.
  1786. */
  1787. int security_path_symlink(const struct path *dir, struct dentry *dentry,
  1788. const char *old_name)
  1789. {
  1790. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  1791. return 0;
  1792. return call_int_hook(path_symlink, dir, dentry, old_name);
  1793. }
  1794. /**
  1795. * security_path_link - Check if creating a hard link is allowed
  1796. * @old_dentry: existing file
  1797. * @new_dir: new parent directory
  1798. * @new_dentry: new link
  1799. *
  1800. * Check permission before creating a new hard link to a file.
  1801. *
  1802. * Return: Returns 0 if permission is granted.
  1803. */
  1804. int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
  1805. struct dentry *new_dentry)
  1806. {
  1807. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
  1808. return 0;
  1809. return call_int_hook(path_link, old_dentry, new_dir, new_dentry);
  1810. }
  1811. /**
  1812. * security_path_rename() - Check if renaming a file is allowed
  1813. * @old_dir: parent directory of the old file
  1814. * @old_dentry: the old file
  1815. * @new_dir: parent directory of the new file
  1816. * @new_dentry: the new file
  1817. * @flags: flags
  1818. *
  1819. * Check for permission to rename a file or directory.
  1820. *
  1821. * Return: Returns 0 if permission is granted.
  1822. */
  1823. int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  1824. const struct path *new_dir, struct dentry *new_dentry,
  1825. unsigned int flags)
  1826. {
  1827. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
  1828. (d_is_positive(new_dentry) &&
  1829. IS_PRIVATE(d_backing_inode(new_dentry)))))
  1830. return 0;
  1831. return call_int_hook(path_rename, old_dir, old_dentry, new_dir,
  1832. new_dentry, flags);
  1833. }
  1834. EXPORT_SYMBOL(security_path_rename);
  1835. /**
  1836. * security_path_truncate() - Check if truncating a file is allowed
  1837. * @path: file
  1838. *
  1839. * Check permission before truncating the file indicated by path. Note that
  1840. * truncation permissions may also be checked based on already opened files,
  1841. * using the security_file_truncate() hook.
  1842. *
  1843. * Return: Returns 0 if permission is granted.
  1844. */
  1845. int security_path_truncate(const struct path *path)
  1846. {
  1847. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  1848. return 0;
  1849. return call_int_hook(path_truncate, path);
  1850. }
  1851. /**
  1852. * security_path_chmod() - Check if changing the file's mode is allowed
  1853. * @path: file
  1854. * @mode: new mode
  1855. *
  1856. * Check for permission to change a mode of the file @path. The new mode is
  1857. * specified in @mode which is a bitmask of constants from
  1858. * <include/uapi/linux/stat.h>.
  1859. *
  1860. * Return: Returns 0 if permission is granted.
  1861. */
  1862. int security_path_chmod(const struct path *path, umode_t mode)
  1863. {
  1864. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  1865. return 0;
  1866. return call_int_hook(path_chmod, path, mode);
  1867. }
  1868. /**
  1869. * security_path_chown() - Check if changing the file's owner/group is allowed
  1870. * @path: file
  1871. * @uid: file owner
  1872. * @gid: file group
  1873. *
  1874. * Check for permission to change owner/group of a file or directory.
  1875. *
  1876. * Return: Returns 0 if permission is granted.
  1877. */
  1878. int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  1879. {
  1880. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  1881. return 0;
  1882. return call_int_hook(path_chown, path, uid, gid);
  1883. }
  1884. /**
  1885. * security_path_chroot() - Check if changing the root directory is allowed
  1886. * @path: directory
  1887. *
  1888. * Check for permission to change root directory.
  1889. *
  1890. * Return: Returns 0 if permission is granted.
  1891. */
  1892. int security_path_chroot(const struct path *path)
  1893. {
  1894. return call_int_hook(path_chroot, path);
  1895. }
  1896. #endif /* CONFIG_SECURITY_PATH */
  1897. /**
  1898. * security_inode_create() - Check if creating a file is allowed
  1899. * @dir: the parent directory
  1900. * @dentry: the file being created
  1901. * @mode: requested file mode
  1902. *
  1903. * Check permission to create a regular file.
  1904. *
  1905. * Return: Returns 0 if permission is granted.
  1906. */
  1907. int security_inode_create(struct inode *dir, struct dentry *dentry,
  1908. umode_t mode)
  1909. {
  1910. if (unlikely(IS_PRIVATE(dir)))
  1911. return 0;
  1912. return call_int_hook(inode_create, dir, dentry, mode);
  1913. }
  1914. EXPORT_SYMBOL_GPL(security_inode_create);
  1915. /**
  1916. * security_inode_post_create_tmpfile() - Update inode security of new tmpfile
  1917. * @idmap: idmap of the mount
  1918. * @inode: inode of the new tmpfile
  1919. *
  1920. * Update inode security data after a tmpfile has been created.
  1921. */
  1922. void security_inode_post_create_tmpfile(struct mnt_idmap *idmap,
  1923. struct inode *inode)
  1924. {
  1925. if (unlikely(IS_PRIVATE(inode)))
  1926. return;
  1927. call_void_hook(inode_post_create_tmpfile, idmap, inode);
  1928. }
  1929. /**
  1930. * security_inode_link() - Check if creating a hard link is allowed
  1931. * @old_dentry: existing file
  1932. * @dir: new parent directory
  1933. * @new_dentry: new link
  1934. *
  1935. * Check permission before creating a new hard link to a file.
  1936. *
  1937. * Return: Returns 0 if permission is granted.
  1938. */
  1939. int security_inode_link(struct dentry *old_dentry, struct inode *dir,
  1940. struct dentry *new_dentry)
  1941. {
  1942. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
  1943. return 0;
  1944. return call_int_hook(inode_link, old_dentry, dir, new_dentry);
  1945. }
  1946. /**
  1947. * security_inode_unlink() - Check if removing a hard link is allowed
  1948. * @dir: parent directory
  1949. * @dentry: file
  1950. *
  1951. * Check the permission to remove a hard link to a file.
  1952. *
  1953. * Return: Returns 0 if permission is granted.
  1954. */
  1955. int security_inode_unlink(struct inode *dir, struct dentry *dentry)
  1956. {
  1957. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1958. return 0;
  1959. return call_int_hook(inode_unlink, dir, dentry);
  1960. }
  1961. /**
  1962. * security_inode_symlink() - Check if creating a symbolic link is allowed
  1963. * @dir: parent directory
  1964. * @dentry: symbolic link
  1965. * @old_name: existing filename
  1966. *
  1967. * Check the permission to create a symbolic link to a file.
  1968. *
  1969. * Return: Returns 0 if permission is granted.
  1970. */
  1971. int security_inode_symlink(struct inode *dir, struct dentry *dentry,
  1972. const char *old_name)
  1973. {
  1974. if (unlikely(IS_PRIVATE(dir)))
  1975. return 0;
  1976. return call_int_hook(inode_symlink, dir, dentry, old_name);
  1977. }
  1978. /**
  1979. * security_inode_mkdir() - Check if creation a new director is allowed
  1980. * @dir: parent directory
  1981. * @dentry: new directory
  1982. * @mode: new directory mode
  1983. *
  1984. * Check permissions to create a new directory in the existing directory
  1985. * associated with inode structure @dir.
  1986. *
  1987. * Return: Returns 0 if permission is granted.
  1988. */
  1989. int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1990. {
  1991. if (unlikely(IS_PRIVATE(dir)))
  1992. return 0;
  1993. return call_int_hook(inode_mkdir, dir, dentry, mode);
  1994. }
  1995. EXPORT_SYMBOL_GPL(security_inode_mkdir);
  1996. /**
  1997. * security_inode_rmdir() - Check if removing a directory is allowed
  1998. * @dir: parent directory
  1999. * @dentry: directory to be removed
  2000. *
  2001. * Check the permission to remove a directory.
  2002. *
  2003. * Return: Returns 0 if permission is granted.
  2004. */
  2005. int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
  2006. {
  2007. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2008. return 0;
  2009. return call_int_hook(inode_rmdir, dir, dentry);
  2010. }
  2011. /**
  2012. * security_inode_mknod() - Check if creating a special file is allowed
  2013. * @dir: parent directory
  2014. * @dentry: new file
  2015. * @mode: new file mode
  2016. * @dev: device number
  2017. *
  2018. * Check permissions when creating a special file (or a socket or a fifo file
  2019. * created via the mknod system call). Note that if mknod operation is being
  2020. * done for a regular file, then the create hook will be called and not this
  2021. * hook.
  2022. *
  2023. * Return: Returns 0 if permission is granted.
  2024. */
  2025. int security_inode_mknod(struct inode *dir, struct dentry *dentry,
  2026. umode_t mode, dev_t dev)
  2027. {
  2028. if (unlikely(IS_PRIVATE(dir)))
  2029. return 0;
  2030. return call_int_hook(inode_mknod, dir, dentry, mode, dev);
  2031. }
  2032. /**
  2033. * security_inode_rename() - Check if renaming a file is allowed
  2034. * @old_dir: parent directory of the old file
  2035. * @old_dentry: the old file
  2036. * @new_dir: parent directory of the new file
  2037. * @new_dentry: the new file
  2038. * @flags: flags
  2039. *
  2040. * Check for permission to rename a file or directory.
  2041. *
  2042. * Return: Returns 0 if permission is granted.
  2043. */
  2044. int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
  2045. struct inode *new_dir, struct dentry *new_dentry,
  2046. unsigned int flags)
  2047. {
  2048. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
  2049. (d_is_positive(new_dentry) &&
  2050. IS_PRIVATE(d_backing_inode(new_dentry)))))
  2051. return 0;
  2052. if (flags & RENAME_EXCHANGE) {
  2053. int err = call_int_hook(inode_rename, new_dir, new_dentry,
  2054. old_dir, old_dentry);
  2055. if (err)
  2056. return err;
  2057. }
  2058. return call_int_hook(inode_rename, old_dir, old_dentry,
  2059. new_dir, new_dentry);
  2060. }
  2061. /**
  2062. * security_inode_readlink() - Check if reading a symbolic link is allowed
  2063. * @dentry: link
  2064. *
  2065. * Check the permission to read the symbolic link.
  2066. *
  2067. * Return: Returns 0 if permission is granted.
  2068. */
  2069. int security_inode_readlink(struct dentry *dentry)
  2070. {
  2071. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2072. return 0;
  2073. return call_int_hook(inode_readlink, dentry);
  2074. }
  2075. /**
  2076. * security_inode_follow_link() - Check if following a symbolic link is allowed
  2077. * @dentry: link dentry
  2078. * @inode: link inode
  2079. * @rcu: true if in RCU-walk mode
  2080. *
  2081. * Check permission to follow a symbolic link when looking up a pathname. If
  2082. * @rcu is true, @inode is not stable.
  2083. *
  2084. * Return: Returns 0 if permission is granted.
  2085. */
  2086. int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
  2087. bool rcu)
  2088. {
  2089. if (unlikely(IS_PRIVATE(inode)))
  2090. return 0;
  2091. return call_int_hook(inode_follow_link, dentry, inode, rcu);
  2092. }
  2093. /**
  2094. * security_inode_permission() - Check if accessing an inode is allowed
  2095. * @inode: inode
  2096. * @mask: access mask
  2097. *
  2098. * Check permission before accessing an inode. This hook is called by the
  2099. * existing Linux permission function, so a security module can use it to
  2100. * provide additional checking for existing Linux permission checks. Notice
  2101. * that this hook is called when a file is opened (as well as many other
  2102. * operations), whereas the file_security_ops permission hook is called when
  2103. * the actual read/write operations are performed.
  2104. *
  2105. * Return: Returns 0 if permission is granted.
  2106. */
  2107. int security_inode_permission(struct inode *inode, int mask)
  2108. {
  2109. if (unlikely(IS_PRIVATE(inode)))
  2110. return 0;
  2111. return call_int_hook(inode_permission, inode, mask);
  2112. }
  2113. /**
  2114. * security_inode_setattr() - Check if setting file attributes is allowed
  2115. * @idmap: idmap of the mount
  2116. * @dentry: file
  2117. * @attr: new attributes
  2118. *
  2119. * Check permission before setting file attributes. Note that the kernel call
  2120. * to notify_change is performed from several locations, whenever file
  2121. * attributes change (such as when a file is truncated, chown/chmod operations,
  2122. * transferring disk quotas, etc).
  2123. *
  2124. * Return: Returns 0 if permission is granted.
  2125. */
  2126. int security_inode_setattr(struct mnt_idmap *idmap,
  2127. struct dentry *dentry, struct iattr *attr)
  2128. {
  2129. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2130. return 0;
  2131. return call_int_hook(inode_setattr, idmap, dentry, attr);
  2132. }
  2133. EXPORT_SYMBOL_GPL(security_inode_setattr);
  2134. /**
  2135. * security_inode_post_setattr() - Update the inode after a setattr operation
  2136. * @idmap: idmap of the mount
  2137. * @dentry: file
  2138. * @ia_valid: file attributes set
  2139. *
  2140. * Update inode security field after successful setting file attributes.
  2141. */
  2142. void security_inode_post_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  2143. int ia_valid)
  2144. {
  2145. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2146. return;
  2147. call_void_hook(inode_post_setattr, idmap, dentry, ia_valid);
  2148. }
  2149. /**
  2150. * security_inode_getattr() - Check if getting file attributes is allowed
  2151. * @path: file
  2152. *
  2153. * Check permission before obtaining file attributes.
  2154. *
  2155. * Return: Returns 0 if permission is granted.
  2156. */
  2157. int security_inode_getattr(const struct path *path)
  2158. {
  2159. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  2160. return 0;
  2161. return call_int_hook(inode_getattr, path);
  2162. }
  2163. /**
  2164. * security_inode_setxattr() - Check if setting file xattrs is allowed
  2165. * @idmap: idmap of the mount
  2166. * @dentry: file
  2167. * @name: xattr name
  2168. * @value: xattr value
  2169. * @size: size of xattr value
  2170. * @flags: flags
  2171. *
  2172. * This hook performs the desired permission checks before setting the extended
  2173. * attributes (xattrs) on @dentry. It is important to note that we have some
  2174. * additional logic before the main LSM implementation calls to detect if we
  2175. * need to perform an additional capability check at the LSM layer.
  2176. *
  2177. * Normally we enforce a capability check prior to executing the various LSM
  2178. * hook implementations, but if a LSM wants to avoid this capability check,
  2179. * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
  2180. * xattrs that it wants to avoid the capability check, leaving the LSM fully
  2181. * responsible for enforcing the access control for the specific xattr. If all
  2182. * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
  2183. * or return a 0 (the default return value), the capability check is still
  2184. * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
  2185. * check is performed.
  2186. *
  2187. * Return: Returns 0 if permission is granted.
  2188. */
  2189. int security_inode_setxattr(struct mnt_idmap *idmap,
  2190. struct dentry *dentry, const char *name,
  2191. const void *value, size_t size, int flags)
  2192. {
  2193. int rc;
  2194. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2195. return 0;
  2196. /* enforce the capability checks at the lsm layer, if needed */
  2197. if (!call_int_hook(inode_xattr_skipcap, name)) {
  2198. rc = cap_inode_setxattr(dentry, name, value, size, flags);
  2199. if (rc)
  2200. return rc;
  2201. }
  2202. return call_int_hook(inode_setxattr, idmap, dentry, name, value, size,
  2203. flags);
  2204. }
  2205. /**
  2206. * security_inode_set_acl() - Check if setting posix acls is allowed
  2207. * @idmap: idmap of the mount
  2208. * @dentry: file
  2209. * @acl_name: acl name
  2210. * @kacl: acl struct
  2211. *
  2212. * Check permission before setting posix acls, the posix acls in @kacl are
  2213. * identified by @acl_name.
  2214. *
  2215. * Return: Returns 0 if permission is granted.
  2216. */
  2217. int security_inode_set_acl(struct mnt_idmap *idmap,
  2218. struct dentry *dentry, const char *acl_name,
  2219. struct posix_acl *kacl)
  2220. {
  2221. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2222. return 0;
  2223. return call_int_hook(inode_set_acl, idmap, dentry, acl_name, kacl);
  2224. }
  2225. /**
  2226. * security_inode_post_set_acl() - Update inode security from posix acls set
  2227. * @dentry: file
  2228. * @acl_name: acl name
  2229. * @kacl: acl struct
  2230. *
  2231. * Update inode security data after successfully setting posix acls on @dentry.
  2232. * The posix acls in @kacl are identified by @acl_name.
  2233. */
  2234. void security_inode_post_set_acl(struct dentry *dentry, const char *acl_name,
  2235. struct posix_acl *kacl)
  2236. {
  2237. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2238. return;
  2239. call_void_hook(inode_post_set_acl, dentry, acl_name, kacl);
  2240. }
  2241. /**
  2242. * security_inode_get_acl() - Check if reading posix acls is allowed
  2243. * @idmap: idmap of the mount
  2244. * @dentry: file
  2245. * @acl_name: acl name
  2246. *
  2247. * Check permission before getting osix acls, the posix acls are identified by
  2248. * @acl_name.
  2249. *
  2250. * Return: Returns 0 if permission is granted.
  2251. */
  2252. int security_inode_get_acl(struct mnt_idmap *idmap,
  2253. struct dentry *dentry, const char *acl_name)
  2254. {
  2255. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2256. return 0;
  2257. return call_int_hook(inode_get_acl, idmap, dentry, acl_name);
  2258. }
  2259. /**
  2260. * security_inode_remove_acl() - Check if removing a posix acl is allowed
  2261. * @idmap: idmap of the mount
  2262. * @dentry: file
  2263. * @acl_name: acl name
  2264. *
  2265. * Check permission before removing posix acls, the posix acls are identified
  2266. * by @acl_name.
  2267. *
  2268. * Return: Returns 0 if permission is granted.
  2269. */
  2270. int security_inode_remove_acl(struct mnt_idmap *idmap,
  2271. struct dentry *dentry, const char *acl_name)
  2272. {
  2273. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2274. return 0;
  2275. return call_int_hook(inode_remove_acl, idmap, dentry, acl_name);
  2276. }
  2277. /**
  2278. * security_inode_post_remove_acl() - Update inode security after rm posix acls
  2279. * @idmap: idmap of the mount
  2280. * @dentry: file
  2281. * @acl_name: acl name
  2282. *
  2283. * Update inode security data after successfully removing posix acls on
  2284. * @dentry in @idmap. The posix acls are identified by @acl_name.
  2285. */
  2286. void security_inode_post_remove_acl(struct mnt_idmap *idmap,
  2287. struct dentry *dentry, const char *acl_name)
  2288. {
  2289. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2290. return;
  2291. call_void_hook(inode_post_remove_acl, idmap, dentry, acl_name);
  2292. }
  2293. /**
  2294. * security_inode_post_setxattr() - Update the inode after a setxattr operation
  2295. * @dentry: file
  2296. * @name: xattr name
  2297. * @value: xattr value
  2298. * @size: xattr value size
  2299. * @flags: flags
  2300. *
  2301. * Update inode security field after successful setxattr operation.
  2302. */
  2303. void security_inode_post_setxattr(struct dentry *dentry, const char *name,
  2304. const void *value, size_t size, int flags)
  2305. {
  2306. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2307. return;
  2308. call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
  2309. }
  2310. /**
  2311. * security_inode_getxattr() - Check if xattr access is allowed
  2312. * @dentry: file
  2313. * @name: xattr name
  2314. *
  2315. * Check permission before obtaining the extended attributes identified by
  2316. * @name for @dentry.
  2317. *
  2318. * Return: Returns 0 if permission is granted.
  2319. */
  2320. int security_inode_getxattr(struct dentry *dentry, const char *name)
  2321. {
  2322. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2323. return 0;
  2324. return call_int_hook(inode_getxattr, dentry, name);
  2325. }
  2326. /**
  2327. * security_inode_listxattr() - Check if listing xattrs is allowed
  2328. * @dentry: file
  2329. *
  2330. * Check permission before obtaining the list of extended attribute names for
  2331. * @dentry.
  2332. *
  2333. * Return: Returns 0 if permission is granted.
  2334. */
  2335. int security_inode_listxattr(struct dentry *dentry)
  2336. {
  2337. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2338. return 0;
  2339. return call_int_hook(inode_listxattr, dentry);
  2340. }
  2341. /**
  2342. * security_inode_removexattr() - Check if removing an xattr is allowed
  2343. * @idmap: idmap of the mount
  2344. * @dentry: file
  2345. * @name: xattr name
  2346. *
  2347. * This hook performs the desired permission checks before setting the extended
  2348. * attributes (xattrs) on @dentry. It is important to note that we have some
  2349. * additional logic before the main LSM implementation calls to detect if we
  2350. * need to perform an additional capability check at the LSM layer.
  2351. *
  2352. * Normally we enforce a capability check prior to executing the various LSM
  2353. * hook implementations, but if a LSM wants to avoid this capability check,
  2354. * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
  2355. * xattrs that it wants to avoid the capability check, leaving the LSM fully
  2356. * responsible for enforcing the access control for the specific xattr. If all
  2357. * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
  2358. * or return a 0 (the default return value), the capability check is still
  2359. * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
  2360. * check is performed.
  2361. *
  2362. * Return: Returns 0 if permission is granted.
  2363. */
  2364. int security_inode_removexattr(struct mnt_idmap *idmap,
  2365. struct dentry *dentry, const char *name)
  2366. {
  2367. int rc;
  2368. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2369. return 0;
  2370. /* enforce the capability checks at the lsm layer, if needed */
  2371. if (!call_int_hook(inode_xattr_skipcap, name)) {
  2372. rc = cap_inode_removexattr(idmap, dentry, name);
  2373. if (rc)
  2374. return rc;
  2375. }
  2376. return call_int_hook(inode_removexattr, idmap, dentry, name);
  2377. }
  2378. /**
  2379. * security_inode_post_removexattr() - Update the inode after a removexattr op
  2380. * @dentry: file
  2381. * @name: xattr name
  2382. *
  2383. * Update the inode after a successful removexattr operation.
  2384. */
  2385. void security_inode_post_removexattr(struct dentry *dentry, const char *name)
  2386. {
  2387. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  2388. return;
  2389. call_void_hook(inode_post_removexattr, dentry, name);
  2390. }
  2391. /**
  2392. * security_inode_need_killpriv() - Check if security_inode_killpriv() required
  2393. * @dentry: associated dentry
  2394. *
  2395. * Called when an inode has been changed to determine if
  2396. * security_inode_killpriv() should be called.
  2397. *
  2398. * Return: Return <0 on error to abort the inode change operation, return 0 if
  2399. * security_inode_killpriv() does not need to be called, return >0 if
  2400. * security_inode_killpriv() does need to be called.
  2401. */
  2402. int security_inode_need_killpriv(struct dentry *dentry)
  2403. {
  2404. return call_int_hook(inode_need_killpriv, dentry);
  2405. }
  2406. /**
  2407. * security_inode_killpriv() - The setuid bit is removed, update LSM state
  2408. * @idmap: idmap of the mount
  2409. * @dentry: associated dentry
  2410. *
  2411. * The @dentry's setuid bit is being removed. Remove similar security labels.
  2412. * Called with the dentry->d_inode->i_mutex held.
  2413. *
  2414. * Return: Return 0 on success. If error is returned, then the operation
  2415. * causing setuid bit removal is failed.
  2416. */
  2417. int security_inode_killpriv(struct mnt_idmap *idmap,
  2418. struct dentry *dentry)
  2419. {
  2420. return call_int_hook(inode_killpriv, idmap, dentry);
  2421. }
  2422. /**
  2423. * security_inode_getsecurity() - Get the xattr security label of an inode
  2424. * @idmap: idmap of the mount
  2425. * @inode: inode
  2426. * @name: xattr name
  2427. * @buffer: security label buffer
  2428. * @alloc: allocation flag
  2429. *
  2430. * Retrieve a copy of the extended attribute representation of the security
  2431. * label associated with @name for @inode via @buffer. Note that @name is the
  2432. * remainder of the attribute name after the security prefix has been removed.
  2433. * @alloc is used to specify if the call should return a value via the buffer
  2434. * or just the value length.
  2435. *
  2436. * Return: Returns size of buffer on success.
  2437. */
  2438. int security_inode_getsecurity(struct mnt_idmap *idmap,
  2439. struct inode *inode, const char *name,
  2440. void **buffer, bool alloc)
  2441. {
  2442. if (unlikely(IS_PRIVATE(inode)))
  2443. return LSM_RET_DEFAULT(inode_getsecurity);
  2444. return call_int_hook(inode_getsecurity, idmap, inode, name, buffer,
  2445. alloc);
  2446. }
  2447. /**
  2448. * security_inode_setsecurity() - Set the xattr security label of an inode
  2449. * @inode: inode
  2450. * @name: xattr name
  2451. * @value: security label
  2452. * @size: length of security label
  2453. * @flags: flags
  2454. *
  2455. * Set the security label associated with @name for @inode from the extended
  2456. * attribute value @value. @size indicates the size of the @value in bytes.
  2457. * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
  2458. * remainder of the attribute name after the security. prefix has been removed.
  2459. *
  2460. * Return: Returns 0 on success.
  2461. */
  2462. int security_inode_setsecurity(struct inode *inode, const char *name,
  2463. const void *value, size_t size, int flags)
  2464. {
  2465. if (unlikely(IS_PRIVATE(inode)))
  2466. return LSM_RET_DEFAULT(inode_setsecurity);
  2467. return call_int_hook(inode_setsecurity, inode, name, value, size,
  2468. flags);
  2469. }
  2470. /**
  2471. * security_inode_listsecurity() - List the xattr security label names
  2472. * @inode: inode
  2473. * @buffer: buffer
  2474. * @buffer_size: size of buffer
  2475. *
  2476. * Copy the extended attribute names for the security labels associated with
  2477. * @inode into @buffer. The maximum size of @buffer is specified by
  2478. * @buffer_size. @buffer may be NULL to request the size of the buffer
  2479. * required.
  2480. *
  2481. * Return: Returns number of bytes used/required on success.
  2482. */
  2483. int security_inode_listsecurity(struct inode *inode,
  2484. char *buffer, size_t buffer_size)
  2485. {
  2486. if (unlikely(IS_PRIVATE(inode)))
  2487. return 0;
  2488. return call_int_hook(inode_listsecurity, inode, buffer, buffer_size);
  2489. }
  2490. EXPORT_SYMBOL(security_inode_listsecurity);
  2491. /**
  2492. * security_inode_getsecid() - Get an inode's secid
  2493. * @inode: inode
  2494. * @secid: secid to return
  2495. *
  2496. * Get the secid associated with the node. In case of failure, @secid will be
  2497. * set to zero.
  2498. */
  2499. void security_inode_getsecid(struct inode *inode, u32 *secid)
  2500. {
  2501. call_void_hook(inode_getsecid, inode, secid);
  2502. }
  2503. /**
  2504. * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
  2505. * @src: union dentry of copy-up file
  2506. * @new: newly created creds
  2507. *
  2508. * A file is about to be copied up from lower layer to upper layer of overlay
  2509. * filesystem. Security module can prepare a set of new creds and modify as
  2510. * need be and return new creds. Caller will switch to new creds temporarily to
  2511. * create new file and release newly allocated creds.
  2512. *
  2513. * Return: Returns 0 on success or a negative error code on error.
  2514. */
  2515. int security_inode_copy_up(struct dentry *src, struct cred **new)
  2516. {
  2517. return call_int_hook(inode_copy_up, src, new);
  2518. }
  2519. EXPORT_SYMBOL(security_inode_copy_up);
  2520. /**
  2521. * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
  2522. * @src: union dentry of copy-up file
  2523. * @name: xattr name
  2524. *
  2525. * Filter the xattrs being copied up when a unioned file is copied up from a
  2526. * lower layer to the union/overlay layer. The caller is responsible for
  2527. * reading and writing the xattrs, this hook is merely a filter.
  2528. *
  2529. * Return: Returns 0 to accept the xattr, -ECANCELED to discard the xattr,
  2530. * -EOPNOTSUPP if the security module does not know about attribute,
  2531. * or a negative error code to abort the copy up.
  2532. */
  2533. int security_inode_copy_up_xattr(struct dentry *src, const char *name)
  2534. {
  2535. int rc;
  2536. rc = call_int_hook(inode_copy_up_xattr, src, name);
  2537. if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
  2538. return rc;
  2539. return LSM_RET_DEFAULT(inode_copy_up_xattr);
  2540. }
  2541. EXPORT_SYMBOL(security_inode_copy_up_xattr);
  2542. /**
  2543. * security_inode_setintegrity() - Set the inode's integrity data
  2544. * @inode: inode
  2545. * @type: type of integrity, e.g. hash digest, signature, etc
  2546. * @value: the integrity value
  2547. * @size: size of the integrity value
  2548. *
  2549. * Register a verified integrity measurement of a inode with LSMs.
  2550. * LSMs should free the previously saved data if @value is NULL.
  2551. *
  2552. * Return: Returns 0 on success, negative values on failure.
  2553. */
  2554. int security_inode_setintegrity(const struct inode *inode,
  2555. enum lsm_integrity_type type, const void *value,
  2556. size_t size)
  2557. {
  2558. return call_int_hook(inode_setintegrity, inode, type, value, size);
  2559. }
  2560. EXPORT_SYMBOL(security_inode_setintegrity);
  2561. /**
  2562. * security_kernfs_init_security() - Init LSM context for a kernfs node
  2563. * @kn_dir: parent kernfs node
  2564. * @kn: the kernfs node to initialize
  2565. *
  2566. * Initialize the security context of a newly created kernfs node based on its
  2567. * own and its parent's attributes.
  2568. *
  2569. * Return: Returns 0 if permission is granted.
  2570. */
  2571. int security_kernfs_init_security(struct kernfs_node *kn_dir,
  2572. struct kernfs_node *kn)
  2573. {
  2574. return call_int_hook(kernfs_init_security, kn_dir, kn);
  2575. }
  2576. /**
  2577. * security_file_permission() - Check file permissions
  2578. * @file: file
  2579. * @mask: requested permissions
  2580. *
  2581. * Check file permissions before accessing an open file. This hook is called
  2582. * by various operations that read or write files. A security module can use
  2583. * this hook to perform additional checking on these operations, e.g. to
  2584. * revalidate permissions on use to support privilege bracketing or policy
  2585. * changes. Notice that this hook is used when the actual read/write
  2586. * operations are performed, whereas the inode_security_ops hook is called when
  2587. * a file is opened (as well as many other operations). Although this hook can
  2588. * be used to revalidate permissions for various system call operations that
  2589. * read or write files, it does not address the revalidation of permissions for
  2590. * memory-mapped files. Security modules must handle this separately if they
  2591. * need such revalidation.
  2592. *
  2593. * Return: Returns 0 if permission is granted.
  2594. */
  2595. int security_file_permission(struct file *file, int mask)
  2596. {
  2597. return call_int_hook(file_permission, file, mask);
  2598. }
  2599. /**
  2600. * security_file_alloc() - Allocate and init a file's LSM blob
  2601. * @file: the file
  2602. *
  2603. * Allocate and attach a security structure to the file->f_security field. The
  2604. * security field is initialized to NULL when the structure is first created.
  2605. *
  2606. * Return: Return 0 if the hook is successful and permission is granted.
  2607. */
  2608. int security_file_alloc(struct file *file)
  2609. {
  2610. int rc = lsm_file_alloc(file);
  2611. if (rc)
  2612. return rc;
  2613. rc = call_int_hook(file_alloc_security, file);
  2614. if (unlikely(rc))
  2615. security_file_free(file);
  2616. return rc;
  2617. }
  2618. /**
  2619. * security_file_release() - Perform actions before releasing the file ref
  2620. * @file: the file
  2621. *
  2622. * Perform actions before releasing the last reference to a file.
  2623. */
  2624. void security_file_release(struct file *file)
  2625. {
  2626. call_void_hook(file_release, file);
  2627. }
  2628. /**
  2629. * security_file_free() - Free a file's LSM blob
  2630. * @file: the file
  2631. *
  2632. * Deallocate and free any security structures stored in file->f_security.
  2633. */
  2634. void security_file_free(struct file *file)
  2635. {
  2636. void *blob;
  2637. call_void_hook(file_free_security, file);
  2638. blob = file->f_security;
  2639. if (blob) {
  2640. file->f_security = NULL;
  2641. kmem_cache_free(lsm_file_cache, blob);
  2642. }
  2643. }
  2644. /**
  2645. * security_file_ioctl() - Check if an ioctl is allowed
  2646. * @file: associated file
  2647. * @cmd: ioctl cmd
  2648. * @arg: ioctl arguments
  2649. *
  2650. * Check permission for an ioctl operation on @file. Note that @arg sometimes
  2651. * represents a user space pointer; in other cases, it may be a simple integer
  2652. * value. When @arg represents a user space pointer, it should never be used
  2653. * by the security module.
  2654. *
  2655. * Return: Returns 0 if permission is granted.
  2656. */
  2657. int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2658. {
  2659. return call_int_hook(file_ioctl, file, cmd, arg);
  2660. }
  2661. EXPORT_SYMBOL_GPL(security_file_ioctl);
  2662. /**
  2663. * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
  2664. * @file: associated file
  2665. * @cmd: ioctl cmd
  2666. * @arg: ioctl arguments
  2667. *
  2668. * Compat version of security_file_ioctl() that correctly handles 32-bit
  2669. * processes running on 64-bit kernels.
  2670. *
  2671. * Return: Returns 0 if permission is granted.
  2672. */
  2673. int security_file_ioctl_compat(struct file *file, unsigned int cmd,
  2674. unsigned long arg)
  2675. {
  2676. return call_int_hook(file_ioctl_compat, file, cmd, arg);
  2677. }
  2678. EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
  2679. static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
  2680. {
  2681. /*
  2682. * Does we have PROT_READ and does the application expect
  2683. * it to imply PROT_EXEC? If not, nothing to talk about...
  2684. */
  2685. if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
  2686. return prot;
  2687. if (!(current->personality & READ_IMPLIES_EXEC))
  2688. return prot;
  2689. /*
  2690. * if that's an anonymous mapping, let it.
  2691. */
  2692. if (!file)
  2693. return prot | PROT_EXEC;
  2694. /*
  2695. * ditto if it's not on noexec mount, except that on !MMU we need
  2696. * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
  2697. */
  2698. if (!path_noexec(&file->f_path)) {
  2699. #ifndef CONFIG_MMU
  2700. if (file->f_op->mmap_capabilities) {
  2701. unsigned caps = file->f_op->mmap_capabilities(file);
  2702. if (!(caps & NOMMU_MAP_EXEC))
  2703. return prot;
  2704. }
  2705. #endif
  2706. return prot | PROT_EXEC;
  2707. }
  2708. /* anything on noexec mount won't get PROT_EXEC */
  2709. return prot;
  2710. }
  2711. /**
  2712. * security_mmap_file() - Check if mmap'ing a file is allowed
  2713. * @file: file
  2714. * @prot: protection applied by the kernel
  2715. * @flags: flags
  2716. *
  2717. * Check permissions for a mmap operation. The @file may be NULL, e.g. if
  2718. * mapping anonymous memory.
  2719. *
  2720. * Return: Returns 0 if permission is granted.
  2721. */
  2722. int security_mmap_file(struct file *file, unsigned long prot,
  2723. unsigned long flags)
  2724. {
  2725. return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot),
  2726. flags);
  2727. }
  2728. /**
  2729. * security_mmap_addr() - Check if mmap'ing an address is allowed
  2730. * @addr: address
  2731. *
  2732. * Check permissions for a mmap operation at @addr.
  2733. *
  2734. * Return: Returns 0 if permission is granted.
  2735. */
  2736. int security_mmap_addr(unsigned long addr)
  2737. {
  2738. return call_int_hook(mmap_addr, addr);
  2739. }
  2740. /**
  2741. * security_file_mprotect() - Check if changing memory protections is allowed
  2742. * @vma: memory region
  2743. * @reqprot: application requested protection
  2744. * @prot: protection applied by the kernel
  2745. *
  2746. * Check permissions before changing memory access permissions.
  2747. *
  2748. * Return: Returns 0 if permission is granted.
  2749. */
  2750. int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
  2751. unsigned long prot)
  2752. {
  2753. return call_int_hook(file_mprotect, vma, reqprot, prot);
  2754. }
  2755. /**
  2756. * security_file_lock() - Check if a file lock is allowed
  2757. * @file: file
  2758. * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
  2759. *
  2760. * Check permission before performing file locking operations. Note the hook
  2761. * mediates both flock and fcntl style locks.
  2762. *
  2763. * Return: Returns 0 if permission is granted.
  2764. */
  2765. int security_file_lock(struct file *file, unsigned int cmd)
  2766. {
  2767. return call_int_hook(file_lock, file, cmd);
  2768. }
  2769. /**
  2770. * security_file_fcntl() - Check if fcntl() op is allowed
  2771. * @file: file
  2772. * @cmd: fcntl command
  2773. * @arg: command argument
  2774. *
  2775. * Check permission before allowing the file operation specified by @cmd from
  2776. * being performed on the file @file. Note that @arg sometimes represents a
  2777. * user space pointer; in other cases, it may be a simple integer value. When
  2778. * @arg represents a user space pointer, it should never be used by the
  2779. * security module.
  2780. *
  2781. * Return: Returns 0 if permission is granted.
  2782. */
  2783. int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  2784. {
  2785. return call_int_hook(file_fcntl, file, cmd, arg);
  2786. }
  2787. /**
  2788. * security_file_set_fowner() - Set the file owner info in the LSM blob
  2789. * @file: the file
  2790. *
  2791. * Save owner security information (typically from current->security) in
  2792. * file->f_security for later use by the send_sigiotask hook.
  2793. *
  2794. * This hook is called with file->f_owner.lock held.
  2795. *
  2796. * Return: Returns 0 on success.
  2797. */
  2798. void security_file_set_fowner(struct file *file)
  2799. {
  2800. call_void_hook(file_set_fowner, file);
  2801. }
  2802. /**
  2803. * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
  2804. * @tsk: target task
  2805. * @fown: signal sender
  2806. * @sig: signal to be sent, SIGIO is sent if 0
  2807. *
  2808. * Check permission for the file owner @fown to send SIGIO or SIGURG to the
  2809. * process @tsk. Note that this hook is sometimes called from interrupt. Note
  2810. * that the fown_struct, @fown, is never outside the context of a struct file,
  2811. * so the file structure (and associated security information) can always be
  2812. * obtained: container_of(fown, struct file, f_owner).
  2813. *
  2814. * Return: Returns 0 if permission is granted.
  2815. */
  2816. int security_file_send_sigiotask(struct task_struct *tsk,
  2817. struct fown_struct *fown, int sig)
  2818. {
  2819. return call_int_hook(file_send_sigiotask, tsk, fown, sig);
  2820. }
  2821. /**
  2822. * security_file_receive() - Check if receiving a file via IPC is allowed
  2823. * @file: file being received
  2824. *
  2825. * This hook allows security modules to control the ability of a process to
  2826. * receive an open file descriptor via socket IPC.
  2827. *
  2828. * Return: Returns 0 if permission is granted.
  2829. */
  2830. int security_file_receive(struct file *file)
  2831. {
  2832. return call_int_hook(file_receive, file);
  2833. }
  2834. /**
  2835. * security_file_open() - Save open() time state for late use by the LSM
  2836. * @file:
  2837. *
  2838. * Save open-time permission checking state for later use upon file_permission,
  2839. * and recheck access if anything has changed since inode_permission.
  2840. *
  2841. * Return: Returns 0 if permission is granted.
  2842. */
  2843. int security_file_open(struct file *file)
  2844. {
  2845. int ret;
  2846. ret = call_int_hook(file_open, file);
  2847. if (ret)
  2848. return ret;
  2849. return fsnotify_open_perm(file);
  2850. }
  2851. /**
  2852. * security_file_post_open() - Evaluate a file after it has been opened
  2853. * @file: the file
  2854. * @mask: access mask
  2855. *
  2856. * Evaluate an opened file and the access mask requested with open(). The hook
  2857. * is useful for LSMs that require the file content to be available in order to
  2858. * make decisions.
  2859. *
  2860. * Return: Returns 0 if permission is granted.
  2861. */
  2862. int security_file_post_open(struct file *file, int mask)
  2863. {
  2864. return call_int_hook(file_post_open, file, mask);
  2865. }
  2866. EXPORT_SYMBOL_GPL(security_file_post_open);
  2867. /**
  2868. * security_file_truncate() - Check if truncating a file is allowed
  2869. * @file: file
  2870. *
  2871. * Check permission before truncating a file, i.e. using ftruncate. Note that
  2872. * truncation permission may also be checked based on the path, using the
  2873. * @path_truncate hook.
  2874. *
  2875. * Return: Returns 0 if permission is granted.
  2876. */
  2877. int security_file_truncate(struct file *file)
  2878. {
  2879. return call_int_hook(file_truncate, file);
  2880. }
  2881. /**
  2882. * security_task_alloc() - Allocate a task's LSM blob
  2883. * @task: the task
  2884. * @clone_flags: flags indicating what is being shared
  2885. *
  2886. * Handle allocation of task-related resources.
  2887. *
  2888. * Return: Returns a zero on success, negative values on failure.
  2889. */
  2890. int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
  2891. {
  2892. int rc = lsm_task_alloc(task);
  2893. if (rc)
  2894. return rc;
  2895. rc = call_int_hook(task_alloc, task, clone_flags);
  2896. if (unlikely(rc))
  2897. security_task_free(task);
  2898. return rc;
  2899. }
  2900. /**
  2901. * security_task_free() - Free a task's LSM blob and related resources
  2902. * @task: task
  2903. *
  2904. * Handle release of task-related resources. Note that this can be called from
  2905. * interrupt context.
  2906. */
  2907. void security_task_free(struct task_struct *task)
  2908. {
  2909. call_void_hook(task_free, task);
  2910. kfree(task->security);
  2911. task->security = NULL;
  2912. }
  2913. /**
  2914. * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
  2915. * @cred: credentials
  2916. * @gfp: gfp flags
  2917. *
  2918. * Only allocate sufficient memory and attach to @cred such that
  2919. * cred_transfer() will not get ENOMEM.
  2920. *
  2921. * Return: Returns 0 on success, negative values on failure.
  2922. */
  2923. int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  2924. {
  2925. int rc = lsm_cred_alloc(cred, gfp);
  2926. if (rc)
  2927. return rc;
  2928. rc = call_int_hook(cred_alloc_blank, cred, gfp);
  2929. if (unlikely(rc))
  2930. security_cred_free(cred);
  2931. return rc;
  2932. }
  2933. /**
  2934. * security_cred_free() - Free the cred's LSM blob and associated resources
  2935. * @cred: credentials
  2936. *
  2937. * Deallocate and clear the cred->security field in a set of credentials.
  2938. */
  2939. void security_cred_free(struct cred *cred)
  2940. {
  2941. /*
  2942. * There is a failure case in prepare_creds() that
  2943. * may result in a call here with ->security being NULL.
  2944. */
  2945. if (unlikely(cred->security == NULL))
  2946. return;
  2947. call_void_hook(cred_free, cred);
  2948. kfree(cred->security);
  2949. cred->security = NULL;
  2950. }
  2951. /**
  2952. * security_prepare_creds() - Prepare a new set of credentials
  2953. * @new: new credentials
  2954. * @old: original credentials
  2955. * @gfp: gfp flags
  2956. *
  2957. * Prepare a new set of credentials by copying the data from the old set.
  2958. *
  2959. * Return: Returns 0 on success, negative values on failure.
  2960. */
  2961. int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
  2962. {
  2963. int rc = lsm_cred_alloc(new, gfp);
  2964. if (rc)
  2965. return rc;
  2966. rc = call_int_hook(cred_prepare, new, old, gfp);
  2967. if (unlikely(rc))
  2968. security_cred_free(new);
  2969. return rc;
  2970. }
  2971. /**
  2972. * security_transfer_creds() - Transfer creds
  2973. * @new: target credentials
  2974. * @old: original credentials
  2975. *
  2976. * Transfer data from original creds to new creds.
  2977. */
  2978. void security_transfer_creds(struct cred *new, const struct cred *old)
  2979. {
  2980. call_void_hook(cred_transfer, new, old);
  2981. }
  2982. /**
  2983. * security_cred_getsecid() - Get the secid from a set of credentials
  2984. * @c: credentials
  2985. * @secid: secid value
  2986. *
  2987. * Retrieve the security identifier of the cred structure @c. In case of
  2988. * failure, @secid will be set to zero.
  2989. */
  2990. void security_cred_getsecid(const struct cred *c, u32 *secid)
  2991. {
  2992. *secid = 0;
  2993. call_void_hook(cred_getsecid, c, secid);
  2994. }
  2995. EXPORT_SYMBOL(security_cred_getsecid);
  2996. /**
  2997. * security_kernel_act_as() - Set the kernel credentials to act as secid
  2998. * @new: credentials
  2999. * @secid: secid
  3000. *
  3001. * Set the credentials for a kernel service to act as (subjective context).
  3002. * The current task must be the one that nominated @secid.
  3003. *
  3004. * Return: Returns 0 if successful.
  3005. */
  3006. int security_kernel_act_as(struct cred *new, u32 secid)
  3007. {
  3008. return call_int_hook(kernel_act_as, new, secid);
  3009. }
  3010. /**
  3011. * security_kernel_create_files_as() - Set file creation context using an inode
  3012. * @new: target credentials
  3013. * @inode: reference inode
  3014. *
  3015. * Set the file creation context in a set of credentials to be the same as the
  3016. * objective context of the specified inode. The current task must be the one
  3017. * that nominated @inode.
  3018. *
  3019. * Return: Returns 0 if successful.
  3020. */
  3021. int security_kernel_create_files_as(struct cred *new, struct inode *inode)
  3022. {
  3023. return call_int_hook(kernel_create_files_as, new, inode);
  3024. }
  3025. /**
  3026. * security_kernel_module_request() - Check if loading a module is allowed
  3027. * @kmod_name: module name
  3028. *
  3029. * Ability to trigger the kernel to automatically upcall to userspace for
  3030. * userspace to load a kernel module with the given name.
  3031. *
  3032. * Return: Returns 0 if successful.
  3033. */
  3034. int security_kernel_module_request(char *kmod_name)
  3035. {
  3036. return call_int_hook(kernel_module_request, kmod_name);
  3037. }
  3038. /**
  3039. * security_kernel_read_file() - Read a file specified by userspace
  3040. * @file: file
  3041. * @id: file identifier
  3042. * @contents: trust if security_kernel_post_read_file() will be called
  3043. *
  3044. * Read a file specified by userspace.
  3045. *
  3046. * Return: Returns 0 if permission is granted.
  3047. */
  3048. int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
  3049. bool contents)
  3050. {
  3051. return call_int_hook(kernel_read_file, file, id, contents);
  3052. }
  3053. EXPORT_SYMBOL_GPL(security_kernel_read_file);
  3054. /**
  3055. * security_kernel_post_read_file() - Read a file specified by userspace
  3056. * @file: file
  3057. * @buf: file contents
  3058. * @size: size of file contents
  3059. * @id: file identifier
  3060. *
  3061. * Read a file specified by userspace. This must be paired with a prior call
  3062. * to security_kernel_read_file() call that indicated this hook would also be
  3063. * called, see security_kernel_read_file() for more information.
  3064. *
  3065. * Return: Returns 0 if permission is granted.
  3066. */
  3067. int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
  3068. enum kernel_read_file_id id)
  3069. {
  3070. return call_int_hook(kernel_post_read_file, file, buf, size, id);
  3071. }
  3072. EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
  3073. /**
  3074. * security_kernel_load_data() - Load data provided by userspace
  3075. * @id: data identifier
  3076. * @contents: true if security_kernel_post_load_data() will be called
  3077. *
  3078. * Load data provided by userspace.
  3079. *
  3080. * Return: Returns 0 if permission is granted.
  3081. */
  3082. int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
  3083. {
  3084. return call_int_hook(kernel_load_data, id, contents);
  3085. }
  3086. EXPORT_SYMBOL_GPL(security_kernel_load_data);
  3087. /**
  3088. * security_kernel_post_load_data() - Load userspace data from a non-file source
  3089. * @buf: data
  3090. * @size: size of data
  3091. * @id: data identifier
  3092. * @description: text description of data, specific to the id value
  3093. *
  3094. * Load data provided by a non-file source (usually userspace buffer). This
  3095. * must be paired with a prior security_kernel_load_data() call that indicated
  3096. * this hook would also be called, see security_kernel_load_data() for more
  3097. * information.
  3098. *
  3099. * Return: Returns 0 if permission is granted.
  3100. */
  3101. int security_kernel_post_load_data(char *buf, loff_t size,
  3102. enum kernel_load_data_id id,
  3103. char *description)
  3104. {
  3105. return call_int_hook(kernel_post_load_data, buf, size, id, description);
  3106. }
  3107. EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
  3108. /**
  3109. * security_task_fix_setuid() - Update LSM with new user id attributes
  3110. * @new: updated credentials
  3111. * @old: credentials being replaced
  3112. * @flags: LSM_SETID_* flag values
  3113. *
  3114. * Update the module's state after setting one or more of the user identity
  3115. * attributes of the current process. The @flags parameter indicates which of
  3116. * the set*uid system calls invoked this hook. If @new is the set of
  3117. * credentials that will be installed. Modifications should be made to this
  3118. * rather than to @current->cred.
  3119. *
  3120. * Return: Returns 0 on success.
  3121. */
  3122. int security_task_fix_setuid(struct cred *new, const struct cred *old,
  3123. int flags)
  3124. {
  3125. return call_int_hook(task_fix_setuid, new, old, flags);
  3126. }
  3127. /**
  3128. * security_task_fix_setgid() - Update LSM with new group id attributes
  3129. * @new: updated credentials
  3130. * @old: credentials being replaced
  3131. * @flags: LSM_SETID_* flag value
  3132. *
  3133. * Update the module's state after setting one or more of the group identity
  3134. * attributes of the current process. The @flags parameter indicates which of
  3135. * the set*gid system calls invoked this hook. @new is the set of credentials
  3136. * that will be installed. Modifications should be made to this rather than to
  3137. * @current->cred.
  3138. *
  3139. * Return: Returns 0 on success.
  3140. */
  3141. int security_task_fix_setgid(struct cred *new, const struct cred *old,
  3142. int flags)
  3143. {
  3144. return call_int_hook(task_fix_setgid, new, old, flags);
  3145. }
  3146. /**
  3147. * security_task_fix_setgroups() - Update LSM with new supplementary groups
  3148. * @new: updated credentials
  3149. * @old: credentials being replaced
  3150. *
  3151. * Update the module's state after setting the supplementary group identity
  3152. * attributes of the current process. @new is the set of credentials that will
  3153. * be installed. Modifications should be made to this rather than to
  3154. * @current->cred.
  3155. *
  3156. * Return: Returns 0 on success.
  3157. */
  3158. int security_task_fix_setgroups(struct cred *new, const struct cred *old)
  3159. {
  3160. return call_int_hook(task_fix_setgroups, new, old);
  3161. }
  3162. /**
  3163. * security_task_setpgid() - Check if setting the pgid is allowed
  3164. * @p: task being modified
  3165. * @pgid: new pgid
  3166. *
  3167. * Check permission before setting the process group identifier of the process
  3168. * @p to @pgid.
  3169. *
  3170. * Return: Returns 0 if permission is granted.
  3171. */
  3172. int security_task_setpgid(struct task_struct *p, pid_t pgid)
  3173. {
  3174. return call_int_hook(task_setpgid, p, pgid);
  3175. }
  3176. /**
  3177. * security_task_getpgid() - Check if getting the pgid is allowed
  3178. * @p: task
  3179. *
  3180. * Check permission before getting the process group identifier of the process
  3181. * @p.
  3182. *
  3183. * Return: Returns 0 if permission is granted.
  3184. */
  3185. int security_task_getpgid(struct task_struct *p)
  3186. {
  3187. return call_int_hook(task_getpgid, p);
  3188. }
  3189. /**
  3190. * security_task_getsid() - Check if getting the session id is allowed
  3191. * @p: task
  3192. *
  3193. * Check permission before getting the session identifier of the process @p.
  3194. *
  3195. * Return: Returns 0 if permission is granted.
  3196. */
  3197. int security_task_getsid(struct task_struct *p)
  3198. {
  3199. return call_int_hook(task_getsid, p);
  3200. }
  3201. /**
  3202. * security_current_getsecid_subj() - Get the current task's subjective secid
  3203. * @secid: secid value
  3204. *
  3205. * Retrieve the subjective security identifier of the current task and return
  3206. * it in @secid. In case of failure, @secid will be set to zero.
  3207. */
  3208. void security_current_getsecid_subj(u32 *secid)
  3209. {
  3210. *secid = 0;
  3211. call_void_hook(current_getsecid_subj, secid);
  3212. }
  3213. EXPORT_SYMBOL(security_current_getsecid_subj);
  3214. /**
  3215. * security_task_getsecid_obj() - Get a task's objective secid
  3216. * @p: target task
  3217. * @secid: secid value
  3218. *
  3219. * Retrieve the objective security identifier of the task_struct in @p and
  3220. * return it in @secid. In case of failure, @secid will be set to zero.
  3221. */
  3222. void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
  3223. {
  3224. *secid = 0;
  3225. call_void_hook(task_getsecid_obj, p, secid);
  3226. }
  3227. EXPORT_SYMBOL(security_task_getsecid_obj);
  3228. /**
  3229. * security_task_setnice() - Check if setting a task's nice value is allowed
  3230. * @p: target task
  3231. * @nice: nice value
  3232. *
  3233. * Check permission before setting the nice value of @p to @nice.
  3234. *
  3235. * Return: Returns 0 if permission is granted.
  3236. */
  3237. int security_task_setnice(struct task_struct *p, int nice)
  3238. {
  3239. return call_int_hook(task_setnice, p, nice);
  3240. }
  3241. /**
  3242. * security_task_setioprio() - Check if setting a task's ioprio is allowed
  3243. * @p: target task
  3244. * @ioprio: ioprio value
  3245. *
  3246. * Check permission before setting the ioprio value of @p to @ioprio.
  3247. *
  3248. * Return: Returns 0 if permission is granted.
  3249. */
  3250. int security_task_setioprio(struct task_struct *p, int ioprio)
  3251. {
  3252. return call_int_hook(task_setioprio, p, ioprio);
  3253. }
  3254. /**
  3255. * security_task_getioprio() - Check if getting a task's ioprio is allowed
  3256. * @p: task
  3257. *
  3258. * Check permission before getting the ioprio value of @p.
  3259. *
  3260. * Return: Returns 0 if permission is granted.
  3261. */
  3262. int security_task_getioprio(struct task_struct *p)
  3263. {
  3264. return call_int_hook(task_getioprio, p);
  3265. }
  3266. /**
  3267. * security_task_prlimit() - Check if get/setting resources limits is allowed
  3268. * @cred: current task credentials
  3269. * @tcred: target task credentials
  3270. * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
  3271. *
  3272. * Check permission before getting and/or setting the resource limits of
  3273. * another task.
  3274. *
  3275. * Return: Returns 0 if permission is granted.
  3276. */
  3277. int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
  3278. unsigned int flags)
  3279. {
  3280. return call_int_hook(task_prlimit, cred, tcred, flags);
  3281. }
  3282. /**
  3283. * security_task_setrlimit() - Check if setting a new rlimit value is allowed
  3284. * @p: target task's group leader
  3285. * @resource: resource whose limit is being set
  3286. * @new_rlim: new resource limit
  3287. *
  3288. * Check permission before setting the resource limits of process @p for
  3289. * @resource to @new_rlim. The old resource limit values can be examined by
  3290. * dereferencing (p->signal->rlim + resource).
  3291. *
  3292. * Return: Returns 0 if permission is granted.
  3293. */
  3294. int security_task_setrlimit(struct task_struct *p, unsigned int resource,
  3295. struct rlimit *new_rlim)
  3296. {
  3297. return call_int_hook(task_setrlimit, p, resource, new_rlim);
  3298. }
  3299. /**
  3300. * security_task_setscheduler() - Check if setting sched policy/param is allowed
  3301. * @p: target task
  3302. *
  3303. * Check permission before setting scheduling policy and/or parameters of
  3304. * process @p.
  3305. *
  3306. * Return: Returns 0 if permission is granted.
  3307. */
  3308. int security_task_setscheduler(struct task_struct *p)
  3309. {
  3310. return call_int_hook(task_setscheduler, p);
  3311. }
  3312. /**
  3313. * security_task_getscheduler() - Check if getting scheduling info is allowed
  3314. * @p: target task
  3315. *
  3316. * Check permission before obtaining scheduling information for process @p.
  3317. *
  3318. * Return: Returns 0 if permission is granted.
  3319. */
  3320. int security_task_getscheduler(struct task_struct *p)
  3321. {
  3322. return call_int_hook(task_getscheduler, p);
  3323. }
  3324. /**
  3325. * security_task_movememory() - Check if moving memory is allowed
  3326. * @p: task
  3327. *
  3328. * Check permission before moving memory owned by process @p.
  3329. *
  3330. * Return: Returns 0 if permission is granted.
  3331. */
  3332. int security_task_movememory(struct task_struct *p)
  3333. {
  3334. return call_int_hook(task_movememory, p);
  3335. }
  3336. /**
  3337. * security_task_kill() - Check if sending a signal is allowed
  3338. * @p: target process
  3339. * @info: signal information
  3340. * @sig: signal value
  3341. * @cred: credentials of the signal sender, NULL if @current
  3342. *
  3343. * Check permission before sending signal @sig to @p. @info can be NULL, the
  3344. * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
  3345. * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
  3346. * the kernel and should typically be permitted. SIGIO signals are handled
  3347. * separately by the send_sigiotask hook in file_security_ops.
  3348. *
  3349. * Return: Returns 0 if permission is granted.
  3350. */
  3351. int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
  3352. int sig, const struct cred *cred)
  3353. {
  3354. return call_int_hook(task_kill, p, info, sig, cred);
  3355. }
  3356. /**
  3357. * security_task_prctl() - Check if a prctl op is allowed
  3358. * @option: operation
  3359. * @arg2: argument
  3360. * @arg3: argument
  3361. * @arg4: argument
  3362. * @arg5: argument
  3363. *
  3364. * Check permission before performing a process control operation on the
  3365. * current process.
  3366. *
  3367. * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
  3368. * to cause prctl() to return immediately with that value.
  3369. */
  3370. int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  3371. unsigned long arg4, unsigned long arg5)
  3372. {
  3373. int thisrc;
  3374. int rc = LSM_RET_DEFAULT(task_prctl);
  3375. struct lsm_static_call *scall;
  3376. lsm_for_each_hook(scall, task_prctl) {
  3377. thisrc = scall->hl->hook.task_prctl(option, arg2, arg3, arg4, arg5);
  3378. if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
  3379. rc = thisrc;
  3380. if (thisrc != 0)
  3381. break;
  3382. }
  3383. }
  3384. return rc;
  3385. }
  3386. /**
  3387. * security_task_to_inode() - Set the security attributes of a task's inode
  3388. * @p: task
  3389. * @inode: inode
  3390. *
  3391. * Set the security attributes for an inode based on an associated task's
  3392. * security attributes, e.g. for /proc/pid inodes.
  3393. */
  3394. void security_task_to_inode(struct task_struct *p, struct inode *inode)
  3395. {
  3396. call_void_hook(task_to_inode, p, inode);
  3397. }
  3398. /**
  3399. * security_create_user_ns() - Check if creating a new userns is allowed
  3400. * @cred: prepared creds
  3401. *
  3402. * Check permission prior to creating a new user namespace.
  3403. *
  3404. * Return: Returns 0 if successful, otherwise < 0 error code.
  3405. */
  3406. int security_create_user_ns(const struct cred *cred)
  3407. {
  3408. return call_int_hook(userns_create, cred);
  3409. }
  3410. /**
  3411. * security_ipc_permission() - Check if sysv ipc access is allowed
  3412. * @ipcp: ipc permission structure
  3413. * @flag: requested permissions
  3414. *
  3415. * Check permissions for access to IPC.
  3416. *
  3417. * Return: Returns 0 if permission is granted.
  3418. */
  3419. int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
  3420. {
  3421. return call_int_hook(ipc_permission, ipcp, flag);
  3422. }
  3423. /**
  3424. * security_ipc_getsecid() - Get the sysv ipc object's secid
  3425. * @ipcp: ipc permission structure
  3426. * @secid: secid pointer
  3427. *
  3428. * Get the secid associated with the ipc object. In case of failure, @secid
  3429. * will be set to zero.
  3430. */
  3431. void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
  3432. {
  3433. *secid = 0;
  3434. call_void_hook(ipc_getsecid, ipcp, secid);
  3435. }
  3436. /**
  3437. * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
  3438. * @msg: message structure
  3439. *
  3440. * Allocate and attach a security structure to the msg->security field. The
  3441. * security field is initialized to NULL when the structure is first created.
  3442. *
  3443. * Return: Return 0 if operation was successful and permission is granted.
  3444. */
  3445. int security_msg_msg_alloc(struct msg_msg *msg)
  3446. {
  3447. int rc = lsm_msg_msg_alloc(msg);
  3448. if (unlikely(rc))
  3449. return rc;
  3450. rc = call_int_hook(msg_msg_alloc_security, msg);
  3451. if (unlikely(rc))
  3452. security_msg_msg_free(msg);
  3453. return rc;
  3454. }
  3455. /**
  3456. * security_msg_msg_free() - Free a sysv ipc message LSM blob
  3457. * @msg: message structure
  3458. *
  3459. * Deallocate the security structure for this message.
  3460. */
  3461. void security_msg_msg_free(struct msg_msg *msg)
  3462. {
  3463. call_void_hook(msg_msg_free_security, msg);
  3464. kfree(msg->security);
  3465. msg->security = NULL;
  3466. }
  3467. /**
  3468. * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
  3469. * @msq: sysv ipc permission structure
  3470. *
  3471. * Allocate and attach a security structure to @msg. The security field is
  3472. * initialized to NULL when the structure is first created.
  3473. *
  3474. * Return: Returns 0 if operation was successful and permission is granted.
  3475. */
  3476. int security_msg_queue_alloc(struct kern_ipc_perm *msq)
  3477. {
  3478. int rc = lsm_ipc_alloc(msq);
  3479. if (unlikely(rc))
  3480. return rc;
  3481. rc = call_int_hook(msg_queue_alloc_security, msq);
  3482. if (unlikely(rc))
  3483. security_msg_queue_free(msq);
  3484. return rc;
  3485. }
  3486. /**
  3487. * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
  3488. * @msq: sysv ipc permission structure
  3489. *
  3490. * Deallocate security field @perm->security for the message queue.
  3491. */
  3492. void security_msg_queue_free(struct kern_ipc_perm *msq)
  3493. {
  3494. call_void_hook(msg_queue_free_security, msq);
  3495. kfree(msq->security);
  3496. msq->security = NULL;
  3497. }
  3498. /**
  3499. * security_msg_queue_associate() - Check if a msg queue operation is allowed
  3500. * @msq: sysv ipc permission structure
  3501. * @msqflg: operation flags
  3502. *
  3503. * Check permission when a message queue is requested through the msgget system
  3504. * call. This hook is only called when returning the message queue identifier
  3505. * for an existing message queue, not when a new message queue is created.
  3506. *
  3507. * Return: Return 0 if permission is granted.
  3508. */
  3509. int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
  3510. {
  3511. return call_int_hook(msg_queue_associate, msq, msqflg);
  3512. }
  3513. /**
  3514. * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
  3515. * @msq: sysv ipc permission structure
  3516. * @cmd: operation
  3517. *
  3518. * Check permission when a message control operation specified by @cmd is to be
  3519. * performed on the message queue with permissions.
  3520. *
  3521. * Return: Returns 0 if permission is granted.
  3522. */
  3523. int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
  3524. {
  3525. return call_int_hook(msg_queue_msgctl, msq, cmd);
  3526. }
  3527. /**
  3528. * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
  3529. * @msq: sysv ipc permission structure
  3530. * @msg: message
  3531. * @msqflg: operation flags
  3532. *
  3533. * Check permission before a message, @msg, is enqueued on the message queue
  3534. * with permissions specified in @msq.
  3535. *
  3536. * Return: Returns 0 if permission is granted.
  3537. */
  3538. int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
  3539. struct msg_msg *msg, int msqflg)
  3540. {
  3541. return call_int_hook(msg_queue_msgsnd, msq, msg, msqflg);
  3542. }
  3543. /**
  3544. * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
  3545. * @msq: sysv ipc permission structure
  3546. * @msg: message
  3547. * @target: target task
  3548. * @type: type of message requested
  3549. * @mode: operation flags
  3550. *
  3551. * Check permission before a message, @msg, is removed from the message queue.
  3552. * The @target task structure contains a pointer to the process that will be
  3553. * receiving the message (not equal to the current process when inline receives
  3554. * are being performed).
  3555. *
  3556. * Return: Returns 0 if permission is granted.
  3557. */
  3558. int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
  3559. struct task_struct *target, long type, int mode)
  3560. {
  3561. return call_int_hook(msg_queue_msgrcv, msq, msg, target, type, mode);
  3562. }
  3563. /**
  3564. * security_shm_alloc() - Allocate a sysv shm LSM blob
  3565. * @shp: sysv ipc permission structure
  3566. *
  3567. * Allocate and attach a security structure to the @shp security field. The
  3568. * security field is initialized to NULL when the structure is first created.
  3569. *
  3570. * Return: Returns 0 if operation was successful and permission is granted.
  3571. */
  3572. int security_shm_alloc(struct kern_ipc_perm *shp)
  3573. {
  3574. int rc = lsm_ipc_alloc(shp);
  3575. if (unlikely(rc))
  3576. return rc;
  3577. rc = call_int_hook(shm_alloc_security, shp);
  3578. if (unlikely(rc))
  3579. security_shm_free(shp);
  3580. return rc;
  3581. }
  3582. /**
  3583. * security_shm_free() - Free a sysv shm LSM blob
  3584. * @shp: sysv ipc permission structure
  3585. *
  3586. * Deallocate the security structure @perm->security for the memory segment.
  3587. */
  3588. void security_shm_free(struct kern_ipc_perm *shp)
  3589. {
  3590. call_void_hook(shm_free_security, shp);
  3591. kfree(shp->security);
  3592. shp->security = NULL;
  3593. }
  3594. /**
  3595. * security_shm_associate() - Check if a sysv shm operation is allowed
  3596. * @shp: sysv ipc permission structure
  3597. * @shmflg: operation flags
  3598. *
  3599. * Check permission when a shared memory region is requested through the shmget
  3600. * system call. This hook is only called when returning the shared memory
  3601. * region identifier for an existing region, not when a new shared memory
  3602. * region is created.
  3603. *
  3604. * Return: Returns 0 if permission is granted.
  3605. */
  3606. int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
  3607. {
  3608. return call_int_hook(shm_associate, shp, shmflg);
  3609. }
  3610. /**
  3611. * security_shm_shmctl() - Check if a sysv shm operation is allowed
  3612. * @shp: sysv ipc permission structure
  3613. * @cmd: operation
  3614. *
  3615. * Check permission when a shared memory control operation specified by @cmd is
  3616. * to be performed on the shared memory region with permissions in @shp.
  3617. *
  3618. * Return: Return 0 if permission is granted.
  3619. */
  3620. int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
  3621. {
  3622. return call_int_hook(shm_shmctl, shp, cmd);
  3623. }
  3624. /**
  3625. * security_shm_shmat() - Check if a sysv shm attach operation is allowed
  3626. * @shp: sysv ipc permission structure
  3627. * @shmaddr: address of memory region to attach
  3628. * @shmflg: operation flags
  3629. *
  3630. * Check permissions prior to allowing the shmat system call to attach the
  3631. * shared memory segment with permissions @shp to the data segment of the
  3632. * calling process. The attaching address is specified by @shmaddr.
  3633. *
  3634. * Return: Returns 0 if permission is granted.
  3635. */
  3636. int security_shm_shmat(struct kern_ipc_perm *shp,
  3637. char __user *shmaddr, int shmflg)
  3638. {
  3639. return call_int_hook(shm_shmat, shp, shmaddr, shmflg);
  3640. }
  3641. /**
  3642. * security_sem_alloc() - Allocate a sysv semaphore LSM blob
  3643. * @sma: sysv ipc permission structure
  3644. *
  3645. * Allocate and attach a security structure to the @sma security field. The
  3646. * security field is initialized to NULL when the structure is first created.
  3647. *
  3648. * Return: Returns 0 if operation was successful and permission is granted.
  3649. */
  3650. int security_sem_alloc(struct kern_ipc_perm *sma)
  3651. {
  3652. int rc = lsm_ipc_alloc(sma);
  3653. if (unlikely(rc))
  3654. return rc;
  3655. rc = call_int_hook(sem_alloc_security, sma);
  3656. if (unlikely(rc))
  3657. security_sem_free(sma);
  3658. return rc;
  3659. }
  3660. /**
  3661. * security_sem_free() - Free a sysv semaphore LSM blob
  3662. * @sma: sysv ipc permission structure
  3663. *
  3664. * Deallocate security structure @sma->security for the semaphore.
  3665. */
  3666. void security_sem_free(struct kern_ipc_perm *sma)
  3667. {
  3668. call_void_hook(sem_free_security, sma);
  3669. kfree(sma->security);
  3670. sma->security = NULL;
  3671. }
  3672. /**
  3673. * security_sem_associate() - Check if a sysv semaphore operation is allowed
  3674. * @sma: sysv ipc permission structure
  3675. * @semflg: operation flags
  3676. *
  3677. * Check permission when a semaphore is requested through the semget system
  3678. * call. This hook is only called when returning the semaphore identifier for
  3679. * an existing semaphore, not when a new one must be created.
  3680. *
  3681. * Return: Returns 0 if permission is granted.
  3682. */
  3683. int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
  3684. {
  3685. return call_int_hook(sem_associate, sma, semflg);
  3686. }
  3687. /**
  3688. * security_sem_semctl() - Check if a sysv semaphore operation is allowed
  3689. * @sma: sysv ipc permission structure
  3690. * @cmd: operation
  3691. *
  3692. * Check permission when a semaphore operation specified by @cmd is to be
  3693. * performed on the semaphore.
  3694. *
  3695. * Return: Returns 0 if permission is granted.
  3696. */
  3697. int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
  3698. {
  3699. return call_int_hook(sem_semctl, sma, cmd);
  3700. }
  3701. /**
  3702. * security_sem_semop() - Check if a sysv semaphore operation is allowed
  3703. * @sma: sysv ipc permission structure
  3704. * @sops: operations to perform
  3705. * @nsops: number of operations
  3706. * @alter: flag indicating changes will be made
  3707. *
  3708. * Check permissions before performing operations on members of the semaphore
  3709. * set. If the @alter flag is nonzero, the semaphore set may be modified.
  3710. *
  3711. * Return: Returns 0 if permission is granted.
  3712. */
  3713. int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
  3714. unsigned nsops, int alter)
  3715. {
  3716. return call_int_hook(sem_semop, sma, sops, nsops, alter);
  3717. }
  3718. /**
  3719. * security_d_instantiate() - Populate an inode's LSM state based on a dentry
  3720. * @dentry: dentry
  3721. * @inode: inode
  3722. *
  3723. * Fill in @inode security information for a @dentry if allowed.
  3724. */
  3725. void security_d_instantiate(struct dentry *dentry, struct inode *inode)
  3726. {
  3727. if (unlikely(inode && IS_PRIVATE(inode)))
  3728. return;
  3729. call_void_hook(d_instantiate, dentry, inode);
  3730. }
  3731. EXPORT_SYMBOL(security_d_instantiate);
  3732. /*
  3733. * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
  3734. */
  3735. /**
  3736. * security_getselfattr - Read an LSM attribute of the current process.
  3737. * @attr: which attribute to return
  3738. * @uctx: the user-space destination for the information, or NULL
  3739. * @size: pointer to the size of space available to receive the data
  3740. * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
  3741. * attributes associated with the LSM identified in the passed @ctx be
  3742. * reported.
  3743. *
  3744. * A NULL value for @uctx can be used to get both the number of attributes
  3745. * and the size of the data.
  3746. *
  3747. * Returns the number of attributes found on success, negative value
  3748. * on error. @size is reset to the total size of the data.
  3749. * If @size is insufficient to contain the data -E2BIG is returned.
  3750. */
  3751. int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
  3752. u32 __user *size, u32 flags)
  3753. {
  3754. struct lsm_static_call *scall;
  3755. struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, };
  3756. u8 __user *base = (u8 __user *)uctx;
  3757. u32 entrysize;
  3758. u32 total = 0;
  3759. u32 left;
  3760. bool toobig = false;
  3761. bool single = false;
  3762. int count = 0;
  3763. int rc;
  3764. if (attr == LSM_ATTR_UNDEF)
  3765. return -EINVAL;
  3766. if (size == NULL)
  3767. return -EINVAL;
  3768. if (get_user(left, size))
  3769. return -EFAULT;
  3770. if (flags) {
  3771. /*
  3772. * Only flag supported is LSM_FLAG_SINGLE
  3773. */
  3774. if (flags != LSM_FLAG_SINGLE || !uctx)
  3775. return -EINVAL;
  3776. if (copy_from_user(&lctx, uctx, sizeof(lctx)))
  3777. return -EFAULT;
  3778. /*
  3779. * If the LSM ID isn't specified it is an error.
  3780. */
  3781. if (lctx.id == LSM_ID_UNDEF)
  3782. return -EINVAL;
  3783. single = true;
  3784. }
  3785. /*
  3786. * In the usual case gather all the data from the LSMs.
  3787. * In the single case only get the data from the LSM specified.
  3788. */
  3789. lsm_for_each_hook(scall, getselfattr) {
  3790. if (single && lctx.id != scall->hl->lsmid->id)
  3791. continue;
  3792. entrysize = left;
  3793. if (base)
  3794. uctx = (struct lsm_ctx __user *)(base + total);
  3795. rc = scall->hl->hook.getselfattr(attr, uctx, &entrysize, flags);
  3796. if (rc == -EOPNOTSUPP) {
  3797. rc = 0;
  3798. continue;
  3799. }
  3800. if (rc == -E2BIG) {
  3801. rc = 0;
  3802. left = 0;
  3803. toobig = true;
  3804. } else if (rc < 0)
  3805. return rc;
  3806. else
  3807. left -= entrysize;
  3808. total += entrysize;
  3809. count += rc;
  3810. if (single)
  3811. break;
  3812. }
  3813. if (put_user(total, size))
  3814. return -EFAULT;
  3815. if (toobig)
  3816. return -E2BIG;
  3817. if (count == 0)
  3818. return LSM_RET_DEFAULT(getselfattr);
  3819. return count;
  3820. }
  3821. /*
  3822. * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
  3823. */
  3824. /**
  3825. * security_setselfattr - Set an LSM attribute on the current process.
  3826. * @attr: which attribute to set
  3827. * @uctx: the user-space source for the information
  3828. * @size: the size of the data
  3829. * @flags: reserved for future use, must be 0
  3830. *
  3831. * Set an LSM attribute for the current process. The LSM, attribute
  3832. * and new value are included in @uctx.
  3833. *
  3834. * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT
  3835. * if the user buffer is inaccessible, E2BIG if size is too big, or an
  3836. * LSM specific failure.
  3837. */
  3838. int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
  3839. u32 size, u32 flags)
  3840. {
  3841. struct lsm_static_call *scall;
  3842. struct lsm_ctx *lctx;
  3843. int rc = LSM_RET_DEFAULT(setselfattr);
  3844. u64 required_len;
  3845. if (flags)
  3846. return -EINVAL;
  3847. if (size < sizeof(*lctx))
  3848. return -EINVAL;
  3849. if (size > PAGE_SIZE)
  3850. return -E2BIG;
  3851. lctx = memdup_user(uctx, size);
  3852. if (IS_ERR(lctx))
  3853. return PTR_ERR(lctx);
  3854. if (size < lctx->len ||
  3855. check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) ||
  3856. lctx->len < required_len) {
  3857. rc = -EINVAL;
  3858. goto free_out;
  3859. }
  3860. lsm_for_each_hook(scall, setselfattr)
  3861. if ((scall->hl->lsmid->id) == lctx->id) {
  3862. rc = scall->hl->hook.setselfattr(attr, lctx, size, flags);
  3863. break;
  3864. }
  3865. free_out:
  3866. kfree(lctx);
  3867. return rc;
  3868. }
  3869. /**
  3870. * security_getprocattr() - Read an attribute for a task
  3871. * @p: the task
  3872. * @lsmid: LSM identification
  3873. * @name: attribute name
  3874. * @value: attribute value
  3875. *
  3876. * Read attribute @name for task @p and store it into @value if allowed.
  3877. *
  3878. * Return: Returns the length of @value on success, a negative value otherwise.
  3879. */
  3880. int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
  3881. char **value)
  3882. {
  3883. struct lsm_static_call *scall;
  3884. lsm_for_each_hook(scall, getprocattr) {
  3885. if (lsmid != 0 && lsmid != scall->hl->lsmid->id)
  3886. continue;
  3887. return scall->hl->hook.getprocattr(p, name, value);
  3888. }
  3889. return LSM_RET_DEFAULT(getprocattr);
  3890. }
  3891. /**
  3892. * security_setprocattr() - Set an attribute for a task
  3893. * @lsmid: LSM identification
  3894. * @name: attribute name
  3895. * @value: attribute value
  3896. * @size: attribute value size
  3897. *
  3898. * Write (set) the current task's attribute @name to @value, size @size if
  3899. * allowed.
  3900. *
  3901. * Return: Returns bytes written on success, a negative value otherwise.
  3902. */
  3903. int security_setprocattr(int lsmid, const char *name, void *value, size_t size)
  3904. {
  3905. struct lsm_static_call *scall;
  3906. lsm_for_each_hook(scall, setprocattr) {
  3907. if (lsmid != 0 && lsmid != scall->hl->lsmid->id)
  3908. continue;
  3909. return scall->hl->hook.setprocattr(name, value, size);
  3910. }
  3911. return LSM_RET_DEFAULT(setprocattr);
  3912. }
  3913. /**
  3914. * security_netlink_send() - Save info and check if netlink sending is allowed
  3915. * @sk: sending socket
  3916. * @skb: netlink message
  3917. *
  3918. * Save security information for a netlink message so that permission checking
  3919. * can be performed when the message is processed. The security information
  3920. * can be saved using the eff_cap field of the netlink_skb_parms structure.
  3921. * Also may be used to provide fine grained control over message transmission.
  3922. *
  3923. * Return: Returns 0 if the information was successfully saved and message is
  3924. * allowed to be transmitted.
  3925. */
  3926. int security_netlink_send(struct sock *sk, struct sk_buff *skb)
  3927. {
  3928. return call_int_hook(netlink_send, sk, skb);
  3929. }
  3930. /**
  3931. * security_ismaclabel() - Check if the named attribute is a MAC label
  3932. * @name: full extended attribute name
  3933. *
  3934. * Check if the extended attribute specified by @name represents a MAC label.
  3935. *
  3936. * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
  3937. */
  3938. int security_ismaclabel(const char *name)
  3939. {
  3940. return call_int_hook(ismaclabel, name);
  3941. }
  3942. EXPORT_SYMBOL(security_ismaclabel);
  3943. /**
  3944. * security_secid_to_secctx() - Convert a secid to a secctx
  3945. * @secid: secid
  3946. * @secdata: secctx
  3947. * @seclen: secctx length
  3948. *
  3949. * Convert secid to security context. If @secdata is NULL the length of the
  3950. * result will be returned in @seclen, but no @secdata will be returned. This
  3951. * does mean that the length could change between calls to check the length and
  3952. * the next call which actually allocates and returns the @secdata.
  3953. *
  3954. * Return: Return 0 on success, error on failure.
  3955. */
  3956. int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  3957. {
  3958. return call_int_hook(secid_to_secctx, secid, secdata, seclen);
  3959. }
  3960. EXPORT_SYMBOL(security_secid_to_secctx);
  3961. /**
  3962. * security_secctx_to_secid() - Convert a secctx to a secid
  3963. * @secdata: secctx
  3964. * @seclen: length of secctx
  3965. * @secid: secid
  3966. *
  3967. * Convert security context to secid.
  3968. *
  3969. * Return: Returns 0 on success, error on failure.
  3970. */
  3971. int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  3972. {
  3973. *secid = 0;
  3974. return call_int_hook(secctx_to_secid, secdata, seclen, secid);
  3975. }
  3976. EXPORT_SYMBOL(security_secctx_to_secid);
  3977. /**
  3978. * security_release_secctx() - Free a secctx buffer
  3979. * @secdata: secctx
  3980. * @seclen: length of secctx
  3981. *
  3982. * Release the security context.
  3983. */
  3984. void security_release_secctx(char *secdata, u32 seclen)
  3985. {
  3986. call_void_hook(release_secctx, secdata, seclen);
  3987. }
  3988. EXPORT_SYMBOL(security_release_secctx);
  3989. /**
  3990. * security_inode_invalidate_secctx() - Invalidate an inode's security label
  3991. * @inode: inode
  3992. *
  3993. * Notify the security module that it must revalidate the security context of
  3994. * an inode.
  3995. */
  3996. void security_inode_invalidate_secctx(struct inode *inode)
  3997. {
  3998. call_void_hook(inode_invalidate_secctx, inode);
  3999. }
  4000. EXPORT_SYMBOL(security_inode_invalidate_secctx);
  4001. /**
  4002. * security_inode_notifysecctx() - Notify the LSM of an inode's security label
  4003. * @inode: inode
  4004. * @ctx: secctx
  4005. * @ctxlen: length of secctx
  4006. *
  4007. * Notify the security module of what the security context of an inode should
  4008. * be. Initializes the incore security context managed by the security module
  4009. * for this inode. Example usage: NFS client invokes this hook to initialize
  4010. * the security context in its incore inode to the value provided by the server
  4011. * for the file when the server returned the file's attributes to the client.
  4012. * Must be called with inode->i_mutex locked.
  4013. *
  4014. * Return: Returns 0 on success, error on failure.
  4015. */
  4016. int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  4017. {
  4018. return call_int_hook(inode_notifysecctx, inode, ctx, ctxlen);
  4019. }
  4020. EXPORT_SYMBOL(security_inode_notifysecctx);
  4021. /**
  4022. * security_inode_setsecctx() - Change the security label of an inode
  4023. * @dentry: inode
  4024. * @ctx: secctx
  4025. * @ctxlen: length of secctx
  4026. *
  4027. * Change the security context of an inode. Updates the incore security
  4028. * context managed by the security module and invokes the fs code as needed
  4029. * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
  4030. * context. Example usage: NFS server invokes this hook to change the security
  4031. * context in its incore inode and on the backing filesystem to a value
  4032. * provided by the client on a SETATTR operation. Must be called with
  4033. * inode->i_mutex locked.
  4034. *
  4035. * Return: Returns 0 on success, error on failure.
  4036. */
  4037. int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  4038. {
  4039. return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen);
  4040. }
  4041. EXPORT_SYMBOL(security_inode_setsecctx);
  4042. /**
  4043. * security_inode_getsecctx() - Get the security label of an inode
  4044. * @inode: inode
  4045. * @ctx: secctx
  4046. * @ctxlen: length of secctx
  4047. *
  4048. * On success, returns 0 and fills out @ctx and @ctxlen with the security
  4049. * context for the given @inode.
  4050. *
  4051. * Return: Returns 0 on success, error on failure.
  4052. */
  4053. int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  4054. {
  4055. return call_int_hook(inode_getsecctx, inode, ctx, ctxlen);
  4056. }
  4057. EXPORT_SYMBOL(security_inode_getsecctx);
  4058. #ifdef CONFIG_WATCH_QUEUE
  4059. /**
  4060. * security_post_notification() - Check if a watch notification can be posted
  4061. * @w_cred: credentials of the task that set the watch
  4062. * @cred: credentials of the task which triggered the watch
  4063. * @n: the notification
  4064. *
  4065. * Check to see if a watch notification can be posted to a particular queue.
  4066. *
  4067. * Return: Returns 0 if permission is granted.
  4068. */
  4069. int security_post_notification(const struct cred *w_cred,
  4070. const struct cred *cred,
  4071. struct watch_notification *n)
  4072. {
  4073. return call_int_hook(post_notification, w_cred, cred, n);
  4074. }
  4075. #endif /* CONFIG_WATCH_QUEUE */
  4076. #ifdef CONFIG_KEY_NOTIFICATIONS
  4077. /**
  4078. * security_watch_key() - Check if a task is allowed to watch for key events
  4079. * @key: the key to watch
  4080. *
  4081. * Check to see if a process is allowed to watch for event notifications from
  4082. * a key or keyring.
  4083. *
  4084. * Return: Returns 0 if permission is granted.
  4085. */
  4086. int security_watch_key(struct key *key)
  4087. {
  4088. return call_int_hook(watch_key, key);
  4089. }
  4090. #endif /* CONFIG_KEY_NOTIFICATIONS */
  4091. #ifdef CONFIG_SECURITY_NETWORK
  4092. /**
  4093. * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
  4094. * @sock: originating sock
  4095. * @other: peer sock
  4096. * @newsk: new sock
  4097. *
  4098. * Check permissions before establishing a Unix domain stream connection
  4099. * between @sock and @other.
  4100. *
  4101. * The @unix_stream_connect and @unix_may_send hooks were necessary because
  4102. * Linux provides an alternative to the conventional file name space for Unix
  4103. * domain sockets. Whereas binding and connecting to sockets in the file name
  4104. * space is mediated by the typical file permissions (and caught by the mknod
  4105. * and permission hooks in inode_security_ops), binding and connecting to
  4106. * sockets in the abstract name space is completely unmediated. Sufficient
  4107. * control of Unix domain sockets in the abstract name space isn't possible
  4108. * using only the socket layer hooks, since we need to know the actual target
  4109. * socket, which is not looked up until we are inside the af_unix code.
  4110. *
  4111. * Return: Returns 0 if permission is granted.
  4112. */
  4113. int security_unix_stream_connect(struct sock *sock, struct sock *other,
  4114. struct sock *newsk)
  4115. {
  4116. return call_int_hook(unix_stream_connect, sock, other, newsk);
  4117. }
  4118. EXPORT_SYMBOL(security_unix_stream_connect);
  4119. /**
  4120. * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
  4121. * @sock: originating sock
  4122. * @other: peer sock
  4123. *
  4124. * Check permissions before connecting or sending datagrams from @sock to
  4125. * @other.
  4126. *
  4127. * The @unix_stream_connect and @unix_may_send hooks were necessary because
  4128. * Linux provides an alternative to the conventional file name space for Unix
  4129. * domain sockets. Whereas binding and connecting to sockets in the file name
  4130. * space is mediated by the typical file permissions (and caught by the mknod
  4131. * and permission hooks in inode_security_ops), binding and connecting to
  4132. * sockets in the abstract name space is completely unmediated. Sufficient
  4133. * control of Unix domain sockets in the abstract name space isn't possible
  4134. * using only the socket layer hooks, since we need to know the actual target
  4135. * socket, which is not looked up until we are inside the af_unix code.
  4136. *
  4137. * Return: Returns 0 if permission is granted.
  4138. */
  4139. int security_unix_may_send(struct socket *sock, struct socket *other)
  4140. {
  4141. return call_int_hook(unix_may_send, sock, other);
  4142. }
  4143. EXPORT_SYMBOL(security_unix_may_send);
  4144. /**
  4145. * security_socket_create() - Check if creating a new socket is allowed
  4146. * @family: protocol family
  4147. * @type: communications type
  4148. * @protocol: requested protocol
  4149. * @kern: set to 1 if a kernel socket is requested
  4150. *
  4151. * Check permissions prior to creating a new socket.
  4152. *
  4153. * Return: Returns 0 if permission is granted.
  4154. */
  4155. int security_socket_create(int family, int type, int protocol, int kern)
  4156. {
  4157. return call_int_hook(socket_create, family, type, protocol, kern);
  4158. }
  4159. /**
  4160. * security_socket_post_create() - Initialize a newly created socket
  4161. * @sock: socket
  4162. * @family: protocol family
  4163. * @type: communications type
  4164. * @protocol: requested protocol
  4165. * @kern: set to 1 if a kernel socket is requested
  4166. *
  4167. * This hook allows a module to update or allocate a per-socket security
  4168. * structure. Note that the security field was not added directly to the socket
  4169. * structure, but rather, the socket security information is stored in the
  4170. * associated inode. Typically, the inode alloc_security hook will allocate
  4171. * and attach security information to SOCK_INODE(sock)->i_security. This hook
  4172. * may be used to update the SOCK_INODE(sock)->i_security field with additional
  4173. * information that wasn't available when the inode was allocated.
  4174. *
  4175. * Return: Returns 0 if permission is granted.
  4176. */
  4177. int security_socket_post_create(struct socket *sock, int family,
  4178. int type, int protocol, int kern)
  4179. {
  4180. return call_int_hook(socket_post_create, sock, family, type,
  4181. protocol, kern);
  4182. }
  4183. /**
  4184. * security_socket_socketpair() - Check if creating a socketpair is allowed
  4185. * @socka: first socket
  4186. * @sockb: second socket
  4187. *
  4188. * Check permissions before creating a fresh pair of sockets.
  4189. *
  4190. * Return: Returns 0 if permission is granted and the connection was
  4191. * established.
  4192. */
  4193. int security_socket_socketpair(struct socket *socka, struct socket *sockb)
  4194. {
  4195. return call_int_hook(socket_socketpair, socka, sockb);
  4196. }
  4197. EXPORT_SYMBOL(security_socket_socketpair);
  4198. /**
  4199. * security_socket_bind() - Check if a socket bind operation is allowed
  4200. * @sock: socket
  4201. * @address: requested bind address
  4202. * @addrlen: length of address
  4203. *
  4204. * Check permission before socket protocol layer bind operation is performed
  4205. * and the socket @sock is bound to the address specified in the @address
  4206. * parameter.
  4207. *
  4208. * Return: Returns 0 if permission is granted.
  4209. */
  4210. int security_socket_bind(struct socket *sock,
  4211. struct sockaddr *address, int addrlen)
  4212. {
  4213. return call_int_hook(socket_bind, sock, address, addrlen);
  4214. }
  4215. /**
  4216. * security_socket_connect() - Check if a socket connect operation is allowed
  4217. * @sock: socket
  4218. * @address: address of remote connection point
  4219. * @addrlen: length of address
  4220. *
  4221. * Check permission before socket protocol layer connect operation attempts to
  4222. * connect socket @sock to a remote address, @address.
  4223. *
  4224. * Return: Returns 0 if permission is granted.
  4225. */
  4226. int security_socket_connect(struct socket *sock,
  4227. struct sockaddr *address, int addrlen)
  4228. {
  4229. return call_int_hook(socket_connect, sock, address, addrlen);
  4230. }
  4231. /**
  4232. * security_socket_listen() - Check if a socket is allowed to listen
  4233. * @sock: socket
  4234. * @backlog: connection queue size
  4235. *
  4236. * Check permission before socket protocol layer listen operation.
  4237. *
  4238. * Return: Returns 0 if permission is granted.
  4239. */
  4240. int security_socket_listen(struct socket *sock, int backlog)
  4241. {
  4242. return call_int_hook(socket_listen, sock, backlog);
  4243. }
  4244. /**
  4245. * security_socket_accept() - Check if a socket is allowed to accept connections
  4246. * @sock: listening socket
  4247. * @newsock: newly creation connection socket
  4248. *
  4249. * Check permission before accepting a new connection. Note that the new
  4250. * socket, @newsock, has been created and some information copied to it, but
  4251. * the accept operation has not actually been performed.
  4252. *
  4253. * Return: Returns 0 if permission is granted.
  4254. */
  4255. int security_socket_accept(struct socket *sock, struct socket *newsock)
  4256. {
  4257. return call_int_hook(socket_accept, sock, newsock);
  4258. }
  4259. /**
  4260. * security_socket_sendmsg() - Check if sending a message is allowed
  4261. * @sock: sending socket
  4262. * @msg: message to send
  4263. * @size: size of message
  4264. *
  4265. * Check permission before transmitting a message to another socket.
  4266. *
  4267. * Return: Returns 0 if permission is granted.
  4268. */
  4269. int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
  4270. {
  4271. return call_int_hook(socket_sendmsg, sock, msg, size);
  4272. }
  4273. /**
  4274. * security_socket_recvmsg() - Check if receiving a message is allowed
  4275. * @sock: receiving socket
  4276. * @msg: message to receive
  4277. * @size: size of message
  4278. * @flags: operational flags
  4279. *
  4280. * Check permission before receiving a message from a socket.
  4281. *
  4282. * Return: Returns 0 if permission is granted.
  4283. */
  4284. int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
  4285. int size, int flags)
  4286. {
  4287. return call_int_hook(socket_recvmsg, sock, msg, size, flags);
  4288. }
  4289. /**
  4290. * security_socket_getsockname() - Check if reading the socket addr is allowed
  4291. * @sock: socket
  4292. *
  4293. * Check permission before reading the local address (name) of the socket
  4294. * object.
  4295. *
  4296. * Return: Returns 0 if permission is granted.
  4297. */
  4298. int security_socket_getsockname(struct socket *sock)
  4299. {
  4300. return call_int_hook(socket_getsockname, sock);
  4301. }
  4302. /**
  4303. * security_socket_getpeername() - Check if reading the peer's addr is allowed
  4304. * @sock: socket
  4305. *
  4306. * Check permission before the remote address (name) of a socket object.
  4307. *
  4308. * Return: Returns 0 if permission is granted.
  4309. */
  4310. int security_socket_getpeername(struct socket *sock)
  4311. {
  4312. return call_int_hook(socket_getpeername, sock);
  4313. }
  4314. /**
  4315. * security_socket_getsockopt() - Check if reading a socket option is allowed
  4316. * @sock: socket
  4317. * @level: option's protocol level
  4318. * @optname: option name
  4319. *
  4320. * Check permissions before retrieving the options associated with socket
  4321. * @sock.
  4322. *
  4323. * Return: Returns 0 if permission is granted.
  4324. */
  4325. int security_socket_getsockopt(struct socket *sock, int level, int optname)
  4326. {
  4327. return call_int_hook(socket_getsockopt, sock, level, optname);
  4328. }
  4329. /**
  4330. * security_socket_setsockopt() - Check if setting a socket option is allowed
  4331. * @sock: socket
  4332. * @level: option's protocol level
  4333. * @optname: option name
  4334. *
  4335. * Check permissions before setting the options associated with socket @sock.
  4336. *
  4337. * Return: Returns 0 if permission is granted.
  4338. */
  4339. int security_socket_setsockopt(struct socket *sock, int level, int optname)
  4340. {
  4341. return call_int_hook(socket_setsockopt, sock, level, optname);
  4342. }
  4343. /**
  4344. * security_socket_shutdown() - Checks if shutting down the socket is allowed
  4345. * @sock: socket
  4346. * @how: flag indicating how sends and receives are handled
  4347. *
  4348. * Checks permission before all or part of a connection on the socket @sock is
  4349. * shut down.
  4350. *
  4351. * Return: Returns 0 if permission is granted.
  4352. */
  4353. int security_socket_shutdown(struct socket *sock, int how)
  4354. {
  4355. return call_int_hook(socket_shutdown, sock, how);
  4356. }
  4357. /**
  4358. * security_sock_rcv_skb() - Check if an incoming network packet is allowed
  4359. * @sk: destination sock
  4360. * @skb: incoming packet
  4361. *
  4362. * Check permissions on incoming network packets. This hook is distinct from
  4363. * Netfilter's IP input hooks since it is the first time that the incoming
  4364. * sk_buff @skb has been associated with a particular socket, @sk. Must not
  4365. * sleep inside this hook because some callers hold spinlocks.
  4366. *
  4367. * Return: Returns 0 if permission is granted.
  4368. */
  4369. int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  4370. {
  4371. return call_int_hook(socket_sock_rcv_skb, sk, skb);
  4372. }
  4373. EXPORT_SYMBOL(security_sock_rcv_skb);
  4374. /**
  4375. * security_socket_getpeersec_stream() - Get the remote peer label
  4376. * @sock: socket
  4377. * @optval: destination buffer
  4378. * @optlen: size of peer label copied into the buffer
  4379. * @len: maximum size of the destination buffer
  4380. *
  4381. * This hook allows the security module to provide peer socket security state
  4382. * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
  4383. * For tcp sockets this can be meaningful if the socket is associated with an
  4384. * ipsec SA.
  4385. *
  4386. * Return: Returns 0 if all is well, otherwise, typical getsockopt return
  4387. * values.
  4388. */
  4389. int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
  4390. sockptr_t optlen, unsigned int len)
  4391. {
  4392. return call_int_hook(socket_getpeersec_stream, sock, optval, optlen,
  4393. len);
  4394. }
  4395. /**
  4396. * security_socket_getpeersec_dgram() - Get the remote peer label
  4397. * @sock: socket
  4398. * @skb: datagram packet
  4399. * @secid: remote peer label secid
  4400. *
  4401. * This hook allows the security module to provide peer socket security state
  4402. * for udp sockets on a per-packet basis to userspace via getsockopt
  4403. * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
  4404. * option via getsockopt. It can then retrieve the security state returned by
  4405. * this hook for a packet via the SCM_SECURITY ancillary message type.
  4406. *
  4407. * Return: Returns 0 on success, error on failure.
  4408. */
  4409. int security_socket_getpeersec_dgram(struct socket *sock,
  4410. struct sk_buff *skb, u32 *secid)
  4411. {
  4412. return call_int_hook(socket_getpeersec_dgram, sock, skb, secid);
  4413. }
  4414. EXPORT_SYMBOL(security_socket_getpeersec_dgram);
  4415. /**
  4416. * lsm_sock_alloc - allocate a composite sock blob
  4417. * @sock: the sock that needs a blob
  4418. * @gfp: allocation mode
  4419. *
  4420. * Allocate the sock blob for all the modules
  4421. *
  4422. * Returns 0, or -ENOMEM if memory can't be allocated.
  4423. */
  4424. static int lsm_sock_alloc(struct sock *sock, gfp_t gfp)
  4425. {
  4426. return lsm_blob_alloc(&sock->sk_security, blob_sizes.lbs_sock, gfp);
  4427. }
  4428. /**
  4429. * security_sk_alloc() - Allocate and initialize a sock's LSM blob
  4430. * @sk: sock
  4431. * @family: protocol family
  4432. * @priority: gfp flags
  4433. *
  4434. * Allocate and attach a security structure to the sk->sk_security field, which
  4435. * is used to copy security attributes between local stream sockets.
  4436. *
  4437. * Return: Returns 0 on success, error on failure.
  4438. */
  4439. int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
  4440. {
  4441. int rc = lsm_sock_alloc(sk, priority);
  4442. if (unlikely(rc))
  4443. return rc;
  4444. rc = call_int_hook(sk_alloc_security, sk, family, priority);
  4445. if (unlikely(rc))
  4446. security_sk_free(sk);
  4447. return rc;
  4448. }
  4449. /**
  4450. * security_sk_free() - Free the sock's LSM blob
  4451. * @sk: sock
  4452. *
  4453. * Deallocate security structure.
  4454. */
  4455. void security_sk_free(struct sock *sk)
  4456. {
  4457. call_void_hook(sk_free_security, sk);
  4458. kfree(sk->sk_security);
  4459. sk->sk_security = NULL;
  4460. }
  4461. /**
  4462. * security_sk_clone() - Clone a sock's LSM state
  4463. * @sk: original sock
  4464. * @newsk: target sock
  4465. *
  4466. * Clone/copy security structure.
  4467. */
  4468. void security_sk_clone(const struct sock *sk, struct sock *newsk)
  4469. {
  4470. call_void_hook(sk_clone_security, sk, newsk);
  4471. }
  4472. EXPORT_SYMBOL(security_sk_clone);
  4473. /**
  4474. * security_sk_classify_flow() - Set a flow's secid based on socket
  4475. * @sk: original socket
  4476. * @flic: target flow
  4477. *
  4478. * Set the target flow's secid to socket's secid.
  4479. */
  4480. void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
  4481. {
  4482. call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
  4483. }
  4484. EXPORT_SYMBOL(security_sk_classify_flow);
  4485. /**
  4486. * security_req_classify_flow() - Set a flow's secid based on request_sock
  4487. * @req: request_sock
  4488. * @flic: target flow
  4489. *
  4490. * Sets @flic's secid to @req's secid.
  4491. */
  4492. void security_req_classify_flow(const struct request_sock *req,
  4493. struct flowi_common *flic)
  4494. {
  4495. call_void_hook(req_classify_flow, req, flic);
  4496. }
  4497. EXPORT_SYMBOL(security_req_classify_flow);
  4498. /**
  4499. * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
  4500. * @sk: sock being grafted
  4501. * @parent: target parent socket
  4502. *
  4503. * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
  4504. * LSM state from @parent.
  4505. */
  4506. void security_sock_graft(struct sock *sk, struct socket *parent)
  4507. {
  4508. call_void_hook(sock_graft, sk, parent);
  4509. }
  4510. EXPORT_SYMBOL(security_sock_graft);
  4511. /**
  4512. * security_inet_conn_request() - Set request_sock state using incoming connect
  4513. * @sk: parent listening sock
  4514. * @skb: incoming connection
  4515. * @req: new request_sock
  4516. *
  4517. * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
  4518. *
  4519. * Return: Returns 0 if permission is granted.
  4520. */
  4521. int security_inet_conn_request(const struct sock *sk,
  4522. struct sk_buff *skb, struct request_sock *req)
  4523. {
  4524. return call_int_hook(inet_conn_request, sk, skb, req);
  4525. }
  4526. EXPORT_SYMBOL(security_inet_conn_request);
  4527. /**
  4528. * security_inet_csk_clone() - Set new sock LSM state based on request_sock
  4529. * @newsk: new sock
  4530. * @req: connection request_sock
  4531. *
  4532. * Set that LSM state of @sock using the LSM state from @req.
  4533. */
  4534. void security_inet_csk_clone(struct sock *newsk,
  4535. const struct request_sock *req)
  4536. {
  4537. call_void_hook(inet_csk_clone, newsk, req);
  4538. }
  4539. /**
  4540. * security_inet_conn_established() - Update sock's LSM state with connection
  4541. * @sk: sock
  4542. * @skb: connection packet
  4543. *
  4544. * Update @sock's LSM state to represent a new connection from @skb.
  4545. */
  4546. void security_inet_conn_established(struct sock *sk,
  4547. struct sk_buff *skb)
  4548. {
  4549. call_void_hook(inet_conn_established, sk, skb);
  4550. }
  4551. EXPORT_SYMBOL(security_inet_conn_established);
  4552. /**
  4553. * security_secmark_relabel_packet() - Check if setting a secmark is allowed
  4554. * @secid: new secmark value
  4555. *
  4556. * Check if the process should be allowed to relabel packets to @secid.
  4557. *
  4558. * Return: Returns 0 if permission is granted.
  4559. */
  4560. int security_secmark_relabel_packet(u32 secid)
  4561. {
  4562. return call_int_hook(secmark_relabel_packet, secid);
  4563. }
  4564. EXPORT_SYMBOL(security_secmark_relabel_packet);
  4565. /**
  4566. * security_secmark_refcount_inc() - Increment the secmark labeling rule count
  4567. *
  4568. * Tells the LSM to increment the number of secmark labeling rules loaded.
  4569. */
  4570. void security_secmark_refcount_inc(void)
  4571. {
  4572. call_void_hook(secmark_refcount_inc);
  4573. }
  4574. EXPORT_SYMBOL(security_secmark_refcount_inc);
  4575. /**
  4576. * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
  4577. *
  4578. * Tells the LSM to decrement the number of secmark labeling rules loaded.
  4579. */
  4580. void security_secmark_refcount_dec(void)
  4581. {
  4582. call_void_hook(secmark_refcount_dec);
  4583. }
  4584. EXPORT_SYMBOL(security_secmark_refcount_dec);
  4585. /**
  4586. * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
  4587. * @security: pointer to the LSM blob
  4588. *
  4589. * This hook allows a module to allocate a security structure for a TUN device,
  4590. * returning the pointer in @security.
  4591. *
  4592. * Return: Returns a zero on success, negative values on failure.
  4593. */
  4594. int security_tun_dev_alloc_security(void **security)
  4595. {
  4596. int rc;
  4597. rc = lsm_blob_alloc(security, blob_sizes.lbs_tun_dev, GFP_KERNEL);
  4598. if (rc)
  4599. return rc;
  4600. rc = call_int_hook(tun_dev_alloc_security, *security);
  4601. if (rc) {
  4602. kfree(*security);
  4603. *security = NULL;
  4604. }
  4605. return rc;
  4606. }
  4607. EXPORT_SYMBOL(security_tun_dev_alloc_security);
  4608. /**
  4609. * security_tun_dev_free_security() - Free a TUN device LSM blob
  4610. * @security: LSM blob
  4611. *
  4612. * This hook allows a module to free the security structure for a TUN device.
  4613. */
  4614. void security_tun_dev_free_security(void *security)
  4615. {
  4616. kfree(security);
  4617. }
  4618. EXPORT_SYMBOL(security_tun_dev_free_security);
  4619. /**
  4620. * security_tun_dev_create() - Check if creating a TUN device is allowed
  4621. *
  4622. * Check permissions prior to creating a new TUN device.
  4623. *
  4624. * Return: Returns 0 if permission is granted.
  4625. */
  4626. int security_tun_dev_create(void)
  4627. {
  4628. return call_int_hook(tun_dev_create);
  4629. }
  4630. EXPORT_SYMBOL(security_tun_dev_create);
  4631. /**
  4632. * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
  4633. * @security: TUN device LSM blob
  4634. *
  4635. * Check permissions prior to attaching to a TUN device queue.
  4636. *
  4637. * Return: Returns 0 if permission is granted.
  4638. */
  4639. int security_tun_dev_attach_queue(void *security)
  4640. {
  4641. return call_int_hook(tun_dev_attach_queue, security);
  4642. }
  4643. EXPORT_SYMBOL(security_tun_dev_attach_queue);
  4644. /**
  4645. * security_tun_dev_attach() - Update TUN device LSM state on attach
  4646. * @sk: associated sock
  4647. * @security: TUN device LSM blob
  4648. *
  4649. * This hook can be used by the module to update any security state associated
  4650. * with the TUN device's sock structure.
  4651. *
  4652. * Return: Returns 0 if permission is granted.
  4653. */
  4654. int security_tun_dev_attach(struct sock *sk, void *security)
  4655. {
  4656. return call_int_hook(tun_dev_attach, sk, security);
  4657. }
  4658. EXPORT_SYMBOL(security_tun_dev_attach);
  4659. /**
  4660. * security_tun_dev_open() - Update TUN device LSM state on open
  4661. * @security: TUN device LSM blob
  4662. *
  4663. * This hook can be used by the module to update any security state associated
  4664. * with the TUN device's security structure.
  4665. *
  4666. * Return: Returns 0 if permission is granted.
  4667. */
  4668. int security_tun_dev_open(void *security)
  4669. {
  4670. return call_int_hook(tun_dev_open, security);
  4671. }
  4672. EXPORT_SYMBOL(security_tun_dev_open);
  4673. /**
  4674. * security_sctp_assoc_request() - Update the LSM on a SCTP association req
  4675. * @asoc: SCTP association
  4676. * @skb: packet requesting the association
  4677. *
  4678. * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
  4679. *
  4680. * Return: Returns 0 on success, error on failure.
  4681. */
  4682. int security_sctp_assoc_request(struct sctp_association *asoc,
  4683. struct sk_buff *skb)
  4684. {
  4685. return call_int_hook(sctp_assoc_request, asoc, skb);
  4686. }
  4687. EXPORT_SYMBOL(security_sctp_assoc_request);
  4688. /**
  4689. * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
  4690. * @sk: socket
  4691. * @optname: SCTP option to validate
  4692. * @address: list of IP addresses to validate
  4693. * @addrlen: length of the address list
  4694. *
  4695. * Validiate permissions required for each address associated with sock @sk.
  4696. * Depending on @optname, the addresses will be treated as either a connect or
  4697. * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
  4698. * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
  4699. *
  4700. * Return: Returns 0 on success, error on failure.
  4701. */
  4702. int security_sctp_bind_connect(struct sock *sk, int optname,
  4703. struct sockaddr *address, int addrlen)
  4704. {
  4705. return call_int_hook(sctp_bind_connect, sk, optname, address, addrlen);
  4706. }
  4707. EXPORT_SYMBOL(security_sctp_bind_connect);
  4708. /**
  4709. * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
  4710. * @asoc: SCTP association
  4711. * @sk: original sock
  4712. * @newsk: target sock
  4713. *
  4714. * Called whenever a new socket is created by accept(2) (i.e. a TCP style
  4715. * socket) or when a socket is 'peeled off' e.g userspace calls
  4716. * sctp_peeloff(3).
  4717. */
  4718. void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
  4719. struct sock *newsk)
  4720. {
  4721. call_void_hook(sctp_sk_clone, asoc, sk, newsk);
  4722. }
  4723. EXPORT_SYMBOL(security_sctp_sk_clone);
  4724. /**
  4725. * security_sctp_assoc_established() - Update LSM state when assoc established
  4726. * @asoc: SCTP association
  4727. * @skb: packet establishing the association
  4728. *
  4729. * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
  4730. * security module.
  4731. *
  4732. * Return: Returns 0 if permission is granted.
  4733. */
  4734. int security_sctp_assoc_established(struct sctp_association *asoc,
  4735. struct sk_buff *skb)
  4736. {
  4737. return call_int_hook(sctp_assoc_established, asoc, skb);
  4738. }
  4739. EXPORT_SYMBOL(security_sctp_assoc_established);
  4740. /**
  4741. * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
  4742. * @sk: the owning MPTCP socket
  4743. * @ssk: the new subflow
  4744. *
  4745. * Update the labeling for the given MPTCP subflow, to match the one of the
  4746. * owning MPTCP socket. This hook has to be called after the socket creation and
  4747. * initialization via the security_socket_create() and
  4748. * security_socket_post_create() LSM hooks.
  4749. *
  4750. * Return: Returns 0 on success or a negative error code on failure.
  4751. */
  4752. int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
  4753. {
  4754. return call_int_hook(mptcp_add_subflow, sk, ssk);
  4755. }
  4756. #endif /* CONFIG_SECURITY_NETWORK */
  4757. #ifdef CONFIG_SECURITY_INFINIBAND
  4758. /**
  4759. * security_ib_pkey_access() - Check if access to an IB pkey is allowed
  4760. * @sec: LSM blob
  4761. * @subnet_prefix: subnet prefix of the port
  4762. * @pkey: IB pkey
  4763. *
  4764. * Check permission to access a pkey when modifying a QP.
  4765. *
  4766. * Return: Returns 0 if permission is granted.
  4767. */
  4768. int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
  4769. {
  4770. return call_int_hook(ib_pkey_access, sec, subnet_prefix, pkey);
  4771. }
  4772. EXPORT_SYMBOL(security_ib_pkey_access);
  4773. /**
  4774. * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
  4775. * @sec: LSM blob
  4776. * @dev_name: IB device name
  4777. * @port_num: port number
  4778. *
  4779. * Check permissions to send and receive SMPs on a end port.
  4780. *
  4781. * Return: Returns 0 if permission is granted.
  4782. */
  4783. int security_ib_endport_manage_subnet(void *sec,
  4784. const char *dev_name, u8 port_num)
  4785. {
  4786. return call_int_hook(ib_endport_manage_subnet, sec, dev_name, port_num);
  4787. }
  4788. EXPORT_SYMBOL(security_ib_endport_manage_subnet);
  4789. /**
  4790. * security_ib_alloc_security() - Allocate an Infiniband LSM blob
  4791. * @sec: LSM blob
  4792. *
  4793. * Allocate a security structure for Infiniband objects.
  4794. *
  4795. * Return: Returns 0 on success, non-zero on failure.
  4796. */
  4797. int security_ib_alloc_security(void **sec)
  4798. {
  4799. int rc;
  4800. rc = lsm_blob_alloc(sec, blob_sizes.lbs_ib, GFP_KERNEL);
  4801. if (rc)
  4802. return rc;
  4803. rc = call_int_hook(ib_alloc_security, *sec);
  4804. if (rc) {
  4805. kfree(*sec);
  4806. *sec = NULL;
  4807. }
  4808. return rc;
  4809. }
  4810. EXPORT_SYMBOL(security_ib_alloc_security);
  4811. /**
  4812. * security_ib_free_security() - Free an Infiniband LSM blob
  4813. * @sec: LSM blob
  4814. *
  4815. * Deallocate an Infiniband security structure.
  4816. */
  4817. void security_ib_free_security(void *sec)
  4818. {
  4819. kfree(sec);
  4820. }
  4821. EXPORT_SYMBOL(security_ib_free_security);
  4822. #endif /* CONFIG_SECURITY_INFINIBAND */
  4823. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  4824. /**
  4825. * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
  4826. * @ctxp: xfrm security context being added to the SPD
  4827. * @sec_ctx: security label provided by userspace
  4828. * @gfp: gfp flags
  4829. *
  4830. * Allocate a security structure to the xp->security field; the security field
  4831. * is initialized to NULL when the xfrm_policy is allocated.
  4832. *
  4833. * Return: Return 0 if operation was successful.
  4834. */
  4835. int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  4836. struct xfrm_user_sec_ctx *sec_ctx,
  4837. gfp_t gfp)
  4838. {
  4839. return call_int_hook(xfrm_policy_alloc_security, ctxp, sec_ctx, gfp);
  4840. }
  4841. EXPORT_SYMBOL(security_xfrm_policy_alloc);
  4842. /**
  4843. * security_xfrm_policy_clone() - Clone xfrm policy LSM state
  4844. * @old_ctx: xfrm security context
  4845. * @new_ctxp: target xfrm security context
  4846. *
  4847. * Allocate a security structure in new_ctxp that contains the information from
  4848. * the old_ctx structure.
  4849. *
  4850. * Return: Return 0 if operation was successful.
  4851. */
  4852. int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
  4853. struct xfrm_sec_ctx **new_ctxp)
  4854. {
  4855. return call_int_hook(xfrm_policy_clone_security, old_ctx, new_ctxp);
  4856. }
  4857. /**
  4858. * security_xfrm_policy_free() - Free a xfrm security context
  4859. * @ctx: xfrm security context
  4860. *
  4861. * Free LSM resources associated with @ctx.
  4862. */
  4863. void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  4864. {
  4865. call_void_hook(xfrm_policy_free_security, ctx);
  4866. }
  4867. EXPORT_SYMBOL(security_xfrm_policy_free);
  4868. /**
  4869. * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
  4870. * @ctx: xfrm security context
  4871. *
  4872. * Authorize deletion of a SPD entry.
  4873. *
  4874. * Return: Returns 0 if permission is granted.
  4875. */
  4876. int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  4877. {
  4878. return call_int_hook(xfrm_policy_delete_security, ctx);
  4879. }
  4880. /**
  4881. * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
  4882. * @x: xfrm state being added to the SAD
  4883. * @sec_ctx: security label provided by userspace
  4884. *
  4885. * Allocate a security structure to the @x->security field; the security field
  4886. * is initialized to NULL when the xfrm_state is allocated. Set the context to
  4887. * correspond to @sec_ctx.
  4888. *
  4889. * Return: Return 0 if operation was successful.
  4890. */
  4891. int security_xfrm_state_alloc(struct xfrm_state *x,
  4892. struct xfrm_user_sec_ctx *sec_ctx)
  4893. {
  4894. return call_int_hook(xfrm_state_alloc, x, sec_ctx);
  4895. }
  4896. EXPORT_SYMBOL(security_xfrm_state_alloc);
  4897. /**
  4898. * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
  4899. * @x: xfrm state being added to the SAD
  4900. * @polsec: associated policy's security context
  4901. * @secid: secid from the flow
  4902. *
  4903. * Allocate a security structure to the x->security field; the security field
  4904. * is initialized to NULL when the xfrm_state is allocated. Set the context to
  4905. * correspond to secid.
  4906. *
  4907. * Return: Returns 0 if operation was successful.
  4908. */
  4909. int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
  4910. struct xfrm_sec_ctx *polsec, u32 secid)
  4911. {
  4912. return call_int_hook(xfrm_state_alloc_acquire, x, polsec, secid);
  4913. }
  4914. /**
  4915. * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
  4916. * @x: xfrm state
  4917. *
  4918. * Authorize deletion of x->security.
  4919. *
  4920. * Return: Returns 0 if permission is granted.
  4921. */
  4922. int security_xfrm_state_delete(struct xfrm_state *x)
  4923. {
  4924. return call_int_hook(xfrm_state_delete_security, x);
  4925. }
  4926. EXPORT_SYMBOL(security_xfrm_state_delete);
  4927. /**
  4928. * security_xfrm_state_free() - Free a xfrm state
  4929. * @x: xfrm state
  4930. *
  4931. * Deallocate x->security.
  4932. */
  4933. void security_xfrm_state_free(struct xfrm_state *x)
  4934. {
  4935. call_void_hook(xfrm_state_free_security, x);
  4936. }
  4937. /**
  4938. * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
  4939. * @ctx: target xfrm security context
  4940. * @fl_secid: flow secid used to authorize access
  4941. *
  4942. * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
  4943. * packet. The hook is called when selecting either a per-socket policy or a
  4944. * generic xfrm policy.
  4945. *
  4946. * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
  4947. * other errors.
  4948. */
  4949. int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
  4950. {
  4951. return call_int_hook(xfrm_policy_lookup, ctx, fl_secid);
  4952. }
  4953. /**
  4954. * security_xfrm_state_pol_flow_match() - Check for a xfrm match
  4955. * @x: xfrm state to match
  4956. * @xp: xfrm policy to check for a match
  4957. * @flic: flow to check for a match.
  4958. *
  4959. * Check @xp and @flic for a match with @x.
  4960. *
  4961. * Return: Returns 1 if there is a match.
  4962. */
  4963. int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
  4964. struct xfrm_policy *xp,
  4965. const struct flowi_common *flic)
  4966. {
  4967. struct lsm_static_call *scall;
  4968. int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
  4969. /*
  4970. * Since this function is expected to return 0 or 1, the judgment
  4971. * becomes difficult if multiple LSMs supply this call. Fortunately,
  4972. * we can use the first LSM's judgment because currently only SELinux
  4973. * supplies this call.
  4974. *
  4975. * For speed optimization, we explicitly break the loop rather than
  4976. * using the macro
  4977. */
  4978. lsm_for_each_hook(scall, xfrm_state_pol_flow_match) {
  4979. rc = scall->hl->hook.xfrm_state_pol_flow_match(x, xp, flic);
  4980. break;
  4981. }
  4982. return rc;
  4983. }
  4984. /**
  4985. * security_xfrm_decode_session() - Determine the xfrm secid for a packet
  4986. * @skb: xfrm packet
  4987. * @secid: secid
  4988. *
  4989. * Decode the packet in @skb and return the security label in @secid.
  4990. *
  4991. * Return: Return 0 if all xfrms used have the same secid.
  4992. */
  4993. int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
  4994. {
  4995. return call_int_hook(xfrm_decode_session, skb, secid, 1);
  4996. }
  4997. void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
  4998. {
  4999. int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
  5000. 0);
  5001. BUG_ON(rc);
  5002. }
  5003. EXPORT_SYMBOL(security_skb_classify_flow);
  5004. #endif /* CONFIG_SECURITY_NETWORK_XFRM */
  5005. #ifdef CONFIG_KEYS
  5006. /**
  5007. * security_key_alloc() - Allocate and initialize a kernel key LSM blob
  5008. * @key: key
  5009. * @cred: credentials
  5010. * @flags: allocation flags
  5011. *
  5012. * Permit allocation of a key and assign security data. Note that key does not
  5013. * have a serial number assigned at this point.
  5014. *
  5015. * Return: Return 0 if permission is granted, -ve error otherwise.
  5016. */
  5017. int security_key_alloc(struct key *key, const struct cred *cred,
  5018. unsigned long flags)
  5019. {
  5020. int rc = lsm_key_alloc(key);
  5021. if (unlikely(rc))
  5022. return rc;
  5023. rc = call_int_hook(key_alloc, key, cred, flags);
  5024. if (unlikely(rc))
  5025. security_key_free(key);
  5026. return rc;
  5027. }
  5028. /**
  5029. * security_key_free() - Free a kernel key LSM blob
  5030. * @key: key
  5031. *
  5032. * Notification of destruction; free security data.
  5033. */
  5034. void security_key_free(struct key *key)
  5035. {
  5036. kfree(key->security);
  5037. key->security = NULL;
  5038. }
  5039. /**
  5040. * security_key_permission() - Check if a kernel key operation is allowed
  5041. * @key_ref: key reference
  5042. * @cred: credentials of actor requesting access
  5043. * @need_perm: requested permissions
  5044. *
  5045. * See whether a specific operational right is granted to a process on a key.
  5046. *
  5047. * Return: Return 0 if permission is granted, -ve error otherwise.
  5048. */
  5049. int security_key_permission(key_ref_t key_ref, const struct cred *cred,
  5050. enum key_need_perm need_perm)
  5051. {
  5052. return call_int_hook(key_permission, key_ref, cred, need_perm);
  5053. }
  5054. /**
  5055. * security_key_getsecurity() - Get the key's security label
  5056. * @key: key
  5057. * @buffer: security label buffer
  5058. *
  5059. * Get a textual representation of the security context attached to a key for
  5060. * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the
  5061. * storage for the NUL-terminated string and the caller should free it.
  5062. *
  5063. * Return: Returns the length of @buffer (including terminating NUL) or -ve if
  5064. * an error occurs. May also return 0 (and a NULL buffer pointer) if
  5065. * there is no security label assigned to the key.
  5066. */
  5067. int security_key_getsecurity(struct key *key, char **buffer)
  5068. {
  5069. *buffer = NULL;
  5070. return call_int_hook(key_getsecurity, key, buffer);
  5071. }
  5072. /**
  5073. * security_key_post_create_or_update() - Notification of key create or update
  5074. * @keyring: keyring to which the key is linked to
  5075. * @key: created or updated key
  5076. * @payload: data used to instantiate or update the key
  5077. * @payload_len: length of payload
  5078. * @flags: key flags
  5079. * @create: flag indicating whether the key was created or updated
  5080. *
  5081. * Notify the caller of a key creation or update.
  5082. */
  5083. void security_key_post_create_or_update(struct key *keyring, struct key *key,
  5084. const void *payload, size_t payload_len,
  5085. unsigned long flags, bool create)
  5086. {
  5087. call_void_hook(key_post_create_or_update, keyring, key, payload,
  5088. payload_len, flags, create);
  5089. }
  5090. #endif /* CONFIG_KEYS */
  5091. #ifdef CONFIG_AUDIT
  5092. /**
  5093. * security_audit_rule_init() - Allocate and init an LSM audit rule struct
  5094. * @field: audit action
  5095. * @op: rule operator
  5096. * @rulestr: rule context
  5097. * @lsmrule: receive buffer for audit rule struct
  5098. * @gfp: GFP flag used for kmalloc
  5099. *
  5100. * Allocate and initialize an LSM audit rule structure.
  5101. *
  5102. * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
  5103. * an invalid rule.
  5104. */
  5105. int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
  5106. gfp_t gfp)
  5107. {
  5108. return call_int_hook(audit_rule_init, field, op, rulestr, lsmrule, gfp);
  5109. }
  5110. /**
  5111. * security_audit_rule_known() - Check if an audit rule contains LSM fields
  5112. * @krule: audit rule
  5113. *
  5114. * Specifies whether given @krule contains any fields related to the current
  5115. * LSM.
  5116. *
  5117. * Return: Returns 1 in case of relation found, 0 otherwise.
  5118. */
  5119. int security_audit_rule_known(struct audit_krule *krule)
  5120. {
  5121. return call_int_hook(audit_rule_known, krule);
  5122. }
  5123. /**
  5124. * security_audit_rule_free() - Free an LSM audit rule struct
  5125. * @lsmrule: audit rule struct
  5126. *
  5127. * Deallocate the LSM audit rule structure previously allocated by
  5128. * audit_rule_init().
  5129. */
  5130. void security_audit_rule_free(void *lsmrule)
  5131. {
  5132. call_void_hook(audit_rule_free, lsmrule);
  5133. }
  5134. /**
  5135. * security_audit_rule_match() - Check if a label matches an audit rule
  5136. * @secid: security label
  5137. * @field: LSM audit field
  5138. * @op: matching operator
  5139. * @lsmrule: audit rule
  5140. *
  5141. * Determine if given @secid matches a rule previously approved by
  5142. * security_audit_rule_known().
  5143. *
  5144. * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
  5145. * failure.
  5146. */
  5147. int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
  5148. {
  5149. return call_int_hook(audit_rule_match, secid, field, op, lsmrule);
  5150. }
  5151. #endif /* CONFIG_AUDIT */
  5152. #ifdef CONFIG_BPF_SYSCALL
  5153. /**
  5154. * security_bpf() - Check if the bpf syscall operation is allowed
  5155. * @cmd: command
  5156. * @attr: bpf attribute
  5157. * @size: size
  5158. *
  5159. * Do a initial check for all bpf syscalls after the attribute is copied into
  5160. * the kernel. The actual security module can implement their own rules to
  5161. * check the specific cmd they need.
  5162. *
  5163. * Return: Returns 0 if permission is granted.
  5164. */
  5165. int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
  5166. {
  5167. return call_int_hook(bpf, cmd, attr, size);
  5168. }
  5169. /**
  5170. * security_bpf_map() - Check if access to a bpf map is allowed
  5171. * @map: bpf map
  5172. * @fmode: mode
  5173. *
  5174. * Do a check when the kernel generates and returns a file descriptor for eBPF
  5175. * maps.
  5176. *
  5177. * Return: Returns 0 if permission is granted.
  5178. */
  5179. int security_bpf_map(struct bpf_map *map, fmode_t fmode)
  5180. {
  5181. return call_int_hook(bpf_map, map, fmode);
  5182. }
  5183. /**
  5184. * security_bpf_prog() - Check if access to a bpf program is allowed
  5185. * @prog: bpf program
  5186. *
  5187. * Do a check when the kernel generates and returns a file descriptor for eBPF
  5188. * programs.
  5189. *
  5190. * Return: Returns 0 if permission is granted.
  5191. */
  5192. int security_bpf_prog(struct bpf_prog *prog)
  5193. {
  5194. return call_int_hook(bpf_prog, prog);
  5195. }
  5196. /**
  5197. * security_bpf_map_create() - Check if BPF map creation is allowed
  5198. * @map: BPF map object
  5199. * @attr: BPF syscall attributes used to create BPF map
  5200. * @token: BPF token used to grant user access
  5201. *
  5202. * Do a check when the kernel creates a new BPF map. This is also the
  5203. * point where LSM blob is allocated for LSMs that need them.
  5204. *
  5205. * Return: Returns 0 on success, error on failure.
  5206. */
  5207. int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
  5208. struct bpf_token *token)
  5209. {
  5210. return call_int_hook(bpf_map_create, map, attr, token);
  5211. }
  5212. /**
  5213. * security_bpf_prog_load() - Check if loading of BPF program is allowed
  5214. * @prog: BPF program object
  5215. * @attr: BPF syscall attributes used to create BPF program
  5216. * @token: BPF token used to grant user access to BPF subsystem
  5217. *
  5218. * Perform an access control check when the kernel loads a BPF program and
  5219. * allocates associated BPF program object. This hook is also responsible for
  5220. * allocating any required LSM state for the BPF program.
  5221. *
  5222. * Return: Returns 0 on success, error on failure.
  5223. */
  5224. int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
  5225. struct bpf_token *token)
  5226. {
  5227. return call_int_hook(bpf_prog_load, prog, attr, token);
  5228. }
  5229. /**
  5230. * security_bpf_token_create() - Check if creating of BPF token is allowed
  5231. * @token: BPF token object
  5232. * @attr: BPF syscall attributes used to create BPF token
  5233. * @path: path pointing to BPF FS mount point from which BPF token is created
  5234. *
  5235. * Do a check when the kernel instantiates a new BPF token object from BPF FS
  5236. * instance. This is also the point where LSM blob can be allocated for LSMs.
  5237. *
  5238. * Return: Returns 0 on success, error on failure.
  5239. */
  5240. int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
  5241. const struct path *path)
  5242. {
  5243. return call_int_hook(bpf_token_create, token, attr, path);
  5244. }
  5245. /**
  5246. * security_bpf_token_cmd() - Check if BPF token is allowed to delegate
  5247. * requested BPF syscall command
  5248. * @token: BPF token object
  5249. * @cmd: BPF syscall command requested to be delegated by BPF token
  5250. *
  5251. * Do a check when the kernel decides whether provided BPF token should allow
  5252. * delegation of requested BPF syscall command.
  5253. *
  5254. * Return: Returns 0 on success, error on failure.
  5255. */
  5256. int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
  5257. {
  5258. return call_int_hook(bpf_token_cmd, token, cmd);
  5259. }
  5260. /**
  5261. * security_bpf_token_capable() - Check if BPF token is allowed to delegate
  5262. * requested BPF-related capability
  5263. * @token: BPF token object
  5264. * @cap: capabilities requested to be delegated by BPF token
  5265. *
  5266. * Do a check when the kernel decides whether provided BPF token should allow
  5267. * delegation of requested BPF-related capabilities.
  5268. *
  5269. * Return: Returns 0 on success, error on failure.
  5270. */
  5271. int security_bpf_token_capable(const struct bpf_token *token, int cap)
  5272. {
  5273. return call_int_hook(bpf_token_capable, token, cap);
  5274. }
  5275. /**
  5276. * security_bpf_map_free() - Free a bpf map's LSM blob
  5277. * @map: bpf map
  5278. *
  5279. * Clean up the security information stored inside bpf map.
  5280. */
  5281. void security_bpf_map_free(struct bpf_map *map)
  5282. {
  5283. call_void_hook(bpf_map_free, map);
  5284. }
  5285. /**
  5286. * security_bpf_prog_free() - Free a BPF program's LSM blob
  5287. * @prog: BPF program struct
  5288. *
  5289. * Clean up the security information stored inside BPF program.
  5290. */
  5291. void security_bpf_prog_free(struct bpf_prog *prog)
  5292. {
  5293. call_void_hook(bpf_prog_free, prog);
  5294. }
  5295. /**
  5296. * security_bpf_token_free() - Free a BPF token's LSM blob
  5297. * @token: BPF token struct
  5298. *
  5299. * Clean up the security information stored inside BPF token.
  5300. */
  5301. void security_bpf_token_free(struct bpf_token *token)
  5302. {
  5303. call_void_hook(bpf_token_free, token);
  5304. }
  5305. #endif /* CONFIG_BPF_SYSCALL */
  5306. /**
  5307. * security_locked_down() - Check if a kernel feature is allowed
  5308. * @what: requested kernel feature
  5309. *
  5310. * Determine whether a kernel feature that potentially enables arbitrary code
  5311. * execution in kernel space should be permitted.
  5312. *
  5313. * Return: Returns 0 if permission is granted.
  5314. */
  5315. int security_locked_down(enum lockdown_reason what)
  5316. {
  5317. return call_int_hook(locked_down, what);
  5318. }
  5319. EXPORT_SYMBOL(security_locked_down);
  5320. /**
  5321. * security_bdev_alloc() - Allocate a block device LSM blob
  5322. * @bdev: block device
  5323. *
  5324. * Allocate and attach a security structure to @bdev->bd_security. The
  5325. * security field is initialized to NULL when the bdev structure is
  5326. * allocated.
  5327. *
  5328. * Return: Return 0 if operation was successful.
  5329. */
  5330. int security_bdev_alloc(struct block_device *bdev)
  5331. {
  5332. int rc = 0;
  5333. rc = lsm_bdev_alloc(bdev);
  5334. if (unlikely(rc))
  5335. return rc;
  5336. rc = call_int_hook(bdev_alloc_security, bdev);
  5337. if (unlikely(rc))
  5338. security_bdev_free(bdev);
  5339. return rc;
  5340. }
  5341. EXPORT_SYMBOL(security_bdev_alloc);
  5342. /**
  5343. * security_bdev_free() - Free a block device's LSM blob
  5344. * @bdev: block device
  5345. *
  5346. * Deallocate the bdev security structure and set @bdev->bd_security to NULL.
  5347. */
  5348. void security_bdev_free(struct block_device *bdev)
  5349. {
  5350. if (!bdev->bd_security)
  5351. return;
  5352. call_void_hook(bdev_free_security, bdev);
  5353. kfree(bdev->bd_security);
  5354. bdev->bd_security = NULL;
  5355. }
  5356. EXPORT_SYMBOL(security_bdev_free);
  5357. /**
  5358. * security_bdev_setintegrity() - Set the device's integrity data
  5359. * @bdev: block device
  5360. * @type: type of integrity, e.g. hash digest, signature, etc
  5361. * @value: the integrity value
  5362. * @size: size of the integrity value
  5363. *
  5364. * Register a verified integrity measurement of a bdev with LSMs.
  5365. * LSMs should free the previously saved data if @value is NULL.
  5366. * Please note that the new hook should be invoked every time the security
  5367. * information is updated to keep these data current. For example, in dm-verity,
  5368. * if the mapping table is reloaded and configured to use a different dm-verity
  5369. * target with a new roothash and signing information, the previously stored
  5370. * data in the LSM blob will become obsolete. It is crucial to re-invoke the
  5371. * hook to refresh these data and ensure they are up to date. This necessity
  5372. * arises from the design of device-mapper, where a device-mapper device is
  5373. * first created, and then targets are subsequently loaded into it. These
  5374. * targets can be modified multiple times during the device's lifetime.
  5375. * Therefore, while the LSM blob is allocated during the creation of the block
  5376. * device, its actual contents are not initialized at this stage and can change
  5377. * substantially over time. This includes alterations from data that the LSMs
  5378. * 'trusts' to those they do not, making it essential to handle these changes
  5379. * correctly. Failure to address this dynamic aspect could potentially allow
  5380. * for bypassing LSM checks.
  5381. *
  5382. * Return: Returns 0 on success, negative values on failure.
  5383. */
  5384. int security_bdev_setintegrity(struct block_device *bdev,
  5385. enum lsm_integrity_type type, const void *value,
  5386. size_t size)
  5387. {
  5388. return call_int_hook(bdev_setintegrity, bdev, type, value, size);
  5389. }
  5390. EXPORT_SYMBOL(security_bdev_setintegrity);
  5391. #ifdef CONFIG_PERF_EVENTS
  5392. /**
  5393. * security_perf_event_open() - Check if a perf event open is allowed
  5394. * @attr: perf event attribute
  5395. * @type: type of event
  5396. *
  5397. * Check whether the @type of perf_event_open syscall is allowed.
  5398. *
  5399. * Return: Returns 0 if permission is granted.
  5400. */
  5401. int security_perf_event_open(struct perf_event_attr *attr, int type)
  5402. {
  5403. return call_int_hook(perf_event_open, attr, type);
  5404. }
  5405. /**
  5406. * security_perf_event_alloc() - Allocate a perf event LSM blob
  5407. * @event: perf event
  5408. *
  5409. * Allocate and save perf_event security info.
  5410. *
  5411. * Return: Returns 0 on success, error on failure.
  5412. */
  5413. int security_perf_event_alloc(struct perf_event *event)
  5414. {
  5415. int rc;
  5416. rc = lsm_blob_alloc(&event->security, blob_sizes.lbs_perf_event,
  5417. GFP_KERNEL);
  5418. if (rc)
  5419. return rc;
  5420. rc = call_int_hook(perf_event_alloc, event);
  5421. if (rc) {
  5422. kfree(event->security);
  5423. event->security = NULL;
  5424. }
  5425. return rc;
  5426. }
  5427. /**
  5428. * security_perf_event_free() - Free a perf event LSM blob
  5429. * @event: perf event
  5430. *
  5431. * Release (free) perf_event security info.
  5432. */
  5433. void security_perf_event_free(struct perf_event *event)
  5434. {
  5435. kfree(event->security);
  5436. event->security = NULL;
  5437. }
  5438. /**
  5439. * security_perf_event_read() - Check if reading a perf event label is allowed
  5440. * @event: perf event
  5441. *
  5442. * Read perf_event security info if allowed.
  5443. *
  5444. * Return: Returns 0 if permission is granted.
  5445. */
  5446. int security_perf_event_read(struct perf_event *event)
  5447. {
  5448. return call_int_hook(perf_event_read, event);
  5449. }
  5450. /**
  5451. * security_perf_event_write() - Check if writing a perf event label is allowed
  5452. * @event: perf event
  5453. *
  5454. * Write perf_event security info if allowed.
  5455. *
  5456. * Return: Returns 0 if permission is granted.
  5457. */
  5458. int security_perf_event_write(struct perf_event *event)
  5459. {
  5460. return call_int_hook(perf_event_write, event);
  5461. }
  5462. #endif /* CONFIG_PERF_EVENTS */
  5463. #ifdef CONFIG_IO_URING
  5464. /**
  5465. * security_uring_override_creds() - Check if overriding creds is allowed
  5466. * @new: new credentials
  5467. *
  5468. * Check if the current task, executing an io_uring operation, is allowed to
  5469. * override it's credentials with @new.
  5470. *
  5471. * Return: Returns 0 if permission is granted.
  5472. */
  5473. int security_uring_override_creds(const struct cred *new)
  5474. {
  5475. return call_int_hook(uring_override_creds, new);
  5476. }
  5477. /**
  5478. * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
  5479. *
  5480. * Check whether the current task is allowed to spawn a io_uring polling thread
  5481. * (IORING_SETUP_SQPOLL).
  5482. *
  5483. * Return: Returns 0 if permission is granted.
  5484. */
  5485. int security_uring_sqpoll(void)
  5486. {
  5487. return call_int_hook(uring_sqpoll);
  5488. }
  5489. /**
  5490. * security_uring_cmd() - Check if a io_uring passthrough command is allowed
  5491. * @ioucmd: command
  5492. *
  5493. * Check whether the file_operations uring_cmd is allowed to run.
  5494. *
  5495. * Return: Returns 0 if permission is granted.
  5496. */
  5497. int security_uring_cmd(struct io_uring_cmd *ioucmd)
  5498. {
  5499. return call_int_hook(uring_cmd, ioucmd);
  5500. }
  5501. #endif /* CONFIG_IO_URING */
  5502. /**
  5503. * security_initramfs_populated() - Notify LSMs that initramfs has been loaded
  5504. *
  5505. * Tells the LSMs the initramfs has been unpacked into the rootfs.
  5506. */
  5507. void security_initramfs_populated(void)
  5508. {
  5509. call_void_hook(initramfs_populated);
  5510. }