smack_lsm.c 79 KB

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