smack_lsm.c 88 KB

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