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, mode, &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, const 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 = XATTR_SMACK_SUFFIX;
  509. if (value) {
  510. rcu_read_lock();
  511. may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
  512. rcu_read_unlock();
  513. /*
  514. * If the access rule allows transmutation and
  515. * the directory requests transmutation then
  516. * by all means transmute.
  517. * Mark the inode as changed.
  518. */
  519. if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
  520. smk_inode_transmutable(dir)) {
  521. isp = dsp;
  522. issp->smk_flags |= SMK_INODE_CHANGED;
  523. }
  524. *value = kstrdup(isp, GFP_NOFS);
  525. if (*value == NULL)
  526. return -ENOMEM;
  527. }
  528. if (len)
  529. *len = strlen(isp) + 1;
  530. return 0;
  531. }
  532. /**
  533. * smack_inode_link - Smack check on link
  534. * @old_dentry: the existing object
  535. * @dir: unused
  536. * @new_dentry: the new object
  537. *
  538. * Returns 0 if access is permitted, an error code otherwise
  539. */
  540. static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
  541. struct dentry *new_dentry)
  542. {
  543. char *isp;
  544. struct smk_audit_info ad;
  545. int rc;
  546. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  547. smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
  548. isp = smk_of_inode(old_dentry->d_inode);
  549. rc = smk_curacc(isp, MAY_WRITE, &ad);
  550. if (rc == 0 && new_dentry->d_inode != NULL) {
  551. isp = smk_of_inode(new_dentry->d_inode);
  552. smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
  553. rc = smk_curacc(isp, MAY_WRITE, &ad);
  554. }
  555. return rc;
  556. }
  557. /**
  558. * smack_inode_unlink - Smack check on inode deletion
  559. * @dir: containing directory object
  560. * @dentry: file to unlink
  561. *
  562. * Returns 0 if current can write the containing directory
  563. * and the object, error code otherwise
  564. */
  565. static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
  566. {
  567. struct inode *ip = dentry->d_inode;
  568. struct smk_audit_info ad;
  569. int rc;
  570. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  571. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  572. /*
  573. * You need write access to the thing you're unlinking
  574. */
  575. rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
  576. if (rc == 0) {
  577. /*
  578. * You also need write access to the containing directory
  579. */
  580. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  581. smk_ad_setfield_u_fs_inode(&ad, dir);
  582. rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
  583. }
  584. return rc;
  585. }
  586. /**
  587. * smack_inode_rmdir - Smack check on directory deletion
  588. * @dir: containing directory object
  589. * @dentry: directory to unlink
  590. *
  591. * Returns 0 if current can write the containing directory
  592. * and the directory, error code otherwise
  593. */
  594. static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
  595. {
  596. struct smk_audit_info ad;
  597. int rc;
  598. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  599. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  600. /*
  601. * You need write access to the thing you're removing
  602. */
  603. rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
  604. if (rc == 0) {
  605. /*
  606. * You also need write access to the containing directory
  607. */
  608. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  609. smk_ad_setfield_u_fs_inode(&ad, dir);
  610. rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
  611. }
  612. return rc;
  613. }
  614. /**
  615. * smack_inode_rename - Smack check on rename
  616. * @old_inode: the old directory
  617. * @old_dentry: unused
  618. * @new_inode: the new directory
  619. * @new_dentry: unused
  620. *
  621. * Read and write access is required on both the old and
  622. * new directories.
  623. *
  624. * Returns 0 if access is permitted, an error code otherwise
  625. */
  626. static int smack_inode_rename(struct inode *old_inode,
  627. struct dentry *old_dentry,
  628. struct inode *new_inode,
  629. struct dentry *new_dentry)
  630. {
  631. int rc;
  632. char *isp;
  633. struct smk_audit_info ad;
  634. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  635. smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
  636. isp = smk_of_inode(old_dentry->d_inode);
  637. rc = smk_curacc(isp, MAY_READWRITE, &ad);
  638. if (rc == 0 && new_dentry->d_inode != NULL) {
  639. isp = smk_of_inode(new_dentry->d_inode);
  640. smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
  641. rc = smk_curacc(isp, MAY_READWRITE, &ad);
  642. }
  643. return rc;
  644. }
  645. /**
  646. * smack_inode_permission - Smack version of permission()
  647. * @inode: the inode in question
  648. * @mask: the access requested
  649. *
  650. * This is the important Smack hook.
  651. *
  652. * Returns 0 if access is permitted, -EACCES otherwise
  653. */
  654. static int smack_inode_permission(struct inode *inode, int mask)
  655. {
  656. struct smk_audit_info ad;
  657. int no_block = mask & MAY_NOT_BLOCK;
  658. mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
  659. /*
  660. * No permission to check. Existence test. Yup, it's there.
  661. */
  662. if (mask == 0)
  663. return 0;
  664. /* May be droppable after audit */
  665. if (no_block)
  666. return -ECHILD;
  667. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
  668. smk_ad_setfield_u_fs_inode(&ad, inode);
  669. return smk_curacc(smk_of_inode(inode), mask, &ad);
  670. }
  671. /**
  672. * smack_inode_setattr - Smack check for setting attributes
  673. * @dentry: the object
  674. * @iattr: for the force flag
  675. *
  676. * Returns 0 if access is permitted, an error code otherwise
  677. */
  678. static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
  679. {
  680. struct smk_audit_info ad;
  681. /*
  682. * Need to allow for clearing the setuid bit.
  683. */
  684. if (iattr->ia_valid & ATTR_FORCE)
  685. return 0;
  686. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  687. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  688. return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
  689. }
  690. /**
  691. * smack_inode_getattr - Smack check for getting attributes
  692. * @mnt: unused
  693. * @dentry: the object
  694. *
  695. * Returns 0 if access is permitted, an error code otherwise
  696. */
  697. static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
  698. {
  699. struct smk_audit_info ad;
  700. struct path path;
  701. path.dentry = dentry;
  702. path.mnt = mnt;
  703. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  704. smk_ad_setfield_u_fs_path(&ad, path);
  705. return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
  706. }
  707. /**
  708. * smack_inode_setxattr - Smack check for setting xattrs
  709. * @dentry: the object
  710. * @name: name of the attribute
  711. * @value: unused
  712. * @size: unused
  713. * @flags: unused
  714. *
  715. * This protects the Smack attribute explicitly.
  716. *
  717. * Returns 0 if access is permitted, an error code otherwise
  718. */
  719. static int smack_inode_setxattr(struct dentry *dentry, const char *name,
  720. const void *value, size_t size, int flags)
  721. {
  722. struct smk_audit_info ad;
  723. int rc = 0;
  724. if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
  725. strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
  726. strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
  727. strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
  728. strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
  729. if (!smack_privileged(CAP_MAC_ADMIN))
  730. rc = -EPERM;
  731. /*
  732. * check label validity here so import wont fail on
  733. * post_setxattr
  734. */
  735. if (size == 0 || size >= SMK_LONGLABEL ||
  736. smk_import(value, size) == NULL)
  737. rc = -EINVAL;
  738. } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
  739. if (!smack_privileged(CAP_MAC_ADMIN))
  740. rc = -EPERM;
  741. if (size != TRANS_TRUE_SIZE ||
  742. strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
  743. rc = -EINVAL;
  744. } else
  745. rc = cap_inode_setxattr(dentry, name, value, size, flags);
  746. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  747. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  748. if (rc == 0)
  749. rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
  750. return rc;
  751. }
  752. /**
  753. * smack_inode_post_setxattr - Apply the Smack update approved above
  754. * @dentry: object
  755. * @name: attribute name
  756. * @value: attribute value
  757. * @size: attribute size
  758. * @flags: unused
  759. *
  760. * Set the pointer in the inode blob to the entry found
  761. * in the master label list.
  762. */
  763. static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
  764. const void *value, size_t size, int flags)
  765. {
  766. struct smack_known *skp;
  767. struct inode_smack *isp = dentry->d_inode->i_security;
  768. if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
  769. isp->smk_flags |= SMK_INODE_TRANSMUTE;
  770. return;
  771. }
  772. skp = smk_import_entry(value, size);
  773. if (strcmp(name, XATTR_NAME_SMACK) == 0) {
  774. if (skp != NULL)
  775. isp->smk_inode = skp->smk_known;
  776. else
  777. isp->smk_inode = smack_known_invalid.smk_known;
  778. } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
  779. if (skp != NULL)
  780. isp->smk_task = skp;
  781. else
  782. isp->smk_task = &smack_known_invalid;
  783. } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
  784. if (skp != NULL)
  785. isp->smk_mmap = skp;
  786. else
  787. isp->smk_mmap = &smack_known_invalid;
  788. }
  789. return;
  790. }
  791. /**
  792. * smack_inode_getxattr - Smack check on getxattr
  793. * @dentry: the object
  794. * @name: unused
  795. *
  796. * Returns 0 if access is permitted, an error code otherwise
  797. */
  798. static int smack_inode_getxattr(struct dentry *dentry, const char *name)
  799. {
  800. struct smk_audit_info ad;
  801. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  802. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  803. return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
  804. }
  805. /**
  806. * smack_inode_removexattr - Smack check on removexattr
  807. * @dentry: the object
  808. * @name: name of the attribute
  809. *
  810. * Removing the Smack attribute requires CAP_MAC_ADMIN
  811. *
  812. * Returns 0 if access is permitted, an error code otherwise
  813. */
  814. static int smack_inode_removexattr(struct dentry *dentry, const char *name)
  815. {
  816. struct inode_smack *isp;
  817. struct smk_audit_info ad;
  818. int rc = 0;
  819. if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
  820. strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
  821. strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
  822. strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
  823. strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
  824. strcmp(name, XATTR_NAME_SMACKMMAP)) {
  825. if (!smack_privileged(CAP_MAC_ADMIN))
  826. rc = -EPERM;
  827. } else
  828. rc = cap_inode_removexattr(dentry, name);
  829. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
  830. smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
  831. if (rc == 0)
  832. rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
  833. if (rc == 0) {
  834. isp = dentry->d_inode->i_security;
  835. isp->smk_task = NULL;
  836. isp->smk_mmap = NULL;
  837. }
  838. return rc;
  839. }
  840. /**
  841. * smack_inode_getsecurity - get smack xattrs
  842. * @inode: the object
  843. * @name: attribute name
  844. * @buffer: where to put the result
  845. * @alloc: unused
  846. *
  847. * Returns the size of the attribute or an error code
  848. */
  849. static int smack_inode_getsecurity(const struct inode *inode,
  850. const char *name, void **buffer,
  851. bool alloc)
  852. {
  853. struct socket_smack *ssp;
  854. struct socket *sock;
  855. struct super_block *sbp;
  856. struct inode *ip = (struct inode *)inode;
  857. char *isp;
  858. int ilen;
  859. int rc = 0;
  860. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
  861. isp = smk_of_inode(inode);
  862. ilen = strlen(isp) + 1;
  863. *buffer = isp;
  864. return ilen;
  865. }
  866. /*
  867. * The rest of the Smack xattrs are only on sockets.
  868. */
  869. sbp = ip->i_sb;
  870. if (sbp->s_magic != SOCKFS_MAGIC)
  871. return -EOPNOTSUPP;
  872. sock = SOCKET_I(ip);
  873. if (sock == NULL || sock->sk == NULL)
  874. return -EOPNOTSUPP;
  875. ssp = sock->sk->sk_security;
  876. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  877. isp = ssp->smk_in;
  878. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
  879. isp = ssp->smk_out->smk_known;
  880. else
  881. return -EOPNOTSUPP;
  882. ilen = strlen(isp) + 1;
  883. if (rc == 0) {
  884. *buffer = isp;
  885. rc = ilen;
  886. }
  887. return rc;
  888. }
  889. /**
  890. * smack_inode_listsecurity - list the Smack attributes
  891. * @inode: the object
  892. * @buffer: where they go
  893. * @buffer_size: size of buffer
  894. *
  895. * Returns 0 on success, -EINVAL otherwise
  896. */
  897. static int smack_inode_listsecurity(struct inode *inode, char *buffer,
  898. size_t buffer_size)
  899. {
  900. int len = strlen(XATTR_NAME_SMACK);
  901. if (buffer != NULL && len <= buffer_size) {
  902. memcpy(buffer, XATTR_NAME_SMACK, len);
  903. return len;
  904. }
  905. return -EINVAL;
  906. }
  907. /**
  908. * smack_inode_getsecid - Extract inode's security id
  909. * @inode: inode to extract the info from
  910. * @secid: where result will be saved
  911. */
  912. static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
  913. {
  914. struct inode_smack *isp = inode->i_security;
  915. *secid = smack_to_secid(isp->smk_inode);
  916. }
  917. /*
  918. * File Hooks
  919. */
  920. /**
  921. * smack_file_permission - Smack check on file operations
  922. * @file: unused
  923. * @mask: unused
  924. *
  925. * Returns 0
  926. *
  927. * Should access checks be done on each read or write?
  928. * UNICOS and SELinux say yes.
  929. * Trusted Solaris, Trusted Irix, and just about everyone else says no.
  930. *
  931. * I'll say no for now. Smack does not do the frequent
  932. * label changing that SELinux does.
  933. */
  934. static int smack_file_permission(struct file *file, int mask)
  935. {
  936. return 0;
  937. }
  938. /**
  939. * smack_file_alloc_security - assign a file security blob
  940. * @file: the object
  941. *
  942. * The security blob for a file is a pointer to the master
  943. * label list, so no allocation is done.
  944. *
  945. * Returns 0
  946. */
  947. static int smack_file_alloc_security(struct file *file)
  948. {
  949. struct smack_known *skp = smk_of_current();
  950. file->f_security = skp->smk_known;
  951. return 0;
  952. }
  953. /**
  954. * smack_file_free_security - clear a file security blob
  955. * @file: the object
  956. *
  957. * The security blob for a file is a pointer to the master
  958. * label list, so no memory is freed.
  959. */
  960. static void smack_file_free_security(struct file *file)
  961. {
  962. file->f_security = NULL;
  963. }
  964. /**
  965. * smack_file_ioctl - Smack check on ioctls
  966. * @file: the object
  967. * @cmd: what to do
  968. * @arg: unused
  969. *
  970. * Relies heavily on the correct use of the ioctl command conventions.
  971. *
  972. * Returns 0 if allowed, error code otherwise
  973. */
  974. static int smack_file_ioctl(struct file *file, unsigned int cmd,
  975. unsigned long arg)
  976. {
  977. int rc = 0;
  978. struct smk_audit_info ad;
  979. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  980. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  981. if (_IOC_DIR(cmd) & _IOC_WRITE)
  982. rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
  983. if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
  984. rc = smk_curacc(file->f_security, MAY_READ, &ad);
  985. return rc;
  986. }
  987. /**
  988. * smack_file_lock - Smack check on file locking
  989. * @file: the object
  990. * @cmd: unused
  991. *
  992. * Returns 0 if current has lock access, error code otherwise
  993. */
  994. static int smack_file_lock(struct file *file, unsigned int cmd)
  995. {
  996. struct smk_audit_info ad;
  997. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  998. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  999. return smk_curacc(file->f_security, MAY_LOCK, &ad);
  1000. }
  1001. /**
  1002. * smack_file_fcntl - Smack check on fcntl
  1003. * @file: the object
  1004. * @cmd: what action to check
  1005. * @arg: unused
  1006. *
  1007. * Generally these operations are harmless.
  1008. * File locking operations present an obvious mechanism
  1009. * for passing information, so they require write access.
  1010. *
  1011. * Returns 0 if current has access, error code otherwise
  1012. */
  1013. static int smack_file_fcntl(struct file *file, unsigned int cmd,
  1014. unsigned long arg)
  1015. {
  1016. struct smk_audit_info ad;
  1017. int rc = 0;
  1018. switch (cmd) {
  1019. case F_GETLK:
  1020. break;
  1021. case F_SETLK:
  1022. case F_SETLKW:
  1023. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1024. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1025. rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
  1026. break;
  1027. case F_SETOWN:
  1028. case F_SETSIG:
  1029. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
  1030. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1031. rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. return rc;
  1037. }
  1038. /**
  1039. * smack_mmap_file :
  1040. * Check permissions for a mmap operation. The @file may be NULL, e.g.
  1041. * if mapping anonymous memory.
  1042. * @file contains the file structure for file to map (may be NULL).
  1043. * @reqprot contains the protection requested by the application.
  1044. * @prot contains the protection that will be applied by the kernel.
  1045. * @flags contains the operational flags.
  1046. * Return 0 if permission is granted.
  1047. */
  1048. static int smack_mmap_file(struct file *file,
  1049. unsigned long reqprot, unsigned long prot,
  1050. unsigned long flags)
  1051. {
  1052. struct smack_known *skp;
  1053. struct smack_known *mkp;
  1054. struct smack_rule *srp;
  1055. struct task_smack *tsp;
  1056. char *osmack;
  1057. struct inode_smack *isp;
  1058. int may;
  1059. int mmay;
  1060. int tmay;
  1061. int rc;
  1062. if (file == NULL)
  1063. return 0;
  1064. isp = file_inode(file)->i_security;
  1065. if (isp->smk_mmap == NULL)
  1066. return 0;
  1067. mkp = isp->smk_mmap;
  1068. tsp = current_security();
  1069. skp = smk_of_current();
  1070. rc = 0;
  1071. rcu_read_lock();
  1072. /*
  1073. * For each Smack rule associated with the subject
  1074. * label verify that the SMACK64MMAP also has access
  1075. * to that rule's object label.
  1076. */
  1077. list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
  1078. osmack = srp->smk_object;
  1079. /*
  1080. * Matching labels always allows access.
  1081. */
  1082. if (mkp->smk_known == osmack)
  1083. continue;
  1084. /*
  1085. * If there is a matching local rule take
  1086. * that into account as well.
  1087. */
  1088. may = smk_access_entry(srp->smk_subject->smk_known, osmack,
  1089. &tsp->smk_rules);
  1090. if (may == -ENOENT)
  1091. may = srp->smk_access;
  1092. else
  1093. may &= srp->smk_access;
  1094. /*
  1095. * If may is zero the SMACK64MMAP subject can't
  1096. * possibly have less access.
  1097. */
  1098. if (may == 0)
  1099. continue;
  1100. /*
  1101. * Fetch the global list entry.
  1102. * If there isn't one a SMACK64MMAP subject
  1103. * can't have as much access as current.
  1104. */
  1105. mmay = smk_access_entry(mkp->smk_known, osmack,
  1106. &mkp->smk_rules);
  1107. if (mmay == -ENOENT) {
  1108. rc = -EACCES;
  1109. break;
  1110. }
  1111. /*
  1112. * If there is a local entry it modifies the
  1113. * potential access, too.
  1114. */
  1115. tmay = smk_access_entry(mkp->smk_known, osmack,
  1116. &tsp->smk_rules);
  1117. if (tmay != -ENOENT)
  1118. mmay &= tmay;
  1119. /*
  1120. * If there is any access available to current that is
  1121. * not available to a SMACK64MMAP subject
  1122. * deny access.
  1123. */
  1124. if ((may | mmay) != mmay) {
  1125. rc = -EACCES;
  1126. break;
  1127. }
  1128. }
  1129. rcu_read_unlock();
  1130. return rc;
  1131. }
  1132. /**
  1133. * smack_file_set_fowner - set the file security blob value
  1134. * @file: object in question
  1135. *
  1136. * Returns 0
  1137. * Further research may be required on this one.
  1138. */
  1139. static int smack_file_set_fowner(struct file *file)
  1140. {
  1141. struct smack_known *skp = smk_of_current();
  1142. file->f_security = skp->smk_known;
  1143. return 0;
  1144. }
  1145. /**
  1146. * smack_file_send_sigiotask - Smack on sigio
  1147. * @tsk: The target task
  1148. * @fown: the object the signal come from
  1149. * @signum: unused
  1150. *
  1151. * Allow a privileged task to get signals even if it shouldn't
  1152. *
  1153. * Returns 0 if a subject with the object's smack could
  1154. * write to the task, an error code otherwise.
  1155. */
  1156. static int smack_file_send_sigiotask(struct task_struct *tsk,
  1157. struct fown_struct *fown, int signum)
  1158. {
  1159. struct smack_known *skp;
  1160. struct smack_known *tkp = smk_of_task(tsk->cred->security);
  1161. struct file *file;
  1162. int rc;
  1163. struct smk_audit_info ad;
  1164. /*
  1165. * struct fown_struct is never outside the context of a struct file
  1166. */
  1167. file = container_of(fown, struct file, f_owner);
  1168. /* we don't log here as rc can be overriden */
  1169. skp = smk_find_entry(file->f_security);
  1170. rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
  1171. if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
  1172. rc = 0;
  1173. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1174. smk_ad_setfield_u_tsk(&ad, tsk);
  1175. smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
  1176. return rc;
  1177. }
  1178. /**
  1179. * smack_file_receive - Smack file receive check
  1180. * @file: the object
  1181. *
  1182. * Returns 0 if current has access, error code otherwise
  1183. */
  1184. static int smack_file_receive(struct file *file)
  1185. {
  1186. int may = 0;
  1187. struct smk_audit_info ad;
  1188. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1189. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1190. /*
  1191. * This code relies on bitmasks.
  1192. */
  1193. if (file->f_mode & FMODE_READ)
  1194. may = MAY_READ;
  1195. if (file->f_mode & FMODE_WRITE)
  1196. may |= MAY_WRITE;
  1197. return smk_curacc(file->f_security, may, &ad);
  1198. }
  1199. /**
  1200. * smack_file_open - Smack dentry open processing
  1201. * @file: the object
  1202. * @cred: unused
  1203. *
  1204. * Set the security blob in the file structure.
  1205. *
  1206. * Returns 0
  1207. */
  1208. static int smack_file_open(struct file *file, const struct cred *cred)
  1209. {
  1210. struct inode_smack *isp = file_inode(file)->i_security;
  1211. file->f_security = isp->smk_inode;
  1212. return 0;
  1213. }
  1214. /*
  1215. * Task hooks
  1216. */
  1217. /**
  1218. * smack_cred_alloc_blank - "allocate" blank task-level security credentials
  1219. * @new: the new credentials
  1220. * @gfp: the atomicity of any memory allocations
  1221. *
  1222. * Prepare a blank set of credentials for modification. This must allocate all
  1223. * the memory the LSM module might require such that cred_transfer() can
  1224. * complete without error.
  1225. */
  1226. static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  1227. {
  1228. struct task_smack *tsp;
  1229. tsp = new_task_smack(NULL, NULL, gfp);
  1230. if (tsp == NULL)
  1231. return -ENOMEM;
  1232. cred->security = tsp;
  1233. return 0;
  1234. }
  1235. /**
  1236. * smack_cred_free - "free" task-level security credentials
  1237. * @cred: the credentials in question
  1238. *
  1239. */
  1240. static void smack_cred_free(struct cred *cred)
  1241. {
  1242. struct task_smack *tsp = cred->security;
  1243. struct smack_rule *rp;
  1244. struct list_head *l;
  1245. struct list_head *n;
  1246. if (tsp == NULL)
  1247. return;
  1248. cred->security = NULL;
  1249. list_for_each_safe(l, n, &tsp->smk_rules) {
  1250. rp = list_entry(l, struct smack_rule, list);
  1251. list_del(&rp->list);
  1252. kfree(rp);
  1253. }
  1254. kfree(tsp);
  1255. }
  1256. /**
  1257. * smack_cred_prepare - prepare new set of credentials for modification
  1258. * @new: the new credentials
  1259. * @old: the original credentials
  1260. * @gfp: the atomicity of any memory allocations
  1261. *
  1262. * Prepare a new set of credentials for modification.
  1263. */
  1264. static int smack_cred_prepare(struct cred *new, const struct cred *old,
  1265. gfp_t gfp)
  1266. {
  1267. struct task_smack *old_tsp = old->security;
  1268. struct task_smack *new_tsp;
  1269. int rc;
  1270. new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
  1271. if (new_tsp == NULL)
  1272. return -ENOMEM;
  1273. rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
  1274. if (rc != 0)
  1275. return rc;
  1276. new->security = new_tsp;
  1277. return 0;
  1278. }
  1279. /**
  1280. * smack_cred_transfer - Transfer the old credentials to the new credentials
  1281. * @new: the new credentials
  1282. * @old: the original credentials
  1283. *
  1284. * Fill in a set of blank credentials from another set of credentials.
  1285. */
  1286. static void smack_cred_transfer(struct cred *new, const struct cred *old)
  1287. {
  1288. struct task_smack *old_tsp = old->security;
  1289. struct task_smack *new_tsp = new->security;
  1290. new_tsp->smk_task = old_tsp->smk_task;
  1291. new_tsp->smk_forked = old_tsp->smk_task;
  1292. mutex_init(&new_tsp->smk_rules_lock);
  1293. INIT_LIST_HEAD(&new_tsp->smk_rules);
  1294. /* cbs copy rule list */
  1295. }
  1296. /**
  1297. * smack_kernel_act_as - Set the subjective context in a set of credentials
  1298. * @new: points to the set of credentials to be modified.
  1299. * @secid: specifies the security ID to be set
  1300. *
  1301. * Set the security data for a kernel service.
  1302. */
  1303. static int smack_kernel_act_as(struct cred *new, u32 secid)
  1304. {
  1305. struct task_smack *new_tsp = new->security;
  1306. struct smack_known *skp = smack_from_secid(secid);
  1307. if (skp == NULL)
  1308. return -EINVAL;
  1309. new_tsp->smk_task = skp;
  1310. return 0;
  1311. }
  1312. /**
  1313. * smack_kernel_create_files_as - Set the file creation label in a set of creds
  1314. * @new: points to the set of credentials to be modified
  1315. * @inode: points to the inode to use as a reference
  1316. *
  1317. * Set the file creation context in a set of credentials to the same
  1318. * as the objective context of the specified inode
  1319. */
  1320. static int smack_kernel_create_files_as(struct cred *new,
  1321. struct inode *inode)
  1322. {
  1323. struct inode_smack *isp = inode->i_security;
  1324. struct task_smack *tsp = new->security;
  1325. tsp->smk_forked = smk_find_entry(isp->smk_inode);
  1326. tsp->smk_task = tsp->smk_forked;
  1327. return 0;
  1328. }
  1329. /**
  1330. * smk_curacc_on_task - helper to log task related access
  1331. * @p: the task object
  1332. * @access: the access requested
  1333. * @caller: name of the calling function for audit
  1334. *
  1335. * Return 0 if access is permitted
  1336. */
  1337. static int smk_curacc_on_task(struct task_struct *p, int access,
  1338. const char *caller)
  1339. {
  1340. struct smk_audit_info ad;
  1341. struct smack_known *skp = smk_of_task(task_security(p));
  1342. smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
  1343. smk_ad_setfield_u_tsk(&ad, p);
  1344. return smk_curacc(skp->smk_known, access, &ad);
  1345. }
  1346. /**
  1347. * smack_task_setpgid - Smack check on setting pgid
  1348. * @p: the task object
  1349. * @pgid: unused
  1350. *
  1351. * Return 0 if write access is permitted
  1352. */
  1353. static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
  1354. {
  1355. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1356. }
  1357. /**
  1358. * smack_task_getpgid - Smack access check for getpgid
  1359. * @p: the object task
  1360. *
  1361. * Returns 0 if current can read the object task, error code otherwise
  1362. */
  1363. static int smack_task_getpgid(struct task_struct *p)
  1364. {
  1365. return smk_curacc_on_task(p, MAY_READ, __func__);
  1366. }
  1367. /**
  1368. * smack_task_getsid - Smack access check for getsid
  1369. * @p: the object task
  1370. *
  1371. * Returns 0 if current can read the object task, error code otherwise
  1372. */
  1373. static int smack_task_getsid(struct task_struct *p)
  1374. {
  1375. return smk_curacc_on_task(p, MAY_READ, __func__);
  1376. }
  1377. /**
  1378. * smack_task_getsecid - get the secid of the task
  1379. * @p: the object task
  1380. * @secid: where to put the result
  1381. *
  1382. * Sets the secid to contain a u32 version of the smack label.
  1383. */
  1384. static void smack_task_getsecid(struct task_struct *p, u32 *secid)
  1385. {
  1386. struct smack_known *skp = smk_of_task(task_security(p));
  1387. *secid = skp->smk_secid;
  1388. }
  1389. /**
  1390. * smack_task_setnice - Smack check on setting nice
  1391. * @p: the task object
  1392. * @nice: unused
  1393. *
  1394. * Return 0 if write access is permitted
  1395. */
  1396. static int smack_task_setnice(struct task_struct *p, int nice)
  1397. {
  1398. int rc;
  1399. rc = cap_task_setnice(p, nice);
  1400. if (rc == 0)
  1401. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1402. return rc;
  1403. }
  1404. /**
  1405. * smack_task_setioprio - Smack check on setting ioprio
  1406. * @p: the task object
  1407. * @ioprio: unused
  1408. *
  1409. * Return 0 if write access is permitted
  1410. */
  1411. static int smack_task_setioprio(struct task_struct *p, int ioprio)
  1412. {
  1413. int rc;
  1414. rc = cap_task_setioprio(p, ioprio);
  1415. if (rc == 0)
  1416. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1417. return rc;
  1418. }
  1419. /**
  1420. * smack_task_getioprio - Smack check on reading ioprio
  1421. * @p: the task object
  1422. *
  1423. * Return 0 if read access is permitted
  1424. */
  1425. static int smack_task_getioprio(struct task_struct *p)
  1426. {
  1427. return smk_curacc_on_task(p, MAY_READ, __func__);
  1428. }
  1429. /**
  1430. * smack_task_setscheduler - Smack check on setting scheduler
  1431. * @p: the task object
  1432. * @policy: unused
  1433. * @lp: unused
  1434. *
  1435. * Return 0 if read access is permitted
  1436. */
  1437. static int smack_task_setscheduler(struct task_struct *p)
  1438. {
  1439. int rc;
  1440. rc = cap_task_setscheduler(p);
  1441. if (rc == 0)
  1442. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1443. return rc;
  1444. }
  1445. /**
  1446. * smack_task_getscheduler - Smack check on reading scheduler
  1447. * @p: the task object
  1448. *
  1449. * Return 0 if read access is permitted
  1450. */
  1451. static int smack_task_getscheduler(struct task_struct *p)
  1452. {
  1453. return smk_curacc_on_task(p, MAY_READ, __func__);
  1454. }
  1455. /**
  1456. * smack_task_movememory - Smack check on moving memory
  1457. * @p: the task object
  1458. *
  1459. * Return 0 if write access is permitted
  1460. */
  1461. static int smack_task_movememory(struct task_struct *p)
  1462. {
  1463. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1464. }
  1465. /**
  1466. * smack_task_kill - Smack check on signal delivery
  1467. * @p: the task object
  1468. * @info: unused
  1469. * @sig: unused
  1470. * @secid: identifies the smack to use in lieu of current's
  1471. *
  1472. * Return 0 if write access is permitted
  1473. *
  1474. * The secid behavior is an artifact of an SELinux hack
  1475. * in the USB code. Someday it may go away.
  1476. */
  1477. static int smack_task_kill(struct task_struct *p, struct siginfo *info,
  1478. int sig, u32 secid)
  1479. {
  1480. struct smk_audit_info ad;
  1481. struct smack_known *skp;
  1482. struct smack_known *tkp = smk_of_task(task_security(p));
  1483. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1484. smk_ad_setfield_u_tsk(&ad, p);
  1485. /*
  1486. * Sending a signal requires that the sender
  1487. * can write the receiver.
  1488. */
  1489. if (secid == 0)
  1490. return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
  1491. /*
  1492. * If the secid isn't 0 we're dealing with some USB IO
  1493. * specific behavior. This is not clean. For one thing
  1494. * we can't take privilege into account.
  1495. */
  1496. skp = smack_from_secid(secid);
  1497. return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
  1498. }
  1499. /**
  1500. * smack_task_wait - Smack access check for waiting
  1501. * @p: task to wait for
  1502. *
  1503. * Returns 0
  1504. */
  1505. static int smack_task_wait(struct task_struct *p)
  1506. {
  1507. /*
  1508. * Allow the operation to succeed.
  1509. * Zombies are bad.
  1510. * In userless environments (e.g. phones) programs
  1511. * get marked with SMACK64EXEC and even if the parent
  1512. * and child shouldn't be talking the parent still
  1513. * may expect to know when the child exits.
  1514. */
  1515. return 0;
  1516. }
  1517. /**
  1518. * smack_task_to_inode - copy task smack into the inode blob
  1519. * @p: task to copy from
  1520. * @inode: inode to copy to
  1521. *
  1522. * Sets the smack pointer in the inode security blob
  1523. */
  1524. static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
  1525. {
  1526. struct inode_smack *isp = inode->i_security;
  1527. struct smack_known *skp = smk_of_task(task_security(p));
  1528. isp->smk_inode = skp->smk_known;
  1529. }
  1530. /*
  1531. * Socket hooks.
  1532. */
  1533. /**
  1534. * smack_sk_alloc_security - Allocate a socket blob
  1535. * @sk: the socket
  1536. * @family: unused
  1537. * @gfp_flags: memory allocation flags
  1538. *
  1539. * Assign Smack pointers to current
  1540. *
  1541. * Returns 0 on success, -ENOMEM is there's no memory
  1542. */
  1543. static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
  1544. {
  1545. struct smack_known *skp = smk_of_current();
  1546. struct socket_smack *ssp;
  1547. ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
  1548. if (ssp == NULL)
  1549. return -ENOMEM;
  1550. ssp->smk_in = skp->smk_known;
  1551. ssp->smk_out = skp;
  1552. ssp->smk_packet = NULL;
  1553. sk->sk_security = ssp;
  1554. return 0;
  1555. }
  1556. /**
  1557. * smack_sk_free_security - Free a socket blob
  1558. * @sk: the socket
  1559. *
  1560. * Clears the blob pointer
  1561. */
  1562. static void smack_sk_free_security(struct sock *sk)
  1563. {
  1564. kfree(sk->sk_security);
  1565. }
  1566. /**
  1567. * smack_host_label - check host based restrictions
  1568. * @sip: the object end
  1569. *
  1570. * looks for host based access restrictions
  1571. *
  1572. * This version will only be appropriate for really small sets of single label
  1573. * hosts. The caller is responsible for ensuring that the RCU read lock is
  1574. * taken before calling this function.
  1575. *
  1576. * Returns the label of the far end or NULL if it's not special.
  1577. */
  1578. static char *smack_host_label(struct sockaddr_in *sip)
  1579. {
  1580. struct smk_netlbladdr *snp;
  1581. struct in_addr *siap = &sip->sin_addr;
  1582. if (siap->s_addr == 0)
  1583. return NULL;
  1584. list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
  1585. /*
  1586. * we break after finding the first match because
  1587. * the list is sorted from longest to shortest mask
  1588. * so we have found the most specific match
  1589. */
  1590. if ((&snp->smk_host.sin_addr)->s_addr ==
  1591. (siap->s_addr & (&snp->smk_mask)->s_addr)) {
  1592. /* we have found the special CIPSO option */
  1593. if (snp->smk_label == smack_cipso_option)
  1594. return NULL;
  1595. return snp->smk_label;
  1596. }
  1597. return NULL;
  1598. }
  1599. /**
  1600. * smack_netlabel - Set the secattr on a socket
  1601. * @sk: the socket
  1602. * @labeled: socket label scheme
  1603. *
  1604. * Convert the outbound smack value (smk_out) to a
  1605. * secattr and attach it to the socket.
  1606. *
  1607. * Returns 0 on success or an error code
  1608. */
  1609. static int smack_netlabel(struct sock *sk, int labeled)
  1610. {
  1611. struct smack_known *skp;
  1612. struct socket_smack *ssp = sk->sk_security;
  1613. int rc = 0;
  1614. /*
  1615. * Usually the netlabel code will handle changing the
  1616. * packet labeling based on the label.
  1617. * The case of a single label host is different, because
  1618. * a single label host should never get a labeled packet
  1619. * even though the label is usually associated with a packet
  1620. * label.
  1621. */
  1622. local_bh_disable();
  1623. bh_lock_sock_nested(sk);
  1624. if (ssp->smk_out == smack_net_ambient ||
  1625. labeled == SMACK_UNLABELED_SOCKET)
  1626. netlbl_sock_delattr(sk);
  1627. else {
  1628. skp = ssp->smk_out;
  1629. rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
  1630. }
  1631. bh_unlock_sock(sk);
  1632. local_bh_enable();
  1633. return rc;
  1634. }
  1635. /**
  1636. * smack_netlbel_send - Set the secattr on a socket and perform access checks
  1637. * @sk: the socket
  1638. * @sap: the destination address
  1639. *
  1640. * Set the correct secattr for the given socket based on the destination
  1641. * address and perform any outbound access checks needed.
  1642. *
  1643. * Returns 0 on success or an error code.
  1644. *
  1645. */
  1646. static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
  1647. {
  1648. struct smack_known *skp;
  1649. int rc;
  1650. int sk_lbl;
  1651. char *hostsp;
  1652. struct socket_smack *ssp = sk->sk_security;
  1653. struct smk_audit_info ad;
  1654. rcu_read_lock();
  1655. hostsp = smack_host_label(sap);
  1656. if (hostsp != NULL) {
  1657. #ifdef CONFIG_AUDIT
  1658. struct lsm_network_audit net;
  1659. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  1660. ad.a.u.net->family = sap->sin_family;
  1661. ad.a.u.net->dport = sap->sin_port;
  1662. ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
  1663. #endif
  1664. sk_lbl = SMACK_UNLABELED_SOCKET;
  1665. skp = ssp->smk_out;
  1666. rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
  1667. } else {
  1668. sk_lbl = SMACK_CIPSO_SOCKET;
  1669. rc = 0;
  1670. }
  1671. rcu_read_unlock();
  1672. if (rc != 0)
  1673. return rc;
  1674. return smack_netlabel(sk, sk_lbl);
  1675. }
  1676. /**
  1677. * smk_ipv6_port_label - Smack port access table management
  1678. * @sock: socket
  1679. * @address: address
  1680. *
  1681. * Create or update the port list entry
  1682. */
  1683. static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
  1684. {
  1685. struct sock *sk = sock->sk;
  1686. struct sockaddr_in6 *addr6;
  1687. struct socket_smack *ssp = sock->sk->sk_security;
  1688. struct smk_port_label *spp;
  1689. unsigned short port = 0;
  1690. if (address == NULL) {
  1691. /*
  1692. * This operation is changing the Smack information
  1693. * on the bound socket. Take the changes to the port
  1694. * as well.
  1695. */
  1696. list_for_each_entry(spp, &smk_ipv6_port_list, list) {
  1697. if (sk != spp->smk_sock)
  1698. continue;
  1699. spp->smk_in = ssp->smk_in;
  1700. spp->smk_out = ssp->smk_out;
  1701. return;
  1702. }
  1703. /*
  1704. * A NULL address is only used for updating existing
  1705. * bound entries. If there isn't one, it's OK.
  1706. */
  1707. return;
  1708. }
  1709. addr6 = (struct sockaddr_in6 *)address;
  1710. port = ntohs(addr6->sin6_port);
  1711. /*
  1712. * This is a special case that is safely ignored.
  1713. */
  1714. if (port == 0)
  1715. return;
  1716. /*
  1717. * Look for an existing port list entry.
  1718. * This is an indication that a port is getting reused.
  1719. */
  1720. list_for_each_entry(spp, &smk_ipv6_port_list, list) {
  1721. if (spp->smk_port != port)
  1722. continue;
  1723. spp->smk_port = port;
  1724. spp->smk_sock = sk;
  1725. spp->smk_in = ssp->smk_in;
  1726. spp->smk_out = ssp->smk_out;
  1727. return;
  1728. }
  1729. /*
  1730. * A new port entry is required.
  1731. */
  1732. spp = kzalloc(sizeof(*spp), GFP_KERNEL);
  1733. if (spp == NULL)
  1734. return;
  1735. spp->smk_port = port;
  1736. spp->smk_sock = sk;
  1737. spp->smk_in = ssp->smk_in;
  1738. spp->smk_out = ssp->smk_out;
  1739. list_add(&spp->list, &smk_ipv6_port_list);
  1740. return;
  1741. }
  1742. /**
  1743. * smk_ipv6_port_check - check Smack port access
  1744. * @sock: socket
  1745. * @address: address
  1746. *
  1747. * Create or update the port list entry
  1748. */
  1749. static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
  1750. int act)
  1751. {
  1752. __be16 *bep;
  1753. __be32 *be32p;
  1754. struct smk_port_label *spp;
  1755. struct socket_smack *ssp = sk->sk_security;
  1756. struct smack_known *skp;
  1757. unsigned short port = 0;
  1758. char *object;
  1759. struct smk_audit_info ad;
  1760. #ifdef CONFIG_AUDIT
  1761. struct lsm_network_audit net;
  1762. #endif
  1763. if (act == SMK_RECEIVING) {
  1764. skp = smack_net_ambient;
  1765. object = ssp->smk_in;
  1766. } else {
  1767. skp = ssp->smk_out;
  1768. object = smack_net_ambient->smk_known;
  1769. }
  1770. /*
  1771. * Get the IP address and port from the address.
  1772. */
  1773. port = ntohs(address->sin6_port);
  1774. bep = (__be16 *)(&address->sin6_addr);
  1775. be32p = (__be32 *)(&address->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 = address->sin6_addr;
  1803. else
  1804. ad.a.u.net->v6info.daddr = address->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, (struct sockaddr_in6 *)sap,
  1929. SMK_CONNECTING);
  1930. break;
  1931. }
  1932. return rc;
  1933. }
  1934. /**
  1935. * smack_flags_to_may - convert S_ to MAY_ values
  1936. * @flags: the S_ value
  1937. *
  1938. * Returns the equivalent MAY_ value
  1939. */
  1940. static int smack_flags_to_may(int flags)
  1941. {
  1942. int may = 0;
  1943. if (flags & S_IRUGO)
  1944. may |= MAY_READ;
  1945. if (flags & S_IWUGO)
  1946. may |= MAY_WRITE;
  1947. if (flags & S_IXUGO)
  1948. may |= MAY_EXEC;
  1949. return may;
  1950. }
  1951. /**
  1952. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  1953. * @msg: the object
  1954. *
  1955. * Returns 0
  1956. */
  1957. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  1958. {
  1959. struct smack_known *skp = smk_of_current();
  1960. msg->security = skp->smk_known;
  1961. return 0;
  1962. }
  1963. /**
  1964. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  1965. * @msg: the object
  1966. *
  1967. * Clears the blob pointer
  1968. */
  1969. static void smack_msg_msg_free_security(struct msg_msg *msg)
  1970. {
  1971. msg->security = NULL;
  1972. }
  1973. /**
  1974. * smack_of_shm - the smack pointer for the shm
  1975. * @shp: the object
  1976. *
  1977. * Returns a pointer to the smack value
  1978. */
  1979. static char *smack_of_shm(struct shmid_kernel *shp)
  1980. {
  1981. return (char *)shp->shm_perm.security;
  1982. }
  1983. /**
  1984. * smack_shm_alloc_security - Set the security blob for shm
  1985. * @shp: the object
  1986. *
  1987. * Returns 0
  1988. */
  1989. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  1990. {
  1991. struct kern_ipc_perm *isp = &shp->shm_perm;
  1992. struct smack_known *skp = smk_of_current();
  1993. isp->security = skp->smk_known;
  1994. return 0;
  1995. }
  1996. /**
  1997. * smack_shm_free_security - Clear the security blob for shm
  1998. * @shp: the object
  1999. *
  2000. * Clears the blob pointer
  2001. */
  2002. static void smack_shm_free_security(struct shmid_kernel *shp)
  2003. {
  2004. struct kern_ipc_perm *isp = &shp->shm_perm;
  2005. isp->security = NULL;
  2006. }
  2007. /**
  2008. * smk_curacc_shm : check if current has access on shm
  2009. * @shp : the object
  2010. * @access : access requested
  2011. *
  2012. * Returns 0 if current has the requested access, error code otherwise
  2013. */
  2014. static int smk_curacc_shm(struct shmid_kernel *shp, int access)
  2015. {
  2016. char *ssp = smack_of_shm(shp);
  2017. struct smk_audit_info ad;
  2018. #ifdef CONFIG_AUDIT
  2019. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2020. ad.a.u.ipc_id = shp->shm_perm.id;
  2021. #endif
  2022. return smk_curacc(ssp, access, &ad);
  2023. }
  2024. /**
  2025. * smack_shm_associate - Smack access check for shm
  2026. * @shp: the object
  2027. * @shmflg: access requested
  2028. *
  2029. * Returns 0 if current has the requested access, error code otherwise
  2030. */
  2031. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  2032. {
  2033. int may;
  2034. may = smack_flags_to_may(shmflg);
  2035. return smk_curacc_shm(shp, may);
  2036. }
  2037. /**
  2038. * smack_shm_shmctl - Smack access check for shm
  2039. * @shp: the object
  2040. * @cmd: what it wants to do
  2041. *
  2042. * Returns 0 if current has the requested access, error code otherwise
  2043. */
  2044. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  2045. {
  2046. int may;
  2047. switch (cmd) {
  2048. case IPC_STAT:
  2049. case SHM_STAT:
  2050. may = MAY_READ;
  2051. break;
  2052. case IPC_SET:
  2053. case SHM_LOCK:
  2054. case SHM_UNLOCK:
  2055. case IPC_RMID:
  2056. may = MAY_READWRITE;
  2057. break;
  2058. case IPC_INFO:
  2059. case SHM_INFO:
  2060. /*
  2061. * System level information.
  2062. */
  2063. return 0;
  2064. default:
  2065. return -EINVAL;
  2066. }
  2067. return smk_curacc_shm(shp, may);
  2068. }
  2069. /**
  2070. * smack_shm_shmat - Smack access for shmat
  2071. * @shp: the object
  2072. * @shmaddr: unused
  2073. * @shmflg: access requested
  2074. *
  2075. * Returns 0 if current has the requested access, error code otherwise
  2076. */
  2077. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  2078. int shmflg)
  2079. {
  2080. int may;
  2081. may = smack_flags_to_may(shmflg);
  2082. return smk_curacc_shm(shp, may);
  2083. }
  2084. /**
  2085. * smack_of_sem - the smack pointer for the sem
  2086. * @sma: the object
  2087. *
  2088. * Returns a pointer to the smack value
  2089. */
  2090. static char *smack_of_sem(struct sem_array *sma)
  2091. {
  2092. return (char *)sma->sem_perm.security;
  2093. }
  2094. /**
  2095. * smack_sem_alloc_security - Set the security blob for sem
  2096. * @sma: the object
  2097. *
  2098. * Returns 0
  2099. */
  2100. static int smack_sem_alloc_security(struct sem_array *sma)
  2101. {
  2102. struct kern_ipc_perm *isp = &sma->sem_perm;
  2103. struct smack_known *skp = smk_of_current();
  2104. isp->security = skp->smk_known;
  2105. return 0;
  2106. }
  2107. /**
  2108. * smack_sem_free_security - Clear the security blob for sem
  2109. * @sma: the object
  2110. *
  2111. * Clears the blob pointer
  2112. */
  2113. static void smack_sem_free_security(struct sem_array *sma)
  2114. {
  2115. struct kern_ipc_perm *isp = &sma->sem_perm;
  2116. isp->security = NULL;
  2117. }
  2118. /**
  2119. * smk_curacc_sem : check if current has access on sem
  2120. * @sma : the object
  2121. * @access : access requested
  2122. *
  2123. * Returns 0 if current has the requested access, error code otherwise
  2124. */
  2125. static int smk_curacc_sem(struct sem_array *sma, int access)
  2126. {
  2127. char *ssp = smack_of_sem(sma);
  2128. struct smk_audit_info ad;
  2129. #ifdef CONFIG_AUDIT
  2130. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2131. ad.a.u.ipc_id = sma->sem_perm.id;
  2132. #endif
  2133. return smk_curacc(ssp, access, &ad);
  2134. }
  2135. /**
  2136. * smack_sem_associate - Smack access check for sem
  2137. * @sma: the object
  2138. * @semflg: access requested
  2139. *
  2140. * Returns 0 if current has the requested access, error code otherwise
  2141. */
  2142. static int smack_sem_associate(struct sem_array *sma, int semflg)
  2143. {
  2144. int may;
  2145. may = smack_flags_to_may(semflg);
  2146. return smk_curacc_sem(sma, may);
  2147. }
  2148. /**
  2149. * smack_sem_shmctl - Smack access check for sem
  2150. * @sma: the object
  2151. * @cmd: what it wants to do
  2152. *
  2153. * Returns 0 if current has the requested access, error code otherwise
  2154. */
  2155. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  2156. {
  2157. int may;
  2158. switch (cmd) {
  2159. case GETPID:
  2160. case GETNCNT:
  2161. case GETZCNT:
  2162. case GETVAL:
  2163. case GETALL:
  2164. case IPC_STAT:
  2165. case SEM_STAT:
  2166. may = MAY_READ;
  2167. break;
  2168. case SETVAL:
  2169. case SETALL:
  2170. case IPC_RMID:
  2171. case IPC_SET:
  2172. may = MAY_READWRITE;
  2173. break;
  2174. case IPC_INFO:
  2175. case SEM_INFO:
  2176. /*
  2177. * System level information
  2178. */
  2179. return 0;
  2180. default:
  2181. return -EINVAL;
  2182. }
  2183. return smk_curacc_sem(sma, may);
  2184. }
  2185. /**
  2186. * smack_sem_semop - Smack checks of semaphore operations
  2187. * @sma: the object
  2188. * @sops: unused
  2189. * @nsops: unused
  2190. * @alter: unused
  2191. *
  2192. * Treated as read and write in all cases.
  2193. *
  2194. * Returns 0 if access is allowed, error code otherwise
  2195. */
  2196. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  2197. unsigned nsops, int alter)
  2198. {
  2199. return smk_curacc_sem(sma, MAY_READWRITE);
  2200. }
  2201. /**
  2202. * smack_msg_alloc_security - Set the security blob for msg
  2203. * @msq: the object
  2204. *
  2205. * Returns 0
  2206. */
  2207. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  2208. {
  2209. struct kern_ipc_perm *kisp = &msq->q_perm;
  2210. struct smack_known *skp = smk_of_current();
  2211. kisp->security = skp->smk_known;
  2212. return 0;
  2213. }
  2214. /**
  2215. * smack_msg_free_security - Clear the security blob for msg
  2216. * @msq: the object
  2217. *
  2218. * Clears the blob pointer
  2219. */
  2220. static void smack_msg_queue_free_security(struct msg_queue *msq)
  2221. {
  2222. struct kern_ipc_perm *kisp = &msq->q_perm;
  2223. kisp->security = NULL;
  2224. }
  2225. /**
  2226. * smack_of_msq - the smack pointer for the msq
  2227. * @msq: the object
  2228. *
  2229. * Returns a pointer to the smack value
  2230. */
  2231. static char *smack_of_msq(struct msg_queue *msq)
  2232. {
  2233. return (char *)msq->q_perm.security;
  2234. }
  2235. /**
  2236. * smk_curacc_msq : helper to check if current has access on msq
  2237. * @msq : the msq
  2238. * @access : access requested
  2239. *
  2240. * return 0 if current has access, error otherwise
  2241. */
  2242. static int smk_curacc_msq(struct msg_queue *msq, int access)
  2243. {
  2244. char *msp = smack_of_msq(msq);
  2245. struct smk_audit_info ad;
  2246. #ifdef CONFIG_AUDIT
  2247. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2248. ad.a.u.ipc_id = msq->q_perm.id;
  2249. #endif
  2250. return smk_curacc(msp, access, &ad);
  2251. }
  2252. /**
  2253. * smack_msg_queue_associate - Smack access check for msg_queue
  2254. * @msq: the object
  2255. * @msqflg: access requested
  2256. *
  2257. * Returns 0 if current has the requested access, error code otherwise
  2258. */
  2259. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  2260. {
  2261. int may;
  2262. may = smack_flags_to_may(msqflg);
  2263. return smk_curacc_msq(msq, may);
  2264. }
  2265. /**
  2266. * smack_msg_queue_msgctl - Smack access check for msg_queue
  2267. * @msq: the object
  2268. * @cmd: what it wants to do
  2269. *
  2270. * Returns 0 if current has the requested access, error code otherwise
  2271. */
  2272. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  2273. {
  2274. int may;
  2275. switch (cmd) {
  2276. case IPC_STAT:
  2277. case MSG_STAT:
  2278. may = MAY_READ;
  2279. break;
  2280. case IPC_SET:
  2281. case IPC_RMID:
  2282. may = MAY_READWRITE;
  2283. break;
  2284. case IPC_INFO:
  2285. case MSG_INFO:
  2286. /*
  2287. * System level information
  2288. */
  2289. return 0;
  2290. default:
  2291. return -EINVAL;
  2292. }
  2293. return smk_curacc_msq(msq, may);
  2294. }
  2295. /**
  2296. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2297. * @msq: the object
  2298. * @msg: unused
  2299. * @msqflg: access requested
  2300. *
  2301. * Returns 0 if current has the requested access, error code otherwise
  2302. */
  2303. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  2304. int msqflg)
  2305. {
  2306. int may;
  2307. may = smack_flags_to_may(msqflg);
  2308. return smk_curacc_msq(msq, may);
  2309. }
  2310. /**
  2311. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2312. * @msq: the object
  2313. * @msg: unused
  2314. * @target: unused
  2315. * @type: unused
  2316. * @mode: unused
  2317. *
  2318. * Returns 0 if current has read and write access, error code otherwise
  2319. */
  2320. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  2321. struct task_struct *target, long type, int mode)
  2322. {
  2323. return smk_curacc_msq(msq, MAY_READWRITE);
  2324. }
  2325. /**
  2326. * smack_ipc_permission - Smack access for ipc_permission()
  2327. * @ipp: the object permissions
  2328. * @flag: access requested
  2329. *
  2330. * Returns 0 if current has read and write access, error code otherwise
  2331. */
  2332. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  2333. {
  2334. char *isp = ipp->security;
  2335. int may = smack_flags_to_may(flag);
  2336. struct smk_audit_info ad;
  2337. #ifdef CONFIG_AUDIT
  2338. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2339. ad.a.u.ipc_id = ipp->id;
  2340. #endif
  2341. return smk_curacc(isp, may, &ad);
  2342. }
  2343. /**
  2344. * smack_ipc_getsecid - Extract smack security id
  2345. * @ipp: the object permissions
  2346. * @secid: where result will be saved
  2347. */
  2348. static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
  2349. {
  2350. char *smack = ipp->security;
  2351. *secid = smack_to_secid(smack);
  2352. }
  2353. /**
  2354. * smack_d_instantiate - Make sure the blob is correct on an inode
  2355. * @opt_dentry: dentry where inode will be attached
  2356. * @inode: the object
  2357. *
  2358. * Set the inode's security blob if it hasn't been done already.
  2359. */
  2360. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  2361. {
  2362. struct super_block *sbp;
  2363. struct superblock_smack *sbsp;
  2364. struct inode_smack *isp;
  2365. struct smack_known *skp;
  2366. struct smack_known *ckp = smk_of_current();
  2367. char *final;
  2368. char trattr[TRANS_TRUE_SIZE];
  2369. int transflag = 0;
  2370. int rc;
  2371. struct dentry *dp;
  2372. if (inode == NULL)
  2373. return;
  2374. isp = inode->i_security;
  2375. mutex_lock(&isp->smk_lock);
  2376. /*
  2377. * If the inode is already instantiated
  2378. * take the quick way out
  2379. */
  2380. if (isp->smk_flags & SMK_INODE_INSTANT)
  2381. goto unlockandout;
  2382. sbp = inode->i_sb;
  2383. sbsp = sbp->s_security;
  2384. /*
  2385. * We're going to use the superblock default label
  2386. * if there's no label on the file.
  2387. */
  2388. final = sbsp->smk_default;
  2389. /*
  2390. * If this is the root inode the superblock
  2391. * may be in the process of initialization.
  2392. * If that is the case use the root value out
  2393. * of the superblock.
  2394. */
  2395. if (opt_dentry->d_parent == opt_dentry) {
  2396. isp->smk_inode = sbsp->smk_root;
  2397. isp->smk_flags |= SMK_INODE_INSTANT;
  2398. goto unlockandout;
  2399. }
  2400. /*
  2401. * This is pretty hackish.
  2402. * Casey says that we shouldn't have to do
  2403. * file system specific code, but it does help
  2404. * with keeping it simple.
  2405. */
  2406. switch (sbp->s_magic) {
  2407. case SMACK_MAGIC:
  2408. /*
  2409. * Casey says that it's a little embarrassing
  2410. * that the smack file system doesn't do
  2411. * extended attributes.
  2412. */
  2413. final = smack_known_star.smk_known;
  2414. break;
  2415. case PIPEFS_MAGIC:
  2416. /*
  2417. * Casey says pipes are easy (?)
  2418. */
  2419. final = smack_known_star.smk_known;
  2420. break;
  2421. case DEVPTS_SUPER_MAGIC:
  2422. /*
  2423. * devpts seems content with the label of the task.
  2424. * Programs that change smack have to treat the
  2425. * pty with respect.
  2426. */
  2427. final = ckp->smk_known;
  2428. break;
  2429. case SOCKFS_MAGIC:
  2430. /*
  2431. * Socket access is controlled by the socket
  2432. * structures associated with the task involved.
  2433. */
  2434. final = smack_known_star.smk_known;
  2435. break;
  2436. case PROC_SUPER_MAGIC:
  2437. /*
  2438. * Casey says procfs appears not to care.
  2439. * The superblock default suffices.
  2440. */
  2441. break;
  2442. case TMPFS_MAGIC:
  2443. /*
  2444. * Device labels should come from the filesystem,
  2445. * but watch out, because they're volitile,
  2446. * getting recreated on every reboot.
  2447. */
  2448. final = smack_known_star.smk_known;
  2449. /*
  2450. * No break.
  2451. *
  2452. * If a smack value has been set we want to use it,
  2453. * but since tmpfs isn't giving us the opportunity
  2454. * to set mount options simulate setting the
  2455. * superblock default.
  2456. */
  2457. default:
  2458. /*
  2459. * This isn't an understood special case.
  2460. * Get the value from the xattr.
  2461. */
  2462. /*
  2463. * UNIX domain sockets use lower level socket data.
  2464. */
  2465. if (S_ISSOCK(inode->i_mode)) {
  2466. final = smack_known_star.smk_known;
  2467. break;
  2468. }
  2469. /*
  2470. * No xattr support means, alas, no SMACK label.
  2471. * Use the aforeapplied default.
  2472. * It would be curious if the label of the task
  2473. * does not match that assigned.
  2474. */
  2475. if (inode->i_op->getxattr == NULL)
  2476. break;
  2477. /*
  2478. * Get the dentry for xattr.
  2479. */
  2480. dp = dget(opt_dentry);
  2481. skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
  2482. if (skp != NULL)
  2483. final = skp->smk_known;
  2484. /*
  2485. * Transmuting directory
  2486. */
  2487. if (S_ISDIR(inode->i_mode)) {
  2488. /*
  2489. * If this is a new directory and the label was
  2490. * transmuted when the inode was initialized
  2491. * set the transmute attribute on the directory
  2492. * and mark the inode.
  2493. *
  2494. * If there is a transmute attribute on the
  2495. * directory mark the inode.
  2496. */
  2497. if (isp->smk_flags & SMK_INODE_CHANGED) {
  2498. isp->smk_flags &= ~SMK_INODE_CHANGED;
  2499. rc = inode->i_op->setxattr(dp,
  2500. XATTR_NAME_SMACKTRANSMUTE,
  2501. TRANS_TRUE, TRANS_TRUE_SIZE,
  2502. 0);
  2503. } else {
  2504. rc = inode->i_op->getxattr(dp,
  2505. XATTR_NAME_SMACKTRANSMUTE, trattr,
  2506. TRANS_TRUE_SIZE);
  2507. if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
  2508. TRANS_TRUE_SIZE) != 0)
  2509. rc = -EINVAL;
  2510. }
  2511. if (rc >= 0)
  2512. transflag = SMK_INODE_TRANSMUTE;
  2513. }
  2514. isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
  2515. isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
  2516. dput(dp);
  2517. break;
  2518. }
  2519. if (final == NULL)
  2520. isp->smk_inode = ckp->smk_known;
  2521. else
  2522. isp->smk_inode = final;
  2523. isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
  2524. unlockandout:
  2525. mutex_unlock(&isp->smk_lock);
  2526. return;
  2527. }
  2528. /**
  2529. * smack_getprocattr - Smack process attribute access
  2530. * @p: the object task
  2531. * @name: the name of the attribute in /proc/.../attr
  2532. * @value: where to put the result
  2533. *
  2534. * Places a copy of the task Smack into value
  2535. *
  2536. * Returns the length of the smack label or an error code
  2537. */
  2538. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  2539. {
  2540. struct smack_known *skp = smk_of_task(task_security(p));
  2541. char *cp;
  2542. int slen;
  2543. if (strcmp(name, "current") != 0)
  2544. return -EINVAL;
  2545. cp = kstrdup(skp->smk_known, GFP_KERNEL);
  2546. if (cp == NULL)
  2547. return -ENOMEM;
  2548. slen = strlen(cp);
  2549. *value = cp;
  2550. return slen;
  2551. }
  2552. /**
  2553. * smack_setprocattr - Smack process attribute setting
  2554. * @p: the object task
  2555. * @name: the name of the attribute in /proc/.../attr
  2556. * @value: the value to set
  2557. * @size: the size of the value
  2558. *
  2559. * Sets the Smack value of the task. Only setting self
  2560. * is permitted and only with privilege
  2561. *
  2562. * Returns the length of the smack label or an error code
  2563. */
  2564. static int smack_setprocattr(struct task_struct *p, char *name,
  2565. void *value, size_t size)
  2566. {
  2567. struct task_smack *tsp;
  2568. struct cred *new;
  2569. struct smack_known *skp;
  2570. /*
  2571. * Changing another process' Smack value is too dangerous
  2572. * and supports no sane use case.
  2573. */
  2574. if (p != current)
  2575. return -EPERM;
  2576. if (!smack_privileged(CAP_MAC_ADMIN))
  2577. return -EPERM;
  2578. if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
  2579. return -EINVAL;
  2580. if (strcmp(name, "current") != 0)
  2581. return -EINVAL;
  2582. skp = smk_import_entry(value, size);
  2583. if (skp == NULL)
  2584. return -EINVAL;
  2585. /*
  2586. * No process is ever allowed the web ("@") label.
  2587. */
  2588. if (skp == &smack_known_web)
  2589. return -EPERM;
  2590. new = prepare_creds();
  2591. if (new == NULL)
  2592. return -ENOMEM;
  2593. tsp = new->security;
  2594. tsp->smk_task = skp;
  2595. commit_creds(new);
  2596. return size;
  2597. }
  2598. /**
  2599. * smack_unix_stream_connect - Smack access on UDS
  2600. * @sock: one sock
  2601. * @other: the other sock
  2602. * @newsk: unused
  2603. *
  2604. * Return 0 if a subject with the smack of sock could access
  2605. * an object with the smack of other, otherwise an error code
  2606. */
  2607. static int smack_unix_stream_connect(struct sock *sock,
  2608. struct sock *other, struct sock *newsk)
  2609. {
  2610. struct smack_known *skp;
  2611. struct socket_smack *ssp = sock->sk_security;
  2612. struct socket_smack *osp = other->sk_security;
  2613. struct socket_smack *nsp = newsk->sk_security;
  2614. struct smk_audit_info ad;
  2615. int rc = 0;
  2616. #ifdef CONFIG_AUDIT
  2617. struct lsm_network_audit net;
  2618. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2619. smk_ad_setfield_u_net_sk(&ad, other);
  2620. #endif
  2621. if (!smack_privileged(CAP_MAC_OVERRIDE)) {
  2622. skp = ssp->smk_out;
  2623. rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
  2624. }
  2625. /*
  2626. * Cross reference the peer labels for SO_PEERSEC.
  2627. */
  2628. if (rc == 0) {
  2629. nsp->smk_packet = ssp->smk_out->smk_known;
  2630. ssp->smk_packet = osp->smk_out->smk_known;
  2631. }
  2632. return rc;
  2633. }
  2634. /**
  2635. * smack_unix_may_send - Smack access on UDS
  2636. * @sock: one socket
  2637. * @other: the other socket
  2638. *
  2639. * Return 0 if a subject with the smack of sock could access
  2640. * an object with the smack of other, otherwise an error code
  2641. */
  2642. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  2643. {
  2644. struct socket_smack *ssp = sock->sk->sk_security;
  2645. struct socket_smack *osp = other->sk->sk_security;
  2646. struct smack_known *skp;
  2647. struct smk_audit_info ad;
  2648. #ifdef CONFIG_AUDIT
  2649. struct lsm_network_audit net;
  2650. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2651. smk_ad_setfield_u_net_sk(&ad, other->sk);
  2652. #endif
  2653. if (smack_privileged(CAP_MAC_OVERRIDE))
  2654. return 0;
  2655. skp = ssp->smk_out;
  2656. return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
  2657. }
  2658. /**
  2659. * smack_socket_sendmsg - Smack check based on destination host
  2660. * @sock: the socket
  2661. * @msg: the message
  2662. * @size: the size of the message
  2663. *
  2664. * Return 0 if the current subject can write to the destination host.
  2665. * For IPv4 this is only a question if the destination is a single label host.
  2666. * For IPv6 this is a check against the label of the port.
  2667. */
  2668. static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  2669. int size)
  2670. {
  2671. struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
  2672. struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
  2673. int rc = 0;
  2674. /*
  2675. * Perfectly reasonable for this to be NULL
  2676. */
  2677. if (sip == NULL)
  2678. return 0;
  2679. switch (sip->sin_family) {
  2680. case AF_INET:
  2681. rc = smack_netlabel_send(sock->sk, sip);
  2682. break;
  2683. case AF_INET6:
  2684. rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
  2685. break;
  2686. }
  2687. return rc;
  2688. }
  2689. /**
  2690. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
  2691. * @sap: netlabel secattr
  2692. * @ssp: socket security information
  2693. *
  2694. * Returns a pointer to a Smack label entry found on the label list.
  2695. */
  2696. static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
  2697. struct socket_smack *ssp)
  2698. {
  2699. struct smack_known *skp;
  2700. int found = 0;
  2701. int acat;
  2702. int kcat;
  2703. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
  2704. /*
  2705. * Looks like a CIPSO packet.
  2706. * If there are flags but no level netlabel isn't
  2707. * behaving the way we expect it to.
  2708. *
  2709. * Look it up in the label table
  2710. * Without guidance regarding the smack value
  2711. * for the packet fall back on the network
  2712. * ambient value.
  2713. */
  2714. rcu_read_lock();
  2715. list_for_each_entry(skp, &smack_known_list, list) {
  2716. if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
  2717. continue;
  2718. /*
  2719. * Compare the catsets. Use the netlbl APIs.
  2720. */
  2721. if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
  2722. if ((skp->smk_netlabel.flags &
  2723. NETLBL_SECATTR_MLS_CAT) == 0)
  2724. found = 1;
  2725. break;
  2726. }
  2727. for (acat = -1, kcat = -1; acat == kcat; ) {
  2728. acat = netlbl_secattr_catmap_walk(
  2729. sap->attr.mls.cat, acat + 1);
  2730. kcat = netlbl_secattr_catmap_walk(
  2731. skp->smk_netlabel.attr.mls.cat,
  2732. kcat + 1);
  2733. if (acat < 0 || kcat < 0)
  2734. break;
  2735. }
  2736. if (acat == kcat) {
  2737. found = 1;
  2738. break;
  2739. }
  2740. }
  2741. rcu_read_unlock();
  2742. if (found)
  2743. return skp;
  2744. if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
  2745. return &smack_known_web;
  2746. return &smack_known_star;
  2747. }
  2748. if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
  2749. /*
  2750. * Looks like a fallback, which gives us a secid.
  2751. */
  2752. skp = smack_from_secid(sap->attr.secid);
  2753. /*
  2754. * This has got to be a bug because it is
  2755. * impossible to specify a fallback without
  2756. * specifying the label, which will ensure
  2757. * it has a secid, and the only way to get a
  2758. * secid is from a fallback.
  2759. */
  2760. BUG_ON(skp == NULL);
  2761. return skp;
  2762. }
  2763. /*
  2764. * Without guidance regarding the smack value
  2765. * for the packet fall back on the network
  2766. * ambient value.
  2767. */
  2768. return smack_net_ambient;
  2769. }
  2770. static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
  2771. {
  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_in6 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);