smack_lsm.c 118 KB

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