smack_lsm.c 68 KB

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