smack_lsm.c 69 KB

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