smack_lsm.c 86 KB

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