smack_lsm.c 113 KB

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