smack_lsm.c 92 KB

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