smack_lsm.c 85 KB

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