smack_lsm.c 63 KB

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