smack_lsm.c 85 KB

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