smack_lsm.c 84 KB

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