smack_lsm.c 118 KB

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