hooks.c 188 KB

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