smack_lsm.c 85 KB

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