smack_lsm.c 128 KB

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