smack_lsm.c 57 KB

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