smack_lsm.c 95 KB

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