smack_lsm.c 116 KB

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