smack_lsm.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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 path *path,
  273. char *type, unsigned long flags, void *data)
  274. {
  275. struct superblock_smack *sbp = 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. /*
  982. * Special cases where signals really ought to go through
  983. * in spite of policy. Stephen Smalley suggests it may
  984. * make sense to change the caller so that it doesn't
  985. * bother with the LSM hook in these cases.
  986. */
  987. if (info != SEND_SIG_NOINFO &&
  988. (is_si_special(info) || SI_FROMKERNEL(info)))
  989. return 0;
  990. /*
  991. * Sending a signal requires that the sender
  992. * can write the receiver.
  993. */
  994. if (secid == 0)
  995. return smk_curacc(p->security, MAY_WRITE);
  996. /*
  997. * If the secid isn't 0 we're dealing with some USB IO
  998. * specific behavior. This is not clean. For one thing
  999. * we can't take privilege into account.
  1000. */
  1001. return smk_access(smack_from_secid(secid), p->security, MAY_WRITE);
  1002. }
  1003. /**
  1004. * smack_task_wait - Smack access check for waiting
  1005. * @p: task to wait for
  1006. *
  1007. * Returns 0 if current can wait for p, error code otherwise
  1008. */
  1009. static int smack_task_wait(struct task_struct *p)
  1010. {
  1011. int rc;
  1012. rc = smk_access(current->security, p->security, MAY_WRITE);
  1013. if (rc == 0)
  1014. return 0;
  1015. /*
  1016. * Allow the operation to succeed if either task
  1017. * has privilege to perform operations that might
  1018. * account for the smack labels having gotten to
  1019. * be different in the first place.
  1020. *
  1021. * This breaks the strict subjet/object access
  1022. * control ideal, taking the object's privilege
  1023. * state into account in the decision as well as
  1024. * the smack value.
  1025. */
  1026. if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE))
  1027. return 0;
  1028. return rc;
  1029. }
  1030. /**
  1031. * smack_task_to_inode - copy task smack into the inode blob
  1032. * @p: task to copy from
  1033. * inode: inode to copy to
  1034. *
  1035. * Sets the smack pointer in the inode security blob
  1036. */
  1037. static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
  1038. {
  1039. struct inode_smack *isp = inode->i_security;
  1040. isp->smk_inode = p->security;
  1041. }
  1042. /*
  1043. * Socket hooks.
  1044. */
  1045. /**
  1046. * smack_sk_alloc_security - Allocate a socket blob
  1047. * @sk: the socket
  1048. * @family: unused
  1049. * @priority: memory allocation priority
  1050. *
  1051. * Assign Smack pointers to current
  1052. *
  1053. * Returns 0 on success, -ENOMEM is there's no memory
  1054. */
  1055. static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
  1056. {
  1057. char *csp = current->security;
  1058. struct socket_smack *ssp;
  1059. ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
  1060. if (ssp == NULL)
  1061. return -ENOMEM;
  1062. ssp->smk_in = csp;
  1063. ssp->smk_out = csp;
  1064. ssp->smk_packet[0] = '\0';
  1065. sk->sk_security = ssp;
  1066. return 0;
  1067. }
  1068. /**
  1069. * smack_sk_free_security - Free a socket blob
  1070. * @sk: the socket
  1071. *
  1072. * Clears the blob pointer
  1073. */
  1074. static void smack_sk_free_security(struct sock *sk)
  1075. {
  1076. kfree(sk->sk_security);
  1077. }
  1078. /**
  1079. * smack_set_catset - convert a capset to netlabel mls categories
  1080. * @catset: the Smack categories
  1081. * @sap: where to put the netlabel categories
  1082. *
  1083. * Allocates and fills attr.mls.cat
  1084. */
  1085. static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
  1086. {
  1087. unsigned char *cp;
  1088. unsigned char m;
  1089. int cat;
  1090. int rc;
  1091. int byte;
  1092. if (catset == 0)
  1093. return;
  1094. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  1095. sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1096. sap->attr.mls.cat->startbit = 0;
  1097. for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
  1098. for (m = 0x80; m != 0; m >>= 1, cat++) {
  1099. if ((m & *cp) == 0)
  1100. continue;
  1101. rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
  1102. cat, GFP_ATOMIC);
  1103. }
  1104. }
  1105. /**
  1106. * smack_to_secattr - fill a secattr from a smack value
  1107. * @smack: the smack value
  1108. * @nlsp: where the result goes
  1109. *
  1110. * Casey says that CIPSO is good enough for now.
  1111. * It can be used to effect.
  1112. * It can also be abused to effect when necessary.
  1113. * Appologies to the TSIG group in general and GW in particular.
  1114. */
  1115. static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
  1116. {
  1117. struct smack_cipso cipso;
  1118. int rc;
  1119. switch (smack_net_nltype) {
  1120. case NETLBL_NLTYPE_CIPSOV4:
  1121. nlsp->domain = smack;
  1122. nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  1123. rc = smack_to_cipso(smack, &cipso);
  1124. if (rc == 0) {
  1125. nlsp->attr.mls.lvl = cipso.smk_level;
  1126. smack_set_catset(cipso.smk_catset, nlsp);
  1127. } else {
  1128. nlsp->attr.mls.lvl = smack_cipso_direct;
  1129. smack_set_catset(smack, nlsp);
  1130. }
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. }
  1136. /**
  1137. * smack_netlabel - Set the secattr on a socket
  1138. * @sk: the socket
  1139. *
  1140. * Convert the outbound smack value (smk_out) to a
  1141. * secattr and attach it to the socket.
  1142. *
  1143. * Returns 0 on success or an error code
  1144. */
  1145. static int smack_netlabel(struct sock *sk)
  1146. {
  1147. struct socket_smack *ssp;
  1148. struct netlbl_lsm_secattr secattr;
  1149. int rc;
  1150. ssp = sk->sk_security;
  1151. netlbl_secattr_init(&secattr);
  1152. smack_to_secattr(ssp->smk_out, &secattr);
  1153. rc = netlbl_sock_setattr(sk, &secattr);
  1154. netlbl_secattr_destroy(&secattr);
  1155. return rc;
  1156. }
  1157. /**
  1158. * smack_inode_setsecurity - set smack xattrs
  1159. * @inode: the object
  1160. * @name: attribute name
  1161. * @value: attribute value
  1162. * @size: size of the attribute
  1163. * @flags: unused
  1164. *
  1165. * Sets the named attribute in the appropriate blob
  1166. *
  1167. * Returns 0 on success, or an error code
  1168. */
  1169. static int smack_inode_setsecurity(struct inode *inode, const char *name,
  1170. const void *value, size_t size, int flags)
  1171. {
  1172. char *sp;
  1173. struct inode_smack *nsp = inode->i_security;
  1174. struct socket_smack *ssp;
  1175. struct socket *sock;
  1176. int rc = 0;
  1177. if (value == NULL || size > SMK_LABELLEN)
  1178. return -EACCES;
  1179. sp = smk_import(value, size);
  1180. if (sp == NULL)
  1181. return -EINVAL;
  1182. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
  1183. nsp->smk_inode = sp;
  1184. return 0;
  1185. }
  1186. /*
  1187. * The rest of the Smack xattrs are only on sockets.
  1188. */
  1189. if (inode->i_sb->s_magic != SOCKFS_MAGIC)
  1190. return -EOPNOTSUPP;
  1191. sock = SOCKET_I(inode);
  1192. if (sock == NULL || sock->sk == NULL)
  1193. return -EOPNOTSUPP;
  1194. ssp = sock->sk->sk_security;
  1195. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  1196. ssp->smk_in = sp;
  1197. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
  1198. ssp->smk_out = sp;
  1199. rc = smack_netlabel(sock->sk);
  1200. if (rc != 0)
  1201. printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
  1202. __func__, -rc);
  1203. } else
  1204. return -EOPNOTSUPP;
  1205. return 0;
  1206. }
  1207. /**
  1208. * smack_socket_post_create - finish socket setup
  1209. * @sock: the socket
  1210. * @family: protocol family
  1211. * @type: unused
  1212. * @protocol: unused
  1213. * @kern: unused
  1214. *
  1215. * Sets the netlabel information on the socket
  1216. *
  1217. * Returns 0 on success, and error code otherwise
  1218. */
  1219. static int smack_socket_post_create(struct socket *sock, int family,
  1220. int type, int protocol, int kern)
  1221. {
  1222. if (family != PF_INET || sock->sk == NULL)
  1223. return 0;
  1224. /*
  1225. * Set the outbound netlbl.
  1226. */
  1227. return smack_netlabel(sock->sk);
  1228. }
  1229. /**
  1230. * smack_flags_to_may - convert S_ to MAY_ values
  1231. * @flags: the S_ value
  1232. *
  1233. * Returns the equivalent MAY_ value
  1234. */
  1235. static int smack_flags_to_may(int flags)
  1236. {
  1237. int may = 0;
  1238. if (flags & S_IRUGO)
  1239. may |= MAY_READ;
  1240. if (flags & S_IWUGO)
  1241. may |= MAY_WRITE;
  1242. if (flags & S_IXUGO)
  1243. may |= MAY_EXEC;
  1244. return may;
  1245. }
  1246. /**
  1247. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  1248. * @msg: the object
  1249. *
  1250. * Returns 0
  1251. */
  1252. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  1253. {
  1254. msg->security = current->security;
  1255. return 0;
  1256. }
  1257. /**
  1258. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  1259. * @msg: the object
  1260. *
  1261. * Clears the blob pointer
  1262. */
  1263. static void smack_msg_msg_free_security(struct msg_msg *msg)
  1264. {
  1265. msg->security = NULL;
  1266. }
  1267. /**
  1268. * smack_of_shm - the smack pointer for the shm
  1269. * @shp: the object
  1270. *
  1271. * Returns a pointer to the smack value
  1272. */
  1273. static char *smack_of_shm(struct shmid_kernel *shp)
  1274. {
  1275. return (char *)shp->shm_perm.security;
  1276. }
  1277. /**
  1278. * smack_shm_alloc_security - Set the security blob for shm
  1279. * @shp: the object
  1280. *
  1281. * Returns 0
  1282. */
  1283. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  1284. {
  1285. struct kern_ipc_perm *isp = &shp->shm_perm;
  1286. isp->security = current->security;
  1287. return 0;
  1288. }
  1289. /**
  1290. * smack_shm_free_security - Clear the security blob for shm
  1291. * @shp: the object
  1292. *
  1293. * Clears the blob pointer
  1294. */
  1295. static void smack_shm_free_security(struct shmid_kernel *shp)
  1296. {
  1297. struct kern_ipc_perm *isp = &shp->shm_perm;
  1298. isp->security = NULL;
  1299. }
  1300. /**
  1301. * smack_shm_associate - Smack access check for shm
  1302. * @shp: the object
  1303. * @shmflg: access requested
  1304. *
  1305. * Returns 0 if current has the requested access, error code otherwise
  1306. */
  1307. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  1308. {
  1309. char *ssp = smack_of_shm(shp);
  1310. int may;
  1311. may = smack_flags_to_may(shmflg);
  1312. return smk_curacc(ssp, may);
  1313. }
  1314. /**
  1315. * smack_shm_shmctl - Smack access check for shm
  1316. * @shp: the object
  1317. * @cmd: what it wants to do
  1318. *
  1319. * Returns 0 if current has the requested access, error code otherwise
  1320. */
  1321. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  1322. {
  1323. char *ssp;
  1324. int may;
  1325. switch (cmd) {
  1326. case IPC_STAT:
  1327. case SHM_STAT:
  1328. may = MAY_READ;
  1329. break;
  1330. case IPC_SET:
  1331. case SHM_LOCK:
  1332. case SHM_UNLOCK:
  1333. case IPC_RMID:
  1334. may = MAY_READWRITE;
  1335. break;
  1336. case IPC_INFO:
  1337. case SHM_INFO:
  1338. /*
  1339. * System level information.
  1340. */
  1341. return 0;
  1342. default:
  1343. return -EINVAL;
  1344. }
  1345. ssp = smack_of_shm(shp);
  1346. return smk_curacc(ssp, may);
  1347. }
  1348. /**
  1349. * smack_shm_shmat - Smack access for shmat
  1350. * @shp: the object
  1351. * @shmaddr: unused
  1352. * @shmflg: access requested
  1353. *
  1354. * Returns 0 if current has the requested access, error code otherwise
  1355. */
  1356. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  1357. int shmflg)
  1358. {
  1359. char *ssp = smack_of_shm(shp);
  1360. int may;
  1361. may = smack_flags_to_may(shmflg);
  1362. return smk_curacc(ssp, may);
  1363. }
  1364. /**
  1365. * smack_of_sem - the smack pointer for the sem
  1366. * @sma: the object
  1367. *
  1368. * Returns a pointer to the smack value
  1369. */
  1370. static char *smack_of_sem(struct sem_array *sma)
  1371. {
  1372. return (char *)sma->sem_perm.security;
  1373. }
  1374. /**
  1375. * smack_sem_alloc_security - Set the security blob for sem
  1376. * @sma: the object
  1377. *
  1378. * Returns 0
  1379. */
  1380. static int smack_sem_alloc_security(struct sem_array *sma)
  1381. {
  1382. struct kern_ipc_perm *isp = &sma->sem_perm;
  1383. isp->security = current->security;
  1384. return 0;
  1385. }
  1386. /**
  1387. * smack_sem_free_security - Clear the security blob for sem
  1388. * @sma: the object
  1389. *
  1390. * Clears the blob pointer
  1391. */
  1392. static void smack_sem_free_security(struct sem_array *sma)
  1393. {
  1394. struct kern_ipc_perm *isp = &sma->sem_perm;
  1395. isp->security = NULL;
  1396. }
  1397. /**
  1398. * smack_sem_associate - Smack access check for sem
  1399. * @sma: the object
  1400. * @semflg: access requested
  1401. *
  1402. * Returns 0 if current has the requested access, error code otherwise
  1403. */
  1404. static int smack_sem_associate(struct sem_array *sma, int semflg)
  1405. {
  1406. char *ssp = smack_of_sem(sma);
  1407. int may;
  1408. may = smack_flags_to_may(semflg);
  1409. return smk_curacc(ssp, may);
  1410. }
  1411. /**
  1412. * smack_sem_shmctl - Smack access check for sem
  1413. * @sma: the object
  1414. * @cmd: what it wants to do
  1415. *
  1416. * Returns 0 if current has the requested access, error code otherwise
  1417. */
  1418. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  1419. {
  1420. char *ssp;
  1421. int may;
  1422. switch (cmd) {
  1423. case GETPID:
  1424. case GETNCNT:
  1425. case GETZCNT:
  1426. case GETVAL:
  1427. case GETALL:
  1428. case IPC_STAT:
  1429. case SEM_STAT:
  1430. may = MAY_READ;
  1431. break;
  1432. case SETVAL:
  1433. case SETALL:
  1434. case IPC_RMID:
  1435. case IPC_SET:
  1436. may = MAY_READWRITE;
  1437. break;
  1438. case IPC_INFO:
  1439. case SEM_INFO:
  1440. /*
  1441. * System level information
  1442. */
  1443. return 0;
  1444. default:
  1445. return -EINVAL;
  1446. }
  1447. ssp = smack_of_sem(sma);
  1448. return smk_curacc(ssp, may);
  1449. }
  1450. /**
  1451. * smack_sem_semop - Smack checks of semaphore operations
  1452. * @sma: the object
  1453. * @sops: unused
  1454. * @nsops: unused
  1455. * @alter: unused
  1456. *
  1457. * Treated as read and write in all cases.
  1458. *
  1459. * Returns 0 if access is allowed, error code otherwise
  1460. */
  1461. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  1462. unsigned nsops, int alter)
  1463. {
  1464. char *ssp = smack_of_sem(sma);
  1465. return smk_curacc(ssp, MAY_READWRITE);
  1466. }
  1467. /**
  1468. * smack_msg_alloc_security - Set the security blob for msg
  1469. * @msq: the object
  1470. *
  1471. * Returns 0
  1472. */
  1473. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  1474. {
  1475. struct kern_ipc_perm *kisp = &msq->q_perm;
  1476. kisp->security = current->security;
  1477. return 0;
  1478. }
  1479. /**
  1480. * smack_msg_free_security - Clear the security blob for msg
  1481. * @msq: the object
  1482. *
  1483. * Clears the blob pointer
  1484. */
  1485. static void smack_msg_queue_free_security(struct msg_queue *msq)
  1486. {
  1487. struct kern_ipc_perm *kisp = &msq->q_perm;
  1488. kisp->security = NULL;
  1489. }
  1490. /**
  1491. * smack_of_msq - the smack pointer for the msq
  1492. * @msq: the object
  1493. *
  1494. * Returns a pointer to the smack value
  1495. */
  1496. static char *smack_of_msq(struct msg_queue *msq)
  1497. {
  1498. return (char *)msq->q_perm.security;
  1499. }
  1500. /**
  1501. * smack_msg_queue_associate - Smack access check for msg_queue
  1502. * @msq: the object
  1503. * @msqflg: access requested
  1504. *
  1505. * Returns 0 if current has the requested access, error code otherwise
  1506. */
  1507. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  1508. {
  1509. char *msp = smack_of_msq(msq);
  1510. int may;
  1511. may = smack_flags_to_may(msqflg);
  1512. return smk_curacc(msp, may);
  1513. }
  1514. /**
  1515. * smack_msg_queue_msgctl - Smack access check for msg_queue
  1516. * @msq: the object
  1517. * @cmd: what it wants to do
  1518. *
  1519. * Returns 0 if current has the requested access, error code otherwise
  1520. */
  1521. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  1522. {
  1523. char *msp;
  1524. int may;
  1525. switch (cmd) {
  1526. case IPC_STAT:
  1527. case MSG_STAT:
  1528. may = MAY_READ;
  1529. break;
  1530. case IPC_SET:
  1531. case IPC_RMID:
  1532. may = MAY_READWRITE;
  1533. break;
  1534. case IPC_INFO:
  1535. case MSG_INFO:
  1536. /*
  1537. * System level information
  1538. */
  1539. return 0;
  1540. default:
  1541. return -EINVAL;
  1542. }
  1543. msp = smack_of_msq(msq);
  1544. return smk_curacc(msp, may);
  1545. }
  1546. /**
  1547. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1548. * @msq: the object
  1549. * @msg: unused
  1550. * @msqflg: access requested
  1551. *
  1552. * Returns 0 if current has the requested access, error code otherwise
  1553. */
  1554. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  1555. int msqflg)
  1556. {
  1557. char *msp = smack_of_msq(msq);
  1558. int rc;
  1559. rc = smack_flags_to_may(msqflg);
  1560. return smk_curacc(msp, rc);
  1561. }
  1562. /**
  1563. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1564. * @msq: the object
  1565. * @msg: unused
  1566. * @target: unused
  1567. * @type: unused
  1568. * @mode: unused
  1569. *
  1570. * Returns 0 if current has read and write access, error code otherwise
  1571. */
  1572. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  1573. struct task_struct *target, long type, int mode)
  1574. {
  1575. char *msp = smack_of_msq(msq);
  1576. return smk_curacc(msp, MAY_READWRITE);
  1577. }
  1578. /**
  1579. * smack_ipc_permission - Smack access for ipc_permission()
  1580. * @ipp: the object permissions
  1581. * @flag: access requested
  1582. *
  1583. * Returns 0 if current has read and write access, error code otherwise
  1584. */
  1585. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  1586. {
  1587. char *isp = ipp->security;
  1588. int may;
  1589. may = smack_flags_to_may(flag);
  1590. return smk_curacc(isp, may);
  1591. }
  1592. /* module stacking operations */
  1593. /**
  1594. * smack_register_security - stack capability module
  1595. * @name: module name
  1596. * @ops: module operations - ignored
  1597. *
  1598. * Allow the capability module to register.
  1599. */
  1600. static int smack_register_security(const char *name,
  1601. struct security_operations *ops)
  1602. {
  1603. if (strcmp(name, "capability") != 0)
  1604. return -EINVAL;
  1605. printk(KERN_INFO "%s: Registering secondary module %s\n",
  1606. __func__, name);
  1607. return 0;
  1608. }
  1609. /**
  1610. * smack_d_instantiate - Make sure the blob is correct on an inode
  1611. * @opt_dentry: unused
  1612. * @inode: the object
  1613. *
  1614. * Set the inode's security blob if it hasn't been done already.
  1615. */
  1616. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  1617. {
  1618. struct super_block *sbp;
  1619. struct superblock_smack *sbsp;
  1620. struct inode_smack *isp;
  1621. char *csp = current->security;
  1622. char *fetched;
  1623. char *final;
  1624. struct dentry *dp;
  1625. if (inode == NULL)
  1626. return;
  1627. isp = inode->i_security;
  1628. mutex_lock(&isp->smk_lock);
  1629. /*
  1630. * If the inode is already instantiated
  1631. * take the quick way out
  1632. */
  1633. if (isp->smk_flags & SMK_INODE_INSTANT)
  1634. goto unlockandout;
  1635. sbp = inode->i_sb;
  1636. sbsp = sbp->s_security;
  1637. /*
  1638. * We're going to use the superblock default label
  1639. * if there's no label on the file.
  1640. */
  1641. final = sbsp->smk_default;
  1642. /*
  1643. * This is pretty hackish.
  1644. * Casey says that we shouldn't have to do
  1645. * file system specific code, but it does help
  1646. * with keeping it simple.
  1647. */
  1648. switch (sbp->s_magic) {
  1649. case SMACK_MAGIC:
  1650. /*
  1651. * Casey says that it's a little embarassing
  1652. * that the smack file system doesn't do
  1653. * extended attributes.
  1654. */
  1655. final = smack_known_star.smk_known;
  1656. break;
  1657. case PIPEFS_MAGIC:
  1658. /*
  1659. * Casey says pipes are easy (?)
  1660. */
  1661. final = smack_known_star.smk_known;
  1662. break;
  1663. case DEVPTS_SUPER_MAGIC:
  1664. /*
  1665. * devpts seems content with the label of the task.
  1666. * Programs that change smack have to treat the
  1667. * pty with respect.
  1668. */
  1669. final = csp;
  1670. break;
  1671. case SOCKFS_MAGIC:
  1672. /*
  1673. * Casey says sockets get the smack of the task.
  1674. */
  1675. final = csp;
  1676. break;
  1677. case PROC_SUPER_MAGIC:
  1678. /*
  1679. * Casey says procfs appears not to care.
  1680. * The superblock default suffices.
  1681. */
  1682. break;
  1683. case TMPFS_MAGIC:
  1684. /*
  1685. * Device labels should come from the filesystem,
  1686. * but watch out, because they're volitile,
  1687. * getting recreated on every reboot.
  1688. */
  1689. final = smack_known_star.smk_known;
  1690. /*
  1691. * No break.
  1692. *
  1693. * If a smack value has been set we want to use it,
  1694. * but since tmpfs isn't giving us the opportunity
  1695. * to set mount options simulate setting the
  1696. * superblock default.
  1697. */
  1698. default:
  1699. /*
  1700. * This isn't an understood special case.
  1701. * Get the value from the xattr.
  1702. *
  1703. * No xattr support means, alas, no SMACK label.
  1704. * Use the aforeapplied default.
  1705. * It would be curious if the label of the task
  1706. * does not match that assigned.
  1707. */
  1708. if (inode->i_op->getxattr == NULL)
  1709. break;
  1710. /*
  1711. * Get the dentry for xattr.
  1712. */
  1713. if (opt_dentry == NULL) {
  1714. dp = d_find_alias(inode);
  1715. if (dp == NULL)
  1716. break;
  1717. } else {
  1718. dp = dget(opt_dentry);
  1719. if (dp == NULL)
  1720. break;
  1721. }
  1722. fetched = smk_fetch(inode, dp);
  1723. if (fetched != NULL)
  1724. final = fetched;
  1725. dput(dp);
  1726. break;
  1727. }
  1728. if (final == NULL)
  1729. isp->smk_inode = csp;
  1730. else
  1731. isp->smk_inode = final;
  1732. isp->smk_flags |= SMK_INODE_INSTANT;
  1733. unlockandout:
  1734. mutex_unlock(&isp->smk_lock);
  1735. return;
  1736. }
  1737. /**
  1738. * smack_getprocattr - Smack process attribute access
  1739. * @p: the object task
  1740. * @name: the name of the attribute in /proc/.../attr
  1741. * @value: where to put the result
  1742. *
  1743. * Places a copy of the task Smack into value
  1744. *
  1745. * Returns the length of the smack label or an error code
  1746. */
  1747. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  1748. {
  1749. char *cp;
  1750. int slen;
  1751. if (strcmp(name, "current") != 0)
  1752. return -EINVAL;
  1753. cp = kstrdup(p->security, GFP_KERNEL);
  1754. if (cp == NULL)
  1755. return -ENOMEM;
  1756. slen = strlen(cp);
  1757. *value = cp;
  1758. return slen;
  1759. }
  1760. /**
  1761. * smack_setprocattr - Smack process attribute setting
  1762. * @p: the object task
  1763. * @name: the name of the attribute in /proc/.../attr
  1764. * @value: the value to set
  1765. * @size: the size of the value
  1766. *
  1767. * Sets the Smack value of the task. Only setting self
  1768. * is permitted and only with privilege
  1769. *
  1770. * Returns the length of the smack label or an error code
  1771. */
  1772. static int smack_setprocattr(struct task_struct *p, char *name,
  1773. void *value, size_t size)
  1774. {
  1775. char *newsmack;
  1776. if (!__capable(p, CAP_MAC_ADMIN))
  1777. return -EPERM;
  1778. /*
  1779. * Changing another process' Smack value is too dangerous
  1780. * and supports no sane use case.
  1781. */
  1782. if (p != current)
  1783. return -EPERM;
  1784. if (value == NULL || size == 0 || size >= SMK_LABELLEN)
  1785. return -EINVAL;
  1786. if (strcmp(name, "current") != 0)
  1787. return -EINVAL;
  1788. newsmack = smk_import(value, size);
  1789. if (newsmack == NULL)
  1790. return -EINVAL;
  1791. p->security = newsmack;
  1792. return size;
  1793. }
  1794. /**
  1795. * smack_unix_stream_connect - Smack access on UDS
  1796. * @sock: one socket
  1797. * @other: the other socket
  1798. * @newsk: unused
  1799. *
  1800. * Return 0 if a subject with the smack of sock could access
  1801. * an object with the smack of other, otherwise an error code
  1802. */
  1803. static int smack_unix_stream_connect(struct socket *sock,
  1804. struct socket *other, struct sock *newsk)
  1805. {
  1806. struct inode *sp = SOCK_INODE(sock);
  1807. struct inode *op = SOCK_INODE(other);
  1808. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
  1809. }
  1810. /**
  1811. * smack_unix_may_send - Smack access on UDS
  1812. * @sock: one socket
  1813. * @other: the other socket
  1814. *
  1815. * Return 0 if a subject with the smack of sock could access
  1816. * an object with the smack of other, otherwise an error code
  1817. */
  1818. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  1819. {
  1820. struct inode *sp = SOCK_INODE(sock);
  1821. struct inode *op = SOCK_INODE(other);
  1822. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
  1823. }
  1824. /**
  1825. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
  1826. * pair to smack
  1827. * @sap: netlabel secattr
  1828. * @sip: where to put the result
  1829. *
  1830. * Copies a smack label into sip
  1831. */
  1832. static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
  1833. {
  1834. char smack[SMK_LABELLEN];
  1835. int pcat;
  1836. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
  1837. /*
  1838. * If there are flags but no level netlabel isn't
  1839. * behaving the way we expect it to.
  1840. *
  1841. * Without guidance regarding the smack value
  1842. * for the packet fall back on the network
  1843. * ambient value.
  1844. */
  1845. strncpy(sip, smack_net_ambient, SMK_MAXLEN);
  1846. return;
  1847. }
  1848. /*
  1849. * Get the categories, if any
  1850. */
  1851. memset(smack, '\0', SMK_LABELLEN);
  1852. if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
  1853. for (pcat = -1;;) {
  1854. pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
  1855. pcat + 1);
  1856. if (pcat < 0)
  1857. break;
  1858. smack_catset_bit(pcat, smack);
  1859. }
  1860. /*
  1861. * If it is CIPSO using smack direct mapping
  1862. * we are already done. WeeHee.
  1863. */
  1864. if (sap->attr.mls.lvl == smack_cipso_direct) {
  1865. memcpy(sip, smack, SMK_MAXLEN);
  1866. return;
  1867. }
  1868. /*
  1869. * Look it up in the supplied table if it is not a direct mapping.
  1870. */
  1871. smack_from_cipso(sap->attr.mls.lvl, smack, sip);
  1872. return;
  1873. }
  1874. /**
  1875. * smack_socket_sock_rcv_skb - Smack packet delivery access check
  1876. * @sk: socket
  1877. * @skb: packet
  1878. *
  1879. * Returns 0 if the packet should be delivered, an error code otherwise
  1880. */
  1881. static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1882. {
  1883. struct netlbl_lsm_secattr secattr;
  1884. struct socket_smack *ssp = sk->sk_security;
  1885. char smack[SMK_LABELLEN];
  1886. int rc;
  1887. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1888. return 0;
  1889. /*
  1890. * Translate what netlabel gave us.
  1891. */
  1892. memset(smack, '\0', SMK_LABELLEN);
  1893. netlbl_secattr_init(&secattr);
  1894. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
  1895. if (rc == 0)
  1896. smack_from_secattr(&secattr, smack);
  1897. else
  1898. strncpy(smack, smack_net_ambient, SMK_MAXLEN);
  1899. netlbl_secattr_destroy(&secattr);
  1900. /*
  1901. * Receiving a packet requires that the other end
  1902. * be able to write here. Read access is not required.
  1903. * This is the simplist possible security model
  1904. * for networking.
  1905. */
  1906. return smk_access(smack, ssp->smk_in, MAY_WRITE);
  1907. }
  1908. /**
  1909. * smack_socket_getpeersec_stream - pull in packet label
  1910. * @sock: the socket
  1911. * @optval: user's destination
  1912. * @optlen: size thereof
  1913. * @len: max thereoe
  1914. *
  1915. * returns zero on success, an error code otherwise
  1916. */
  1917. static int smack_socket_getpeersec_stream(struct socket *sock,
  1918. char __user *optval,
  1919. int __user *optlen, unsigned len)
  1920. {
  1921. struct socket_smack *ssp;
  1922. int slen;
  1923. int rc = 0;
  1924. ssp = sock->sk->sk_security;
  1925. slen = strlen(ssp->smk_packet) + 1;
  1926. if (slen > len)
  1927. rc = -ERANGE;
  1928. else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
  1929. rc = -EFAULT;
  1930. if (put_user(slen, optlen) != 0)
  1931. rc = -EFAULT;
  1932. return rc;
  1933. }
  1934. /**
  1935. * smack_socket_getpeersec_dgram - pull in packet label
  1936. * @sock: the socket
  1937. * @skb: packet data
  1938. * @secid: pointer to where to put the secid of the packet
  1939. *
  1940. * Sets the netlabel socket state on sk from parent
  1941. */
  1942. static int smack_socket_getpeersec_dgram(struct socket *sock,
  1943. struct sk_buff *skb, u32 *secid)
  1944. {
  1945. struct netlbl_lsm_secattr secattr;
  1946. struct sock *sk;
  1947. char smack[SMK_LABELLEN];
  1948. int family = PF_INET;
  1949. u32 s;
  1950. int rc;
  1951. /*
  1952. * Only works for families with packets.
  1953. */
  1954. if (sock != NULL) {
  1955. sk = sock->sk;
  1956. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1957. return 0;
  1958. family = sk->sk_family;
  1959. }
  1960. /*
  1961. * Translate what netlabel gave us.
  1962. */
  1963. memset(smack, '\0', SMK_LABELLEN);
  1964. netlbl_secattr_init(&secattr);
  1965. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  1966. if (rc == 0)
  1967. smack_from_secattr(&secattr, smack);
  1968. netlbl_secattr_destroy(&secattr);
  1969. /*
  1970. * Give up if we couldn't get anything
  1971. */
  1972. if (rc != 0)
  1973. return rc;
  1974. s = smack_to_secid(smack);
  1975. if (s == 0)
  1976. return -EINVAL;
  1977. *secid = s;
  1978. return 0;
  1979. }
  1980. /**
  1981. * smack_sock_graft - graft access state between two sockets
  1982. * @sk: fresh sock
  1983. * @parent: donor socket
  1984. *
  1985. * Sets the netlabel socket state on sk from parent
  1986. */
  1987. static void smack_sock_graft(struct sock *sk, struct socket *parent)
  1988. {
  1989. struct socket_smack *ssp;
  1990. int rc;
  1991. if (sk == NULL)
  1992. return;
  1993. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1994. return;
  1995. ssp = sk->sk_security;
  1996. ssp->smk_in = current->security;
  1997. ssp->smk_out = current->security;
  1998. ssp->smk_packet[0] = '\0';
  1999. rc = smack_netlabel(sk);
  2000. if (rc != 0)
  2001. printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
  2002. __func__, -rc);
  2003. }
  2004. /**
  2005. * smack_inet_conn_request - Smack access check on connect
  2006. * @sk: socket involved
  2007. * @skb: packet
  2008. * @req: unused
  2009. *
  2010. * Returns 0 if a task with the packet label could write to
  2011. * the socket, otherwise an error code
  2012. */
  2013. static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  2014. struct request_sock *req)
  2015. {
  2016. struct netlbl_lsm_secattr skb_secattr;
  2017. struct socket_smack *ssp = sk->sk_security;
  2018. char smack[SMK_LABELLEN];
  2019. int rc;
  2020. if (skb == NULL)
  2021. return -EACCES;
  2022. memset(smack, '\0', SMK_LABELLEN);
  2023. netlbl_secattr_init(&skb_secattr);
  2024. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
  2025. if (rc == 0)
  2026. smack_from_secattr(&skb_secattr, smack);
  2027. else
  2028. strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
  2029. netlbl_secattr_destroy(&skb_secattr);
  2030. /*
  2031. * Receiving a packet requires that the other end
  2032. * be able to write here. Read access is not required.
  2033. *
  2034. * If the request is successful save the peer's label
  2035. * so that SO_PEERCRED can report it.
  2036. */
  2037. rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
  2038. if (rc == 0)
  2039. strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
  2040. return rc;
  2041. }
  2042. /*
  2043. * Key management security hooks
  2044. *
  2045. * Casey has not tested key support very heavily.
  2046. * The permission check is most likely too restrictive.
  2047. * If you care about keys please have a look.
  2048. */
  2049. #ifdef CONFIG_KEYS
  2050. /**
  2051. * smack_key_alloc - Set the key security blob
  2052. * @key: object
  2053. * @tsk: the task associated with the key
  2054. * @flags: unused
  2055. *
  2056. * No allocation required
  2057. *
  2058. * Returns 0
  2059. */
  2060. static int smack_key_alloc(struct key *key, struct task_struct *tsk,
  2061. unsigned long flags)
  2062. {
  2063. key->security = tsk->security;
  2064. return 0;
  2065. }
  2066. /**
  2067. * smack_key_free - Clear the key security blob
  2068. * @key: the object
  2069. *
  2070. * Clear the blob pointer
  2071. */
  2072. static void smack_key_free(struct key *key)
  2073. {
  2074. key->security = NULL;
  2075. }
  2076. /*
  2077. * smack_key_permission - Smack access on a key
  2078. * @key_ref: gets to the object
  2079. * @context: task involved
  2080. * @perm: unused
  2081. *
  2082. * Return 0 if the task has read and write to the object,
  2083. * an error code otherwise
  2084. */
  2085. static int smack_key_permission(key_ref_t key_ref,
  2086. struct task_struct *context, key_perm_t perm)
  2087. {
  2088. struct key *keyp;
  2089. keyp = key_ref_to_ptr(key_ref);
  2090. if (keyp == NULL)
  2091. return -EINVAL;
  2092. /*
  2093. * If the key hasn't been initialized give it access so that
  2094. * it may do so.
  2095. */
  2096. if (keyp->security == NULL)
  2097. return 0;
  2098. /*
  2099. * This should not occur
  2100. */
  2101. if (context->security == NULL)
  2102. return -EACCES;
  2103. return smk_access(context->security, keyp->security, MAY_READWRITE);
  2104. }
  2105. #endif /* CONFIG_KEYS */
  2106. /*
  2107. * smack_secid_to_secctx - return the smack label for a secid
  2108. * @secid: incoming integer
  2109. * @secdata: destination
  2110. * @seclen: how long it is
  2111. *
  2112. * Exists for networking code.
  2113. */
  2114. static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  2115. {
  2116. char *sp = smack_from_secid(secid);
  2117. *secdata = sp;
  2118. *seclen = strlen(sp);
  2119. return 0;
  2120. }
  2121. /*
  2122. * smack_secctx_to_secid - return the secid for a smack label
  2123. * @secdata: smack label
  2124. * @seclen: how long result is
  2125. * @secid: outgoing integer
  2126. *
  2127. * Exists for audit and networking code.
  2128. */
  2129. static int smack_secctx_to_secid(char *secdata, u32 seclen, u32 *secid)
  2130. {
  2131. *secid = smack_to_secid(secdata);
  2132. return 0;
  2133. }
  2134. /*
  2135. * smack_release_secctx - don't do anything.
  2136. * @key_ref: unused
  2137. * @context: unused
  2138. * @perm: unused
  2139. *
  2140. * Exists to make sure nothing gets done, and properly
  2141. */
  2142. static void smack_release_secctx(char *secdata, u32 seclen)
  2143. {
  2144. }
  2145. struct security_operations smack_ops = {
  2146. .name = "smack",
  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. if (!security_module_enable(&smack_ops))
  2263. return 0;
  2264. printk(KERN_INFO "Smack: Initializing.\n");
  2265. /*
  2266. * Set the security state for the initial task.
  2267. */
  2268. current->security = &smack_known_floor.smk_known;
  2269. /*
  2270. * Initialize locks
  2271. */
  2272. spin_lock_init(&smack_known_unset.smk_cipsolock);
  2273. spin_lock_init(&smack_known_huh.smk_cipsolock);
  2274. spin_lock_init(&smack_known_hat.smk_cipsolock);
  2275. spin_lock_init(&smack_known_star.smk_cipsolock);
  2276. spin_lock_init(&smack_known_floor.smk_cipsolock);
  2277. spin_lock_init(&smack_known_invalid.smk_cipsolock);
  2278. /*
  2279. * Register with LSM
  2280. */
  2281. if (register_security(&smack_ops))
  2282. panic("smack: Unable to register with kernel.\n");
  2283. return 0;
  2284. }
  2285. /*
  2286. * Smack requires early initialization in order to label
  2287. * all processes and objects when they are created.
  2288. */
  2289. security_initcall(smack_init);