smack_lsm.c 108 KB

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