smack_lsm.c 104 KB

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