smack_lsm.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  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. Because of the masking
  1318. * it cannot shortcut out on the first match. There are
  1319. * numerious ways to address the problem, but none of them
  1320. * have been applied here.
  1321. *
  1322. * Returns the label of the far end or NULL if it's not special.
  1323. */
  1324. static char *smack_host_label(struct sockaddr_in *sip)
  1325. {
  1326. struct smk_netlbladdr *snp;
  1327. char *bestlabel = NULL;
  1328. struct in_addr *siap = &sip->sin_addr;
  1329. struct in_addr *liap;
  1330. struct in_addr *miap;
  1331. struct in_addr bestmask;
  1332. if (siap->s_addr == 0)
  1333. return NULL;
  1334. bestmask.s_addr = 0;
  1335. for (snp = smack_netlbladdrs; snp != NULL; snp = snp->smk_next) {
  1336. liap = &snp->smk_host.sin_addr;
  1337. miap = &snp->smk_mask;
  1338. /*
  1339. * If the addresses match after applying the list entry mask
  1340. * the entry matches the address. If it doesn't move along to
  1341. * the next entry.
  1342. */
  1343. if ((liap->s_addr & miap->s_addr) !=
  1344. (siap->s_addr & miap->s_addr))
  1345. continue;
  1346. /*
  1347. * If the list entry mask identifies a single address
  1348. * it can't get any more specific.
  1349. */
  1350. if (miap->s_addr == 0xffffffff)
  1351. return snp->smk_label;
  1352. /*
  1353. * If the list entry mask is less specific than the best
  1354. * already found this entry is uninteresting.
  1355. */
  1356. if ((miap->s_addr | bestmask.s_addr) == bestmask.s_addr)
  1357. continue;
  1358. /*
  1359. * This is better than any entry found so far.
  1360. */
  1361. bestmask.s_addr = miap->s_addr;
  1362. bestlabel = snp->smk_label;
  1363. }
  1364. return bestlabel;
  1365. }
  1366. /**
  1367. * smack_socket_connect - connect access check
  1368. * @sock: the socket
  1369. * @sap: the other end
  1370. * @addrlen: size of sap
  1371. *
  1372. * Verifies that a connection may be possible
  1373. *
  1374. * Returns 0 on success, and error code otherwise
  1375. */
  1376. static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
  1377. int addrlen)
  1378. {
  1379. struct socket_smack *ssp = sock->sk->sk_security;
  1380. char *hostsp;
  1381. int rc;
  1382. if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
  1383. return 0;
  1384. if (addrlen < sizeof(struct sockaddr_in))
  1385. return -EINVAL;
  1386. hostsp = smack_host_label((struct sockaddr_in *)sap);
  1387. if (hostsp == NULL) {
  1388. if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
  1389. return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  1390. return 0;
  1391. }
  1392. rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
  1393. if (rc != 0)
  1394. return rc;
  1395. if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
  1396. return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
  1397. return 0;
  1398. }
  1399. /**
  1400. * smack_flags_to_may - convert S_ to MAY_ values
  1401. * @flags: the S_ value
  1402. *
  1403. * Returns the equivalent MAY_ value
  1404. */
  1405. static int smack_flags_to_may(int flags)
  1406. {
  1407. int may = 0;
  1408. if (flags & S_IRUGO)
  1409. may |= MAY_READ;
  1410. if (flags & S_IWUGO)
  1411. may |= MAY_WRITE;
  1412. if (flags & S_IXUGO)
  1413. may |= MAY_EXEC;
  1414. return may;
  1415. }
  1416. /**
  1417. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  1418. * @msg: the object
  1419. *
  1420. * Returns 0
  1421. */
  1422. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  1423. {
  1424. msg->security = current_security();
  1425. return 0;
  1426. }
  1427. /**
  1428. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  1429. * @msg: the object
  1430. *
  1431. * Clears the blob pointer
  1432. */
  1433. static void smack_msg_msg_free_security(struct msg_msg *msg)
  1434. {
  1435. msg->security = NULL;
  1436. }
  1437. /**
  1438. * smack_of_shm - the smack pointer for the shm
  1439. * @shp: the object
  1440. *
  1441. * Returns a pointer to the smack value
  1442. */
  1443. static char *smack_of_shm(struct shmid_kernel *shp)
  1444. {
  1445. return (char *)shp->shm_perm.security;
  1446. }
  1447. /**
  1448. * smack_shm_alloc_security - Set the security blob for shm
  1449. * @shp: the object
  1450. *
  1451. * Returns 0
  1452. */
  1453. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  1454. {
  1455. struct kern_ipc_perm *isp = &shp->shm_perm;
  1456. isp->security = current_security();
  1457. return 0;
  1458. }
  1459. /**
  1460. * smack_shm_free_security - Clear the security blob for shm
  1461. * @shp: the object
  1462. *
  1463. * Clears the blob pointer
  1464. */
  1465. static void smack_shm_free_security(struct shmid_kernel *shp)
  1466. {
  1467. struct kern_ipc_perm *isp = &shp->shm_perm;
  1468. isp->security = NULL;
  1469. }
  1470. /**
  1471. * smack_shm_associate - Smack access check for shm
  1472. * @shp: the object
  1473. * @shmflg: access requested
  1474. *
  1475. * Returns 0 if current has the requested access, error code otherwise
  1476. */
  1477. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  1478. {
  1479. char *ssp = smack_of_shm(shp);
  1480. int may;
  1481. may = smack_flags_to_may(shmflg);
  1482. return smk_curacc(ssp, may);
  1483. }
  1484. /**
  1485. * smack_shm_shmctl - Smack access check for shm
  1486. * @shp: the object
  1487. * @cmd: what it wants to do
  1488. *
  1489. * Returns 0 if current has the requested access, error code otherwise
  1490. */
  1491. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  1492. {
  1493. char *ssp;
  1494. int may;
  1495. switch (cmd) {
  1496. case IPC_STAT:
  1497. case SHM_STAT:
  1498. may = MAY_READ;
  1499. break;
  1500. case IPC_SET:
  1501. case SHM_LOCK:
  1502. case SHM_UNLOCK:
  1503. case IPC_RMID:
  1504. may = MAY_READWRITE;
  1505. break;
  1506. case IPC_INFO:
  1507. case SHM_INFO:
  1508. /*
  1509. * System level information.
  1510. */
  1511. return 0;
  1512. default:
  1513. return -EINVAL;
  1514. }
  1515. ssp = smack_of_shm(shp);
  1516. return smk_curacc(ssp, may);
  1517. }
  1518. /**
  1519. * smack_shm_shmat - Smack access for shmat
  1520. * @shp: the object
  1521. * @shmaddr: unused
  1522. * @shmflg: access requested
  1523. *
  1524. * Returns 0 if current has the requested access, error code otherwise
  1525. */
  1526. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  1527. int shmflg)
  1528. {
  1529. char *ssp = smack_of_shm(shp);
  1530. int may;
  1531. may = smack_flags_to_may(shmflg);
  1532. return smk_curacc(ssp, may);
  1533. }
  1534. /**
  1535. * smack_of_sem - the smack pointer for the sem
  1536. * @sma: the object
  1537. *
  1538. * Returns a pointer to the smack value
  1539. */
  1540. static char *smack_of_sem(struct sem_array *sma)
  1541. {
  1542. return (char *)sma->sem_perm.security;
  1543. }
  1544. /**
  1545. * smack_sem_alloc_security - Set the security blob for sem
  1546. * @sma: the object
  1547. *
  1548. * Returns 0
  1549. */
  1550. static int smack_sem_alloc_security(struct sem_array *sma)
  1551. {
  1552. struct kern_ipc_perm *isp = &sma->sem_perm;
  1553. isp->security = current_security();
  1554. return 0;
  1555. }
  1556. /**
  1557. * smack_sem_free_security - Clear the security blob for sem
  1558. * @sma: the object
  1559. *
  1560. * Clears the blob pointer
  1561. */
  1562. static void smack_sem_free_security(struct sem_array *sma)
  1563. {
  1564. struct kern_ipc_perm *isp = &sma->sem_perm;
  1565. isp->security = NULL;
  1566. }
  1567. /**
  1568. * smack_sem_associate - Smack access check for sem
  1569. * @sma: the object
  1570. * @semflg: access requested
  1571. *
  1572. * Returns 0 if current has the requested access, error code otherwise
  1573. */
  1574. static int smack_sem_associate(struct sem_array *sma, int semflg)
  1575. {
  1576. char *ssp = smack_of_sem(sma);
  1577. int may;
  1578. may = smack_flags_to_may(semflg);
  1579. return smk_curacc(ssp, may);
  1580. }
  1581. /**
  1582. * smack_sem_shmctl - Smack access check for sem
  1583. * @sma: the object
  1584. * @cmd: what it wants to do
  1585. *
  1586. * Returns 0 if current has the requested access, error code otherwise
  1587. */
  1588. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  1589. {
  1590. char *ssp;
  1591. int may;
  1592. switch (cmd) {
  1593. case GETPID:
  1594. case GETNCNT:
  1595. case GETZCNT:
  1596. case GETVAL:
  1597. case GETALL:
  1598. case IPC_STAT:
  1599. case SEM_STAT:
  1600. may = MAY_READ;
  1601. break;
  1602. case SETVAL:
  1603. case SETALL:
  1604. case IPC_RMID:
  1605. case IPC_SET:
  1606. may = MAY_READWRITE;
  1607. break;
  1608. case IPC_INFO:
  1609. case SEM_INFO:
  1610. /*
  1611. * System level information
  1612. */
  1613. return 0;
  1614. default:
  1615. return -EINVAL;
  1616. }
  1617. ssp = smack_of_sem(sma);
  1618. return smk_curacc(ssp, may);
  1619. }
  1620. /**
  1621. * smack_sem_semop - Smack checks of semaphore operations
  1622. * @sma: the object
  1623. * @sops: unused
  1624. * @nsops: unused
  1625. * @alter: unused
  1626. *
  1627. * Treated as read and write in all cases.
  1628. *
  1629. * Returns 0 if access is allowed, error code otherwise
  1630. */
  1631. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  1632. unsigned nsops, int alter)
  1633. {
  1634. char *ssp = smack_of_sem(sma);
  1635. return smk_curacc(ssp, MAY_READWRITE);
  1636. }
  1637. /**
  1638. * smack_msg_alloc_security - Set the security blob for msg
  1639. * @msq: the object
  1640. *
  1641. * Returns 0
  1642. */
  1643. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  1644. {
  1645. struct kern_ipc_perm *kisp = &msq->q_perm;
  1646. kisp->security = current_security();
  1647. return 0;
  1648. }
  1649. /**
  1650. * smack_msg_free_security - Clear the security blob for msg
  1651. * @msq: the object
  1652. *
  1653. * Clears the blob pointer
  1654. */
  1655. static void smack_msg_queue_free_security(struct msg_queue *msq)
  1656. {
  1657. struct kern_ipc_perm *kisp = &msq->q_perm;
  1658. kisp->security = NULL;
  1659. }
  1660. /**
  1661. * smack_of_msq - the smack pointer for the msq
  1662. * @msq: the object
  1663. *
  1664. * Returns a pointer to the smack value
  1665. */
  1666. static char *smack_of_msq(struct msg_queue *msq)
  1667. {
  1668. return (char *)msq->q_perm.security;
  1669. }
  1670. /**
  1671. * smack_msg_queue_associate - Smack access check for msg_queue
  1672. * @msq: the object
  1673. * @msqflg: access requested
  1674. *
  1675. * Returns 0 if current has the requested access, error code otherwise
  1676. */
  1677. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  1678. {
  1679. char *msp = smack_of_msq(msq);
  1680. int may;
  1681. may = smack_flags_to_may(msqflg);
  1682. return smk_curacc(msp, may);
  1683. }
  1684. /**
  1685. * smack_msg_queue_msgctl - Smack access check for msg_queue
  1686. * @msq: the object
  1687. * @cmd: what it wants to do
  1688. *
  1689. * Returns 0 if current has the requested access, error code otherwise
  1690. */
  1691. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  1692. {
  1693. char *msp;
  1694. int may;
  1695. switch (cmd) {
  1696. case IPC_STAT:
  1697. case MSG_STAT:
  1698. may = MAY_READ;
  1699. break;
  1700. case IPC_SET:
  1701. case IPC_RMID:
  1702. may = MAY_READWRITE;
  1703. break;
  1704. case IPC_INFO:
  1705. case MSG_INFO:
  1706. /*
  1707. * System level information
  1708. */
  1709. return 0;
  1710. default:
  1711. return -EINVAL;
  1712. }
  1713. msp = smack_of_msq(msq);
  1714. return smk_curacc(msp, may);
  1715. }
  1716. /**
  1717. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1718. * @msq: the object
  1719. * @msg: unused
  1720. * @msqflg: access requested
  1721. *
  1722. * Returns 0 if current has the requested access, error code otherwise
  1723. */
  1724. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  1725. int msqflg)
  1726. {
  1727. char *msp = smack_of_msq(msq);
  1728. int rc;
  1729. rc = smack_flags_to_may(msqflg);
  1730. return smk_curacc(msp, rc);
  1731. }
  1732. /**
  1733. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1734. * @msq: the object
  1735. * @msg: unused
  1736. * @target: unused
  1737. * @type: unused
  1738. * @mode: unused
  1739. *
  1740. * Returns 0 if current has read and write access, error code otherwise
  1741. */
  1742. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  1743. struct task_struct *target, long type, int mode)
  1744. {
  1745. char *msp = smack_of_msq(msq);
  1746. return smk_curacc(msp, MAY_READWRITE);
  1747. }
  1748. /**
  1749. * smack_ipc_permission - Smack access for ipc_permission()
  1750. * @ipp: the object permissions
  1751. * @flag: access requested
  1752. *
  1753. * Returns 0 if current has read and write access, error code otherwise
  1754. */
  1755. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  1756. {
  1757. char *isp = ipp->security;
  1758. int may;
  1759. may = smack_flags_to_may(flag);
  1760. return smk_curacc(isp, may);
  1761. }
  1762. /**
  1763. * smack_ipc_getsecid - Extract smack security id
  1764. * @ipcp: the object permissions
  1765. * @secid: where result will be saved
  1766. */
  1767. static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
  1768. {
  1769. char *smack = ipp->security;
  1770. *secid = smack_to_secid(smack);
  1771. }
  1772. /**
  1773. * smack_d_instantiate - Make sure the blob is correct on an inode
  1774. * @opt_dentry: unused
  1775. * @inode: the object
  1776. *
  1777. * Set the inode's security blob if it hasn't been done already.
  1778. */
  1779. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  1780. {
  1781. struct super_block *sbp;
  1782. struct superblock_smack *sbsp;
  1783. struct inode_smack *isp;
  1784. char *csp = current_security();
  1785. char *fetched;
  1786. char *final;
  1787. struct dentry *dp;
  1788. if (inode == NULL)
  1789. return;
  1790. isp = inode->i_security;
  1791. mutex_lock(&isp->smk_lock);
  1792. /*
  1793. * If the inode is already instantiated
  1794. * take the quick way out
  1795. */
  1796. if (isp->smk_flags & SMK_INODE_INSTANT)
  1797. goto unlockandout;
  1798. sbp = inode->i_sb;
  1799. sbsp = sbp->s_security;
  1800. /*
  1801. * We're going to use the superblock default label
  1802. * if there's no label on the file.
  1803. */
  1804. final = sbsp->smk_default;
  1805. /*
  1806. * If this is the root inode the superblock
  1807. * may be in the process of initialization.
  1808. * If that is the case use the root value out
  1809. * of the superblock.
  1810. */
  1811. if (opt_dentry->d_parent == opt_dentry) {
  1812. isp->smk_inode = sbsp->smk_root;
  1813. isp->smk_flags |= SMK_INODE_INSTANT;
  1814. goto unlockandout;
  1815. }
  1816. /*
  1817. * This is pretty hackish.
  1818. * Casey says that we shouldn't have to do
  1819. * file system specific code, but it does help
  1820. * with keeping it simple.
  1821. */
  1822. switch (sbp->s_magic) {
  1823. case SMACK_MAGIC:
  1824. /*
  1825. * Casey says that it's a little embarassing
  1826. * that the smack file system doesn't do
  1827. * extended attributes.
  1828. */
  1829. final = smack_known_star.smk_known;
  1830. break;
  1831. case PIPEFS_MAGIC:
  1832. /*
  1833. * Casey says pipes are easy (?)
  1834. */
  1835. final = smack_known_star.smk_known;
  1836. break;
  1837. case DEVPTS_SUPER_MAGIC:
  1838. /*
  1839. * devpts seems content with the label of the task.
  1840. * Programs that change smack have to treat the
  1841. * pty with respect.
  1842. */
  1843. final = csp;
  1844. break;
  1845. case SOCKFS_MAGIC:
  1846. /*
  1847. * Casey says sockets get the smack of the task.
  1848. */
  1849. final = csp;
  1850. break;
  1851. case PROC_SUPER_MAGIC:
  1852. /*
  1853. * Casey says procfs appears not to care.
  1854. * The superblock default suffices.
  1855. */
  1856. break;
  1857. case TMPFS_MAGIC:
  1858. /*
  1859. * Device labels should come from the filesystem,
  1860. * but watch out, because they're volitile,
  1861. * getting recreated on every reboot.
  1862. */
  1863. final = smack_known_star.smk_known;
  1864. /*
  1865. * No break.
  1866. *
  1867. * If a smack value has been set we want to use it,
  1868. * but since tmpfs isn't giving us the opportunity
  1869. * to set mount options simulate setting the
  1870. * superblock default.
  1871. */
  1872. default:
  1873. /*
  1874. * This isn't an understood special case.
  1875. * Get the value from the xattr.
  1876. *
  1877. * No xattr support means, alas, no SMACK label.
  1878. * Use the aforeapplied default.
  1879. * It would be curious if the label of the task
  1880. * does not match that assigned.
  1881. */
  1882. if (inode->i_op->getxattr == NULL)
  1883. break;
  1884. /*
  1885. * Get the dentry for xattr.
  1886. */
  1887. if (opt_dentry == NULL) {
  1888. dp = d_find_alias(inode);
  1889. if (dp == NULL)
  1890. break;
  1891. } else {
  1892. dp = dget(opt_dentry);
  1893. if (dp == NULL)
  1894. break;
  1895. }
  1896. fetched = smk_fetch(inode, dp);
  1897. if (fetched != NULL)
  1898. final = fetched;
  1899. dput(dp);
  1900. break;
  1901. }
  1902. if (final == NULL)
  1903. isp->smk_inode = csp;
  1904. else
  1905. isp->smk_inode = final;
  1906. isp->smk_flags |= SMK_INODE_INSTANT;
  1907. unlockandout:
  1908. mutex_unlock(&isp->smk_lock);
  1909. return;
  1910. }
  1911. /**
  1912. * smack_getprocattr - Smack process attribute access
  1913. * @p: the object task
  1914. * @name: the name of the attribute in /proc/.../attr
  1915. * @value: where to put the result
  1916. *
  1917. * Places a copy of the task Smack into value
  1918. *
  1919. * Returns the length of the smack label or an error code
  1920. */
  1921. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  1922. {
  1923. char *cp;
  1924. int slen;
  1925. if (strcmp(name, "current") != 0)
  1926. return -EINVAL;
  1927. cp = kstrdup(task_security(p), GFP_KERNEL);
  1928. if (cp == NULL)
  1929. return -ENOMEM;
  1930. slen = strlen(cp);
  1931. *value = cp;
  1932. return slen;
  1933. }
  1934. /**
  1935. * smack_setprocattr - Smack process attribute setting
  1936. * @p: the object task
  1937. * @name: the name of the attribute in /proc/.../attr
  1938. * @value: the value to set
  1939. * @size: the size of the value
  1940. *
  1941. * Sets the Smack value of the task. Only setting self
  1942. * is permitted and only with privilege
  1943. *
  1944. * Returns the length of the smack label or an error code
  1945. */
  1946. static int smack_setprocattr(struct task_struct *p, char *name,
  1947. void *value, size_t size)
  1948. {
  1949. struct cred *new;
  1950. char *newsmack;
  1951. /*
  1952. * Changing another process' Smack value is too dangerous
  1953. * and supports no sane use case.
  1954. */
  1955. if (p != current)
  1956. return -EPERM;
  1957. if (!capable(CAP_MAC_ADMIN))
  1958. return -EPERM;
  1959. if (value == NULL || size == 0 || size >= SMK_LABELLEN)
  1960. return -EINVAL;
  1961. if (strcmp(name, "current") != 0)
  1962. return -EINVAL;
  1963. newsmack = smk_import(value, size);
  1964. if (newsmack == NULL)
  1965. return -EINVAL;
  1966. /*
  1967. * No process is ever allowed the web ("@") label.
  1968. */
  1969. if (newsmack == smack_known_web.smk_known)
  1970. return -EPERM;
  1971. new = prepare_creds();
  1972. if (new == NULL)
  1973. return -ENOMEM;
  1974. new->security = newsmack;
  1975. commit_creds(new);
  1976. return size;
  1977. }
  1978. /**
  1979. * smack_unix_stream_connect - Smack access on UDS
  1980. * @sock: one socket
  1981. * @other: the other socket
  1982. * @newsk: unused
  1983. *
  1984. * Return 0 if a subject with the smack of sock could access
  1985. * an object with the smack of other, otherwise an error code
  1986. */
  1987. static int smack_unix_stream_connect(struct socket *sock,
  1988. struct socket *other, struct sock *newsk)
  1989. {
  1990. struct inode *sp = SOCK_INODE(sock);
  1991. struct inode *op = SOCK_INODE(other);
  1992. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
  1993. }
  1994. /**
  1995. * smack_unix_may_send - Smack access on UDS
  1996. * @sock: one socket
  1997. * @other: the other socket
  1998. *
  1999. * Return 0 if a subject with the smack of sock could access
  2000. * an object with the smack of other, otherwise an error code
  2001. */
  2002. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  2003. {
  2004. struct inode *sp = SOCK_INODE(sock);
  2005. struct inode *op = SOCK_INODE(other);
  2006. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
  2007. }
  2008. /**
  2009. * smack_socket_sendmsg - Smack check based on destination host
  2010. * @sock: the socket
  2011. * @msghdr: the message
  2012. * @size: the size of the message
  2013. *
  2014. * Return 0 if the current subject can write to the destination
  2015. * host. This is only a question if the destination is a single
  2016. * label host.
  2017. */
  2018. static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  2019. int size)
  2020. {
  2021. struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
  2022. struct socket_smack *ssp = sock->sk->sk_security;
  2023. char *hostsp;
  2024. int rc;
  2025. /*
  2026. * Perfectly reasonable for this to be NULL
  2027. */
  2028. if (sip == NULL || sip->sin_family != PF_INET)
  2029. return 0;
  2030. hostsp = smack_host_label(sip);
  2031. if (hostsp == NULL) {
  2032. if (ssp->smk_labeled != SMACK_CIPSO_SOCKET)
  2033. return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  2034. return 0;
  2035. }
  2036. rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
  2037. if (rc != 0)
  2038. return rc;
  2039. if (ssp->smk_labeled != SMACK_UNLABELED_SOCKET)
  2040. return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
  2041. return 0;
  2042. }
  2043. /**
  2044. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
  2045. * pair to smack
  2046. * @sap: netlabel secattr
  2047. * @sip: where to put the result
  2048. *
  2049. * Copies a smack label into sip
  2050. */
  2051. static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
  2052. {
  2053. char smack[SMK_LABELLEN];
  2054. char *sp;
  2055. int pcat;
  2056. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
  2057. /*
  2058. * Looks like a CIPSO packet.
  2059. * If there are flags but no level netlabel isn't
  2060. * behaving the way we expect it to.
  2061. *
  2062. * Get the categories, if any
  2063. * Without guidance regarding the smack value
  2064. * for the packet fall back on the network
  2065. * ambient value.
  2066. */
  2067. memset(smack, '\0', SMK_LABELLEN);
  2068. if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
  2069. for (pcat = -1;;) {
  2070. pcat = netlbl_secattr_catmap_walk(
  2071. sap->attr.mls.cat, pcat + 1);
  2072. if (pcat < 0)
  2073. break;
  2074. smack_catset_bit(pcat, smack);
  2075. }
  2076. /*
  2077. * If it is CIPSO using smack direct mapping
  2078. * we are already done. WeeHee.
  2079. */
  2080. if (sap->attr.mls.lvl == smack_cipso_direct) {
  2081. memcpy(sip, smack, SMK_MAXLEN);
  2082. return;
  2083. }
  2084. /*
  2085. * Look it up in the supplied table if it is not
  2086. * a direct mapping.
  2087. */
  2088. smack_from_cipso(sap->attr.mls.lvl, smack, sip);
  2089. return;
  2090. }
  2091. if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
  2092. /*
  2093. * Looks like a fallback, which gives us a secid.
  2094. */
  2095. sp = smack_from_secid(sap->attr.secid);
  2096. /*
  2097. * This has got to be a bug because it is
  2098. * impossible to specify a fallback without
  2099. * specifying the label, which will ensure
  2100. * it has a secid, and the only way to get a
  2101. * secid is from a fallback.
  2102. */
  2103. BUG_ON(sp == NULL);
  2104. strncpy(sip, sp, SMK_MAXLEN);
  2105. return;
  2106. }
  2107. /*
  2108. * Without guidance regarding the smack value
  2109. * for the packet fall back on the network
  2110. * ambient value.
  2111. */
  2112. strncpy(sip, smack_net_ambient, SMK_MAXLEN);
  2113. return;
  2114. }
  2115. /**
  2116. * smack_socket_sock_rcv_skb - Smack packet delivery access check
  2117. * @sk: socket
  2118. * @skb: packet
  2119. *
  2120. * Returns 0 if the packet should be delivered, an error code otherwise
  2121. */
  2122. static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  2123. {
  2124. struct netlbl_lsm_secattr secattr;
  2125. struct socket_smack *ssp = sk->sk_security;
  2126. char smack[SMK_LABELLEN];
  2127. char *csp;
  2128. int rc;
  2129. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  2130. return 0;
  2131. /*
  2132. * Translate what netlabel gave us.
  2133. */
  2134. netlbl_secattr_init(&secattr);
  2135. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
  2136. if (rc == 0) {
  2137. smack_from_secattr(&secattr, smack);
  2138. csp = smack;
  2139. } else
  2140. csp = smack_net_ambient;
  2141. netlbl_secattr_destroy(&secattr);
  2142. /*
  2143. * Receiving a packet requires that the other end
  2144. * be able to write here. Read access is not required.
  2145. * This is the simplist possible security model
  2146. * for networking.
  2147. */
  2148. rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
  2149. if (rc != 0)
  2150. netlbl_skbuff_err(skb, rc, 0);
  2151. return rc;
  2152. }
  2153. /**
  2154. * smack_socket_getpeersec_stream - pull in packet label
  2155. * @sock: the socket
  2156. * @optval: user's destination
  2157. * @optlen: size thereof
  2158. * @len: max thereoe
  2159. *
  2160. * returns zero on success, an error code otherwise
  2161. */
  2162. static int smack_socket_getpeersec_stream(struct socket *sock,
  2163. char __user *optval,
  2164. int __user *optlen, unsigned len)
  2165. {
  2166. struct socket_smack *ssp;
  2167. int slen;
  2168. int rc = 0;
  2169. ssp = sock->sk->sk_security;
  2170. slen = strlen(ssp->smk_packet) + 1;
  2171. if (slen > len)
  2172. rc = -ERANGE;
  2173. else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
  2174. rc = -EFAULT;
  2175. if (put_user(slen, optlen) != 0)
  2176. rc = -EFAULT;
  2177. return rc;
  2178. }
  2179. /**
  2180. * smack_socket_getpeersec_dgram - pull in packet label
  2181. * @sock: the socket
  2182. * @skb: packet data
  2183. * @secid: pointer to where to put the secid of the packet
  2184. *
  2185. * Sets the netlabel socket state on sk from parent
  2186. */
  2187. static int smack_socket_getpeersec_dgram(struct socket *sock,
  2188. struct sk_buff *skb, u32 *secid)
  2189. {
  2190. struct netlbl_lsm_secattr secattr;
  2191. struct sock *sk;
  2192. char smack[SMK_LABELLEN];
  2193. int family = PF_INET;
  2194. u32 s;
  2195. int rc;
  2196. /*
  2197. * Only works for families with packets.
  2198. */
  2199. if (sock != NULL) {
  2200. sk = sock->sk;
  2201. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  2202. return 0;
  2203. family = sk->sk_family;
  2204. }
  2205. /*
  2206. * Translate what netlabel gave us.
  2207. */
  2208. netlbl_secattr_init(&secattr);
  2209. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  2210. if (rc == 0)
  2211. smack_from_secattr(&secattr, smack);
  2212. netlbl_secattr_destroy(&secattr);
  2213. /*
  2214. * Give up if we couldn't get anything
  2215. */
  2216. if (rc != 0)
  2217. return rc;
  2218. s = smack_to_secid(smack);
  2219. if (s == 0)
  2220. return -EINVAL;
  2221. *secid = s;
  2222. return 0;
  2223. }
  2224. /**
  2225. * smack_sock_graft - graft access state between two sockets
  2226. * @sk: fresh sock
  2227. * @parent: donor socket
  2228. *
  2229. * Sets the netlabel socket state on sk from parent
  2230. */
  2231. static void smack_sock_graft(struct sock *sk, struct socket *parent)
  2232. {
  2233. struct socket_smack *ssp;
  2234. int rc;
  2235. if (sk == NULL)
  2236. return;
  2237. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  2238. return;
  2239. ssp = sk->sk_security;
  2240. ssp->smk_in = ssp->smk_out = current_security();
  2241. ssp->smk_packet[0] = '\0';
  2242. rc = smack_netlabel(sk, SMACK_CIPSO_SOCKET);
  2243. if (rc != 0)
  2244. printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
  2245. __func__, -rc);
  2246. }
  2247. /**
  2248. * smack_inet_conn_request - Smack access check on connect
  2249. * @sk: socket involved
  2250. * @skb: packet
  2251. * @req: unused
  2252. *
  2253. * Returns 0 if a task with the packet label could write to
  2254. * the socket, otherwise an error code
  2255. */
  2256. static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  2257. struct request_sock *req)
  2258. {
  2259. struct netlbl_lsm_secattr skb_secattr;
  2260. struct socket_smack *ssp = sk->sk_security;
  2261. char smack[SMK_LABELLEN];
  2262. int rc;
  2263. if (skb == NULL)
  2264. return -EACCES;
  2265. netlbl_secattr_init(&skb_secattr);
  2266. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
  2267. if (rc == 0)
  2268. smack_from_secattr(&skb_secattr, smack);
  2269. else
  2270. strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
  2271. netlbl_secattr_destroy(&skb_secattr);
  2272. /*
  2273. * Receiving a packet requires that the other end
  2274. * be able to write here. Read access is not required.
  2275. *
  2276. * If the request is successful save the peer's label
  2277. * so that SO_PEERCRED can report it.
  2278. */
  2279. rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
  2280. if (rc == 0)
  2281. strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
  2282. return rc;
  2283. }
  2284. /*
  2285. * Key management security hooks
  2286. *
  2287. * Casey has not tested key support very heavily.
  2288. * The permission check is most likely too restrictive.
  2289. * If you care about keys please have a look.
  2290. */
  2291. #ifdef CONFIG_KEYS
  2292. /**
  2293. * smack_key_alloc - Set the key security blob
  2294. * @key: object
  2295. * @cred: the credentials to use
  2296. * @flags: unused
  2297. *
  2298. * No allocation required
  2299. *
  2300. * Returns 0
  2301. */
  2302. static int smack_key_alloc(struct key *key, const struct cred *cred,
  2303. unsigned long flags)
  2304. {
  2305. key->security = cred->security;
  2306. return 0;
  2307. }
  2308. /**
  2309. * smack_key_free - Clear the key security blob
  2310. * @key: the object
  2311. *
  2312. * Clear the blob pointer
  2313. */
  2314. static void smack_key_free(struct key *key)
  2315. {
  2316. key->security = NULL;
  2317. }
  2318. /*
  2319. * smack_key_permission - Smack access on a key
  2320. * @key_ref: gets to the object
  2321. * @cred: the credentials to use
  2322. * @perm: unused
  2323. *
  2324. * Return 0 if the task has read and write to the object,
  2325. * an error code otherwise
  2326. */
  2327. static int smack_key_permission(key_ref_t key_ref,
  2328. const struct cred *cred, key_perm_t perm)
  2329. {
  2330. struct key *keyp;
  2331. keyp = key_ref_to_ptr(key_ref);
  2332. if (keyp == NULL)
  2333. return -EINVAL;
  2334. /*
  2335. * If the key hasn't been initialized give it access so that
  2336. * it may do so.
  2337. */
  2338. if (keyp->security == NULL)
  2339. return 0;
  2340. /*
  2341. * This should not occur
  2342. */
  2343. if (cred->security == NULL)
  2344. return -EACCES;
  2345. return smk_access(cred->security, keyp->security, MAY_READWRITE);
  2346. }
  2347. #endif /* CONFIG_KEYS */
  2348. /*
  2349. * Smack Audit hooks
  2350. *
  2351. * Audit requires a unique representation of each Smack specific
  2352. * rule. This unique representation is used to distinguish the
  2353. * object to be audited from remaining kernel objects and also
  2354. * works as a glue between the audit hooks.
  2355. *
  2356. * Since repository entries are added but never deleted, we'll use
  2357. * the smack_known label address related to the given audit rule as
  2358. * the needed unique representation. This also better fits the smack
  2359. * model where nearly everything is a label.
  2360. */
  2361. #ifdef CONFIG_AUDIT
  2362. /**
  2363. * smack_audit_rule_init - Initialize a smack audit rule
  2364. * @field: audit rule fields given from user-space (audit.h)
  2365. * @op: required testing operator (=, !=, >, <, ...)
  2366. * @rulestr: smack label to be audited
  2367. * @vrule: pointer to save our own audit rule representation
  2368. *
  2369. * Prepare to audit cases where (@field @op @rulestr) is true.
  2370. * The label to be audited is created if necessay.
  2371. */
  2372. static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2373. {
  2374. char **rule = (char **)vrule;
  2375. *rule = NULL;
  2376. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2377. return -EINVAL;
  2378. if (op != Audit_equal && op != Audit_not_equal)
  2379. return -EINVAL;
  2380. *rule = smk_import(rulestr, 0);
  2381. return 0;
  2382. }
  2383. /**
  2384. * smack_audit_rule_known - Distinguish Smack audit rules
  2385. * @krule: rule of interest, in Audit kernel representation format
  2386. *
  2387. * This is used to filter Smack rules from remaining Audit ones.
  2388. * If it's proved that this rule belongs to us, the
  2389. * audit_rule_match hook will be called to do the final judgement.
  2390. */
  2391. static int smack_audit_rule_known(struct audit_krule *krule)
  2392. {
  2393. struct audit_field *f;
  2394. int i;
  2395. for (i = 0; i < krule->field_count; i++) {
  2396. f = &krule->fields[i];
  2397. if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
  2398. return 1;
  2399. }
  2400. return 0;
  2401. }
  2402. /**
  2403. * smack_audit_rule_match - Audit given object ?
  2404. * @secid: security id for identifying the object to test
  2405. * @field: audit rule flags given from user-space
  2406. * @op: required testing operator
  2407. * @vrule: smack internal rule presentation
  2408. * @actx: audit context associated with the check
  2409. *
  2410. * The core Audit hook. It's used to take the decision of
  2411. * whether to audit or not to audit a given object.
  2412. */
  2413. static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
  2414. struct audit_context *actx)
  2415. {
  2416. char *smack;
  2417. char *rule = vrule;
  2418. if (!rule) {
  2419. audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
  2420. "Smack: missing rule\n");
  2421. return -ENOENT;
  2422. }
  2423. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2424. return 0;
  2425. smack = smack_from_secid(secid);
  2426. /*
  2427. * No need to do string comparisons. If a match occurs,
  2428. * both pointers will point to the same smack_known
  2429. * label.
  2430. */
  2431. if (op == Audit_equal)
  2432. return (rule == smack);
  2433. if (op == Audit_not_equal)
  2434. return (rule != smack);
  2435. return 0;
  2436. }
  2437. /**
  2438. * smack_audit_rule_free - free smack rule representation
  2439. * @vrule: rule to be freed.
  2440. *
  2441. * No memory was allocated.
  2442. */
  2443. static void smack_audit_rule_free(void *vrule)
  2444. {
  2445. /* No-op */
  2446. }
  2447. #endif /* CONFIG_AUDIT */
  2448. /*
  2449. * smack_secid_to_secctx - return the smack label for a secid
  2450. * @secid: incoming integer
  2451. * @secdata: destination
  2452. * @seclen: how long it is
  2453. *
  2454. * Exists for networking code.
  2455. */
  2456. static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  2457. {
  2458. char *sp = smack_from_secid(secid);
  2459. *secdata = sp;
  2460. *seclen = strlen(sp);
  2461. return 0;
  2462. }
  2463. /*
  2464. * smack_secctx_to_secid - return the secid for a smack label
  2465. * @secdata: smack label
  2466. * @seclen: how long result is
  2467. * @secid: outgoing integer
  2468. *
  2469. * Exists for audit and networking code.
  2470. */
  2471. static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  2472. {
  2473. *secid = smack_to_secid(secdata);
  2474. return 0;
  2475. }
  2476. /*
  2477. * smack_release_secctx - don't do anything.
  2478. * @key_ref: unused
  2479. * @context: unused
  2480. * @perm: unused
  2481. *
  2482. * Exists to make sure nothing gets done, and properly
  2483. */
  2484. static void smack_release_secctx(char *secdata, u32 seclen)
  2485. {
  2486. }
  2487. struct security_operations smack_ops = {
  2488. .name = "smack",
  2489. .ptrace_may_access = smack_ptrace_may_access,
  2490. .ptrace_traceme = smack_ptrace_traceme,
  2491. .capget = cap_capget,
  2492. .capset = cap_capset,
  2493. .capable = cap_capable,
  2494. .syslog = smack_syslog,
  2495. .settime = cap_settime,
  2496. .vm_enough_memory = cap_vm_enough_memory,
  2497. .bprm_set_creds = cap_bprm_set_creds,
  2498. .bprm_secureexec = cap_bprm_secureexec,
  2499. .sb_alloc_security = smack_sb_alloc_security,
  2500. .sb_free_security = smack_sb_free_security,
  2501. .sb_copy_data = smack_sb_copy_data,
  2502. .sb_kern_mount = smack_sb_kern_mount,
  2503. .sb_statfs = smack_sb_statfs,
  2504. .sb_mount = smack_sb_mount,
  2505. .sb_umount = smack_sb_umount,
  2506. .inode_alloc_security = smack_inode_alloc_security,
  2507. .inode_free_security = smack_inode_free_security,
  2508. .inode_init_security = smack_inode_init_security,
  2509. .inode_link = smack_inode_link,
  2510. .inode_unlink = smack_inode_unlink,
  2511. .inode_rmdir = smack_inode_rmdir,
  2512. .inode_rename = smack_inode_rename,
  2513. .inode_permission = smack_inode_permission,
  2514. .inode_setattr = smack_inode_setattr,
  2515. .inode_getattr = smack_inode_getattr,
  2516. .inode_setxattr = smack_inode_setxattr,
  2517. .inode_post_setxattr = smack_inode_post_setxattr,
  2518. .inode_getxattr = smack_inode_getxattr,
  2519. .inode_removexattr = smack_inode_removexattr,
  2520. .inode_need_killpriv = cap_inode_need_killpriv,
  2521. .inode_killpriv = cap_inode_killpriv,
  2522. .inode_getsecurity = smack_inode_getsecurity,
  2523. .inode_setsecurity = smack_inode_setsecurity,
  2524. .inode_listsecurity = smack_inode_listsecurity,
  2525. .inode_getsecid = smack_inode_getsecid,
  2526. .file_permission = smack_file_permission,
  2527. .file_alloc_security = smack_file_alloc_security,
  2528. .file_free_security = smack_file_free_security,
  2529. .file_ioctl = smack_file_ioctl,
  2530. .file_lock = smack_file_lock,
  2531. .file_fcntl = smack_file_fcntl,
  2532. .file_set_fowner = smack_file_set_fowner,
  2533. .file_send_sigiotask = smack_file_send_sigiotask,
  2534. .file_receive = smack_file_receive,
  2535. .cred_free = smack_cred_free,
  2536. .cred_prepare = smack_cred_prepare,
  2537. .cred_commit = smack_cred_commit,
  2538. .kernel_act_as = smack_kernel_act_as,
  2539. .kernel_create_files_as = smack_kernel_create_files_as,
  2540. .task_fix_setuid = cap_task_fix_setuid,
  2541. .task_setpgid = smack_task_setpgid,
  2542. .task_getpgid = smack_task_getpgid,
  2543. .task_getsid = smack_task_getsid,
  2544. .task_getsecid = smack_task_getsecid,
  2545. .task_setnice = smack_task_setnice,
  2546. .task_setioprio = smack_task_setioprio,
  2547. .task_getioprio = smack_task_getioprio,
  2548. .task_setscheduler = smack_task_setscheduler,
  2549. .task_getscheduler = smack_task_getscheduler,
  2550. .task_movememory = smack_task_movememory,
  2551. .task_kill = smack_task_kill,
  2552. .task_wait = smack_task_wait,
  2553. .task_to_inode = smack_task_to_inode,
  2554. .task_prctl = cap_task_prctl,
  2555. .ipc_permission = smack_ipc_permission,
  2556. .ipc_getsecid = smack_ipc_getsecid,
  2557. .msg_msg_alloc_security = smack_msg_msg_alloc_security,
  2558. .msg_msg_free_security = smack_msg_msg_free_security,
  2559. .msg_queue_alloc_security = smack_msg_queue_alloc_security,
  2560. .msg_queue_free_security = smack_msg_queue_free_security,
  2561. .msg_queue_associate = smack_msg_queue_associate,
  2562. .msg_queue_msgctl = smack_msg_queue_msgctl,
  2563. .msg_queue_msgsnd = smack_msg_queue_msgsnd,
  2564. .msg_queue_msgrcv = smack_msg_queue_msgrcv,
  2565. .shm_alloc_security = smack_shm_alloc_security,
  2566. .shm_free_security = smack_shm_free_security,
  2567. .shm_associate = smack_shm_associate,
  2568. .shm_shmctl = smack_shm_shmctl,
  2569. .shm_shmat = smack_shm_shmat,
  2570. .sem_alloc_security = smack_sem_alloc_security,
  2571. .sem_free_security = smack_sem_free_security,
  2572. .sem_associate = smack_sem_associate,
  2573. .sem_semctl = smack_sem_semctl,
  2574. .sem_semop = smack_sem_semop,
  2575. .netlink_send = cap_netlink_send,
  2576. .netlink_recv = cap_netlink_recv,
  2577. .d_instantiate = smack_d_instantiate,
  2578. .getprocattr = smack_getprocattr,
  2579. .setprocattr = smack_setprocattr,
  2580. .unix_stream_connect = smack_unix_stream_connect,
  2581. .unix_may_send = smack_unix_may_send,
  2582. .socket_post_create = smack_socket_post_create,
  2583. .socket_connect = smack_socket_connect,
  2584. .socket_sendmsg = smack_socket_sendmsg,
  2585. .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
  2586. .socket_getpeersec_stream = smack_socket_getpeersec_stream,
  2587. .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
  2588. .sk_alloc_security = smack_sk_alloc_security,
  2589. .sk_free_security = smack_sk_free_security,
  2590. .sock_graft = smack_sock_graft,
  2591. .inet_conn_request = smack_inet_conn_request,
  2592. /* key management security hooks */
  2593. #ifdef CONFIG_KEYS
  2594. .key_alloc = smack_key_alloc,
  2595. .key_free = smack_key_free,
  2596. .key_permission = smack_key_permission,
  2597. #endif /* CONFIG_KEYS */
  2598. /* Audit hooks */
  2599. #ifdef CONFIG_AUDIT
  2600. .audit_rule_init = smack_audit_rule_init,
  2601. .audit_rule_known = smack_audit_rule_known,
  2602. .audit_rule_match = smack_audit_rule_match,
  2603. .audit_rule_free = smack_audit_rule_free,
  2604. #endif /* CONFIG_AUDIT */
  2605. .secid_to_secctx = smack_secid_to_secctx,
  2606. .secctx_to_secid = smack_secctx_to_secid,
  2607. .release_secctx = smack_release_secctx,
  2608. };
  2609. /**
  2610. * smack_init - initialize the smack system
  2611. *
  2612. * Returns 0
  2613. */
  2614. static __init int smack_init(void)
  2615. {
  2616. struct cred *cred;
  2617. if (!security_module_enable(&smack_ops))
  2618. return 0;
  2619. printk(KERN_INFO "Smack: Initializing.\n");
  2620. /*
  2621. * Set the security state for the initial task.
  2622. */
  2623. cred = (struct cred *) current->cred;
  2624. cred->security = &smack_known_floor.smk_known;
  2625. /*
  2626. * Initialize locks
  2627. */
  2628. spin_lock_init(&smack_known_huh.smk_cipsolock);
  2629. spin_lock_init(&smack_known_hat.smk_cipsolock);
  2630. spin_lock_init(&smack_known_star.smk_cipsolock);
  2631. spin_lock_init(&smack_known_floor.smk_cipsolock);
  2632. spin_lock_init(&smack_known_invalid.smk_cipsolock);
  2633. /*
  2634. * Register with LSM
  2635. */
  2636. if (register_security(&smack_ops))
  2637. panic("smack: Unable to register with kernel.\n");
  2638. return 0;
  2639. }
  2640. /*
  2641. * Smack requires early initialization in order to label
  2642. * all processes and objects when they are created.
  2643. */
  2644. security_initcall(smack_init);