smack_lsm.c 86 KB

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