smack_lsm.c 76 KB

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