smack_lsm.c 107 KB

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