smack_lsm.c 62 KB

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