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