smack_lsm.c 61 KB

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