smack_lsm.c 67 KB

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