smack_lsm.c 83 KB

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