smack_lsm.c 101 KB

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