smack_lsm.c 116 KB

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