smack_lsm.c 61 KB

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