hooks.c 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Security-Enhanced Linux (SELinux) security module
  4. *
  5. * This file contains the SELinux hook function implementations.
  6. *
  7. * Authors: Stephen Smalley, <stephen.smalley.work@gmail.com>
  8. * Chris Vance, <cvance@nai.com>
  9. * Wayne Salamon, <wsalamon@nai.com>
  10. * James Morris <jmorris@redhat.com>
  11. *
  12. * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
  13. * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
  14. * Eric Paris <eparis@redhat.com>
  15. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  16. * <dgoeddel@trustedcs.com>
  17. * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
  18. * Paul Moore <paul@paul-moore.com>
  19. * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
  20. * Yuichi Nakamura <ynakam@hitachisoft.jp>
  21. * Copyright (C) 2016 Mellanox Technologies
  22. */
  23. #include <linux/init.h>
  24. #include <linux/kd.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kernel_read_file.h>
  27. #include <linux/errno.h>
  28. #include <linux/sched/signal.h>
  29. #include <linux/sched/task.h>
  30. #include <linux/lsm_hooks.h>
  31. #include <linux/xattr.h>
  32. #include <linux/capability.h>
  33. #include <linux/unistd.h>
  34. #include <linux/mm.h>
  35. #include <linux/mman.h>
  36. #include <linux/slab.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/swap.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/syscalls.h>
  42. #include <linux/dcache.h>
  43. #include <linux/file.h>
  44. #include <linux/fdtable.h>
  45. #include <linux/namei.h>
  46. #include <linux/mount.h>
  47. #include <linux/fs_context.h>
  48. #include <linux/fs_parser.h>
  49. #include <linux/netfilter_ipv4.h>
  50. #include <linux/netfilter_ipv6.h>
  51. #include <linux/tty.h>
  52. #include <net/icmp.h>
  53. #include <net/ip.h> /* for local_port_range[] */
  54. #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
  55. #include <net/inet_connection_sock.h>
  56. #include <net/net_namespace.h>
  57. #include <net/netlabel.h>
  58. #include <linux/uaccess.h>
  59. #include <asm/ioctls.h>
  60. #include <linux/atomic.h>
  61. #include <linux/bitops.h>
  62. #include <linux/interrupt.h>
  63. #include <linux/netdevice.h> /* for network interface checks */
  64. #include <net/netlink.h>
  65. #include <linux/tcp.h>
  66. #include <linux/udp.h>
  67. #include <linux/dccp.h>
  68. #include <linux/sctp.h>
  69. #include <net/sctp/structs.h>
  70. #include <linux/quota.h>
  71. #include <linux/un.h> /* for Unix socket types */
  72. #include <net/af_unix.h> /* for Unix socket types */
  73. #include <linux/parser.h>
  74. #include <linux/nfs_mount.h>
  75. #include <net/ipv6.h>
  76. #include <linux/hugetlb.h>
  77. #include <linux/personality.h>
  78. #include <linux/audit.h>
  79. #include <linux/string.h>
  80. #include <linux/mutex.h>
  81. #include <linux/posix-timers.h>
  82. #include <linux/syslog.h>
  83. #include <linux/user_namespace.h>
  84. #include <linux/export.h>
  85. #include <linux/msg.h>
  86. #include <linux/shm.h>
  87. #include <uapi/linux/shm.h>
  88. #include <linux/bpf.h>
  89. #include <linux/kernfs.h>
  90. #include <linux/stringhash.h> /* for hashlen_string() */
  91. #include <uapi/linux/mount.h>
  92. #include <linux/fsnotify.h>
  93. #include <linux/fanotify.h>
  94. #include <linux/io_uring/cmd.h>
  95. #include <uapi/linux/lsm.h>
  96. #include "avc.h"
  97. #include "objsec.h"
  98. #include "netif.h"
  99. #include "netnode.h"
  100. #include "netport.h"
  101. #include "ibpkey.h"
  102. #include "xfrm.h"
  103. #include "netlabel.h"
  104. #include "audit.h"
  105. #include "avc_ss.h"
  106. #define SELINUX_INODE_INIT_XATTRS 1
  107. struct selinux_state selinux_state;
  108. /* SECMARK reference count */
  109. static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
  110. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  111. static int selinux_enforcing_boot __initdata;
  112. static int __init enforcing_setup(char *str)
  113. {
  114. unsigned long enforcing;
  115. if (!kstrtoul(str, 0, &enforcing))
  116. selinux_enforcing_boot = enforcing ? 1 : 0;
  117. return 1;
  118. }
  119. __setup("enforcing=", enforcing_setup);
  120. #else
  121. #define selinux_enforcing_boot 1
  122. #endif
  123. int selinux_enabled_boot __initdata = 1;
  124. #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
  125. static int __init selinux_enabled_setup(char *str)
  126. {
  127. unsigned long enabled;
  128. if (!kstrtoul(str, 0, &enabled))
  129. selinux_enabled_boot = enabled ? 1 : 0;
  130. return 1;
  131. }
  132. __setup("selinux=", selinux_enabled_setup);
  133. #endif
  134. static int __init checkreqprot_setup(char *str)
  135. {
  136. unsigned long checkreqprot;
  137. if (!kstrtoul(str, 0, &checkreqprot)) {
  138. if (checkreqprot)
  139. pr_err("SELinux: checkreqprot set to 1 via kernel parameter. This is no longer supported.\n");
  140. }
  141. return 1;
  142. }
  143. __setup("checkreqprot=", checkreqprot_setup);
  144. /**
  145. * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
  146. *
  147. * Description:
  148. * This function checks the SECMARK reference counter to see if any SECMARK
  149. * targets are currently configured, if the reference counter is greater than
  150. * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
  151. * enabled, false (0) if SECMARK is disabled. If the always_check_network
  152. * policy capability is enabled, SECMARK is always considered enabled.
  153. *
  154. */
  155. static int selinux_secmark_enabled(void)
  156. {
  157. return (selinux_policycap_alwaysnetwork() ||
  158. atomic_read(&selinux_secmark_refcount));
  159. }
  160. /**
  161. * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
  162. *
  163. * Description:
  164. * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
  165. * (1) if any are enabled or false (0) if neither are enabled. If the
  166. * always_check_network policy capability is enabled, peer labeling
  167. * is always considered enabled.
  168. *
  169. */
  170. static int selinux_peerlbl_enabled(void)
  171. {
  172. return (selinux_policycap_alwaysnetwork() ||
  173. netlbl_enabled() || selinux_xfrm_enabled());
  174. }
  175. static int selinux_netcache_avc_callback(u32 event)
  176. {
  177. if (event == AVC_CALLBACK_RESET) {
  178. sel_netif_flush();
  179. sel_netnode_flush();
  180. sel_netport_flush();
  181. synchronize_net();
  182. }
  183. return 0;
  184. }
  185. static int selinux_lsm_notifier_avc_callback(u32 event)
  186. {
  187. if (event == AVC_CALLBACK_RESET) {
  188. sel_ib_pkey_flush();
  189. call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
  190. }
  191. return 0;
  192. }
  193. /*
  194. * initialise the security for the init task
  195. */
  196. static void cred_init_security(void)
  197. {
  198. struct task_security_struct *tsec;
  199. tsec = selinux_cred(unrcu_pointer(current->real_cred));
  200. tsec->osid = tsec->sid = SECINITSID_KERNEL;
  201. }
  202. /*
  203. * get the security ID of a set of credentials
  204. */
  205. static inline u32 cred_sid(const struct cred *cred)
  206. {
  207. const struct task_security_struct *tsec;
  208. tsec = selinux_cred(cred);
  209. return tsec->sid;
  210. }
  211. static void __ad_net_init(struct common_audit_data *ad,
  212. struct lsm_network_audit *net,
  213. int ifindex, struct sock *sk, u16 family)
  214. {
  215. ad->type = LSM_AUDIT_DATA_NET;
  216. ad->u.net = net;
  217. net->netif = ifindex;
  218. net->sk = sk;
  219. net->family = family;
  220. }
  221. static void ad_net_init_from_sk(struct common_audit_data *ad,
  222. struct lsm_network_audit *net,
  223. struct sock *sk)
  224. {
  225. __ad_net_init(ad, net, 0, sk, 0);
  226. }
  227. static void ad_net_init_from_iif(struct common_audit_data *ad,
  228. struct lsm_network_audit *net,
  229. int ifindex, u16 family)
  230. {
  231. __ad_net_init(ad, net, ifindex, NULL, family);
  232. }
  233. /*
  234. * get the objective security ID of a task
  235. */
  236. static inline u32 task_sid_obj(const struct task_struct *task)
  237. {
  238. u32 sid;
  239. rcu_read_lock();
  240. sid = cred_sid(__task_cred(task));
  241. rcu_read_unlock();
  242. return sid;
  243. }
  244. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
  245. /*
  246. * Try reloading inode security labels that have been marked as invalid. The
  247. * @may_sleep parameter indicates when sleeping and thus reloading labels is
  248. * allowed; when set to false, returns -ECHILD when the label is
  249. * invalid. The @dentry parameter should be set to a dentry of the inode.
  250. */
  251. static int __inode_security_revalidate(struct inode *inode,
  252. struct dentry *dentry,
  253. bool may_sleep)
  254. {
  255. struct inode_security_struct *isec = selinux_inode(inode);
  256. might_sleep_if(may_sleep);
  257. /*
  258. * The check of isec->initialized below is racy but
  259. * inode_doinit_with_dentry() will recheck with
  260. * isec->lock held.
  261. */
  262. if (selinux_initialized() &&
  263. data_race(isec->initialized != LABEL_INITIALIZED)) {
  264. if (!may_sleep)
  265. return -ECHILD;
  266. /*
  267. * Try reloading the inode security label. This will fail if
  268. * @opt_dentry is NULL and no dentry for this inode can be
  269. * found; in that case, continue using the old label.
  270. */
  271. inode_doinit_with_dentry(inode, dentry);
  272. }
  273. return 0;
  274. }
  275. static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
  276. {
  277. return selinux_inode(inode);
  278. }
  279. static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
  280. {
  281. int error;
  282. error = __inode_security_revalidate(inode, NULL, !rcu);
  283. if (error)
  284. return ERR_PTR(error);
  285. return selinux_inode(inode);
  286. }
  287. /*
  288. * Get the security label of an inode.
  289. */
  290. static struct inode_security_struct *inode_security(struct inode *inode)
  291. {
  292. __inode_security_revalidate(inode, NULL, true);
  293. return selinux_inode(inode);
  294. }
  295. static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
  296. {
  297. struct inode *inode = d_backing_inode(dentry);
  298. return selinux_inode(inode);
  299. }
  300. /*
  301. * Get the security label of a dentry's backing inode.
  302. */
  303. static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
  304. {
  305. struct inode *inode = d_backing_inode(dentry);
  306. __inode_security_revalidate(inode, dentry, true);
  307. return selinux_inode(inode);
  308. }
  309. static void inode_free_security(struct inode *inode)
  310. {
  311. struct inode_security_struct *isec = selinux_inode(inode);
  312. struct superblock_security_struct *sbsec;
  313. if (!isec)
  314. return;
  315. sbsec = selinux_superblock(inode->i_sb);
  316. /*
  317. * As not all inode security structures are in a list, we check for
  318. * empty list outside of the lock to make sure that we won't waste
  319. * time taking a lock doing nothing.
  320. *
  321. * The list_del_init() function can be safely called more than once.
  322. * It should not be possible for this function to be called with
  323. * concurrent list_add(), but for better safety against future changes
  324. * in the code, we use list_empty_careful() here.
  325. */
  326. if (!list_empty_careful(&isec->list)) {
  327. spin_lock(&sbsec->isec_lock);
  328. list_del_init(&isec->list);
  329. spin_unlock(&sbsec->isec_lock);
  330. }
  331. }
  332. struct selinux_mnt_opts {
  333. u32 fscontext_sid;
  334. u32 context_sid;
  335. u32 rootcontext_sid;
  336. u32 defcontext_sid;
  337. };
  338. static void selinux_free_mnt_opts(void *mnt_opts)
  339. {
  340. kfree(mnt_opts);
  341. }
  342. enum {
  343. Opt_error = -1,
  344. Opt_context = 0,
  345. Opt_defcontext = 1,
  346. Opt_fscontext = 2,
  347. Opt_rootcontext = 3,
  348. Opt_seclabel = 4,
  349. };
  350. #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
  351. static const struct {
  352. const char *name;
  353. int len;
  354. int opt;
  355. bool has_arg;
  356. } tokens[] = {
  357. A(context, true),
  358. A(fscontext, true),
  359. A(defcontext, true),
  360. A(rootcontext, true),
  361. A(seclabel, false),
  362. };
  363. #undef A
  364. static int match_opt_prefix(char *s, int l, char **arg)
  365. {
  366. int i;
  367. for (i = 0; i < ARRAY_SIZE(tokens); i++) {
  368. size_t len = tokens[i].len;
  369. if (len > l || memcmp(s, tokens[i].name, len))
  370. continue;
  371. if (tokens[i].has_arg) {
  372. if (len == l || s[len] != '=')
  373. continue;
  374. *arg = s + len + 1;
  375. } else if (len != l)
  376. continue;
  377. return tokens[i].opt;
  378. }
  379. return Opt_error;
  380. }
  381. #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
  382. static int may_context_mount_sb_relabel(u32 sid,
  383. struct superblock_security_struct *sbsec,
  384. const struct cred *cred)
  385. {
  386. const struct task_security_struct *tsec = selinux_cred(cred);
  387. int rc;
  388. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  389. FILESYSTEM__RELABELFROM, NULL);
  390. if (rc)
  391. return rc;
  392. rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
  393. FILESYSTEM__RELABELTO, NULL);
  394. return rc;
  395. }
  396. static int may_context_mount_inode_relabel(u32 sid,
  397. struct superblock_security_struct *sbsec,
  398. const struct cred *cred)
  399. {
  400. const struct task_security_struct *tsec = selinux_cred(cred);
  401. int rc;
  402. rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
  403. FILESYSTEM__RELABELFROM, NULL);
  404. if (rc)
  405. return rc;
  406. rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
  407. FILESYSTEM__ASSOCIATE, NULL);
  408. return rc;
  409. }
  410. static int selinux_is_genfs_special_handling(struct super_block *sb)
  411. {
  412. /* Special handling. Genfs but also in-core setxattr handler */
  413. return !strcmp(sb->s_type->name, "sysfs") ||
  414. !strcmp(sb->s_type->name, "pstore") ||
  415. !strcmp(sb->s_type->name, "debugfs") ||
  416. !strcmp(sb->s_type->name, "tracefs") ||
  417. !strcmp(sb->s_type->name, "rootfs") ||
  418. (selinux_policycap_cgroupseclabel() &&
  419. (!strcmp(sb->s_type->name, "cgroup") ||
  420. !strcmp(sb->s_type->name, "cgroup2")));
  421. }
  422. static int selinux_is_sblabel_mnt(struct super_block *sb)
  423. {
  424. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  425. /*
  426. * IMPORTANT: Double-check logic in this function when adding a new
  427. * SECURITY_FS_USE_* definition!
  428. */
  429. BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
  430. switch (sbsec->behavior) {
  431. case SECURITY_FS_USE_XATTR:
  432. case SECURITY_FS_USE_TRANS:
  433. case SECURITY_FS_USE_TASK:
  434. case SECURITY_FS_USE_NATIVE:
  435. return 1;
  436. case SECURITY_FS_USE_GENFS:
  437. return selinux_is_genfs_special_handling(sb);
  438. /* Never allow relabeling on context mounts */
  439. case SECURITY_FS_USE_MNTPOINT:
  440. case SECURITY_FS_USE_NONE:
  441. default:
  442. return 0;
  443. }
  444. }
  445. static int sb_check_xattr_support(struct super_block *sb)
  446. {
  447. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  448. struct dentry *root = sb->s_root;
  449. struct inode *root_inode = d_backing_inode(root);
  450. u32 sid;
  451. int rc;
  452. /*
  453. * Make sure that the xattr handler exists and that no
  454. * error other than -ENODATA is returned by getxattr on
  455. * the root directory. -ENODATA is ok, as this may be
  456. * the first boot of the SELinux kernel before we have
  457. * assigned xattr values to the filesystem.
  458. */
  459. if (!(root_inode->i_opflags & IOP_XATTR)) {
  460. pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
  461. sb->s_id, sb->s_type->name);
  462. goto fallback;
  463. }
  464. rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
  465. if (rc < 0 && rc != -ENODATA) {
  466. if (rc == -EOPNOTSUPP) {
  467. pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
  468. sb->s_id, sb->s_type->name);
  469. goto fallback;
  470. } else {
  471. pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
  472. sb->s_id, sb->s_type->name, -rc);
  473. return rc;
  474. }
  475. }
  476. return 0;
  477. fallback:
  478. /* No xattr support - try to fallback to genfs if possible. */
  479. rc = security_genfs_sid(sb->s_type->name, "/",
  480. SECCLASS_DIR, &sid);
  481. if (rc)
  482. return -EOPNOTSUPP;
  483. pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
  484. sb->s_id, sb->s_type->name);
  485. sbsec->behavior = SECURITY_FS_USE_GENFS;
  486. sbsec->sid = sid;
  487. return 0;
  488. }
  489. static int sb_finish_set_opts(struct super_block *sb)
  490. {
  491. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  492. struct dentry *root = sb->s_root;
  493. struct inode *root_inode = d_backing_inode(root);
  494. int rc = 0;
  495. if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
  496. rc = sb_check_xattr_support(sb);
  497. if (rc)
  498. return rc;
  499. }
  500. sbsec->flags |= SE_SBINITIALIZED;
  501. /*
  502. * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
  503. * leave the flag untouched because sb_clone_mnt_opts might be handing
  504. * us a superblock that needs the flag to be cleared.
  505. */
  506. if (selinux_is_sblabel_mnt(sb))
  507. sbsec->flags |= SBLABEL_MNT;
  508. else
  509. sbsec->flags &= ~SBLABEL_MNT;
  510. /* Initialize the root inode. */
  511. rc = inode_doinit_with_dentry(root_inode, root);
  512. /* Initialize any other inodes associated with the superblock, e.g.
  513. inodes created prior to initial policy load or inodes created
  514. during get_sb by a pseudo filesystem that directly
  515. populates itself. */
  516. spin_lock(&sbsec->isec_lock);
  517. while (!list_empty(&sbsec->isec_head)) {
  518. struct inode_security_struct *isec =
  519. list_first_entry(&sbsec->isec_head,
  520. struct inode_security_struct, list);
  521. struct inode *inode = isec->inode;
  522. list_del_init(&isec->list);
  523. spin_unlock(&sbsec->isec_lock);
  524. inode = igrab(inode);
  525. if (inode) {
  526. if (!IS_PRIVATE(inode))
  527. inode_doinit_with_dentry(inode, NULL);
  528. iput(inode);
  529. }
  530. spin_lock(&sbsec->isec_lock);
  531. }
  532. spin_unlock(&sbsec->isec_lock);
  533. return rc;
  534. }
  535. static int bad_option(struct superblock_security_struct *sbsec, char flag,
  536. u32 old_sid, u32 new_sid)
  537. {
  538. char mnt_flags = sbsec->flags & SE_MNTMASK;
  539. /* check if the old mount command had the same options */
  540. if (sbsec->flags & SE_SBINITIALIZED)
  541. if (!(sbsec->flags & flag) ||
  542. (old_sid != new_sid))
  543. return 1;
  544. /* check if we were passed the same options twice,
  545. * aka someone passed context=a,context=b
  546. */
  547. if (!(sbsec->flags & SE_SBINITIALIZED))
  548. if (mnt_flags & flag)
  549. return 1;
  550. return 0;
  551. }
  552. /*
  553. * Allow filesystems with binary mount data to explicitly set mount point
  554. * labeling information.
  555. */
  556. static int selinux_set_mnt_opts(struct super_block *sb,
  557. void *mnt_opts,
  558. unsigned long kern_flags,
  559. unsigned long *set_kern_flags)
  560. {
  561. const struct cred *cred = current_cred();
  562. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  563. struct dentry *root = sb->s_root;
  564. struct selinux_mnt_opts *opts = mnt_opts;
  565. struct inode_security_struct *root_isec;
  566. u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
  567. u32 defcontext_sid = 0;
  568. int rc = 0;
  569. /*
  570. * Specifying internal flags without providing a place to
  571. * place the results is not allowed
  572. */
  573. if (kern_flags && !set_kern_flags)
  574. return -EINVAL;
  575. mutex_lock(&sbsec->lock);
  576. if (!selinux_initialized()) {
  577. if (!opts) {
  578. /* Defer initialization until selinux_complete_init,
  579. after the initial policy is loaded and the security
  580. server is ready to handle calls. */
  581. if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
  582. sbsec->flags |= SE_SBNATIVE;
  583. *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
  584. }
  585. goto out;
  586. }
  587. rc = -EINVAL;
  588. pr_warn("SELinux: Unable to set superblock options "
  589. "before the security server is initialized\n");
  590. goto out;
  591. }
  592. /*
  593. * Binary mount data FS will come through this function twice. Once
  594. * from an explicit call and once from the generic calls from the vfs.
  595. * Since the generic VFS calls will not contain any security mount data
  596. * we need to skip the double mount verification.
  597. *
  598. * This does open a hole in which we will not notice if the first
  599. * mount using this sb set explicit options and a second mount using
  600. * this sb does not set any security options. (The first options
  601. * will be used for both mounts)
  602. */
  603. if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
  604. && !opts)
  605. goto out;
  606. root_isec = backing_inode_security_novalidate(root);
  607. /*
  608. * parse the mount options, check if they are valid sids.
  609. * also check if someone is trying to mount the same sb more
  610. * than once with different security options.
  611. */
  612. if (opts) {
  613. if (opts->fscontext_sid) {
  614. fscontext_sid = opts->fscontext_sid;
  615. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
  616. fscontext_sid))
  617. goto out_double_mount;
  618. sbsec->flags |= FSCONTEXT_MNT;
  619. }
  620. if (opts->context_sid) {
  621. context_sid = opts->context_sid;
  622. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
  623. context_sid))
  624. goto out_double_mount;
  625. sbsec->flags |= CONTEXT_MNT;
  626. }
  627. if (opts->rootcontext_sid) {
  628. rootcontext_sid = opts->rootcontext_sid;
  629. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
  630. rootcontext_sid))
  631. goto out_double_mount;
  632. sbsec->flags |= ROOTCONTEXT_MNT;
  633. }
  634. if (opts->defcontext_sid) {
  635. defcontext_sid = opts->defcontext_sid;
  636. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
  637. defcontext_sid))
  638. goto out_double_mount;
  639. sbsec->flags |= DEFCONTEXT_MNT;
  640. }
  641. }
  642. if (sbsec->flags & SE_SBINITIALIZED) {
  643. /* previously mounted with options, but not on this attempt? */
  644. if ((sbsec->flags & SE_MNTMASK) && !opts)
  645. goto out_double_mount;
  646. rc = 0;
  647. goto out;
  648. }
  649. if (strcmp(sb->s_type->name, "proc") == 0)
  650. sbsec->flags |= SE_SBPROC | SE_SBGENFS;
  651. if (!strcmp(sb->s_type->name, "debugfs") ||
  652. !strcmp(sb->s_type->name, "tracefs") ||
  653. !strcmp(sb->s_type->name, "binder") ||
  654. !strcmp(sb->s_type->name, "bpf") ||
  655. !strcmp(sb->s_type->name, "pstore") ||
  656. !strcmp(sb->s_type->name, "securityfs"))
  657. sbsec->flags |= SE_SBGENFS;
  658. if (!strcmp(sb->s_type->name, "sysfs") ||
  659. !strcmp(sb->s_type->name, "cgroup") ||
  660. !strcmp(sb->s_type->name, "cgroup2"))
  661. sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
  662. if (!sbsec->behavior) {
  663. /*
  664. * Determine the labeling behavior to use for this
  665. * filesystem type.
  666. */
  667. rc = security_fs_use(sb);
  668. if (rc) {
  669. pr_warn("%s: security_fs_use(%s) returned %d\n",
  670. __func__, sb->s_type->name, rc);
  671. goto out;
  672. }
  673. }
  674. /*
  675. * If this is a user namespace mount and the filesystem type is not
  676. * explicitly whitelisted, then no contexts are allowed on the command
  677. * line and security labels must be ignored.
  678. */
  679. if (sb->s_user_ns != &init_user_ns &&
  680. strcmp(sb->s_type->name, "tmpfs") &&
  681. strcmp(sb->s_type->name, "ramfs") &&
  682. strcmp(sb->s_type->name, "devpts") &&
  683. strcmp(sb->s_type->name, "overlay")) {
  684. if (context_sid || fscontext_sid || rootcontext_sid ||
  685. defcontext_sid) {
  686. rc = -EACCES;
  687. goto out;
  688. }
  689. if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
  690. sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
  691. rc = security_transition_sid(current_sid(),
  692. current_sid(),
  693. SECCLASS_FILE, NULL,
  694. &sbsec->mntpoint_sid);
  695. if (rc)
  696. goto out;
  697. }
  698. goto out_set_opts;
  699. }
  700. /* sets the context of the superblock for the fs being mounted. */
  701. if (fscontext_sid) {
  702. rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
  703. if (rc)
  704. goto out;
  705. sbsec->sid = fscontext_sid;
  706. }
  707. /*
  708. * Switch to using mount point labeling behavior.
  709. * sets the label used on all file below the mountpoint, and will set
  710. * the superblock context if not already set.
  711. */
  712. if (sbsec->flags & SE_SBNATIVE) {
  713. /*
  714. * This means we are initializing a superblock that has been
  715. * mounted before the SELinux was initialized and the
  716. * filesystem requested native labeling. We had already
  717. * returned SECURITY_LSM_NATIVE_LABELS in *set_kern_flags
  718. * in the original mount attempt, so now we just need to set
  719. * the SECURITY_FS_USE_NATIVE behavior.
  720. */
  721. sbsec->behavior = SECURITY_FS_USE_NATIVE;
  722. } else if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
  723. sbsec->behavior = SECURITY_FS_USE_NATIVE;
  724. *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
  725. }
  726. if (context_sid) {
  727. if (!fscontext_sid) {
  728. rc = may_context_mount_sb_relabel(context_sid, sbsec,
  729. cred);
  730. if (rc)
  731. goto out;
  732. sbsec->sid = context_sid;
  733. } else {
  734. rc = may_context_mount_inode_relabel(context_sid, sbsec,
  735. cred);
  736. if (rc)
  737. goto out;
  738. }
  739. if (!rootcontext_sid)
  740. rootcontext_sid = context_sid;
  741. sbsec->mntpoint_sid = context_sid;
  742. sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
  743. }
  744. if (rootcontext_sid) {
  745. rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
  746. cred);
  747. if (rc)
  748. goto out;
  749. root_isec->sid = rootcontext_sid;
  750. root_isec->initialized = LABEL_INITIALIZED;
  751. }
  752. if (defcontext_sid) {
  753. if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
  754. sbsec->behavior != SECURITY_FS_USE_NATIVE) {
  755. rc = -EINVAL;
  756. pr_warn("SELinux: defcontext option is "
  757. "invalid for this filesystem type\n");
  758. goto out;
  759. }
  760. if (defcontext_sid != sbsec->def_sid) {
  761. rc = may_context_mount_inode_relabel(defcontext_sid,
  762. sbsec, cred);
  763. if (rc)
  764. goto out;
  765. }
  766. sbsec->def_sid = defcontext_sid;
  767. }
  768. out_set_opts:
  769. rc = sb_finish_set_opts(sb);
  770. out:
  771. mutex_unlock(&sbsec->lock);
  772. return rc;
  773. out_double_mount:
  774. rc = -EINVAL;
  775. pr_warn("SELinux: mount invalid. Same superblock, different "
  776. "security settings for (dev %s, type %s)\n", sb->s_id,
  777. sb->s_type->name);
  778. goto out;
  779. }
  780. static int selinux_cmp_sb_context(const struct super_block *oldsb,
  781. const struct super_block *newsb)
  782. {
  783. struct superblock_security_struct *old = selinux_superblock(oldsb);
  784. struct superblock_security_struct *new = selinux_superblock(newsb);
  785. char oldflags = old->flags & SE_MNTMASK;
  786. char newflags = new->flags & SE_MNTMASK;
  787. if (oldflags != newflags)
  788. goto mismatch;
  789. if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
  790. goto mismatch;
  791. if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
  792. goto mismatch;
  793. if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
  794. goto mismatch;
  795. if (oldflags & ROOTCONTEXT_MNT) {
  796. struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
  797. struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
  798. if (oldroot->sid != newroot->sid)
  799. goto mismatch;
  800. }
  801. return 0;
  802. mismatch:
  803. pr_warn("SELinux: mount invalid. Same superblock, "
  804. "different security settings for (dev %s, "
  805. "type %s)\n", newsb->s_id, newsb->s_type->name);
  806. return -EBUSY;
  807. }
  808. static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
  809. struct super_block *newsb,
  810. unsigned long kern_flags,
  811. unsigned long *set_kern_flags)
  812. {
  813. int rc = 0;
  814. const struct superblock_security_struct *oldsbsec =
  815. selinux_superblock(oldsb);
  816. struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
  817. int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
  818. int set_context = (oldsbsec->flags & CONTEXT_MNT);
  819. int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
  820. /*
  821. * Specifying internal flags without providing a place to
  822. * place the results is not allowed.
  823. */
  824. if (kern_flags && !set_kern_flags)
  825. return -EINVAL;
  826. mutex_lock(&newsbsec->lock);
  827. /*
  828. * if the parent was able to be mounted it clearly had no special lsm
  829. * mount options. thus we can safely deal with this superblock later
  830. */
  831. if (!selinux_initialized()) {
  832. if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
  833. newsbsec->flags |= SE_SBNATIVE;
  834. *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
  835. }
  836. goto out;
  837. }
  838. /* how can we clone if the old one wasn't set up?? */
  839. BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
  840. /* if fs is reusing a sb, make sure that the contexts match */
  841. if (newsbsec->flags & SE_SBINITIALIZED) {
  842. mutex_unlock(&newsbsec->lock);
  843. if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
  844. *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
  845. return selinux_cmp_sb_context(oldsb, newsb);
  846. }
  847. newsbsec->flags = oldsbsec->flags;
  848. newsbsec->sid = oldsbsec->sid;
  849. newsbsec->def_sid = oldsbsec->def_sid;
  850. newsbsec->behavior = oldsbsec->behavior;
  851. if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
  852. !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
  853. rc = security_fs_use(newsb);
  854. if (rc)
  855. goto out;
  856. }
  857. if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
  858. newsbsec->behavior = SECURITY_FS_USE_NATIVE;
  859. *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
  860. }
  861. if (set_context) {
  862. u32 sid = oldsbsec->mntpoint_sid;
  863. if (!set_fscontext)
  864. newsbsec->sid = sid;
  865. if (!set_rootcontext) {
  866. struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
  867. newisec->sid = sid;
  868. }
  869. newsbsec->mntpoint_sid = sid;
  870. }
  871. if (set_rootcontext) {
  872. const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
  873. struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
  874. newisec->sid = oldisec->sid;
  875. }
  876. sb_finish_set_opts(newsb);
  877. out:
  878. mutex_unlock(&newsbsec->lock);
  879. return rc;
  880. }
  881. /*
  882. * NOTE: the caller is responsible for freeing the memory even if on error.
  883. */
  884. static int selinux_add_opt(int token, const char *s, void **mnt_opts)
  885. {
  886. struct selinux_mnt_opts *opts = *mnt_opts;
  887. u32 *dst_sid;
  888. int rc;
  889. if (token == Opt_seclabel)
  890. /* eaten and completely ignored */
  891. return 0;
  892. if (!s)
  893. return -EINVAL;
  894. if (!selinux_initialized()) {
  895. pr_warn("SELinux: Unable to set superblock options before the security server is initialized\n");
  896. return -EINVAL;
  897. }
  898. if (!opts) {
  899. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  900. if (!opts)
  901. return -ENOMEM;
  902. *mnt_opts = opts;
  903. }
  904. switch (token) {
  905. case Opt_context:
  906. if (opts->context_sid || opts->defcontext_sid)
  907. goto err;
  908. dst_sid = &opts->context_sid;
  909. break;
  910. case Opt_fscontext:
  911. if (opts->fscontext_sid)
  912. goto err;
  913. dst_sid = &opts->fscontext_sid;
  914. break;
  915. case Opt_rootcontext:
  916. if (opts->rootcontext_sid)
  917. goto err;
  918. dst_sid = &opts->rootcontext_sid;
  919. break;
  920. case Opt_defcontext:
  921. if (opts->context_sid || opts->defcontext_sid)
  922. goto err;
  923. dst_sid = &opts->defcontext_sid;
  924. break;
  925. default:
  926. WARN_ON(1);
  927. return -EINVAL;
  928. }
  929. rc = security_context_str_to_sid(s, dst_sid, GFP_KERNEL);
  930. if (rc)
  931. pr_warn("SELinux: security_context_str_to_sid (%s) failed with errno=%d\n",
  932. s, rc);
  933. return rc;
  934. err:
  935. pr_warn(SEL_MOUNT_FAIL_MSG);
  936. return -EINVAL;
  937. }
  938. static int show_sid(struct seq_file *m, u32 sid)
  939. {
  940. char *context = NULL;
  941. u32 len;
  942. int rc;
  943. rc = security_sid_to_context(sid, &context, &len);
  944. if (!rc) {
  945. bool has_comma = strchr(context, ',');
  946. seq_putc(m, '=');
  947. if (has_comma)
  948. seq_putc(m, '\"');
  949. seq_escape(m, context, "\"\n\\");
  950. if (has_comma)
  951. seq_putc(m, '\"');
  952. }
  953. kfree(context);
  954. return rc;
  955. }
  956. static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
  957. {
  958. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  959. int rc;
  960. if (!(sbsec->flags & SE_SBINITIALIZED))
  961. return 0;
  962. if (!selinux_initialized())
  963. return 0;
  964. if (sbsec->flags & FSCONTEXT_MNT) {
  965. seq_putc(m, ',');
  966. seq_puts(m, FSCONTEXT_STR);
  967. rc = show_sid(m, sbsec->sid);
  968. if (rc)
  969. return rc;
  970. }
  971. if (sbsec->flags & CONTEXT_MNT) {
  972. seq_putc(m, ',');
  973. seq_puts(m, CONTEXT_STR);
  974. rc = show_sid(m, sbsec->mntpoint_sid);
  975. if (rc)
  976. return rc;
  977. }
  978. if (sbsec->flags & DEFCONTEXT_MNT) {
  979. seq_putc(m, ',');
  980. seq_puts(m, DEFCONTEXT_STR);
  981. rc = show_sid(m, sbsec->def_sid);
  982. if (rc)
  983. return rc;
  984. }
  985. if (sbsec->flags & ROOTCONTEXT_MNT) {
  986. struct dentry *root = sb->s_root;
  987. struct inode_security_struct *isec = backing_inode_security(root);
  988. seq_putc(m, ',');
  989. seq_puts(m, ROOTCONTEXT_STR);
  990. rc = show_sid(m, isec->sid);
  991. if (rc)
  992. return rc;
  993. }
  994. if (sbsec->flags & SBLABEL_MNT) {
  995. seq_putc(m, ',');
  996. seq_puts(m, SECLABEL_STR);
  997. }
  998. return 0;
  999. }
  1000. static inline u16 inode_mode_to_security_class(umode_t mode)
  1001. {
  1002. switch (mode & S_IFMT) {
  1003. case S_IFSOCK:
  1004. return SECCLASS_SOCK_FILE;
  1005. case S_IFLNK:
  1006. return SECCLASS_LNK_FILE;
  1007. case S_IFREG:
  1008. return SECCLASS_FILE;
  1009. case S_IFBLK:
  1010. return SECCLASS_BLK_FILE;
  1011. case S_IFDIR:
  1012. return SECCLASS_DIR;
  1013. case S_IFCHR:
  1014. return SECCLASS_CHR_FILE;
  1015. case S_IFIFO:
  1016. return SECCLASS_FIFO_FILE;
  1017. }
  1018. return SECCLASS_FILE;
  1019. }
  1020. static inline int default_protocol_stream(int protocol)
  1021. {
  1022. return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
  1023. protocol == IPPROTO_MPTCP);
  1024. }
  1025. static inline int default_protocol_dgram(int protocol)
  1026. {
  1027. return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
  1028. }
  1029. static inline u16 socket_type_to_security_class(int family, int type, int protocol)
  1030. {
  1031. bool extsockclass = selinux_policycap_extsockclass();
  1032. switch (family) {
  1033. case PF_UNIX:
  1034. switch (type) {
  1035. case SOCK_STREAM:
  1036. case SOCK_SEQPACKET:
  1037. return SECCLASS_UNIX_STREAM_SOCKET;
  1038. case SOCK_DGRAM:
  1039. case SOCK_RAW:
  1040. return SECCLASS_UNIX_DGRAM_SOCKET;
  1041. }
  1042. break;
  1043. case PF_INET:
  1044. case PF_INET6:
  1045. switch (type) {
  1046. case SOCK_STREAM:
  1047. case SOCK_SEQPACKET:
  1048. if (default_protocol_stream(protocol))
  1049. return SECCLASS_TCP_SOCKET;
  1050. else if (extsockclass && protocol == IPPROTO_SCTP)
  1051. return SECCLASS_SCTP_SOCKET;
  1052. else
  1053. return SECCLASS_RAWIP_SOCKET;
  1054. case SOCK_DGRAM:
  1055. if (default_protocol_dgram(protocol))
  1056. return SECCLASS_UDP_SOCKET;
  1057. else if (extsockclass && (protocol == IPPROTO_ICMP ||
  1058. protocol == IPPROTO_ICMPV6))
  1059. return SECCLASS_ICMP_SOCKET;
  1060. else
  1061. return SECCLASS_RAWIP_SOCKET;
  1062. case SOCK_DCCP:
  1063. return SECCLASS_DCCP_SOCKET;
  1064. default:
  1065. return SECCLASS_RAWIP_SOCKET;
  1066. }
  1067. break;
  1068. case PF_NETLINK:
  1069. switch (protocol) {
  1070. case NETLINK_ROUTE:
  1071. return SECCLASS_NETLINK_ROUTE_SOCKET;
  1072. case NETLINK_SOCK_DIAG:
  1073. return SECCLASS_NETLINK_TCPDIAG_SOCKET;
  1074. case NETLINK_NFLOG:
  1075. return SECCLASS_NETLINK_NFLOG_SOCKET;
  1076. case NETLINK_XFRM:
  1077. return SECCLASS_NETLINK_XFRM_SOCKET;
  1078. case NETLINK_SELINUX:
  1079. return SECCLASS_NETLINK_SELINUX_SOCKET;
  1080. case NETLINK_ISCSI:
  1081. return SECCLASS_NETLINK_ISCSI_SOCKET;
  1082. case NETLINK_AUDIT:
  1083. return SECCLASS_NETLINK_AUDIT_SOCKET;
  1084. case NETLINK_FIB_LOOKUP:
  1085. return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
  1086. case NETLINK_CONNECTOR:
  1087. return SECCLASS_NETLINK_CONNECTOR_SOCKET;
  1088. case NETLINK_NETFILTER:
  1089. return SECCLASS_NETLINK_NETFILTER_SOCKET;
  1090. case NETLINK_DNRTMSG:
  1091. return SECCLASS_NETLINK_DNRT_SOCKET;
  1092. case NETLINK_KOBJECT_UEVENT:
  1093. return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
  1094. case NETLINK_GENERIC:
  1095. return SECCLASS_NETLINK_GENERIC_SOCKET;
  1096. case NETLINK_SCSITRANSPORT:
  1097. return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
  1098. case NETLINK_RDMA:
  1099. return SECCLASS_NETLINK_RDMA_SOCKET;
  1100. case NETLINK_CRYPTO:
  1101. return SECCLASS_NETLINK_CRYPTO_SOCKET;
  1102. default:
  1103. return SECCLASS_NETLINK_SOCKET;
  1104. }
  1105. case PF_PACKET:
  1106. return SECCLASS_PACKET_SOCKET;
  1107. case PF_KEY:
  1108. return SECCLASS_KEY_SOCKET;
  1109. case PF_APPLETALK:
  1110. return SECCLASS_APPLETALK_SOCKET;
  1111. }
  1112. if (extsockclass) {
  1113. switch (family) {
  1114. case PF_AX25:
  1115. return SECCLASS_AX25_SOCKET;
  1116. case PF_IPX:
  1117. return SECCLASS_IPX_SOCKET;
  1118. case PF_NETROM:
  1119. return SECCLASS_NETROM_SOCKET;
  1120. case PF_ATMPVC:
  1121. return SECCLASS_ATMPVC_SOCKET;
  1122. case PF_X25:
  1123. return SECCLASS_X25_SOCKET;
  1124. case PF_ROSE:
  1125. return SECCLASS_ROSE_SOCKET;
  1126. case PF_DECnet:
  1127. return SECCLASS_DECNET_SOCKET;
  1128. case PF_ATMSVC:
  1129. return SECCLASS_ATMSVC_SOCKET;
  1130. case PF_RDS:
  1131. return SECCLASS_RDS_SOCKET;
  1132. case PF_IRDA:
  1133. return SECCLASS_IRDA_SOCKET;
  1134. case PF_PPPOX:
  1135. return SECCLASS_PPPOX_SOCKET;
  1136. case PF_LLC:
  1137. return SECCLASS_LLC_SOCKET;
  1138. case PF_CAN:
  1139. return SECCLASS_CAN_SOCKET;
  1140. case PF_TIPC:
  1141. return SECCLASS_TIPC_SOCKET;
  1142. case PF_BLUETOOTH:
  1143. return SECCLASS_BLUETOOTH_SOCKET;
  1144. case PF_IUCV:
  1145. return SECCLASS_IUCV_SOCKET;
  1146. case PF_RXRPC:
  1147. return SECCLASS_RXRPC_SOCKET;
  1148. case PF_ISDN:
  1149. return SECCLASS_ISDN_SOCKET;
  1150. case PF_PHONET:
  1151. return SECCLASS_PHONET_SOCKET;
  1152. case PF_IEEE802154:
  1153. return SECCLASS_IEEE802154_SOCKET;
  1154. case PF_CAIF:
  1155. return SECCLASS_CAIF_SOCKET;
  1156. case PF_ALG:
  1157. return SECCLASS_ALG_SOCKET;
  1158. case PF_NFC:
  1159. return SECCLASS_NFC_SOCKET;
  1160. case PF_VSOCK:
  1161. return SECCLASS_VSOCK_SOCKET;
  1162. case PF_KCM:
  1163. return SECCLASS_KCM_SOCKET;
  1164. case PF_QIPCRTR:
  1165. return SECCLASS_QIPCRTR_SOCKET;
  1166. case PF_SMC:
  1167. return SECCLASS_SMC_SOCKET;
  1168. case PF_XDP:
  1169. return SECCLASS_XDP_SOCKET;
  1170. case PF_MCTP:
  1171. return SECCLASS_MCTP_SOCKET;
  1172. #if PF_MAX > 46
  1173. #error New address family defined, please update this function.
  1174. #endif
  1175. }
  1176. }
  1177. return SECCLASS_SOCKET;
  1178. }
  1179. static int selinux_genfs_get_sid(struct dentry *dentry,
  1180. u16 tclass,
  1181. u16 flags,
  1182. u32 *sid)
  1183. {
  1184. int rc;
  1185. struct super_block *sb = dentry->d_sb;
  1186. char *buffer, *path;
  1187. buffer = (char *)__get_free_page(GFP_KERNEL);
  1188. if (!buffer)
  1189. return -ENOMEM;
  1190. path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
  1191. if (IS_ERR(path))
  1192. rc = PTR_ERR(path);
  1193. else {
  1194. if (flags & SE_SBPROC) {
  1195. /* each process gets a /proc/PID/ entry. Strip off the
  1196. * PID part to get a valid selinux labeling.
  1197. * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
  1198. while (path[1] >= '0' && path[1] <= '9') {
  1199. path[1] = '/';
  1200. path++;
  1201. }
  1202. }
  1203. rc = security_genfs_sid(sb->s_type->name,
  1204. path, tclass, sid);
  1205. if (rc == -ENOENT) {
  1206. /* No match in policy, mark as unlabeled. */
  1207. *sid = SECINITSID_UNLABELED;
  1208. rc = 0;
  1209. }
  1210. }
  1211. free_page((unsigned long)buffer);
  1212. return rc;
  1213. }
  1214. static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
  1215. u32 def_sid, u32 *sid)
  1216. {
  1217. #define INITCONTEXTLEN 255
  1218. char *context;
  1219. unsigned int len;
  1220. int rc;
  1221. len = INITCONTEXTLEN;
  1222. context = kmalloc(len + 1, GFP_NOFS);
  1223. if (!context)
  1224. return -ENOMEM;
  1225. context[len] = '\0';
  1226. rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
  1227. if (rc == -ERANGE) {
  1228. kfree(context);
  1229. /* Need a larger buffer. Query for the right size. */
  1230. rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
  1231. if (rc < 0)
  1232. return rc;
  1233. len = rc;
  1234. context = kmalloc(len + 1, GFP_NOFS);
  1235. if (!context)
  1236. return -ENOMEM;
  1237. context[len] = '\0';
  1238. rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
  1239. context, len);
  1240. }
  1241. if (rc < 0) {
  1242. kfree(context);
  1243. if (rc != -ENODATA) {
  1244. pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
  1245. __func__, -rc, inode->i_sb->s_id, inode->i_ino);
  1246. return rc;
  1247. }
  1248. *sid = def_sid;
  1249. return 0;
  1250. }
  1251. rc = security_context_to_sid_default(context, rc, sid,
  1252. def_sid, GFP_NOFS);
  1253. if (rc) {
  1254. char *dev = inode->i_sb->s_id;
  1255. unsigned long ino = inode->i_ino;
  1256. if (rc == -EINVAL) {
  1257. pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n",
  1258. ino, dev, context);
  1259. } else {
  1260. pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
  1261. __func__, context, -rc, dev, ino);
  1262. }
  1263. }
  1264. kfree(context);
  1265. return 0;
  1266. }
  1267. /* The inode's security attributes must be initialized before first use. */
  1268. static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
  1269. {
  1270. struct superblock_security_struct *sbsec = NULL;
  1271. struct inode_security_struct *isec = selinux_inode(inode);
  1272. u32 task_sid, sid = 0;
  1273. u16 sclass;
  1274. struct dentry *dentry;
  1275. int rc = 0;
  1276. if (isec->initialized == LABEL_INITIALIZED)
  1277. return 0;
  1278. spin_lock(&isec->lock);
  1279. if (isec->initialized == LABEL_INITIALIZED)
  1280. goto out_unlock;
  1281. if (isec->sclass == SECCLASS_FILE)
  1282. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  1283. sbsec = selinux_superblock(inode->i_sb);
  1284. if (!(sbsec->flags & SE_SBINITIALIZED)) {
  1285. /* Defer initialization until selinux_complete_init,
  1286. after the initial policy is loaded and the security
  1287. server is ready to handle calls. */
  1288. spin_lock(&sbsec->isec_lock);
  1289. if (list_empty(&isec->list))
  1290. list_add(&isec->list, &sbsec->isec_head);
  1291. spin_unlock(&sbsec->isec_lock);
  1292. goto out_unlock;
  1293. }
  1294. sclass = isec->sclass;
  1295. task_sid = isec->task_sid;
  1296. sid = isec->sid;
  1297. isec->initialized = LABEL_PENDING;
  1298. spin_unlock(&isec->lock);
  1299. switch (sbsec->behavior) {
  1300. /*
  1301. * In case of SECURITY_FS_USE_NATIVE we need to re-fetch the labels
  1302. * via xattr when called from delayed_superblock_init().
  1303. */
  1304. case SECURITY_FS_USE_NATIVE:
  1305. case SECURITY_FS_USE_XATTR:
  1306. if (!(inode->i_opflags & IOP_XATTR)) {
  1307. sid = sbsec->def_sid;
  1308. break;
  1309. }
  1310. /* Need a dentry, since the xattr API requires one.
  1311. Life would be simpler if we could just pass the inode. */
  1312. if (opt_dentry) {
  1313. /* Called from d_instantiate or d_splice_alias. */
  1314. dentry = dget(opt_dentry);
  1315. } else {
  1316. /*
  1317. * Called from selinux_complete_init, try to find a dentry.
  1318. * Some filesystems really want a connected one, so try
  1319. * that first. We could split SECURITY_FS_USE_XATTR in
  1320. * two, depending upon that...
  1321. */
  1322. dentry = d_find_alias(inode);
  1323. if (!dentry)
  1324. dentry = d_find_any_alias(inode);
  1325. }
  1326. if (!dentry) {
  1327. /*
  1328. * this is can be hit on boot when a file is accessed
  1329. * before the policy is loaded. When we load policy we
  1330. * may find inodes that have no dentry on the
  1331. * sbsec->isec_head list. No reason to complain as these
  1332. * will get fixed up the next time we go through
  1333. * inode_doinit with a dentry, before these inodes could
  1334. * be used again by userspace.
  1335. */
  1336. goto out_invalid;
  1337. }
  1338. rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
  1339. &sid);
  1340. dput(dentry);
  1341. if (rc)
  1342. goto out;
  1343. break;
  1344. case SECURITY_FS_USE_TASK:
  1345. sid = task_sid;
  1346. break;
  1347. case SECURITY_FS_USE_TRANS:
  1348. /* Default to the fs SID. */
  1349. sid = sbsec->sid;
  1350. /* Try to obtain a transition SID. */
  1351. rc = security_transition_sid(task_sid, sid,
  1352. sclass, NULL, &sid);
  1353. if (rc)
  1354. goto out;
  1355. break;
  1356. case SECURITY_FS_USE_MNTPOINT:
  1357. sid = sbsec->mntpoint_sid;
  1358. break;
  1359. default:
  1360. /* Default to the fs superblock SID. */
  1361. sid = sbsec->sid;
  1362. if ((sbsec->flags & SE_SBGENFS) &&
  1363. (!S_ISLNK(inode->i_mode) ||
  1364. selinux_policycap_genfs_seclabel_symlinks())) {
  1365. /* We must have a dentry to determine the label on
  1366. * procfs inodes */
  1367. if (opt_dentry) {
  1368. /* Called from d_instantiate or
  1369. * d_splice_alias. */
  1370. dentry = dget(opt_dentry);
  1371. } else {
  1372. /* Called from selinux_complete_init, try to
  1373. * find a dentry. Some filesystems really want
  1374. * a connected one, so try that first.
  1375. */
  1376. dentry = d_find_alias(inode);
  1377. if (!dentry)
  1378. dentry = d_find_any_alias(inode);
  1379. }
  1380. /*
  1381. * This can be hit on boot when a file is accessed
  1382. * before the policy is loaded. When we load policy we
  1383. * may find inodes that have no dentry on the
  1384. * sbsec->isec_head list. No reason to complain as
  1385. * these will get fixed up the next time we go through
  1386. * inode_doinit() with a dentry, before these inodes
  1387. * could be used again by userspace.
  1388. */
  1389. if (!dentry)
  1390. goto out_invalid;
  1391. rc = selinux_genfs_get_sid(dentry, sclass,
  1392. sbsec->flags, &sid);
  1393. if (rc) {
  1394. dput(dentry);
  1395. goto out;
  1396. }
  1397. if ((sbsec->flags & SE_SBGENFS_XATTR) &&
  1398. (inode->i_opflags & IOP_XATTR)) {
  1399. rc = inode_doinit_use_xattr(inode, dentry,
  1400. sid, &sid);
  1401. if (rc) {
  1402. dput(dentry);
  1403. goto out;
  1404. }
  1405. }
  1406. dput(dentry);
  1407. }
  1408. break;
  1409. }
  1410. out:
  1411. spin_lock(&isec->lock);
  1412. if (isec->initialized == LABEL_PENDING) {
  1413. if (rc) {
  1414. isec->initialized = LABEL_INVALID;
  1415. goto out_unlock;
  1416. }
  1417. isec->initialized = LABEL_INITIALIZED;
  1418. isec->sid = sid;
  1419. }
  1420. out_unlock:
  1421. spin_unlock(&isec->lock);
  1422. return rc;
  1423. out_invalid:
  1424. spin_lock(&isec->lock);
  1425. if (isec->initialized == LABEL_PENDING) {
  1426. isec->initialized = LABEL_INVALID;
  1427. isec->sid = sid;
  1428. }
  1429. spin_unlock(&isec->lock);
  1430. return 0;
  1431. }
  1432. /* Convert a Linux signal to an access vector. */
  1433. static inline u32 signal_to_av(int sig)
  1434. {
  1435. u32 perm = 0;
  1436. switch (sig) {
  1437. case SIGCHLD:
  1438. /* Commonly granted from child to parent. */
  1439. perm = PROCESS__SIGCHLD;
  1440. break;
  1441. case SIGKILL:
  1442. /* Cannot be caught or ignored */
  1443. perm = PROCESS__SIGKILL;
  1444. break;
  1445. case SIGSTOP:
  1446. /* Cannot be caught or ignored */
  1447. perm = PROCESS__SIGSTOP;
  1448. break;
  1449. default:
  1450. /* All other signals. */
  1451. perm = PROCESS__SIGNAL;
  1452. break;
  1453. }
  1454. return perm;
  1455. }
  1456. #if CAP_LAST_CAP > 63
  1457. #error Fix SELinux to handle capabilities > 63.
  1458. #endif
  1459. /* Check whether a task is allowed to use a capability. */
  1460. static int cred_has_capability(const struct cred *cred,
  1461. int cap, unsigned int opts, bool initns)
  1462. {
  1463. struct common_audit_data ad;
  1464. struct av_decision avd;
  1465. u16 sclass;
  1466. u32 sid = cred_sid(cred);
  1467. u32 av = CAP_TO_MASK(cap);
  1468. int rc;
  1469. ad.type = LSM_AUDIT_DATA_CAP;
  1470. ad.u.cap = cap;
  1471. switch (CAP_TO_INDEX(cap)) {
  1472. case 0:
  1473. sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
  1474. break;
  1475. case 1:
  1476. sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
  1477. break;
  1478. default:
  1479. pr_err("SELinux: out of range capability %d\n", cap);
  1480. BUG();
  1481. return -EINVAL;
  1482. }
  1483. rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
  1484. if (!(opts & CAP_OPT_NOAUDIT)) {
  1485. int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
  1486. if (rc2)
  1487. return rc2;
  1488. }
  1489. return rc;
  1490. }
  1491. /* Check whether a task has a particular permission to an inode.
  1492. The 'adp' parameter is optional and allows other audit
  1493. data to be passed (e.g. the dentry). */
  1494. static int inode_has_perm(const struct cred *cred,
  1495. struct inode *inode,
  1496. u32 perms,
  1497. struct common_audit_data *adp)
  1498. {
  1499. struct inode_security_struct *isec;
  1500. u32 sid;
  1501. if (unlikely(IS_PRIVATE(inode)))
  1502. return 0;
  1503. sid = cred_sid(cred);
  1504. isec = selinux_inode(inode);
  1505. return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
  1506. }
  1507. /* Same as inode_has_perm, but pass explicit audit data containing
  1508. the dentry to help the auditing code to more easily generate the
  1509. pathname if needed. */
  1510. static inline int dentry_has_perm(const struct cred *cred,
  1511. struct dentry *dentry,
  1512. u32 av)
  1513. {
  1514. struct inode *inode = d_backing_inode(dentry);
  1515. struct common_audit_data ad;
  1516. ad.type = LSM_AUDIT_DATA_DENTRY;
  1517. ad.u.dentry = dentry;
  1518. __inode_security_revalidate(inode, dentry, true);
  1519. return inode_has_perm(cred, inode, av, &ad);
  1520. }
  1521. /* Same as inode_has_perm, but pass explicit audit data containing
  1522. the path to help the auditing code to more easily generate the
  1523. pathname if needed. */
  1524. static inline int path_has_perm(const struct cred *cred,
  1525. const struct path *path,
  1526. u32 av)
  1527. {
  1528. struct inode *inode = d_backing_inode(path->dentry);
  1529. struct common_audit_data ad;
  1530. ad.type = LSM_AUDIT_DATA_PATH;
  1531. ad.u.path = *path;
  1532. __inode_security_revalidate(inode, path->dentry, true);
  1533. return inode_has_perm(cred, inode, av, &ad);
  1534. }
  1535. /* Same as path_has_perm, but uses the inode from the file struct. */
  1536. static inline int file_path_has_perm(const struct cred *cred,
  1537. struct file *file,
  1538. u32 av)
  1539. {
  1540. struct common_audit_data ad;
  1541. ad.type = LSM_AUDIT_DATA_FILE;
  1542. ad.u.file = file;
  1543. return inode_has_perm(cred, file_inode(file), av, &ad);
  1544. }
  1545. #ifdef CONFIG_BPF_SYSCALL
  1546. static int bpf_fd_pass(const struct file *file, u32 sid);
  1547. #endif
  1548. /* Check whether a task can use an open file descriptor to
  1549. access an inode in a given way. Check access to the
  1550. descriptor itself, and then use dentry_has_perm to
  1551. check a particular permission to the file.
  1552. Access to the descriptor is implicitly granted if it
  1553. has the same SID as the process. If av is zero, then
  1554. access to the file is not checked, e.g. for cases
  1555. where only the descriptor is affected like seek. */
  1556. static int file_has_perm(const struct cred *cred,
  1557. struct file *file,
  1558. u32 av)
  1559. {
  1560. struct file_security_struct *fsec = selinux_file(file);
  1561. struct inode *inode = file_inode(file);
  1562. struct common_audit_data ad;
  1563. u32 sid = cred_sid(cred);
  1564. int rc;
  1565. ad.type = LSM_AUDIT_DATA_FILE;
  1566. ad.u.file = file;
  1567. if (sid != fsec->sid) {
  1568. rc = avc_has_perm(sid, fsec->sid,
  1569. SECCLASS_FD,
  1570. FD__USE,
  1571. &ad);
  1572. if (rc)
  1573. goto out;
  1574. }
  1575. #ifdef CONFIG_BPF_SYSCALL
  1576. rc = bpf_fd_pass(file, cred_sid(cred));
  1577. if (rc)
  1578. return rc;
  1579. #endif
  1580. /* av is zero if only checking access to the descriptor. */
  1581. rc = 0;
  1582. if (av)
  1583. rc = inode_has_perm(cred, inode, av, &ad);
  1584. out:
  1585. return rc;
  1586. }
  1587. /*
  1588. * Determine the label for an inode that might be unioned.
  1589. */
  1590. static int
  1591. selinux_determine_inode_label(const struct task_security_struct *tsec,
  1592. struct inode *dir,
  1593. const struct qstr *name, u16 tclass,
  1594. u32 *_new_isid)
  1595. {
  1596. const struct superblock_security_struct *sbsec =
  1597. selinux_superblock(dir->i_sb);
  1598. if ((sbsec->flags & SE_SBINITIALIZED) &&
  1599. (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
  1600. *_new_isid = sbsec->mntpoint_sid;
  1601. } else if ((sbsec->flags & SBLABEL_MNT) &&
  1602. tsec->create_sid) {
  1603. *_new_isid = tsec->create_sid;
  1604. } else {
  1605. const struct inode_security_struct *dsec = inode_security(dir);
  1606. return security_transition_sid(tsec->sid,
  1607. dsec->sid, tclass,
  1608. name, _new_isid);
  1609. }
  1610. return 0;
  1611. }
  1612. /* Check whether a task can create a file. */
  1613. static int may_create(struct inode *dir,
  1614. struct dentry *dentry,
  1615. u16 tclass)
  1616. {
  1617. const struct task_security_struct *tsec = selinux_cred(current_cred());
  1618. struct inode_security_struct *dsec;
  1619. struct superblock_security_struct *sbsec;
  1620. u32 sid, newsid;
  1621. struct common_audit_data ad;
  1622. int rc;
  1623. dsec = inode_security(dir);
  1624. sbsec = selinux_superblock(dir->i_sb);
  1625. sid = tsec->sid;
  1626. ad.type = LSM_AUDIT_DATA_DENTRY;
  1627. ad.u.dentry = dentry;
  1628. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
  1629. DIR__ADD_NAME | DIR__SEARCH,
  1630. &ad);
  1631. if (rc)
  1632. return rc;
  1633. rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
  1634. &newsid);
  1635. if (rc)
  1636. return rc;
  1637. rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
  1638. if (rc)
  1639. return rc;
  1640. return avc_has_perm(newsid, sbsec->sid,
  1641. SECCLASS_FILESYSTEM,
  1642. FILESYSTEM__ASSOCIATE, &ad);
  1643. }
  1644. #define MAY_LINK 0
  1645. #define MAY_UNLINK 1
  1646. #define MAY_RMDIR 2
  1647. /* Check whether a task can link, unlink, or rmdir a file/directory. */
  1648. static int may_link(struct inode *dir,
  1649. struct dentry *dentry,
  1650. int kind)
  1651. {
  1652. struct inode_security_struct *dsec, *isec;
  1653. struct common_audit_data ad;
  1654. u32 sid = current_sid();
  1655. u32 av;
  1656. int rc;
  1657. dsec = inode_security(dir);
  1658. isec = backing_inode_security(dentry);
  1659. ad.type = LSM_AUDIT_DATA_DENTRY;
  1660. ad.u.dentry = dentry;
  1661. av = DIR__SEARCH;
  1662. av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
  1663. rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
  1664. if (rc)
  1665. return rc;
  1666. switch (kind) {
  1667. case MAY_LINK:
  1668. av = FILE__LINK;
  1669. break;
  1670. case MAY_UNLINK:
  1671. av = FILE__UNLINK;
  1672. break;
  1673. case MAY_RMDIR:
  1674. av = DIR__RMDIR;
  1675. break;
  1676. default:
  1677. pr_warn("SELinux: %s: unrecognized kind %d\n",
  1678. __func__, kind);
  1679. return 0;
  1680. }
  1681. rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
  1682. return rc;
  1683. }
  1684. static inline int may_rename(struct inode *old_dir,
  1685. struct dentry *old_dentry,
  1686. struct inode *new_dir,
  1687. struct dentry *new_dentry)
  1688. {
  1689. struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
  1690. struct common_audit_data ad;
  1691. u32 sid = current_sid();
  1692. u32 av;
  1693. int old_is_dir, new_is_dir;
  1694. int rc;
  1695. old_dsec = inode_security(old_dir);
  1696. old_isec = backing_inode_security(old_dentry);
  1697. old_is_dir = d_is_dir(old_dentry);
  1698. new_dsec = inode_security(new_dir);
  1699. ad.type = LSM_AUDIT_DATA_DENTRY;
  1700. ad.u.dentry = old_dentry;
  1701. rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
  1702. DIR__REMOVE_NAME | DIR__SEARCH, &ad);
  1703. if (rc)
  1704. return rc;
  1705. rc = avc_has_perm(sid, old_isec->sid,
  1706. old_isec->sclass, FILE__RENAME, &ad);
  1707. if (rc)
  1708. return rc;
  1709. if (old_is_dir && new_dir != old_dir) {
  1710. rc = avc_has_perm(sid, old_isec->sid,
  1711. old_isec->sclass, DIR__REPARENT, &ad);
  1712. if (rc)
  1713. return rc;
  1714. }
  1715. ad.u.dentry = new_dentry;
  1716. av = DIR__ADD_NAME | DIR__SEARCH;
  1717. if (d_is_positive(new_dentry))
  1718. av |= DIR__REMOVE_NAME;
  1719. rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
  1720. if (rc)
  1721. return rc;
  1722. if (d_is_positive(new_dentry)) {
  1723. new_isec = backing_inode_security(new_dentry);
  1724. new_is_dir = d_is_dir(new_dentry);
  1725. rc = avc_has_perm(sid, new_isec->sid,
  1726. new_isec->sclass,
  1727. (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
  1728. if (rc)
  1729. return rc;
  1730. }
  1731. return 0;
  1732. }
  1733. /* Check whether a task can perform a filesystem operation. */
  1734. static int superblock_has_perm(const struct cred *cred,
  1735. const struct super_block *sb,
  1736. u32 perms,
  1737. struct common_audit_data *ad)
  1738. {
  1739. struct superblock_security_struct *sbsec;
  1740. u32 sid = cred_sid(cred);
  1741. sbsec = selinux_superblock(sb);
  1742. return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
  1743. }
  1744. /* Convert a Linux mode and permission mask to an access vector. */
  1745. static inline u32 file_mask_to_av(int mode, int mask)
  1746. {
  1747. u32 av = 0;
  1748. if (!S_ISDIR(mode)) {
  1749. if (mask & MAY_EXEC)
  1750. av |= FILE__EXECUTE;
  1751. if (mask & MAY_READ)
  1752. av |= FILE__READ;
  1753. if (mask & MAY_APPEND)
  1754. av |= FILE__APPEND;
  1755. else if (mask & MAY_WRITE)
  1756. av |= FILE__WRITE;
  1757. } else {
  1758. if (mask & MAY_EXEC)
  1759. av |= DIR__SEARCH;
  1760. if (mask & MAY_WRITE)
  1761. av |= DIR__WRITE;
  1762. if (mask & MAY_READ)
  1763. av |= DIR__READ;
  1764. }
  1765. return av;
  1766. }
  1767. /* Convert a Linux file to an access vector. */
  1768. static inline u32 file_to_av(const struct file *file)
  1769. {
  1770. u32 av = 0;
  1771. if (file->f_mode & FMODE_READ)
  1772. av |= FILE__READ;
  1773. if (file->f_mode & FMODE_WRITE) {
  1774. if (file->f_flags & O_APPEND)
  1775. av |= FILE__APPEND;
  1776. else
  1777. av |= FILE__WRITE;
  1778. }
  1779. if (!av) {
  1780. /*
  1781. * Special file opened with flags 3 for ioctl-only use.
  1782. */
  1783. av = FILE__IOCTL;
  1784. }
  1785. return av;
  1786. }
  1787. /*
  1788. * Convert a file to an access vector and include the correct
  1789. * open permission.
  1790. */
  1791. static inline u32 open_file_to_av(struct file *file)
  1792. {
  1793. u32 av = file_to_av(file);
  1794. struct inode *inode = file_inode(file);
  1795. if (selinux_policycap_openperm() &&
  1796. inode->i_sb->s_magic != SOCKFS_MAGIC)
  1797. av |= FILE__OPEN;
  1798. return av;
  1799. }
  1800. /* Hook functions begin here. */
  1801. static int selinux_binder_set_context_mgr(const struct cred *mgr)
  1802. {
  1803. return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER,
  1804. BINDER__SET_CONTEXT_MGR, NULL);
  1805. }
  1806. static int selinux_binder_transaction(const struct cred *from,
  1807. const struct cred *to)
  1808. {
  1809. u32 mysid = current_sid();
  1810. u32 fromsid = cred_sid(from);
  1811. u32 tosid = cred_sid(to);
  1812. int rc;
  1813. if (mysid != fromsid) {
  1814. rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
  1815. BINDER__IMPERSONATE, NULL);
  1816. if (rc)
  1817. return rc;
  1818. }
  1819. return avc_has_perm(fromsid, tosid,
  1820. SECCLASS_BINDER, BINDER__CALL, NULL);
  1821. }
  1822. static int selinux_binder_transfer_binder(const struct cred *from,
  1823. const struct cred *to)
  1824. {
  1825. return avc_has_perm(cred_sid(from), cred_sid(to),
  1826. SECCLASS_BINDER, BINDER__TRANSFER,
  1827. NULL);
  1828. }
  1829. static int selinux_binder_transfer_file(const struct cred *from,
  1830. const struct cred *to,
  1831. const struct file *file)
  1832. {
  1833. u32 sid = cred_sid(to);
  1834. struct file_security_struct *fsec = selinux_file(file);
  1835. struct dentry *dentry = file->f_path.dentry;
  1836. struct inode_security_struct *isec;
  1837. struct common_audit_data ad;
  1838. int rc;
  1839. ad.type = LSM_AUDIT_DATA_PATH;
  1840. ad.u.path = file->f_path;
  1841. if (sid != fsec->sid) {
  1842. rc = avc_has_perm(sid, fsec->sid,
  1843. SECCLASS_FD,
  1844. FD__USE,
  1845. &ad);
  1846. if (rc)
  1847. return rc;
  1848. }
  1849. #ifdef CONFIG_BPF_SYSCALL
  1850. rc = bpf_fd_pass(file, sid);
  1851. if (rc)
  1852. return rc;
  1853. #endif
  1854. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1855. return 0;
  1856. isec = backing_inode_security(dentry);
  1857. return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
  1858. &ad);
  1859. }
  1860. static int selinux_ptrace_access_check(struct task_struct *child,
  1861. unsigned int mode)
  1862. {
  1863. u32 sid = current_sid();
  1864. u32 csid = task_sid_obj(child);
  1865. if (mode & PTRACE_MODE_READ)
  1866. return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ,
  1867. NULL);
  1868. return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE,
  1869. NULL);
  1870. }
  1871. static int selinux_ptrace_traceme(struct task_struct *parent)
  1872. {
  1873. return avc_has_perm(task_sid_obj(parent), task_sid_obj(current),
  1874. SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
  1875. }
  1876. static int selinux_capget(const struct task_struct *target, kernel_cap_t *effective,
  1877. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  1878. {
  1879. return avc_has_perm(current_sid(), task_sid_obj(target),
  1880. SECCLASS_PROCESS, PROCESS__GETCAP, NULL);
  1881. }
  1882. static int selinux_capset(struct cred *new, const struct cred *old,
  1883. const kernel_cap_t *effective,
  1884. const kernel_cap_t *inheritable,
  1885. const kernel_cap_t *permitted)
  1886. {
  1887. return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
  1888. PROCESS__SETCAP, NULL);
  1889. }
  1890. /*
  1891. * (This comment used to live with the selinux_task_setuid hook,
  1892. * which was removed).
  1893. *
  1894. * Since setuid only affects the current process, and since the SELinux
  1895. * controls are not based on the Linux identity attributes, SELinux does not
  1896. * need to control this operation. However, SELinux does control the use of
  1897. * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
  1898. */
  1899. static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
  1900. int cap, unsigned int opts)
  1901. {
  1902. return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
  1903. }
  1904. static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb)
  1905. {
  1906. const struct cred *cred = current_cred();
  1907. int rc = 0;
  1908. if (!sb)
  1909. return 0;
  1910. switch (cmds) {
  1911. case Q_SYNC:
  1912. case Q_QUOTAON:
  1913. case Q_QUOTAOFF:
  1914. case Q_SETINFO:
  1915. case Q_SETQUOTA:
  1916. case Q_XQUOTAOFF:
  1917. case Q_XQUOTAON:
  1918. case Q_XSETQLIM:
  1919. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
  1920. break;
  1921. case Q_GETFMT:
  1922. case Q_GETINFO:
  1923. case Q_GETQUOTA:
  1924. case Q_XGETQUOTA:
  1925. case Q_XGETQSTAT:
  1926. case Q_XGETQSTATV:
  1927. case Q_XGETNEXTQUOTA:
  1928. rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
  1929. break;
  1930. default:
  1931. rc = 0; /* let the kernel handle invalid cmds */
  1932. break;
  1933. }
  1934. return rc;
  1935. }
  1936. static int selinux_quota_on(struct dentry *dentry)
  1937. {
  1938. const struct cred *cred = current_cred();
  1939. return dentry_has_perm(cred, dentry, FILE__QUOTAON);
  1940. }
  1941. static int selinux_syslog(int type)
  1942. {
  1943. switch (type) {
  1944. case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
  1945. case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
  1946. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  1947. SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
  1948. case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
  1949. case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
  1950. /* Set level of messages printed to console */
  1951. case SYSLOG_ACTION_CONSOLE_LEVEL:
  1952. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  1953. SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
  1954. NULL);
  1955. }
  1956. /* All other syslog types */
  1957. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  1958. SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
  1959. }
  1960. /*
  1961. * Check permission for allocating a new virtual mapping. Returns
  1962. * 0 if permission is granted, negative error code if not.
  1963. *
  1964. * Do not audit the selinux permission check, as this is applied to all
  1965. * processes that allocate mappings.
  1966. */
  1967. static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
  1968. {
  1969. return cred_has_capability(current_cred(), CAP_SYS_ADMIN,
  1970. CAP_OPT_NOAUDIT, true);
  1971. }
  1972. /* binprm security operations */
  1973. static u32 ptrace_parent_sid(void)
  1974. {
  1975. u32 sid = 0;
  1976. struct task_struct *tracer;
  1977. rcu_read_lock();
  1978. tracer = ptrace_parent(current);
  1979. if (tracer)
  1980. sid = task_sid_obj(tracer);
  1981. rcu_read_unlock();
  1982. return sid;
  1983. }
  1984. static int check_nnp_nosuid(const struct linux_binprm *bprm,
  1985. const struct task_security_struct *old_tsec,
  1986. const struct task_security_struct *new_tsec)
  1987. {
  1988. int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
  1989. int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
  1990. int rc;
  1991. u32 av;
  1992. if (!nnp && !nosuid)
  1993. return 0; /* neither NNP nor nosuid */
  1994. if (new_tsec->sid == old_tsec->sid)
  1995. return 0; /* No change in credentials */
  1996. /*
  1997. * If the policy enables the nnp_nosuid_transition policy capability,
  1998. * then we permit transitions under NNP or nosuid if the
  1999. * policy allows the corresponding permission between
  2000. * the old and new contexts.
  2001. */
  2002. if (selinux_policycap_nnp_nosuid_transition()) {
  2003. av = 0;
  2004. if (nnp)
  2005. av |= PROCESS2__NNP_TRANSITION;
  2006. if (nosuid)
  2007. av |= PROCESS2__NOSUID_TRANSITION;
  2008. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  2009. SECCLASS_PROCESS2, av, NULL);
  2010. if (!rc)
  2011. return 0;
  2012. }
  2013. /*
  2014. * We also permit NNP or nosuid transitions to bounded SIDs,
  2015. * i.e. SIDs that are guaranteed to only be allowed a subset
  2016. * of the permissions of the current SID.
  2017. */
  2018. rc = security_bounded_transition(old_tsec->sid,
  2019. new_tsec->sid);
  2020. if (!rc)
  2021. return 0;
  2022. /*
  2023. * On failure, preserve the errno values for NNP vs nosuid.
  2024. * NNP: Operation not permitted for caller.
  2025. * nosuid: Permission denied to file.
  2026. */
  2027. if (nnp)
  2028. return -EPERM;
  2029. return -EACCES;
  2030. }
  2031. static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
  2032. {
  2033. const struct task_security_struct *old_tsec;
  2034. struct task_security_struct *new_tsec;
  2035. struct inode_security_struct *isec;
  2036. struct common_audit_data ad;
  2037. struct inode *inode = file_inode(bprm->file);
  2038. int rc;
  2039. /* SELinux context only depends on initial program or script and not
  2040. * the script interpreter */
  2041. old_tsec = selinux_cred(current_cred());
  2042. new_tsec = selinux_cred(bprm->cred);
  2043. isec = inode_security(inode);
  2044. /* Default to the current task SID. */
  2045. new_tsec->sid = old_tsec->sid;
  2046. new_tsec->osid = old_tsec->sid;
  2047. /* Reset fs, key, and sock SIDs on execve. */
  2048. new_tsec->create_sid = 0;
  2049. new_tsec->keycreate_sid = 0;
  2050. new_tsec->sockcreate_sid = 0;
  2051. /*
  2052. * Before policy is loaded, label any task outside kernel space
  2053. * as SECINITSID_INIT, so that any userspace tasks surviving from
  2054. * early boot end up with a label different from SECINITSID_KERNEL
  2055. * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
  2056. */
  2057. if (!selinux_initialized()) {
  2058. new_tsec->sid = SECINITSID_INIT;
  2059. /* also clear the exec_sid just in case */
  2060. new_tsec->exec_sid = 0;
  2061. return 0;
  2062. }
  2063. if (old_tsec->exec_sid) {
  2064. new_tsec->sid = old_tsec->exec_sid;
  2065. /* Reset exec SID on execve. */
  2066. new_tsec->exec_sid = 0;
  2067. /* Fail on NNP or nosuid if not an allowed transition. */
  2068. rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
  2069. if (rc)
  2070. return rc;
  2071. } else {
  2072. /* Check for a default transition on this program. */
  2073. rc = security_transition_sid(old_tsec->sid,
  2074. isec->sid, SECCLASS_PROCESS, NULL,
  2075. &new_tsec->sid);
  2076. if (rc)
  2077. return rc;
  2078. /*
  2079. * Fallback to old SID on NNP or nosuid if not an allowed
  2080. * transition.
  2081. */
  2082. rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
  2083. if (rc)
  2084. new_tsec->sid = old_tsec->sid;
  2085. }
  2086. ad.type = LSM_AUDIT_DATA_FILE;
  2087. ad.u.file = bprm->file;
  2088. if (new_tsec->sid == old_tsec->sid) {
  2089. rc = avc_has_perm(old_tsec->sid, isec->sid,
  2090. SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
  2091. if (rc)
  2092. return rc;
  2093. } else {
  2094. /* Check permissions for the transition. */
  2095. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  2096. SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
  2097. if (rc)
  2098. return rc;
  2099. rc = avc_has_perm(new_tsec->sid, isec->sid,
  2100. SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
  2101. if (rc)
  2102. return rc;
  2103. /* Check for shared state */
  2104. if (bprm->unsafe & LSM_UNSAFE_SHARE) {
  2105. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  2106. SECCLASS_PROCESS, PROCESS__SHARE,
  2107. NULL);
  2108. if (rc)
  2109. return -EPERM;
  2110. }
  2111. /* Make sure that anyone attempting to ptrace over a task that
  2112. * changes its SID has the appropriate permit */
  2113. if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
  2114. u32 ptsid = ptrace_parent_sid();
  2115. if (ptsid != 0) {
  2116. rc = avc_has_perm(ptsid, new_tsec->sid,
  2117. SECCLASS_PROCESS,
  2118. PROCESS__PTRACE, NULL);
  2119. if (rc)
  2120. return -EPERM;
  2121. }
  2122. }
  2123. /* Clear any possibly unsafe personality bits on exec: */
  2124. bprm->per_clear |= PER_CLEAR_ON_SETID;
  2125. /* Enable secure mode for SIDs transitions unless
  2126. the noatsecure permission is granted between
  2127. the two SIDs, i.e. ahp returns 0. */
  2128. rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
  2129. SECCLASS_PROCESS, PROCESS__NOATSECURE,
  2130. NULL);
  2131. bprm->secureexec |= !!rc;
  2132. }
  2133. return 0;
  2134. }
  2135. static int match_file(const void *p, struct file *file, unsigned fd)
  2136. {
  2137. return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
  2138. }
  2139. /* Derived from fs/exec.c:flush_old_files. */
  2140. static inline void flush_unauthorized_files(const struct cred *cred,
  2141. struct files_struct *files)
  2142. {
  2143. struct file *file, *devnull = NULL;
  2144. struct tty_struct *tty;
  2145. int drop_tty = 0;
  2146. unsigned n;
  2147. tty = get_current_tty();
  2148. if (tty) {
  2149. spin_lock(&tty->files_lock);
  2150. if (!list_empty(&tty->tty_files)) {
  2151. struct tty_file_private *file_priv;
  2152. /* Revalidate access to controlling tty.
  2153. Use file_path_has_perm on the tty path directly
  2154. rather than using file_has_perm, as this particular
  2155. open file may belong to another process and we are
  2156. only interested in the inode-based check here. */
  2157. file_priv = list_first_entry(&tty->tty_files,
  2158. struct tty_file_private, list);
  2159. file = file_priv->file;
  2160. if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
  2161. drop_tty = 1;
  2162. }
  2163. spin_unlock(&tty->files_lock);
  2164. tty_kref_put(tty);
  2165. }
  2166. /* Reset controlling tty. */
  2167. if (drop_tty)
  2168. no_tty();
  2169. /* Revalidate access to inherited open files. */
  2170. n = iterate_fd(files, 0, match_file, cred);
  2171. if (!n) /* none found? */
  2172. return;
  2173. devnull = dentry_open(&selinux_null, O_RDWR, cred);
  2174. if (IS_ERR(devnull))
  2175. devnull = NULL;
  2176. /* replace all the matching ones with this */
  2177. do {
  2178. replace_fd(n - 1, devnull, 0);
  2179. } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
  2180. if (devnull)
  2181. fput(devnull);
  2182. }
  2183. /*
  2184. * Prepare a process for imminent new credential changes due to exec
  2185. */
  2186. static void selinux_bprm_committing_creds(const struct linux_binprm *bprm)
  2187. {
  2188. struct task_security_struct *new_tsec;
  2189. struct rlimit *rlim, *initrlim;
  2190. int rc, i;
  2191. new_tsec = selinux_cred(bprm->cred);
  2192. if (new_tsec->sid == new_tsec->osid)
  2193. return;
  2194. /* Close files for which the new task SID is not authorized. */
  2195. flush_unauthorized_files(bprm->cred, current->files);
  2196. /* Always clear parent death signal on SID transitions. */
  2197. current->pdeath_signal = 0;
  2198. /* Check whether the new SID can inherit resource limits from the old
  2199. * SID. If not, reset all soft limits to the lower of the current
  2200. * task's hard limit and the init task's soft limit.
  2201. *
  2202. * Note that the setting of hard limits (even to lower them) can be
  2203. * controlled by the setrlimit check. The inclusion of the init task's
  2204. * soft limit into the computation is to avoid resetting soft limits
  2205. * higher than the default soft limit for cases where the default is
  2206. * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
  2207. */
  2208. rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
  2209. PROCESS__RLIMITINH, NULL);
  2210. if (rc) {
  2211. /* protect against do_prlimit() */
  2212. task_lock(current);
  2213. for (i = 0; i < RLIM_NLIMITS; i++) {
  2214. rlim = current->signal->rlim + i;
  2215. initrlim = init_task.signal->rlim + i;
  2216. rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
  2217. }
  2218. task_unlock(current);
  2219. if (IS_ENABLED(CONFIG_POSIX_TIMERS))
  2220. update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
  2221. }
  2222. }
  2223. /*
  2224. * Clean up the process immediately after the installation of new credentials
  2225. * due to exec
  2226. */
  2227. static void selinux_bprm_committed_creds(const struct linux_binprm *bprm)
  2228. {
  2229. const struct task_security_struct *tsec = selinux_cred(current_cred());
  2230. u32 osid, sid;
  2231. int rc;
  2232. osid = tsec->osid;
  2233. sid = tsec->sid;
  2234. if (sid == osid)
  2235. return;
  2236. /* Check whether the new SID can inherit signal state from the old SID.
  2237. * If not, clear itimers to avoid subsequent signal generation and
  2238. * flush and unblock signals.
  2239. *
  2240. * This must occur _after_ the task SID has been updated so that any
  2241. * kill done after the flush will be checked against the new SID.
  2242. */
  2243. rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
  2244. if (rc) {
  2245. clear_itimer();
  2246. spin_lock_irq(&unrcu_pointer(current->sighand)->siglock);
  2247. if (!fatal_signal_pending(current)) {
  2248. flush_sigqueue(&current->pending);
  2249. flush_sigqueue(&current->signal->shared_pending);
  2250. flush_signal_handlers(current, 1);
  2251. sigemptyset(&current->blocked);
  2252. recalc_sigpending();
  2253. }
  2254. spin_unlock_irq(&unrcu_pointer(current->sighand)->siglock);
  2255. }
  2256. /* Wake up the parent if it is waiting so that it can recheck
  2257. * wait permission to the new task SID. */
  2258. read_lock(&tasklist_lock);
  2259. __wake_up_parent(current, unrcu_pointer(current->real_parent));
  2260. read_unlock(&tasklist_lock);
  2261. }
  2262. /* superblock security operations */
  2263. static int selinux_sb_alloc_security(struct super_block *sb)
  2264. {
  2265. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  2266. mutex_init(&sbsec->lock);
  2267. INIT_LIST_HEAD(&sbsec->isec_head);
  2268. spin_lock_init(&sbsec->isec_lock);
  2269. sbsec->sid = SECINITSID_UNLABELED;
  2270. sbsec->def_sid = SECINITSID_FILE;
  2271. sbsec->mntpoint_sid = SECINITSID_UNLABELED;
  2272. return 0;
  2273. }
  2274. static inline int opt_len(const char *s)
  2275. {
  2276. bool open_quote = false;
  2277. int len;
  2278. char c;
  2279. for (len = 0; (c = s[len]) != '\0'; len++) {
  2280. if (c == '"')
  2281. open_quote = !open_quote;
  2282. if (c == ',' && !open_quote)
  2283. break;
  2284. }
  2285. return len;
  2286. }
  2287. static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
  2288. {
  2289. char *from = options;
  2290. char *to = options;
  2291. bool first = true;
  2292. int rc;
  2293. while (1) {
  2294. int len = opt_len(from);
  2295. int token;
  2296. char *arg = NULL;
  2297. token = match_opt_prefix(from, len, &arg);
  2298. if (token != Opt_error) {
  2299. char *p, *q;
  2300. /* strip quotes */
  2301. if (arg) {
  2302. for (p = q = arg; p < from + len; p++) {
  2303. char c = *p;
  2304. if (c != '"')
  2305. *q++ = c;
  2306. }
  2307. arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
  2308. if (!arg) {
  2309. rc = -ENOMEM;
  2310. goto free_opt;
  2311. }
  2312. }
  2313. rc = selinux_add_opt(token, arg, mnt_opts);
  2314. kfree(arg);
  2315. arg = NULL;
  2316. if (unlikely(rc)) {
  2317. goto free_opt;
  2318. }
  2319. } else {
  2320. if (!first) { // copy with preceding comma
  2321. from--;
  2322. len++;
  2323. }
  2324. if (to != from)
  2325. memmove(to, from, len);
  2326. to += len;
  2327. first = false;
  2328. }
  2329. if (!from[len])
  2330. break;
  2331. from += len + 1;
  2332. }
  2333. *to = '\0';
  2334. return 0;
  2335. free_opt:
  2336. if (*mnt_opts) {
  2337. selinux_free_mnt_opts(*mnt_opts);
  2338. *mnt_opts = NULL;
  2339. }
  2340. return rc;
  2341. }
  2342. static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
  2343. {
  2344. struct selinux_mnt_opts *opts = mnt_opts;
  2345. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  2346. /*
  2347. * Superblock not initialized (i.e. no options) - reject if any
  2348. * options specified, otherwise accept.
  2349. */
  2350. if (!(sbsec->flags & SE_SBINITIALIZED))
  2351. return opts ? 1 : 0;
  2352. /*
  2353. * Superblock initialized and no options specified - reject if
  2354. * superblock has any options set, otherwise accept.
  2355. */
  2356. if (!opts)
  2357. return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
  2358. if (opts->fscontext_sid) {
  2359. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
  2360. opts->fscontext_sid))
  2361. return 1;
  2362. }
  2363. if (opts->context_sid) {
  2364. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
  2365. opts->context_sid))
  2366. return 1;
  2367. }
  2368. if (opts->rootcontext_sid) {
  2369. struct inode_security_struct *root_isec;
  2370. root_isec = backing_inode_security(sb->s_root);
  2371. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
  2372. opts->rootcontext_sid))
  2373. return 1;
  2374. }
  2375. if (opts->defcontext_sid) {
  2376. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
  2377. opts->defcontext_sid))
  2378. return 1;
  2379. }
  2380. return 0;
  2381. }
  2382. static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
  2383. {
  2384. struct selinux_mnt_opts *opts = mnt_opts;
  2385. struct superblock_security_struct *sbsec = selinux_superblock(sb);
  2386. if (!(sbsec->flags & SE_SBINITIALIZED))
  2387. return 0;
  2388. if (!opts)
  2389. return 0;
  2390. if (opts->fscontext_sid) {
  2391. if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
  2392. opts->fscontext_sid))
  2393. goto out_bad_option;
  2394. }
  2395. if (opts->context_sid) {
  2396. if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
  2397. opts->context_sid))
  2398. goto out_bad_option;
  2399. }
  2400. if (opts->rootcontext_sid) {
  2401. struct inode_security_struct *root_isec;
  2402. root_isec = backing_inode_security(sb->s_root);
  2403. if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
  2404. opts->rootcontext_sid))
  2405. goto out_bad_option;
  2406. }
  2407. if (opts->defcontext_sid) {
  2408. if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
  2409. opts->defcontext_sid))
  2410. goto out_bad_option;
  2411. }
  2412. return 0;
  2413. out_bad_option:
  2414. pr_warn("SELinux: unable to change security options "
  2415. "during remount (dev %s, type=%s)\n", sb->s_id,
  2416. sb->s_type->name);
  2417. return -EINVAL;
  2418. }
  2419. static int selinux_sb_kern_mount(const struct super_block *sb)
  2420. {
  2421. const struct cred *cred = current_cred();
  2422. struct common_audit_data ad;
  2423. ad.type = LSM_AUDIT_DATA_DENTRY;
  2424. ad.u.dentry = sb->s_root;
  2425. return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
  2426. }
  2427. static int selinux_sb_statfs(struct dentry *dentry)
  2428. {
  2429. const struct cred *cred = current_cred();
  2430. struct common_audit_data ad;
  2431. ad.type = LSM_AUDIT_DATA_DENTRY;
  2432. ad.u.dentry = dentry->d_sb->s_root;
  2433. return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
  2434. }
  2435. static int selinux_mount(const char *dev_name,
  2436. const struct path *path,
  2437. const char *type,
  2438. unsigned long flags,
  2439. void *data)
  2440. {
  2441. const struct cred *cred = current_cred();
  2442. if (flags & MS_REMOUNT)
  2443. return superblock_has_perm(cred, path->dentry->d_sb,
  2444. FILESYSTEM__REMOUNT, NULL);
  2445. else
  2446. return path_has_perm(cred, path, FILE__MOUNTON);
  2447. }
  2448. static int selinux_move_mount(const struct path *from_path,
  2449. const struct path *to_path)
  2450. {
  2451. const struct cred *cred = current_cred();
  2452. return path_has_perm(cred, to_path, FILE__MOUNTON);
  2453. }
  2454. static int selinux_umount(struct vfsmount *mnt, int flags)
  2455. {
  2456. const struct cred *cred = current_cred();
  2457. return superblock_has_perm(cred, mnt->mnt_sb,
  2458. FILESYSTEM__UNMOUNT, NULL);
  2459. }
  2460. static int selinux_fs_context_submount(struct fs_context *fc,
  2461. struct super_block *reference)
  2462. {
  2463. const struct superblock_security_struct *sbsec = selinux_superblock(reference);
  2464. struct selinux_mnt_opts *opts;
  2465. /*
  2466. * Ensure that fc->security remains NULL when no options are set
  2467. * as expected by selinux_set_mnt_opts().
  2468. */
  2469. if (!(sbsec->flags & (FSCONTEXT_MNT|CONTEXT_MNT|DEFCONTEXT_MNT)))
  2470. return 0;
  2471. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  2472. if (!opts)
  2473. return -ENOMEM;
  2474. if (sbsec->flags & FSCONTEXT_MNT)
  2475. opts->fscontext_sid = sbsec->sid;
  2476. if (sbsec->flags & CONTEXT_MNT)
  2477. opts->context_sid = sbsec->mntpoint_sid;
  2478. if (sbsec->flags & DEFCONTEXT_MNT)
  2479. opts->defcontext_sid = sbsec->def_sid;
  2480. fc->security = opts;
  2481. return 0;
  2482. }
  2483. static int selinux_fs_context_dup(struct fs_context *fc,
  2484. struct fs_context *src_fc)
  2485. {
  2486. const struct selinux_mnt_opts *src = src_fc->security;
  2487. if (!src)
  2488. return 0;
  2489. fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
  2490. return fc->security ? 0 : -ENOMEM;
  2491. }
  2492. static const struct fs_parameter_spec selinux_fs_parameters[] = {
  2493. fsparam_string(CONTEXT_STR, Opt_context),
  2494. fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
  2495. fsparam_string(FSCONTEXT_STR, Opt_fscontext),
  2496. fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
  2497. fsparam_flag (SECLABEL_STR, Opt_seclabel),
  2498. {}
  2499. };
  2500. static int selinux_fs_context_parse_param(struct fs_context *fc,
  2501. struct fs_parameter *param)
  2502. {
  2503. struct fs_parse_result result;
  2504. int opt;
  2505. opt = fs_parse(fc, selinux_fs_parameters, param, &result);
  2506. if (opt < 0)
  2507. return opt;
  2508. return selinux_add_opt(opt, param->string, &fc->security);
  2509. }
  2510. /* inode security operations */
  2511. static int selinux_inode_alloc_security(struct inode *inode)
  2512. {
  2513. struct inode_security_struct *isec = selinux_inode(inode);
  2514. u32 sid = current_sid();
  2515. spin_lock_init(&isec->lock);
  2516. INIT_LIST_HEAD(&isec->list);
  2517. isec->inode = inode;
  2518. isec->sid = SECINITSID_UNLABELED;
  2519. isec->sclass = SECCLASS_FILE;
  2520. isec->task_sid = sid;
  2521. isec->initialized = LABEL_INVALID;
  2522. return 0;
  2523. }
  2524. static void selinux_inode_free_security(struct inode *inode)
  2525. {
  2526. inode_free_security(inode);
  2527. }
  2528. static int selinux_dentry_init_security(struct dentry *dentry, int mode,
  2529. const struct qstr *name,
  2530. const char **xattr_name, void **ctx,
  2531. u32 *ctxlen)
  2532. {
  2533. u32 newsid;
  2534. int rc;
  2535. rc = selinux_determine_inode_label(selinux_cred(current_cred()),
  2536. d_inode(dentry->d_parent), name,
  2537. inode_mode_to_security_class(mode),
  2538. &newsid);
  2539. if (rc)
  2540. return rc;
  2541. if (xattr_name)
  2542. *xattr_name = XATTR_NAME_SELINUX;
  2543. return security_sid_to_context(newsid, (char **)ctx,
  2544. ctxlen);
  2545. }
  2546. static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
  2547. struct qstr *name,
  2548. const struct cred *old,
  2549. struct cred *new)
  2550. {
  2551. u32 newsid;
  2552. int rc;
  2553. struct task_security_struct *tsec;
  2554. rc = selinux_determine_inode_label(selinux_cred(old),
  2555. d_inode(dentry->d_parent), name,
  2556. inode_mode_to_security_class(mode),
  2557. &newsid);
  2558. if (rc)
  2559. return rc;
  2560. tsec = selinux_cred(new);
  2561. tsec->create_sid = newsid;
  2562. return 0;
  2563. }
  2564. static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
  2565. const struct qstr *qstr,
  2566. struct xattr *xattrs, int *xattr_count)
  2567. {
  2568. const struct task_security_struct *tsec = selinux_cred(current_cred());
  2569. struct superblock_security_struct *sbsec;
  2570. struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
  2571. u32 newsid, clen;
  2572. u16 newsclass;
  2573. int rc;
  2574. char *context;
  2575. sbsec = selinux_superblock(dir->i_sb);
  2576. newsid = tsec->create_sid;
  2577. newsclass = inode_mode_to_security_class(inode->i_mode);
  2578. rc = selinux_determine_inode_label(tsec, dir, qstr, newsclass, &newsid);
  2579. if (rc)
  2580. return rc;
  2581. /* Possibly defer initialization to selinux_complete_init. */
  2582. if (sbsec->flags & SE_SBINITIALIZED) {
  2583. struct inode_security_struct *isec = selinux_inode(inode);
  2584. isec->sclass = newsclass;
  2585. isec->sid = newsid;
  2586. isec->initialized = LABEL_INITIALIZED;
  2587. }
  2588. if (!selinux_initialized() ||
  2589. !(sbsec->flags & SBLABEL_MNT))
  2590. return -EOPNOTSUPP;
  2591. if (xattr) {
  2592. rc = security_sid_to_context_force(newsid,
  2593. &context, &clen);
  2594. if (rc)
  2595. return rc;
  2596. xattr->value = context;
  2597. xattr->value_len = clen;
  2598. xattr->name = XATTR_SELINUX_SUFFIX;
  2599. }
  2600. return 0;
  2601. }
  2602. static int selinux_inode_init_security_anon(struct inode *inode,
  2603. const struct qstr *name,
  2604. const struct inode *context_inode)
  2605. {
  2606. u32 sid = current_sid();
  2607. struct common_audit_data ad;
  2608. struct inode_security_struct *isec;
  2609. int rc;
  2610. if (unlikely(!selinux_initialized()))
  2611. return 0;
  2612. isec = selinux_inode(inode);
  2613. /*
  2614. * We only get here once per ephemeral inode. The inode has
  2615. * been initialized via inode_alloc_security but is otherwise
  2616. * untouched.
  2617. */
  2618. if (context_inode) {
  2619. struct inode_security_struct *context_isec =
  2620. selinux_inode(context_inode);
  2621. if (context_isec->initialized != LABEL_INITIALIZED) {
  2622. pr_err("SELinux: context_inode is not initialized\n");
  2623. return -EACCES;
  2624. }
  2625. isec->sclass = context_isec->sclass;
  2626. isec->sid = context_isec->sid;
  2627. } else {
  2628. isec->sclass = SECCLASS_ANON_INODE;
  2629. rc = security_transition_sid(
  2630. sid, sid,
  2631. isec->sclass, name, &isec->sid);
  2632. if (rc)
  2633. return rc;
  2634. }
  2635. isec->initialized = LABEL_INITIALIZED;
  2636. /*
  2637. * Now that we've initialized security, check whether we're
  2638. * allowed to actually create this type of anonymous inode.
  2639. */
  2640. ad.type = LSM_AUDIT_DATA_ANONINODE;
  2641. ad.u.anonclass = name ? (const char *)name->name : "?";
  2642. return avc_has_perm(sid,
  2643. isec->sid,
  2644. isec->sclass,
  2645. FILE__CREATE,
  2646. &ad);
  2647. }
  2648. static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
  2649. {
  2650. return may_create(dir, dentry, SECCLASS_FILE);
  2651. }
  2652. static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2653. {
  2654. return may_link(dir, old_dentry, MAY_LINK);
  2655. }
  2656. static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
  2657. {
  2658. return may_link(dir, dentry, MAY_UNLINK);
  2659. }
  2660. static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
  2661. {
  2662. return may_create(dir, dentry, SECCLASS_LNK_FILE);
  2663. }
  2664. static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
  2665. {
  2666. return may_create(dir, dentry, SECCLASS_DIR);
  2667. }
  2668. static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
  2669. {
  2670. return may_link(dir, dentry, MAY_RMDIR);
  2671. }
  2672. static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2673. {
  2674. return may_create(dir, dentry, inode_mode_to_security_class(mode));
  2675. }
  2676. static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
  2677. struct inode *new_inode, struct dentry *new_dentry)
  2678. {
  2679. return may_rename(old_inode, old_dentry, new_inode, new_dentry);
  2680. }
  2681. static int selinux_inode_readlink(struct dentry *dentry)
  2682. {
  2683. const struct cred *cred = current_cred();
  2684. return dentry_has_perm(cred, dentry, FILE__READ);
  2685. }
  2686. static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
  2687. bool rcu)
  2688. {
  2689. struct common_audit_data ad;
  2690. struct inode_security_struct *isec;
  2691. u32 sid = current_sid();
  2692. ad.type = LSM_AUDIT_DATA_DENTRY;
  2693. ad.u.dentry = dentry;
  2694. isec = inode_security_rcu(inode, rcu);
  2695. if (IS_ERR(isec))
  2696. return PTR_ERR(isec);
  2697. return avc_has_perm(sid, isec->sid, isec->sclass, FILE__READ, &ad);
  2698. }
  2699. static noinline int audit_inode_permission(struct inode *inode,
  2700. u32 perms, u32 audited, u32 denied,
  2701. int result)
  2702. {
  2703. struct common_audit_data ad;
  2704. struct inode_security_struct *isec = selinux_inode(inode);
  2705. ad.type = LSM_AUDIT_DATA_INODE;
  2706. ad.u.inode = inode;
  2707. return slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
  2708. audited, denied, result, &ad);
  2709. }
  2710. static int selinux_inode_permission(struct inode *inode, int mask)
  2711. {
  2712. u32 perms;
  2713. bool from_access;
  2714. bool no_block = mask & MAY_NOT_BLOCK;
  2715. struct inode_security_struct *isec;
  2716. u32 sid = current_sid();
  2717. struct av_decision avd;
  2718. int rc, rc2;
  2719. u32 audited, denied;
  2720. from_access = mask & MAY_ACCESS;
  2721. mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
  2722. /* No permission to check. Existence test. */
  2723. if (!mask)
  2724. return 0;
  2725. if (unlikely(IS_PRIVATE(inode)))
  2726. return 0;
  2727. perms = file_mask_to_av(inode->i_mode, mask);
  2728. isec = inode_security_rcu(inode, no_block);
  2729. if (IS_ERR(isec))
  2730. return PTR_ERR(isec);
  2731. rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0,
  2732. &avd);
  2733. audited = avc_audit_required(perms, &avd, rc,
  2734. from_access ? FILE__AUDIT_ACCESS : 0,
  2735. &denied);
  2736. if (likely(!audited))
  2737. return rc;
  2738. rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
  2739. if (rc2)
  2740. return rc2;
  2741. return rc;
  2742. }
  2743. static int selinux_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  2744. struct iattr *iattr)
  2745. {
  2746. const struct cred *cred = current_cred();
  2747. struct inode *inode = d_backing_inode(dentry);
  2748. unsigned int ia_valid = iattr->ia_valid;
  2749. __u32 av = FILE__WRITE;
  2750. /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
  2751. if (ia_valid & ATTR_FORCE) {
  2752. ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
  2753. ATTR_FORCE);
  2754. if (!ia_valid)
  2755. return 0;
  2756. }
  2757. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
  2758. ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
  2759. return dentry_has_perm(cred, dentry, FILE__SETATTR);
  2760. if (selinux_policycap_openperm() &&
  2761. inode->i_sb->s_magic != SOCKFS_MAGIC &&
  2762. (ia_valid & ATTR_SIZE) &&
  2763. !(ia_valid & ATTR_FILE))
  2764. av |= FILE__OPEN;
  2765. return dentry_has_perm(cred, dentry, av);
  2766. }
  2767. static int selinux_inode_getattr(const struct path *path)
  2768. {
  2769. return path_has_perm(current_cred(), path, FILE__GETATTR);
  2770. }
  2771. static bool has_cap_mac_admin(bool audit)
  2772. {
  2773. const struct cred *cred = current_cred();
  2774. unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
  2775. if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
  2776. return false;
  2777. if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
  2778. return false;
  2779. return true;
  2780. }
  2781. /**
  2782. * selinux_inode_xattr_skipcap - Skip the xattr capability checks?
  2783. * @name: name of the xattr
  2784. *
  2785. * Returns 1 to indicate that SELinux "owns" the access control rights to xattrs
  2786. * named @name; the LSM layer should avoid enforcing any traditional
  2787. * capability based access controls on this xattr. Returns 0 to indicate that
  2788. * SELinux does not "own" the access control rights to xattrs named @name and is
  2789. * deferring to the LSM layer for further access controls, including capability
  2790. * based controls.
  2791. */
  2792. static int selinux_inode_xattr_skipcap(const char *name)
  2793. {
  2794. /* require capability check if not a selinux xattr */
  2795. return !strcmp(name, XATTR_NAME_SELINUX);
  2796. }
  2797. static int selinux_inode_setxattr(struct mnt_idmap *idmap,
  2798. struct dentry *dentry, const char *name,
  2799. const void *value, size_t size, int flags)
  2800. {
  2801. struct inode *inode = d_backing_inode(dentry);
  2802. struct inode_security_struct *isec;
  2803. struct superblock_security_struct *sbsec;
  2804. struct common_audit_data ad;
  2805. u32 newsid, sid = current_sid();
  2806. int rc = 0;
  2807. /* if not a selinux xattr, only check the ordinary setattr perm */
  2808. if (strcmp(name, XATTR_NAME_SELINUX))
  2809. return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
  2810. if (!selinux_initialized())
  2811. return (inode_owner_or_capable(idmap, inode) ? 0 : -EPERM);
  2812. sbsec = selinux_superblock(inode->i_sb);
  2813. if (!(sbsec->flags & SBLABEL_MNT))
  2814. return -EOPNOTSUPP;
  2815. if (!inode_owner_or_capable(idmap, inode))
  2816. return -EPERM;
  2817. ad.type = LSM_AUDIT_DATA_DENTRY;
  2818. ad.u.dentry = dentry;
  2819. isec = backing_inode_security(dentry);
  2820. rc = avc_has_perm(sid, isec->sid, isec->sclass,
  2821. FILE__RELABELFROM, &ad);
  2822. if (rc)
  2823. return rc;
  2824. rc = security_context_to_sid(value, size, &newsid,
  2825. GFP_KERNEL);
  2826. if (rc == -EINVAL) {
  2827. if (!has_cap_mac_admin(true)) {
  2828. struct audit_buffer *ab;
  2829. size_t audit_size;
  2830. /* We strip a nul only if it is at the end, otherwise the
  2831. * context contains a nul and we should audit that */
  2832. if (value) {
  2833. const char *str = value;
  2834. if (str[size - 1] == '\0')
  2835. audit_size = size - 1;
  2836. else
  2837. audit_size = size;
  2838. } else {
  2839. audit_size = 0;
  2840. }
  2841. ab = audit_log_start(audit_context(),
  2842. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  2843. if (!ab)
  2844. return rc;
  2845. audit_log_format(ab, "op=setxattr invalid_context=");
  2846. audit_log_n_untrustedstring(ab, value, audit_size);
  2847. audit_log_end(ab);
  2848. return rc;
  2849. }
  2850. rc = security_context_to_sid_force(value,
  2851. size, &newsid);
  2852. }
  2853. if (rc)
  2854. return rc;
  2855. rc = avc_has_perm(sid, newsid, isec->sclass,
  2856. FILE__RELABELTO, &ad);
  2857. if (rc)
  2858. return rc;
  2859. rc = security_validate_transition(isec->sid, newsid,
  2860. sid, isec->sclass);
  2861. if (rc)
  2862. return rc;
  2863. return avc_has_perm(newsid,
  2864. sbsec->sid,
  2865. SECCLASS_FILESYSTEM,
  2866. FILESYSTEM__ASSOCIATE,
  2867. &ad);
  2868. }
  2869. static int selinux_inode_set_acl(struct mnt_idmap *idmap,
  2870. struct dentry *dentry, const char *acl_name,
  2871. struct posix_acl *kacl)
  2872. {
  2873. return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
  2874. }
  2875. static int selinux_inode_get_acl(struct mnt_idmap *idmap,
  2876. struct dentry *dentry, const char *acl_name)
  2877. {
  2878. return dentry_has_perm(current_cred(), dentry, FILE__GETATTR);
  2879. }
  2880. static int selinux_inode_remove_acl(struct mnt_idmap *idmap,
  2881. struct dentry *dentry, const char *acl_name)
  2882. {
  2883. return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
  2884. }
  2885. static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
  2886. const void *value, size_t size,
  2887. int flags)
  2888. {
  2889. struct inode *inode = d_backing_inode(dentry);
  2890. struct inode_security_struct *isec;
  2891. u32 newsid;
  2892. int rc;
  2893. if (strcmp(name, XATTR_NAME_SELINUX)) {
  2894. /* Not an attribute we recognize, so nothing to do. */
  2895. return;
  2896. }
  2897. if (!selinux_initialized()) {
  2898. /* If we haven't even been initialized, then we can't validate
  2899. * against a policy, so leave the label as invalid. It may
  2900. * resolve to a valid label on the next revalidation try if
  2901. * we've since initialized.
  2902. */
  2903. return;
  2904. }
  2905. rc = security_context_to_sid_force(value, size,
  2906. &newsid);
  2907. if (rc) {
  2908. pr_err("SELinux: unable to map context to SID"
  2909. "for (%s, %lu), rc=%d\n",
  2910. inode->i_sb->s_id, inode->i_ino, -rc);
  2911. return;
  2912. }
  2913. isec = backing_inode_security(dentry);
  2914. spin_lock(&isec->lock);
  2915. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  2916. isec->sid = newsid;
  2917. isec->initialized = LABEL_INITIALIZED;
  2918. spin_unlock(&isec->lock);
  2919. }
  2920. static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
  2921. {
  2922. const struct cred *cred = current_cred();
  2923. return dentry_has_perm(cred, dentry, FILE__GETATTR);
  2924. }
  2925. static int selinux_inode_listxattr(struct dentry *dentry)
  2926. {
  2927. const struct cred *cred = current_cred();
  2928. return dentry_has_perm(cred, dentry, FILE__GETATTR);
  2929. }
  2930. static int selinux_inode_removexattr(struct mnt_idmap *idmap,
  2931. struct dentry *dentry, const char *name)
  2932. {
  2933. /* if not a selinux xattr, only check the ordinary setattr perm */
  2934. if (strcmp(name, XATTR_NAME_SELINUX))
  2935. return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
  2936. if (!selinux_initialized())
  2937. return 0;
  2938. /* No one is allowed to remove a SELinux security label.
  2939. You can change the label, but all data must be labeled. */
  2940. return -EACCES;
  2941. }
  2942. static int selinux_path_notify(const struct path *path, u64 mask,
  2943. unsigned int obj_type)
  2944. {
  2945. int ret;
  2946. u32 perm;
  2947. struct common_audit_data ad;
  2948. ad.type = LSM_AUDIT_DATA_PATH;
  2949. ad.u.path = *path;
  2950. /*
  2951. * Set permission needed based on the type of mark being set.
  2952. * Performs an additional check for sb watches.
  2953. */
  2954. switch (obj_type) {
  2955. case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
  2956. perm = FILE__WATCH_MOUNT;
  2957. break;
  2958. case FSNOTIFY_OBJ_TYPE_SB:
  2959. perm = FILE__WATCH_SB;
  2960. ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
  2961. FILESYSTEM__WATCH, &ad);
  2962. if (ret)
  2963. return ret;
  2964. break;
  2965. case FSNOTIFY_OBJ_TYPE_INODE:
  2966. perm = FILE__WATCH;
  2967. break;
  2968. default:
  2969. return -EINVAL;
  2970. }
  2971. /* blocking watches require the file:watch_with_perm permission */
  2972. if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
  2973. perm |= FILE__WATCH_WITH_PERM;
  2974. /* watches on read-like events need the file:watch_reads permission */
  2975. if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
  2976. perm |= FILE__WATCH_READS;
  2977. return path_has_perm(current_cred(), path, perm);
  2978. }
  2979. /*
  2980. * Copy the inode security context value to the user.
  2981. *
  2982. * Permission check is handled by selinux_inode_getxattr hook.
  2983. */
  2984. static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
  2985. struct inode *inode, const char *name,
  2986. void **buffer, bool alloc)
  2987. {
  2988. u32 size;
  2989. int error;
  2990. char *context = NULL;
  2991. struct inode_security_struct *isec;
  2992. /*
  2993. * If we're not initialized yet, then we can't validate contexts, so
  2994. * just let vfs_getxattr fall back to using the on-disk xattr.
  2995. */
  2996. if (!selinux_initialized() ||
  2997. strcmp(name, XATTR_SELINUX_SUFFIX))
  2998. return -EOPNOTSUPP;
  2999. /*
  3000. * If the caller has CAP_MAC_ADMIN, then get the raw context
  3001. * value even if it is not defined by current policy; otherwise,
  3002. * use the in-core value under current policy.
  3003. * Use the non-auditing forms of the permission checks since
  3004. * getxattr may be called by unprivileged processes commonly
  3005. * and lack of permission just means that we fall back to the
  3006. * in-core context value, not a denial.
  3007. */
  3008. isec = inode_security(inode);
  3009. if (has_cap_mac_admin(false))
  3010. error = security_sid_to_context_force(isec->sid, &context,
  3011. &size);
  3012. else
  3013. error = security_sid_to_context(isec->sid,
  3014. &context, &size);
  3015. if (error)
  3016. return error;
  3017. error = size;
  3018. if (alloc) {
  3019. *buffer = context;
  3020. goto out_nofree;
  3021. }
  3022. kfree(context);
  3023. out_nofree:
  3024. return error;
  3025. }
  3026. static int selinux_inode_setsecurity(struct inode *inode, const char *name,
  3027. const void *value, size_t size, int flags)
  3028. {
  3029. struct inode_security_struct *isec = inode_security_novalidate(inode);
  3030. struct superblock_security_struct *sbsec;
  3031. u32 newsid;
  3032. int rc;
  3033. if (strcmp(name, XATTR_SELINUX_SUFFIX))
  3034. return -EOPNOTSUPP;
  3035. sbsec = selinux_superblock(inode->i_sb);
  3036. if (!(sbsec->flags & SBLABEL_MNT))
  3037. return -EOPNOTSUPP;
  3038. if (!value || !size)
  3039. return -EACCES;
  3040. rc = security_context_to_sid(value, size, &newsid,
  3041. GFP_KERNEL);
  3042. if (rc)
  3043. return rc;
  3044. spin_lock(&isec->lock);
  3045. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  3046. isec->sid = newsid;
  3047. isec->initialized = LABEL_INITIALIZED;
  3048. spin_unlock(&isec->lock);
  3049. return 0;
  3050. }
  3051. static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
  3052. {
  3053. const int len = sizeof(XATTR_NAME_SELINUX);
  3054. if (!selinux_initialized())
  3055. return 0;
  3056. if (buffer && len <= buffer_size)
  3057. memcpy(buffer, XATTR_NAME_SELINUX, len);
  3058. return len;
  3059. }
  3060. static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
  3061. {
  3062. struct inode_security_struct *isec = inode_security_novalidate(inode);
  3063. *secid = isec->sid;
  3064. }
  3065. static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
  3066. {
  3067. u32 sid;
  3068. struct task_security_struct *tsec;
  3069. struct cred *new_creds = *new;
  3070. if (new_creds == NULL) {
  3071. new_creds = prepare_creds();
  3072. if (!new_creds)
  3073. return -ENOMEM;
  3074. }
  3075. tsec = selinux_cred(new_creds);
  3076. /* Get label from overlay inode and set it in create_sid */
  3077. selinux_inode_getsecid(d_inode(src), &sid);
  3078. tsec->create_sid = sid;
  3079. *new = new_creds;
  3080. return 0;
  3081. }
  3082. static int selinux_inode_copy_up_xattr(struct dentry *dentry, const char *name)
  3083. {
  3084. /* The copy_up hook above sets the initial context on an inode, but we
  3085. * don't then want to overwrite it by blindly copying all the lower
  3086. * xattrs up. Instead, filter out SELinux-related xattrs following
  3087. * policy load.
  3088. */
  3089. if (selinux_initialized() && !strcmp(name, XATTR_NAME_SELINUX))
  3090. return -ECANCELED; /* Discard */
  3091. /*
  3092. * Any other attribute apart from SELINUX is not claimed, supported
  3093. * by selinux.
  3094. */
  3095. return -EOPNOTSUPP;
  3096. }
  3097. /* kernfs node operations */
  3098. static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
  3099. struct kernfs_node *kn)
  3100. {
  3101. const struct task_security_struct *tsec = selinux_cred(current_cred());
  3102. u32 parent_sid, newsid, clen;
  3103. int rc;
  3104. char *context;
  3105. rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
  3106. if (rc == -ENODATA)
  3107. return 0;
  3108. else if (rc < 0)
  3109. return rc;
  3110. clen = (u32)rc;
  3111. context = kmalloc(clen, GFP_KERNEL);
  3112. if (!context)
  3113. return -ENOMEM;
  3114. rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
  3115. if (rc < 0) {
  3116. kfree(context);
  3117. return rc;
  3118. }
  3119. rc = security_context_to_sid(context, clen, &parent_sid,
  3120. GFP_KERNEL);
  3121. kfree(context);
  3122. if (rc)
  3123. return rc;
  3124. if (tsec->create_sid) {
  3125. newsid = tsec->create_sid;
  3126. } else {
  3127. u16 secclass = inode_mode_to_security_class(kn->mode);
  3128. struct qstr q;
  3129. q.name = kn->name;
  3130. q.hash_len = hashlen_string(kn_dir, kn->name);
  3131. rc = security_transition_sid(tsec->sid,
  3132. parent_sid, secclass, &q,
  3133. &newsid);
  3134. if (rc)
  3135. return rc;
  3136. }
  3137. rc = security_sid_to_context_force(newsid,
  3138. &context, &clen);
  3139. if (rc)
  3140. return rc;
  3141. rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
  3142. XATTR_CREATE);
  3143. kfree(context);
  3144. return rc;
  3145. }
  3146. /* file security operations */
  3147. static int selinux_revalidate_file_permission(struct file *file, int mask)
  3148. {
  3149. const struct cred *cred = current_cred();
  3150. struct inode *inode = file_inode(file);
  3151. /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
  3152. if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
  3153. mask |= MAY_APPEND;
  3154. return file_has_perm(cred, file,
  3155. file_mask_to_av(inode->i_mode, mask));
  3156. }
  3157. static int selinux_file_permission(struct file *file, int mask)
  3158. {
  3159. struct inode *inode = file_inode(file);
  3160. struct file_security_struct *fsec = selinux_file(file);
  3161. struct inode_security_struct *isec;
  3162. u32 sid = current_sid();
  3163. if (!mask)
  3164. /* No permission to check. Existence test. */
  3165. return 0;
  3166. isec = inode_security(inode);
  3167. if (sid == fsec->sid && fsec->isid == isec->sid &&
  3168. fsec->pseqno == avc_policy_seqno())
  3169. /* No change since file_open check. */
  3170. return 0;
  3171. return selinux_revalidate_file_permission(file, mask);
  3172. }
  3173. static int selinux_file_alloc_security(struct file *file)
  3174. {
  3175. struct file_security_struct *fsec = selinux_file(file);
  3176. u32 sid = current_sid();
  3177. fsec->sid = sid;
  3178. fsec->fown_sid = sid;
  3179. return 0;
  3180. }
  3181. /*
  3182. * Check whether a task has the ioctl permission and cmd
  3183. * operation to an inode.
  3184. */
  3185. static int ioctl_has_perm(const struct cred *cred, struct file *file,
  3186. u32 requested, u16 cmd)
  3187. {
  3188. struct common_audit_data ad;
  3189. struct file_security_struct *fsec = selinux_file(file);
  3190. struct inode *inode = file_inode(file);
  3191. struct inode_security_struct *isec;
  3192. struct lsm_ioctlop_audit ioctl;
  3193. u32 ssid = cred_sid(cred);
  3194. int rc;
  3195. u8 driver = cmd >> 8;
  3196. u8 xperm = cmd & 0xff;
  3197. ad.type = LSM_AUDIT_DATA_IOCTL_OP;
  3198. ad.u.op = &ioctl;
  3199. ad.u.op->cmd = cmd;
  3200. ad.u.op->path = file->f_path;
  3201. if (ssid != fsec->sid) {
  3202. rc = avc_has_perm(ssid, fsec->sid,
  3203. SECCLASS_FD,
  3204. FD__USE,
  3205. &ad);
  3206. if (rc)
  3207. goto out;
  3208. }
  3209. if (unlikely(IS_PRIVATE(inode)))
  3210. return 0;
  3211. isec = inode_security(inode);
  3212. rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
  3213. requested, driver, xperm, &ad);
  3214. out:
  3215. return rc;
  3216. }
  3217. static int selinux_file_ioctl(struct file *file, unsigned int cmd,
  3218. unsigned long arg)
  3219. {
  3220. const struct cred *cred = current_cred();
  3221. int error = 0;
  3222. switch (cmd) {
  3223. case FIONREAD:
  3224. case FIBMAP:
  3225. case FIGETBSZ:
  3226. case FS_IOC_GETFLAGS:
  3227. case FS_IOC_GETVERSION:
  3228. error = file_has_perm(cred, file, FILE__GETATTR);
  3229. break;
  3230. case FS_IOC_SETFLAGS:
  3231. case FS_IOC_SETVERSION:
  3232. error = file_has_perm(cred, file, FILE__SETATTR);
  3233. break;
  3234. /* sys_ioctl() checks */
  3235. case FIONBIO:
  3236. case FIOASYNC:
  3237. error = file_has_perm(cred, file, 0);
  3238. break;
  3239. case KDSKBENT:
  3240. case KDSKBSENT:
  3241. error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
  3242. CAP_OPT_NONE, true);
  3243. break;
  3244. case FIOCLEX:
  3245. case FIONCLEX:
  3246. if (!selinux_policycap_ioctl_skip_cloexec())
  3247. error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
  3248. break;
  3249. /* default case assumes that the command will go
  3250. * to the file's ioctl() function.
  3251. */
  3252. default:
  3253. error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
  3254. }
  3255. return error;
  3256. }
  3257. static int selinux_file_ioctl_compat(struct file *file, unsigned int cmd,
  3258. unsigned long arg)
  3259. {
  3260. /*
  3261. * If we are in a 64-bit kernel running 32-bit userspace, we need to
  3262. * make sure we don't compare 32-bit flags to 64-bit flags.
  3263. */
  3264. switch (cmd) {
  3265. case FS_IOC32_GETFLAGS:
  3266. cmd = FS_IOC_GETFLAGS;
  3267. break;
  3268. case FS_IOC32_SETFLAGS:
  3269. cmd = FS_IOC_SETFLAGS;
  3270. break;
  3271. case FS_IOC32_GETVERSION:
  3272. cmd = FS_IOC_GETVERSION;
  3273. break;
  3274. case FS_IOC32_SETVERSION:
  3275. cmd = FS_IOC_SETVERSION;
  3276. break;
  3277. default:
  3278. break;
  3279. }
  3280. return selinux_file_ioctl(file, cmd, arg);
  3281. }
  3282. static int default_noexec __ro_after_init;
  3283. static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
  3284. {
  3285. const struct cred *cred = current_cred();
  3286. u32 sid = cred_sid(cred);
  3287. int rc = 0;
  3288. if (default_noexec &&
  3289. (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
  3290. (!shared && (prot & PROT_WRITE)))) {
  3291. /*
  3292. * We are making executable an anonymous mapping or a
  3293. * private file mapping that will also be writable.
  3294. * This has an additional check.
  3295. */
  3296. rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
  3297. PROCESS__EXECMEM, NULL);
  3298. if (rc)
  3299. goto error;
  3300. }
  3301. if (file) {
  3302. /* read access is always possible with a mapping */
  3303. u32 av = FILE__READ;
  3304. /* write access only matters if the mapping is shared */
  3305. if (shared && (prot & PROT_WRITE))
  3306. av |= FILE__WRITE;
  3307. if (prot & PROT_EXEC)
  3308. av |= FILE__EXECUTE;
  3309. return file_has_perm(cred, file, av);
  3310. }
  3311. error:
  3312. return rc;
  3313. }
  3314. static int selinux_mmap_addr(unsigned long addr)
  3315. {
  3316. int rc = 0;
  3317. if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
  3318. u32 sid = current_sid();
  3319. rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
  3320. MEMPROTECT__MMAP_ZERO, NULL);
  3321. }
  3322. return rc;
  3323. }
  3324. static int selinux_mmap_file(struct file *file,
  3325. unsigned long reqprot __always_unused,
  3326. unsigned long prot, unsigned long flags)
  3327. {
  3328. struct common_audit_data ad;
  3329. int rc;
  3330. if (file) {
  3331. ad.type = LSM_AUDIT_DATA_FILE;
  3332. ad.u.file = file;
  3333. rc = inode_has_perm(current_cred(), file_inode(file),
  3334. FILE__MAP, &ad);
  3335. if (rc)
  3336. return rc;
  3337. }
  3338. return file_map_prot_check(file, prot,
  3339. (flags & MAP_TYPE) == MAP_SHARED);
  3340. }
  3341. static int selinux_file_mprotect(struct vm_area_struct *vma,
  3342. unsigned long reqprot __always_unused,
  3343. unsigned long prot)
  3344. {
  3345. const struct cred *cred = current_cred();
  3346. u32 sid = cred_sid(cred);
  3347. if (default_noexec &&
  3348. (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
  3349. int rc = 0;
  3350. /*
  3351. * We don't use the vma_is_initial_heap() helper as it has
  3352. * a history of problems and is currently broken on systems
  3353. * where there is no heap, e.g. brk == start_brk. Before
  3354. * replacing the conditional below with vma_is_initial_heap(),
  3355. * or something similar, please ensure that the logic is the
  3356. * same as what we have below or you have tested every possible
  3357. * corner case you can think to test.
  3358. */
  3359. if (vma->vm_start >= vma->vm_mm->start_brk &&
  3360. vma->vm_end <= vma->vm_mm->brk) {
  3361. rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
  3362. PROCESS__EXECHEAP, NULL);
  3363. } else if (!vma->vm_file && (vma_is_initial_stack(vma) ||
  3364. vma_is_stack_for_current(vma))) {
  3365. rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
  3366. PROCESS__EXECSTACK, NULL);
  3367. } else if (vma->vm_file && vma->anon_vma) {
  3368. /*
  3369. * We are making executable a file mapping that has
  3370. * had some COW done. Since pages might have been
  3371. * written, check ability to execute the possibly
  3372. * modified content. This typically should only
  3373. * occur for text relocations.
  3374. */
  3375. rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
  3376. }
  3377. if (rc)
  3378. return rc;
  3379. }
  3380. return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
  3381. }
  3382. static int selinux_file_lock(struct file *file, unsigned int cmd)
  3383. {
  3384. const struct cred *cred = current_cred();
  3385. return file_has_perm(cred, file, FILE__LOCK);
  3386. }
  3387. static int selinux_file_fcntl(struct file *file, unsigned int cmd,
  3388. unsigned long arg)
  3389. {
  3390. const struct cred *cred = current_cred();
  3391. int err = 0;
  3392. switch (cmd) {
  3393. case F_SETFL:
  3394. if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
  3395. err = file_has_perm(cred, file, FILE__WRITE);
  3396. break;
  3397. }
  3398. fallthrough;
  3399. case F_SETOWN:
  3400. case F_SETSIG:
  3401. case F_GETFL:
  3402. case F_GETOWN:
  3403. case F_GETSIG:
  3404. case F_GETOWNER_UIDS:
  3405. /* Just check FD__USE permission */
  3406. err = file_has_perm(cred, file, 0);
  3407. break;
  3408. case F_GETLK:
  3409. case F_SETLK:
  3410. case F_SETLKW:
  3411. case F_OFD_GETLK:
  3412. case F_OFD_SETLK:
  3413. case F_OFD_SETLKW:
  3414. #if BITS_PER_LONG == 32
  3415. case F_GETLK64:
  3416. case F_SETLK64:
  3417. case F_SETLKW64:
  3418. #endif
  3419. err = file_has_perm(cred, file, FILE__LOCK);
  3420. break;
  3421. }
  3422. return err;
  3423. }
  3424. static void selinux_file_set_fowner(struct file *file)
  3425. {
  3426. struct file_security_struct *fsec;
  3427. fsec = selinux_file(file);
  3428. fsec->fown_sid = current_sid();
  3429. }
  3430. static int selinux_file_send_sigiotask(struct task_struct *tsk,
  3431. struct fown_struct *fown, int signum)
  3432. {
  3433. struct file *file;
  3434. u32 sid = task_sid_obj(tsk);
  3435. u32 perm;
  3436. struct file_security_struct *fsec;
  3437. /* struct fown_struct is never outside the context of a struct file */
  3438. file = fown->file;
  3439. fsec = selinux_file(file);
  3440. if (!signum)
  3441. perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
  3442. else
  3443. perm = signal_to_av(signum);
  3444. return avc_has_perm(fsec->fown_sid, sid,
  3445. SECCLASS_PROCESS, perm, NULL);
  3446. }
  3447. static int selinux_file_receive(struct file *file)
  3448. {
  3449. const struct cred *cred = current_cred();
  3450. return file_has_perm(cred, file, file_to_av(file));
  3451. }
  3452. static int selinux_file_open(struct file *file)
  3453. {
  3454. struct file_security_struct *fsec;
  3455. struct inode_security_struct *isec;
  3456. fsec = selinux_file(file);
  3457. isec = inode_security(file_inode(file));
  3458. /*
  3459. * Save inode label and policy sequence number
  3460. * at open-time so that selinux_file_permission
  3461. * can determine whether revalidation is necessary.
  3462. * Task label is already saved in the file security
  3463. * struct as its SID.
  3464. */
  3465. fsec->isid = isec->sid;
  3466. fsec->pseqno = avc_policy_seqno();
  3467. /*
  3468. * Since the inode label or policy seqno may have changed
  3469. * between the selinux_inode_permission check and the saving
  3470. * of state above, recheck that access is still permitted.
  3471. * Otherwise, access might never be revalidated against the
  3472. * new inode label or new policy.
  3473. * This check is not redundant - do not remove.
  3474. */
  3475. return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
  3476. }
  3477. /* task security operations */
  3478. static int selinux_task_alloc(struct task_struct *task,
  3479. unsigned long clone_flags)
  3480. {
  3481. u32 sid = current_sid();
  3482. return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
  3483. }
  3484. /*
  3485. * prepare a new set of credentials for modification
  3486. */
  3487. static int selinux_cred_prepare(struct cred *new, const struct cred *old,
  3488. gfp_t gfp)
  3489. {
  3490. const struct task_security_struct *old_tsec = selinux_cred(old);
  3491. struct task_security_struct *tsec = selinux_cred(new);
  3492. *tsec = *old_tsec;
  3493. return 0;
  3494. }
  3495. /*
  3496. * transfer the SELinux data to a blank set of creds
  3497. */
  3498. static void selinux_cred_transfer(struct cred *new, const struct cred *old)
  3499. {
  3500. const struct task_security_struct *old_tsec = selinux_cred(old);
  3501. struct task_security_struct *tsec = selinux_cred(new);
  3502. *tsec = *old_tsec;
  3503. }
  3504. static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
  3505. {
  3506. *secid = cred_sid(c);
  3507. }
  3508. /*
  3509. * set the security data for a kernel service
  3510. * - all the creation contexts are set to unlabelled
  3511. */
  3512. static int selinux_kernel_act_as(struct cred *new, u32 secid)
  3513. {
  3514. struct task_security_struct *tsec = selinux_cred(new);
  3515. u32 sid = current_sid();
  3516. int ret;
  3517. ret = avc_has_perm(sid, secid,
  3518. SECCLASS_KERNEL_SERVICE,
  3519. KERNEL_SERVICE__USE_AS_OVERRIDE,
  3520. NULL);
  3521. if (ret == 0) {
  3522. tsec->sid = secid;
  3523. tsec->create_sid = 0;
  3524. tsec->keycreate_sid = 0;
  3525. tsec->sockcreate_sid = 0;
  3526. }
  3527. return ret;
  3528. }
  3529. /*
  3530. * set the file creation context in a security record to the same as the
  3531. * objective context of the specified inode
  3532. */
  3533. static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
  3534. {
  3535. struct inode_security_struct *isec = inode_security(inode);
  3536. struct task_security_struct *tsec = selinux_cred(new);
  3537. u32 sid = current_sid();
  3538. int ret;
  3539. ret = avc_has_perm(sid, isec->sid,
  3540. SECCLASS_KERNEL_SERVICE,
  3541. KERNEL_SERVICE__CREATE_FILES_AS,
  3542. NULL);
  3543. if (ret == 0)
  3544. tsec->create_sid = isec->sid;
  3545. return ret;
  3546. }
  3547. static int selinux_kernel_module_request(char *kmod_name)
  3548. {
  3549. struct common_audit_data ad;
  3550. ad.type = LSM_AUDIT_DATA_KMOD;
  3551. ad.u.kmod_name = kmod_name;
  3552. return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
  3553. SYSTEM__MODULE_REQUEST, &ad);
  3554. }
  3555. static int selinux_kernel_module_from_file(struct file *file)
  3556. {
  3557. struct common_audit_data ad;
  3558. struct inode_security_struct *isec;
  3559. struct file_security_struct *fsec;
  3560. u32 sid = current_sid();
  3561. int rc;
  3562. /* init_module */
  3563. if (file == NULL)
  3564. return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
  3565. SYSTEM__MODULE_LOAD, NULL);
  3566. /* finit_module */
  3567. ad.type = LSM_AUDIT_DATA_FILE;
  3568. ad.u.file = file;
  3569. fsec = selinux_file(file);
  3570. if (sid != fsec->sid) {
  3571. rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
  3572. if (rc)
  3573. return rc;
  3574. }
  3575. isec = inode_security(file_inode(file));
  3576. return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM,
  3577. SYSTEM__MODULE_LOAD, &ad);
  3578. }
  3579. static int selinux_kernel_read_file(struct file *file,
  3580. enum kernel_read_file_id id,
  3581. bool contents)
  3582. {
  3583. int rc = 0;
  3584. switch (id) {
  3585. case READING_MODULE:
  3586. rc = selinux_kernel_module_from_file(contents ? file : NULL);
  3587. break;
  3588. default:
  3589. break;
  3590. }
  3591. return rc;
  3592. }
  3593. static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
  3594. {
  3595. int rc = 0;
  3596. switch (id) {
  3597. case LOADING_MODULE:
  3598. rc = selinux_kernel_module_from_file(NULL);
  3599. break;
  3600. default:
  3601. break;
  3602. }
  3603. return rc;
  3604. }
  3605. static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
  3606. {
  3607. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3608. PROCESS__SETPGID, NULL);
  3609. }
  3610. static int selinux_task_getpgid(struct task_struct *p)
  3611. {
  3612. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3613. PROCESS__GETPGID, NULL);
  3614. }
  3615. static int selinux_task_getsid(struct task_struct *p)
  3616. {
  3617. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3618. PROCESS__GETSESSION, NULL);
  3619. }
  3620. static void selinux_current_getsecid_subj(u32 *secid)
  3621. {
  3622. *secid = current_sid();
  3623. }
  3624. static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
  3625. {
  3626. *secid = task_sid_obj(p);
  3627. }
  3628. static int selinux_task_setnice(struct task_struct *p, int nice)
  3629. {
  3630. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3631. PROCESS__SETSCHED, NULL);
  3632. }
  3633. static int selinux_task_setioprio(struct task_struct *p, int ioprio)
  3634. {
  3635. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3636. PROCESS__SETSCHED, NULL);
  3637. }
  3638. static int selinux_task_getioprio(struct task_struct *p)
  3639. {
  3640. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3641. PROCESS__GETSCHED, NULL);
  3642. }
  3643. static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
  3644. unsigned int flags)
  3645. {
  3646. u32 av = 0;
  3647. if (!flags)
  3648. return 0;
  3649. if (flags & LSM_PRLIMIT_WRITE)
  3650. av |= PROCESS__SETRLIMIT;
  3651. if (flags & LSM_PRLIMIT_READ)
  3652. av |= PROCESS__GETRLIMIT;
  3653. return avc_has_perm(cred_sid(cred), cred_sid(tcred),
  3654. SECCLASS_PROCESS, av, NULL);
  3655. }
  3656. static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
  3657. struct rlimit *new_rlim)
  3658. {
  3659. struct rlimit *old_rlim = p->signal->rlim + resource;
  3660. /* Control the ability to change the hard limit (whether
  3661. lowering or raising it), so that the hard limit can
  3662. later be used as a safe reset point for the soft limit
  3663. upon context transitions. See selinux_bprm_committing_creds. */
  3664. if (old_rlim->rlim_max != new_rlim->rlim_max)
  3665. return avc_has_perm(current_sid(), task_sid_obj(p),
  3666. SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
  3667. return 0;
  3668. }
  3669. static int selinux_task_setscheduler(struct task_struct *p)
  3670. {
  3671. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3672. PROCESS__SETSCHED, NULL);
  3673. }
  3674. static int selinux_task_getscheduler(struct task_struct *p)
  3675. {
  3676. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3677. PROCESS__GETSCHED, NULL);
  3678. }
  3679. static int selinux_task_movememory(struct task_struct *p)
  3680. {
  3681. return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
  3682. PROCESS__SETSCHED, NULL);
  3683. }
  3684. static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
  3685. int sig, const struct cred *cred)
  3686. {
  3687. u32 secid;
  3688. u32 perm;
  3689. if (!sig)
  3690. perm = PROCESS__SIGNULL; /* null signal; existence test */
  3691. else
  3692. perm = signal_to_av(sig);
  3693. if (!cred)
  3694. secid = current_sid();
  3695. else
  3696. secid = cred_sid(cred);
  3697. return avc_has_perm(secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
  3698. }
  3699. static void selinux_task_to_inode(struct task_struct *p,
  3700. struct inode *inode)
  3701. {
  3702. struct inode_security_struct *isec = selinux_inode(inode);
  3703. u32 sid = task_sid_obj(p);
  3704. spin_lock(&isec->lock);
  3705. isec->sclass = inode_mode_to_security_class(inode->i_mode);
  3706. isec->sid = sid;
  3707. isec->initialized = LABEL_INITIALIZED;
  3708. spin_unlock(&isec->lock);
  3709. }
  3710. static int selinux_userns_create(const struct cred *cred)
  3711. {
  3712. u32 sid = current_sid();
  3713. return avc_has_perm(sid, sid, SECCLASS_USER_NAMESPACE,
  3714. USER_NAMESPACE__CREATE, NULL);
  3715. }
  3716. /* Returns error only if unable to parse addresses */
  3717. static int selinux_parse_skb_ipv4(struct sk_buff *skb,
  3718. struct common_audit_data *ad, u8 *proto)
  3719. {
  3720. int offset, ihlen, ret = -EINVAL;
  3721. struct iphdr _iph, *ih;
  3722. offset = skb_network_offset(skb);
  3723. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  3724. if (ih == NULL)
  3725. goto out;
  3726. ihlen = ih->ihl * 4;
  3727. if (ihlen < sizeof(_iph))
  3728. goto out;
  3729. ad->u.net->v4info.saddr = ih->saddr;
  3730. ad->u.net->v4info.daddr = ih->daddr;
  3731. ret = 0;
  3732. if (proto)
  3733. *proto = ih->protocol;
  3734. switch (ih->protocol) {
  3735. case IPPROTO_TCP: {
  3736. struct tcphdr _tcph, *th;
  3737. if (ntohs(ih->frag_off) & IP_OFFSET)
  3738. break;
  3739. offset += ihlen;
  3740. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  3741. if (th == NULL)
  3742. break;
  3743. ad->u.net->sport = th->source;
  3744. ad->u.net->dport = th->dest;
  3745. break;
  3746. }
  3747. case IPPROTO_UDP: {
  3748. struct udphdr _udph, *uh;
  3749. if (ntohs(ih->frag_off) & IP_OFFSET)
  3750. break;
  3751. offset += ihlen;
  3752. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  3753. if (uh == NULL)
  3754. break;
  3755. ad->u.net->sport = uh->source;
  3756. ad->u.net->dport = uh->dest;
  3757. break;
  3758. }
  3759. case IPPROTO_DCCP: {
  3760. struct dccp_hdr _dccph, *dh;
  3761. if (ntohs(ih->frag_off) & IP_OFFSET)
  3762. break;
  3763. offset += ihlen;
  3764. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  3765. if (dh == NULL)
  3766. break;
  3767. ad->u.net->sport = dh->dccph_sport;
  3768. ad->u.net->dport = dh->dccph_dport;
  3769. break;
  3770. }
  3771. #if IS_ENABLED(CONFIG_IP_SCTP)
  3772. case IPPROTO_SCTP: {
  3773. struct sctphdr _sctph, *sh;
  3774. if (ntohs(ih->frag_off) & IP_OFFSET)
  3775. break;
  3776. offset += ihlen;
  3777. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  3778. if (sh == NULL)
  3779. break;
  3780. ad->u.net->sport = sh->source;
  3781. ad->u.net->dport = sh->dest;
  3782. break;
  3783. }
  3784. #endif
  3785. default:
  3786. break;
  3787. }
  3788. out:
  3789. return ret;
  3790. }
  3791. #if IS_ENABLED(CONFIG_IPV6)
  3792. /* Returns error only if unable to parse addresses */
  3793. static int selinux_parse_skb_ipv6(struct sk_buff *skb,
  3794. struct common_audit_data *ad, u8 *proto)
  3795. {
  3796. u8 nexthdr;
  3797. int ret = -EINVAL, offset;
  3798. struct ipv6hdr _ipv6h, *ip6;
  3799. __be16 frag_off;
  3800. offset = skb_network_offset(skb);
  3801. ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
  3802. if (ip6 == NULL)
  3803. goto out;
  3804. ad->u.net->v6info.saddr = ip6->saddr;
  3805. ad->u.net->v6info.daddr = ip6->daddr;
  3806. ret = 0;
  3807. nexthdr = ip6->nexthdr;
  3808. offset += sizeof(_ipv6h);
  3809. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  3810. if (offset < 0)
  3811. goto out;
  3812. if (proto)
  3813. *proto = nexthdr;
  3814. switch (nexthdr) {
  3815. case IPPROTO_TCP: {
  3816. struct tcphdr _tcph, *th;
  3817. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  3818. if (th == NULL)
  3819. break;
  3820. ad->u.net->sport = th->source;
  3821. ad->u.net->dport = th->dest;
  3822. break;
  3823. }
  3824. case IPPROTO_UDP: {
  3825. struct udphdr _udph, *uh;
  3826. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  3827. if (uh == NULL)
  3828. break;
  3829. ad->u.net->sport = uh->source;
  3830. ad->u.net->dport = uh->dest;
  3831. break;
  3832. }
  3833. case IPPROTO_DCCP: {
  3834. struct dccp_hdr _dccph, *dh;
  3835. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  3836. if (dh == NULL)
  3837. break;
  3838. ad->u.net->sport = dh->dccph_sport;
  3839. ad->u.net->dport = dh->dccph_dport;
  3840. break;
  3841. }
  3842. #if IS_ENABLED(CONFIG_IP_SCTP)
  3843. case IPPROTO_SCTP: {
  3844. struct sctphdr _sctph, *sh;
  3845. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  3846. if (sh == NULL)
  3847. break;
  3848. ad->u.net->sport = sh->source;
  3849. ad->u.net->dport = sh->dest;
  3850. break;
  3851. }
  3852. #endif
  3853. /* includes fragments */
  3854. default:
  3855. break;
  3856. }
  3857. out:
  3858. return ret;
  3859. }
  3860. #endif /* IPV6 */
  3861. static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
  3862. char **_addrp, int src, u8 *proto)
  3863. {
  3864. char *addrp;
  3865. int ret;
  3866. switch (ad->u.net->family) {
  3867. case PF_INET:
  3868. ret = selinux_parse_skb_ipv4(skb, ad, proto);
  3869. if (ret)
  3870. goto parse_error;
  3871. addrp = (char *)(src ? &ad->u.net->v4info.saddr :
  3872. &ad->u.net->v4info.daddr);
  3873. goto okay;
  3874. #if IS_ENABLED(CONFIG_IPV6)
  3875. case PF_INET6:
  3876. ret = selinux_parse_skb_ipv6(skb, ad, proto);
  3877. if (ret)
  3878. goto parse_error;
  3879. addrp = (char *)(src ? &ad->u.net->v6info.saddr :
  3880. &ad->u.net->v6info.daddr);
  3881. goto okay;
  3882. #endif /* IPV6 */
  3883. default:
  3884. addrp = NULL;
  3885. goto okay;
  3886. }
  3887. parse_error:
  3888. pr_warn(
  3889. "SELinux: failure in selinux_parse_skb(),"
  3890. " unable to parse packet\n");
  3891. return ret;
  3892. okay:
  3893. if (_addrp)
  3894. *_addrp = addrp;
  3895. return 0;
  3896. }
  3897. /**
  3898. * selinux_skb_peerlbl_sid - Determine the peer label of a packet
  3899. * @skb: the packet
  3900. * @family: protocol family
  3901. * @sid: the packet's peer label SID
  3902. *
  3903. * Description:
  3904. * Check the various different forms of network peer labeling and determine
  3905. * the peer label/SID for the packet; most of the magic actually occurs in
  3906. * the security server function security_net_peersid_cmp(). The function
  3907. * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
  3908. * or -EACCES if @sid is invalid due to inconsistencies with the different
  3909. * peer labels.
  3910. *
  3911. */
  3912. static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
  3913. {
  3914. int err;
  3915. u32 xfrm_sid;
  3916. u32 nlbl_sid;
  3917. u32 nlbl_type;
  3918. err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
  3919. if (unlikely(err))
  3920. return -EACCES;
  3921. err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
  3922. if (unlikely(err))
  3923. return -EACCES;
  3924. err = security_net_peersid_resolve(nlbl_sid,
  3925. nlbl_type, xfrm_sid, sid);
  3926. if (unlikely(err)) {
  3927. pr_warn(
  3928. "SELinux: failure in selinux_skb_peerlbl_sid(),"
  3929. " unable to determine packet's peer label\n");
  3930. return -EACCES;
  3931. }
  3932. return 0;
  3933. }
  3934. /**
  3935. * selinux_conn_sid - Determine the child socket label for a connection
  3936. * @sk_sid: the parent socket's SID
  3937. * @skb_sid: the packet's SID
  3938. * @conn_sid: the resulting connection SID
  3939. *
  3940. * If @skb_sid is valid then the user:role:type information from @sk_sid is
  3941. * combined with the MLS information from @skb_sid in order to create
  3942. * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
  3943. * of @sk_sid. Returns zero on success, negative values on failure.
  3944. *
  3945. */
  3946. static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
  3947. {
  3948. int err = 0;
  3949. if (skb_sid != SECSID_NULL)
  3950. err = security_sid_mls_copy(sk_sid, skb_sid,
  3951. conn_sid);
  3952. else
  3953. *conn_sid = sk_sid;
  3954. return err;
  3955. }
  3956. /* socket security operations */
  3957. static int socket_sockcreate_sid(const struct task_security_struct *tsec,
  3958. u16 secclass, u32 *socksid)
  3959. {
  3960. if (tsec->sockcreate_sid > SECSID_NULL) {
  3961. *socksid = tsec->sockcreate_sid;
  3962. return 0;
  3963. }
  3964. return security_transition_sid(tsec->sid, tsec->sid,
  3965. secclass, NULL, socksid);
  3966. }
  3967. static int sock_has_perm(struct sock *sk, u32 perms)
  3968. {
  3969. struct sk_security_struct *sksec = selinux_sock(sk);
  3970. struct common_audit_data ad;
  3971. struct lsm_network_audit net;
  3972. if (sksec->sid == SECINITSID_KERNEL)
  3973. return 0;
  3974. /*
  3975. * Before POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT, sockets that
  3976. * inherited the kernel context from early boot used to be skipped
  3977. * here, so preserve that behavior unless the capability is set.
  3978. *
  3979. * By setting the capability the policy signals that it is ready
  3980. * for this quirk to be fixed. Note that sockets created by a kernel
  3981. * thread or a usermode helper executed without a transition will
  3982. * still be skipped in this check regardless of the policycap
  3983. * setting.
  3984. */
  3985. if (!selinux_policycap_userspace_initial_context() &&
  3986. sksec->sid == SECINITSID_INIT)
  3987. return 0;
  3988. ad_net_init_from_sk(&ad, &net, sk);
  3989. return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms,
  3990. &ad);
  3991. }
  3992. static int selinux_socket_create(int family, int type,
  3993. int protocol, int kern)
  3994. {
  3995. const struct task_security_struct *tsec = selinux_cred(current_cred());
  3996. u32 newsid;
  3997. u16 secclass;
  3998. int rc;
  3999. if (kern)
  4000. return 0;
  4001. secclass = socket_type_to_security_class(family, type, protocol);
  4002. rc = socket_sockcreate_sid(tsec, secclass, &newsid);
  4003. if (rc)
  4004. return rc;
  4005. return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
  4006. }
  4007. static int selinux_socket_post_create(struct socket *sock, int family,
  4008. int type, int protocol, int kern)
  4009. {
  4010. const struct task_security_struct *tsec = selinux_cred(current_cred());
  4011. struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
  4012. struct sk_security_struct *sksec;
  4013. u16 sclass = socket_type_to_security_class(family, type, protocol);
  4014. u32 sid = SECINITSID_KERNEL;
  4015. int err = 0;
  4016. if (!kern) {
  4017. err = socket_sockcreate_sid(tsec, sclass, &sid);
  4018. if (err)
  4019. return err;
  4020. }
  4021. isec->sclass = sclass;
  4022. isec->sid = sid;
  4023. isec->initialized = LABEL_INITIALIZED;
  4024. if (sock->sk) {
  4025. sksec = selinux_sock(sock->sk);
  4026. sksec->sclass = sclass;
  4027. sksec->sid = sid;
  4028. /* Allows detection of the first association on this socket */
  4029. if (sksec->sclass == SECCLASS_SCTP_SOCKET)
  4030. sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
  4031. err = selinux_netlbl_socket_post_create(sock->sk, family);
  4032. }
  4033. return err;
  4034. }
  4035. static int selinux_socket_socketpair(struct socket *socka,
  4036. struct socket *sockb)
  4037. {
  4038. struct sk_security_struct *sksec_a = selinux_sock(socka->sk);
  4039. struct sk_security_struct *sksec_b = selinux_sock(sockb->sk);
  4040. sksec_a->peer_sid = sksec_b->sid;
  4041. sksec_b->peer_sid = sksec_a->sid;
  4042. return 0;
  4043. }
  4044. /* Range of port numbers used to automatically bind.
  4045. Need to determine whether we should perform a name_bind
  4046. permission check between the socket and the port number. */
  4047. static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
  4048. {
  4049. struct sock *sk = sock->sk;
  4050. struct sk_security_struct *sksec = selinux_sock(sk);
  4051. u16 family;
  4052. int err;
  4053. err = sock_has_perm(sk, SOCKET__BIND);
  4054. if (err)
  4055. goto out;
  4056. /* If PF_INET or PF_INET6, check name_bind permission for the port. */
  4057. family = sk->sk_family;
  4058. if (family == PF_INET || family == PF_INET6) {
  4059. char *addrp;
  4060. struct common_audit_data ad;
  4061. struct lsm_network_audit net = {0,};
  4062. struct sockaddr_in *addr4 = NULL;
  4063. struct sockaddr_in6 *addr6 = NULL;
  4064. u16 family_sa;
  4065. unsigned short snum;
  4066. u32 sid, node_perm;
  4067. /*
  4068. * sctp_bindx(3) calls via selinux_sctp_bind_connect()
  4069. * that validates multiple binding addresses. Because of this
  4070. * need to check address->sa_family as it is possible to have
  4071. * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
  4072. */
  4073. if (addrlen < offsetofend(struct sockaddr, sa_family))
  4074. return -EINVAL;
  4075. family_sa = address->sa_family;
  4076. switch (family_sa) {
  4077. case AF_UNSPEC:
  4078. case AF_INET:
  4079. if (addrlen < sizeof(struct sockaddr_in))
  4080. return -EINVAL;
  4081. addr4 = (struct sockaddr_in *)address;
  4082. if (family_sa == AF_UNSPEC) {
  4083. if (family == PF_INET6) {
  4084. /* Length check from inet6_bind_sk() */
  4085. if (addrlen < SIN6_LEN_RFC2133)
  4086. return -EINVAL;
  4087. /* Family check from __inet6_bind() */
  4088. goto err_af;
  4089. }
  4090. /* see __inet_bind(), we only want to allow
  4091. * AF_UNSPEC if the address is INADDR_ANY
  4092. */
  4093. if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
  4094. goto err_af;
  4095. family_sa = AF_INET;
  4096. }
  4097. snum = ntohs(addr4->sin_port);
  4098. addrp = (char *)&addr4->sin_addr.s_addr;
  4099. break;
  4100. case AF_INET6:
  4101. if (addrlen < SIN6_LEN_RFC2133)
  4102. return -EINVAL;
  4103. addr6 = (struct sockaddr_in6 *)address;
  4104. snum = ntohs(addr6->sin6_port);
  4105. addrp = (char *)&addr6->sin6_addr.s6_addr;
  4106. break;
  4107. default:
  4108. goto err_af;
  4109. }
  4110. ad.type = LSM_AUDIT_DATA_NET;
  4111. ad.u.net = &net;
  4112. ad.u.net->sport = htons(snum);
  4113. ad.u.net->family = family_sa;
  4114. if (snum) {
  4115. int low, high;
  4116. inet_get_local_port_range(sock_net(sk), &low, &high);
  4117. if (inet_port_requires_bind_service(sock_net(sk), snum) ||
  4118. snum < low || snum > high) {
  4119. err = sel_netport_sid(sk->sk_protocol,
  4120. snum, &sid);
  4121. if (err)
  4122. goto out;
  4123. err = avc_has_perm(sksec->sid, sid,
  4124. sksec->sclass,
  4125. SOCKET__NAME_BIND, &ad);
  4126. if (err)
  4127. goto out;
  4128. }
  4129. }
  4130. switch (sksec->sclass) {
  4131. case SECCLASS_TCP_SOCKET:
  4132. node_perm = TCP_SOCKET__NODE_BIND;
  4133. break;
  4134. case SECCLASS_UDP_SOCKET:
  4135. node_perm = UDP_SOCKET__NODE_BIND;
  4136. break;
  4137. case SECCLASS_DCCP_SOCKET:
  4138. node_perm = DCCP_SOCKET__NODE_BIND;
  4139. break;
  4140. case SECCLASS_SCTP_SOCKET:
  4141. node_perm = SCTP_SOCKET__NODE_BIND;
  4142. break;
  4143. default:
  4144. node_perm = RAWIP_SOCKET__NODE_BIND;
  4145. break;
  4146. }
  4147. err = sel_netnode_sid(addrp, family_sa, &sid);
  4148. if (err)
  4149. goto out;
  4150. if (family_sa == AF_INET)
  4151. ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
  4152. else
  4153. ad.u.net->v6info.saddr = addr6->sin6_addr;
  4154. err = avc_has_perm(sksec->sid, sid,
  4155. sksec->sclass, node_perm, &ad);
  4156. if (err)
  4157. goto out;
  4158. }
  4159. out:
  4160. return err;
  4161. err_af:
  4162. /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
  4163. if (sksec->sclass == SECCLASS_SCTP_SOCKET)
  4164. return -EINVAL;
  4165. return -EAFNOSUPPORT;
  4166. }
  4167. /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
  4168. * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
  4169. */
  4170. static int selinux_socket_connect_helper(struct socket *sock,
  4171. struct sockaddr *address, int addrlen)
  4172. {
  4173. struct sock *sk = sock->sk;
  4174. struct sk_security_struct *sksec = selinux_sock(sk);
  4175. int err;
  4176. err = sock_has_perm(sk, SOCKET__CONNECT);
  4177. if (err)
  4178. return err;
  4179. if (addrlen < offsetofend(struct sockaddr, sa_family))
  4180. return -EINVAL;
  4181. /* connect(AF_UNSPEC) has special handling, as it is a documented
  4182. * way to disconnect the socket
  4183. */
  4184. if (address->sa_family == AF_UNSPEC)
  4185. return 0;
  4186. /*
  4187. * If a TCP, DCCP or SCTP socket, check name_connect permission
  4188. * for the port.
  4189. */
  4190. if (sksec->sclass == SECCLASS_TCP_SOCKET ||
  4191. sksec->sclass == SECCLASS_DCCP_SOCKET ||
  4192. sksec->sclass == SECCLASS_SCTP_SOCKET) {
  4193. struct common_audit_data ad;
  4194. struct lsm_network_audit net = {0,};
  4195. struct sockaddr_in *addr4 = NULL;
  4196. struct sockaddr_in6 *addr6 = NULL;
  4197. unsigned short snum;
  4198. u32 sid, perm;
  4199. /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
  4200. * that validates multiple connect addresses. Because of this
  4201. * need to check address->sa_family as it is possible to have
  4202. * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
  4203. */
  4204. switch (address->sa_family) {
  4205. case AF_INET:
  4206. addr4 = (struct sockaddr_in *)address;
  4207. if (addrlen < sizeof(struct sockaddr_in))
  4208. return -EINVAL;
  4209. snum = ntohs(addr4->sin_port);
  4210. break;
  4211. case AF_INET6:
  4212. addr6 = (struct sockaddr_in6 *)address;
  4213. if (addrlen < SIN6_LEN_RFC2133)
  4214. return -EINVAL;
  4215. snum = ntohs(addr6->sin6_port);
  4216. break;
  4217. default:
  4218. /* Note that SCTP services expect -EINVAL, whereas
  4219. * others expect -EAFNOSUPPORT.
  4220. */
  4221. if (sksec->sclass == SECCLASS_SCTP_SOCKET)
  4222. return -EINVAL;
  4223. else
  4224. return -EAFNOSUPPORT;
  4225. }
  4226. err = sel_netport_sid(sk->sk_protocol, snum, &sid);
  4227. if (err)
  4228. return err;
  4229. switch (sksec->sclass) {
  4230. case SECCLASS_TCP_SOCKET:
  4231. perm = TCP_SOCKET__NAME_CONNECT;
  4232. break;
  4233. case SECCLASS_DCCP_SOCKET:
  4234. perm = DCCP_SOCKET__NAME_CONNECT;
  4235. break;
  4236. case SECCLASS_SCTP_SOCKET:
  4237. perm = SCTP_SOCKET__NAME_CONNECT;
  4238. break;
  4239. }
  4240. ad.type = LSM_AUDIT_DATA_NET;
  4241. ad.u.net = &net;
  4242. ad.u.net->dport = htons(snum);
  4243. ad.u.net->family = address->sa_family;
  4244. err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
  4245. if (err)
  4246. return err;
  4247. }
  4248. return 0;
  4249. }
  4250. /* Supports connect(2), see comments in selinux_socket_connect_helper() */
  4251. static int selinux_socket_connect(struct socket *sock,
  4252. struct sockaddr *address, int addrlen)
  4253. {
  4254. int err;
  4255. struct sock *sk = sock->sk;
  4256. err = selinux_socket_connect_helper(sock, address, addrlen);
  4257. if (err)
  4258. return err;
  4259. return selinux_netlbl_socket_connect(sk, address);
  4260. }
  4261. static int selinux_socket_listen(struct socket *sock, int backlog)
  4262. {
  4263. return sock_has_perm(sock->sk, SOCKET__LISTEN);
  4264. }
  4265. static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
  4266. {
  4267. int err;
  4268. struct inode_security_struct *isec;
  4269. struct inode_security_struct *newisec;
  4270. u16 sclass;
  4271. u32 sid;
  4272. err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
  4273. if (err)
  4274. return err;
  4275. isec = inode_security_novalidate(SOCK_INODE(sock));
  4276. spin_lock(&isec->lock);
  4277. sclass = isec->sclass;
  4278. sid = isec->sid;
  4279. spin_unlock(&isec->lock);
  4280. newisec = inode_security_novalidate(SOCK_INODE(newsock));
  4281. newisec->sclass = sclass;
  4282. newisec->sid = sid;
  4283. newisec->initialized = LABEL_INITIALIZED;
  4284. return 0;
  4285. }
  4286. static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  4287. int size)
  4288. {
  4289. return sock_has_perm(sock->sk, SOCKET__WRITE);
  4290. }
  4291. static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
  4292. int size, int flags)
  4293. {
  4294. return sock_has_perm(sock->sk, SOCKET__READ);
  4295. }
  4296. static int selinux_socket_getsockname(struct socket *sock)
  4297. {
  4298. return sock_has_perm(sock->sk, SOCKET__GETATTR);
  4299. }
  4300. static int selinux_socket_getpeername(struct socket *sock)
  4301. {
  4302. return sock_has_perm(sock->sk, SOCKET__GETATTR);
  4303. }
  4304. static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
  4305. {
  4306. int err;
  4307. err = sock_has_perm(sock->sk, SOCKET__SETOPT);
  4308. if (err)
  4309. return err;
  4310. return selinux_netlbl_socket_setsockopt(sock, level, optname);
  4311. }
  4312. static int selinux_socket_getsockopt(struct socket *sock, int level,
  4313. int optname)
  4314. {
  4315. return sock_has_perm(sock->sk, SOCKET__GETOPT);
  4316. }
  4317. static int selinux_socket_shutdown(struct socket *sock, int how)
  4318. {
  4319. return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
  4320. }
  4321. static int selinux_socket_unix_stream_connect(struct sock *sock,
  4322. struct sock *other,
  4323. struct sock *newsk)
  4324. {
  4325. struct sk_security_struct *sksec_sock = selinux_sock(sock);
  4326. struct sk_security_struct *sksec_other = selinux_sock(other);
  4327. struct sk_security_struct *sksec_new = selinux_sock(newsk);
  4328. struct common_audit_data ad;
  4329. struct lsm_network_audit net;
  4330. int err;
  4331. ad_net_init_from_sk(&ad, &net, other);
  4332. err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
  4333. sksec_other->sclass,
  4334. UNIX_STREAM_SOCKET__CONNECTTO, &ad);
  4335. if (err)
  4336. return err;
  4337. /* server child socket */
  4338. sksec_new->peer_sid = sksec_sock->sid;
  4339. err = security_sid_mls_copy(sksec_other->sid,
  4340. sksec_sock->sid, &sksec_new->sid);
  4341. if (err)
  4342. return err;
  4343. /* connecting socket */
  4344. sksec_sock->peer_sid = sksec_new->sid;
  4345. return 0;
  4346. }
  4347. static int selinux_socket_unix_may_send(struct socket *sock,
  4348. struct socket *other)
  4349. {
  4350. struct sk_security_struct *ssec = selinux_sock(sock->sk);
  4351. struct sk_security_struct *osec = selinux_sock(other->sk);
  4352. struct common_audit_data ad;
  4353. struct lsm_network_audit net;
  4354. ad_net_init_from_sk(&ad, &net, other->sk);
  4355. return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
  4356. &ad);
  4357. }
  4358. static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
  4359. char *addrp, u16 family, u32 peer_sid,
  4360. struct common_audit_data *ad)
  4361. {
  4362. int err;
  4363. u32 if_sid;
  4364. u32 node_sid;
  4365. err = sel_netif_sid(ns, ifindex, &if_sid);
  4366. if (err)
  4367. return err;
  4368. err = avc_has_perm(peer_sid, if_sid,
  4369. SECCLASS_NETIF, NETIF__INGRESS, ad);
  4370. if (err)
  4371. return err;
  4372. err = sel_netnode_sid(addrp, family, &node_sid);
  4373. if (err)
  4374. return err;
  4375. return avc_has_perm(peer_sid, node_sid,
  4376. SECCLASS_NODE, NODE__RECVFROM, ad);
  4377. }
  4378. static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
  4379. u16 family)
  4380. {
  4381. int err = 0;
  4382. struct sk_security_struct *sksec = selinux_sock(sk);
  4383. u32 sk_sid = sksec->sid;
  4384. struct common_audit_data ad;
  4385. struct lsm_network_audit net;
  4386. char *addrp;
  4387. ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
  4388. err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
  4389. if (err)
  4390. return err;
  4391. if (selinux_secmark_enabled()) {
  4392. err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
  4393. PACKET__RECV, &ad);
  4394. if (err)
  4395. return err;
  4396. }
  4397. err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
  4398. if (err)
  4399. return err;
  4400. err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
  4401. return err;
  4402. }
  4403. static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  4404. {
  4405. int err, peerlbl_active, secmark_active;
  4406. struct sk_security_struct *sksec = selinux_sock(sk);
  4407. u16 family = sk->sk_family;
  4408. u32 sk_sid = sksec->sid;
  4409. struct common_audit_data ad;
  4410. struct lsm_network_audit net;
  4411. char *addrp;
  4412. if (family != PF_INET && family != PF_INET6)
  4413. return 0;
  4414. /* Handle mapped IPv4 packets arriving via IPv6 sockets */
  4415. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  4416. family = PF_INET;
  4417. /* If any sort of compatibility mode is enabled then handoff processing
  4418. * to the selinux_sock_rcv_skb_compat() function to deal with the
  4419. * special handling. We do this in an attempt to keep this function
  4420. * as fast and as clean as possible. */
  4421. if (!selinux_policycap_netpeer())
  4422. return selinux_sock_rcv_skb_compat(sk, skb, family);
  4423. secmark_active = selinux_secmark_enabled();
  4424. peerlbl_active = selinux_peerlbl_enabled();
  4425. if (!secmark_active && !peerlbl_active)
  4426. return 0;
  4427. ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
  4428. err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
  4429. if (err)
  4430. return err;
  4431. if (peerlbl_active) {
  4432. u32 peer_sid;
  4433. err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
  4434. if (err)
  4435. return err;
  4436. err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
  4437. addrp, family, peer_sid, &ad);
  4438. if (err) {
  4439. selinux_netlbl_err(skb, family, err, 0);
  4440. return err;
  4441. }
  4442. err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
  4443. PEER__RECV, &ad);
  4444. if (err) {
  4445. selinux_netlbl_err(skb, family, err, 0);
  4446. return err;
  4447. }
  4448. }
  4449. if (secmark_active) {
  4450. err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
  4451. PACKET__RECV, &ad);
  4452. if (err)
  4453. return err;
  4454. }
  4455. return err;
  4456. }
  4457. static int selinux_socket_getpeersec_stream(struct socket *sock,
  4458. sockptr_t optval, sockptr_t optlen,
  4459. unsigned int len)
  4460. {
  4461. int err = 0;
  4462. char *scontext = NULL;
  4463. u32 scontext_len;
  4464. struct sk_security_struct *sksec = selinux_sock(sock->sk);
  4465. u32 peer_sid = SECSID_NULL;
  4466. if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
  4467. sksec->sclass == SECCLASS_TCP_SOCKET ||
  4468. sksec->sclass == SECCLASS_SCTP_SOCKET)
  4469. peer_sid = sksec->peer_sid;
  4470. if (peer_sid == SECSID_NULL)
  4471. return -ENOPROTOOPT;
  4472. err = security_sid_to_context(peer_sid, &scontext,
  4473. &scontext_len);
  4474. if (err)
  4475. return err;
  4476. if (scontext_len > len) {
  4477. err = -ERANGE;
  4478. goto out_len;
  4479. }
  4480. if (copy_to_sockptr(optval, scontext, scontext_len))
  4481. err = -EFAULT;
  4482. out_len:
  4483. if (copy_to_sockptr(optlen, &scontext_len, sizeof(scontext_len)))
  4484. err = -EFAULT;
  4485. kfree(scontext);
  4486. return err;
  4487. }
  4488. static int selinux_socket_getpeersec_dgram(struct socket *sock,
  4489. struct sk_buff *skb, u32 *secid)
  4490. {
  4491. u32 peer_secid = SECSID_NULL;
  4492. u16 family;
  4493. if (skb && skb->protocol == htons(ETH_P_IP))
  4494. family = PF_INET;
  4495. else if (skb && skb->protocol == htons(ETH_P_IPV6))
  4496. family = PF_INET6;
  4497. else if (sock)
  4498. family = sock->sk->sk_family;
  4499. else {
  4500. *secid = SECSID_NULL;
  4501. return -EINVAL;
  4502. }
  4503. if (sock && family == PF_UNIX) {
  4504. struct inode_security_struct *isec;
  4505. isec = inode_security_novalidate(SOCK_INODE(sock));
  4506. peer_secid = isec->sid;
  4507. } else if (skb)
  4508. selinux_skb_peerlbl_sid(skb, family, &peer_secid);
  4509. *secid = peer_secid;
  4510. if (peer_secid == SECSID_NULL)
  4511. return -ENOPROTOOPT;
  4512. return 0;
  4513. }
  4514. static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
  4515. {
  4516. struct sk_security_struct *sksec = selinux_sock(sk);
  4517. sksec->peer_sid = SECINITSID_UNLABELED;
  4518. sksec->sid = SECINITSID_UNLABELED;
  4519. sksec->sclass = SECCLASS_SOCKET;
  4520. selinux_netlbl_sk_security_reset(sksec);
  4521. return 0;
  4522. }
  4523. static void selinux_sk_free_security(struct sock *sk)
  4524. {
  4525. struct sk_security_struct *sksec = selinux_sock(sk);
  4526. selinux_netlbl_sk_security_free(sksec);
  4527. }
  4528. static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
  4529. {
  4530. struct sk_security_struct *sksec = selinux_sock(sk);
  4531. struct sk_security_struct *newsksec = selinux_sock(newsk);
  4532. newsksec->sid = sksec->sid;
  4533. newsksec->peer_sid = sksec->peer_sid;
  4534. newsksec->sclass = sksec->sclass;
  4535. selinux_netlbl_sk_security_reset(newsksec);
  4536. }
  4537. static void selinux_sk_getsecid(const struct sock *sk, u32 *secid)
  4538. {
  4539. if (!sk)
  4540. *secid = SECINITSID_ANY_SOCKET;
  4541. else {
  4542. const struct sk_security_struct *sksec = selinux_sock(sk);
  4543. *secid = sksec->sid;
  4544. }
  4545. }
  4546. static void selinux_sock_graft(struct sock *sk, struct socket *parent)
  4547. {
  4548. struct inode_security_struct *isec =
  4549. inode_security_novalidate(SOCK_INODE(parent));
  4550. struct sk_security_struct *sksec = selinux_sock(sk);
  4551. if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
  4552. sk->sk_family == PF_UNIX)
  4553. isec->sid = sksec->sid;
  4554. sksec->sclass = isec->sclass;
  4555. }
  4556. /*
  4557. * Determines peer_secid for the asoc and updates socket's peer label
  4558. * if it's the first association on the socket.
  4559. */
  4560. static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
  4561. struct sk_buff *skb)
  4562. {
  4563. struct sock *sk = asoc->base.sk;
  4564. u16 family = sk->sk_family;
  4565. struct sk_security_struct *sksec = selinux_sock(sk);
  4566. struct common_audit_data ad;
  4567. struct lsm_network_audit net;
  4568. int err;
  4569. /* handle mapped IPv4 packets arriving via IPv6 sockets */
  4570. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  4571. family = PF_INET;
  4572. if (selinux_peerlbl_enabled()) {
  4573. asoc->peer_secid = SECSID_NULL;
  4574. /* This will return peer_sid = SECSID_NULL if there are
  4575. * no peer labels, see security_net_peersid_resolve().
  4576. */
  4577. err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid);
  4578. if (err)
  4579. return err;
  4580. if (asoc->peer_secid == SECSID_NULL)
  4581. asoc->peer_secid = SECINITSID_UNLABELED;
  4582. } else {
  4583. asoc->peer_secid = SECINITSID_UNLABELED;
  4584. }
  4585. if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
  4586. sksec->sctp_assoc_state = SCTP_ASSOC_SET;
  4587. /* Here as first association on socket. As the peer SID
  4588. * was allowed by peer recv (and the netif/node checks),
  4589. * then it is approved by policy and used as the primary
  4590. * peer SID for getpeercon(3).
  4591. */
  4592. sksec->peer_sid = asoc->peer_secid;
  4593. } else if (sksec->peer_sid != asoc->peer_secid) {
  4594. /* Other association peer SIDs are checked to enforce
  4595. * consistency among the peer SIDs.
  4596. */
  4597. ad_net_init_from_sk(&ad, &net, asoc->base.sk);
  4598. err = avc_has_perm(sksec->peer_sid, asoc->peer_secid,
  4599. sksec->sclass, SCTP_SOCKET__ASSOCIATION,
  4600. &ad);
  4601. if (err)
  4602. return err;
  4603. }
  4604. return 0;
  4605. }
  4606. /* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This
  4607. * happens on an incoming connect(2), sctp_connectx(3) or
  4608. * sctp_sendmsg(3) (with no association already present).
  4609. */
  4610. static int selinux_sctp_assoc_request(struct sctp_association *asoc,
  4611. struct sk_buff *skb)
  4612. {
  4613. struct sk_security_struct *sksec = selinux_sock(asoc->base.sk);
  4614. u32 conn_sid;
  4615. int err;
  4616. if (!selinux_policycap_extsockclass())
  4617. return 0;
  4618. err = selinux_sctp_process_new_assoc(asoc, skb);
  4619. if (err)
  4620. return err;
  4621. /* Compute the MLS component for the connection and store
  4622. * the information in asoc. This will be used by SCTP TCP type
  4623. * sockets and peeled off connections as they cause a new
  4624. * socket to be generated. selinux_sctp_sk_clone() will then
  4625. * plug this into the new socket.
  4626. */
  4627. err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid);
  4628. if (err)
  4629. return err;
  4630. asoc->secid = conn_sid;
  4631. /* Set any NetLabel labels including CIPSO/CALIPSO options. */
  4632. return selinux_netlbl_sctp_assoc_request(asoc, skb);
  4633. }
  4634. /* Called when SCTP receives a COOKIE ACK chunk as the final
  4635. * response to an association request (initited by us).
  4636. */
  4637. static int selinux_sctp_assoc_established(struct sctp_association *asoc,
  4638. struct sk_buff *skb)
  4639. {
  4640. struct sk_security_struct *sksec = selinux_sock(asoc->base.sk);
  4641. if (!selinux_policycap_extsockclass())
  4642. return 0;
  4643. /* Inherit secid from the parent socket - this will be picked up
  4644. * by selinux_sctp_sk_clone() if the association gets peeled off
  4645. * into a new socket.
  4646. */
  4647. asoc->secid = sksec->sid;
  4648. return selinux_sctp_process_new_assoc(asoc, skb);
  4649. }
  4650. /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
  4651. * based on their @optname.
  4652. */
  4653. static int selinux_sctp_bind_connect(struct sock *sk, int optname,
  4654. struct sockaddr *address,
  4655. int addrlen)
  4656. {
  4657. int len, err = 0, walk_size = 0;
  4658. void *addr_buf;
  4659. struct sockaddr *addr;
  4660. struct socket *sock;
  4661. if (!selinux_policycap_extsockclass())
  4662. return 0;
  4663. /* Process one or more addresses that may be IPv4 or IPv6 */
  4664. sock = sk->sk_socket;
  4665. addr_buf = address;
  4666. while (walk_size < addrlen) {
  4667. if (walk_size + sizeof(sa_family_t) > addrlen)
  4668. return -EINVAL;
  4669. addr = addr_buf;
  4670. switch (addr->sa_family) {
  4671. case AF_UNSPEC:
  4672. case AF_INET:
  4673. len = sizeof(struct sockaddr_in);
  4674. break;
  4675. case AF_INET6:
  4676. len = sizeof(struct sockaddr_in6);
  4677. break;
  4678. default:
  4679. return -EINVAL;
  4680. }
  4681. if (walk_size + len > addrlen)
  4682. return -EINVAL;
  4683. err = -EINVAL;
  4684. switch (optname) {
  4685. /* Bind checks */
  4686. case SCTP_PRIMARY_ADDR:
  4687. case SCTP_SET_PEER_PRIMARY_ADDR:
  4688. case SCTP_SOCKOPT_BINDX_ADD:
  4689. err = selinux_socket_bind(sock, addr, len);
  4690. break;
  4691. /* Connect checks */
  4692. case SCTP_SOCKOPT_CONNECTX:
  4693. case SCTP_PARAM_SET_PRIMARY:
  4694. case SCTP_PARAM_ADD_IP:
  4695. case SCTP_SENDMSG_CONNECT:
  4696. err = selinux_socket_connect_helper(sock, addr, len);
  4697. if (err)
  4698. return err;
  4699. /* As selinux_sctp_bind_connect() is called by the
  4700. * SCTP protocol layer, the socket is already locked,
  4701. * therefore selinux_netlbl_socket_connect_locked()
  4702. * is called here. The situations handled are:
  4703. * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
  4704. * whenever a new IP address is added or when a new
  4705. * primary address is selected.
  4706. * Note that an SCTP connect(2) call happens before
  4707. * the SCTP protocol layer and is handled via
  4708. * selinux_socket_connect().
  4709. */
  4710. err = selinux_netlbl_socket_connect_locked(sk, addr);
  4711. break;
  4712. }
  4713. if (err)
  4714. return err;
  4715. addr_buf += len;
  4716. walk_size += len;
  4717. }
  4718. return 0;
  4719. }
  4720. /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
  4721. static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
  4722. struct sock *newsk)
  4723. {
  4724. struct sk_security_struct *sksec = selinux_sock(sk);
  4725. struct sk_security_struct *newsksec = selinux_sock(newsk);
  4726. /* If policy does not support SECCLASS_SCTP_SOCKET then call
  4727. * the non-sctp clone version.
  4728. */
  4729. if (!selinux_policycap_extsockclass())
  4730. return selinux_sk_clone_security(sk, newsk);
  4731. newsksec->sid = asoc->secid;
  4732. newsksec->peer_sid = asoc->peer_secid;
  4733. newsksec->sclass = sksec->sclass;
  4734. selinux_netlbl_sctp_sk_clone(sk, newsk);
  4735. }
  4736. static int selinux_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
  4737. {
  4738. struct sk_security_struct *ssksec = selinux_sock(ssk);
  4739. struct sk_security_struct *sksec = selinux_sock(sk);
  4740. ssksec->sclass = sksec->sclass;
  4741. ssksec->sid = sksec->sid;
  4742. /* replace the existing subflow label deleting the existing one
  4743. * and re-recreating a new label using the updated context
  4744. */
  4745. selinux_netlbl_sk_security_free(ssksec);
  4746. return selinux_netlbl_socket_post_create(ssk, ssk->sk_family);
  4747. }
  4748. static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
  4749. struct request_sock *req)
  4750. {
  4751. struct sk_security_struct *sksec = selinux_sock(sk);
  4752. int err;
  4753. u16 family = req->rsk_ops->family;
  4754. u32 connsid;
  4755. u32 peersid;
  4756. err = selinux_skb_peerlbl_sid(skb, family, &peersid);
  4757. if (err)
  4758. return err;
  4759. err = selinux_conn_sid(sksec->sid, peersid, &connsid);
  4760. if (err)
  4761. return err;
  4762. req->secid = connsid;
  4763. req->peer_secid = peersid;
  4764. return selinux_netlbl_inet_conn_request(req, family);
  4765. }
  4766. static void selinux_inet_csk_clone(struct sock *newsk,
  4767. const struct request_sock *req)
  4768. {
  4769. struct sk_security_struct *newsksec = selinux_sock(newsk);
  4770. newsksec->sid = req->secid;
  4771. newsksec->peer_sid = req->peer_secid;
  4772. /* NOTE: Ideally, we should also get the isec->sid for the
  4773. new socket in sync, but we don't have the isec available yet.
  4774. So we will wait until sock_graft to do it, by which
  4775. time it will have been created and available. */
  4776. /* We don't need to take any sort of lock here as we are the only
  4777. * thread with access to newsksec */
  4778. selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
  4779. }
  4780. static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
  4781. {
  4782. u16 family = sk->sk_family;
  4783. struct sk_security_struct *sksec = selinux_sock(sk);
  4784. /* handle mapped IPv4 packets arriving via IPv6 sockets */
  4785. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  4786. family = PF_INET;
  4787. selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
  4788. }
  4789. static int selinux_secmark_relabel_packet(u32 sid)
  4790. {
  4791. return avc_has_perm(current_sid(), sid, SECCLASS_PACKET, PACKET__RELABELTO,
  4792. NULL);
  4793. }
  4794. static void selinux_secmark_refcount_inc(void)
  4795. {
  4796. atomic_inc(&selinux_secmark_refcount);
  4797. }
  4798. static void selinux_secmark_refcount_dec(void)
  4799. {
  4800. atomic_dec(&selinux_secmark_refcount);
  4801. }
  4802. static void selinux_req_classify_flow(const struct request_sock *req,
  4803. struct flowi_common *flic)
  4804. {
  4805. flic->flowic_secid = req->secid;
  4806. }
  4807. static int selinux_tun_dev_alloc_security(void *security)
  4808. {
  4809. struct tun_security_struct *tunsec = selinux_tun_dev(security);
  4810. tunsec->sid = current_sid();
  4811. return 0;
  4812. }
  4813. static int selinux_tun_dev_create(void)
  4814. {
  4815. u32 sid = current_sid();
  4816. /* we aren't taking into account the "sockcreate" SID since the socket
  4817. * that is being created here is not a socket in the traditional sense,
  4818. * instead it is a private sock, accessible only to the kernel, and
  4819. * representing a wide range of network traffic spanning multiple
  4820. * connections unlike traditional sockets - check the TUN driver to
  4821. * get a better understanding of why this socket is special */
  4822. return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
  4823. NULL);
  4824. }
  4825. static int selinux_tun_dev_attach_queue(void *security)
  4826. {
  4827. struct tun_security_struct *tunsec = selinux_tun_dev(security);
  4828. return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
  4829. TUN_SOCKET__ATTACH_QUEUE, NULL);
  4830. }
  4831. static int selinux_tun_dev_attach(struct sock *sk, void *security)
  4832. {
  4833. struct tun_security_struct *tunsec = selinux_tun_dev(security);
  4834. struct sk_security_struct *sksec = selinux_sock(sk);
  4835. /* we don't currently perform any NetLabel based labeling here and it
  4836. * isn't clear that we would want to do so anyway; while we could apply
  4837. * labeling without the support of the TUN user the resulting labeled
  4838. * traffic from the other end of the connection would almost certainly
  4839. * cause confusion to the TUN user that had no idea network labeling
  4840. * protocols were being used */
  4841. sksec->sid = tunsec->sid;
  4842. sksec->sclass = SECCLASS_TUN_SOCKET;
  4843. return 0;
  4844. }
  4845. static int selinux_tun_dev_open(void *security)
  4846. {
  4847. struct tun_security_struct *tunsec = selinux_tun_dev(security);
  4848. u32 sid = current_sid();
  4849. int err;
  4850. err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
  4851. TUN_SOCKET__RELABELFROM, NULL);
  4852. if (err)
  4853. return err;
  4854. err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
  4855. TUN_SOCKET__RELABELTO, NULL);
  4856. if (err)
  4857. return err;
  4858. tunsec->sid = sid;
  4859. return 0;
  4860. }
  4861. #ifdef CONFIG_NETFILTER
  4862. static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
  4863. const struct nf_hook_state *state)
  4864. {
  4865. int ifindex;
  4866. u16 family;
  4867. char *addrp;
  4868. u32 peer_sid;
  4869. struct common_audit_data ad;
  4870. struct lsm_network_audit net;
  4871. int secmark_active, peerlbl_active;
  4872. if (!selinux_policycap_netpeer())
  4873. return NF_ACCEPT;
  4874. secmark_active = selinux_secmark_enabled();
  4875. peerlbl_active = selinux_peerlbl_enabled();
  4876. if (!secmark_active && !peerlbl_active)
  4877. return NF_ACCEPT;
  4878. family = state->pf;
  4879. if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
  4880. return NF_DROP;
  4881. ifindex = state->in->ifindex;
  4882. ad_net_init_from_iif(&ad, &net, ifindex, family);
  4883. if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
  4884. return NF_DROP;
  4885. if (peerlbl_active) {
  4886. int err;
  4887. err = selinux_inet_sys_rcv_skb(state->net, ifindex,
  4888. addrp, family, peer_sid, &ad);
  4889. if (err) {
  4890. selinux_netlbl_err(skb, family, err, 1);
  4891. return NF_DROP;
  4892. }
  4893. }
  4894. if (secmark_active)
  4895. if (avc_has_perm(peer_sid, skb->secmark,
  4896. SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
  4897. return NF_DROP;
  4898. if (netlbl_enabled())
  4899. /* we do this in the FORWARD path and not the POST_ROUTING
  4900. * path because we want to make sure we apply the necessary
  4901. * labeling before IPsec is applied so we can leverage AH
  4902. * protection */
  4903. if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
  4904. return NF_DROP;
  4905. return NF_ACCEPT;
  4906. }
  4907. static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
  4908. const struct nf_hook_state *state)
  4909. {
  4910. struct sock *sk;
  4911. u32 sid;
  4912. if (!netlbl_enabled())
  4913. return NF_ACCEPT;
  4914. /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
  4915. * because we want to make sure we apply the necessary labeling
  4916. * before IPsec is applied so we can leverage AH protection */
  4917. sk = skb->sk;
  4918. if (sk) {
  4919. struct sk_security_struct *sksec;
  4920. if (sk_listener(sk))
  4921. /* if the socket is the listening state then this
  4922. * packet is a SYN-ACK packet which means it needs to
  4923. * be labeled based on the connection/request_sock and
  4924. * not the parent socket. unfortunately, we can't
  4925. * lookup the request_sock yet as it isn't queued on
  4926. * the parent socket until after the SYN-ACK is sent.
  4927. * the "solution" is to simply pass the packet as-is
  4928. * as any IP option based labeling should be copied
  4929. * from the initial connection request (in the IP
  4930. * layer). it is far from ideal, but until we get a
  4931. * security label in the packet itself this is the
  4932. * best we can do. */
  4933. return NF_ACCEPT;
  4934. /* standard practice, label using the parent socket */
  4935. sksec = selinux_sock(sk);
  4936. sid = sksec->sid;
  4937. } else
  4938. sid = SECINITSID_KERNEL;
  4939. if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
  4940. return NF_DROP;
  4941. return NF_ACCEPT;
  4942. }
  4943. static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
  4944. const struct nf_hook_state *state)
  4945. {
  4946. struct sock *sk;
  4947. struct sk_security_struct *sksec;
  4948. struct common_audit_data ad;
  4949. struct lsm_network_audit net;
  4950. u8 proto = 0;
  4951. sk = skb_to_full_sk(skb);
  4952. if (sk == NULL)
  4953. return NF_ACCEPT;
  4954. sksec = selinux_sock(sk);
  4955. ad_net_init_from_iif(&ad, &net, state->out->ifindex, state->pf);
  4956. if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
  4957. return NF_DROP;
  4958. if (selinux_secmark_enabled())
  4959. if (avc_has_perm(sksec->sid, skb->secmark,
  4960. SECCLASS_PACKET, PACKET__SEND, &ad))
  4961. return NF_DROP_ERR(-ECONNREFUSED);
  4962. if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
  4963. return NF_DROP_ERR(-ECONNREFUSED);
  4964. return NF_ACCEPT;
  4965. }
  4966. static unsigned int selinux_ip_postroute(void *priv,
  4967. struct sk_buff *skb,
  4968. const struct nf_hook_state *state)
  4969. {
  4970. u16 family;
  4971. u32 secmark_perm;
  4972. u32 peer_sid;
  4973. int ifindex;
  4974. struct sock *sk;
  4975. struct common_audit_data ad;
  4976. struct lsm_network_audit net;
  4977. char *addrp;
  4978. int secmark_active, peerlbl_active;
  4979. /* If any sort of compatibility mode is enabled then handoff processing
  4980. * to the selinux_ip_postroute_compat() function to deal with the
  4981. * special handling. We do this in an attempt to keep this function
  4982. * as fast and as clean as possible. */
  4983. if (!selinux_policycap_netpeer())
  4984. return selinux_ip_postroute_compat(skb, state);
  4985. secmark_active = selinux_secmark_enabled();
  4986. peerlbl_active = selinux_peerlbl_enabled();
  4987. if (!secmark_active && !peerlbl_active)
  4988. return NF_ACCEPT;
  4989. sk = skb_to_full_sk(skb);
  4990. #ifdef CONFIG_XFRM
  4991. /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
  4992. * packet transformation so allow the packet to pass without any checks
  4993. * since we'll have another chance to perform access control checks
  4994. * when the packet is on it's final way out.
  4995. * NOTE: there appear to be some IPv6 multicast cases where skb->dst
  4996. * is NULL, in this case go ahead and apply access control.
  4997. * NOTE: if this is a local socket (skb->sk != NULL) that is in the
  4998. * TCP listening state we cannot wait until the XFRM processing
  4999. * is done as we will miss out on the SA label if we do;
  5000. * unfortunately, this means more work, but it is only once per
  5001. * connection. */
  5002. if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
  5003. !(sk && sk_listener(sk)))
  5004. return NF_ACCEPT;
  5005. #endif
  5006. family = state->pf;
  5007. if (sk == NULL) {
  5008. /* Without an associated socket the packet is either coming
  5009. * from the kernel or it is being forwarded; check the packet
  5010. * to determine which and if the packet is being forwarded
  5011. * query the packet directly to determine the security label. */
  5012. if (skb->skb_iif) {
  5013. secmark_perm = PACKET__FORWARD_OUT;
  5014. if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
  5015. return NF_DROP;
  5016. } else {
  5017. secmark_perm = PACKET__SEND;
  5018. peer_sid = SECINITSID_KERNEL;
  5019. }
  5020. } else if (sk_listener(sk)) {
  5021. /* Locally generated packet but the associated socket is in the
  5022. * listening state which means this is a SYN-ACK packet. In
  5023. * this particular case the correct security label is assigned
  5024. * to the connection/request_sock but unfortunately we can't
  5025. * query the request_sock as it isn't queued on the parent
  5026. * socket until after the SYN-ACK packet is sent; the only
  5027. * viable choice is to regenerate the label like we do in
  5028. * selinux_inet_conn_request(). See also selinux_ip_output()
  5029. * for similar problems. */
  5030. u32 skb_sid;
  5031. struct sk_security_struct *sksec;
  5032. sksec = selinux_sock(sk);
  5033. if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
  5034. return NF_DROP;
  5035. /* At this point, if the returned skb peerlbl is SECSID_NULL
  5036. * and the packet has been through at least one XFRM
  5037. * transformation then we must be dealing with the "final"
  5038. * form of labeled IPsec packet; since we've already applied
  5039. * all of our access controls on this packet we can safely
  5040. * pass the packet. */
  5041. if (skb_sid == SECSID_NULL) {
  5042. switch (family) {
  5043. case PF_INET:
  5044. if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
  5045. return NF_ACCEPT;
  5046. break;
  5047. case PF_INET6:
  5048. if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
  5049. return NF_ACCEPT;
  5050. break;
  5051. default:
  5052. return NF_DROP_ERR(-ECONNREFUSED);
  5053. }
  5054. }
  5055. if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
  5056. return NF_DROP;
  5057. secmark_perm = PACKET__SEND;
  5058. } else {
  5059. /* Locally generated packet, fetch the security label from the
  5060. * associated socket. */
  5061. struct sk_security_struct *sksec = selinux_sock(sk);
  5062. peer_sid = sksec->sid;
  5063. secmark_perm = PACKET__SEND;
  5064. }
  5065. ifindex = state->out->ifindex;
  5066. ad_net_init_from_iif(&ad, &net, ifindex, family);
  5067. if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
  5068. return NF_DROP;
  5069. if (secmark_active)
  5070. if (avc_has_perm(peer_sid, skb->secmark,
  5071. SECCLASS_PACKET, secmark_perm, &ad))
  5072. return NF_DROP_ERR(-ECONNREFUSED);
  5073. if (peerlbl_active) {
  5074. u32 if_sid;
  5075. u32 node_sid;
  5076. if (sel_netif_sid(state->net, ifindex, &if_sid))
  5077. return NF_DROP;
  5078. if (avc_has_perm(peer_sid, if_sid,
  5079. SECCLASS_NETIF, NETIF__EGRESS, &ad))
  5080. return NF_DROP_ERR(-ECONNREFUSED);
  5081. if (sel_netnode_sid(addrp, family, &node_sid))
  5082. return NF_DROP;
  5083. if (avc_has_perm(peer_sid, node_sid,
  5084. SECCLASS_NODE, NODE__SENDTO, &ad))
  5085. return NF_DROP_ERR(-ECONNREFUSED);
  5086. }
  5087. return NF_ACCEPT;
  5088. }
  5089. #endif /* CONFIG_NETFILTER */
  5090. static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
  5091. {
  5092. int rc = 0;
  5093. unsigned int msg_len;
  5094. unsigned int data_len = skb->len;
  5095. unsigned char *data = skb->data;
  5096. struct nlmsghdr *nlh;
  5097. struct sk_security_struct *sksec = selinux_sock(sk);
  5098. u16 sclass = sksec->sclass;
  5099. u32 perm;
  5100. while (data_len >= nlmsg_total_size(0)) {
  5101. nlh = (struct nlmsghdr *)data;
  5102. /* NOTE: the nlmsg_len field isn't reliably set by some netlink
  5103. * users which means we can't reject skb's with bogus
  5104. * length fields; our solution is to follow what
  5105. * netlink_rcv_skb() does and simply skip processing at
  5106. * messages with length fields that are clearly junk
  5107. */
  5108. if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
  5109. return 0;
  5110. rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
  5111. if (rc == 0) {
  5112. rc = sock_has_perm(sk, perm);
  5113. if (rc)
  5114. return rc;
  5115. } else if (rc == -EINVAL) {
  5116. /* -EINVAL is a missing msg/perm mapping */
  5117. pr_warn_ratelimited("SELinux: unrecognized netlink"
  5118. " message: protocol=%hu nlmsg_type=%hu sclass=%s"
  5119. " pid=%d comm=%s\n",
  5120. sk->sk_protocol, nlh->nlmsg_type,
  5121. secclass_map[sclass - 1].name,
  5122. task_pid_nr(current), current->comm);
  5123. if (enforcing_enabled() &&
  5124. !security_get_allow_unknown())
  5125. return rc;
  5126. rc = 0;
  5127. } else if (rc == -ENOENT) {
  5128. /* -ENOENT is a missing socket/class mapping, ignore */
  5129. rc = 0;
  5130. } else {
  5131. return rc;
  5132. }
  5133. /* move to the next message after applying netlink padding */
  5134. msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
  5135. if (msg_len >= data_len)
  5136. return 0;
  5137. data_len -= msg_len;
  5138. data += msg_len;
  5139. }
  5140. return rc;
  5141. }
  5142. static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
  5143. {
  5144. isec->sclass = sclass;
  5145. isec->sid = current_sid();
  5146. }
  5147. static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
  5148. u32 perms)
  5149. {
  5150. struct ipc_security_struct *isec;
  5151. struct common_audit_data ad;
  5152. u32 sid = current_sid();
  5153. isec = selinux_ipc(ipc_perms);
  5154. ad.type = LSM_AUDIT_DATA_IPC;
  5155. ad.u.ipc_id = ipc_perms->key;
  5156. return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
  5157. }
  5158. static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
  5159. {
  5160. struct msg_security_struct *msec;
  5161. msec = selinux_msg_msg(msg);
  5162. msec->sid = SECINITSID_UNLABELED;
  5163. return 0;
  5164. }
  5165. /* message queue security operations */
  5166. static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
  5167. {
  5168. struct ipc_security_struct *isec;
  5169. struct common_audit_data ad;
  5170. u32 sid = current_sid();
  5171. isec = selinux_ipc(msq);
  5172. ipc_init_security(isec, SECCLASS_MSGQ);
  5173. ad.type = LSM_AUDIT_DATA_IPC;
  5174. ad.u.ipc_id = msq->key;
  5175. return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  5176. MSGQ__CREATE, &ad);
  5177. }
  5178. static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
  5179. {
  5180. struct ipc_security_struct *isec;
  5181. struct common_audit_data ad;
  5182. u32 sid = current_sid();
  5183. isec = selinux_ipc(msq);
  5184. ad.type = LSM_AUDIT_DATA_IPC;
  5185. ad.u.ipc_id = msq->key;
  5186. return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  5187. MSGQ__ASSOCIATE, &ad);
  5188. }
  5189. static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
  5190. {
  5191. u32 perms;
  5192. switch (cmd) {
  5193. case IPC_INFO:
  5194. case MSG_INFO:
  5195. /* No specific object, just general system-wide information. */
  5196. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  5197. SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
  5198. case IPC_STAT:
  5199. case MSG_STAT:
  5200. case MSG_STAT_ANY:
  5201. perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
  5202. break;
  5203. case IPC_SET:
  5204. perms = MSGQ__SETATTR;
  5205. break;
  5206. case IPC_RMID:
  5207. perms = MSGQ__DESTROY;
  5208. break;
  5209. default:
  5210. return 0;
  5211. }
  5212. return ipc_has_perm(msq, perms);
  5213. }
  5214. static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
  5215. {
  5216. struct ipc_security_struct *isec;
  5217. struct msg_security_struct *msec;
  5218. struct common_audit_data ad;
  5219. u32 sid = current_sid();
  5220. int rc;
  5221. isec = selinux_ipc(msq);
  5222. msec = selinux_msg_msg(msg);
  5223. /*
  5224. * First time through, need to assign label to the message
  5225. */
  5226. if (msec->sid == SECINITSID_UNLABELED) {
  5227. /*
  5228. * Compute new sid based on current process and
  5229. * message queue this message will be stored in
  5230. */
  5231. rc = security_transition_sid(sid, isec->sid,
  5232. SECCLASS_MSG, NULL, &msec->sid);
  5233. if (rc)
  5234. return rc;
  5235. }
  5236. ad.type = LSM_AUDIT_DATA_IPC;
  5237. ad.u.ipc_id = msq->key;
  5238. /* Can this process write to the queue? */
  5239. rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
  5240. MSGQ__WRITE, &ad);
  5241. if (!rc)
  5242. /* Can this process send the message */
  5243. rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
  5244. MSG__SEND, &ad);
  5245. if (!rc)
  5246. /* Can the message be put in the queue? */
  5247. rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
  5248. MSGQ__ENQUEUE, &ad);
  5249. return rc;
  5250. }
  5251. static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
  5252. struct task_struct *target,
  5253. long type, int mode)
  5254. {
  5255. struct ipc_security_struct *isec;
  5256. struct msg_security_struct *msec;
  5257. struct common_audit_data ad;
  5258. u32 sid = task_sid_obj(target);
  5259. int rc;
  5260. isec = selinux_ipc(msq);
  5261. msec = selinux_msg_msg(msg);
  5262. ad.type = LSM_AUDIT_DATA_IPC;
  5263. ad.u.ipc_id = msq->key;
  5264. rc = avc_has_perm(sid, isec->sid,
  5265. SECCLASS_MSGQ, MSGQ__READ, &ad);
  5266. if (!rc)
  5267. rc = avc_has_perm(sid, msec->sid,
  5268. SECCLASS_MSG, MSG__RECEIVE, &ad);
  5269. return rc;
  5270. }
  5271. /* Shared Memory security operations */
  5272. static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
  5273. {
  5274. struct ipc_security_struct *isec;
  5275. struct common_audit_data ad;
  5276. u32 sid = current_sid();
  5277. isec = selinux_ipc(shp);
  5278. ipc_init_security(isec, SECCLASS_SHM);
  5279. ad.type = LSM_AUDIT_DATA_IPC;
  5280. ad.u.ipc_id = shp->key;
  5281. return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
  5282. SHM__CREATE, &ad);
  5283. }
  5284. static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
  5285. {
  5286. struct ipc_security_struct *isec;
  5287. struct common_audit_data ad;
  5288. u32 sid = current_sid();
  5289. isec = selinux_ipc(shp);
  5290. ad.type = LSM_AUDIT_DATA_IPC;
  5291. ad.u.ipc_id = shp->key;
  5292. return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
  5293. SHM__ASSOCIATE, &ad);
  5294. }
  5295. /* Note, at this point, shp is locked down */
  5296. static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
  5297. {
  5298. u32 perms;
  5299. switch (cmd) {
  5300. case IPC_INFO:
  5301. case SHM_INFO:
  5302. /* No specific object, just general system-wide information. */
  5303. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  5304. SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
  5305. case IPC_STAT:
  5306. case SHM_STAT:
  5307. case SHM_STAT_ANY:
  5308. perms = SHM__GETATTR | SHM__ASSOCIATE;
  5309. break;
  5310. case IPC_SET:
  5311. perms = SHM__SETATTR;
  5312. break;
  5313. case SHM_LOCK:
  5314. case SHM_UNLOCK:
  5315. perms = SHM__LOCK;
  5316. break;
  5317. case IPC_RMID:
  5318. perms = SHM__DESTROY;
  5319. break;
  5320. default:
  5321. return 0;
  5322. }
  5323. return ipc_has_perm(shp, perms);
  5324. }
  5325. static int selinux_shm_shmat(struct kern_ipc_perm *shp,
  5326. char __user *shmaddr, int shmflg)
  5327. {
  5328. u32 perms;
  5329. if (shmflg & SHM_RDONLY)
  5330. perms = SHM__READ;
  5331. else
  5332. perms = SHM__READ | SHM__WRITE;
  5333. return ipc_has_perm(shp, perms);
  5334. }
  5335. /* Semaphore security operations */
  5336. static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
  5337. {
  5338. struct ipc_security_struct *isec;
  5339. struct common_audit_data ad;
  5340. u32 sid = current_sid();
  5341. isec = selinux_ipc(sma);
  5342. ipc_init_security(isec, SECCLASS_SEM);
  5343. ad.type = LSM_AUDIT_DATA_IPC;
  5344. ad.u.ipc_id = sma->key;
  5345. return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
  5346. SEM__CREATE, &ad);
  5347. }
  5348. static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
  5349. {
  5350. struct ipc_security_struct *isec;
  5351. struct common_audit_data ad;
  5352. u32 sid = current_sid();
  5353. isec = selinux_ipc(sma);
  5354. ad.type = LSM_AUDIT_DATA_IPC;
  5355. ad.u.ipc_id = sma->key;
  5356. return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
  5357. SEM__ASSOCIATE, &ad);
  5358. }
  5359. /* Note, at this point, sma is locked down */
  5360. static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
  5361. {
  5362. int err;
  5363. u32 perms;
  5364. switch (cmd) {
  5365. case IPC_INFO:
  5366. case SEM_INFO:
  5367. /* No specific object, just general system-wide information. */
  5368. return avc_has_perm(current_sid(), SECINITSID_KERNEL,
  5369. SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
  5370. case GETPID:
  5371. case GETNCNT:
  5372. case GETZCNT:
  5373. perms = SEM__GETATTR;
  5374. break;
  5375. case GETVAL:
  5376. case GETALL:
  5377. perms = SEM__READ;
  5378. break;
  5379. case SETVAL:
  5380. case SETALL:
  5381. perms = SEM__WRITE;
  5382. break;
  5383. case IPC_RMID:
  5384. perms = SEM__DESTROY;
  5385. break;
  5386. case IPC_SET:
  5387. perms = SEM__SETATTR;
  5388. break;
  5389. case IPC_STAT:
  5390. case SEM_STAT:
  5391. case SEM_STAT_ANY:
  5392. perms = SEM__GETATTR | SEM__ASSOCIATE;
  5393. break;
  5394. default:
  5395. return 0;
  5396. }
  5397. err = ipc_has_perm(sma, perms);
  5398. return err;
  5399. }
  5400. static int selinux_sem_semop(struct kern_ipc_perm *sma,
  5401. struct sembuf *sops, unsigned nsops, int alter)
  5402. {
  5403. u32 perms;
  5404. if (alter)
  5405. perms = SEM__READ | SEM__WRITE;
  5406. else
  5407. perms = SEM__READ;
  5408. return ipc_has_perm(sma, perms);
  5409. }
  5410. static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
  5411. {
  5412. u32 av = 0;
  5413. av = 0;
  5414. if (flag & S_IRUGO)
  5415. av |= IPC__UNIX_READ;
  5416. if (flag & S_IWUGO)
  5417. av |= IPC__UNIX_WRITE;
  5418. if (av == 0)
  5419. return 0;
  5420. return ipc_has_perm(ipcp, av);
  5421. }
  5422. static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
  5423. {
  5424. struct ipc_security_struct *isec = selinux_ipc(ipcp);
  5425. *secid = isec->sid;
  5426. }
  5427. static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
  5428. {
  5429. if (inode)
  5430. inode_doinit_with_dentry(inode, dentry);
  5431. }
  5432. static int selinux_lsm_getattr(unsigned int attr, struct task_struct *p,
  5433. char **value)
  5434. {
  5435. const struct task_security_struct *tsec;
  5436. int error;
  5437. u32 sid;
  5438. u32 len;
  5439. rcu_read_lock();
  5440. tsec = selinux_cred(__task_cred(p));
  5441. if (p != current) {
  5442. error = avc_has_perm(current_sid(), tsec->sid,
  5443. SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
  5444. if (error)
  5445. goto err_unlock;
  5446. }
  5447. switch (attr) {
  5448. case LSM_ATTR_CURRENT:
  5449. sid = tsec->sid;
  5450. break;
  5451. case LSM_ATTR_PREV:
  5452. sid = tsec->osid;
  5453. break;
  5454. case LSM_ATTR_EXEC:
  5455. sid = tsec->exec_sid;
  5456. break;
  5457. case LSM_ATTR_FSCREATE:
  5458. sid = tsec->create_sid;
  5459. break;
  5460. case LSM_ATTR_KEYCREATE:
  5461. sid = tsec->keycreate_sid;
  5462. break;
  5463. case LSM_ATTR_SOCKCREATE:
  5464. sid = tsec->sockcreate_sid;
  5465. break;
  5466. default:
  5467. error = -EOPNOTSUPP;
  5468. goto err_unlock;
  5469. }
  5470. rcu_read_unlock();
  5471. if (sid == SECSID_NULL) {
  5472. *value = NULL;
  5473. return 0;
  5474. }
  5475. error = security_sid_to_context(sid, value, &len);
  5476. if (error)
  5477. return error;
  5478. return len;
  5479. err_unlock:
  5480. rcu_read_unlock();
  5481. return error;
  5482. }
  5483. static int selinux_lsm_setattr(u64 attr, void *value, size_t size)
  5484. {
  5485. struct task_security_struct *tsec;
  5486. struct cred *new;
  5487. u32 mysid = current_sid(), sid = 0, ptsid;
  5488. int error;
  5489. char *str = value;
  5490. /*
  5491. * Basic control over ability to set these attributes at all.
  5492. */
  5493. switch (attr) {
  5494. case LSM_ATTR_EXEC:
  5495. error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
  5496. PROCESS__SETEXEC, NULL);
  5497. break;
  5498. case LSM_ATTR_FSCREATE:
  5499. error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
  5500. PROCESS__SETFSCREATE, NULL);
  5501. break;
  5502. case LSM_ATTR_KEYCREATE:
  5503. error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
  5504. PROCESS__SETKEYCREATE, NULL);
  5505. break;
  5506. case LSM_ATTR_SOCKCREATE:
  5507. error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
  5508. PROCESS__SETSOCKCREATE, NULL);
  5509. break;
  5510. case LSM_ATTR_CURRENT:
  5511. error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
  5512. PROCESS__SETCURRENT, NULL);
  5513. break;
  5514. default:
  5515. error = -EOPNOTSUPP;
  5516. break;
  5517. }
  5518. if (error)
  5519. return error;
  5520. /* Obtain a SID for the context, if one was specified. */
  5521. if (size && str[0] && str[0] != '\n') {
  5522. if (str[size-1] == '\n') {
  5523. str[size-1] = 0;
  5524. size--;
  5525. }
  5526. error = security_context_to_sid(value, size,
  5527. &sid, GFP_KERNEL);
  5528. if (error == -EINVAL && attr == LSM_ATTR_FSCREATE) {
  5529. if (!has_cap_mac_admin(true)) {
  5530. struct audit_buffer *ab;
  5531. size_t audit_size;
  5532. /* We strip a nul only if it is at the end,
  5533. * otherwise the context contains a nul and
  5534. * we should audit that */
  5535. if (str[size - 1] == '\0')
  5536. audit_size = size - 1;
  5537. else
  5538. audit_size = size;
  5539. ab = audit_log_start(audit_context(),
  5540. GFP_ATOMIC,
  5541. AUDIT_SELINUX_ERR);
  5542. if (!ab)
  5543. return error;
  5544. audit_log_format(ab, "op=fscreate invalid_context=");
  5545. audit_log_n_untrustedstring(ab, value,
  5546. audit_size);
  5547. audit_log_end(ab);
  5548. return error;
  5549. }
  5550. error = security_context_to_sid_force(value, size,
  5551. &sid);
  5552. }
  5553. if (error)
  5554. return error;
  5555. }
  5556. new = prepare_creds();
  5557. if (!new)
  5558. return -ENOMEM;
  5559. /* Permission checking based on the specified context is
  5560. performed during the actual operation (execve,
  5561. open/mkdir/...), when we know the full context of the
  5562. operation. See selinux_bprm_creds_for_exec for the execve
  5563. checks and may_create for the file creation checks. The
  5564. operation will then fail if the context is not permitted. */
  5565. tsec = selinux_cred(new);
  5566. if (attr == LSM_ATTR_EXEC) {
  5567. tsec->exec_sid = sid;
  5568. } else if (attr == LSM_ATTR_FSCREATE) {
  5569. tsec->create_sid = sid;
  5570. } else if (attr == LSM_ATTR_KEYCREATE) {
  5571. if (sid) {
  5572. error = avc_has_perm(mysid, sid,
  5573. SECCLASS_KEY, KEY__CREATE, NULL);
  5574. if (error)
  5575. goto abort_change;
  5576. }
  5577. tsec->keycreate_sid = sid;
  5578. } else if (attr == LSM_ATTR_SOCKCREATE) {
  5579. tsec->sockcreate_sid = sid;
  5580. } else if (attr == LSM_ATTR_CURRENT) {
  5581. error = -EINVAL;
  5582. if (sid == 0)
  5583. goto abort_change;
  5584. if (!current_is_single_threaded()) {
  5585. error = security_bounded_transition(tsec->sid, sid);
  5586. if (error)
  5587. goto abort_change;
  5588. }
  5589. /* Check permissions for the transition. */
  5590. error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
  5591. PROCESS__DYNTRANSITION, NULL);
  5592. if (error)
  5593. goto abort_change;
  5594. /* Check for ptracing, and update the task SID if ok.
  5595. Otherwise, leave SID unchanged and fail. */
  5596. ptsid = ptrace_parent_sid();
  5597. if (ptsid != 0) {
  5598. error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
  5599. PROCESS__PTRACE, NULL);
  5600. if (error)
  5601. goto abort_change;
  5602. }
  5603. tsec->sid = sid;
  5604. } else {
  5605. error = -EINVAL;
  5606. goto abort_change;
  5607. }
  5608. commit_creds(new);
  5609. return size;
  5610. abort_change:
  5611. abort_creds(new);
  5612. return error;
  5613. }
  5614. /**
  5615. * selinux_getselfattr - Get SELinux current task attributes
  5616. * @attr: the requested attribute
  5617. * @ctx: buffer to receive the result
  5618. * @size: buffer size (input), buffer size used (output)
  5619. * @flags: unused
  5620. *
  5621. * Fill the passed user space @ctx with the details of the requested
  5622. * attribute.
  5623. *
  5624. * Returns the number of attributes on success, an error code otherwise.
  5625. * There will only ever be one attribute.
  5626. */
  5627. static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
  5628. u32 *size, u32 flags)
  5629. {
  5630. int rc;
  5631. char *val = NULL;
  5632. int val_len;
  5633. val_len = selinux_lsm_getattr(attr, current, &val);
  5634. if (val_len < 0)
  5635. return val_len;
  5636. rc = lsm_fill_user_ctx(ctx, size, val, val_len, LSM_ID_SELINUX, 0);
  5637. kfree(val);
  5638. return (!rc ? 1 : rc);
  5639. }
  5640. static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
  5641. u32 size, u32 flags)
  5642. {
  5643. int rc;
  5644. rc = selinux_lsm_setattr(attr, ctx->ctx, ctx->ctx_len);
  5645. if (rc > 0)
  5646. return 0;
  5647. return rc;
  5648. }
  5649. static int selinux_getprocattr(struct task_struct *p,
  5650. const char *name, char **value)
  5651. {
  5652. unsigned int attr = lsm_name_to_attr(name);
  5653. int rc;
  5654. if (attr) {
  5655. rc = selinux_lsm_getattr(attr, p, value);
  5656. if (rc != -EOPNOTSUPP)
  5657. return rc;
  5658. }
  5659. return -EINVAL;
  5660. }
  5661. static int selinux_setprocattr(const char *name, void *value, size_t size)
  5662. {
  5663. int attr = lsm_name_to_attr(name);
  5664. if (attr)
  5665. return selinux_lsm_setattr(attr, value, size);
  5666. return -EINVAL;
  5667. }
  5668. static int selinux_ismaclabel(const char *name)
  5669. {
  5670. return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
  5671. }
  5672. static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  5673. {
  5674. return security_sid_to_context(secid,
  5675. secdata, seclen);
  5676. }
  5677. static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  5678. {
  5679. return security_context_to_sid(secdata, seclen,
  5680. secid, GFP_KERNEL);
  5681. }
  5682. static void selinux_release_secctx(char *secdata, u32 seclen)
  5683. {
  5684. kfree(secdata);
  5685. }
  5686. static void selinux_inode_invalidate_secctx(struct inode *inode)
  5687. {
  5688. struct inode_security_struct *isec = selinux_inode(inode);
  5689. spin_lock(&isec->lock);
  5690. isec->initialized = LABEL_INVALID;
  5691. spin_unlock(&isec->lock);
  5692. }
  5693. /*
  5694. * called with inode->i_mutex locked
  5695. */
  5696. static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  5697. {
  5698. int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
  5699. ctx, ctxlen, 0);
  5700. /* Do not return error when suppressing label (SBLABEL_MNT not set). */
  5701. return rc == -EOPNOTSUPP ? 0 : rc;
  5702. }
  5703. /*
  5704. * called with inode->i_mutex locked
  5705. */
  5706. static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  5707. {
  5708. return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
  5709. ctx, ctxlen, 0, NULL);
  5710. }
  5711. static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  5712. {
  5713. int len = 0;
  5714. len = selinux_inode_getsecurity(&nop_mnt_idmap, inode,
  5715. XATTR_SELINUX_SUFFIX, ctx, true);
  5716. if (len < 0)
  5717. return len;
  5718. *ctxlen = len;
  5719. return 0;
  5720. }
  5721. #ifdef CONFIG_KEYS
  5722. static int selinux_key_alloc(struct key *k, const struct cred *cred,
  5723. unsigned long flags)
  5724. {
  5725. const struct task_security_struct *tsec;
  5726. struct key_security_struct *ksec = selinux_key(k);
  5727. tsec = selinux_cred(cred);
  5728. if (tsec->keycreate_sid)
  5729. ksec->sid = tsec->keycreate_sid;
  5730. else
  5731. ksec->sid = tsec->sid;
  5732. return 0;
  5733. }
  5734. static int selinux_key_permission(key_ref_t key_ref,
  5735. const struct cred *cred,
  5736. enum key_need_perm need_perm)
  5737. {
  5738. struct key *key;
  5739. struct key_security_struct *ksec;
  5740. u32 perm, sid;
  5741. switch (need_perm) {
  5742. case KEY_NEED_VIEW:
  5743. perm = KEY__VIEW;
  5744. break;
  5745. case KEY_NEED_READ:
  5746. perm = KEY__READ;
  5747. break;
  5748. case KEY_NEED_WRITE:
  5749. perm = KEY__WRITE;
  5750. break;
  5751. case KEY_NEED_SEARCH:
  5752. perm = KEY__SEARCH;
  5753. break;
  5754. case KEY_NEED_LINK:
  5755. perm = KEY__LINK;
  5756. break;
  5757. case KEY_NEED_SETATTR:
  5758. perm = KEY__SETATTR;
  5759. break;
  5760. case KEY_NEED_UNLINK:
  5761. case KEY_SYSADMIN_OVERRIDE:
  5762. case KEY_AUTHTOKEN_OVERRIDE:
  5763. case KEY_DEFER_PERM_CHECK:
  5764. return 0;
  5765. default:
  5766. WARN_ON(1);
  5767. return -EPERM;
  5768. }
  5769. sid = cred_sid(cred);
  5770. key = key_ref_to_ptr(key_ref);
  5771. ksec = selinux_key(key);
  5772. return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
  5773. }
  5774. static int selinux_key_getsecurity(struct key *key, char **_buffer)
  5775. {
  5776. struct key_security_struct *ksec = selinux_key(key);
  5777. char *context = NULL;
  5778. unsigned len;
  5779. int rc;
  5780. rc = security_sid_to_context(ksec->sid,
  5781. &context, &len);
  5782. if (!rc)
  5783. rc = len;
  5784. *_buffer = context;
  5785. return rc;
  5786. }
  5787. #ifdef CONFIG_KEY_NOTIFICATIONS
  5788. static int selinux_watch_key(struct key *key)
  5789. {
  5790. struct key_security_struct *ksec = selinux_key(key);
  5791. u32 sid = current_sid();
  5792. return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
  5793. }
  5794. #endif
  5795. #endif
  5796. #ifdef CONFIG_SECURITY_INFINIBAND
  5797. static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
  5798. {
  5799. struct common_audit_data ad;
  5800. int err;
  5801. u32 sid = 0;
  5802. struct ib_security_struct *sec = ib_sec;
  5803. struct lsm_ibpkey_audit ibpkey;
  5804. err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
  5805. if (err)
  5806. return err;
  5807. ad.type = LSM_AUDIT_DATA_IBPKEY;
  5808. ibpkey.subnet_prefix = subnet_prefix;
  5809. ibpkey.pkey = pkey_val;
  5810. ad.u.ibpkey = &ibpkey;
  5811. return avc_has_perm(sec->sid, sid,
  5812. SECCLASS_INFINIBAND_PKEY,
  5813. INFINIBAND_PKEY__ACCESS, &ad);
  5814. }
  5815. static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
  5816. u8 port_num)
  5817. {
  5818. struct common_audit_data ad;
  5819. int err;
  5820. u32 sid = 0;
  5821. struct ib_security_struct *sec = ib_sec;
  5822. struct lsm_ibendport_audit ibendport;
  5823. err = security_ib_endport_sid(dev_name, port_num,
  5824. &sid);
  5825. if (err)
  5826. return err;
  5827. ad.type = LSM_AUDIT_DATA_IBENDPORT;
  5828. ibendport.dev_name = dev_name;
  5829. ibendport.port = port_num;
  5830. ad.u.ibendport = &ibendport;
  5831. return avc_has_perm(sec->sid, sid,
  5832. SECCLASS_INFINIBAND_ENDPORT,
  5833. INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
  5834. }
  5835. static int selinux_ib_alloc_security(void *ib_sec)
  5836. {
  5837. struct ib_security_struct *sec = selinux_ib(ib_sec);
  5838. sec->sid = current_sid();
  5839. return 0;
  5840. }
  5841. #endif
  5842. #ifdef CONFIG_BPF_SYSCALL
  5843. static int selinux_bpf(int cmd, union bpf_attr *attr,
  5844. unsigned int size)
  5845. {
  5846. u32 sid = current_sid();
  5847. int ret;
  5848. switch (cmd) {
  5849. case BPF_MAP_CREATE:
  5850. ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
  5851. NULL);
  5852. break;
  5853. case BPF_PROG_LOAD:
  5854. ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
  5855. NULL);
  5856. break;
  5857. default:
  5858. ret = 0;
  5859. break;
  5860. }
  5861. return ret;
  5862. }
  5863. static u32 bpf_map_fmode_to_av(fmode_t fmode)
  5864. {
  5865. u32 av = 0;
  5866. if (fmode & FMODE_READ)
  5867. av |= BPF__MAP_READ;
  5868. if (fmode & FMODE_WRITE)
  5869. av |= BPF__MAP_WRITE;
  5870. return av;
  5871. }
  5872. /* This function will check the file pass through unix socket or binder to see
  5873. * if it is a bpf related object. And apply corresponding checks on the bpf
  5874. * object based on the type. The bpf maps and programs, not like other files and
  5875. * socket, are using a shared anonymous inode inside the kernel as their inode.
  5876. * So checking that inode cannot identify if the process have privilege to
  5877. * access the bpf object and that's why we have to add this additional check in
  5878. * selinux_file_receive and selinux_binder_transfer_files.
  5879. */
  5880. static int bpf_fd_pass(const struct file *file, u32 sid)
  5881. {
  5882. struct bpf_security_struct *bpfsec;
  5883. struct bpf_prog *prog;
  5884. struct bpf_map *map;
  5885. int ret;
  5886. if (file->f_op == &bpf_map_fops) {
  5887. map = file->private_data;
  5888. bpfsec = map->security;
  5889. ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
  5890. bpf_map_fmode_to_av(file->f_mode), NULL);
  5891. if (ret)
  5892. return ret;
  5893. } else if (file->f_op == &bpf_prog_fops) {
  5894. prog = file->private_data;
  5895. bpfsec = prog->aux->security;
  5896. ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
  5897. BPF__PROG_RUN, NULL);
  5898. if (ret)
  5899. return ret;
  5900. }
  5901. return 0;
  5902. }
  5903. static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
  5904. {
  5905. u32 sid = current_sid();
  5906. struct bpf_security_struct *bpfsec;
  5907. bpfsec = map->security;
  5908. return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
  5909. bpf_map_fmode_to_av(fmode), NULL);
  5910. }
  5911. static int selinux_bpf_prog(struct bpf_prog *prog)
  5912. {
  5913. u32 sid = current_sid();
  5914. struct bpf_security_struct *bpfsec;
  5915. bpfsec = prog->aux->security;
  5916. return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
  5917. BPF__PROG_RUN, NULL);
  5918. }
  5919. static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
  5920. struct bpf_token *token)
  5921. {
  5922. struct bpf_security_struct *bpfsec;
  5923. bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
  5924. if (!bpfsec)
  5925. return -ENOMEM;
  5926. bpfsec->sid = current_sid();
  5927. map->security = bpfsec;
  5928. return 0;
  5929. }
  5930. static void selinux_bpf_map_free(struct bpf_map *map)
  5931. {
  5932. struct bpf_security_struct *bpfsec = map->security;
  5933. map->security = NULL;
  5934. kfree(bpfsec);
  5935. }
  5936. static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
  5937. struct bpf_token *token)
  5938. {
  5939. struct bpf_security_struct *bpfsec;
  5940. bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
  5941. if (!bpfsec)
  5942. return -ENOMEM;
  5943. bpfsec->sid = current_sid();
  5944. prog->aux->security = bpfsec;
  5945. return 0;
  5946. }
  5947. static void selinux_bpf_prog_free(struct bpf_prog *prog)
  5948. {
  5949. struct bpf_security_struct *bpfsec = prog->aux->security;
  5950. prog->aux->security = NULL;
  5951. kfree(bpfsec);
  5952. }
  5953. static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
  5954. const struct path *path)
  5955. {
  5956. struct bpf_security_struct *bpfsec;
  5957. bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
  5958. if (!bpfsec)
  5959. return -ENOMEM;
  5960. bpfsec->sid = current_sid();
  5961. token->security = bpfsec;
  5962. return 0;
  5963. }
  5964. static void selinux_bpf_token_free(struct bpf_token *token)
  5965. {
  5966. struct bpf_security_struct *bpfsec = token->security;
  5967. token->security = NULL;
  5968. kfree(bpfsec);
  5969. }
  5970. #endif
  5971. struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
  5972. .lbs_cred = sizeof(struct task_security_struct),
  5973. .lbs_file = sizeof(struct file_security_struct),
  5974. .lbs_inode = sizeof(struct inode_security_struct),
  5975. .lbs_ipc = sizeof(struct ipc_security_struct),
  5976. .lbs_key = sizeof(struct key_security_struct),
  5977. .lbs_msg_msg = sizeof(struct msg_security_struct),
  5978. #ifdef CONFIG_PERF_EVENTS
  5979. .lbs_perf_event = sizeof(struct perf_event_security_struct),
  5980. #endif
  5981. .lbs_sock = sizeof(struct sk_security_struct),
  5982. .lbs_superblock = sizeof(struct superblock_security_struct),
  5983. .lbs_xattr_count = SELINUX_INODE_INIT_XATTRS,
  5984. .lbs_tun_dev = sizeof(struct tun_security_struct),
  5985. .lbs_ib = sizeof(struct ib_security_struct),
  5986. };
  5987. #ifdef CONFIG_PERF_EVENTS
  5988. static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
  5989. {
  5990. u32 requested, sid = current_sid();
  5991. if (type == PERF_SECURITY_OPEN)
  5992. requested = PERF_EVENT__OPEN;
  5993. else if (type == PERF_SECURITY_CPU)
  5994. requested = PERF_EVENT__CPU;
  5995. else if (type == PERF_SECURITY_KERNEL)
  5996. requested = PERF_EVENT__KERNEL;
  5997. else if (type == PERF_SECURITY_TRACEPOINT)
  5998. requested = PERF_EVENT__TRACEPOINT;
  5999. else
  6000. return -EINVAL;
  6001. return avc_has_perm(sid, sid, SECCLASS_PERF_EVENT,
  6002. requested, NULL);
  6003. }
  6004. static int selinux_perf_event_alloc(struct perf_event *event)
  6005. {
  6006. struct perf_event_security_struct *perfsec;
  6007. perfsec = selinux_perf_event(event->security);
  6008. perfsec->sid = current_sid();
  6009. return 0;
  6010. }
  6011. static int selinux_perf_event_read(struct perf_event *event)
  6012. {
  6013. struct perf_event_security_struct *perfsec = event->security;
  6014. u32 sid = current_sid();
  6015. return avc_has_perm(sid, perfsec->sid,
  6016. SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
  6017. }
  6018. static int selinux_perf_event_write(struct perf_event *event)
  6019. {
  6020. struct perf_event_security_struct *perfsec = event->security;
  6021. u32 sid = current_sid();
  6022. return avc_has_perm(sid, perfsec->sid,
  6023. SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
  6024. }
  6025. #endif
  6026. #ifdef CONFIG_IO_URING
  6027. /**
  6028. * selinux_uring_override_creds - check the requested cred override
  6029. * @new: the target creds
  6030. *
  6031. * Check to see if the current task is allowed to override it's credentials
  6032. * to service an io_uring operation.
  6033. */
  6034. static int selinux_uring_override_creds(const struct cred *new)
  6035. {
  6036. return avc_has_perm(current_sid(), cred_sid(new),
  6037. SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
  6038. }
  6039. /**
  6040. * selinux_uring_sqpoll - check if a io_uring polling thread can be created
  6041. *
  6042. * Check to see if the current task is allowed to create a new io_uring
  6043. * kernel polling thread.
  6044. */
  6045. static int selinux_uring_sqpoll(void)
  6046. {
  6047. u32 sid = current_sid();
  6048. return avc_has_perm(sid, sid,
  6049. SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
  6050. }
  6051. /**
  6052. * selinux_uring_cmd - check if IORING_OP_URING_CMD is allowed
  6053. * @ioucmd: the io_uring command structure
  6054. *
  6055. * Check to see if the current domain is allowed to execute an
  6056. * IORING_OP_URING_CMD against the device/file specified in @ioucmd.
  6057. *
  6058. */
  6059. static int selinux_uring_cmd(struct io_uring_cmd *ioucmd)
  6060. {
  6061. struct file *file = ioucmd->file;
  6062. struct inode *inode = file_inode(file);
  6063. struct inode_security_struct *isec = selinux_inode(inode);
  6064. struct common_audit_data ad;
  6065. ad.type = LSM_AUDIT_DATA_FILE;
  6066. ad.u.file = file;
  6067. return avc_has_perm(current_sid(), isec->sid,
  6068. SECCLASS_IO_URING, IO_URING__CMD, &ad);
  6069. }
  6070. #endif /* CONFIG_IO_URING */
  6071. static const struct lsm_id selinux_lsmid = {
  6072. .name = "selinux",
  6073. .id = LSM_ID_SELINUX,
  6074. };
  6075. /*
  6076. * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
  6077. * 1. any hooks that don't belong to (2.) or (3.) below,
  6078. * 2. hooks that both access structures allocated by other hooks, and allocate
  6079. * structures that can be later accessed by other hooks (mostly "cloning"
  6080. * hooks),
  6081. * 3. hooks that only allocate structures that can be later accessed by other
  6082. * hooks ("allocating" hooks).
  6083. *
  6084. * Please follow block comment delimiters in the list to keep this order.
  6085. */
  6086. static struct security_hook_list selinux_hooks[] __ro_after_init = {
  6087. LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
  6088. LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
  6089. LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
  6090. LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
  6091. LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
  6092. LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
  6093. LSM_HOOK_INIT(capget, selinux_capget),
  6094. LSM_HOOK_INIT(capset, selinux_capset),
  6095. LSM_HOOK_INIT(capable, selinux_capable),
  6096. LSM_HOOK_INIT(quotactl, selinux_quotactl),
  6097. LSM_HOOK_INIT(quota_on, selinux_quota_on),
  6098. LSM_HOOK_INIT(syslog, selinux_syslog),
  6099. LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
  6100. LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
  6101. LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
  6102. LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
  6103. LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
  6104. LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
  6105. LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
  6106. LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
  6107. LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
  6108. LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
  6109. LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
  6110. LSM_HOOK_INIT(sb_mount, selinux_mount),
  6111. LSM_HOOK_INIT(sb_umount, selinux_umount),
  6112. LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
  6113. LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
  6114. LSM_HOOK_INIT(move_mount, selinux_move_mount),
  6115. LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
  6116. LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
  6117. LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
  6118. LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
  6119. LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
  6120. LSM_HOOK_INIT(inode_create, selinux_inode_create),
  6121. LSM_HOOK_INIT(inode_link, selinux_inode_link),
  6122. LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
  6123. LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
  6124. LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
  6125. LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
  6126. LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
  6127. LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
  6128. LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
  6129. LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
  6130. LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
  6131. LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
  6132. LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
  6133. LSM_HOOK_INIT(inode_xattr_skipcap, selinux_inode_xattr_skipcap),
  6134. LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
  6135. LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
  6136. LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
  6137. LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
  6138. LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
  6139. LSM_HOOK_INIT(inode_set_acl, selinux_inode_set_acl),
  6140. LSM_HOOK_INIT(inode_get_acl, selinux_inode_get_acl),
  6141. LSM_HOOK_INIT(inode_remove_acl, selinux_inode_remove_acl),
  6142. LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
  6143. LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
  6144. LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
  6145. LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
  6146. LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
  6147. LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
  6148. LSM_HOOK_INIT(path_notify, selinux_path_notify),
  6149. LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
  6150. LSM_HOOK_INIT(file_permission, selinux_file_permission),
  6151. LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
  6152. LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
  6153. LSM_HOOK_INIT(file_ioctl_compat, selinux_file_ioctl_compat),
  6154. LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
  6155. LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
  6156. LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
  6157. LSM_HOOK_INIT(file_lock, selinux_file_lock),
  6158. LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
  6159. LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
  6160. LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
  6161. LSM_HOOK_INIT(file_receive, selinux_file_receive),
  6162. LSM_HOOK_INIT(file_open, selinux_file_open),
  6163. LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
  6164. LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
  6165. LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
  6166. LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
  6167. LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
  6168. LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
  6169. LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
  6170. LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
  6171. LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
  6172. LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
  6173. LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
  6174. LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
  6175. LSM_HOOK_INIT(current_getsecid_subj, selinux_current_getsecid_subj),
  6176. LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
  6177. LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
  6178. LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
  6179. LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
  6180. LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
  6181. LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
  6182. LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
  6183. LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
  6184. LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
  6185. LSM_HOOK_INIT(task_kill, selinux_task_kill),
  6186. LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
  6187. LSM_HOOK_INIT(userns_create, selinux_userns_create),
  6188. LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
  6189. LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
  6190. LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
  6191. LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
  6192. LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
  6193. LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
  6194. LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
  6195. LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
  6196. LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
  6197. LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
  6198. LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
  6199. LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
  6200. LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
  6201. LSM_HOOK_INIT(getselfattr, selinux_getselfattr),
  6202. LSM_HOOK_INIT(setselfattr, selinux_setselfattr),
  6203. LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
  6204. LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
  6205. LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
  6206. LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
  6207. LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
  6208. LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
  6209. LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
  6210. LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
  6211. LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
  6212. LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
  6213. LSM_HOOK_INIT(socket_create, selinux_socket_create),
  6214. LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
  6215. LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
  6216. LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
  6217. LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
  6218. LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
  6219. LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
  6220. LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
  6221. LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
  6222. LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
  6223. LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
  6224. LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
  6225. LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
  6226. LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
  6227. LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
  6228. LSM_HOOK_INIT(socket_getpeersec_stream,
  6229. selinux_socket_getpeersec_stream),
  6230. LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
  6231. LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
  6232. LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
  6233. LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
  6234. LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
  6235. LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
  6236. LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
  6237. LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
  6238. LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established),
  6239. LSM_HOOK_INIT(mptcp_add_subflow, selinux_mptcp_add_subflow),
  6240. LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
  6241. LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
  6242. LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
  6243. LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
  6244. LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
  6245. LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
  6246. LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
  6247. LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
  6248. LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
  6249. LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
  6250. LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
  6251. #ifdef CONFIG_SECURITY_INFINIBAND
  6252. LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
  6253. LSM_HOOK_INIT(ib_endport_manage_subnet,
  6254. selinux_ib_endport_manage_subnet),
  6255. #endif
  6256. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  6257. LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
  6258. LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
  6259. LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
  6260. LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
  6261. LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
  6262. LSM_HOOK_INIT(xfrm_state_pol_flow_match,
  6263. selinux_xfrm_state_pol_flow_match),
  6264. LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
  6265. #endif
  6266. #ifdef CONFIG_KEYS
  6267. LSM_HOOK_INIT(key_permission, selinux_key_permission),
  6268. LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
  6269. #ifdef CONFIG_KEY_NOTIFICATIONS
  6270. LSM_HOOK_INIT(watch_key, selinux_watch_key),
  6271. #endif
  6272. #endif
  6273. #ifdef CONFIG_AUDIT
  6274. LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
  6275. LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
  6276. LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
  6277. #endif
  6278. #ifdef CONFIG_BPF_SYSCALL
  6279. LSM_HOOK_INIT(bpf, selinux_bpf),
  6280. LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
  6281. LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
  6282. LSM_HOOK_INIT(bpf_map_free, selinux_bpf_map_free),
  6283. LSM_HOOK_INIT(bpf_prog_free, selinux_bpf_prog_free),
  6284. LSM_HOOK_INIT(bpf_token_free, selinux_bpf_token_free),
  6285. #endif
  6286. #ifdef CONFIG_PERF_EVENTS
  6287. LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
  6288. LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
  6289. LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
  6290. #endif
  6291. #ifdef CONFIG_IO_URING
  6292. LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
  6293. LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
  6294. LSM_HOOK_INIT(uring_cmd, selinux_uring_cmd),
  6295. #endif
  6296. /*
  6297. * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
  6298. */
  6299. LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount),
  6300. LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
  6301. LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
  6302. LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
  6303. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  6304. LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
  6305. #endif
  6306. /*
  6307. * PUT "ALLOCATING" HOOKS HERE
  6308. */
  6309. LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
  6310. LSM_HOOK_INIT(msg_queue_alloc_security,
  6311. selinux_msg_queue_alloc_security),
  6312. LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
  6313. LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
  6314. LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
  6315. LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
  6316. LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
  6317. LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
  6318. LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
  6319. LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
  6320. #ifdef CONFIG_SECURITY_INFINIBAND
  6321. LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
  6322. #endif
  6323. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  6324. LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
  6325. LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
  6326. LSM_HOOK_INIT(xfrm_state_alloc_acquire,
  6327. selinux_xfrm_state_alloc_acquire),
  6328. #endif
  6329. #ifdef CONFIG_KEYS
  6330. LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
  6331. #endif
  6332. #ifdef CONFIG_AUDIT
  6333. LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
  6334. #endif
  6335. #ifdef CONFIG_BPF_SYSCALL
  6336. LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
  6337. LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
  6338. LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
  6339. #endif
  6340. #ifdef CONFIG_PERF_EVENTS
  6341. LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
  6342. #endif
  6343. };
  6344. static __init int selinux_init(void)
  6345. {
  6346. pr_info("SELinux: Initializing.\n");
  6347. memset(&selinux_state, 0, sizeof(selinux_state));
  6348. enforcing_set(selinux_enforcing_boot);
  6349. selinux_avc_init();
  6350. mutex_init(&selinux_state.status_lock);
  6351. mutex_init(&selinux_state.policy_mutex);
  6352. /* Set the security state for the initial task. */
  6353. cred_init_security();
  6354. default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
  6355. if (!default_noexec)
  6356. pr_notice("SELinux: virtual memory is executable by default\n");
  6357. avc_init();
  6358. avtab_cache_init();
  6359. ebitmap_cache_init();
  6360. hashtab_cache_init();
  6361. security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks),
  6362. &selinux_lsmid);
  6363. if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
  6364. panic("SELinux: Unable to register AVC netcache callback\n");
  6365. if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
  6366. panic("SELinux: Unable to register AVC LSM notifier callback\n");
  6367. if (selinux_enforcing_boot)
  6368. pr_debug("SELinux: Starting in enforcing mode\n");
  6369. else
  6370. pr_debug("SELinux: Starting in permissive mode\n");
  6371. fs_validate_description("selinux", selinux_fs_parameters);
  6372. return 0;
  6373. }
  6374. static void delayed_superblock_init(struct super_block *sb, void *unused)
  6375. {
  6376. selinux_set_mnt_opts(sb, NULL, 0, NULL);
  6377. }
  6378. void selinux_complete_init(void)
  6379. {
  6380. pr_debug("SELinux: Completing initialization.\n");
  6381. /* Set up any superblocks initialized prior to the policy load. */
  6382. pr_debug("SELinux: Setting up existing superblocks.\n");
  6383. iterate_supers(delayed_superblock_init, NULL);
  6384. }
  6385. /* SELinux requires early initialization in order to label
  6386. all processes and objects when they are created. */
  6387. DEFINE_LSM(selinux) = {
  6388. .name = "selinux",
  6389. .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
  6390. .enabled = &selinux_enabled_boot,
  6391. .blobs = &selinux_blob_sizes,
  6392. .init = selinux_init,
  6393. };
  6394. #if defined(CONFIG_NETFILTER)
  6395. static const struct nf_hook_ops selinux_nf_ops[] = {
  6396. {
  6397. .hook = selinux_ip_postroute,
  6398. .pf = NFPROTO_IPV4,
  6399. .hooknum = NF_INET_POST_ROUTING,
  6400. .priority = NF_IP_PRI_SELINUX_LAST,
  6401. },
  6402. {
  6403. .hook = selinux_ip_forward,
  6404. .pf = NFPROTO_IPV4,
  6405. .hooknum = NF_INET_FORWARD,
  6406. .priority = NF_IP_PRI_SELINUX_FIRST,
  6407. },
  6408. {
  6409. .hook = selinux_ip_output,
  6410. .pf = NFPROTO_IPV4,
  6411. .hooknum = NF_INET_LOCAL_OUT,
  6412. .priority = NF_IP_PRI_SELINUX_FIRST,
  6413. },
  6414. #if IS_ENABLED(CONFIG_IPV6)
  6415. {
  6416. .hook = selinux_ip_postroute,
  6417. .pf = NFPROTO_IPV6,
  6418. .hooknum = NF_INET_POST_ROUTING,
  6419. .priority = NF_IP6_PRI_SELINUX_LAST,
  6420. },
  6421. {
  6422. .hook = selinux_ip_forward,
  6423. .pf = NFPROTO_IPV6,
  6424. .hooknum = NF_INET_FORWARD,
  6425. .priority = NF_IP6_PRI_SELINUX_FIRST,
  6426. },
  6427. {
  6428. .hook = selinux_ip_output,
  6429. .pf = NFPROTO_IPV6,
  6430. .hooknum = NF_INET_LOCAL_OUT,
  6431. .priority = NF_IP6_PRI_SELINUX_FIRST,
  6432. },
  6433. #endif /* IPV6 */
  6434. };
  6435. static int __net_init selinux_nf_register(struct net *net)
  6436. {
  6437. return nf_register_net_hooks(net, selinux_nf_ops,
  6438. ARRAY_SIZE(selinux_nf_ops));
  6439. }
  6440. static void __net_exit selinux_nf_unregister(struct net *net)
  6441. {
  6442. nf_unregister_net_hooks(net, selinux_nf_ops,
  6443. ARRAY_SIZE(selinux_nf_ops));
  6444. }
  6445. static struct pernet_operations selinux_net_ops = {
  6446. .init = selinux_nf_register,
  6447. .exit = selinux_nf_unregister,
  6448. };
  6449. static int __init selinux_nf_ip_init(void)
  6450. {
  6451. int err;
  6452. if (!selinux_enabled_boot)
  6453. return 0;
  6454. pr_debug("SELinux: Registering netfilter hooks\n");
  6455. err = register_pernet_subsys(&selinux_net_ops);
  6456. if (err)
  6457. panic("SELinux: register_pernet_subsys: error %d\n", err);
  6458. return 0;
  6459. }
  6460. __initcall(selinux_nf_ip_init);
  6461. #endif /* CONFIG_NETFILTER */