smack_lsm.c 115 KB

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