smack_lsm.c 84 KB

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