smack_lsm.c 92 KB

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