smack_lsm.c 87 KB

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