smack_lsm.c 91 KB

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