smack_lsm.c 88 KB

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