smack_lsm.c 118 KB

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