smack_lsm.c 88 KB

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