smack_lsm.c 101 KB

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