smack_lsm.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839
  1. /*
  2. * Simplified MAC Kernel (smack) security module
  3. *
  4. * This file contains the smack hook function implementations.
  5. *
  6. * Authors:
  7. * Casey Schaufler <casey@schaufler-ca.com>
  8. * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
  9. *
  10. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  11. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  12. * Paul Moore <paul@paul-moore.com>
  13. * Copyright (C) 2010 Nokia Corporation
  14. * Copyright (C) 2011 Intel Corporation.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2,
  18. * as published by the Free Software Foundation.
  19. */
  20. #include <linux/xattr.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/mount.h>
  23. #include <linux/stat.h>
  24. #include <linux/kd.h>
  25. #include <asm/ioctls.h>
  26. #include <linux/ip.h>
  27. #include <linux/tcp.h>
  28. #include <linux/udp.h>
  29. #include <linux/dccp.h>
  30. #include <linux/slab.h>
  31. #include <linux/mutex.h>
  32. #include <linux/pipe_fs_i.h>
  33. #include <net/cipso_ipv4.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <linux/audit.h>
  37. #include <linux/magic.h>
  38. #include <linux/dcache.h>
  39. #include <linux/personality.h>
  40. #include <linux/msg.h>
  41. #include <linux/shm.h>
  42. #include <linux/binfmts.h>
  43. #include <linux/parser.h>
  44. #include "smack.h"
  45. #define TRANS_TRUE "TRUE"
  46. #define TRANS_TRUE_SIZE 4
  47. #define SMK_CONNECTING 0
  48. #define SMK_RECEIVING 1
  49. #define SMK_SENDING 2
  50. #ifdef SMACK_IPV6_PORT_LABELING
  51. static LIST_HEAD(smk_ipv6_port_list);
  52. #endif
  53. static struct kmem_cache *smack_inode_cache;
  54. int smack_enabled;
  55. static const match_table_t smk_mount_tokens = {
  56. {Opt_fsdefault, SMK_FSDEFAULT "%s"},
  57. {Opt_fsfloor, SMK_FSFLOOR "%s"},
  58. {Opt_fshat, SMK_FSHAT "%s"},
  59. {Opt_fsroot, SMK_FSROOT "%s"},
  60. {Opt_fstransmute, SMK_FSTRANS "%s"},
  61. {Opt_error, NULL},
  62. };
  63. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  64. static char *smk_bu_mess[] = {
  65. "Bringup Error", /* Unused */
  66. "Bringup", /* SMACK_BRINGUP_ALLOW */
  67. "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
  68. "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
  69. };
  70. static void smk_bu_mode(int mode, char *s)
  71. {
  72. int i = 0;
  73. if (mode & MAY_READ)
  74. s[i++] = 'r';
  75. if (mode & MAY_WRITE)
  76. s[i++] = 'w';
  77. if (mode & MAY_EXEC)
  78. s[i++] = 'x';
  79. if (mode & MAY_APPEND)
  80. s[i++] = 'a';
  81. if (mode & MAY_TRANSMUTE)
  82. s[i++] = 't';
  83. if (mode & MAY_LOCK)
  84. s[i++] = 'l';
  85. if (i == 0)
  86. s[i++] = '-';
  87. s[i] = '\0';
  88. }
  89. #endif
  90. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  91. static int smk_bu_note(char *note, struct smack_known *sskp,
  92. struct smack_known *oskp, int mode, int rc)
  93. {
  94. char acc[SMK_NUM_ACCESS_TYPE + 1];
  95. if (rc <= 0)
  96. return rc;
  97. if (rc > SMACK_UNCONFINED_OBJECT)
  98. rc = 0;
  99. smk_bu_mode(mode, acc);
  100. pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
  101. sskp->smk_known, oskp->smk_known, acc, note);
  102. return 0;
  103. }
  104. #else
  105. #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
  106. #endif
  107. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  108. static int smk_bu_current(char *note, struct smack_known *oskp,
  109. int mode, int rc)
  110. {
  111. struct task_smack *tsp = current_security();
  112. char acc[SMK_NUM_ACCESS_TYPE + 1];
  113. if (rc <= 0)
  114. return rc;
  115. if (rc > SMACK_UNCONFINED_OBJECT)
  116. rc = 0;
  117. smk_bu_mode(mode, acc);
  118. pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
  119. tsp->smk_task->smk_known, oskp->smk_known,
  120. acc, current->comm, note);
  121. return 0;
  122. }
  123. #else
  124. #define smk_bu_current(note, oskp, mode, RC) (RC)
  125. #endif
  126. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  127. static int smk_bu_task(struct task_struct *otp, int mode, int rc)
  128. {
  129. struct task_smack *tsp = current_security();
  130. struct smack_known *smk_task = smk_of_task_struct(otp);
  131. char acc[SMK_NUM_ACCESS_TYPE + 1];
  132. if (rc <= 0)
  133. return rc;
  134. if (rc > SMACK_UNCONFINED_OBJECT)
  135. rc = 0;
  136. smk_bu_mode(mode, acc);
  137. pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
  138. tsp->smk_task->smk_known, smk_task->smk_known, acc,
  139. current->comm, otp->comm);
  140. return 0;
  141. }
  142. #else
  143. #define smk_bu_task(otp, mode, RC) (RC)
  144. #endif
  145. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  146. static int smk_bu_inode(struct inode *inode, int mode, int rc)
  147. {
  148. struct task_smack *tsp = current_security();
  149. struct inode_smack *isp = inode->i_security;
  150. char acc[SMK_NUM_ACCESS_TYPE + 1];
  151. if (isp->smk_flags & SMK_INODE_IMPURE)
  152. pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
  153. inode->i_sb->s_id, inode->i_ino, current->comm);
  154. if (rc <= 0)
  155. return rc;
  156. if (rc > SMACK_UNCONFINED_OBJECT)
  157. rc = 0;
  158. if (rc == SMACK_UNCONFINED_SUBJECT &&
  159. (mode & (MAY_WRITE | MAY_APPEND)))
  160. isp->smk_flags |= SMK_INODE_IMPURE;
  161. smk_bu_mode(mode, acc);
  162. pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
  163. tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
  164. inode->i_sb->s_id, inode->i_ino, current->comm);
  165. return 0;
  166. }
  167. #else
  168. #define smk_bu_inode(inode, mode, RC) (RC)
  169. #endif
  170. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  171. static int smk_bu_file(struct file *file, int mode, int rc)
  172. {
  173. struct task_smack *tsp = current_security();
  174. struct smack_known *sskp = tsp->smk_task;
  175. struct inode *inode = file_inode(file);
  176. struct inode_smack *isp = inode->i_security;
  177. char acc[SMK_NUM_ACCESS_TYPE + 1];
  178. if (isp->smk_flags & SMK_INODE_IMPURE)
  179. pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
  180. inode->i_sb->s_id, inode->i_ino, current->comm);
  181. if (rc <= 0)
  182. return rc;
  183. if (rc > SMACK_UNCONFINED_OBJECT)
  184. rc = 0;
  185. smk_bu_mode(mode, acc);
  186. pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
  187. sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
  188. inode->i_sb->s_id, inode->i_ino, file,
  189. current->comm);
  190. return 0;
  191. }
  192. #else
  193. #define smk_bu_file(file, mode, RC) (RC)
  194. #endif
  195. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  196. static int smk_bu_credfile(const struct cred *cred, struct file *file,
  197. int mode, int rc)
  198. {
  199. struct task_smack *tsp = cred->security;
  200. struct smack_known *sskp = tsp->smk_task;
  201. struct inode *inode = file->f_inode;
  202. struct inode_smack *isp = inode->i_security;
  203. char acc[SMK_NUM_ACCESS_TYPE + 1];
  204. if (isp->smk_flags & SMK_INODE_IMPURE)
  205. pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
  206. inode->i_sb->s_id, inode->i_ino, current->comm);
  207. if (rc <= 0)
  208. return rc;
  209. if (rc > SMACK_UNCONFINED_OBJECT)
  210. rc = 0;
  211. smk_bu_mode(mode, acc);
  212. pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
  213. sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
  214. inode->i_sb->s_id, inode->i_ino, file,
  215. current->comm);
  216. return 0;
  217. }
  218. #else
  219. #define smk_bu_credfile(cred, file, mode, RC) (RC)
  220. #endif
  221. /**
  222. * smk_fetch - Fetch the smack label from a file.
  223. * @name: type of the label (attribute)
  224. * @ip: a pointer to the inode
  225. * @dp: a pointer to the dentry
  226. *
  227. * Returns a pointer to the master list entry for the Smack label,
  228. * NULL if there was no label to fetch, or an error code.
  229. */
  230. static struct smack_known *smk_fetch(const char *name, struct inode *ip,
  231. struct dentry *dp)
  232. {
  233. int rc;
  234. char *buffer;
  235. struct smack_known *skp = NULL;
  236. if (!(ip->i_opflags & IOP_XATTR))
  237. return ERR_PTR(-EOPNOTSUPP);
  238. buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
  239. if (buffer == NULL)
  240. return ERR_PTR(-ENOMEM);
  241. rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
  242. if (rc < 0)
  243. skp = ERR_PTR(rc);
  244. else if (rc == 0)
  245. skp = NULL;
  246. else
  247. skp = smk_import_entry(buffer, rc);
  248. kfree(buffer);
  249. return skp;
  250. }
  251. /**
  252. * new_inode_smack - allocate an inode security blob
  253. * @skp: a pointer to the Smack label entry to use in the blob
  254. *
  255. * Returns the new blob or NULL if there's no memory available
  256. */
  257. static struct inode_smack *new_inode_smack(struct smack_known *skp)
  258. {
  259. struct inode_smack *isp;
  260. isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
  261. if (isp == NULL)
  262. return NULL;
  263. isp->smk_inode = skp;
  264. isp->smk_flags = 0;
  265. mutex_init(&isp->smk_lock);
  266. return isp;
  267. }
  268. /**
  269. * new_task_smack - allocate a task security blob
  270. * @task: a pointer to the Smack label for the running task
  271. * @forked: a pointer to the Smack label for the forked task
  272. * @gfp: type of the memory for the allocation
  273. *
  274. * Returns the new blob or NULL if there's no memory available
  275. */
  276. static struct task_smack *new_task_smack(struct smack_known *task,
  277. struct smack_known *forked, gfp_t gfp)
  278. {
  279. struct task_smack *tsp;
  280. tsp = kzalloc(sizeof(struct task_smack), gfp);
  281. if (tsp == NULL)
  282. return NULL;
  283. tsp->smk_task = task;
  284. tsp->smk_forked = forked;
  285. INIT_LIST_HEAD(&tsp->smk_rules);
  286. INIT_LIST_HEAD(&tsp->smk_relabel);
  287. mutex_init(&tsp->smk_rules_lock);
  288. return tsp;
  289. }
  290. /**
  291. * smk_copy_rules - copy a rule set
  292. * @nhead: new rules header pointer
  293. * @ohead: old rules header pointer
  294. * @gfp: type of the memory for the allocation
  295. *
  296. * Returns 0 on success, -ENOMEM on error
  297. */
  298. static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
  299. gfp_t gfp)
  300. {
  301. struct smack_rule *nrp;
  302. struct smack_rule *orp;
  303. int rc = 0;
  304. INIT_LIST_HEAD(nhead);
  305. list_for_each_entry_rcu(orp, ohead, list) {
  306. nrp = kzalloc(sizeof(struct smack_rule), gfp);
  307. if (nrp == NULL) {
  308. rc = -ENOMEM;
  309. break;
  310. }
  311. *nrp = *orp;
  312. list_add_rcu(&nrp->list, nhead);
  313. }
  314. return rc;
  315. }
  316. /**
  317. * smk_copy_relabel - copy smk_relabel labels list
  318. * @nhead: new rules header pointer
  319. * @ohead: old rules header pointer
  320. * @gfp: type of the memory for the allocation
  321. *
  322. * Returns 0 on success, -ENOMEM on error
  323. */
  324. static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
  325. gfp_t gfp)
  326. {
  327. struct smack_known_list_elem *nklep;
  328. struct smack_known_list_elem *oklep;
  329. INIT_LIST_HEAD(nhead);
  330. list_for_each_entry(oklep, ohead, list) {
  331. nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
  332. if (nklep == NULL) {
  333. smk_destroy_label_list(nhead);
  334. return -ENOMEM;
  335. }
  336. nklep->smk_label = oklep->smk_label;
  337. list_add(&nklep->list, nhead);
  338. }
  339. return 0;
  340. }
  341. /**
  342. * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
  343. * @mode - input mode in form of PTRACE_MODE_*
  344. *
  345. * Returns a converted MAY_* mode usable by smack rules
  346. */
  347. static inline unsigned int smk_ptrace_mode(unsigned int mode)
  348. {
  349. if (mode & PTRACE_MODE_ATTACH)
  350. return MAY_READWRITE;
  351. if (mode & PTRACE_MODE_READ)
  352. return MAY_READ;
  353. return 0;
  354. }
  355. /**
  356. * smk_ptrace_rule_check - helper for ptrace access
  357. * @tracer: tracer process
  358. * @tracee_known: label entry of the process that's about to be traced
  359. * @mode: ptrace attachment mode (PTRACE_MODE_*)
  360. * @func: name of the function that called us, used for audit
  361. *
  362. * Returns 0 on access granted, -error on error
  363. */
  364. static int smk_ptrace_rule_check(struct task_struct *tracer,
  365. struct smack_known *tracee_known,
  366. unsigned int mode, const char *func)
  367. {
  368. int rc;
  369. struct smk_audit_info ad, *saip = NULL;
  370. struct task_smack *tsp;
  371. struct smack_known *tracer_known;
  372. if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
  373. smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
  374. smk_ad_setfield_u_tsk(&ad, tracer);
  375. saip = &ad;
  376. }
  377. rcu_read_lock();
  378. tsp = __task_cred(tracer)->security;
  379. tracer_known = smk_of_task(tsp);
  380. if ((mode & PTRACE_MODE_ATTACH) &&
  381. (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
  382. smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
  383. if (tracer_known->smk_known == tracee_known->smk_known)
  384. rc = 0;
  385. else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
  386. rc = -EACCES;
  387. else if (capable(CAP_SYS_PTRACE))
  388. rc = 0;
  389. else
  390. rc = -EACCES;
  391. if (saip)
  392. smack_log(tracer_known->smk_known,
  393. tracee_known->smk_known,
  394. 0, rc, saip);
  395. rcu_read_unlock();
  396. return rc;
  397. }
  398. /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
  399. rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
  400. rcu_read_unlock();
  401. return rc;
  402. }
  403. /*
  404. * LSM hooks.
  405. * We he, that is fun!
  406. */
  407. /**
  408. * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
  409. * @ctp: child task pointer
  410. * @mode: ptrace attachment mode (PTRACE_MODE_*)
  411. *
  412. * Returns 0 if access is OK, an error code otherwise
  413. *
  414. * Do the capability checks.
  415. */
  416. static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
  417. {
  418. struct smack_known *skp;
  419. skp = smk_of_task_struct(ctp);
  420. return smk_ptrace_rule_check(current, skp, mode, __func__);
  421. }
  422. /**
  423. * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
  424. * @ptp: parent task pointer
  425. *
  426. * Returns 0 if access is OK, an error code otherwise
  427. *
  428. * Do the capability checks, and require PTRACE_MODE_ATTACH.
  429. */
  430. static int smack_ptrace_traceme(struct task_struct *ptp)
  431. {
  432. int rc;
  433. struct smack_known *skp;
  434. skp = smk_of_task(current_security());
  435. rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
  436. return rc;
  437. }
  438. /**
  439. * smack_syslog - Smack approval on syslog
  440. * @type: message type
  441. *
  442. * Returns 0 on success, error code otherwise.
  443. */
  444. static int smack_syslog(int typefrom_file)
  445. {
  446. int rc = 0;
  447. struct smack_known *skp = smk_of_current();
  448. if (smack_privileged(CAP_MAC_OVERRIDE))
  449. return 0;
  450. if (smack_syslog_label != NULL && smack_syslog_label != skp)
  451. rc = -EACCES;
  452. return rc;
  453. }
  454. /*
  455. * Superblock Hooks.
  456. */
  457. /**
  458. * smack_sb_alloc_security - allocate a superblock blob
  459. * @sb: the superblock getting the blob
  460. *
  461. * Returns 0 on success or -ENOMEM on error.
  462. */
  463. static int smack_sb_alloc_security(struct super_block *sb)
  464. {
  465. struct superblock_smack *sbsp;
  466. sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
  467. if (sbsp == NULL)
  468. return -ENOMEM;
  469. sbsp->smk_root = &smack_known_floor;
  470. sbsp->smk_default = &smack_known_floor;
  471. sbsp->smk_floor = &smack_known_floor;
  472. sbsp->smk_hat = &smack_known_hat;
  473. /*
  474. * SMK_SB_INITIALIZED will be zero from kzalloc.
  475. */
  476. sb->s_security = sbsp;
  477. return 0;
  478. }
  479. /**
  480. * smack_sb_free_security - free a superblock blob
  481. * @sb: the superblock getting the blob
  482. *
  483. */
  484. static void smack_sb_free_security(struct super_block *sb)
  485. {
  486. kfree(sb->s_security);
  487. sb->s_security = NULL;
  488. }
  489. /**
  490. * smack_sb_copy_data - copy mount options data for processing
  491. * @orig: where to start
  492. * @smackopts: mount options string
  493. *
  494. * Returns 0 on success or -ENOMEM on error.
  495. *
  496. * Copy the Smack specific mount options out of the mount
  497. * options list.
  498. */
  499. static int smack_sb_copy_data(char *orig, char *smackopts)
  500. {
  501. char *cp, *commap, *otheropts, *dp;
  502. otheropts = (char *)get_zeroed_page(GFP_KERNEL);
  503. if (otheropts == NULL)
  504. return -ENOMEM;
  505. for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
  506. if (strstr(cp, SMK_FSDEFAULT) == cp)
  507. dp = smackopts;
  508. else if (strstr(cp, SMK_FSFLOOR) == cp)
  509. dp = smackopts;
  510. else if (strstr(cp, SMK_FSHAT) == cp)
  511. dp = smackopts;
  512. else if (strstr(cp, SMK_FSROOT) == cp)
  513. dp = smackopts;
  514. else if (strstr(cp, SMK_FSTRANS) == cp)
  515. dp = smackopts;
  516. else
  517. dp = otheropts;
  518. commap = strchr(cp, ',');
  519. if (commap != NULL)
  520. *commap = '\0';
  521. if (*dp != '\0')
  522. strcat(dp, ",");
  523. strcat(dp, cp);
  524. }
  525. strcpy(orig, otheropts);
  526. free_page((unsigned long)otheropts);
  527. return 0;
  528. }
  529. /**
  530. * smack_parse_opts_str - parse Smack specific mount options
  531. * @options: mount options string
  532. * @opts: where to store converted mount opts
  533. *
  534. * Returns 0 on success or -ENOMEM on error.
  535. *
  536. * converts Smack specific mount options to generic security option format
  537. */
  538. static int smack_parse_opts_str(char *options,
  539. struct security_mnt_opts *opts)
  540. {
  541. char *p;
  542. char *fsdefault = NULL;
  543. char *fsfloor = NULL;
  544. char *fshat = NULL;
  545. char *fsroot = NULL;
  546. char *fstransmute = NULL;
  547. int rc = -ENOMEM;
  548. int num_mnt_opts = 0;
  549. int token;
  550. opts->num_mnt_opts = 0;
  551. if (!options)
  552. return 0;
  553. while ((p = strsep(&options, ",")) != NULL) {
  554. substring_t args[MAX_OPT_ARGS];
  555. if (!*p)
  556. continue;
  557. token = match_token(p, smk_mount_tokens, args);
  558. switch (token) {
  559. case Opt_fsdefault:
  560. if (fsdefault)
  561. goto out_opt_err;
  562. fsdefault = match_strdup(&args[0]);
  563. if (!fsdefault)
  564. goto out_err;
  565. break;
  566. case Opt_fsfloor:
  567. if (fsfloor)
  568. goto out_opt_err;
  569. fsfloor = match_strdup(&args[0]);
  570. if (!fsfloor)
  571. goto out_err;
  572. break;
  573. case Opt_fshat:
  574. if (fshat)
  575. goto out_opt_err;
  576. fshat = match_strdup(&args[0]);
  577. if (!fshat)
  578. goto out_err;
  579. break;
  580. case Opt_fsroot:
  581. if (fsroot)
  582. goto out_opt_err;
  583. fsroot = match_strdup(&args[0]);
  584. if (!fsroot)
  585. goto out_err;
  586. break;
  587. case Opt_fstransmute:
  588. if (fstransmute)
  589. goto out_opt_err;
  590. fstransmute = match_strdup(&args[0]);
  591. if (!fstransmute)
  592. goto out_err;
  593. break;
  594. default:
  595. rc = -EINVAL;
  596. pr_warn("Smack: unknown mount option\n");
  597. goto out_err;
  598. }
  599. }
  600. opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
  601. if (!opts->mnt_opts)
  602. goto out_err;
  603. opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
  604. GFP_ATOMIC);
  605. if (!opts->mnt_opts_flags) {
  606. kfree(opts->mnt_opts);
  607. goto out_err;
  608. }
  609. if (fsdefault) {
  610. opts->mnt_opts[num_mnt_opts] = fsdefault;
  611. opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
  612. }
  613. if (fsfloor) {
  614. opts->mnt_opts[num_mnt_opts] = fsfloor;
  615. opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
  616. }
  617. if (fshat) {
  618. opts->mnt_opts[num_mnt_opts] = fshat;
  619. opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
  620. }
  621. if (fsroot) {
  622. opts->mnt_opts[num_mnt_opts] = fsroot;
  623. opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
  624. }
  625. if (fstransmute) {
  626. opts->mnt_opts[num_mnt_opts] = fstransmute;
  627. opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
  628. }
  629. opts->num_mnt_opts = num_mnt_opts;
  630. return 0;
  631. out_opt_err:
  632. rc = -EINVAL;
  633. pr_warn("Smack: duplicate mount options\n");
  634. out_err:
  635. kfree(fsdefault);
  636. kfree(fsfloor);
  637. kfree(fshat);
  638. kfree(fsroot);
  639. kfree(fstransmute);
  640. return rc;
  641. }
  642. /**
  643. * smack_set_mnt_opts - set Smack specific mount options
  644. * @sb: the file system superblock
  645. * @opts: Smack mount options
  646. * @kern_flags: mount option from kernel space or user space
  647. * @set_kern_flags: where to store converted mount opts
  648. *
  649. * Returns 0 on success, an error code on failure
  650. *
  651. * Allow filesystems with binary mount data to explicitly set Smack mount
  652. * labels.
  653. */
  654. static int smack_set_mnt_opts(struct super_block *sb,
  655. struct security_mnt_opts *opts,
  656. unsigned long kern_flags,
  657. unsigned long *set_kern_flags)
  658. {
  659. struct dentry *root = sb->s_root;
  660. struct inode *inode = d_backing_inode(root);
  661. struct superblock_smack *sp = sb->s_security;
  662. struct inode_smack *isp;
  663. struct smack_known *skp;
  664. int i;
  665. int num_opts = opts->num_mnt_opts;
  666. int transmute = 0;
  667. if (sp->smk_flags & SMK_SB_INITIALIZED)
  668. return 0;
  669. sp->smk_flags |= SMK_SB_INITIALIZED;
  670. for (i = 0; i < num_opts; i++) {
  671. switch (opts->mnt_opts_flags[i]) {
  672. case FSDEFAULT_MNT:
  673. skp = smk_import_entry(opts->mnt_opts[i], 0);
  674. if (IS_ERR(skp))
  675. return PTR_ERR(skp);
  676. sp->smk_default = skp;
  677. break;
  678. case FSFLOOR_MNT:
  679. skp = smk_import_entry(opts->mnt_opts[i], 0);
  680. if (IS_ERR(skp))
  681. return PTR_ERR(skp);
  682. sp->smk_floor = skp;
  683. break;
  684. case FSHAT_MNT:
  685. skp = smk_import_entry(opts->mnt_opts[i], 0);
  686. if (IS_ERR(skp))
  687. return PTR_ERR(skp);
  688. sp->smk_hat = skp;
  689. break;
  690. case FSROOT_MNT:
  691. skp = smk_import_entry(opts->mnt_opts[i], 0);
  692. if (IS_ERR(skp))
  693. return PTR_ERR(skp);
  694. sp->smk_root = skp;
  695. break;
  696. case FSTRANS_MNT:
  697. skp = smk_import_entry(opts->mnt_opts[i], 0);
  698. if (IS_ERR(skp))
  699. return PTR_ERR(skp);
  700. sp->smk_root = skp;
  701. transmute = 1;
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. if (!smack_privileged(CAP_MAC_ADMIN)) {
  708. /*
  709. * Unprivileged mounts don't get to specify Smack values.
  710. */
  711. if (num_opts)
  712. return -EPERM;
  713. /*
  714. * Unprivileged mounts get root and default from the caller.
  715. */
  716. skp = smk_of_current();
  717. sp->smk_root = skp;
  718. sp->smk_default = skp;
  719. /*
  720. * For a handful of fs types with no user-controlled
  721. * backing store it's okay to trust security labels
  722. * in the filesystem. The rest are untrusted.
  723. */
  724. if (sb->s_user_ns != &init_user_ns &&
  725. sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
  726. sb->s_magic != RAMFS_MAGIC) {
  727. transmute = 1;
  728. sp->smk_flags |= SMK_SB_UNTRUSTED;
  729. }
  730. }
  731. /*
  732. * Initialize the root inode.
  733. */
  734. isp = inode->i_security;
  735. if (isp == NULL) {
  736. isp = new_inode_smack(sp->smk_root);
  737. if (isp == NULL)
  738. return -ENOMEM;
  739. inode->i_security = isp;
  740. } else
  741. isp->smk_inode = sp->smk_root;
  742. if (transmute)
  743. isp->smk_flags |= SMK_INODE_TRANSMUTE;
  744. return 0;
  745. }
  746. /**
  747. * smack_sb_kern_mount - Smack specific mount processing
  748. * @sb: the file system superblock
  749. * @flags: the mount flags
  750. * @data: the smack mount options
  751. *
  752. * Returns 0 on success, an error code on failure
  753. */
  754. static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
  755. {
  756. int rc = 0;
  757. char *options = data;
  758. struct security_mnt_opts opts;
  759. security_init_mnt_opts(&opts);
  760. if (!options)
  761. goto out;
  762. rc = smack_parse_opts_str(options, &opts);
  763. if (rc)
  764. goto out_err;
  765. out:
  766. rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
  767. out_err:
  768. security_free_mnt_opts(&opts);
  769. return rc;
  770. }
  771. /**
  772. * smack_sb_statfs - Smack check on statfs
  773. * @dentry: identifies the file system in question
  774. *
  775. * Returns 0 if current can read the floor of the filesystem,
  776. * and error code otherwise
  777. */
  778. static int smack_sb_statfs(struct dentry *dentry)
  779. {
  780. struct superblock_smack *sbp = dentry->d_sb->s_security;
  781. int rc;
  782. struct smk_audit_info ad;
  783. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  784. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  785. rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
  786. rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
  787. return rc;
  788. }
  789. /*
  790. * BPRM hooks
  791. */
  792. /**
  793. * smack_bprm_set_creds - set creds for exec
  794. * @bprm: the exec information
  795. *
  796. * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
  797. */
  798. static int smack_bprm_set_creds(struct linux_binprm *bprm)
  799. {
  800. struct inode *inode = file_inode(bprm->file);
  801. struct task_smack *bsp = bprm->cred->security;
  802. struct inode_smack *isp;
  803. struct superblock_smack *sbsp;
  804. int rc;
  805. if (bprm->cred_prepared)
  806. return 0;
  807. isp = inode->i_security;
  808. if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
  809. return 0;
  810. sbsp = inode->i_sb->s_security;
  811. if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
  812. isp->smk_task != sbsp->smk_root)
  813. return 0;
  814. if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
  815. struct task_struct *tracer;
  816. rc = 0;
  817. rcu_read_lock();
  818. tracer = ptrace_parent(current);
  819. if (likely(tracer != NULL))
  820. rc = smk_ptrace_rule_check(tracer,
  821. isp->smk_task,
  822. PTRACE_MODE_ATTACH,
  823. __func__);
  824. rcu_read_unlock();
  825. if (rc != 0)
  826. return rc;
  827. } else if (bprm->unsafe)
  828. return -EPERM;
  829. bsp->smk_task = isp->smk_task;
  830. bprm->per_clear |= PER_CLEAR_ON_SETID;
  831. return 0;
  832. }
  833. /**
  834. * smack_bprm_committing_creds - Prepare to install the new credentials
  835. * from bprm.
  836. *
  837. * @bprm: binprm for exec
  838. */
  839. static void smack_bprm_committing_creds(struct linux_binprm *bprm)
  840. {
  841. struct task_smack *bsp = bprm->cred->security;
  842. if (bsp->smk_task != bsp->smk_forked)
  843. current->pdeath_signal = 0;
  844. }
  845. /**
  846. * smack_bprm_secureexec - Return the decision to use secureexec.
  847. * @bprm: binprm for exec
  848. *
  849. * Returns 0 on success.
  850. */
  851. static int smack_bprm_secureexec(struct linux_binprm *bprm)
  852. {
  853. struct task_smack *tsp = current_security();
  854. if (tsp->smk_task != tsp->smk_forked)
  855. return 1;
  856. return 0;
  857. }
  858. /*
  859. * Inode hooks
  860. */
  861. /**
  862. * smack_inode_alloc_security - allocate an inode blob
  863. * @inode: the inode in need of a blob
  864. *
  865. * Returns 0 if it gets a blob, -ENOMEM otherwise
  866. */
  867. static int smack_inode_alloc_security(struct inode *inode)
  868. {
  869. struct smack_known *skp = smk_of_current();
  870. inode->i_security = new_inode_smack(skp);
  871. if (inode->i_security == NULL)
  872. return -ENOMEM;
  873. return 0;
  874. }
  875. /**
  876. * smack_inode_free_security - free an inode blob
  877. * @inode: the inode with a blob
  878. *
  879. * Clears the blob pointer in inode
  880. */
  881. static void smack_inode_free_security(struct inode *inode)
  882. {
  883. kmem_cache_free(smack_inode_cache, inode->i_security);
  884. inode->i_security = NULL;
  885. }
  886. /**
  887. * smack_inode_init_security - copy out the smack from an inode
  888. * @inode: the newly created inode
  889. * @dir: containing directory object
  890. * @qstr: unused
  891. * @name: where to put the attribute name
  892. * @value: where to put the attribute value
  893. * @len: where to put the length of the attribute
  894. *
  895. * Returns 0 if it all works out, -ENOMEM if there's no memory
  896. */
  897. static int smack_inode_init_security(struct inode *inode, struct inode *dir,
  898. const struct qstr *qstr, const char **name,
  899. void **value, size_t *len)
  900. {
  901. struct inode_smack *issp = inode->i_security;
  902. struct smack_known *skp = smk_of_current();
  903. struct smack_known *isp = smk_of_inode(inode);
  904. struct smack_known *dsp = smk_of_inode(dir);
  905. int may;
  906. if (name)
  907. *name = XATTR_SMACK_SUFFIX;
  908. if (value && len) {
  909. rcu_read_lock();
  910. may = smk_access_entry(skp->smk_known, dsp->smk_known,
  911. &skp->smk_rules);
  912. rcu_read_unlock();
  913. /*
  914. * If the access rule allows transmutation and
  915. * the directory requests transmutation then
  916. * by all means transmute.
  917. * Mark the inode as changed.
  918. */
  919. if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
  920. smk_inode_transmutable(dir)) {
  921. isp = dsp;
  922. issp->smk_flags |= SMK_INODE_CHANGED;
  923. }
  924. *value = kstrdup(isp->smk_known, GFP_NOFS);
  925. if (*value == NULL)
  926. return -ENOMEM;
  927. *len = strlen(isp->smk_known);
  928. }
  929. return 0;
  930. }
  931. /**
  932. * smack_inode_link - Smack check on link
  933. * @old_dentry: the existing object
  934. * @dir: unused
  935. * @new_dentry: the new object
  936. *
  937. * Returns 0 if access is permitted, an error code otherwise
  938. */
  939. static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
  940. struct dentry *new_dentry)
  941. {
  942. struct smack_known *isp;
  943. struct smk_audit_info ad;
  944. int rc;
  945. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  946. smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
  947. isp = smk_of_inode(d_backing_inode(old_dentry));
  948. rc = smk_curacc(isp, MAY_WRITE, &ad);
  949. rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
  950. if (rc == 0 && d_is_positive(new_dentry)) {
  951. isp = smk_of_inode(d_backing_inode(new_dentry));
  952. smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
  953. rc = smk_curacc(isp, MAY_WRITE, &ad);
  954. rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
  955. }
  956. return rc;
  957. }
  958. /**
  959. * smack_inode_unlink - Smack check on inode deletion
  960. * @dir: containing directory object
  961. * @dentry: file to unlink
  962. *
  963. * Returns 0 if current can write the containing directory
  964. * and the object, error code otherwise
  965. */
  966. static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
  967. {
  968. struct inode *ip = d_backing_inode(dentry);
  969. struct smk_audit_info ad;
  970. int rc;
  971. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  972. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  973. /*
  974. * You need write access to the thing you're unlinking
  975. */
  976. rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
  977. rc = smk_bu_inode(ip, MAY_WRITE, rc);
  978. if (rc == 0) {
  979. /*
  980. * You also need write access to the containing directory
  981. */
  982. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  983. smk_ad_setfield_u_fs_inode(&ad, dir);
  984. rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
  985. rc = smk_bu_inode(dir, MAY_WRITE, rc);
  986. }
  987. return rc;
  988. }
  989. /**
  990. * smack_inode_rmdir - Smack check on directory deletion
  991. * @dir: containing directory object
  992. * @dentry: directory to unlink
  993. *
  994. * Returns 0 if current can write the containing directory
  995. * and the directory, error code otherwise
  996. */
  997. static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
  998. {
  999. struct smk_audit_info ad;
  1000. int rc;
  1001. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1002. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  1003. /*
  1004. * You need write access to the thing you're removing
  1005. */
  1006. rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
  1007. rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
  1008. if (rc == 0) {
  1009. /*
  1010. * You also need write access to the containing directory
  1011. */
  1012. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  1013. smk_ad_setfield_u_fs_inode(&ad, dir);
  1014. rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
  1015. rc = smk_bu_inode(dir, MAY_WRITE, rc);
  1016. }
  1017. return rc;
  1018. }
  1019. /**
  1020. * smack_inode_rename - Smack check on rename
  1021. * @old_inode: unused
  1022. * @old_dentry: the old object
  1023. * @new_inode: unused
  1024. * @new_dentry: the new object
  1025. *
  1026. * Read and write access is required on both the old and
  1027. * new directories.
  1028. *
  1029. * Returns 0 if access is permitted, an error code otherwise
  1030. */
  1031. static int smack_inode_rename(struct inode *old_inode,
  1032. struct dentry *old_dentry,
  1033. struct inode *new_inode,
  1034. struct dentry *new_dentry)
  1035. {
  1036. int rc;
  1037. struct smack_known *isp;
  1038. struct smk_audit_info ad;
  1039. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1040. smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
  1041. isp = smk_of_inode(d_backing_inode(old_dentry));
  1042. rc = smk_curacc(isp, MAY_READWRITE, &ad);
  1043. rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
  1044. if (rc == 0 && d_is_positive(new_dentry)) {
  1045. isp = smk_of_inode(d_backing_inode(new_dentry));
  1046. smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
  1047. rc = smk_curacc(isp, MAY_READWRITE, &ad);
  1048. rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
  1049. }
  1050. return rc;
  1051. }
  1052. /**
  1053. * smack_inode_permission - Smack version of permission()
  1054. * @inode: the inode in question
  1055. * @mask: the access requested
  1056. *
  1057. * This is the important Smack hook.
  1058. *
  1059. * Returns 0 if access is permitted, -EACCES otherwise
  1060. */
  1061. static int smack_inode_permission(struct inode *inode, int mask)
  1062. {
  1063. struct superblock_smack *sbsp = inode->i_sb->s_security;
  1064. struct smk_audit_info ad;
  1065. int no_block = mask & MAY_NOT_BLOCK;
  1066. int rc;
  1067. mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
  1068. /*
  1069. * No permission to check. Existence test. Yup, it's there.
  1070. */
  1071. if (mask == 0)
  1072. return 0;
  1073. if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
  1074. if (smk_of_inode(inode) != sbsp->smk_root)
  1075. return -EACCES;
  1076. }
  1077. /* May be droppable after audit */
  1078. if (no_block)
  1079. return -ECHILD;
  1080. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  1081. smk_ad_setfield_u_fs_inode(&ad, inode);
  1082. rc = smk_curacc(smk_of_inode(inode), mask, &ad);
  1083. rc = smk_bu_inode(inode, mask, rc);
  1084. return rc;
  1085. }
  1086. /**
  1087. * smack_inode_setattr - Smack check for setting attributes
  1088. * @dentry: the object
  1089. * @iattr: for the force flag
  1090. *
  1091. * Returns 0 if access is permitted, an error code otherwise
  1092. */
  1093. static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
  1094. {
  1095. struct smk_audit_info ad;
  1096. int rc;
  1097. /*
  1098. * Need to allow for clearing the setuid bit.
  1099. */
  1100. if (iattr->ia_valid & ATTR_FORCE)
  1101. return 0;
  1102. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1103. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  1104. rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
  1105. rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
  1106. return rc;
  1107. }
  1108. /**
  1109. * smack_inode_getattr - Smack check for getting attributes
  1110. * @mnt: vfsmount of the object
  1111. * @dentry: the object
  1112. *
  1113. * Returns 0 if access is permitted, an error code otherwise
  1114. */
  1115. static int smack_inode_getattr(const struct path *path)
  1116. {
  1117. struct smk_audit_info ad;
  1118. struct inode *inode = d_backing_inode(path->dentry);
  1119. int rc;
  1120. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1121. smk_ad_setfield_u_fs_path(&ad, *path);
  1122. rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
  1123. rc = smk_bu_inode(inode, MAY_READ, rc);
  1124. return rc;
  1125. }
  1126. /**
  1127. * smack_inode_setxattr - Smack check for setting xattrs
  1128. * @dentry: the object
  1129. * @name: name of the attribute
  1130. * @value: value of the attribute
  1131. * @size: size of the value
  1132. * @flags: unused
  1133. *
  1134. * This protects the Smack attribute explicitly.
  1135. *
  1136. * Returns 0 if access is permitted, an error code otherwise
  1137. */
  1138. static int smack_inode_setxattr(struct dentry *dentry, const char *name,
  1139. const void *value, size_t size, int flags)
  1140. {
  1141. struct smk_audit_info ad;
  1142. struct smack_known *skp;
  1143. int check_priv = 0;
  1144. int check_import = 0;
  1145. int check_star = 0;
  1146. int rc = 0;
  1147. /*
  1148. * Check label validity here so import won't fail in post_setxattr
  1149. */
  1150. if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
  1151. strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
  1152. strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
  1153. check_priv = 1;
  1154. check_import = 1;
  1155. } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
  1156. strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
  1157. check_priv = 1;
  1158. check_import = 1;
  1159. check_star = 1;
  1160. } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
  1161. check_priv = 1;
  1162. if (size != TRANS_TRUE_SIZE ||
  1163. strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
  1164. rc = -EINVAL;
  1165. } else
  1166. rc = cap_inode_setxattr(dentry, name, value, size, flags);
  1167. if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
  1168. rc = -EPERM;
  1169. if (rc == 0 && check_import) {
  1170. skp = size ? smk_import_entry(value, size) : NULL;
  1171. if (IS_ERR(skp))
  1172. rc = PTR_ERR(skp);
  1173. else if (skp == NULL || (check_star &&
  1174. (skp == &smack_known_star || skp == &smack_known_web)))
  1175. rc = -EINVAL;
  1176. }
  1177. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1178. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  1179. if (rc == 0) {
  1180. rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
  1181. rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
  1182. }
  1183. return rc;
  1184. }
  1185. /**
  1186. * smack_inode_post_setxattr - Apply the Smack update approved above
  1187. * @dentry: object
  1188. * @name: attribute name
  1189. * @value: attribute value
  1190. * @size: attribute size
  1191. * @flags: unused
  1192. *
  1193. * Set the pointer in the inode blob to the entry found
  1194. * in the master label list.
  1195. */
  1196. static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
  1197. const void *value, size_t size, int flags)
  1198. {
  1199. struct smack_known *skp;
  1200. struct inode_smack *isp = d_backing_inode(dentry)->i_security;
  1201. if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
  1202. isp->smk_flags |= SMK_INODE_TRANSMUTE;
  1203. return;
  1204. }
  1205. if (strcmp(name, XATTR_NAME_SMACK) == 0) {
  1206. skp = smk_import_entry(value, size);
  1207. if (!IS_ERR(skp))
  1208. isp->smk_inode = skp;
  1209. else
  1210. isp->smk_inode = &smack_known_invalid;
  1211. } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
  1212. skp = smk_import_entry(value, size);
  1213. if (!IS_ERR(skp))
  1214. isp->smk_task = skp;
  1215. else
  1216. isp->smk_task = &smack_known_invalid;
  1217. } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
  1218. skp = smk_import_entry(value, size);
  1219. if (!IS_ERR(skp))
  1220. isp->smk_mmap = skp;
  1221. else
  1222. isp->smk_mmap = &smack_known_invalid;
  1223. }
  1224. return;
  1225. }
  1226. /**
  1227. * smack_inode_getxattr - Smack check on getxattr
  1228. * @dentry: the object
  1229. * @name: unused
  1230. *
  1231. * Returns 0 if access is permitted, an error code otherwise
  1232. */
  1233. static int smack_inode_getxattr(struct dentry *dentry, const char *name)
  1234. {
  1235. struct smk_audit_info ad;
  1236. int rc;
  1237. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1238. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  1239. rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
  1240. rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
  1241. return rc;
  1242. }
  1243. /**
  1244. * smack_inode_removexattr - Smack check on removexattr
  1245. * @dentry: the object
  1246. * @name: name of the attribute
  1247. *
  1248. * Removing the Smack attribute requires CAP_MAC_ADMIN
  1249. *
  1250. * Returns 0 if access is permitted, an error code otherwise
  1251. */
  1252. static int smack_inode_removexattr(struct dentry *dentry, const char *name)
  1253. {
  1254. struct inode_smack *isp;
  1255. struct smk_audit_info ad;
  1256. int rc = 0;
  1257. if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
  1258. strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
  1259. strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
  1260. strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
  1261. strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
  1262. strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
  1263. if (!smack_privileged(CAP_MAC_ADMIN))
  1264. rc = -EPERM;
  1265. } else
  1266. rc = cap_inode_removexattr(dentry, name);
  1267. if (rc != 0)
  1268. return rc;
  1269. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  1270. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  1271. rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
  1272. rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
  1273. if (rc != 0)
  1274. return rc;
  1275. isp = d_backing_inode(dentry)->i_security;
  1276. /*
  1277. * Don't do anything special for these.
  1278. * XATTR_NAME_SMACKIPIN
  1279. * XATTR_NAME_SMACKIPOUT
  1280. */
  1281. if (strcmp(name, XATTR_NAME_SMACK) == 0) {
  1282. struct super_block *sbp = dentry->d_sb;
  1283. struct superblock_smack *sbsp = sbp->s_security;
  1284. isp->smk_inode = sbsp->smk_default;
  1285. } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
  1286. isp->smk_task = NULL;
  1287. else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
  1288. isp->smk_mmap = NULL;
  1289. else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
  1290. isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
  1291. return 0;
  1292. }
  1293. /**
  1294. * smack_inode_getsecurity - get smack xattrs
  1295. * @inode: the object
  1296. * @name: attribute name
  1297. * @buffer: where to put the result
  1298. * @alloc: duplicate memory
  1299. *
  1300. * Returns the size of the attribute or an error code
  1301. */
  1302. static int smack_inode_getsecurity(struct inode *inode,
  1303. const char *name, void **buffer,
  1304. bool alloc)
  1305. {
  1306. struct socket_smack *ssp;
  1307. struct socket *sock;
  1308. struct super_block *sbp;
  1309. struct inode *ip = (struct inode *)inode;
  1310. struct smack_known *isp;
  1311. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
  1312. isp = smk_of_inode(inode);
  1313. else {
  1314. /*
  1315. * The rest of the Smack xattrs are only on sockets.
  1316. */
  1317. sbp = ip->i_sb;
  1318. if (sbp->s_magic != SOCKFS_MAGIC)
  1319. return -EOPNOTSUPP;
  1320. sock = SOCKET_I(ip);
  1321. if (sock == NULL || sock->sk == NULL)
  1322. return -EOPNOTSUPP;
  1323. ssp = sock->sk->sk_security;
  1324. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  1325. isp = ssp->smk_in;
  1326. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
  1327. isp = ssp->smk_out;
  1328. else
  1329. return -EOPNOTSUPP;
  1330. }
  1331. if (alloc) {
  1332. *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
  1333. if (*buffer == NULL)
  1334. return -ENOMEM;
  1335. }
  1336. return strlen(isp->smk_known);
  1337. }
  1338. /**
  1339. * smack_inode_listsecurity - list the Smack attributes
  1340. * @inode: the object
  1341. * @buffer: where they go
  1342. * @buffer_size: size of buffer
  1343. */
  1344. static int smack_inode_listsecurity(struct inode *inode, char *buffer,
  1345. size_t buffer_size)
  1346. {
  1347. int len = sizeof(XATTR_NAME_SMACK);
  1348. if (buffer != NULL && len <= buffer_size)
  1349. memcpy(buffer, XATTR_NAME_SMACK, len);
  1350. return len;
  1351. }
  1352. /**
  1353. * smack_inode_getsecid - Extract inode's security id
  1354. * @inode: inode to extract the info from
  1355. * @secid: where result will be saved
  1356. */
  1357. static void smack_inode_getsecid(struct inode *inode, u32 *secid)
  1358. {
  1359. struct inode_smack *isp = inode->i_security;
  1360. *secid = isp->smk_inode->smk_secid;
  1361. }
  1362. /*
  1363. * File Hooks
  1364. */
  1365. /*
  1366. * There is no smack_file_permission hook
  1367. *
  1368. * Should access checks be done on each read or write?
  1369. * UNICOS and SELinux say yes.
  1370. * Trusted Solaris, Trusted Irix, and just about everyone else says no.
  1371. *
  1372. * I'll say no for now. Smack does not do the frequent
  1373. * label changing that SELinux does.
  1374. */
  1375. /**
  1376. * smack_file_alloc_security - assign a file security blob
  1377. * @file: the object
  1378. *
  1379. * The security blob for a file is a pointer to the master
  1380. * label list, so no allocation is done.
  1381. *
  1382. * f_security is the owner security information. It
  1383. * isn't used on file access checks, it's for send_sigio.
  1384. *
  1385. * Returns 0
  1386. */
  1387. static int smack_file_alloc_security(struct file *file)
  1388. {
  1389. struct smack_known *skp = smk_of_current();
  1390. file->f_security = skp;
  1391. return 0;
  1392. }
  1393. /**
  1394. * smack_file_free_security - clear a file security blob
  1395. * @file: the object
  1396. *
  1397. * The security blob for a file is a pointer to the master
  1398. * label list, so no memory is freed.
  1399. */
  1400. static void smack_file_free_security(struct file *file)
  1401. {
  1402. file->f_security = NULL;
  1403. }
  1404. /**
  1405. * smack_file_ioctl - Smack check on ioctls
  1406. * @file: the object
  1407. * @cmd: what to do
  1408. * @arg: unused
  1409. *
  1410. * Relies heavily on the correct use of the ioctl command conventions.
  1411. *
  1412. * Returns 0 if allowed, error code otherwise
  1413. */
  1414. static int smack_file_ioctl(struct file *file, unsigned int cmd,
  1415. unsigned long arg)
  1416. {
  1417. int rc = 0;
  1418. struct smk_audit_info ad;
  1419. struct inode *inode = file_inode(file);
  1420. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1421. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1422. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1423. rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
  1424. rc = smk_bu_file(file, MAY_WRITE, rc);
  1425. }
  1426. if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
  1427. rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
  1428. rc = smk_bu_file(file, MAY_READ, rc);
  1429. }
  1430. return rc;
  1431. }
  1432. /**
  1433. * smack_file_lock - Smack check on file locking
  1434. * @file: the object
  1435. * @cmd: unused
  1436. *
  1437. * Returns 0 if current has lock access, error code otherwise
  1438. */
  1439. static int smack_file_lock(struct file *file, unsigned int cmd)
  1440. {
  1441. struct smk_audit_info ad;
  1442. int rc;
  1443. struct inode *inode = file_inode(file);
  1444. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1445. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1446. rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
  1447. rc = smk_bu_file(file, MAY_LOCK, rc);
  1448. return rc;
  1449. }
  1450. /**
  1451. * smack_file_fcntl - Smack check on fcntl
  1452. * @file: the object
  1453. * @cmd: what action to check
  1454. * @arg: unused
  1455. *
  1456. * Generally these operations are harmless.
  1457. * File locking operations present an obvious mechanism
  1458. * for passing information, so they require write access.
  1459. *
  1460. * Returns 0 if current has access, error code otherwise
  1461. */
  1462. static int smack_file_fcntl(struct file *file, unsigned int cmd,
  1463. unsigned long arg)
  1464. {
  1465. struct smk_audit_info ad;
  1466. int rc = 0;
  1467. struct inode *inode = file_inode(file);
  1468. switch (cmd) {
  1469. case F_GETLK:
  1470. break;
  1471. case F_SETLK:
  1472. case F_SETLKW:
  1473. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1474. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1475. rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
  1476. rc = smk_bu_file(file, MAY_LOCK, rc);
  1477. break;
  1478. case F_SETOWN:
  1479. case F_SETSIG:
  1480. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1481. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1482. rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
  1483. rc = smk_bu_file(file, MAY_WRITE, rc);
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. return rc;
  1489. }
  1490. /**
  1491. * smack_mmap_file :
  1492. * Check permissions for a mmap operation. The @file may be NULL, e.g.
  1493. * if mapping anonymous memory.
  1494. * @file contains the file structure for file to map (may be NULL).
  1495. * @reqprot contains the protection requested by the application.
  1496. * @prot contains the protection that will be applied by the kernel.
  1497. * @flags contains the operational flags.
  1498. * Return 0 if permission is granted.
  1499. */
  1500. static int smack_mmap_file(struct file *file,
  1501. unsigned long reqprot, unsigned long prot,
  1502. unsigned long flags)
  1503. {
  1504. struct smack_known *skp;
  1505. struct smack_known *mkp;
  1506. struct smack_rule *srp;
  1507. struct task_smack *tsp;
  1508. struct smack_known *okp;
  1509. struct inode_smack *isp;
  1510. struct superblock_smack *sbsp;
  1511. int may;
  1512. int mmay;
  1513. int tmay;
  1514. int rc;
  1515. if (file == NULL)
  1516. return 0;
  1517. isp = file_inode(file)->i_security;
  1518. if (isp->smk_mmap == NULL)
  1519. return 0;
  1520. sbsp = file_inode(file)->i_sb->s_security;
  1521. if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
  1522. isp->smk_mmap != sbsp->smk_root)
  1523. return -EACCES;
  1524. mkp = isp->smk_mmap;
  1525. tsp = current_security();
  1526. skp = smk_of_current();
  1527. rc = 0;
  1528. rcu_read_lock();
  1529. /*
  1530. * For each Smack rule associated with the subject
  1531. * label verify that the SMACK64MMAP also has access
  1532. * to that rule's object label.
  1533. */
  1534. list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
  1535. okp = srp->smk_object;
  1536. /*
  1537. * Matching labels always allows access.
  1538. */
  1539. if (mkp->smk_known == okp->smk_known)
  1540. continue;
  1541. /*
  1542. * If there is a matching local rule take
  1543. * that into account as well.
  1544. */
  1545. may = smk_access_entry(srp->smk_subject->smk_known,
  1546. okp->smk_known,
  1547. &tsp->smk_rules);
  1548. if (may == -ENOENT)
  1549. may = srp->smk_access;
  1550. else
  1551. may &= srp->smk_access;
  1552. /*
  1553. * If may is zero the SMACK64MMAP subject can't
  1554. * possibly have less access.
  1555. */
  1556. if (may == 0)
  1557. continue;
  1558. /*
  1559. * Fetch the global list entry.
  1560. * If there isn't one a SMACK64MMAP subject
  1561. * can't have as much access as current.
  1562. */
  1563. mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
  1564. &mkp->smk_rules);
  1565. if (mmay == -ENOENT) {
  1566. rc = -EACCES;
  1567. break;
  1568. }
  1569. /*
  1570. * If there is a local entry it modifies the
  1571. * potential access, too.
  1572. */
  1573. tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
  1574. &tsp->smk_rules);
  1575. if (tmay != -ENOENT)
  1576. mmay &= tmay;
  1577. /*
  1578. * If there is any access available to current that is
  1579. * not available to a SMACK64MMAP subject
  1580. * deny access.
  1581. */
  1582. if ((may | mmay) != mmay) {
  1583. rc = -EACCES;
  1584. break;
  1585. }
  1586. }
  1587. rcu_read_unlock();
  1588. return rc;
  1589. }
  1590. /**
  1591. * smack_file_set_fowner - set the file security blob value
  1592. * @file: object in question
  1593. *
  1594. */
  1595. static void smack_file_set_fowner(struct file *file)
  1596. {
  1597. file->f_security = smk_of_current();
  1598. }
  1599. /**
  1600. * smack_file_send_sigiotask - Smack on sigio
  1601. * @tsk: The target task
  1602. * @fown: the object the signal come from
  1603. * @signum: unused
  1604. *
  1605. * Allow a privileged task to get signals even if it shouldn't
  1606. *
  1607. * Returns 0 if a subject with the object's smack could
  1608. * write to the task, an error code otherwise.
  1609. */
  1610. static int smack_file_send_sigiotask(struct task_struct *tsk,
  1611. struct fown_struct *fown, int signum)
  1612. {
  1613. struct smack_known *skp;
  1614. struct smack_known *tkp = smk_of_task(tsk->cred->security);
  1615. struct file *file;
  1616. int rc;
  1617. struct smk_audit_info ad;
  1618. /*
  1619. * struct fown_struct is never outside the context of a struct file
  1620. */
  1621. file = container_of(fown, struct file, f_owner);
  1622. /* we don't log here as rc can be overriden */
  1623. skp = file->f_security;
  1624. rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
  1625. rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
  1626. if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
  1627. rc = 0;
  1628. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1629. smk_ad_setfield_u_tsk(&ad, tsk);
  1630. smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
  1631. return rc;
  1632. }
  1633. /**
  1634. * smack_file_receive - Smack file receive check
  1635. * @file: the object
  1636. *
  1637. * Returns 0 if current has access, error code otherwise
  1638. */
  1639. static int smack_file_receive(struct file *file)
  1640. {
  1641. int rc;
  1642. int may = 0;
  1643. struct smk_audit_info ad;
  1644. struct inode *inode = file_inode(file);
  1645. struct socket *sock;
  1646. struct task_smack *tsp;
  1647. struct socket_smack *ssp;
  1648. if (unlikely(IS_PRIVATE(inode)))
  1649. return 0;
  1650. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1651. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1652. if (S_ISSOCK(inode->i_mode)) {
  1653. sock = SOCKET_I(inode);
  1654. ssp = sock->sk->sk_security;
  1655. tsp = current_security();
  1656. /*
  1657. * If the receiving process can't write to the
  1658. * passed socket or if the passed socket can't
  1659. * write to the receiving process don't accept
  1660. * the passed socket.
  1661. */
  1662. rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
  1663. rc = smk_bu_file(file, may, rc);
  1664. if (rc < 0)
  1665. return rc;
  1666. rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
  1667. rc = smk_bu_file(file, may, rc);
  1668. return rc;
  1669. }
  1670. /*
  1671. * This code relies on bitmasks.
  1672. */
  1673. if (file->f_mode & FMODE_READ)
  1674. may = MAY_READ;
  1675. if (file->f_mode & FMODE_WRITE)
  1676. may |= MAY_WRITE;
  1677. rc = smk_curacc(smk_of_inode(inode), may, &ad);
  1678. rc = smk_bu_file(file, may, rc);
  1679. return rc;
  1680. }
  1681. /**
  1682. * smack_file_open - Smack dentry open processing
  1683. * @file: the object
  1684. * @cred: task credential
  1685. *
  1686. * Set the security blob in the file structure.
  1687. * Allow the open only if the task has read access. There are
  1688. * many read operations (e.g. fstat) that you can do with an
  1689. * fd even if you have the file open write-only.
  1690. *
  1691. * Returns 0
  1692. */
  1693. static int smack_file_open(struct file *file, const struct cred *cred)
  1694. {
  1695. struct task_smack *tsp = cred->security;
  1696. struct inode *inode = file_inode(file);
  1697. struct smk_audit_info ad;
  1698. int rc;
  1699. if (smack_privileged(CAP_MAC_OVERRIDE))
  1700. return 0;
  1701. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1702. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1703. rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
  1704. rc = smk_bu_credfile(cred, file, MAY_READ, rc);
  1705. return rc;
  1706. }
  1707. /*
  1708. * Task hooks
  1709. */
  1710. /**
  1711. * smack_cred_alloc_blank - "allocate" blank task-level security credentials
  1712. * @new: the new credentials
  1713. * @gfp: the atomicity of any memory allocations
  1714. *
  1715. * Prepare a blank set of credentials for modification. This must allocate all
  1716. * the memory the LSM module might require such that cred_transfer() can
  1717. * complete without error.
  1718. */
  1719. static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  1720. {
  1721. struct task_smack *tsp;
  1722. tsp = new_task_smack(NULL, NULL, gfp);
  1723. if (tsp == NULL)
  1724. return -ENOMEM;
  1725. cred->security = tsp;
  1726. return 0;
  1727. }
  1728. /**
  1729. * smack_cred_free - "free" task-level security credentials
  1730. * @cred: the credentials in question
  1731. *
  1732. */
  1733. static void smack_cred_free(struct cred *cred)
  1734. {
  1735. struct task_smack *tsp = cred->security;
  1736. struct smack_rule *rp;
  1737. struct list_head *l;
  1738. struct list_head *n;
  1739. if (tsp == NULL)
  1740. return;
  1741. cred->security = NULL;
  1742. smk_destroy_label_list(&tsp->smk_relabel);
  1743. list_for_each_safe(l, n, &tsp->smk_rules) {
  1744. rp = list_entry(l, struct smack_rule, list);
  1745. list_del(&rp->list);
  1746. kfree(rp);
  1747. }
  1748. kfree(tsp);
  1749. }
  1750. /**
  1751. * smack_cred_prepare - prepare new set of credentials for modification
  1752. * @new: the new credentials
  1753. * @old: the original credentials
  1754. * @gfp: the atomicity of any memory allocations
  1755. *
  1756. * Prepare a new set of credentials for modification.
  1757. */
  1758. static int smack_cred_prepare(struct cred *new, const struct cred *old,
  1759. gfp_t gfp)
  1760. {
  1761. struct task_smack *old_tsp = old->security;
  1762. struct task_smack *new_tsp;
  1763. int rc;
  1764. new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
  1765. if (new_tsp == NULL)
  1766. return -ENOMEM;
  1767. rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
  1768. if (rc != 0)
  1769. return rc;
  1770. rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
  1771. gfp);
  1772. if (rc != 0)
  1773. return rc;
  1774. new->security = new_tsp;
  1775. return 0;
  1776. }
  1777. /**
  1778. * smack_cred_transfer - Transfer the old credentials to the new credentials
  1779. * @new: the new credentials
  1780. * @old: the original credentials
  1781. *
  1782. * Fill in a set of blank credentials from another set of credentials.
  1783. */
  1784. static void smack_cred_transfer(struct cred *new, const struct cred *old)
  1785. {
  1786. struct task_smack *old_tsp = old->security;
  1787. struct task_smack *new_tsp = new->security;
  1788. new_tsp->smk_task = old_tsp->smk_task;
  1789. new_tsp->smk_forked = old_tsp->smk_task;
  1790. mutex_init(&new_tsp->smk_rules_lock);
  1791. INIT_LIST_HEAD(&new_tsp->smk_rules);
  1792. /* cbs copy rule list */
  1793. }
  1794. /**
  1795. * smack_kernel_act_as - Set the subjective context in a set of credentials
  1796. * @new: points to the set of credentials to be modified.
  1797. * @secid: specifies the security ID to be set
  1798. *
  1799. * Set the security data for a kernel service.
  1800. */
  1801. static int smack_kernel_act_as(struct cred *new, u32 secid)
  1802. {
  1803. struct task_smack *new_tsp = new->security;
  1804. struct smack_known *skp = smack_from_secid(secid);
  1805. if (skp == NULL)
  1806. return -EINVAL;
  1807. new_tsp->smk_task = skp;
  1808. return 0;
  1809. }
  1810. /**
  1811. * smack_kernel_create_files_as - Set the file creation label in a set of creds
  1812. * @new: points to the set of credentials to be modified
  1813. * @inode: points to the inode to use as a reference
  1814. *
  1815. * Set the file creation context in a set of credentials to the same
  1816. * as the objective context of the specified inode
  1817. */
  1818. static int smack_kernel_create_files_as(struct cred *new,
  1819. struct inode *inode)
  1820. {
  1821. struct inode_smack *isp = inode->i_security;
  1822. struct task_smack *tsp = new->security;
  1823. tsp->smk_forked = isp->smk_inode;
  1824. tsp->smk_task = tsp->smk_forked;
  1825. return 0;
  1826. }
  1827. /**
  1828. * smk_curacc_on_task - helper to log task related access
  1829. * @p: the task object
  1830. * @access: the access requested
  1831. * @caller: name of the calling function for audit
  1832. *
  1833. * Return 0 if access is permitted
  1834. */
  1835. static int smk_curacc_on_task(struct task_struct *p, int access,
  1836. const char *caller)
  1837. {
  1838. struct smk_audit_info ad;
  1839. struct smack_known *skp = smk_of_task_struct(p);
  1840. int rc;
  1841. smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
  1842. smk_ad_setfield_u_tsk(&ad, p);
  1843. rc = smk_curacc(skp, access, &ad);
  1844. rc = smk_bu_task(p, access, rc);
  1845. return rc;
  1846. }
  1847. /**
  1848. * smack_task_setpgid - Smack check on setting pgid
  1849. * @p: the task object
  1850. * @pgid: unused
  1851. *
  1852. * Return 0 if write access is permitted
  1853. */
  1854. static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
  1855. {
  1856. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1857. }
  1858. /**
  1859. * smack_task_getpgid - Smack access check for getpgid
  1860. * @p: the object task
  1861. *
  1862. * Returns 0 if current can read the object task, error code otherwise
  1863. */
  1864. static int smack_task_getpgid(struct task_struct *p)
  1865. {
  1866. return smk_curacc_on_task(p, MAY_READ, __func__);
  1867. }
  1868. /**
  1869. * smack_task_getsid - Smack access check for getsid
  1870. * @p: the object task
  1871. *
  1872. * Returns 0 if current can read the object task, error code otherwise
  1873. */
  1874. static int smack_task_getsid(struct task_struct *p)
  1875. {
  1876. return smk_curacc_on_task(p, MAY_READ, __func__);
  1877. }
  1878. /**
  1879. * smack_task_getsecid - get the secid of the task
  1880. * @p: the object task
  1881. * @secid: where to put the result
  1882. *
  1883. * Sets the secid to contain a u32 version of the smack label.
  1884. */
  1885. static void smack_task_getsecid(struct task_struct *p, u32 *secid)
  1886. {
  1887. struct smack_known *skp = smk_of_task_struct(p);
  1888. *secid = skp->smk_secid;
  1889. }
  1890. /**
  1891. * smack_task_setnice - Smack check on setting nice
  1892. * @p: the task object
  1893. * @nice: unused
  1894. *
  1895. * Return 0 if write access is permitted
  1896. */
  1897. static int smack_task_setnice(struct task_struct *p, int nice)
  1898. {
  1899. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1900. }
  1901. /**
  1902. * smack_task_setioprio - Smack check on setting ioprio
  1903. * @p: the task object
  1904. * @ioprio: unused
  1905. *
  1906. * Return 0 if write access is permitted
  1907. */
  1908. static int smack_task_setioprio(struct task_struct *p, int ioprio)
  1909. {
  1910. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1911. }
  1912. /**
  1913. * smack_task_getioprio - Smack check on reading ioprio
  1914. * @p: the task object
  1915. *
  1916. * Return 0 if read access is permitted
  1917. */
  1918. static int smack_task_getioprio(struct task_struct *p)
  1919. {
  1920. return smk_curacc_on_task(p, MAY_READ, __func__);
  1921. }
  1922. /**
  1923. * smack_task_setscheduler - Smack check on setting scheduler
  1924. * @p: the task object
  1925. * @policy: unused
  1926. * @lp: unused
  1927. *
  1928. * Return 0 if read access is permitted
  1929. */
  1930. static int smack_task_setscheduler(struct task_struct *p)
  1931. {
  1932. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1933. }
  1934. /**
  1935. * smack_task_getscheduler - Smack check on reading scheduler
  1936. * @p: the task object
  1937. *
  1938. * Return 0 if read access is permitted
  1939. */
  1940. static int smack_task_getscheduler(struct task_struct *p)
  1941. {
  1942. return smk_curacc_on_task(p, MAY_READ, __func__);
  1943. }
  1944. /**
  1945. * smack_task_movememory - Smack check on moving memory
  1946. * @p: the task object
  1947. *
  1948. * Return 0 if write access is permitted
  1949. */
  1950. static int smack_task_movememory(struct task_struct *p)
  1951. {
  1952. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1953. }
  1954. /**
  1955. * smack_task_kill - Smack check on signal delivery
  1956. * @p: the task object
  1957. * @info: unused
  1958. * @sig: unused
  1959. * @secid: identifies the smack to use in lieu of current's
  1960. *
  1961. * Return 0 if write access is permitted
  1962. *
  1963. * The secid behavior is an artifact of an SELinux hack
  1964. * in the USB code. Someday it may go away.
  1965. */
  1966. static int smack_task_kill(struct task_struct *p, struct siginfo *info,
  1967. int sig, u32 secid)
  1968. {
  1969. struct smk_audit_info ad;
  1970. struct smack_known *skp;
  1971. struct smack_known *tkp = smk_of_task_struct(p);
  1972. int rc;
  1973. if (!sig)
  1974. return 0; /* null signal; existence test */
  1975. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1976. smk_ad_setfield_u_tsk(&ad, p);
  1977. /*
  1978. * Sending a signal requires that the sender
  1979. * can write the receiver.
  1980. */
  1981. if (secid == 0) {
  1982. rc = smk_curacc(tkp, MAY_DELIVER, &ad);
  1983. rc = smk_bu_task(p, MAY_DELIVER, rc);
  1984. return rc;
  1985. }
  1986. /*
  1987. * If the secid isn't 0 we're dealing with some USB IO
  1988. * specific behavior. This is not clean. For one thing
  1989. * we can't take privilege into account.
  1990. */
  1991. skp = smack_from_secid(secid);
  1992. rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
  1993. rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
  1994. return rc;
  1995. }
  1996. /**
  1997. * smack_task_wait - Smack access check for waiting
  1998. * @p: task to wait for
  1999. *
  2000. * Returns 0
  2001. */
  2002. static int smack_task_wait(struct task_struct *p)
  2003. {
  2004. /*
  2005. * Allow the operation to succeed.
  2006. * Zombies are bad.
  2007. * In userless environments (e.g. phones) programs
  2008. * get marked with SMACK64EXEC and even if the parent
  2009. * and child shouldn't be talking the parent still
  2010. * may expect to know when the child exits.
  2011. */
  2012. return 0;
  2013. }
  2014. /**
  2015. * smack_task_to_inode - copy task smack into the inode blob
  2016. * @p: task to copy from
  2017. * @inode: inode to copy to
  2018. *
  2019. * Sets the smack pointer in the inode security blob
  2020. */
  2021. static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
  2022. {
  2023. struct inode_smack *isp = inode->i_security;
  2024. struct smack_known *skp = smk_of_task_struct(p);
  2025. isp->smk_inode = skp;
  2026. }
  2027. /*
  2028. * Socket hooks.
  2029. */
  2030. /**
  2031. * smack_sk_alloc_security - Allocate a socket blob
  2032. * @sk: the socket
  2033. * @family: unused
  2034. * @gfp_flags: memory allocation flags
  2035. *
  2036. * Assign Smack pointers to current
  2037. *
  2038. * Returns 0 on success, -ENOMEM is there's no memory
  2039. */
  2040. static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
  2041. {
  2042. struct smack_known *skp = smk_of_current();
  2043. struct socket_smack *ssp;
  2044. ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
  2045. if (ssp == NULL)
  2046. return -ENOMEM;
  2047. ssp->smk_in = skp;
  2048. ssp->smk_out = skp;
  2049. ssp->smk_packet = NULL;
  2050. sk->sk_security = ssp;
  2051. return 0;
  2052. }
  2053. /**
  2054. * smack_sk_free_security - Free a socket blob
  2055. * @sk: the socket
  2056. *
  2057. * Clears the blob pointer
  2058. */
  2059. static void smack_sk_free_security(struct sock *sk)
  2060. {
  2061. kfree(sk->sk_security);
  2062. }
  2063. /**
  2064. * smack_ipv4host_label - check host based restrictions
  2065. * @sip: the object end
  2066. *
  2067. * looks for host based access restrictions
  2068. *
  2069. * This version will only be appropriate for really small sets of single label
  2070. * hosts. The caller is responsible for ensuring that the RCU read lock is
  2071. * taken before calling this function.
  2072. *
  2073. * Returns the label of the far end or NULL if it's not special.
  2074. */
  2075. static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
  2076. {
  2077. struct smk_net4addr *snp;
  2078. struct in_addr *siap = &sip->sin_addr;
  2079. if (siap->s_addr == 0)
  2080. return NULL;
  2081. list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
  2082. /*
  2083. * we break after finding the first match because
  2084. * the list is sorted from longest to shortest mask
  2085. * so we have found the most specific match
  2086. */
  2087. if (snp->smk_host.s_addr ==
  2088. (siap->s_addr & snp->smk_mask.s_addr))
  2089. return snp->smk_label;
  2090. return NULL;
  2091. }
  2092. #if IS_ENABLED(CONFIG_IPV6)
  2093. /*
  2094. * smk_ipv6_localhost - Check for local ipv6 host address
  2095. * @sip: the address
  2096. *
  2097. * Returns boolean true if this is the localhost address
  2098. */
  2099. static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
  2100. {
  2101. __be16 *be16p = (__be16 *)&sip->sin6_addr;
  2102. __be32 *be32p = (__be32 *)&sip->sin6_addr;
  2103. if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
  2104. ntohs(be16p[7]) == 1)
  2105. return true;
  2106. return false;
  2107. }
  2108. /**
  2109. * smack_ipv6host_label - check host based restrictions
  2110. * @sip: the object end
  2111. *
  2112. * looks for host based access restrictions
  2113. *
  2114. * This version will only be appropriate for really small sets of single label
  2115. * hosts. The caller is responsible for ensuring that the RCU read lock is
  2116. * taken before calling this function.
  2117. *
  2118. * Returns the label of the far end or NULL if it's not special.
  2119. */
  2120. static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
  2121. {
  2122. struct smk_net6addr *snp;
  2123. struct in6_addr *sap = &sip->sin6_addr;
  2124. int i;
  2125. int found = 0;
  2126. /*
  2127. * It's local. Don't look for a host label.
  2128. */
  2129. if (smk_ipv6_localhost(sip))
  2130. return NULL;
  2131. list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
  2132. /*
  2133. * we break after finding the first match because
  2134. * the list is sorted from longest to shortest mask
  2135. * so we have found the most specific match
  2136. */
  2137. for (found = 1, i = 0; i < 8; i++) {
  2138. /*
  2139. * If the label is NULL the entry has
  2140. * been renounced. Ignore it.
  2141. */
  2142. if (snp->smk_label == NULL)
  2143. continue;
  2144. if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
  2145. snp->smk_host.s6_addr16[i]) {
  2146. found = 0;
  2147. break;
  2148. }
  2149. }
  2150. if (found)
  2151. return snp->smk_label;
  2152. }
  2153. return NULL;
  2154. }
  2155. #endif /* CONFIG_IPV6 */
  2156. /**
  2157. * smack_netlabel - Set the secattr on a socket
  2158. * @sk: the socket
  2159. * @labeled: socket label scheme
  2160. *
  2161. * Convert the outbound smack value (smk_out) to a
  2162. * secattr and attach it to the socket.
  2163. *
  2164. * Returns 0 on success or an error code
  2165. */
  2166. static int smack_netlabel(struct sock *sk, int labeled)
  2167. {
  2168. struct smack_known *skp;
  2169. struct socket_smack *ssp = sk->sk_security;
  2170. int rc = 0;
  2171. /*
  2172. * Usually the netlabel code will handle changing the
  2173. * packet labeling based on the label.
  2174. * The case of a single label host is different, because
  2175. * a single label host should never get a labeled packet
  2176. * even though the label is usually associated with a packet
  2177. * label.
  2178. */
  2179. local_bh_disable();
  2180. bh_lock_sock_nested(sk);
  2181. if (ssp->smk_out == smack_net_ambient ||
  2182. labeled == SMACK_UNLABELED_SOCKET)
  2183. netlbl_sock_delattr(sk);
  2184. else {
  2185. skp = ssp->smk_out;
  2186. rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
  2187. }
  2188. bh_unlock_sock(sk);
  2189. local_bh_enable();
  2190. return rc;
  2191. }
  2192. /**
  2193. * smack_netlbel_send - Set the secattr on a socket and perform access checks
  2194. * @sk: the socket
  2195. * @sap: the destination address
  2196. *
  2197. * Set the correct secattr for the given socket based on the destination
  2198. * address and perform any outbound access checks needed.
  2199. *
  2200. * Returns 0 on success or an error code.
  2201. *
  2202. */
  2203. static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
  2204. {
  2205. struct smack_known *skp;
  2206. int rc;
  2207. int sk_lbl;
  2208. struct smack_known *hkp;
  2209. struct socket_smack *ssp = sk->sk_security;
  2210. struct smk_audit_info ad;
  2211. rcu_read_lock();
  2212. hkp = smack_ipv4host_label(sap);
  2213. if (hkp != NULL) {
  2214. #ifdef CONFIG_AUDIT
  2215. struct lsm_network_audit net;
  2216. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2217. ad.a.u.net->family = sap->sin_family;
  2218. ad.a.u.net->dport = sap->sin_port;
  2219. ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
  2220. #endif
  2221. sk_lbl = SMACK_UNLABELED_SOCKET;
  2222. skp = ssp->smk_out;
  2223. rc = smk_access(skp, hkp, MAY_WRITE, &ad);
  2224. rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
  2225. } else {
  2226. sk_lbl = SMACK_CIPSO_SOCKET;
  2227. rc = 0;
  2228. }
  2229. rcu_read_unlock();
  2230. if (rc != 0)
  2231. return rc;
  2232. return smack_netlabel(sk, sk_lbl);
  2233. }
  2234. #if IS_ENABLED(CONFIG_IPV6)
  2235. /**
  2236. * smk_ipv6_check - check Smack access
  2237. * @subject: subject Smack label
  2238. * @object: object Smack label
  2239. * @address: address
  2240. * @act: the action being taken
  2241. *
  2242. * Check an IPv6 access
  2243. */
  2244. static int smk_ipv6_check(struct smack_known *subject,
  2245. struct smack_known *object,
  2246. struct sockaddr_in6 *address, int act)
  2247. {
  2248. #ifdef CONFIG_AUDIT
  2249. struct lsm_network_audit net;
  2250. #endif
  2251. struct smk_audit_info ad;
  2252. int rc;
  2253. #ifdef CONFIG_AUDIT
  2254. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2255. ad.a.u.net->family = PF_INET6;
  2256. ad.a.u.net->dport = ntohs(address->sin6_port);
  2257. if (act == SMK_RECEIVING)
  2258. ad.a.u.net->v6info.saddr = address->sin6_addr;
  2259. else
  2260. ad.a.u.net->v6info.daddr = address->sin6_addr;
  2261. #endif
  2262. rc = smk_access(subject, object, MAY_WRITE, &ad);
  2263. rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
  2264. return rc;
  2265. }
  2266. #endif /* CONFIG_IPV6 */
  2267. #ifdef SMACK_IPV6_PORT_LABELING
  2268. /**
  2269. * smk_ipv6_port_label - Smack port access table management
  2270. * @sock: socket
  2271. * @address: address
  2272. *
  2273. * Create or update the port list entry
  2274. */
  2275. static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
  2276. {
  2277. struct sock *sk = sock->sk;
  2278. struct sockaddr_in6 *addr6;
  2279. struct socket_smack *ssp = sock->sk->sk_security;
  2280. struct smk_port_label *spp;
  2281. unsigned short port = 0;
  2282. if (address == NULL) {
  2283. /*
  2284. * This operation is changing the Smack information
  2285. * on the bound socket. Take the changes to the port
  2286. * as well.
  2287. */
  2288. list_for_each_entry(spp, &smk_ipv6_port_list, list) {
  2289. if (sk != spp->smk_sock)
  2290. continue;
  2291. spp->smk_in = ssp->smk_in;
  2292. spp->smk_out = ssp->smk_out;
  2293. return;
  2294. }
  2295. /*
  2296. * A NULL address is only used for updating existing
  2297. * bound entries. If there isn't one, it's OK.
  2298. */
  2299. return;
  2300. }
  2301. addr6 = (struct sockaddr_in6 *)address;
  2302. port = ntohs(addr6->sin6_port);
  2303. /*
  2304. * This is a special case that is safely ignored.
  2305. */
  2306. if (port == 0)
  2307. return;
  2308. /*
  2309. * Look for an existing port list entry.
  2310. * This is an indication that a port is getting reused.
  2311. */
  2312. list_for_each_entry(spp, &smk_ipv6_port_list, list) {
  2313. if (spp->smk_port != port)
  2314. continue;
  2315. spp->smk_port = port;
  2316. spp->smk_sock = sk;
  2317. spp->smk_in = ssp->smk_in;
  2318. spp->smk_out = ssp->smk_out;
  2319. return;
  2320. }
  2321. /*
  2322. * A new port entry is required.
  2323. */
  2324. spp = kzalloc(sizeof(*spp), GFP_KERNEL);
  2325. if (spp == NULL)
  2326. return;
  2327. spp->smk_port = port;
  2328. spp->smk_sock = sk;
  2329. spp->smk_in = ssp->smk_in;
  2330. spp->smk_out = ssp->smk_out;
  2331. list_add(&spp->list, &smk_ipv6_port_list);
  2332. return;
  2333. }
  2334. /**
  2335. * smk_ipv6_port_check - check Smack port access
  2336. * @sock: socket
  2337. * @address: address
  2338. *
  2339. * Create or update the port list entry
  2340. */
  2341. static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
  2342. int act)
  2343. {
  2344. struct smk_port_label *spp;
  2345. struct socket_smack *ssp = sk->sk_security;
  2346. struct smack_known *skp = NULL;
  2347. unsigned short port;
  2348. struct smack_known *object;
  2349. if (act == SMK_RECEIVING) {
  2350. skp = smack_ipv6host_label(address);
  2351. object = ssp->smk_in;
  2352. } else {
  2353. skp = ssp->smk_out;
  2354. object = smack_ipv6host_label(address);
  2355. }
  2356. /*
  2357. * The other end is a single label host.
  2358. */
  2359. if (skp != NULL && object != NULL)
  2360. return smk_ipv6_check(skp, object, address, act);
  2361. if (skp == NULL)
  2362. skp = smack_net_ambient;
  2363. if (object == NULL)
  2364. object = smack_net_ambient;
  2365. /*
  2366. * It's remote, so port lookup does no good.
  2367. */
  2368. if (!smk_ipv6_localhost(address))
  2369. return smk_ipv6_check(skp, object, address, act);
  2370. /*
  2371. * It's local so the send check has to have passed.
  2372. */
  2373. if (act == SMK_RECEIVING)
  2374. return 0;
  2375. port = ntohs(address->sin6_port);
  2376. list_for_each_entry(spp, &smk_ipv6_port_list, list) {
  2377. if (spp->smk_port != port)
  2378. continue;
  2379. object = spp->smk_in;
  2380. if (act == SMK_CONNECTING)
  2381. ssp->smk_packet = spp->smk_out;
  2382. break;
  2383. }
  2384. return smk_ipv6_check(skp, object, address, act);
  2385. }
  2386. #endif /* SMACK_IPV6_PORT_LABELING */
  2387. /**
  2388. * smack_inode_setsecurity - set smack xattrs
  2389. * @inode: the object
  2390. * @name: attribute name
  2391. * @value: attribute value
  2392. * @size: size of the attribute
  2393. * @flags: unused
  2394. *
  2395. * Sets the named attribute in the appropriate blob
  2396. *
  2397. * Returns 0 on success, or an error code
  2398. */
  2399. static int smack_inode_setsecurity(struct inode *inode, const char *name,
  2400. const void *value, size_t size, int flags)
  2401. {
  2402. struct smack_known *skp;
  2403. struct inode_smack *nsp = inode->i_security;
  2404. struct socket_smack *ssp;
  2405. struct socket *sock;
  2406. int rc = 0;
  2407. if (value == NULL || size > SMK_LONGLABEL || size == 0)
  2408. return -EINVAL;
  2409. skp = smk_import_entry(value, size);
  2410. if (IS_ERR(skp))
  2411. return PTR_ERR(skp);
  2412. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
  2413. nsp->smk_inode = skp;
  2414. nsp->smk_flags |= SMK_INODE_INSTANT;
  2415. return 0;
  2416. }
  2417. /*
  2418. * The rest of the Smack xattrs are only on sockets.
  2419. */
  2420. if (inode->i_sb->s_magic != SOCKFS_MAGIC)
  2421. return -EOPNOTSUPP;
  2422. sock = SOCKET_I(inode);
  2423. if (sock == NULL || sock->sk == NULL)
  2424. return -EOPNOTSUPP;
  2425. ssp = sock->sk->sk_security;
  2426. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  2427. ssp->smk_in = skp;
  2428. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
  2429. ssp->smk_out = skp;
  2430. if (sock->sk->sk_family == PF_INET) {
  2431. rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  2432. if (rc != 0)
  2433. printk(KERN_WARNING
  2434. "Smack: \"%s\" netlbl error %d.\n",
  2435. __func__, -rc);
  2436. }
  2437. } else
  2438. return -EOPNOTSUPP;
  2439. #ifdef SMACK_IPV6_PORT_LABELING
  2440. if (sock->sk->sk_family == PF_INET6)
  2441. smk_ipv6_port_label(sock, NULL);
  2442. #endif
  2443. return 0;
  2444. }
  2445. /**
  2446. * smack_socket_post_create - finish socket setup
  2447. * @sock: the socket
  2448. * @family: protocol family
  2449. * @type: unused
  2450. * @protocol: unused
  2451. * @kern: unused
  2452. *
  2453. * Sets the netlabel information on the socket
  2454. *
  2455. * Returns 0 on success, and error code otherwise
  2456. */
  2457. static int smack_socket_post_create(struct socket *sock, int family,
  2458. int type, int protocol, int kern)
  2459. {
  2460. struct socket_smack *ssp;
  2461. if (sock->sk == NULL)
  2462. return 0;
  2463. /*
  2464. * Sockets created by kernel threads receive web label.
  2465. */
  2466. if (unlikely(current->flags & PF_KTHREAD)) {
  2467. ssp = sock->sk->sk_security;
  2468. ssp->smk_in = &smack_known_web;
  2469. ssp->smk_out = &smack_known_web;
  2470. }
  2471. if (family != PF_INET)
  2472. return 0;
  2473. /*
  2474. * Set the outbound netlbl.
  2475. */
  2476. return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  2477. }
  2478. #ifdef SMACK_IPV6_PORT_LABELING
  2479. /**
  2480. * smack_socket_bind - record port binding information.
  2481. * @sock: the socket
  2482. * @address: the port address
  2483. * @addrlen: size of the address
  2484. *
  2485. * Records the label bound to a port.
  2486. *
  2487. * Returns 0
  2488. */
  2489. static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
  2490. int addrlen)
  2491. {
  2492. if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
  2493. smk_ipv6_port_label(sock, address);
  2494. return 0;
  2495. }
  2496. #endif /* SMACK_IPV6_PORT_LABELING */
  2497. /**
  2498. * smack_socket_connect - connect access check
  2499. * @sock: the socket
  2500. * @sap: the other end
  2501. * @addrlen: size of sap
  2502. *
  2503. * Verifies that a connection may be possible
  2504. *
  2505. * Returns 0 on success, and error code otherwise
  2506. */
  2507. static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
  2508. int addrlen)
  2509. {
  2510. int rc = 0;
  2511. #if IS_ENABLED(CONFIG_IPV6)
  2512. struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
  2513. #endif
  2514. #ifdef SMACK_IPV6_SECMARK_LABELING
  2515. struct smack_known *rsp;
  2516. struct socket_smack *ssp = sock->sk->sk_security;
  2517. #endif
  2518. if (sock->sk == NULL)
  2519. return 0;
  2520. switch (sock->sk->sk_family) {
  2521. case PF_INET:
  2522. if (addrlen < sizeof(struct sockaddr_in))
  2523. return -EINVAL;
  2524. rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
  2525. break;
  2526. case PF_INET6:
  2527. if (addrlen < sizeof(struct sockaddr_in6))
  2528. return -EINVAL;
  2529. #ifdef SMACK_IPV6_SECMARK_LABELING
  2530. rsp = smack_ipv6host_label(sip);
  2531. if (rsp != NULL)
  2532. rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
  2533. SMK_CONNECTING);
  2534. #endif
  2535. #ifdef SMACK_IPV6_PORT_LABELING
  2536. rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
  2537. #endif
  2538. break;
  2539. }
  2540. return rc;
  2541. }
  2542. /**
  2543. * smack_flags_to_may - convert S_ to MAY_ values
  2544. * @flags: the S_ value
  2545. *
  2546. * Returns the equivalent MAY_ value
  2547. */
  2548. static int smack_flags_to_may(int flags)
  2549. {
  2550. int may = 0;
  2551. if (flags & S_IRUGO)
  2552. may |= MAY_READ;
  2553. if (flags & S_IWUGO)
  2554. may |= MAY_WRITE;
  2555. if (flags & S_IXUGO)
  2556. may |= MAY_EXEC;
  2557. return may;
  2558. }
  2559. /**
  2560. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  2561. * @msg: the object
  2562. *
  2563. * Returns 0
  2564. */
  2565. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  2566. {
  2567. struct smack_known *skp = smk_of_current();
  2568. msg->security = skp;
  2569. return 0;
  2570. }
  2571. /**
  2572. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  2573. * @msg: the object
  2574. *
  2575. * Clears the blob pointer
  2576. */
  2577. static void smack_msg_msg_free_security(struct msg_msg *msg)
  2578. {
  2579. msg->security = NULL;
  2580. }
  2581. /**
  2582. * smack_of_shm - the smack pointer for the shm
  2583. * @shp: the object
  2584. *
  2585. * Returns a pointer to the smack value
  2586. */
  2587. static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
  2588. {
  2589. return (struct smack_known *)shp->shm_perm.security;
  2590. }
  2591. /**
  2592. * smack_shm_alloc_security - Set the security blob for shm
  2593. * @shp: the object
  2594. *
  2595. * Returns 0
  2596. */
  2597. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  2598. {
  2599. struct kern_ipc_perm *isp = &shp->shm_perm;
  2600. struct smack_known *skp = smk_of_current();
  2601. isp->security = skp;
  2602. return 0;
  2603. }
  2604. /**
  2605. * smack_shm_free_security - Clear the security blob for shm
  2606. * @shp: the object
  2607. *
  2608. * Clears the blob pointer
  2609. */
  2610. static void smack_shm_free_security(struct shmid_kernel *shp)
  2611. {
  2612. struct kern_ipc_perm *isp = &shp->shm_perm;
  2613. isp->security = NULL;
  2614. }
  2615. /**
  2616. * smk_curacc_shm : check if current has access on shm
  2617. * @shp : the object
  2618. * @access : access requested
  2619. *
  2620. * Returns 0 if current has the requested access, error code otherwise
  2621. */
  2622. static int smk_curacc_shm(struct shmid_kernel *shp, int access)
  2623. {
  2624. struct smack_known *ssp = smack_of_shm(shp);
  2625. struct smk_audit_info ad;
  2626. int rc;
  2627. #ifdef CONFIG_AUDIT
  2628. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2629. ad.a.u.ipc_id = shp->shm_perm.id;
  2630. #endif
  2631. rc = smk_curacc(ssp, access, &ad);
  2632. rc = smk_bu_current("shm", ssp, access, rc);
  2633. return rc;
  2634. }
  2635. /**
  2636. * smack_shm_associate - Smack access check for shm
  2637. * @shp: the object
  2638. * @shmflg: access requested
  2639. *
  2640. * Returns 0 if current has the requested access, error code otherwise
  2641. */
  2642. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  2643. {
  2644. int may;
  2645. may = smack_flags_to_may(shmflg);
  2646. return smk_curacc_shm(shp, may);
  2647. }
  2648. /**
  2649. * smack_shm_shmctl - Smack access check for shm
  2650. * @shp: the object
  2651. * @cmd: what it wants to do
  2652. *
  2653. * Returns 0 if current has the requested access, error code otherwise
  2654. */
  2655. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  2656. {
  2657. int may;
  2658. switch (cmd) {
  2659. case IPC_STAT:
  2660. case SHM_STAT:
  2661. may = MAY_READ;
  2662. break;
  2663. case IPC_SET:
  2664. case SHM_LOCK:
  2665. case SHM_UNLOCK:
  2666. case IPC_RMID:
  2667. may = MAY_READWRITE;
  2668. break;
  2669. case IPC_INFO:
  2670. case SHM_INFO:
  2671. /*
  2672. * System level information.
  2673. */
  2674. return 0;
  2675. default:
  2676. return -EINVAL;
  2677. }
  2678. return smk_curacc_shm(shp, may);
  2679. }
  2680. /**
  2681. * smack_shm_shmat - Smack access for shmat
  2682. * @shp: the object
  2683. * @shmaddr: unused
  2684. * @shmflg: access requested
  2685. *
  2686. * Returns 0 if current has the requested access, error code otherwise
  2687. */
  2688. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  2689. int shmflg)
  2690. {
  2691. int may;
  2692. may = smack_flags_to_may(shmflg);
  2693. return smk_curacc_shm(shp, may);
  2694. }
  2695. /**
  2696. * smack_of_sem - the smack pointer for the sem
  2697. * @sma: the object
  2698. *
  2699. * Returns a pointer to the smack value
  2700. */
  2701. static struct smack_known *smack_of_sem(struct sem_array *sma)
  2702. {
  2703. return (struct smack_known *)sma->sem_perm.security;
  2704. }
  2705. /**
  2706. * smack_sem_alloc_security - Set the security blob for sem
  2707. * @sma: the object
  2708. *
  2709. * Returns 0
  2710. */
  2711. static int smack_sem_alloc_security(struct sem_array *sma)
  2712. {
  2713. struct kern_ipc_perm *isp = &sma->sem_perm;
  2714. struct smack_known *skp = smk_of_current();
  2715. isp->security = skp;
  2716. return 0;
  2717. }
  2718. /**
  2719. * smack_sem_free_security - Clear the security blob for sem
  2720. * @sma: the object
  2721. *
  2722. * Clears the blob pointer
  2723. */
  2724. static void smack_sem_free_security(struct sem_array *sma)
  2725. {
  2726. struct kern_ipc_perm *isp = &sma->sem_perm;
  2727. isp->security = NULL;
  2728. }
  2729. /**
  2730. * smk_curacc_sem : check if current has access on sem
  2731. * @sma : the object
  2732. * @access : access requested
  2733. *
  2734. * Returns 0 if current has the requested access, error code otherwise
  2735. */
  2736. static int smk_curacc_sem(struct sem_array *sma, int access)
  2737. {
  2738. struct smack_known *ssp = smack_of_sem(sma);
  2739. struct smk_audit_info ad;
  2740. int rc;
  2741. #ifdef CONFIG_AUDIT
  2742. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2743. ad.a.u.ipc_id = sma->sem_perm.id;
  2744. #endif
  2745. rc = smk_curacc(ssp, access, &ad);
  2746. rc = smk_bu_current("sem", ssp, access, rc);
  2747. return rc;
  2748. }
  2749. /**
  2750. * smack_sem_associate - Smack access check for sem
  2751. * @sma: the object
  2752. * @semflg: access requested
  2753. *
  2754. * Returns 0 if current has the requested access, error code otherwise
  2755. */
  2756. static int smack_sem_associate(struct sem_array *sma, int semflg)
  2757. {
  2758. int may;
  2759. may = smack_flags_to_may(semflg);
  2760. return smk_curacc_sem(sma, may);
  2761. }
  2762. /**
  2763. * smack_sem_shmctl - Smack access check for sem
  2764. * @sma: the object
  2765. * @cmd: what it wants to do
  2766. *
  2767. * Returns 0 if current has the requested access, error code otherwise
  2768. */
  2769. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  2770. {
  2771. int may;
  2772. switch (cmd) {
  2773. case GETPID:
  2774. case GETNCNT:
  2775. case GETZCNT:
  2776. case GETVAL:
  2777. case GETALL:
  2778. case IPC_STAT:
  2779. case SEM_STAT:
  2780. may = MAY_READ;
  2781. break;
  2782. case SETVAL:
  2783. case SETALL:
  2784. case IPC_RMID:
  2785. case IPC_SET:
  2786. may = MAY_READWRITE;
  2787. break;
  2788. case IPC_INFO:
  2789. case SEM_INFO:
  2790. /*
  2791. * System level information
  2792. */
  2793. return 0;
  2794. default:
  2795. return -EINVAL;
  2796. }
  2797. return smk_curacc_sem(sma, may);
  2798. }
  2799. /**
  2800. * smack_sem_semop - Smack checks of semaphore operations
  2801. * @sma: the object
  2802. * @sops: unused
  2803. * @nsops: unused
  2804. * @alter: unused
  2805. *
  2806. * Treated as read and write in all cases.
  2807. *
  2808. * Returns 0 if access is allowed, error code otherwise
  2809. */
  2810. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  2811. unsigned nsops, int alter)
  2812. {
  2813. return smk_curacc_sem(sma, MAY_READWRITE);
  2814. }
  2815. /**
  2816. * smack_msg_alloc_security - Set the security blob for msg
  2817. * @msq: the object
  2818. *
  2819. * Returns 0
  2820. */
  2821. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  2822. {
  2823. struct kern_ipc_perm *kisp = &msq->q_perm;
  2824. struct smack_known *skp = smk_of_current();
  2825. kisp->security = skp;
  2826. return 0;
  2827. }
  2828. /**
  2829. * smack_msg_free_security - Clear the security blob for msg
  2830. * @msq: the object
  2831. *
  2832. * Clears the blob pointer
  2833. */
  2834. static void smack_msg_queue_free_security(struct msg_queue *msq)
  2835. {
  2836. struct kern_ipc_perm *kisp = &msq->q_perm;
  2837. kisp->security = NULL;
  2838. }
  2839. /**
  2840. * smack_of_msq - the smack pointer for the msq
  2841. * @msq: the object
  2842. *
  2843. * Returns a pointer to the smack label entry
  2844. */
  2845. static struct smack_known *smack_of_msq(struct msg_queue *msq)
  2846. {
  2847. return (struct smack_known *)msq->q_perm.security;
  2848. }
  2849. /**
  2850. * smk_curacc_msq : helper to check if current has access on msq
  2851. * @msq : the msq
  2852. * @access : access requested
  2853. *
  2854. * return 0 if current has access, error otherwise
  2855. */
  2856. static int smk_curacc_msq(struct msg_queue *msq, int access)
  2857. {
  2858. struct smack_known *msp = smack_of_msq(msq);
  2859. struct smk_audit_info ad;
  2860. int rc;
  2861. #ifdef CONFIG_AUDIT
  2862. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2863. ad.a.u.ipc_id = msq->q_perm.id;
  2864. #endif
  2865. rc = smk_curacc(msp, access, &ad);
  2866. rc = smk_bu_current("msq", msp, access, rc);
  2867. return rc;
  2868. }
  2869. /**
  2870. * smack_msg_queue_associate - Smack access check for msg_queue
  2871. * @msq: the object
  2872. * @msqflg: access requested
  2873. *
  2874. * Returns 0 if current has the requested access, error code otherwise
  2875. */
  2876. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  2877. {
  2878. int may;
  2879. may = smack_flags_to_may(msqflg);
  2880. return smk_curacc_msq(msq, may);
  2881. }
  2882. /**
  2883. * smack_msg_queue_msgctl - Smack access check for msg_queue
  2884. * @msq: the object
  2885. * @cmd: what it wants to do
  2886. *
  2887. * Returns 0 if current has the requested access, error code otherwise
  2888. */
  2889. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  2890. {
  2891. int may;
  2892. switch (cmd) {
  2893. case IPC_STAT:
  2894. case MSG_STAT:
  2895. may = MAY_READ;
  2896. break;
  2897. case IPC_SET:
  2898. case IPC_RMID:
  2899. may = MAY_READWRITE;
  2900. break;
  2901. case IPC_INFO:
  2902. case MSG_INFO:
  2903. /*
  2904. * System level information
  2905. */
  2906. return 0;
  2907. default:
  2908. return -EINVAL;
  2909. }
  2910. return smk_curacc_msq(msq, may);
  2911. }
  2912. /**
  2913. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2914. * @msq: the object
  2915. * @msg: unused
  2916. * @msqflg: access requested
  2917. *
  2918. * Returns 0 if current has the requested access, error code otherwise
  2919. */
  2920. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  2921. int msqflg)
  2922. {
  2923. int may;
  2924. may = smack_flags_to_may(msqflg);
  2925. return smk_curacc_msq(msq, may);
  2926. }
  2927. /**
  2928. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2929. * @msq: the object
  2930. * @msg: unused
  2931. * @target: unused
  2932. * @type: unused
  2933. * @mode: unused
  2934. *
  2935. * Returns 0 if current has read and write access, error code otherwise
  2936. */
  2937. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  2938. struct task_struct *target, long type, int mode)
  2939. {
  2940. return smk_curacc_msq(msq, MAY_READWRITE);
  2941. }
  2942. /**
  2943. * smack_ipc_permission - Smack access for ipc_permission()
  2944. * @ipp: the object permissions
  2945. * @flag: access requested
  2946. *
  2947. * Returns 0 if current has read and write access, error code otherwise
  2948. */
  2949. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  2950. {
  2951. struct smack_known *iskp = ipp->security;
  2952. int may = smack_flags_to_may(flag);
  2953. struct smk_audit_info ad;
  2954. int rc;
  2955. #ifdef CONFIG_AUDIT
  2956. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2957. ad.a.u.ipc_id = ipp->id;
  2958. #endif
  2959. rc = smk_curacc(iskp, may, &ad);
  2960. rc = smk_bu_current("svipc", iskp, may, rc);
  2961. return rc;
  2962. }
  2963. /**
  2964. * smack_ipc_getsecid - Extract smack security id
  2965. * @ipp: the object permissions
  2966. * @secid: where result will be saved
  2967. */
  2968. static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
  2969. {
  2970. struct smack_known *iskp = ipp->security;
  2971. *secid = iskp->smk_secid;
  2972. }
  2973. /**
  2974. * smack_d_instantiate - Make sure the blob is correct on an inode
  2975. * @opt_dentry: dentry where inode will be attached
  2976. * @inode: the object
  2977. *
  2978. * Set the inode's security blob if it hasn't been done already.
  2979. */
  2980. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  2981. {
  2982. struct super_block *sbp;
  2983. struct superblock_smack *sbsp;
  2984. struct inode_smack *isp;
  2985. struct smack_known *skp;
  2986. struct smack_known *ckp = smk_of_current();
  2987. struct smack_known *final;
  2988. char trattr[TRANS_TRUE_SIZE];
  2989. int transflag = 0;
  2990. int rc;
  2991. struct dentry *dp;
  2992. if (inode == NULL)
  2993. return;
  2994. isp = inode->i_security;
  2995. mutex_lock(&isp->smk_lock);
  2996. /*
  2997. * If the inode is already instantiated
  2998. * take the quick way out
  2999. */
  3000. if (isp->smk_flags & SMK_INODE_INSTANT)
  3001. goto unlockandout;
  3002. sbp = inode->i_sb;
  3003. sbsp = sbp->s_security;
  3004. /*
  3005. * We're going to use the superblock default label
  3006. * if there's no label on the file.
  3007. */
  3008. final = sbsp->smk_default;
  3009. /*
  3010. * If this is the root inode the superblock
  3011. * may be in the process of initialization.
  3012. * If that is the case use the root value out
  3013. * of the superblock.
  3014. */
  3015. if (opt_dentry->d_parent == opt_dentry) {
  3016. switch (sbp->s_magic) {
  3017. case CGROUP_SUPER_MAGIC:
  3018. /*
  3019. * The cgroup filesystem is never mounted,
  3020. * so there's no opportunity to set the mount
  3021. * options.
  3022. */
  3023. sbsp->smk_root = &smack_known_star;
  3024. sbsp->smk_default = &smack_known_star;
  3025. isp->smk_inode = sbsp->smk_root;
  3026. break;
  3027. case TMPFS_MAGIC:
  3028. /*
  3029. * What about shmem/tmpfs anonymous files with dentry
  3030. * obtained from d_alloc_pseudo()?
  3031. */
  3032. isp->smk_inode = smk_of_current();
  3033. break;
  3034. case PIPEFS_MAGIC:
  3035. isp->smk_inode = smk_of_current();
  3036. break;
  3037. default:
  3038. isp->smk_inode = sbsp->smk_root;
  3039. break;
  3040. }
  3041. isp->smk_flags |= SMK_INODE_INSTANT;
  3042. goto unlockandout;
  3043. }
  3044. /*
  3045. * This is pretty hackish.
  3046. * Casey says that we shouldn't have to do
  3047. * file system specific code, but it does help
  3048. * with keeping it simple.
  3049. */
  3050. switch (sbp->s_magic) {
  3051. case SMACK_MAGIC:
  3052. case PIPEFS_MAGIC:
  3053. case SOCKFS_MAGIC:
  3054. case CGROUP_SUPER_MAGIC:
  3055. /*
  3056. * Casey says that it's a little embarrassing
  3057. * that the smack file system doesn't do
  3058. * extended attributes.
  3059. *
  3060. * Casey says pipes are easy (?)
  3061. *
  3062. * Socket access is controlled by the socket
  3063. * structures associated with the task involved.
  3064. *
  3065. * Cgroupfs is special
  3066. */
  3067. final = &smack_known_star;
  3068. break;
  3069. case DEVPTS_SUPER_MAGIC:
  3070. /*
  3071. * devpts seems content with the label of the task.
  3072. * Programs that change smack have to treat the
  3073. * pty with respect.
  3074. */
  3075. final = ckp;
  3076. break;
  3077. case PROC_SUPER_MAGIC:
  3078. /*
  3079. * Casey says procfs appears not to care.
  3080. * The superblock default suffices.
  3081. */
  3082. break;
  3083. case TMPFS_MAGIC:
  3084. /*
  3085. * Device labels should come from the filesystem,
  3086. * but watch out, because they're volitile,
  3087. * getting recreated on every reboot.
  3088. */
  3089. final = &smack_known_star;
  3090. /*
  3091. * No break.
  3092. *
  3093. * If a smack value has been set we want to use it,
  3094. * but since tmpfs isn't giving us the opportunity
  3095. * to set mount options simulate setting the
  3096. * superblock default.
  3097. */
  3098. default:
  3099. /*
  3100. * This isn't an understood special case.
  3101. * Get the value from the xattr.
  3102. */
  3103. /*
  3104. * UNIX domain sockets use lower level socket data.
  3105. */
  3106. if (S_ISSOCK(inode->i_mode)) {
  3107. final = &smack_known_star;
  3108. break;
  3109. }
  3110. /*
  3111. * No xattr support means, alas, no SMACK label.
  3112. * Use the aforeapplied default.
  3113. * It would be curious if the label of the task
  3114. * does not match that assigned.
  3115. */
  3116. if (!(inode->i_opflags & IOP_XATTR))
  3117. break;
  3118. /*
  3119. * Get the dentry for xattr.
  3120. */
  3121. dp = dget(opt_dentry);
  3122. skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
  3123. if (!IS_ERR_OR_NULL(skp))
  3124. final = skp;
  3125. /*
  3126. * Transmuting directory
  3127. */
  3128. if (S_ISDIR(inode->i_mode)) {
  3129. /*
  3130. * If this is a new directory and the label was
  3131. * transmuted when the inode was initialized
  3132. * set the transmute attribute on the directory
  3133. * and mark the inode.
  3134. *
  3135. * If there is a transmute attribute on the
  3136. * directory mark the inode.
  3137. */
  3138. if (isp->smk_flags & SMK_INODE_CHANGED) {
  3139. isp->smk_flags &= ~SMK_INODE_CHANGED;
  3140. rc = __vfs_setxattr(dp, inode,
  3141. XATTR_NAME_SMACKTRANSMUTE,
  3142. TRANS_TRUE, TRANS_TRUE_SIZE,
  3143. 0);
  3144. } else {
  3145. rc = __vfs_getxattr(dp, inode,
  3146. XATTR_NAME_SMACKTRANSMUTE, trattr,
  3147. TRANS_TRUE_SIZE);
  3148. if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
  3149. TRANS_TRUE_SIZE) != 0)
  3150. rc = -EINVAL;
  3151. }
  3152. if (rc >= 0)
  3153. transflag = SMK_INODE_TRANSMUTE;
  3154. }
  3155. /*
  3156. * Don't let the exec or mmap label be "*" or "@".
  3157. */
  3158. skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
  3159. if (IS_ERR(skp) || skp == &smack_known_star ||
  3160. skp == &smack_known_web)
  3161. skp = NULL;
  3162. isp->smk_task = skp;
  3163. skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
  3164. if (IS_ERR(skp) || skp == &smack_known_star ||
  3165. skp == &smack_known_web)
  3166. skp = NULL;
  3167. isp->smk_mmap = skp;
  3168. dput(dp);
  3169. break;
  3170. }
  3171. if (final == NULL)
  3172. isp->smk_inode = ckp;
  3173. else
  3174. isp->smk_inode = final;
  3175. isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
  3176. unlockandout:
  3177. mutex_unlock(&isp->smk_lock);
  3178. return;
  3179. }
  3180. /**
  3181. * smack_getprocattr - Smack process attribute access
  3182. * @p: the object task
  3183. * @name: the name of the attribute in /proc/.../attr
  3184. * @value: where to put the result
  3185. *
  3186. * Places a copy of the task Smack into value
  3187. *
  3188. * Returns the length of the smack label or an error code
  3189. */
  3190. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  3191. {
  3192. struct smack_known *skp = smk_of_task_struct(p);
  3193. char *cp;
  3194. int slen;
  3195. if (strcmp(name, "current") != 0)
  3196. return -EINVAL;
  3197. cp = kstrdup(skp->smk_known, GFP_KERNEL);
  3198. if (cp == NULL)
  3199. return -ENOMEM;
  3200. slen = strlen(cp);
  3201. *value = cp;
  3202. return slen;
  3203. }
  3204. /**
  3205. * smack_setprocattr - Smack process attribute setting
  3206. * @p: the object task
  3207. * @name: the name of the attribute in /proc/.../attr
  3208. * @value: the value to set
  3209. * @size: the size of the value
  3210. *
  3211. * Sets the Smack value of the task. Only setting self
  3212. * is permitted and only with privilege
  3213. *
  3214. * Returns the length of the smack label or an error code
  3215. */
  3216. static int smack_setprocattr(struct task_struct *p, char *name,
  3217. void *value, size_t size)
  3218. {
  3219. struct task_smack *tsp = current_security();
  3220. struct cred *new;
  3221. struct smack_known *skp;
  3222. struct smack_known_list_elem *sklep;
  3223. int rc;
  3224. /*
  3225. * Changing another process' Smack value is too dangerous
  3226. * and supports no sane use case.
  3227. */
  3228. if (p != current)
  3229. return -EPERM;
  3230. if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
  3231. return -EPERM;
  3232. if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
  3233. return -EINVAL;
  3234. if (strcmp(name, "current") != 0)
  3235. return -EINVAL;
  3236. skp = smk_import_entry(value, size);
  3237. if (IS_ERR(skp))
  3238. return PTR_ERR(skp);
  3239. /*
  3240. * No process is ever allowed the web ("@") label.
  3241. */
  3242. if (skp == &smack_known_web)
  3243. return -EPERM;
  3244. if (!smack_privileged(CAP_MAC_ADMIN)) {
  3245. rc = -EPERM;
  3246. list_for_each_entry(sklep, &tsp->smk_relabel, list)
  3247. if (sklep->smk_label == skp) {
  3248. rc = 0;
  3249. break;
  3250. }
  3251. if (rc)
  3252. return rc;
  3253. }
  3254. new = prepare_creds();
  3255. if (new == NULL)
  3256. return -ENOMEM;
  3257. tsp = new->security;
  3258. tsp->smk_task = skp;
  3259. /*
  3260. * process can change its label only once
  3261. */
  3262. smk_destroy_label_list(&tsp->smk_relabel);
  3263. commit_creds(new);
  3264. return size;
  3265. }
  3266. /**
  3267. * smack_unix_stream_connect - Smack access on UDS
  3268. * @sock: one sock
  3269. * @other: the other sock
  3270. * @newsk: unused
  3271. *
  3272. * Return 0 if a subject with the smack of sock could access
  3273. * an object with the smack of other, otherwise an error code
  3274. */
  3275. static int smack_unix_stream_connect(struct sock *sock,
  3276. struct sock *other, struct sock *newsk)
  3277. {
  3278. struct smack_known *skp;
  3279. struct smack_known *okp;
  3280. struct socket_smack *ssp = sock->sk_security;
  3281. struct socket_smack *osp = other->sk_security;
  3282. struct socket_smack *nsp = newsk->sk_security;
  3283. struct smk_audit_info ad;
  3284. int rc = 0;
  3285. #ifdef CONFIG_AUDIT
  3286. struct lsm_network_audit net;
  3287. #endif
  3288. if (!smack_privileged(CAP_MAC_OVERRIDE)) {
  3289. skp = ssp->smk_out;
  3290. okp = osp->smk_in;
  3291. #ifdef CONFIG_AUDIT
  3292. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  3293. smk_ad_setfield_u_net_sk(&ad, other);
  3294. #endif
  3295. rc = smk_access(skp, okp, MAY_WRITE, &ad);
  3296. rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
  3297. if (rc == 0) {
  3298. okp = osp->smk_out;
  3299. skp = ssp->smk_in;
  3300. rc = smk_access(okp, skp, MAY_WRITE, &ad);
  3301. rc = smk_bu_note("UDS connect", okp, skp,
  3302. MAY_WRITE, rc);
  3303. }
  3304. }
  3305. /*
  3306. * Cross reference the peer labels for SO_PEERSEC.
  3307. */
  3308. if (rc == 0) {
  3309. nsp->smk_packet = ssp->smk_out;
  3310. ssp->smk_packet = osp->smk_out;
  3311. }
  3312. return rc;
  3313. }
  3314. /**
  3315. * smack_unix_may_send - Smack access on UDS
  3316. * @sock: one socket
  3317. * @other: the other socket
  3318. *
  3319. * Return 0 if a subject with the smack of sock could access
  3320. * an object with the smack of other, otherwise an error code
  3321. */
  3322. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  3323. {
  3324. struct socket_smack *ssp = sock->sk->sk_security;
  3325. struct socket_smack *osp = other->sk->sk_security;
  3326. struct smk_audit_info ad;
  3327. int rc;
  3328. #ifdef CONFIG_AUDIT
  3329. struct lsm_network_audit net;
  3330. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  3331. smk_ad_setfield_u_net_sk(&ad, other->sk);
  3332. #endif
  3333. if (smack_privileged(CAP_MAC_OVERRIDE))
  3334. return 0;
  3335. rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
  3336. rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
  3337. return rc;
  3338. }
  3339. /**
  3340. * smack_socket_sendmsg - Smack check based on destination host
  3341. * @sock: the socket
  3342. * @msg: the message
  3343. * @size: the size of the message
  3344. *
  3345. * Return 0 if the current subject can write to the destination host.
  3346. * For IPv4 this is only a question if the destination is a single label host.
  3347. * For IPv6 this is a check against the label of the port.
  3348. */
  3349. static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  3350. int size)
  3351. {
  3352. struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
  3353. #if IS_ENABLED(CONFIG_IPV6)
  3354. struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
  3355. #endif
  3356. #ifdef SMACK_IPV6_SECMARK_LABELING
  3357. struct socket_smack *ssp = sock->sk->sk_security;
  3358. struct smack_known *rsp;
  3359. #endif
  3360. int rc = 0;
  3361. /*
  3362. * Perfectly reasonable for this to be NULL
  3363. */
  3364. if (sip == NULL)
  3365. return 0;
  3366. switch (sock->sk->sk_family) {
  3367. case AF_INET:
  3368. rc = smack_netlabel_send(sock->sk, sip);
  3369. break;
  3370. case AF_INET6:
  3371. #ifdef SMACK_IPV6_SECMARK_LABELING
  3372. rsp = smack_ipv6host_label(sap);
  3373. if (rsp != NULL)
  3374. rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
  3375. SMK_CONNECTING);
  3376. #endif
  3377. #ifdef SMACK_IPV6_PORT_LABELING
  3378. rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
  3379. #endif
  3380. break;
  3381. }
  3382. return rc;
  3383. }
  3384. /**
  3385. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
  3386. * @sap: netlabel secattr
  3387. * @ssp: socket security information
  3388. *
  3389. * Returns a pointer to a Smack label entry found on the label list.
  3390. */
  3391. static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
  3392. struct socket_smack *ssp)
  3393. {
  3394. struct smack_known *skp;
  3395. int found = 0;
  3396. int acat;
  3397. int kcat;
  3398. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
  3399. /*
  3400. * Looks like a CIPSO packet.
  3401. * If there are flags but no level netlabel isn't
  3402. * behaving the way we expect it to.
  3403. *
  3404. * Look it up in the label table
  3405. * Without guidance regarding the smack value
  3406. * for the packet fall back on the network
  3407. * ambient value.
  3408. */
  3409. rcu_read_lock();
  3410. list_for_each_entry(skp, &smack_known_list, list) {
  3411. if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
  3412. continue;
  3413. /*
  3414. * Compare the catsets. Use the netlbl APIs.
  3415. */
  3416. if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
  3417. if ((skp->smk_netlabel.flags &
  3418. NETLBL_SECATTR_MLS_CAT) == 0)
  3419. found = 1;
  3420. break;
  3421. }
  3422. for (acat = -1, kcat = -1; acat == kcat; ) {
  3423. acat = netlbl_catmap_walk(sap->attr.mls.cat,
  3424. acat + 1);
  3425. kcat = netlbl_catmap_walk(
  3426. skp->smk_netlabel.attr.mls.cat,
  3427. kcat + 1);
  3428. if (acat < 0 || kcat < 0)
  3429. break;
  3430. }
  3431. if (acat == kcat) {
  3432. found = 1;
  3433. break;
  3434. }
  3435. }
  3436. rcu_read_unlock();
  3437. if (found)
  3438. return skp;
  3439. if (ssp != NULL && ssp->smk_in == &smack_known_star)
  3440. return &smack_known_web;
  3441. return &smack_known_star;
  3442. }
  3443. if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
  3444. /*
  3445. * Looks like a fallback, which gives us a secid.
  3446. */
  3447. skp = smack_from_secid(sap->attr.secid);
  3448. /*
  3449. * This has got to be a bug because it is
  3450. * impossible to specify a fallback without
  3451. * specifying the label, which will ensure
  3452. * it has a secid, and the only way to get a
  3453. * secid is from a fallback.
  3454. */
  3455. BUG_ON(skp == NULL);
  3456. return skp;
  3457. }
  3458. /*
  3459. * Without guidance regarding the smack value
  3460. * for the packet fall back on the network
  3461. * ambient value.
  3462. */
  3463. return smack_net_ambient;
  3464. }
  3465. #if IS_ENABLED(CONFIG_IPV6)
  3466. static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
  3467. {
  3468. u8 nexthdr;
  3469. int offset;
  3470. int proto = -EINVAL;
  3471. struct ipv6hdr _ipv6h;
  3472. struct ipv6hdr *ip6;
  3473. __be16 frag_off;
  3474. struct tcphdr _tcph, *th;
  3475. struct udphdr _udph, *uh;
  3476. struct dccp_hdr _dccph, *dh;
  3477. sip->sin6_port = 0;
  3478. offset = skb_network_offset(skb);
  3479. ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
  3480. if (ip6 == NULL)
  3481. return -EINVAL;
  3482. sip->sin6_addr = ip6->saddr;
  3483. nexthdr = ip6->nexthdr;
  3484. offset += sizeof(_ipv6h);
  3485. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  3486. if (offset < 0)
  3487. return -EINVAL;
  3488. proto = nexthdr;
  3489. switch (proto) {
  3490. case IPPROTO_TCP:
  3491. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  3492. if (th != NULL)
  3493. sip->sin6_port = th->source;
  3494. break;
  3495. case IPPROTO_UDP:
  3496. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  3497. if (uh != NULL)
  3498. sip->sin6_port = uh->source;
  3499. break;
  3500. case IPPROTO_DCCP:
  3501. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  3502. if (dh != NULL)
  3503. sip->sin6_port = dh->dccph_sport;
  3504. break;
  3505. }
  3506. return proto;
  3507. }
  3508. #endif /* CONFIG_IPV6 */
  3509. /**
  3510. * smack_socket_sock_rcv_skb - Smack packet delivery access check
  3511. * @sk: socket
  3512. * @skb: packet
  3513. *
  3514. * Returns 0 if the packet should be delivered, an error code otherwise
  3515. */
  3516. static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  3517. {
  3518. struct netlbl_lsm_secattr secattr;
  3519. struct socket_smack *ssp = sk->sk_security;
  3520. struct smack_known *skp = NULL;
  3521. int rc = 0;
  3522. struct smk_audit_info ad;
  3523. #ifdef CONFIG_AUDIT
  3524. struct lsm_network_audit net;
  3525. #endif
  3526. #if IS_ENABLED(CONFIG_IPV6)
  3527. struct sockaddr_in6 sadd;
  3528. int proto;
  3529. #endif /* CONFIG_IPV6 */
  3530. switch (sk->sk_family) {
  3531. case PF_INET:
  3532. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  3533. /*
  3534. * If there is a secmark use it rather than the CIPSO label.
  3535. * If there is no secmark fall back to CIPSO.
  3536. * The secmark is assumed to reflect policy better.
  3537. */
  3538. if (skb && skb->secmark != 0) {
  3539. skp = smack_from_secid(skb->secmark);
  3540. goto access_check;
  3541. }
  3542. #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
  3543. /*
  3544. * Translate what netlabel gave us.
  3545. */
  3546. netlbl_secattr_init(&secattr);
  3547. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
  3548. if (rc == 0)
  3549. skp = smack_from_secattr(&secattr, ssp);
  3550. else
  3551. skp = smack_net_ambient;
  3552. netlbl_secattr_destroy(&secattr);
  3553. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  3554. access_check:
  3555. #endif
  3556. #ifdef CONFIG_AUDIT
  3557. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  3558. ad.a.u.net->family = sk->sk_family;
  3559. ad.a.u.net->netif = skb->skb_iif;
  3560. ipv4_skb_to_auditdata(skb, &ad.a, NULL);
  3561. #endif
  3562. /*
  3563. * Receiving a packet requires that the other end
  3564. * be able to write here. Read access is not required.
  3565. * This is the simplist possible security model
  3566. * for networking.
  3567. */
  3568. rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
  3569. rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
  3570. MAY_WRITE, rc);
  3571. if (rc != 0)
  3572. netlbl_skbuff_err(skb, sk->sk_family, rc, 0);
  3573. break;
  3574. #if IS_ENABLED(CONFIG_IPV6)
  3575. case PF_INET6:
  3576. proto = smk_skb_to_addr_ipv6(skb, &sadd);
  3577. if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
  3578. break;
  3579. #ifdef SMACK_IPV6_SECMARK_LABELING
  3580. if (skb && skb->secmark != 0)
  3581. skp = smack_from_secid(skb->secmark);
  3582. else
  3583. skp = smack_ipv6host_label(&sadd);
  3584. if (skp == NULL)
  3585. skp = smack_net_ambient;
  3586. #ifdef CONFIG_AUDIT
  3587. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  3588. ad.a.u.net->family = sk->sk_family;
  3589. ad.a.u.net->netif = skb->skb_iif;
  3590. ipv6_skb_to_auditdata(skb, &ad.a, NULL);
  3591. #endif /* CONFIG_AUDIT */
  3592. rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
  3593. rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
  3594. MAY_WRITE, rc);
  3595. #endif /* SMACK_IPV6_SECMARK_LABELING */
  3596. #ifdef SMACK_IPV6_PORT_LABELING
  3597. rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
  3598. #endif /* SMACK_IPV6_PORT_LABELING */
  3599. break;
  3600. #endif /* CONFIG_IPV6 */
  3601. }
  3602. return rc;
  3603. }
  3604. /**
  3605. * smack_socket_getpeersec_stream - pull in packet label
  3606. * @sock: the socket
  3607. * @optval: user's destination
  3608. * @optlen: size thereof
  3609. * @len: max thereof
  3610. *
  3611. * returns zero on success, an error code otherwise
  3612. */
  3613. static int smack_socket_getpeersec_stream(struct socket *sock,
  3614. char __user *optval,
  3615. int __user *optlen, unsigned len)
  3616. {
  3617. struct socket_smack *ssp;
  3618. char *rcp = "";
  3619. int slen = 1;
  3620. int rc = 0;
  3621. ssp = sock->sk->sk_security;
  3622. if (ssp->smk_packet != NULL) {
  3623. rcp = ssp->smk_packet->smk_known;
  3624. slen = strlen(rcp) + 1;
  3625. }
  3626. if (slen > len)
  3627. rc = -ERANGE;
  3628. else if (copy_to_user(optval, rcp, slen) != 0)
  3629. rc = -EFAULT;
  3630. if (put_user(slen, optlen) != 0)
  3631. rc = -EFAULT;
  3632. return rc;
  3633. }
  3634. /**
  3635. * smack_socket_getpeersec_dgram - pull in packet label
  3636. * @sock: the peer socket
  3637. * @skb: packet data
  3638. * @secid: pointer to where to put the secid of the packet
  3639. *
  3640. * Sets the netlabel socket state on sk from parent
  3641. */
  3642. static int smack_socket_getpeersec_dgram(struct socket *sock,
  3643. struct sk_buff *skb, u32 *secid)
  3644. {
  3645. struct netlbl_lsm_secattr secattr;
  3646. struct socket_smack *ssp = NULL;
  3647. struct smack_known *skp;
  3648. int family = PF_UNSPEC;
  3649. u32 s = 0; /* 0 is the invalid secid */
  3650. int rc;
  3651. if (skb != NULL) {
  3652. if (skb->protocol == htons(ETH_P_IP))
  3653. family = PF_INET;
  3654. #if IS_ENABLED(CONFIG_IPV6)
  3655. else if (skb->protocol == htons(ETH_P_IPV6))
  3656. family = PF_INET6;
  3657. #endif /* CONFIG_IPV6 */
  3658. }
  3659. if (family == PF_UNSPEC && sock != NULL)
  3660. family = sock->sk->sk_family;
  3661. switch (family) {
  3662. case PF_UNIX:
  3663. ssp = sock->sk->sk_security;
  3664. s = ssp->smk_out->smk_secid;
  3665. break;
  3666. case PF_INET:
  3667. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  3668. s = skb->secmark;
  3669. if (s != 0)
  3670. break;
  3671. #endif
  3672. /*
  3673. * Translate what netlabel gave us.
  3674. */
  3675. if (sock != NULL && sock->sk != NULL)
  3676. ssp = sock->sk->sk_security;
  3677. netlbl_secattr_init(&secattr);
  3678. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  3679. if (rc == 0) {
  3680. skp = smack_from_secattr(&secattr, ssp);
  3681. s = skp->smk_secid;
  3682. }
  3683. netlbl_secattr_destroy(&secattr);
  3684. break;
  3685. case PF_INET6:
  3686. #ifdef SMACK_IPV6_SECMARK_LABELING
  3687. s = skb->secmark;
  3688. #endif
  3689. break;
  3690. }
  3691. *secid = s;
  3692. if (s == 0)
  3693. return -EINVAL;
  3694. return 0;
  3695. }
  3696. /**
  3697. * smack_sock_graft - Initialize a newly created socket with an existing sock
  3698. * @sk: child sock
  3699. * @parent: parent socket
  3700. *
  3701. * Set the smk_{in,out} state of an existing sock based on the process that
  3702. * is creating the new socket.
  3703. */
  3704. static void smack_sock_graft(struct sock *sk, struct socket *parent)
  3705. {
  3706. struct socket_smack *ssp;
  3707. struct smack_known *skp = smk_of_current();
  3708. if (sk == NULL ||
  3709. (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
  3710. return;
  3711. ssp = sk->sk_security;
  3712. ssp->smk_in = skp;
  3713. ssp->smk_out = skp;
  3714. /* cssp->smk_packet is already set in smack_inet_csk_clone() */
  3715. }
  3716. /**
  3717. * smack_inet_conn_request - Smack access check on connect
  3718. * @sk: socket involved
  3719. * @skb: packet
  3720. * @req: unused
  3721. *
  3722. * Returns 0 if a task with the packet label could write to
  3723. * the socket, otherwise an error code
  3724. */
  3725. static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  3726. struct request_sock *req)
  3727. {
  3728. u16 family = sk->sk_family;
  3729. struct smack_known *skp;
  3730. struct socket_smack *ssp = sk->sk_security;
  3731. struct netlbl_lsm_secattr secattr;
  3732. struct sockaddr_in addr;
  3733. struct iphdr *hdr;
  3734. struct smack_known *hskp;
  3735. int rc;
  3736. struct smk_audit_info ad;
  3737. #ifdef CONFIG_AUDIT
  3738. struct lsm_network_audit net;
  3739. #endif
  3740. #if IS_ENABLED(CONFIG_IPV6)
  3741. if (family == PF_INET6) {
  3742. /*
  3743. * Handle mapped IPv4 packets arriving
  3744. * via IPv6 sockets. Don't set up netlabel
  3745. * processing on IPv6.
  3746. */
  3747. if (skb->protocol == htons(ETH_P_IP))
  3748. family = PF_INET;
  3749. else
  3750. return 0;
  3751. }
  3752. #endif /* CONFIG_IPV6 */
  3753. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  3754. /*
  3755. * If there is a secmark use it rather than the CIPSO label.
  3756. * If there is no secmark fall back to CIPSO.
  3757. * The secmark is assumed to reflect policy better.
  3758. */
  3759. if (skb && skb->secmark != 0) {
  3760. skp = smack_from_secid(skb->secmark);
  3761. goto access_check;
  3762. }
  3763. #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
  3764. netlbl_secattr_init(&secattr);
  3765. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  3766. if (rc == 0)
  3767. skp = smack_from_secattr(&secattr, ssp);
  3768. else
  3769. skp = &smack_known_huh;
  3770. netlbl_secattr_destroy(&secattr);
  3771. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  3772. access_check:
  3773. #endif
  3774. #ifdef CONFIG_AUDIT
  3775. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  3776. ad.a.u.net->family = family;
  3777. ad.a.u.net->netif = skb->skb_iif;
  3778. ipv4_skb_to_auditdata(skb, &ad.a, NULL);
  3779. #endif
  3780. /*
  3781. * Receiving a packet requires that the other end be able to write
  3782. * here. Read access is not required.
  3783. */
  3784. rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
  3785. rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
  3786. if (rc != 0)
  3787. return rc;
  3788. /*
  3789. * Save the peer's label in the request_sock so we can later setup
  3790. * smk_packet in the child socket so that SO_PEERCRED can report it.
  3791. */
  3792. req->peer_secid = skp->smk_secid;
  3793. /*
  3794. * We need to decide if we want to label the incoming connection here
  3795. * if we do we only need to label the request_sock and the stack will
  3796. * propagate the wire-label to the sock when it is created.
  3797. */
  3798. hdr = ip_hdr(skb);
  3799. addr.sin_addr.s_addr = hdr->saddr;
  3800. rcu_read_lock();
  3801. hskp = smack_ipv4host_label(&addr);
  3802. rcu_read_unlock();
  3803. if (hskp == NULL)
  3804. rc = netlbl_req_setattr(req, &skp->smk_netlabel);
  3805. else
  3806. netlbl_req_delattr(req);
  3807. return rc;
  3808. }
  3809. /**
  3810. * smack_inet_csk_clone - Copy the connection information to the new socket
  3811. * @sk: the new socket
  3812. * @req: the connection's request_sock
  3813. *
  3814. * Transfer the connection's peer label to the newly created socket.
  3815. */
  3816. static void smack_inet_csk_clone(struct sock *sk,
  3817. const struct request_sock *req)
  3818. {
  3819. struct socket_smack *ssp = sk->sk_security;
  3820. struct smack_known *skp;
  3821. if (req->peer_secid != 0) {
  3822. skp = smack_from_secid(req->peer_secid);
  3823. ssp->smk_packet = skp;
  3824. } else
  3825. ssp->smk_packet = NULL;
  3826. }
  3827. /*
  3828. * Key management security hooks
  3829. *
  3830. * Casey has not tested key support very heavily.
  3831. * The permission check is most likely too restrictive.
  3832. * If you care about keys please have a look.
  3833. */
  3834. #ifdef CONFIG_KEYS
  3835. /**
  3836. * smack_key_alloc - Set the key security blob
  3837. * @key: object
  3838. * @cred: the credentials to use
  3839. * @flags: unused
  3840. *
  3841. * No allocation required
  3842. *
  3843. * Returns 0
  3844. */
  3845. static int smack_key_alloc(struct key *key, const struct cred *cred,
  3846. unsigned long flags)
  3847. {
  3848. struct smack_known *skp = smk_of_task(cred->security);
  3849. key->security = skp;
  3850. return 0;
  3851. }
  3852. /**
  3853. * smack_key_free - Clear the key security blob
  3854. * @key: the object
  3855. *
  3856. * Clear the blob pointer
  3857. */
  3858. static void smack_key_free(struct key *key)
  3859. {
  3860. key->security = NULL;
  3861. }
  3862. /**
  3863. * smack_key_permission - Smack access on a key
  3864. * @key_ref: gets to the object
  3865. * @cred: the credentials to use
  3866. * @perm: requested key permissions
  3867. *
  3868. * Return 0 if the task has read and write to the object,
  3869. * an error code otherwise
  3870. */
  3871. static int smack_key_permission(key_ref_t key_ref,
  3872. const struct cred *cred, unsigned perm)
  3873. {
  3874. struct key *keyp;
  3875. struct smk_audit_info ad;
  3876. struct smack_known *tkp = smk_of_task(cred->security);
  3877. int request = 0;
  3878. int rc;
  3879. keyp = key_ref_to_ptr(key_ref);
  3880. if (keyp == NULL)
  3881. return -EINVAL;
  3882. /*
  3883. * If the key hasn't been initialized give it access so that
  3884. * it may do so.
  3885. */
  3886. if (keyp->security == NULL)
  3887. return 0;
  3888. /*
  3889. * This should not occur
  3890. */
  3891. if (tkp == NULL)
  3892. return -EACCES;
  3893. #ifdef CONFIG_AUDIT
  3894. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
  3895. ad.a.u.key_struct.key = keyp->serial;
  3896. ad.a.u.key_struct.key_desc = keyp->description;
  3897. #endif
  3898. if (perm & KEY_NEED_READ)
  3899. request = MAY_READ;
  3900. if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
  3901. request = MAY_WRITE;
  3902. rc = smk_access(tkp, keyp->security, request, &ad);
  3903. rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
  3904. return rc;
  3905. }
  3906. /*
  3907. * smack_key_getsecurity - Smack label tagging the key
  3908. * @key points to the key to be queried
  3909. * @_buffer points to a pointer that should be set to point to the
  3910. * resulting string (if no label or an error occurs).
  3911. * Return the length of the string (including terminating NUL) or -ve if
  3912. * an error.
  3913. * May also return 0 (and a NULL buffer pointer) if there is no label.
  3914. */
  3915. static int smack_key_getsecurity(struct key *key, char **_buffer)
  3916. {
  3917. struct smack_known *skp = key->security;
  3918. size_t length;
  3919. char *copy;
  3920. if (key->security == NULL) {
  3921. *_buffer = NULL;
  3922. return 0;
  3923. }
  3924. copy = kstrdup(skp->smk_known, GFP_KERNEL);
  3925. if (copy == NULL)
  3926. return -ENOMEM;
  3927. length = strlen(copy) + 1;
  3928. *_buffer = copy;
  3929. return length;
  3930. }
  3931. #endif /* CONFIG_KEYS */
  3932. /*
  3933. * Smack Audit hooks
  3934. *
  3935. * Audit requires a unique representation of each Smack specific
  3936. * rule. This unique representation is used to distinguish the
  3937. * object to be audited from remaining kernel objects and also
  3938. * works as a glue between the audit hooks.
  3939. *
  3940. * Since repository entries are added but never deleted, we'll use
  3941. * the smack_known label address related to the given audit rule as
  3942. * the needed unique representation. This also better fits the smack
  3943. * model where nearly everything is a label.
  3944. */
  3945. #ifdef CONFIG_AUDIT
  3946. /**
  3947. * smack_audit_rule_init - Initialize a smack audit rule
  3948. * @field: audit rule fields given from user-space (audit.h)
  3949. * @op: required testing operator (=, !=, >, <, ...)
  3950. * @rulestr: smack label to be audited
  3951. * @vrule: pointer to save our own audit rule representation
  3952. *
  3953. * Prepare to audit cases where (@field @op @rulestr) is true.
  3954. * The label to be audited is created if necessay.
  3955. */
  3956. static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  3957. {
  3958. struct smack_known *skp;
  3959. char **rule = (char **)vrule;
  3960. *rule = NULL;
  3961. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  3962. return -EINVAL;
  3963. if (op != Audit_equal && op != Audit_not_equal)
  3964. return -EINVAL;
  3965. skp = smk_import_entry(rulestr, 0);
  3966. if (IS_ERR(skp))
  3967. return PTR_ERR(skp);
  3968. *rule = skp->smk_known;
  3969. return 0;
  3970. }
  3971. /**
  3972. * smack_audit_rule_known - Distinguish Smack audit rules
  3973. * @krule: rule of interest, in Audit kernel representation format
  3974. *
  3975. * This is used to filter Smack rules from remaining Audit ones.
  3976. * If it's proved that this rule belongs to us, the
  3977. * audit_rule_match hook will be called to do the final judgement.
  3978. */
  3979. static int smack_audit_rule_known(struct audit_krule *krule)
  3980. {
  3981. struct audit_field *f;
  3982. int i;
  3983. for (i = 0; i < krule->field_count; i++) {
  3984. f = &krule->fields[i];
  3985. if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
  3986. return 1;
  3987. }
  3988. return 0;
  3989. }
  3990. /**
  3991. * smack_audit_rule_match - Audit given object ?
  3992. * @secid: security id for identifying the object to test
  3993. * @field: audit rule flags given from user-space
  3994. * @op: required testing operator
  3995. * @vrule: smack internal rule presentation
  3996. * @actx: audit context associated with the check
  3997. *
  3998. * The core Audit hook. It's used to take the decision of
  3999. * whether to audit or not to audit a given object.
  4000. */
  4001. static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
  4002. struct audit_context *actx)
  4003. {
  4004. struct smack_known *skp;
  4005. char *rule = vrule;
  4006. if (unlikely(!rule)) {
  4007. WARN_ONCE(1, "Smack: missing rule\n");
  4008. return -ENOENT;
  4009. }
  4010. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  4011. return 0;
  4012. skp = smack_from_secid(secid);
  4013. /*
  4014. * No need to do string comparisons. If a match occurs,
  4015. * both pointers will point to the same smack_known
  4016. * label.
  4017. */
  4018. if (op == Audit_equal)
  4019. return (rule == skp->smk_known);
  4020. if (op == Audit_not_equal)
  4021. return (rule != skp->smk_known);
  4022. return 0;
  4023. }
  4024. /*
  4025. * There is no need for a smack_audit_rule_free hook.
  4026. * No memory was allocated.
  4027. */
  4028. #endif /* CONFIG_AUDIT */
  4029. /**
  4030. * smack_ismaclabel - check if xattr @name references a smack MAC label
  4031. * @name: Full xattr name to check.
  4032. */
  4033. static int smack_ismaclabel(const char *name)
  4034. {
  4035. return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
  4036. }
  4037. /**
  4038. * smack_secid_to_secctx - return the smack label for a secid
  4039. * @secid: incoming integer
  4040. * @secdata: destination
  4041. * @seclen: how long it is
  4042. *
  4043. * Exists for networking code.
  4044. */
  4045. static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  4046. {
  4047. struct smack_known *skp = smack_from_secid(secid);
  4048. if (secdata)
  4049. *secdata = skp->smk_known;
  4050. *seclen = strlen(skp->smk_known);
  4051. return 0;
  4052. }
  4053. /**
  4054. * smack_secctx_to_secid - return the secid for a smack label
  4055. * @secdata: smack label
  4056. * @seclen: how long result is
  4057. * @secid: outgoing integer
  4058. *
  4059. * Exists for audit and networking code.
  4060. */
  4061. static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  4062. {
  4063. struct smack_known *skp = smk_find_entry(secdata);
  4064. if (skp)
  4065. *secid = skp->smk_secid;
  4066. else
  4067. *secid = 0;
  4068. return 0;
  4069. }
  4070. /*
  4071. * There used to be a smack_release_secctx hook
  4072. * that did nothing back when hooks were in a vector.
  4073. * Now that there's a list such a hook adds cost.
  4074. */
  4075. static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  4076. {
  4077. return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
  4078. }
  4079. static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  4080. {
  4081. return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
  4082. }
  4083. static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  4084. {
  4085. int len = 0;
  4086. len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
  4087. if (len < 0)
  4088. return len;
  4089. *ctxlen = len;
  4090. return 0;
  4091. }
  4092. static struct security_hook_list smack_hooks[] = {
  4093. LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
  4094. LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
  4095. LSM_HOOK_INIT(syslog, smack_syslog),
  4096. LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
  4097. LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
  4098. LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
  4099. LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
  4100. LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
  4101. LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
  4102. LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
  4103. LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
  4104. LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
  4105. LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
  4106. LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
  4107. LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
  4108. LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
  4109. LSM_HOOK_INIT(inode_link, smack_inode_link),
  4110. LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
  4111. LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
  4112. LSM_HOOK_INIT(inode_rename, smack_inode_rename),
  4113. LSM_HOOK_INIT(inode_permission, smack_inode_permission),
  4114. LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
  4115. LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
  4116. LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
  4117. LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
  4118. LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
  4119. LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
  4120. LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
  4121. LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
  4122. LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
  4123. LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
  4124. LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
  4125. LSM_HOOK_INIT(file_free_security, smack_file_free_security),
  4126. LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
  4127. LSM_HOOK_INIT(file_lock, smack_file_lock),
  4128. LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
  4129. LSM_HOOK_INIT(mmap_file, smack_mmap_file),
  4130. LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
  4131. LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
  4132. LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
  4133. LSM_HOOK_INIT(file_receive, smack_file_receive),
  4134. LSM_HOOK_INIT(file_open, smack_file_open),
  4135. LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
  4136. LSM_HOOK_INIT(cred_free, smack_cred_free),
  4137. LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
  4138. LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
  4139. LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
  4140. LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
  4141. LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
  4142. LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
  4143. LSM_HOOK_INIT(task_getsid, smack_task_getsid),
  4144. LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
  4145. LSM_HOOK_INIT(task_setnice, smack_task_setnice),
  4146. LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
  4147. LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
  4148. LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
  4149. LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
  4150. LSM_HOOK_INIT(task_movememory, smack_task_movememory),
  4151. LSM_HOOK_INIT(task_kill, smack_task_kill),
  4152. LSM_HOOK_INIT(task_wait, smack_task_wait),
  4153. LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
  4154. LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
  4155. LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
  4156. LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
  4157. LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
  4158. LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
  4159. LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
  4160. LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
  4161. LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
  4162. LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
  4163. LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
  4164. LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
  4165. LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
  4166. LSM_HOOK_INIT(shm_associate, smack_shm_associate),
  4167. LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
  4168. LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
  4169. LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
  4170. LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
  4171. LSM_HOOK_INIT(sem_associate, smack_sem_associate),
  4172. LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
  4173. LSM_HOOK_INIT(sem_semop, smack_sem_semop),
  4174. LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
  4175. LSM_HOOK_INIT(getprocattr, smack_getprocattr),
  4176. LSM_HOOK_INIT(setprocattr, smack_setprocattr),
  4177. LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
  4178. LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
  4179. LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
  4180. #ifdef SMACK_IPV6_PORT_LABELING
  4181. LSM_HOOK_INIT(socket_bind, smack_socket_bind),
  4182. #endif
  4183. LSM_HOOK_INIT(socket_connect, smack_socket_connect),
  4184. LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
  4185. LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
  4186. LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
  4187. LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
  4188. LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
  4189. LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
  4190. LSM_HOOK_INIT(sock_graft, smack_sock_graft),
  4191. LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
  4192. LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
  4193. /* key management security hooks */
  4194. #ifdef CONFIG_KEYS
  4195. LSM_HOOK_INIT(key_alloc, smack_key_alloc),
  4196. LSM_HOOK_INIT(key_free, smack_key_free),
  4197. LSM_HOOK_INIT(key_permission, smack_key_permission),
  4198. LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
  4199. #endif /* CONFIG_KEYS */
  4200. /* Audit hooks */
  4201. #ifdef CONFIG_AUDIT
  4202. LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
  4203. LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
  4204. LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
  4205. #endif /* CONFIG_AUDIT */
  4206. LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
  4207. LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
  4208. LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
  4209. LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
  4210. LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
  4211. LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
  4212. };
  4213. static __init void init_smack_known_list(void)
  4214. {
  4215. /*
  4216. * Initialize rule list locks
  4217. */
  4218. mutex_init(&smack_known_huh.smk_rules_lock);
  4219. mutex_init(&smack_known_hat.smk_rules_lock);
  4220. mutex_init(&smack_known_floor.smk_rules_lock);
  4221. mutex_init(&smack_known_star.smk_rules_lock);
  4222. mutex_init(&smack_known_invalid.smk_rules_lock);
  4223. mutex_init(&smack_known_web.smk_rules_lock);
  4224. /*
  4225. * Initialize rule lists
  4226. */
  4227. INIT_LIST_HEAD(&smack_known_huh.smk_rules);
  4228. INIT_LIST_HEAD(&smack_known_hat.smk_rules);
  4229. INIT_LIST_HEAD(&smack_known_star.smk_rules);
  4230. INIT_LIST_HEAD(&smack_known_floor.smk_rules);
  4231. INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
  4232. INIT_LIST_HEAD(&smack_known_web.smk_rules);
  4233. /*
  4234. * Create the known labels list
  4235. */
  4236. smk_insert_entry(&smack_known_huh);
  4237. smk_insert_entry(&smack_known_hat);
  4238. smk_insert_entry(&smack_known_star);
  4239. smk_insert_entry(&smack_known_floor);
  4240. smk_insert_entry(&smack_known_invalid);
  4241. smk_insert_entry(&smack_known_web);
  4242. }
  4243. /**
  4244. * smack_init - initialize the smack system
  4245. *
  4246. * Returns 0
  4247. */
  4248. static __init int smack_init(void)
  4249. {
  4250. struct cred *cred;
  4251. struct task_smack *tsp;
  4252. if (!security_module_enable("smack"))
  4253. return 0;
  4254. smack_inode_cache = KMEM_CACHE(inode_smack, 0);
  4255. if (!smack_inode_cache)
  4256. return -ENOMEM;
  4257. tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
  4258. GFP_KERNEL);
  4259. if (tsp == NULL) {
  4260. kmem_cache_destroy(smack_inode_cache);
  4261. return -ENOMEM;
  4262. }
  4263. smack_enabled = 1;
  4264. pr_info("Smack: Initializing.\n");
  4265. #ifdef CONFIG_SECURITY_SMACK_NETFILTER
  4266. pr_info("Smack: Netfilter enabled.\n");
  4267. #endif
  4268. #ifdef SMACK_IPV6_PORT_LABELING
  4269. pr_info("Smack: IPv6 port labeling enabled.\n");
  4270. #endif
  4271. #ifdef SMACK_IPV6_SECMARK_LABELING
  4272. pr_info("Smack: IPv6 Netfilter enabled.\n");
  4273. #endif
  4274. /*
  4275. * Set the security state for the initial task.
  4276. */
  4277. cred = (struct cred *) current->cred;
  4278. cred->security = tsp;
  4279. /* initialize the smack_known_list */
  4280. init_smack_known_list();
  4281. /*
  4282. * Register with LSM
  4283. */
  4284. security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
  4285. return 0;
  4286. }
  4287. /*
  4288. * Smack requires early initialization in order to label
  4289. * all processes and objects when they are created.
  4290. */
  4291. security_initcall(smack_init);