smack_lsm.c 117 KB

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