smack_lsm.c 104 KB

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