smack_lsm.c 91 KB

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