smack_lsm.c 92 KB

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