smack_lsm.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  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. * Sending a signal requires that the sender
  994. * can write the receiver.
  995. */
  996. if (secid == 0)
  997. return smk_curacc(p->security, MAY_WRITE);
  998. /*
  999. * If the secid isn't 0 we're dealing with some USB IO
  1000. * specific behavior. This is not clean. For one thing
  1001. * we can't take privilege into account.
  1002. */
  1003. return smk_access(smack_from_secid(secid), p->security, MAY_WRITE);
  1004. }
  1005. /**
  1006. * smack_task_wait - Smack access check for waiting
  1007. * @p: task to wait for
  1008. *
  1009. * Returns 0 if current can wait for p, error code otherwise
  1010. */
  1011. static int smack_task_wait(struct task_struct *p)
  1012. {
  1013. int rc;
  1014. rc = smk_access(current->security, p->security, MAY_WRITE);
  1015. if (rc == 0)
  1016. return 0;
  1017. /*
  1018. * Allow the operation to succeed if either task
  1019. * has privilege to perform operations that might
  1020. * account for the smack labels having gotten to
  1021. * be different in the first place.
  1022. *
  1023. * This breaks the strict subjet/object access
  1024. * control ideal, taking the object's privilege
  1025. * state into account in the decision as well as
  1026. * the smack value.
  1027. */
  1028. if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE))
  1029. return 0;
  1030. return rc;
  1031. }
  1032. /**
  1033. * smack_task_to_inode - copy task smack into the inode blob
  1034. * @p: task to copy from
  1035. * inode: inode to copy to
  1036. *
  1037. * Sets the smack pointer in the inode security blob
  1038. */
  1039. static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
  1040. {
  1041. struct inode_smack *isp = inode->i_security;
  1042. isp->smk_inode = p->security;
  1043. }
  1044. /*
  1045. * Socket hooks.
  1046. */
  1047. /**
  1048. * smack_sk_alloc_security - Allocate a socket blob
  1049. * @sk: the socket
  1050. * @family: unused
  1051. * @priority: memory allocation priority
  1052. *
  1053. * Assign Smack pointers to current
  1054. *
  1055. * Returns 0 on success, -ENOMEM is there's no memory
  1056. */
  1057. static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
  1058. {
  1059. char *csp = current->security;
  1060. struct socket_smack *ssp;
  1061. ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
  1062. if (ssp == NULL)
  1063. return -ENOMEM;
  1064. ssp->smk_in = csp;
  1065. ssp->smk_out = csp;
  1066. ssp->smk_packet[0] = '\0';
  1067. sk->sk_security = ssp;
  1068. return 0;
  1069. }
  1070. /**
  1071. * smack_sk_free_security - Free a socket blob
  1072. * @sk: the socket
  1073. *
  1074. * Clears the blob pointer
  1075. */
  1076. static void smack_sk_free_security(struct sock *sk)
  1077. {
  1078. kfree(sk->sk_security);
  1079. }
  1080. /**
  1081. * smack_set_catset - convert a capset to netlabel mls categories
  1082. * @catset: the Smack categories
  1083. * @sap: where to put the netlabel categories
  1084. *
  1085. * Allocates and fills attr.mls.cat
  1086. */
  1087. static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
  1088. {
  1089. unsigned char *cp;
  1090. unsigned char m;
  1091. int cat;
  1092. int rc;
  1093. int byte;
  1094. if (!catset)
  1095. return;
  1096. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  1097. sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1098. sap->attr.mls.cat->startbit = 0;
  1099. for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
  1100. for (m = 0x80; m != 0; m >>= 1, cat++) {
  1101. if ((m & *cp) == 0)
  1102. continue;
  1103. rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
  1104. cat, GFP_ATOMIC);
  1105. }
  1106. }
  1107. /**
  1108. * smack_to_secattr - fill a secattr from a smack value
  1109. * @smack: the smack value
  1110. * @nlsp: where the result goes
  1111. *
  1112. * Casey says that CIPSO is good enough for now.
  1113. * It can be used to effect.
  1114. * It can also be abused to effect when necessary.
  1115. * Appologies to the TSIG group in general and GW in particular.
  1116. */
  1117. static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
  1118. {
  1119. struct smack_cipso cipso;
  1120. int rc;
  1121. switch (smack_net_nltype) {
  1122. case NETLBL_NLTYPE_CIPSOV4:
  1123. nlsp->domain = smack;
  1124. nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  1125. rc = smack_to_cipso(smack, &cipso);
  1126. if (rc == 0) {
  1127. nlsp->attr.mls.lvl = cipso.smk_level;
  1128. smack_set_catset(cipso.smk_catset, nlsp);
  1129. } else {
  1130. nlsp->attr.mls.lvl = smack_cipso_direct;
  1131. smack_set_catset(smack, nlsp);
  1132. }
  1133. break;
  1134. default:
  1135. break;
  1136. }
  1137. }
  1138. /**
  1139. * smack_netlabel - Set the secattr on a socket
  1140. * @sk: the socket
  1141. *
  1142. * Convert the outbound smack value (smk_out) to a
  1143. * secattr and attach it to the socket.
  1144. *
  1145. * Returns 0 on success or an error code
  1146. */
  1147. static int smack_netlabel(struct sock *sk)
  1148. {
  1149. struct socket_smack *ssp;
  1150. struct netlbl_lsm_secattr secattr;
  1151. int rc;
  1152. ssp = sk->sk_security;
  1153. netlbl_secattr_init(&secattr);
  1154. smack_to_secattr(ssp->smk_out, &secattr);
  1155. rc = netlbl_sock_setattr(sk, &secattr);
  1156. netlbl_secattr_destroy(&secattr);
  1157. return rc;
  1158. }
  1159. /**
  1160. * smack_inode_setsecurity - set smack xattrs
  1161. * @inode: the object
  1162. * @name: attribute name
  1163. * @value: attribute value
  1164. * @size: size of the attribute
  1165. * @flags: unused
  1166. *
  1167. * Sets the named attribute in the appropriate blob
  1168. *
  1169. * Returns 0 on success, or an error code
  1170. */
  1171. static int smack_inode_setsecurity(struct inode *inode, const char *name,
  1172. const void *value, size_t size, int flags)
  1173. {
  1174. char *sp;
  1175. struct inode_smack *nsp = inode->i_security;
  1176. struct socket_smack *ssp;
  1177. struct socket *sock;
  1178. int rc = 0;
  1179. if (value == NULL || size > SMK_LABELLEN)
  1180. return -EACCES;
  1181. sp = smk_import(value, size);
  1182. if (sp == NULL)
  1183. return -EINVAL;
  1184. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
  1185. nsp->smk_inode = sp;
  1186. return 0;
  1187. }
  1188. /*
  1189. * The rest of the Smack xattrs are only on sockets.
  1190. */
  1191. if (inode->i_sb->s_magic != SOCKFS_MAGIC)
  1192. return -EOPNOTSUPP;
  1193. sock = SOCKET_I(inode);
  1194. if (sock == NULL || sock->sk == NULL)
  1195. return -EOPNOTSUPP;
  1196. ssp = sock->sk->sk_security;
  1197. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  1198. ssp->smk_in = sp;
  1199. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
  1200. ssp->smk_out = sp;
  1201. rc = smack_netlabel(sock->sk);
  1202. if (rc != 0)
  1203. printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
  1204. __func__, -rc);
  1205. } else
  1206. return -EOPNOTSUPP;
  1207. return 0;
  1208. }
  1209. /**
  1210. * smack_socket_post_create - finish socket setup
  1211. * @sock: the socket
  1212. * @family: protocol family
  1213. * @type: unused
  1214. * @protocol: unused
  1215. * @kern: unused
  1216. *
  1217. * Sets the netlabel information on the socket
  1218. *
  1219. * Returns 0 on success, and error code otherwise
  1220. */
  1221. static int smack_socket_post_create(struct socket *sock, int family,
  1222. int type, int protocol, int kern)
  1223. {
  1224. if (family != PF_INET || sock->sk == NULL)
  1225. return 0;
  1226. /*
  1227. * Set the outbound netlbl.
  1228. */
  1229. return smack_netlabel(sock->sk);
  1230. }
  1231. /**
  1232. * smack_flags_to_may - convert S_ to MAY_ values
  1233. * @flags: the S_ value
  1234. *
  1235. * Returns the equivalent MAY_ value
  1236. */
  1237. static int smack_flags_to_may(int flags)
  1238. {
  1239. int may = 0;
  1240. if (flags & S_IRUGO)
  1241. may |= MAY_READ;
  1242. if (flags & S_IWUGO)
  1243. may |= MAY_WRITE;
  1244. if (flags & S_IXUGO)
  1245. may |= MAY_EXEC;
  1246. return may;
  1247. }
  1248. /**
  1249. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  1250. * @msg: the object
  1251. *
  1252. * Returns 0
  1253. */
  1254. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  1255. {
  1256. msg->security = current->security;
  1257. return 0;
  1258. }
  1259. /**
  1260. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  1261. * @msg: the object
  1262. *
  1263. * Clears the blob pointer
  1264. */
  1265. static void smack_msg_msg_free_security(struct msg_msg *msg)
  1266. {
  1267. msg->security = NULL;
  1268. }
  1269. /**
  1270. * smack_of_shm - the smack pointer for the shm
  1271. * @shp: the object
  1272. *
  1273. * Returns a pointer to the smack value
  1274. */
  1275. static char *smack_of_shm(struct shmid_kernel *shp)
  1276. {
  1277. return (char *)shp->shm_perm.security;
  1278. }
  1279. /**
  1280. * smack_shm_alloc_security - Set the security blob for shm
  1281. * @shp: the object
  1282. *
  1283. * Returns 0
  1284. */
  1285. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  1286. {
  1287. struct kern_ipc_perm *isp = &shp->shm_perm;
  1288. isp->security = current->security;
  1289. return 0;
  1290. }
  1291. /**
  1292. * smack_shm_free_security - Clear the security blob for shm
  1293. * @shp: the object
  1294. *
  1295. * Clears the blob pointer
  1296. */
  1297. static void smack_shm_free_security(struct shmid_kernel *shp)
  1298. {
  1299. struct kern_ipc_perm *isp = &shp->shm_perm;
  1300. isp->security = NULL;
  1301. }
  1302. /**
  1303. * smack_shm_associate - Smack access check for shm
  1304. * @shp: the object
  1305. * @shmflg: access requested
  1306. *
  1307. * Returns 0 if current has the requested access, error code otherwise
  1308. */
  1309. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  1310. {
  1311. char *ssp = smack_of_shm(shp);
  1312. int may;
  1313. may = smack_flags_to_may(shmflg);
  1314. return smk_curacc(ssp, may);
  1315. }
  1316. /**
  1317. * smack_shm_shmctl - Smack access check for shm
  1318. * @shp: the object
  1319. * @cmd: what it wants to do
  1320. *
  1321. * Returns 0 if current has the requested access, error code otherwise
  1322. */
  1323. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  1324. {
  1325. char *ssp;
  1326. int may;
  1327. switch (cmd) {
  1328. case IPC_STAT:
  1329. case SHM_STAT:
  1330. may = MAY_READ;
  1331. break;
  1332. case IPC_SET:
  1333. case SHM_LOCK:
  1334. case SHM_UNLOCK:
  1335. case IPC_RMID:
  1336. may = MAY_READWRITE;
  1337. break;
  1338. case IPC_INFO:
  1339. case SHM_INFO:
  1340. /*
  1341. * System level information.
  1342. */
  1343. return 0;
  1344. default:
  1345. return -EINVAL;
  1346. }
  1347. ssp = smack_of_shm(shp);
  1348. return smk_curacc(ssp, may);
  1349. }
  1350. /**
  1351. * smack_shm_shmat - Smack access for shmat
  1352. * @shp: the object
  1353. * @shmaddr: unused
  1354. * @shmflg: access requested
  1355. *
  1356. * Returns 0 if current has the requested access, error code otherwise
  1357. */
  1358. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  1359. int shmflg)
  1360. {
  1361. char *ssp = smack_of_shm(shp);
  1362. int may;
  1363. may = smack_flags_to_may(shmflg);
  1364. return smk_curacc(ssp, may);
  1365. }
  1366. /**
  1367. * smack_of_sem - the smack pointer for the sem
  1368. * @sma: the object
  1369. *
  1370. * Returns a pointer to the smack value
  1371. */
  1372. static char *smack_of_sem(struct sem_array *sma)
  1373. {
  1374. return (char *)sma->sem_perm.security;
  1375. }
  1376. /**
  1377. * smack_sem_alloc_security - Set the security blob for sem
  1378. * @sma: the object
  1379. *
  1380. * Returns 0
  1381. */
  1382. static int smack_sem_alloc_security(struct sem_array *sma)
  1383. {
  1384. struct kern_ipc_perm *isp = &sma->sem_perm;
  1385. isp->security = current->security;
  1386. return 0;
  1387. }
  1388. /**
  1389. * smack_sem_free_security - Clear the security blob for sem
  1390. * @sma: the object
  1391. *
  1392. * Clears the blob pointer
  1393. */
  1394. static void smack_sem_free_security(struct sem_array *sma)
  1395. {
  1396. struct kern_ipc_perm *isp = &sma->sem_perm;
  1397. isp->security = NULL;
  1398. }
  1399. /**
  1400. * smack_sem_associate - Smack access check for sem
  1401. * @sma: the object
  1402. * @semflg: access requested
  1403. *
  1404. * Returns 0 if current has the requested access, error code otherwise
  1405. */
  1406. static int smack_sem_associate(struct sem_array *sma, int semflg)
  1407. {
  1408. char *ssp = smack_of_sem(sma);
  1409. int may;
  1410. may = smack_flags_to_may(semflg);
  1411. return smk_curacc(ssp, may);
  1412. }
  1413. /**
  1414. * smack_sem_shmctl - Smack access check for sem
  1415. * @sma: the object
  1416. * @cmd: what it wants to do
  1417. *
  1418. * Returns 0 if current has the requested access, error code otherwise
  1419. */
  1420. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  1421. {
  1422. char *ssp;
  1423. int may;
  1424. switch (cmd) {
  1425. case GETPID:
  1426. case GETNCNT:
  1427. case GETZCNT:
  1428. case GETVAL:
  1429. case GETALL:
  1430. case IPC_STAT:
  1431. case SEM_STAT:
  1432. may = MAY_READ;
  1433. break;
  1434. case SETVAL:
  1435. case SETALL:
  1436. case IPC_RMID:
  1437. case IPC_SET:
  1438. may = MAY_READWRITE;
  1439. break;
  1440. case IPC_INFO:
  1441. case SEM_INFO:
  1442. /*
  1443. * System level information
  1444. */
  1445. return 0;
  1446. default:
  1447. return -EINVAL;
  1448. }
  1449. ssp = smack_of_sem(sma);
  1450. return smk_curacc(ssp, may);
  1451. }
  1452. /**
  1453. * smack_sem_semop - Smack checks of semaphore operations
  1454. * @sma: the object
  1455. * @sops: unused
  1456. * @nsops: unused
  1457. * @alter: unused
  1458. *
  1459. * Treated as read and write in all cases.
  1460. *
  1461. * Returns 0 if access is allowed, error code otherwise
  1462. */
  1463. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  1464. unsigned nsops, int alter)
  1465. {
  1466. char *ssp = smack_of_sem(sma);
  1467. return smk_curacc(ssp, MAY_READWRITE);
  1468. }
  1469. /**
  1470. * smack_msg_alloc_security - Set the security blob for msg
  1471. * @msq: the object
  1472. *
  1473. * Returns 0
  1474. */
  1475. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  1476. {
  1477. struct kern_ipc_perm *kisp = &msq->q_perm;
  1478. kisp->security = current->security;
  1479. return 0;
  1480. }
  1481. /**
  1482. * smack_msg_free_security - Clear the security blob for msg
  1483. * @msq: the object
  1484. *
  1485. * Clears the blob pointer
  1486. */
  1487. static void smack_msg_queue_free_security(struct msg_queue *msq)
  1488. {
  1489. struct kern_ipc_perm *kisp = &msq->q_perm;
  1490. kisp->security = NULL;
  1491. }
  1492. /**
  1493. * smack_of_msq - the smack pointer for the msq
  1494. * @msq: the object
  1495. *
  1496. * Returns a pointer to the smack value
  1497. */
  1498. static char *smack_of_msq(struct msg_queue *msq)
  1499. {
  1500. return (char *)msq->q_perm.security;
  1501. }
  1502. /**
  1503. * smack_msg_queue_associate - Smack access check for msg_queue
  1504. * @msq: the object
  1505. * @msqflg: access requested
  1506. *
  1507. * Returns 0 if current has the requested access, error code otherwise
  1508. */
  1509. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  1510. {
  1511. char *msp = smack_of_msq(msq);
  1512. int may;
  1513. may = smack_flags_to_may(msqflg);
  1514. return smk_curacc(msp, may);
  1515. }
  1516. /**
  1517. * smack_msg_queue_msgctl - Smack access check for msg_queue
  1518. * @msq: the object
  1519. * @cmd: what it wants to do
  1520. *
  1521. * Returns 0 if current has the requested access, error code otherwise
  1522. */
  1523. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  1524. {
  1525. char *msp;
  1526. int may;
  1527. switch (cmd) {
  1528. case IPC_STAT:
  1529. case MSG_STAT:
  1530. may = MAY_READ;
  1531. break;
  1532. case IPC_SET:
  1533. case IPC_RMID:
  1534. may = MAY_READWRITE;
  1535. break;
  1536. case IPC_INFO:
  1537. case MSG_INFO:
  1538. /*
  1539. * System level information
  1540. */
  1541. return 0;
  1542. default:
  1543. return -EINVAL;
  1544. }
  1545. msp = smack_of_msq(msq);
  1546. return smk_curacc(msp, may);
  1547. }
  1548. /**
  1549. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1550. * @msq: the object
  1551. * @msg: unused
  1552. * @msqflg: access requested
  1553. *
  1554. * Returns 0 if current has the requested access, error code otherwise
  1555. */
  1556. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  1557. int msqflg)
  1558. {
  1559. char *msp = smack_of_msq(msq);
  1560. int rc;
  1561. rc = smack_flags_to_may(msqflg);
  1562. return smk_curacc(msp, rc);
  1563. }
  1564. /**
  1565. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  1566. * @msq: the object
  1567. * @msg: unused
  1568. * @target: unused
  1569. * @type: unused
  1570. * @mode: unused
  1571. *
  1572. * Returns 0 if current has read and write access, error code otherwise
  1573. */
  1574. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  1575. struct task_struct *target, long type, int mode)
  1576. {
  1577. char *msp = smack_of_msq(msq);
  1578. return smk_curacc(msp, MAY_READWRITE);
  1579. }
  1580. /**
  1581. * smack_ipc_permission - Smack access for ipc_permission()
  1582. * @ipp: the object permissions
  1583. * @flag: access requested
  1584. *
  1585. * Returns 0 if current has read and write access, error code otherwise
  1586. */
  1587. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  1588. {
  1589. char *isp = ipp->security;
  1590. int may;
  1591. may = smack_flags_to_may(flag);
  1592. return smk_curacc(isp, may);
  1593. }
  1594. /**
  1595. * smack_ipc_getsecid - Extract smack security id
  1596. * @ipcp: the object permissions
  1597. * @secid: where result will be saved
  1598. */
  1599. static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
  1600. {
  1601. char *smack = ipp->security;
  1602. *secid = smack_to_secid(smack);
  1603. }
  1604. /* module stacking operations */
  1605. /**
  1606. * smack_register_security - stack capability module
  1607. * @name: module name
  1608. * @ops: module operations - ignored
  1609. *
  1610. * Allow the capability module to register.
  1611. */
  1612. static int smack_register_security(const char *name,
  1613. struct security_operations *ops)
  1614. {
  1615. if (strcmp(name, "capability") != 0)
  1616. return -EINVAL;
  1617. printk(KERN_INFO "%s: Registering secondary module %s\n",
  1618. __func__, name);
  1619. return 0;
  1620. }
  1621. /**
  1622. * smack_d_instantiate - Make sure the blob is correct on an inode
  1623. * @opt_dentry: unused
  1624. * @inode: the object
  1625. *
  1626. * Set the inode's security blob if it hasn't been done already.
  1627. */
  1628. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  1629. {
  1630. struct super_block *sbp;
  1631. struct superblock_smack *sbsp;
  1632. struct inode_smack *isp;
  1633. char *csp = current->security;
  1634. char *fetched;
  1635. char *final;
  1636. struct dentry *dp;
  1637. if (inode == NULL)
  1638. return;
  1639. isp = inode->i_security;
  1640. mutex_lock(&isp->smk_lock);
  1641. /*
  1642. * If the inode is already instantiated
  1643. * take the quick way out
  1644. */
  1645. if (isp->smk_flags & SMK_INODE_INSTANT)
  1646. goto unlockandout;
  1647. sbp = inode->i_sb;
  1648. sbsp = sbp->s_security;
  1649. /*
  1650. * We're going to use the superblock default label
  1651. * if there's no label on the file.
  1652. */
  1653. final = sbsp->smk_default;
  1654. /*
  1655. * This is pretty hackish.
  1656. * Casey says that we shouldn't have to do
  1657. * file system specific code, but it does help
  1658. * with keeping it simple.
  1659. */
  1660. switch (sbp->s_magic) {
  1661. case SMACK_MAGIC:
  1662. /*
  1663. * Casey says that it's a little embarassing
  1664. * that the smack file system doesn't do
  1665. * extended attributes.
  1666. */
  1667. final = smack_known_star.smk_known;
  1668. break;
  1669. case PIPEFS_MAGIC:
  1670. /*
  1671. * Casey says pipes are easy (?)
  1672. */
  1673. final = smack_known_star.smk_known;
  1674. break;
  1675. case DEVPTS_SUPER_MAGIC:
  1676. /*
  1677. * devpts seems content with the label of the task.
  1678. * Programs that change smack have to treat the
  1679. * pty with respect.
  1680. */
  1681. final = csp;
  1682. break;
  1683. case SOCKFS_MAGIC:
  1684. /*
  1685. * Casey says sockets get the smack of the task.
  1686. */
  1687. final = csp;
  1688. break;
  1689. case PROC_SUPER_MAGIC:
  1690. /*
  1691. * Casey says procfs appears not to care.
  1692. * The superblock default suffices.
  1693. */
  1694. break;
  1695. case TMPFS_MAGIC:
  1696. /*
  1697. * Device labels should come from the filesystem,
  1698. * but watch out, because they're volitile,
  1699. * getting recreated on every reboot.
  1700. */
  1701. final = smack_known_star.smk_known;
  1702. /*
  1703. * No break.
  1704. *
  1705. * If a smack value has been set we want to use it,
  1706. * but since tmpfs isn't giving us the opportunity
  1707. * to set mount options simulate setting the
  1708. * superblock default.
  1709. */
  1710. default:
  1711. /*
  1712. * This isn't an understood special case.
  1713. * Get the value from the xattr.
  1714. *
  1715. * No xattr support means, alas, no SMACK label.
  1716. * Use the aforeapplied default.
  1717. * It would be curious if the label of the task
  1718. * does not match that assigned.
  1719. */
  1720. if (inode->i_op->getxattr == NULL)
  1721. break;
  1722. /*
  1723. * Get the dentry for xattr.
  1724. */
  1725. if (opt_dentry == NULL) {
  1726. dp = d_find_alias(inode);
  1727. if (dp == NULL)
  1728. break;
  1729. } else {
  1730. dp = dget(opt_dentry);
  1731. if (dp == NULL)
  1732. break;
  1733. }
  1734. fetched = smk_fetch(inode, dp);
  1735. if (fetched != NULL)
  1736. final = fetched;
  1737. dput(dp);
  1738. break;
  1739. }
  1740. if (final == NULL)
  1741. isp->smk_inode = csp;
  1742. else
  1743. isp->smk_inode = final;
  1744. isp->smk_flags |= SMK_INODE_INSTANT;
  1745. unlockandout:
  1746. mutex_unlock(&isp->smk_lock);
  1747. return;
  1748. }
  1749. /**
  1750. * smack_getprocattr - Smack process attribute access
  1751. * @p: the object task
  1752. * @name: the name of the attribute in /proc/.../attr
  1753. * @value: where to put the result
  1754. *
  1755. * Places a copy of the task Smack into value
  1756. *
  1757. * Returns the length of the smack label or an error code
  1758. */
  1759. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  1760. {
  1761. char *cp;
  1762. int slen;
  1763. if (strcmp(name, "current") != 0)
  1764. return -EINVAL;
  1765. cp = kstrdup(p->security, GFP_KERNEL);
  1766. if (cp == NULL)
  1767. return -ENOMEM;
  1768. slen = strlen(cp);
  1769. *value = cp;
  1770. return slen;
  1771. }
  1772. /**
  1773. * smack_setprocattr - Smack process attribute setting
  1774. * @p: the object task
  1775. * @name: the name of the attribute in /proc/.../attr
  1776. * @value: the value to set
  1777. * @size: the size of the value
  1778. *
  1779. * Sets the Smack value of the task. Only setting self
  1780. * is permitted and only with privilege
  1781. *
  1782. * Returns the length of the smack label or an error code
  1783. */
  1784. static int smack_setprocattr(struct task_struct *p, char *name,
  1785. void *value, size_t size)
  1786. {
  1787. char *newsmack;
  1788. if (!__capable(p, CAP_MAC_ADMIN))
  1789. return -EPERM;
  1790. /*
  1791. * Changing another process' Smack value is too dangerous
  1792. * and supports no sane use case.
  1793. */
  1794. if (p != current)
  1795. return -EPERM;
  1796. if (value == NULL || size == 0 || size >= SMK_LABELLEN)
  1797. return -EINVAL;
  1798. if (strcmp(name, "current") != 0)
  1799. return -EINVAL;
  1800. newsmack = smk_import(value, size);
  1801. if (newsmack == NULL)
  1802. return -EINVAL;
  1803. p->security = newsmack;
  1804. return size;
  1805. }
  1806. /**
  1807. * smack_unix_stream_connect - Smack access on UDS
  1808. * @sock: one socket
  1809. * @other: the other socket
  1810. * @newsk: unused
  1811. *
  1812. * Return 0 if a subject with the smack of sock could access
  1813. * an object with the smack of other, otherwise an error code
  1814. */
  1815. static int smack_unix_stream_connect(struct socket *sock,
  1816. struct socket *other, struct sock *newsk)
  1817. {
  1818. struct inode *sp = SOCK_INODE(sock);
  1819. struct inode *op = SOCK_INODE(other);
  1820. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
  1821. }
  1822. /**
  1823. * smack_unix_may_send - Smack access on UDS
  1824. * @sock: one socket
  1825. * @other: the other socket
  1826. *
  1827. * Return 0 if a subject with the smack of sock could access
  1828. * an object with the smack of other, otherwise an error code
  1829. */
  1830. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  1831. {
  1832. struct inode *sp = SOCK_INODE(sock);
  1833. struct inode *op = SOCK_INODE(other);
  1834. return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
  1835. }
  1836. /**
  1837. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
  1838. * pair to smack
  1839. * @sap: netlabel secattr
  1840. * @sip: where to put the result
  1841. *
  1842. * Copies a smack label into sip
  1843. */
  1844. static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
  1845. {
  1846. char smack[SMK_LABELLEN];
  1847. int pcat;
  1848. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
  1849. /*
  1850. * If there are flags but no level netlabel isn't
  1851. * behaving the way we expect it to.
  1852. *
  1853. * Without guidance regarding the smack value
  1854. * for the packet fall back on the network
  1855. * ambient value.
  1856. */
  1857. strncpy(sip, smack_net_ambient, SMK_MAXLEN);
  1858. return;
  1859. }
  1860. /*
  1861. * Get the categories, if any
  1862. */
  1863. memset(smack, '\0', SMK_LABELLEN);
  1864. if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
  1865. for (pcat = -1;;) {
  1866. pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
  1867. pcat + 1);
  1868. if (pcat < 0)
  1869. break;
  1870. smack_catset_bit(pcat, smack);
  1871. }
  1872. /*
  1873. * If it is CIPSO using smack direct mapping
  1874. * we are already done. WeeHee.
  1875. */
  1876. if (sap->attr.mls.lvl == smack_cipso_direct) {
  1877. memcpy(sip, smack, SMK_MAXLEN);
  1878. return;
  1879. }
  1880. /*
  1881. * Look it up in the supplied table if it is not a direct mapping.
  1882. */
  1883. smack_from_cipso(sap->attr.mls.lvl, smack, sip);
  1884. return;
  1885. }
  1886. /**
  1887. * smack_socket_sock_rcv_skb - Smack packet delivery access check
  1888. * @sk: socket
  1889. * @skb: packet
  1890. *
  1891. * Returns 0 if the packet should be delivered, an error code otherwise
  1892. */
  1893. static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1894. {
  1895. struct netlbl_lsm_secattr secattr;
  1896. struct socket_smack *ssp = sk->sk_security;
  1897. char smack[SMK_LABELLEN];
  1898. int rc;
  1899. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1900. return 0;
  1901. /*
  1902. * Translate what netlabel gave us.
  1903. */
  1904. memset(smack, '\0', SMK_LABELLEN);
  1905. netlbl_secattr_init(&secattr);
  1906. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
  1907. if (rc == 0)
  1908. smack_from_secattr(&secattr, smack);
  1909. else
  1910. strncpy(smack, smack_net_ambient, SMK_MAXLEN);
  1911. netlbl_secattr_destroy(&secattr);
  1912. /*
  1913. * Receiving a packet requires that the other end
  1914. * be able to write here. Read access is not required.
  1915. * This is the simplist possible security model
  1916. * for networking.
  1917. */
  1918. return smk_access(smack, ssp->smk_in, MAY_WRITE);
  1919. }
  1920. /**
  1921. * smack_socket_getpeersec_stream - pull in packet label
  1922. * @sock: the socket
  1923. * @optval: user's destination
  1924. * @optlen: size thereof
  1925. * @len: max thereoe
  1926. *
  1927. * returns zero on success, an error code otherwise
  1928. */
  1929. static int smack_socket_getpeersec_stream(struct socket *sock,
  1930. char __user *optval,
  1931. int __user *optlen, unsigned len)
  1932. {
  1933. struct socket_smack *ssp;
  1934. int slen;
  1935. int rc = 0;
  1936. ssp = sock->sk->sk_security;
  1937. slen = strlen(ssp->smk_packet) + 1;
  1938. if (slen > len)
  1939. rc = -ERANGE;
  1940. else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
  1941. rc = -EFAULT;
  1942. if (put_user(slen, optlen) != 0)
  1943. rc = -EFAULT;
  1944. return rc;
  1945. }
  1946. /**
  1947. * smack_socket_getpeersec_dgram - pull in packet label
  1948. * @sock: the socket
  1949. * @skb: packet data
  1950. * @secid: pointer to where to put the secid of the packet
  1951. *
  1952. * Sets the netlabel socket state on sk from parent
  1953. */
  1954. static int smack_socket_getpeersec_dgram(struct socket *sock,
  1955. struct sk_buff *skb, u32 *secid)
  1956. {
  1957. struct netlbl_lsm_secattr secattr;
  1958. struct sock *sk;
  1959. char smack[SMK_LABELLEN];
  1960. int family = PF_INET;
  1961. u32 s;
  1962. int rc;
  1963. /*
  1964. * Only works for families with packets.
  1965. */
  1966. if (sock != NULL) {
  1967. sk = sock->sk;
  1968. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1969. return 0;
  1970. family = sk->sk_family;
  1971. }
  1972. /*
  1973. * Translate what netlabel gave us.
  1974. */
  1975. memset(smack, '\0', SMK_LABELLEN);
  1976. netlbl_secattr_init(&secattr);
  1977. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  1978. if (rc == 0)
  1979. smack_from_secattr(&secattr, smack);
  1980. netlbl_secattr_destroy(&secattr);
  1981. /*
  1982. * Give up if we couldn't get anything
  1983. */
  1984. if (rc != 0)
  1985. return rc;
  1986. s = smack_to_secid(smack);
  1987. if (s == 0)
  1988. return -EINVAL;
  1989. *secid = s;
  1990. return 0;
  1991. }
  1992. /**
  1993. * smack_sock_graft - graft access state between two sockets
  1994. * @sk: fresh sock
  1995. * @parent: donor socket
  1996. *
  1997. * Sets the netlabel socket state on sk from parent
  1998. */
  1999. static void smack_sock_graft(struct sock *sk, struct socket *parent)
  2000. {
  2001. struct socket_smack *ssp;
  2002. int rc;
  2003. if (sk == NULL)
  2004. return;
  2005. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  2006. return;
  2007. ssp = sk->sk_security;
  2008. ssp->smk_in = current->security;
  2009. ssp->smk_out = current->security;
  2010. ssp->smk_packet[0] = '\0';
  2011. rc = smack_netlabel(sk);
  2012. if (rc != 0)
  2013. printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
  2014. __func__, -rc);
  2015. }
  2016. /**
  2017. * smack_inet_conn_request - Smack access check on connect
  2018. * @sk: socket involved
  2019. * @skb: packet
  2020. * @req: unused
  2021. *
  2022. * Returns 0 if a task with the packet label could write to
  2023. * the socket, otherwise an error code
  2024. */
  2025. static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  2026. struct request_sock *req)
  2027. {
  2028. struct netlbl_lsm_secattr skb_secattr;
  2029. struct socket_smack *ssp = sk->sk_security;
  2030. char smack[SMK_LABELLEN];
  2031. int rc;
  2032. if (skb == NULL)
  2033. return -EACCES;
  2034. memset(smack, '\0', SMK_LABELLEN);
  2035. netlbl_secattr_init(&skb_secattr);
  2036. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
  2037. if (rc == 0)
  2038. smack_from_secattr(&skb_secattr, smack);
  2039. else
  2040. strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
  2041. netlbl_secattr_destroy(&skb_secattr);
  2042. /*
  2043. * Receiving a packet requires that the other end
  2044. * be able to write here. Read access is not required.
  2045. *
  2046. * If the request is successful save the peer's label
  2047. * so that SO_PEERCRED can report it.
  2048. */
  2049. rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
  2050. if (rc == 0)
  2051. strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
  2052. return rc;
  2053. }
  2054. /*
  2055. * Key management security hooks
  2056. *
  2057. * Casey has not tested key support very heavily.
  2058. * The permission check is most likely too restrictive.
  2059. * If you care about keys please have a look.
  2060. */
  2061. #ifdef CONFIG_KEYS
  2062. /**
  2063. * smack_key_alloc - Set the key security blob
  2064. * @key: object
  2065. * @tsk: the task associated with the key
  2066. * @flags: unused
  2067. *
  2068. * No allocation required
  2069. *
  2070. * Returns 0
  2071. */
  2072. static int smack_key_alloc(struct key *key, struct task_struct *tsk,
  2073. unsigned long flags)
  2074. {
  2075. key->security = tsk->security;
  2076. return 0;
  2077. }
  2078. /**
  2079. * smack_key_free - Clear the key security blob
  2080. * @key: the object
  2081. *
  2082. * Clear the blob pointer
  2083. */
  2084. static void smack_key_free(struct key *key)
  2085. {
  2086. key->security = NULL;
  2087. }
  2088. /*
  2089. * smack_key_permission - Smack access on a key
  2090. * @key_ref: gets to the object
  2091. * @context: task involved
  2092. * @perm: unused
  2093. *
  2094. * Return 0 if the task has read and write to the object,
  2095. * an error code otherwise
  2096. */
  2097. static int smack_key_permission(key_ref_t key_ref,
  2098. struct task_struct *context, key_perm_t perm)
  2099. {
  2100. struct key *keyp;
  2101. keyp = key_ref_to_ptr(key_ref);
  2102. if (keyp == NULL)
  2103. return -EINVAL;
  2104. /*
  2105. * If the key hasn't been initialized give it access so that
  2106. * it may do so.
  2107. */
  2108. if (keyp->security == NULL)
  2109. return 0;
  2110. /*
  2111. * This should not occur
  2112. */
  2113. if (context->security == NULL)
  2114. return -EACCES;
  2115. return smk_access(context->security, keyp->security, MAY_READWRITE);
  2116. }
  2117. #endif /* CONFIG_KEYS */
  2118. /*
  2119. * Smack Audit hooks
  2120. *
  2121. * Audit requires a unique representation of each Smack specific
  2122. * rule. This unique representation is used to distinguish the
  2123. * object to be audited from remaining kernel objects and also
  2124. * works as a glue between the audit hooks.
  2125. *
  2126. * Since repository entries are added but never deleted, we'll use
  2127. * the smack_known label address related to the given audit rule as
  2128. * the needed unique representation. This also better fits the smack
  2129. * model where nearly everything is a label.
  2130. */
  2131. #ifdef CONFIG_AUDIT
  2132. /**
  2133. * smack_audit_rule_init - Initialize a smack audit rule
  2134. * @field: audit rule fields given from user-space (audit.h)
  2135. * @op: required testing operator (=, !=, >, <, ...)
  2136. * @rulestr: smack label to be audited
  2137. * @vrule: pointer to save our own audit rule representation
  2138. *
  2139. * Prepare to audit cases where (@field @op @rulestr) is true.
  2140. * The label to be audited is created if necessay.
  2141. */
  2142. static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2143. {
  2144. char **rule = (char **)vrule;
  2145. *rule = NULL;
  2146. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2147. return -EINVAL;
  2148. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  2149. return -EINVAL;
  2150. *rule = smk_import(rulestr, 0);
  2151. return 0;
  2152. }
  2153. /**
  2154. * smack_audit_rule_known - Distinguish Smack audit rules
  2155. * @krule: rule of interest, in Audit kernel representation format
  2156. *
  2157. * This is used to filter Smack rules from remaining Audit ones.
  2158. * If it's proved that this rule belongs to us, the
  2159. * audit_rule_match hook will be called to do the final judgement.
  2160. */
  2161. static int smack_audit_rule_known(struct audit_krule *krule)
  2162. {
  2163. struct audit_field *f;
  2164. int i;
  2165. for (i = 0; i < krule->field_count; i++) {
  2166. f = &krule->fields[i];
  2167. if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
  2168. return 1;
  2169. }
  2170. return 0;
  2171. }
  2172. /**
  2173. * smack_audit_rule_match - Audit given object ?
  2174. * @secid: security id for identifying the object to test
  2175. * @field: audit rule flags given from user-space
  2176. * @op: required testing operator
  2177. * @vrule: smack internal rule presentation
  2178. * @actx: audit context associated with the check
  2179. *
  2180. * The core Audit hook. It's used to take the decision of
  2181. * whether to audit or not to audit a given object.
  2182. */
  2183. static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
  2184. struct audit_context *actx)
  2185. {
  2186. char *smack;
  2187. char *rule = vrule;
  2188. if (!rule) {
  2189. audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
  2190. "Smack: missing rule\n");
  2191. return -ENOENT;
  2192. }
  2193. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2194. return 0;
  2195. smack = smack_from_secid(secid);
  2196. /*
  2197. * No need to do string comparisons. If a match occurs,
  2198. * both pointers will point to the same smack_known
  2199. * label.
  2200. */
  2201. if (op == AUDIT_EQUAL)
  2202. return (rule == smack);
  2203. if (op == AUDIT_NOT_EQUAL)
  2204. return (rule != smack);
  2205. return 0;
  2206. }
  2207. /**
  2208. * smack_audit_rule_free - free smack rule representation
  2209. * @vrule: rule to be freed.
  2210. *
  2211. * No memory was allocated.
  2212. */
  2213. static void smack_audit_rule_free(void *vrule)
  2214. {
  2215. /* No-op */
  2216. }
  2217. #endif /* CONFIG_AUDIT */
  2218. /*
  2219. * smack_secid_to_secctx - return the smack label for a secid
  2220. * @secid: incoming integer
  2221. * @secdata: destination
  2222. * @seclen: how long it is
  2223. *
  2224. * Exists for networking code.
  2225. */
  2226. static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  2227. {
  2228. char *sp = smack_from_secid(secid);
  2229. *secdata = sp;
  2230. *seclen = strlen(sp);
  2231. return 0;
  2232. }
  2233. /*
  2234. * smack_secctx_to_secid - return the secid for a smack label
  2235. * @secdata: smack label
  2236. * @seclen: how long result is
  2237. * @secid: outgoing integer
  2238. *
  2239. * Exists for audit and networking code.
  2240. */
  2241. static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  2242. {
  2243. *secid = smack_to_secid(secdata);
  2244. return 0;
  2245. }
  2246. /*
  2247. * smack_release_secctx - don't do anything.
  2248. * @key_ref: unused
  2249. * @context: unused
  2250. * @perm: unused
  2251. *
  2252. * Exists to make sure nothing gets done, and properly
  2253. */
  2254. static void smack_release_secctx(char *secdata, u32 seclen)
  2255. {
  2256. }
  2257. struct security_operations smack_ops = {
  2258. .name = "smack",
  2259. .ptrace = smack_ptrace,
  2260. .capget = cap_capget,
  2261. .capset_check = cap_capset_check,
  2262. .capset_set = cap_capset_set,
  2263. .capable = cap_capable,
  2264. .syslog = smack_syslog,
  2265. .settime = cap_settime,
  2266. .vm_enough_memory = cap_vm_enough_memory,
  2267. .bprm_apply_creds = cap_bprm_apply_creds,
  2268. .bprm_set_security = cap_bprm_set_security,
  2269. .bprm_secureexec = cap_bprm_secureexec,
  2270. .sb_alloc_security = smack_sb_alloc_security,
  2271. .sb_free_security = smack_sb_free_security,
  2272. .sb_copy_data = smack_sb_copy_data,
  2273. .sb_kern_mount = smack_sb_kern_mount,
  2274. .sb_statfs = smack_sb_statfs,
  2275. .sb_mount = smack_sb_mount,
  2276. .sb_umount = smack_sb_umount,
  2277. .inode_alloc_security = smack_inode_alloc_security,
  2278. .inode_free_security = smack_inode_free_security,
  2279. .inode_init_security = smack_inode_init_security,
  2280. .inode_link = smack_inode_link,
  2281. .inode_unlink = smack_inode_unlink,
  2282. .inode_rmdir = smack_inode_rmdir,
  2283. .inode_rename = smack_inode_rename,
  2284. .inode_permission = smack_inode_permission,
  2285. .inode_setattr = smack_inode_setattr,
  2286. .inode_getattr = smack_inode_getattr,
  2287. .inode_setxattr = smack_inode_setxattr,
  2288. .inode_post_setxattr = smack_inode_post_setxattr,
  2289. .inode_getxattr = smack_inode_getxattr,
  2290. .inode_removexattr = smack_inode_removexattr,
  2291. .inode_need_killpriv = cap_inode_need_killpriv,
  2292. .inode_killpriv = cap_inode_killpriv,
  2293. .inode_getsecurity = smack_inode_getsecurity,
  2294. .inode_setsecurity = smack_inode_setsecurity,
  2295. .inode_listsecurity = smack_inode_listsecurity,
  2296. .inode_getsecid = smack_inode_getsecid,
  2297. .file_permission = smack_file_permission,
  2298. .file_alloc_security = smack_file_alloc_security,
  2299. .file_free_security = smack_file_free_security,
  2300. .file_ioctl = smack_file_ioctl,
  2301. .file_lock = smack_file_lock,
  2302. .file_fcntl = smack_file_fcntl,
  2303. .file_set_fowner = smack_file_set_fowner,
  2304. .file_send_sigiotask = smack_file_send_sigiotask,
  2305. .file_receive = smack_file_receive,
  2306. .task_alloc_security = smack_task_alloc_security,
  2307. .task_free_security = smack_task_free_security,
  2308. .task_post_setuid = cap_task_post_setuid,
  2309. .task_setpgid = smack_task_setpgid,
  2310. .task_getpgid = smack_task_getpgid,
  2311. .task_getsid = smack_task_getsid,
  2312. .task_getsecid = smack_task_getsecid,
  2313. .task_setnice = smack_task_setnice,
  2314. .task_setioprio = smack_task_setioprio,
  2315. .task_getioprio = smack_task_getioprio,
  2316. .task_setscheduler = smack_task_setscheduler,
  2317. .task_getscheduler = smack_task_getscheduler,
  2318. .task_movememory = smack_task_movememory,
  2319. .task_kill = smack_task_kill,
  2320. .task_wait = smack_task_wait,
  2321. .task_reparent_to_init = cap_task_reparent_to_init,
  2322. .task_to_inode = smack_task_to_inode,
  2323. .task_prctl = cap_task_prctl,
  2324. .ipc_permission = smack_ipc_permission,
  2325. .ipc_getsecid = smack_ipc_getsecid,
  2326. .msg_msg_alloc_security = smack_msg_msg_alloc_security,
  2327. .msg_msg_free_security = smack_msg_msg_free_security,
  2328. .msg_queue_alloc_security = smack_msg_queue_alloc_security,
  2329. .msg_queue_free_security = smack_msg_queue_free_security,
  2330. .msg_queue_associate = smack_msg_queue_associate,
  2331. .msg_queue_msgctl = smack_msg_queue_msgctl,
  2332. .msg_queue_msgsnd = smack_msg_queue_msgsnd,
  2333. .msg_queue_msgrcv = smack_msg_queue_msgrcv,
  2334. .shm_alloc_security = smack_shm_alloc_security,
  2335. .shm_free_security = smack_shm_free_security,
  2336. .shm_associate = smack_shm_associate,
  2337. .shm_shmctl = smack_shm_shmctl,
  2338. .shm_shmat = smack_shm_shmat,
  2339. .sem_alloc_security = smack_sem_alloc_security,
  2340. .sem_free_security = smack_sem_free_security,
  2341. .sem_associate = smack_sem_associate,
  2342. .sem_semctl = smack_sem_semctl,
  2343. .sem_semop = smack_sem_semop,
  2344. .netlink_send = cap_netlink_send,
  2345. .netlink_recv = cap_netlink_recv,
  2346. .register_security = smack_register_security,
  2347. .d_instantiate = smack_d_instantiate,
  2348. .getprocattr = smack_getprocattr,
  2349. .setprocattr = smack_setprocattr,
  2350. .unix_stream_connect = smack_unix_stream_connect,
  2351. .unix_may_send = smack_unix_may_send,
  2352. .socket_post_create = smack_socket_post_create,
  2353. .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
  2354. .socket_getpeersec_stream = smack_socket_getpeersec_stream,
  2355. .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
  2356. .sk_alloc_security = smack_sk_alloc_security,
  2357. .sk_free_security = smack_sk_free_security,
  2358. .sock_graft = smack_sock_graft,
  2359. .inet_conn_request = smack_inet_conn_request,
  2360. /* key management security hooks */
  2361. #ifdef CONFIG_KEYS
  2362. .key_alloc = smack_key_alloc,
  2363. .key_free = smack_key_free,
  2364. .key_permission = smack_key_permission,
  2365. #endif /* CONFIG_KEYS */
  2366. /* Audit hooks */
  2367. #ifdef CONFIG_AUDIT
  2368. .audit_rule_init = smack_audit_rule_init,
  2369. .audit_rule_known = smack_audit_rule_known,
  2370. .audit_rule_match = smack_audit_rule_match,
  2371. .audit_rule_free = smack_audit_rule_free,
  2372. #endif /* CONFIG_AUDIT */
  2373. .secid_to_secctx = smack_secid_to_secctx,
  2374. .secctx_to_secid = smack_secctx_to_secid,
  2375. .release_secctx = smack_release_secctx,
  2376. };
  2377. /**
  2378. * smack_init - initialize the smack system
  2379. *
  2380. * Returns 0
  2381. */
  2382. static __init int smack_init(void)
  2383. {
  2384. if (!security_module_enable(&smack_ops))
  2385. return 0;
  2386. printk(KERN_INFO "Smack: Initializing.\n");
  2387. /*
  2388. * Set the security state for the initial task.
  2389. */
  2390. current->security = &smack_known_floor.smk_known;
  2391. /*
  2392. * Initialize locks
  2393. */
  2394. spin_lock_init(&smack_known_unset.smk_cipsolock);
  2395. spin_lock_init(&smack_known_huh.smk_cipsolock);
  2396. spin_lock_init(&smack_known_hat.smk_cipsolock);
  2397. spin_lock_init(&smack_known_star.smk_cipsolock);
  2398. spin_lock_init(&smack_known_floor.smk_cipsolock);
  2399. spin_lock_init(&smack_known_invalid.smk_cipsolock);
  2400. /*
  2401. * Register with LSM
  2402. */
  2403. if (register_security(&smack_ops))
  2404. panic("smack: Unable to register with kernel.\n");
  2405. return 0;
  2406. }
  2407. /*
  2408. * Smack requires early initialization in order to label
  2409. * all processes and objects when they are created.
  2410. */
  2411. security_initcall(smack_init);