smack_lsm.c 69 KB

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