smack_lsm.c 84 KB

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