smack_lsm.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609
  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 = file_inode(bprm->file);
  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. int may;
  1033. int mmay;
  1034. int tmay;
  1035. int rc;
  1036. if (file == NULL)
  1037. return 0;
  1038. isp = file_inode(file)->i_security;
  1039. if (isp->smk_mmap == NULL)
  1040. return 0;
  1041. msmack = isp->smk_mmap;
  1042. tsp = current_security();
  1043. sp = smk_of_current();
  1044. skp = smk_find_entry(sp);
  1045. rc = 0;
  1046. rcu_read_lock();
  1047. /*
  1048. * For each Smack rule associated with the subject
  1049. * label verify that the SMACK64MMAP also has access
  1050. * to that rule's object label.
  1051. */
  1052. list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
  1053. osmack = srp->smk_object;
  1054. /*
  1055. * Matching labels always allows access.
  1056. */
  1057. if (msmack == osmack)
  1058. continue;
  1059. /*
  1060. * If there is a matching local rule take
  1061. * that into account as well.
  1062. */
  1063. may = smk_access_entry(srp->smk_subject, osmack,
  1064. &tsp->smk_rules);
  1065. if (may == -ENOENT)
  1066. may = srp->smk_access;
  1067. else
  1068. may &= srp->smk_access;
  1069. /*
  1070. * If may is zero the SMACK64MMAP subject can't
  1071. * possibly have less access.
  1072. */
  1073. if (may == 0)
  1074. continue;
  1075. /*
  1076. * Fetch the global list entry.
  1077. * If there isn't one a SMACK64MMAP subject
  1078. * can't have as much access as current.
  1079. */
  1080. skp = smk_find_entry(msmack);
  1081. mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
  1082. if (mmay == -ENOENT) {
  1083. rc = -EACCES;
  1084. break;
  1085. }
  1086. /*
  1087. * If there is a local entry it modifies the
  1088. * potential access, too.
  1089. */
  1090. tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
  1091. if (tmay != -ENOENT)
  1092. mmay &= tmay;
  1093. /*
  1094. * If there is any access available to current that is
  1095. * not available to a SMACK64MMAP subject
  1096. * deny access.
  1097. */
  1098. if ((may | mmay) != mmay) {
  1099. rc = -EACCES;
  1100. break;
  1101. }
  1102. }
  1103. rcu_read_unlock();
  1104. return rc;
  1105. }
  1106. /**
  1107. * smack_file_set_fowner - set the file security blob value
  1108. * @file: object in question
  1109. *
  1110. * Returns 0
  1111. * Further research may be required on this one.
  1112. */
  1113. static int smack_file_set_fowner(struct file *file)
  1114. {
  1115. file->f_security = smk_of_current();
  1116. return 0;
  1117. }
  1118. /**
  1119. * smack_file_send_sigiotask - Smack on sigio
  1120. * @tsk: The target task
  1121. * @fown: the object the signal come from
  1122. * @signum: unused
  1123. *
  1124. * Allow a privileged task to get signals even if it shouldn't
  1125. *
  1126. * Returns 0 if a subject with the object's smack could
  1127. * write to the task, an error code otherwise.
  1128. */
  1129. static int smack_file_send_sigiotask(struct task_struct *tsk,
  1130. struct fown_struct *fown, int signum)
  1131. {
  1132. struct file *file;
  1133. int rc;
  1134. char *tsp = smk_of_task(tsk->cred->security);
  1135. struct smk_audit_info ad;
  1136. /*
  1137. * struct fown_struct is never outside the context of a struct file
  1138. */
  1139. file = container_of(fown, struct file, f_owner);
  1140. /* we don't log here as rc can be overriden */
  1141. rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
  1142. if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
  1143. rc = 0;
  1144. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1145. smk_ad_setfield_u_tsk(&ad, tsk);
  1146. smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
  1147. return rc;
  1148. }
  1149. /**
  1150. * smack_file_receive - Smack file receive check
  1151. * @file: the object
  1152. *
  1153. * Returns 0 if current has access, error code otherwise
  1154. */
  1155. static int smack_file_receive(struct file *file)
  1156. {
  1157. int may = 0;
  1158. struct smk_audit_info ad;
  1159. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1160. smk_ad_setfield_u_fs_path(&ad, file->f_path);
  1161. /*
  1162. * This code relies on bitmasks.
  1163. */
  1164. if (file->f_mode & FMODE_READ)
  1165. may = MAY_READ;
  1166. if (file->f_mode & FMODE_WRITE)
  1167. may |= MAY_WRITE;
  1168. return smk_curacc(file->f_security, may, &ad);
  1169. }
  1170. /**
  1171. * smack_file_open - Smack dentry open processing
  1172. * @file: the object
  1173. * @cred: unused
  1174. *
  1175. * Set the security blob in the file structure.
  1176. *
  1177. * Returns 0
  1178. */
  1179. static int smack_file_open(struct file *file, const struct cred *cred)
  1180. {
  1181. struct inode_smack *isp = file_inode(file)->i_security;
  1182. file->f_security = isp->smk_inode;
  1183. return 0;
  1184. }
  1185. /*
  1186. * Task hooks
  1187. */
  1188. /**
  1189. * smack_cred_alloc_blank - "allocate" blank task-level security credentials
  1190. * @new: the new credentials
  1191. * @gfp: the atomicity of any memory allocations
  1192. *
  1193. * Prepare a blank set of credentials for modification. This must allocate all
  1194. * the memory the LSM module might require such that cred_transfer() can
  1195. * complete without error.
  1196. */
  1197. static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  1198. {
  1199. struct task_smack *tsp;
  1200. tsp = new_task_smack(NULL, NULL, gfp);
  1201. if (tsp == NULL)
  1202. return -ENOMEM;
  1203. cred->security = tsp;
  1204. return 0;
  1205. }
  1206. /**
  1207. * smack_cred_free - "free" task-level security credentials
  1208. * @cred: the credentials in question
  1209. *
  1210. */
  1211. static void smack_cred_free(struct cred *cred)
  1212. {
  1213. struct task_smack *tsp = cred->security;
  1214. struct smack_rule *rp;
  1215. struct list_head *l;
  1216. struct list_head *n;
  1217. if (tsp == NULL)
  1218. return;
  1219. cred->security = NULL;
  1220. list_for_each_safe(l, n, &tsp->smk_rules) {
  1221. rp = list_entry(l, struct smack_rule, list);
  1222. list_del(&rp->list);
  1223. kfree(rp);
  1224. }
  1225. kfree(tsp);
  1226. }
  1227. /**
  1228. * smack_cred_prepare - prepare new set of credentials for modification
  1229. * @new: the new credentials
  1230. * @old: the original credentials
  1231. * @gfp: the atomicity of any memory allocations
  1232. *
  1233. * Prepare a new set of credentials for modification.
  1234. */
  1235. static int smack_cred_prepare(struct cred *new, const struct cred *old,
  1236. gfp_t gfp)
  1237. {
  1238. struct task_smack *old_tsp = old->security;
  1239. struct task_smack *new_tsp;
  1240. int rc;
  1241. new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
  1242. if (new_tsp == NULL)
  1243. return -ENOMEM;
  1244. rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
  1245. if (rc != 0)
  1246. return rc;
  1247. new->security = new_tsp;
  1248. return 0;
  1249. }
  1250. /**
  1251. * smack_cred_transfer - Transfer the old credentials to the new credentials
  1252. * @new: the new credentials
  1253. * @old: the original credentials
  1254. *
  1255. * Fill in a set of blank credentials from another set of credentials.
  1256. */
  1257. static void smack_cred_transfer(struct cred *new, const struct cred *old)
  1258. {
  1259. struct task_smack *old_tsp = old->security;
  1260. struct task_smack *new_tsp = new->security;
  1261. new_tsp->smk_task = old_tsp->smk_task;
  1262. new_tsp->smk_forked = old_tsp->smk_task;
  1263. mutex_init(&new_tsp->smk_rules_lock);
  1264. INIT_LIST_HEAD(&new_tsp->smk_rules);
  1265. /* cbs copy rule list */
  1266. }
  1267. /**
  1268. * smack_kernel_act_as - Set the subjective context in a set of credentials
  1269. * @new: points to the set of credentials to be modified.
  1270. * @secid: specifies the security ID to be set
  1271. *
  1272. * Set the security data for a kernel service.
  1273. */
  1274. static int smack_kernel_act_as(struct cred *new, u32 secid)
  1275. {
  1276. struct task_smack *new_tsp = new->security;
  1277. char *smack = smack_from_secid(secid);
  1278. if (smack == NULL)
  1279. return -EINVAL;
  1280. new_tsp->smk_task = smack;
  1281. return 0;
  1282. }
  1283. /**
  1284. * smack_kernel_create_files_as - Set the file creation label in a set of creds
  1285. * @new: points to the set of credentials to be modified
  1286. * @inode: points to the inode to use as a reference
  1287. *
  1288. * Set the file creation context in a set of credentials to the same
  1289. * as the objective context of the specified inode
  1290. */
  1291. static int smack_kernel_create_files_as(struct cred *new,
  1292. struct inode *inode)
  1293. {
  1294. struct inode_smack *isp = inode->i_security;
  1295. struct task_smack *tsp = new->security;
  1296. tsp->smk_forked = isp->smk_inode;
  1297. tsp->smk_task = isp->smk_inode;
  1298. return 0;
  1299. }
  1300. /**
  1301. * smk_curacc_on_task - helper to log task related access
  1302. * @p: the task object
  1303. * @access: the access requested
  1304. * @caller: name of the calling function for audit
  1305. *
  1306. * Return 0 if access is permitted
  1307. */
  1308. static int smk_curacc_on_task(struct task_struct *p, int access,
  1309. const char *caller)
  1310. {
  1311. struct smk_audit_info ad;
  1312. smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
  1313. smk_ad_setfield_u_tsk(&ad, p);
  1314. return smk_curacc(smk_of_task(task_security(p)), access, &ad);
  1315. }
  1316. /**
  1317. * smack_task_setpgid - Smack check on setting pgid
  1318. * @p: the task object
  1319. * @pgid: unused
  1320. *
  1321. * Return 0 if write access is permitted
  1322. */
  1323. static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
  1324. {
  1325. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1326. }
  1327. /**
  1328. * smack_task_getpgid - Smack access check for getpgid
  1329. * @p: the object task
  1330. *
  1331. * Returns 0 if current can read the object task, error code otherwise
  1332. */
  1333. static int smack_task_getpgid(struct task_struct *p)
  1334. {
  1335. return smk_curacc_on_task(p, MAY_READ, __func__);
  1336. }
  1337. /**
  1338. * smack_task_getsid - Smack access check for getsid
  1339. * @p: the object task
  1340. *
  1341. * Returns 0 if current can read the object task, error code otherwise
  1342. */
  1343. static int smack_task_getsid(struct task_struct *p)
  1344. {
  1345. return smk_curacc_on_task(p, MAY_READ, __func__);
  1346. }
  1347. /**
  1348. * smack_task_getsecid - get the secid of the task
  1349. * @p: the object task
  1350. * @secid: where to put the result
  1351. *
  1352. * Sets the secid to contain a u32 version of the smack label.
  1353. */
  1354. static void smack_task_getsecid(struct task_struct *p, u32 *secid)
  1355. {
  1356. *secid = smack_to_secid(smk_of_task(task_security(p)));
  1357. }
  1358. /**
  1359. * smack_task_setnice - Smack check on setting nice
  1360. * @p: the task object
  1361. * @nice: unused
  1362. *
  1363. * Return 0 if write access is permitted
  1364. */
  1365. static int smack_task_setnice(struct task_struct *p, int nice)
  1366. {
  1367. int rc;
  1368. rc = cap_task_setnice(p, nice);
  1369. if (rc == 0)
  1370. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1371. return rc;
  1372. }
  1373. /**
  1374. * smack_task_setioprio - Smack check on setting ioprio
  1375. * @p: the task object
  1376. * @ioprio: unused
  1377. *
  1378. * Return 0 if write access is permitted
  1379. */
  1380. static int smack_task_setioprio(struct task_struct *p, int ioprio)
  1381. {
  1382. int rc;
  1383. rc = cap_task_setioprio(p, ioprio);
  1384. if (rc == 0)
  1385. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1386. return rc;
  1387. }
  1388. /**
  1389. * smack_task_getioprio - Smack check on reading ioprio
  1390. * @p: the task object
  1391. *
  1392. * Return 0 if read access is permitted
  1393. */
  1394. static int smack_task_getioprio(struct task_struct *p)
  1395. {
  1396. return smk_curacc_on_task(p, MAY_READ, __func__);
  1397. }
  1398. /**
  1399. * smack_task_setscheduler - Smack check on setting scheduler
  1400. * @p: the task object
  1401. * @policy: unused
  1402. * @lp: unused
  1403. *
  1404. * Return 0 if read access is permitted
  1405. */
  1406. static int smack_task_setscheduler(struct task_struct *p)
  1407. {
  1408. int rc;
  1409. rc = cap_task_setscheduler(p);
  1410. if (rc == 0)
  1411. rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
  1412. return rc;
  1413. }
  1414. /**
  1415. * smack_task_getscheduler - Smack check on reading scheduler
  1416. * @p: the task object
  1417. *
  1418. * Return 0 if read access is permitted
  1419. */
  1420. static int smack_task_getscheduler(struct task_struct *p)
  1421. {
  1422. return smk_curacc_on_task(p, MAY_READ, __func__);
  1423. }
  1424. /**
  1425. * smack_task_movememory - Smack check on moving memory
  1426. * @p: the task object
  1427. *
  1428. * Return 0 if write access is permitted
  1429. */
  1430. static int smack_task_movememory(struct task_struct *p)
  1431. {
  1432. return smk_curacc_on_task(p, MAY_WRITE, __func__);
  1433. }
  1434. /**
  1435. * smack_task_kill - Smack check on signal delivery
  1436. * @p: the task object
  1437. * @info: unused
  1438. * @sig: unused
  1439. * @secid: identifies the smack to use in lieu of current's
  1440. *
  1441. * Return 0 if write access is permitted
  1442. *
  1443. * The secid behavior is an artifact of an SELinux hack
  1444. * in the USB code. Someday it may go away.
  1445. */
  1446. static int smack_task_kill(struct task_struct *p, struct siginfo *info,
  1447. int sig, u32 secid)
  1448. {
  1449. struct smk_audit_info ad;
  1450. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
  1451. smk_ad_setfield_u_tsk(&ad, p);
  1452. /*
  1453. * Sending a signal requires that the sender
  1454. * can write the receiver.
  1455. */
  1456. if (secid == 0)
  1457. return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
  1458. &ad);
  1459. /*
  1460. * If the secid isn't 0 we're dealing with some USB IO
  1461. * specific behavior. This is not clean. For one thing
  1462. * we can't take privilege into account.
  1463. */
  1464. return smk_access(smack_from_secid(secid),
  1465. smk_of_task(task_security(p)), MAY_WRITE, &ad);
  1466. }
  1467. /**
  1468. * smack_task_wait - Smack access check for waiting
  1469. * @p: task to wait for
  1470. *
  1471. * Returns 0
  1472. */
  1473. static int smack_task_wait(struct task_struct *p)
  1474. {
  1475. /*
  1476. * Allow the operation to succeed.
  1477. * Zombies are bad.
  1478. * In userless environments (e.g. phones) programs
  1479. * get marked with SMACK64EXEC and even if the parent
  1480. * and child shouldn't be talking the parent still
  1481. * may expect to know when the child exits.
  1482. */
  1483. return 0;
  1484. }
  1485. /**
  1486. * smack_task_to_inode - copy task smack into the inode blob
  1487. * @p: task to copy from
  1488. * @inode: inode to copy to
  1489. *
  1490. * Sets the smack pointer in the inode security blob
  1491. */
  1492. static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
  1493. {
  1494. struct inode_smack *isp = inode->i_security;
  1495. isp->smk_inode = smk_of_task(task_security(p));
  1496. }
  1497. /*
  1498. * Socket hooks.
  1499. */
  1500. /**
  1501. * smack_sk_alloc_security - Allocate a socket blob
  1502. * @sk: the socket
  1503. * @family: unused
  1504. * @gfp_flags: memory allocation flags
  1505. *
  1506. * Assign Smack pointers to current
  1507. *
  1508. * Returns 0 on success, -ENOMEM is there's no memory
  1509. */
  1510. static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
  1511. {
  1512. char *csp = smk_of_current();
  1513. struct socket_smack *ssp;
  1514. ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
  1515. if (ssp == NULL)
  1516. return -ENOMEM;
  1517. ssp->smk_in = csp;
  1518. ssp->smk_out = csp;
  1519. ssp->smk_packet = NULL;
  1520. sk->sk_security = ssp;
  1521. return 0;
  1522. }
  1523. /**
  1524. * smack_sk_free_security - Free a socket blob
  1525. * @sk: the socket
  1526. *
  1527. * Clears the blob pointer
  1528. */
  1529. static void smack_sk_free_security(struct sock *sk)
  1530. {
  1531. kfree(sk->sk_security);
  1532. }
  1533. /**
  1534. * smack_host_label - check host based restrictions
  1535. * @sip: the object end
  1536. *
  1537. * looks for host based access restrictions
  1538. *
  1539. * This version will only be appropriate for really small sets of single label
  1540. * hosts. The caller is responsible for ensuring that the RCU read lock is
  1541. * taken before calling this function.
  1542. *
  1543. * Returns the label of the far end or NULL if it's not special.
  1544. */
  1545. static char *smack_host_label(struct sockaddr_in *sip)
  1546. {
  1547. struct smk_netlbladdr *snp;
  1548. struct in_addr *siap = &sip->sin_addr;
  1549. if (siap->s_addr == 0)
  1550. return NULL;
  1551. list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
  1552. /*
  1553. * we break after finding the first match because
  1554. * the list is sorted from longest to shortest mask
  1555. * so we have found the most specific match
  1556. */
  1557. if ((&snp->smk_host.sin_addr)->s_addr ==
  1558. (siap->s_addr & (&snp->smk_mask)->s_addr)) {
  1559. /* we have found the special CIPSO option */
  1560. if (snp->smk_label == smack_cipso_option)
  1561. return NULL;
  1562. return snp->smk_label;
  1563. }
  1564. return NULL;
  1565. }
  1566. /**
  1567. * smack_netlabel - Set the secattr on a socket
  1568. * @sk: the socket
  1569. * @labeled: socket label scheme
  1570. *
  1571. * Convert the outbound smack value (smk_out) to a
  1572. * secattr and attach it to the socket.
  1573. *
  1574. * Returns 0 on success or an error code
  1575. */
  1576. static int smack_netlabel(struct sock *sk, int labeled)
  1577. {
  1578. struct smack_known *skp;
  1579. struct socket_smack *ssp = sk->sk_security;
  1580. int rc = 0;
  1581. /*
  1582. * Usually the netlabel code will handle changing the
  1583. * packet labeling based on the label.
  1584. * The case of a single label host is different, because
  1585. * a single label host should never get a labeled packet
  1586. * even though the label is usually associated with a packet
  1587. * label.
  1588. */
  1589. local_bh_disable();
  1590. bh_lock_sock_nested(sk);
  1591. if (ssp->smk_out == smack_net_ambient ||
  1592. labeled == SMACK_UNLABELED_SOCKET)
  1593. netlbl_sock_delattr(sk);
  1594. else {
  1595. skp = smk_find_entry(ssp->smk_out);
  1596. rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
  1597. }
  1598. bh_unlock_sock(sk);
  1599. local_bh_enable();
  1600. return rc;
  1601. }
  1602. /**
  1603. * smack_netlbel_send - Set the secattr on a socket and perform access checks
  1604. * @sk: the socket
  1605. * @sap: the destination address
  1606. *
  1607. * Set the correct secattr for the given socket based on the destination
  1608. * address and perform any outbound access checks needed.
  1609. *
  1610. * Returns 0 on success or an error code.
  1611. *
  1612. */
  1613. static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
  1614. {
  1615. int rc;
  1616. int sk_lbl;
  1617. char *hostsp;
  1618. struct socket_smack *ssp = sk->sk_security;
  1619. struct smk_audit_info ad;
  1620. rcu_read_lock();
  1621. hostsp = smack_host_label(sap);
  1622. if (hostsp != NULL) {
  1623. #ifdef CONFIG_AUDIT
  1624. struct lsm_network_audit net;
  1625. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  1626. ad.a.u.net->family = sap->sin_family;
  1627. ad.a.u.net->dport = sap->sin_port;
  1628. ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
  1629. #endif
  1630. sk_lbl = SMACK_UNLABELED_SOCKET;
  1631. rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
  1632. } else {
  1633. sk_lbl = SMACK_CIPSO_SOCKET;
  1634. rc = 0;
  1635. }
  1636. rcu_read_unlock();
  1637. if (rc != 0)
  1638. return rc;
  1639. return smack_netlabel(sk, sk_lbl);
  1640. }
  1641. /**
  1642. * smack_inode_setsecurity - set smack xattrs
  1643. * @inode: the object
  1644. * @name: attribute name
  1645. * @value: attribute value
  1646. * @size: size of the attribute
  1647. * @flags: unused
  1648. *
  1649. * Sets the named attribute in the appropriate blob
  1650. *
  1651. * Returns 0 on success, or an error code
  1652. */
  1653. static int smack_inode_setsecurity(struct inode *inode, const char *name,
  1654. const void *value, size_t size, int flags)
  1655. {
  1656. char *sp;
  1657. struct inode_smack *nsp = inode->i_security;
  1658. struct socket_smack *ssp;
  1659. struct socket *sock;
  1660. int rc = 0;
  1661. if (value == NULL || size > SMK_LONGLABEL || size == 0)
  1662. return -EACCES;
  1663. sp = smk_import(value, size);
  1664. if (sp == NULL)
  1665. return -EINVAL;
  1666. if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
  1667. nsp->smk_inode = sp;
  1668. nsp->smk_flags |= SMK_INODE_INSTANT;
  1669. return 0;
  1670. }
  1671. /*
  1672. * The rest of the Smack xattrs are only on sockets.
  1673. */
  1674. if (inode->i_sb->s_magic != SOCKFS_MAGIC)
  1675. return -EOPNOTSUPP;
  1676. sock = SOCKET_I(inode);
  1677. if (sock == NULL || sock->sk == NULL)
  1678. return -EOPNOTSUPP;
  1679. ssp = sock->sk->sk_security;
  1680. if (strcmp(name, XATTR_SMACK_IPIN) == 0)
  1681. ssp->smk_in = sp;
  1682. else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
  1683. ssp->smk_out = sp;
  1684. if (sock->sk->sk_family != PF_UNIX) {
  1685. rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  1686. if (rc != 0)
  1687. printk(KERN_WARNING
  1688. "Smack: \"%s\" netlbl error %d.\n",
  1689. __func__, -rc);
  1690. }
  1691. } else
  1692. return -EOPNOTSUPP;
  1693. return 0;
  1694. }
  1695. /**
  1696. * smack_socket_post_create - finish socket setup
  1697. * @sock: the socket
  1698. * @family: protocol family
  1699. * @type: unused
  1700. * @protocol: unused
  1701. * @kern: unused
  1702. *
  1703. * Sets the netlabel information on the socket
  1704. *
  1705. * Returns 0 on success, and error code otherwise
  1706. */
  1707. static int smack_socket_post_create(struct socket *sock, int family,
  1708. int type, int protocol, int kern)
  1709. {
  1710. if (family != PF_INET || sock->sk == NULL)
  1711. return 0;
  1712. /*
  1713. * Set the outbound netlbl.
  1714. */
  1715. return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
  1716. }
  1717. /**
  1718. * smack_socket_connect - connect access check
  1719. * @sock: the socket
  1720. * @sap: the other end
  1721. * @addrlen: size of sap
  1722. *
  1723. * Verifies that a connection may be possible
  1724. *
  1725. * Returns 0 on success, and error code otherwise
  1726. */
  1727. static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
  1728. int addrlen)
  1729. {
  1730. if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
  1731. return 0;
  1732. if (addrlen < sizeof(struct sockaddr_in))
  1733. return -EINVAL;
  1734. return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
  1735. }
  1736. /**
  1737. * smack_flags_to_may - convert S_ to MAY_ values
  1738. * @flags: the S_ value
  1739. *
  1740. * Returns the equivalent MAY_ value
  1741. */
  1742. static int smack_flags_to_may(int flags)
  1743. {
  1744. int may = 0;
  1745. if (flags & S_IRUGO)
  1746. may |= MAY_READ;
  1747. if (flags & S_IWUGO)
  1748. may |= MAY_WRITE;
  1749. if (flags & S_IXUGO)
  1750. may |= MAY_EXEC;
  1751. return may;
  1752. }
  1753. /**
  1754. * smack_msg_msg_alloc_security - Set the security blob for msg_msg
  1755. * @msg: the object
  1756. *
  1757. * Returns 0
  1758. */
  1759. static int smack_msg_msg_alloc_security(struct msg_msg *msg)
  1760. {
  1761. msg->security = smk_of_current();
  1762. return 0;
  1763. }
  1764. /**
  1765. * smack_msg_msg_free_security - Clear the security blob for msg_msg
  1766. * @msg: the object
  1767. *
  1768. * Clears the blob pointer
  1769. */
  1770. static void smack_msg_msg_free_security(struct msg_msg *msg)
  1771. {
  1772. msg->security = NULL;
  1773. }
  1774. /**
  1775. * smack_of_shm - the smack pointer for the shm
  1776. * @shp: the object
  1777. *
  1778. * Returns a pointer to the smack value
  1779. */
  1780. static char *smack_of_shm(struct shmid_kernel *shp)
  1781. {
  1782. return (char *)shp->shm_perm.security;
  1783. }
  1784. /**
  1785. * smack_shm_alloc_security - Set the security blob for shm
  1786. * @shp: the object
  1787. *
  1788. * Returns 0
  1789. */
  1790. static int smack_shm_alloc_security(struct shmid_kernel *shp)
  1791. {
  1792. struct kern_ipc_perm *isp = &shp->shm_perm;
  1793. isp->security = smk_of_current();
  1794. return 0;
  1795. }
  1796. /**
  1797. * smack_shm_free_security - Clear the security blob for shm
  1798. * @shp: the object
  1799. *
  1800. * Clears the blob pointer
  1801. */
  1802. static void smack_shm_free_security(struct shmid_kernel *shp)
  1803. {
  1804. struct kern_ipc_perm *isp = &shp->shm_perm;
  1805. isp->security = NULL;
  1806. }
  1807. /**
  1808. * smk_curacc_shm : check if current has access on shm
  1809. * @shp : the object
  1810. * @access : access requested
  1811. *
  1812. * Returns 0 if current has the requested access, error code otherwise
  1813. */
  1814. static int smk_curacc_shm(struct shmid_kernel *shp, int access)
  1815. {
  1816. char *ssp = smack_of_shm(shp);
  1817. struct smk_audit_info ad;
  1818. #ifdef CONFIG_AUDIT
  1819. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  1820. ad.a.u.ipc_id = shp->shm_perm.id;
  1821. #endif
  1822. return smk_curacc(ssp, access, &ad);
  1823. }
  1824. /**
  1825. * smack_shm_associate - Smack access check for shm
  1826. * @shp: the object
  1827. * @shmflg: access requested
  1828. *
  1829. * Returns 0 if current has the requested access, error code otherwise
  1830. */
  1831. static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
  1832. {
  1833. int may;
  1834. may = smack_flags_to_may(shmflg);
  1835. return smk_curacc_shm(shp, may);
  1836. }
  1837. /**
  1838. * smack_shm_shmctl - Smack access check for shm
  1839. * @shp: the object
  1840. * @cmd: what it wants to do
  1841. *
  1842. * Returns 0 if current has the requested access, error code otherwise
  1843. */
  1844. static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
  1845. {
  1846. int may;
  1847. switch (cmd) {
  1848. case IPC_STAT:
  1849. case SHM_STAT:
  1850. may = MAY_READ;
  1851. break;
  1852. case IPC_SET:
  1853. case SHM_LOCK:
  1854. case SHM_UNLOCK:
  1855. case IPC_RMID:
  1856. may = MAY_READWRITE;
  1857. break;
  1858. case IPC_INFO:
  1859. case SHM_INFO:
  1860. /*
  1861. * System level information.
  1862. */
  1863. return 0;
  1864. default:
  1865. return -EINVAL;
  1866. }
  1867. return smk_curacc_shm(shp, may);
  1868. }
  1869. /**
  1870. * smack_shm_shmat - Smack access for shmat
  1871. * @shp: the object
  1872. * @shmaddr: unused
  1873. * @shmflg: access requested
  1874. *
  1875. * Returns 0 if current has the requested access, error code otherwise
  1876. */
  1877. static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
  1878. int shmflg)
  1879. {
  1880. int may;
  1881. may = smack_flags_to_may(shmflg);
  1882. return smk_curacc_shm(shp, may);
  1883. }
  1884. /**
  1885. * smack_of_sem - the smack pointer for the sem
  1886. * @sma: the object
  1887. *
  1888. * Returns a pointer to the smack value
  1889. */
  1890. static char *smack_of_sem(struct sem_array *sma)
  1891. {
  1892. return (char *)sma->sem_perm.security;
  1893. }
  1894. /**
  1895. * smack_sem_alloc_security - Set the security blob for sem
  1896. * @sma: the object
  1897. *
  1898. * Returns 0
  1899. */
  1900. static int smack_sem_alloc_security(struct sem_array *sma)
  1901. {
  1902. struct kern_ipc_perm *isp = &sma->sem_perm;
  1903. isp->security = smk_of_current();
  1904. return 0;
  1905. }
  1906. /**
  1907. * smack_sem_free_security - Clear the security blob for sem
  1908. * @sma: the object
  1909. *
  1910. * Clears the blob pointer
  1911. */
  1912. static void smack_sem_free_security(struct sem_array *sma)
  1913. {
  1914. struct kern_ipc_perm *isp = &sma->sem_perm;
  1915. isp->security = NULL;
  1916. }
  1917. /**
  1918. * smk_curacc_sem : check if current has access on sem
  1919. * @sma : the object
  1920. * @access : access requested
  1921. *
  1922. * Returns 0 if current has the requested access, error code otherwise
  1923. */
  1924. static int smk_curacc_sem(struct sem_array *sma, int access)
  1925. {
  1926. char *ssp = smack_of_sem(sma);
  1927. struct smk_audit_info ad;
  1928. #ifdef CONFIG_AUDIT
  1929. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  1930. ad.a.u.ipc_id = sma->sem_perm.id;
  1931. #endif
  1932. return smk_curacc(ssp, access, &ad);
  1933. }
  1934. /**
  1935. * smack_sem_associate - Smack access check for sem
  1936. * @sma: the object
  1937. * @semflg: access requested
  1938. *
  1939. * Returns 0 if current has the requested access, error code otherwise
  1940. */
  1941. static int smack_sem_associate(struct sem_array *sma, int semflg)
  1942. {
  1943. int may;
  1944. may = smack_flags_to_may(semflg);
  1945. return smk_curacc_sem(sma, may);
  1946. }
  1947. /**
  1948. * smack_sem_shmctl - Smack access check for sem
  1949. * @sma: the object
  1950. * @cmd: what it wants to do
  1951. *
  1952. * Returns 0 if current has the requested access, error code otherwise
  1953. */
  1954. static int smack_sem_semctl(struct sem_array *sma, int cmd)
  1955. {
  1956. int may;
  1957. switch (cmd) {
  1958. case GETPID:
  1959. case GETNCNT:
  1960. case GETZCNT:
  1961. case GETVAL:
  1962. case GETALL:
  1963. case IPC_STAT:
  1964. case SEM_STAT:
  1965. may = MAY_READ;
  1966. break;
  1967. case SETVAL:
  1968. case SETALL:
  1969. case IPC_RMID:
  1970. case IPC_SET:
  1971. may = MAY_READWRITE;
  1972. break;
  1973. case IPC_INFO:
  1974. case SEM_INFO:
  1975. /*
  1976. * System level information
  1977. */
  1978. return 0;
  1979. default:
  1980. return -EINVAL;
  1981. }
  1982. return smk_curacc_sem(sma, may);
  1983. }
  1984. /**
  1985. * smack_sem_semop - Smack checks of semaphore operations
  1986. * @sma: the object
  1987. * @sops: unused
  1988. * @nsops: unused
  1989. * @alter: unused
  1990. *
  1991. * Treated as read and write in all cases.
  1992. *
  1993. * Returns 0 if access is allowed, error code otherwise
  1994. */
  1995. static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
  1996. unsigned nsops, int alter)
  1997. {
  1998. return smk_curacc_sem(sma, MAY_READWRITE);
  1999. }
  2000. /**
  2001. * smack_msg_alloc_security - Set the security blob for msg
  2002. * @msq: the object
  2003. *
  2004. * Returns 0
  2005. */
  2006. static int smack_msg_queue_alloc_security(struct msg_queue *msq)
  2007. {
  2008. struct kern_ipc_perm *kisp = &msq->q_perm;
  2009. kisp->security = smk_of_current();
  2010. return 0;
  2011. }
  2012. /**
  2013. * smack_msg_free_security - Clear the security blob for msg
  2014. * @msq: the object
  2015. *
  2016. * Clears the blob pointer
  2017. */
  2018. static void smack_msg_queue_free_security(struct msg_queue *msq)
  2019. {
  2020. struct kern_ipc_perm *kisp = &msq->q_perm;
  2021. kisp->security = NULL;
  2022. }
  2023. /**
  2024. * smack_of_msq - the smack pointer for the msq
  2025. * @msq: the object
  2026. *
  2027. * Returns a pointer to the smack value
  2028. */
  2029. static char *smack_of_msq(struct msg_queue *msq)
  2030. {
  2031. return (char *)msq->q_perm.security;
  2032. }
  2033. /**
  2034. * smk_curacc_msq : helper to check if current has access on msq
  2035. * @msq : the msq
  2036. * @access : access requested
  2037. *
  2038. * return 0 if current has access, error otherwise
  2039. */
  2040. static int smk_curacc_msq(struct msg_queue *msq, int access)
  2041. {
  2042. char *msp = smack_of_msq(msq);
  2043. struct smk_audit_info ad;
  2044. #ifdef CONFIG_AUDIT
  2045. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2046. ad.a.u.ipc_id = msq->q_perm.id;
  2047. #endif
  2048. return smk_curacc(msp, access, &ad);
  2049. }
  2050. /**
  2051. * smack_msg_queue_associate - Smack access check for msg_queue
  2052. * @msq: the object
  2053. * @msqflg: access requested
  2054. *
  2055. * Returns 0 if current has the requested access, error code otherwise
  2056. */
  2057. static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
  2058. {
  2059. int may;
  2060. may = smack_flags_to_may(msqflg);
  2061. return smk_curacc_msq(msq, may);
  2062. }
  2063. /**
  2064. * smack_msg_queue_msgctl - Smack access check for msg_queue
  2065. * @msq: the object
  2066. * @cmd: what it wants to do
  2067. *
  2068. * Returns 0 if current has the requested access, error code otherwise
  2069. */
  2070. static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
  2071. {
  2072. int may;
  2073. switch (cmd) {
  2074. case IPC_STAT:
  2075. case MSG_STAT:
  2076. may = MAY_READ;
  2077. break;
  2078. case IPC_SET:
  2079. case IPC_RMID:
  2080. may = MAY_READWRITE;
  2081. break;
  2082. case IPC_INFO:
  2083. case MSG_INFO:
  2084. /*
  2085. * System level information
  2086. */
  2087. return 0;
  2088. default:
  2089. return -EINVAL;
  2090. }
  2091. return smk_curacc_msq(msq, may);
  2092. }
  2093. /**
  2094. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2095. * @msq: the object
  2096. * @msg: unused
  2097. * @msqflg: access requested
  2098. *
  2099. * Returns 0 if current has the requested access, error code otherwise
  2100. */
  2101. static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
  2102. int msqflg)
  2103. {
  2104. int may;
  2105. may = smack_flags_to_may(msqflg);
  2106. return smk_curacc_msq(msq, may);
  2107. }
  2108. /**
  2109. * smack_msg_queue_msgsnd - Smack access check for msg_queue
  2110. * @msq: the object
  2111. * @msg: unused
  2112. * @target: unused
  2113. * @type: unused
  2114. * @mode: unused
  2115. *
  2116. * Returns 0 if current has read and write access, error code otherwise
  2117. */
  2118. static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
  2119. struct task_struct *target, long type, int mode)
  2120. {
  2121. return smk_curacc_msq(msq, MAY_READWRITE);
  2122. }
  2123. /**
  2124. * smack_ipc_permission - Smack access for ipc_permission()
  2125. * @ipp: the object permissions
  2126. * @flag: access requested
  2127. *
  2128. * Returns 0 if current has read and write access, error code otherwise
  2129. */
  2130. static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
  2131. {
  2132. char *isp = ipp->security;
  2133. int may = smack_flags_to_may(flag);
  2134. struct smk_audit_info ad;
  2135. #ifdef CONFIG_AUDIT
  2136. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
  2137. ad.a.u.ipc_id = ipp->id;
  2138. #endif
  2139. return smk_curacc(isp, may, &ad);
  2140. }
  2141. /**
  2142. * smack_ipc_getsecid - Extract smack security id
  2143. * @ipp: the object permissions
  2144. * @secid: where result will be saved
  2145. */
  2146. static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
  2147. {
  2148. char *smack = ipp->security;
  2149. *secid = smack_to_secid(smack);
  2150. }
  2151. /**
  2152. * smack_d_instantiate - Make sure the blob is correct on an inode
  2153. * @opt_dentry: dentry where inode will be attached
  2154. * @inode: the object
  2155. *
  2156. * Set the inode's security blob if it hasn't been done already.
  2157. */
  2158. static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
  2159. {
  2160. struct super_block *sbp;
  2161. struct superblock_smack *sbsp;
  2162. struct inode_smack *isp;
  2163. char *csp = smk_of_current();
  2164. char *fetched;
  2165. char *final;
  2166. char trattr[TRANS_TRUE_SIZE];
  2167. int transflag = 0;
  2168. int rc;
  2169. struct dentry *dp;
  2170. if (inode == NULL)
  2171. return;
  2172. isp = inode->i_security;
  2173. mutex_lock(&isp->smk_lock);
  2174. /*
  2175. * If the inode is already instantiated
  2176. * take the quick way out
  2177. */
  2178. if (isp->smk_flags & SMK_INODE_INSTANT)
  2179. goto unlockandout;
  2180. sbp = inode->i_sb;
  2181. sbsp = sbp->s_security;
  2182. /*
  2183. * We're going to use the superblock default label
  2184. * if there's no label on the file.
  2185. */
  2186. final = sbsp->smk_default;
  2187. /*
  2188. * If this is the root inode the superblock
  2189. * may be in the process of initialization.
  2190. * If that is the case use the root value out
  2191. * of the superblock.
  2192. */
  2193. if (opt_dentry->d_parent == opt_dentry) {
  2194. isp->smk_inode = sbsp->smk_root;
  2195. isp->smk_flags |= SMK_INODE_INSTANT;
  2196. goto unlockandout;
  2197. }
  2198. /*
  2199. * This is pretty hackish.
  2200. * Casey says that we shouldn't have to do
  2201. * file system specific code, but it does help
  2202. * with keeping it simple.
  2203. */
  2204. switch (sbp->s_magic) {
  2205. case SMACK_MAGIC:
  2206. /*
  2207. * Casey says that it's a little embarrassing
  2208. * that the smack file system doesn't do
  2209. * extended attributes.
  2210. */
  2211. final = smack_known_star.smk_known;
  2212. break;
  2213. case PIPEFS_MAGIC:
  2214. /*
  2215. * Casey says pipes are easy (?)
  2216. */
  2217. final = smack_known_star.smk_known;
  2218. break;
  2219. case DEVPTS_SUPER_MAGIC:
  2220. /*
  2221. * devpts seems content with the label of the task.
  2222. * Programs that change smack have to treat the
  2223. * pty with respect.
  2224. */
  2225. final = csp;
  2226. break;
  2227. case SOCKFS_MAGIC:
  2228. /*
  2229. * Socket access is controlled by the socket
  2230. * structures associated with the task involved.
  2231. */
  2232. final = smack_known_star.smk_known;
  2233. break;
  2234. case PROC_SUPER_MAGIC:
  2235. /*
  2236. * Casey says procfs appears not to care.
  2237. * The superblock default suffices.
  2238. */
  2239. break;
  2240. case TMPFS_MAGIC:
  2241. /*
  2242. * Device labels should come from the filesystem,
  2243. * but watch out, because they're volitile,
  2244. * getting recreated on every reboot.
  2245. */
  2246. final = smack_known_star.smk_known;
  2247. /*
  2248. * No break.
  2249. *
  2250. * If a smack value has been set we want to use it,
  2251. * but since tmpfs isn't giving us the opportunity
  2252. * to set mount options simulate setting the
  2253. * superblock default.
  2254. */
  2255. default:
  2256. /*
  2257. * This isn't an understood special case.
  2258. * Get the value from the xattr.
  2259. */
  2260. /*
  2261. * UNIX domain sockets use lower level socket data.
  2262. */
  2263. if (S_ISSOCK(inode->i_mode)) {
  2264. final = smack_known_star.smk_known;
  2265. break;
  2266. }
  2267. /*
  2268. * No xattr support means, alas, no SMACK label.
  2269. * Use the aforeapplied default.
  2270. * It would be curious if the label of the task
  2271. * does not match that assigned.
  2272. */
  2273. if (inode->i_op->getxattr == NULL)
  2274. break;
  2275. /*
  2276. * Get the dentry for xattr.
  2277. */
  2278. dp = dget(opt_dentry);
  2279. fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
  2280. if (fetched != NULL)
  2281. final = fetched;
  2282. /*
  2283. * Transmuting directory
  2284. */
  2285. if (S_ISDIR(inode->i_mode)) {
  2286. /*
  2287. * If this is a new directory and the label was
  2288. * transmuted when the inode was initialized
  2289. * set the transmute attribute on the directory
  2290. * and mark the inode.
  2291. *
  2292. * If there is a transmute attribute on the
  2293. * directory mark the inode.
  2294. */
  2295. if (isp->smk_flags & SMK_INODE_CHANGED) {
  2296. isp->smk_flags &= ~SMK_INODE_CHANGED;
  2297. rc = inode->i_op->setxattr(dp,
  2298. XATTR_NAME_SMACKTRANSMUTE,
  2299. TRANS_TRUE, TRANS_TRUE_SIZE,
  2300. 0);
  2301. } else {
  2302. rc = inode->i_op->getxattr(dp,
  2303. XATTR_NAME_SMACKTRANSMUTE, trattr,
  2304. TRANS_TRUE_SIZE);
  2305. if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
  2306. TRANS_TRUE_SIZE) != 0)
  2307. rc = -EINVAL;
  2308. }
  2309. if (rc >= 0)
  2310. transflag = SMK_INODE_TRANSMUTE;
  2311. }
  2312. isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
  2313. isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
  2314. dput(dp);
  2315. break;
  2316. }
  2317. if (final == NULL)
  2318. isp->smk_inode = csp;
  2319. else
  2320. isp->smk_inode = final;
  2321. isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
  2322. unlockandout:
  2323. mutex_unlock(&isp->smk_lock);
  2324. return;
  2325. }
  2326. /**
  2327. * smack_getprocattr - Smack process attribute access
  2328. * @p: the object task
  2329. * @name: the name of the attribute in /proc/.../attr
  2330. * @value: where to put the result
  2331. *
  2332. * Places a copy of the task Smack into value
  2333. *
  2334. * Returns the length of the smack label or an error code
  2335. */
  2336. static int smack_getprocattr(struct task_struct *p, char *name, char **value)
  2337. {
  2338. char *cp;
  2339. int slen;
  2340. if (strcmp(name, "current") != 0)
  2341. return -EINVAL;
  2342. cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
  2343. if (cp == NULL)
  2344. return -ENOMEM;
  2345. slen = strlen(cp);
  2346. *value = cp;
  2347. return slen;
  2348. }
  2349. /**
  2350. * smack_setprocattr - Smack process attribute setting
  2351. * @p: the object task
  2352. * @name: the name of the attribute in /proc/.../attr
  2353. * @value: the value to set
  2354. * @size: the size of the value
  2355. *
  2356. * Sets the Smack value of the task. Only setting self
  2357. * is permitted and only with privilege
  2358. *
  2359. * Returns the length of the smack label or an error code
  2360. */
  2361. static int smack_setprocattr(struct task_struct *p, char *name,
  2362. void *value, size_t size)
  2363. {
  2364. struct task_smack *tsp;
  2365. struct cred *new;
  2366. char *newsmack;
  2367. /*
  2368. * Changing another process' Smack value is too dangerous
  2369. * and supports no sane use case.
  2370. */
  2371. if (p != current)
  2372. return -EPERM;
  2373. if (!smack_privileged(CAP_MAC_ADMIN))
  2374. return -EPERM;
  2375. if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
  2376. return -EINVAL;
  2377. if (strcmp(name, "current") != 0)
  2378. return -EINVAL;
  2379. newsmack = smk_import(value, size);
  2380. if (newsmack == NULL)
  2381. return -EINVAL;
  2382. /*
  2383. * No process is ever allowed the web ("@") label.
  2384. */
  2385. if (newsmack == smack_known_web.smk_known)
  2386. return -EPERM;
  2387. new = prepare_creds();
  2388. if (new == NULL)
  2389. return -ENOMEM;
  2390. tsp = new->security;
  2391. tsp->smk_task = newsmack;
  2392. commit_creds(new);
  2393. return size;
  2394. }
  2395. /**
  2396. * smack_unix_stream_connect - Smack access on UDS
  2397. * @sock: one sock
  2398. * @other: the other sock
  2399. * @newsk: unused
  2400. *
  2401. * Return 0 if a subject with the smack of sock could access
  2402. * an object with the smack of other, otherwise an error code
  2403. */
  2404. static int smack_unix_stream_connect(struct sock *sock,
  2405. struct sock *other, struct sock *newsk)
  2406. {
  2407. struct socket_smack *ssp = sock->sk_security;
  2408. struct socket_smack *osp = other->sk_security;
  2409. struct socket_smack *nsp = newsk->sk_security;
  2410. struct smk_audit_info ad;
  2411. int rc = 0;
  2412. #ifdef CONFIG_AUDIT
  2413. struct lsm_network_audit net;
  2414. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2415. smk_ad_setfield_u_net_sk(&ad, other);
  2416. #endif
  2417. if (!smack_privileged(CAP_MAC_OVERRIDE))
  2418. rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
  2419. /*
  2420. * Cross reference the peer labels for SO_PEERSEC.
  2421. */
  2422. if (rc == 0) {
  2423. nsp->smk_packet = ssp->smk_out;
  2424. ssp->smk_packet = osp->smk_out;
  2425. }
  2426. return rc;
  2427. }
  2428. /**
  2429. * smack_unix_may_send - Smack access on UDS
  2430. * @sock: one socket
  2431. * @other: the other socket
  2432. *
  2433. * Return 0 if a subject with the smack of sock could access
  2434. * an object with the smack of other, otherwise an error code
  2435. */
  2436. static int smack_unix_may_send(struct socket *sock, struct socket *other)
  2437. {
  2438. struct socket_smack *ssp = sock->sk->sk_security;
  2439. struct socket_smack *osp = other->sk->sk_security;
  2440. struct smk_audit_info ad;
  2441. int rc = 0;
  2442. #ifdef CONFIG_AUDIT
  2443. struct lsm_network_audit net;
  2444. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2445. smk_ad_setfield_u_net_sk(&ad, other->sk);
  2446. #endif
  2447. if (!smack_privileged(CAP_MAC_OVERRIDE))
  2448. rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
  2449. return rc;
  2450. }
  2451. /**
  2452. * smack_socket_sendmsg - Smack check based on destination host
  2453. * @sock: the socket
  2454. * @msg: the message
  2455. * @size: the size of the message
  2456. *
  2457. * Return 0 if the current subject can write to the destination
  2458. * host. This is only a question if the destination is a single
  2459. * label host.
  2460. */
  2461. static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  2462. int size)
  2463. {
  2464. struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
  2465. /*
  2466. * Perfectly reasonable for this to be NULL
  2467. */
  2468. if (sip == NULL || sip->sin_family != AF_INET)
  2469. return 0;
  2470. return smack_netlabel_send(sock->sk, sip);
  2471. }
  2472. /**
  2473. * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
  2474. * @sap: netlabel secattr
  2475. * @ssp: socket security information
  2476. *
  2477. * Returns a pointer to a Smack label found on the label list.
  2478. */
  2479. static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
  2480. struct socket_smack *ssp)
  2481. {
  2482. struct smack_known *kp;
  2483. char *sp;
  2484. int found = 0;
  2485. if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
  2486. /*
  2487. * Looks like a CIPSO packet.
  2488. * If there are flags but no level netlabel isn't
  2489. * behaving the way we expect it to.
  2490. *
  2491. * Look it up in the label table
  2492. * Without guidance regarding the smack value
  2493. * for the packet fall back on the network
  2494. * ambient value.
  2495. */
  2496. rcu_read_lock();
  2497. list_for_each_entry(kp, &smack_known_list, list) {
  2498. if (sap->attr.mls.lvl != kp->smk_netlabel.attr.mls.lvl)
  2499. continue;
  2500. if (memcmp(sap->attr.mls.cat,
  2501. kp->smk_netlabel.attr.mls.cat,
  2502. SMK_CIPSOLEN) != 0)
  2503. continue;
  2504. found = 1;
  2505. break;
  2506. }
  2507. rcu_read_unlock();
  2508. if (found)
  2509. return kp->smk_known;
  2510. if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
  2511. return smack_known_web.smk_known;
  2512. return smack_known_star.smk_known;
  2513. }
  2514. if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
  2515. /*
  2516. * Looks like a fallback, which gives us a secid.
  2517. */
  2518. sp = smack_from_secid(sap->attr.secid);
  2519. /*
  2520. * This has got to be a bug because it is
  2521. * impossible to specify a fallback without
  2522. * specifying the label, which will ensure
  2523. * it has a secid, and the only way to get a
  2524. * secid is from a fallback.
  2525. */
  2526. BUG_ON(sp == NULL);
  2527. return sp;
  2528. }
  2529. /*
  2530. * Without guidance regarding the smack value
  2531. * for the packet fall back on the network
  2532. * ambient value.
  2533. */
  2534. return smack_net_ambient;
  2535. }
  2536. /**
  2537. * smack_socket_sock_rcv_skb - Smack packet delivery access check
  2538. * @sk: socket
  2539. * @skb: packet
  2540. *
  2541. * Returns 0 if the packet should be delivered, an error code otherwise
  2542. */
  2543. static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  2544. {
  2545. struct netlbl_lsm_secattr secattr;
  2546. struct socket_smack *ssp = sk->sk_security;
  2547. char *csp;
  2548. int rc;
  2549. struct smk_audit_info ad;
  2550. #ifdef CONFIG_AUDIT
  2551. struct lsm_network_audit net;
  2552. #endif
  2553. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  2554. return 0;
  2555. /*
  2556. * Translate what netlabel gave us.
  2557. */
  2558. netlbl_secattr_init(&secattr);
  2559. rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
  2560. if (rc == 0)
  2561. csp = smack_from_secattr(&secattr, ssp);
  2562. else
  2563. csp = smack_net_ambient;
  2564. netlbl_secattr_destroy(&secattr);
  2565. #ifdef CONFIG_AUDIT
  2566. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2567. ad.a.u.net->family = sk->sk_family;
  2568. ad.a.u.net->netif = skb->skb_iif;
  2569. ipv4_skb_to_auditdata(skb, &ad.a, NULL);
  2570. #endif
  2571. /*
  2572. * Receiving a packet requires that the other end
  2573. * be able to write here. Read access is not required.
  2574. * This is the simplist possible security model
  2575. * for networking.
  2576. */
  2577. rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
  2578. if (rc != 0)
  2579. netlbl_skbuff_err(skb, rc, 0);
  2580. return rc;
  2581. }
  2582. /**
  2583. * smack_socket_getpeersec_stream - pull in packet label
  2584. * @sock: the socket
  2585. * @optval: user's destination
  2586. * @optlen: size thereof
  2587. * @len: max thereof
  2588. *
  2589. * returns zero on success, an error code otherwise
  2590. */
  2591. static int smack_socket_getpeersec_stream(struct socket *sock,
  2592. char __user *optval,
  2593. int __user *optlen, unsigned len)
  2594. {
  2595. struct socket_smack *ssp;
  2596. char *rcp = "";
  2597. int slen = 1;
  2598. int rc = 0;
  2599. ssp = sock->sk->sk_security;
  2600. if (ssp->smk_packet != NULL) {
  2601. rcp = ssp->smk_packet;
  2602. slen = strlen(rcp) + 1;
  2603. }
  2604. if (slen > len)
  2605. rc = -ERANGE;
  2606. else if (copy_to_user(optval, rcp, slen) != 0)
  2607. rc = -EFAULT;
  2608. if (put_user(slen, optlen) != 0)
  2609. rc = -EFAULT;
  2610. return rc;
  2611. }
  2612. /**
  2613. * smack_socket_getpeersec_dgram - pull in packet label
  2614. * @sock: the peer socket
  2615. * @skb: packet data
  2616. * @secid: pointer to where to put the secid of the packet
  2617. *
  2618. * Sets the netlabel socket state on sk from parent
  2619. */
  2620. static int smack_socket_getpeersec_dgram(struct socket *sock,
  2621. struct sk_buff *skb, u32 *secid)
  2622. {
  2623. struct netlbl_lsm_secattr secattr;
  2624. struct socket_smack *ssp = NULL;
  2625. char *sp;
  2626. int family = PF_UNSPEC;
  2627. u32 s = 0; /* 0 is the invalid secid */
  2628. int rc;
  2629. if (skb != NULL) {
  2630. if (skb->protocol == htons(ETH_P_IP))
  2631. family = PF_INET;
  2632. else if (skb->protocol == htons(ETH_P_IPV6))
  2633. family = PF_INET6;
  2634. }
  2635. if (family == PF_UNSPEC && sock != NULL)
  2636. family = sock->sk->sk_family;
  2637. if (family == PF_UNIX) {
  2638. ssp = sock->sk->sk_security;
  2639. s = smack_to_secid(ssp->smk_out);
  2640. } else if (family == PF_INET || family == PF_INET6) {
  2641. /*
  2642. * Translate what netlabel gave us.
  2643. */
  2644. if (sock != NULL && sock->sk != NULL)
  2645. ssp = sock->sk->sk_security;
  2646. netlbl_secattr_init(&secattr);
  2647. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  2648. if (rc == 0) {
  2649. sp = smack_from_secattr(&secattr, ssp);
  2650. s = smack_to_secid(sp);
  2651. }
  2652. netlbl_secattr_destroy(&secattr);
  2653. }
  2654. *secid = s;
  2655. if (s == 0)
  2656. return -EINVAL;
  2657. return 0;
  2658. }
  2659. /**
  2660. * smack_sock_graft - Initialize a newly created socket with an existing sock
  2661. * @sk: child sock
  2662. * @parent: parent socket
  2663. *
  2664. * Set the smk_{in,out} state of an existing sock based on the process that
  2665. * is creating the new socket.
  2666. */
  2667. static void smack_sock_graft(struct sock *sk, struct socket *parent)
  2668. {
  2669. struct socket_smack *ssp;
  2670. if (sk == NULL ||
  2671. (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
  2672. return;
  2673. ssp = sk->sk_security;
  2674. ssp->smk_in = ssp->smk_out = smk_of_current();
  2675. /* cssp->smk_packet is already set in smack_inet_csk_clone() */
  2676. }
  2677. /**
  2678. * smack_inet_conn_request - Smack access check on connect
  2679. * @sk: socket involved
  2680. * @skb: packet
  2681. * @req: unused
  2682. *
  2683. * Returns 0 if a task with the packet label could write to
  2684. * the socket, otherwise an error code
  2685. */
  2686. static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
  2687. struct request_sock *req)
  2688. {
  2689. u16 family = sk->sk_family;
  2690. struct smack_known *skp;
  2691. struct socket_smack *ssp = sk->sk_security;
  2692. struct netlbl_lsm_secattr secattr;
  2693. struct sockaddr_in addr;
  2694. struct iphdr *hdr;
  2695. char *sp;
  2696. char *hsp;
  2697. int rc;
  2698. struct smk_audit_info ad;
  2699. #ifdef CONFIG_AUDIT
  2700. struct lsm_network_audit net;
  2701. #endif
  2702. /* handle mapped IPv4 packets arriving via IPv6 sockets */
  2703. if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
  2704. family = PF_INET;
  2705. netlbl_secattr_init(&secattr);
  2706. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  2707. if (rc == 0)
  2708. sp = smack_from_secattr(&secattr, ssp);
  2709. else
  2710. sp = smack_known_huh.smk_known;
  2711. netlbl_secattr_destroy(&secattr);
  2712. #ifdef CONFIG_AUDIT
  2713. smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
  2714. ad.a.u.net->family = family;
  2715. ad.a.u.net->netif = skb->skb_iif;
  2716. ipv4_skb_to_auditdata(skb, &ad.a, NULL);
  2717. #endif
  2718. /*
  2719. * Receiving a packet requires that the other end be able to write
  2720. * here. Read access is not required.
  2721. */
  2722. rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
  2723. if (rc != 0)
  2724. return rc;
  2725. /*
  2726. * Save the peer's label in the request_sock so we can later setup
  2727. * smk_packet in the child socket so that SO_PEERCRED can report it.
  2728. */
  2729. req->peer_secid = smack_to_secid(sp);
  2730. /*
  2731. * We need to decide if we want to label the incoming connection here
  2732. * if we do we only need to label the request_sock and the stack will
  2733. * propagate the wire-label to the sock when it is created.
  2734. */
  2735. hdr = ip_hdr(skb);
  2736. addr.sin_addr.s_addr = hdr->saddr;
  2737. rcu_read_lock();
  2738. hsp = smack_host_label(&addr);
  2739. rcu_read_unlock();
  2740. if (hsp == NULL) {
  2741. skp = smk_find_entry(sp);
  2742. rc = netlbl_req_setattr(req, &skp->smk_netlabel);
  2743. } else
  2744. netlbl_req_delattr(req);
  2745. return rc;
  2746. }
  2747. /**
  2748. * smack_inet_csk_clone - Copy the connection information to the new socket
  2749. * @sk: the new socket
  2750. * @req: the connection's request_sock
  2751. *
  2752. * Transfer the connection's peer label to the newly created socket.
  2753. */
  2754. static void smack_inet_csk_clone(struct sock *sk,
  2755. const struct request_sock *req)
  2756. {
  2757. struct socket_smack *ssp = sk->sk_security;
  2758. if (req->peer_secid != 0)
  2759. ssp->smk_packet = smack_from_secid(req->peer_secid);
  2760. else
  2761. ssp->smk_packet = NULL;
  2762. }
  2763. /*
  2764. * Key management security hooks
  2765. *
  2766. * Casey has not tested key support very heavily.
  2767. * The permission check is most likely too restrictive.
  2768. * If you care about keys please have a look.
  2769. */
  2770. #ifdef CONFIG_KEYS
  2771. /**
  2772. * smack_key_alloc - Set the key security blob
  2773. * @key: object
  2774. * @cred: the credentials to use
  2775. * @flags: unused
  2776. *
  2777. * No allocation required
  2778. *
  2779. * Returns 0
  2780. */
  2781. static int smack_key_alloc(struct key *key, const struct cred *cred,
  2782. unsigned long flags)
  2783. {
  2784. key->security = smk_of_task(cred->security);
  2785. return 0;
  2786. }
  2787. /**
  2788. * smack_key_free - Clear the key security blob
  2789. * @key: the object
  2790. *
  2791. * Clear the blob pointer
  2792. */
  2793. static void smack_key_free(struct key *key)
  2794. {
  2795. key->security = NULL;
  2796. }
  2797. /*
  2798. * smack_key_permission - Smack access on a key
  2799. * @key_ref: gets to the object
  2800. * @cred: the credentials to use
  2801. * @perm: unused
  2802. *
  2803. * Return 0 if the task has read and write to the object,
  2804. * an error code otherwise
  2805. */
  2806. static int smack_key_permission(key_ref_t key_ref,
  2807. const struct cred *cred, key_perm_t perm)
  2808. {
  2809. struct key *keyp;
  2810. struct smk_audit_info ad;
  2811. char *tsp = smk_of_task(cred->security);
  2812. keyp = key_ref_to_ptr(key_ref);
  2813. if (keyp == NULL)
  2814. return -EINVAL;
  2815. /*
  2816. * If the key hasn't been initialized give it access so that
  2817. * it may do so.
  2818. */
  2819. if (keyp->security == NULL)
  2820. return 0;
  2821. /*
  2822. * This should not occur
  2823. */
  2824. if (tsp == NULL)
  2825. return -EACCES;
  2826. #ifdef CONFIG_AUDIT
  2827. smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
  2828. ad.a.u.key_struct.key = keyp->serial;
  2829. ad.a.u.key_struct.key_desc = keyp->description;
  2830. #endif
  2831. return smk_access(tsp, keyp->security,
  2832. MAY_READWRITE, &ad);
  2833. }
  2834. #endif /* CONFIG_KEYS */
  2835. /*
  2836. * Smack Audit hooks
  2837. *
  2838. * Audit requires a unique representation of each Smack specific
  2839. * rule. This unique representation is used to distinguish the
  2840. * object to be audited from remaining kernel objects and also
  2841. * works as a glue between the audit hooks.
  2842. *
  2843. * Since repository entries are added but never deleted, we'll use
  2844. * the smack_known label address related to the given audit rule as
  2845. * the needed unique representation. This also better fits the smack
  2846. * model where nearly everything is a label.
  2847. */
  2848. #ifdef CONFIG_AUDIT
  2849. /**
  2850. * smack_audit_rule_init - Initialize a smack audit rule
  2851. * @field: audit rule fields given from user-space (audit.h)
  2852. * @op: required testing operator (=, !=, >, <, ...)
  2853. * @rulestr: smack label to be audited
  2854. * @vrule: pointer to save our own audit rule representation
  2855. *
  2856. * Prepare to audit cases where (@field @op @rulestr) is true.
  2857. * The label to be audited is created if necessay.
  2858. */
  2859. static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2860. {
  2861. char **rule = (char **)vrule;
  2862. *rule = NULL;
  2863. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2864. return -EINVAL;
  2865. if (op != Audit_equal && op != Audit_not_equal)
  2866. return -EINVAL;
  2867. *rule = smk_import(rulestr, 0);
  2868. return 0;
  2869. }
  2870. /**
  2871. * smack_audit_rule_known - Distinguish Smack audit rules
  2872. * @krule: rule of interest, in Audit kernel representation format
  2873. *
  2874. * This is used to filter Smack rules from remaining Audit ones.
  2875. * If it's proved that this rule belongs to us, the
  2876. * audit_rule_match hook will be called to do the final judgement.
  2877. */
  2878. static int smack_audit_rule_known(struct audit_krule *krule)
  2879. {
  2880. struct audit_field *f;
  2881. int i;
  2882. for (i = 0; i < krule->field_count; i++) {
  2883. f = &krule->fields[i];
  2884. if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
  2885. return 1;
  2886. }
  2887. return 0;
  2888. }
  2889. /**
  2890. * smack_audit_rule_match - Audit given object ?
  2891. * @secid: security id for identifying the object to test
  2892. * @field: audit rule flags given from user-space
  2893. * @op: required testing operator
  2894. * @vrule: smack internal rule presentation
  2895. * @actx: audit context associated with the check
  2896. *
  2897. * The core Audit hook. It's used to take the decision of
  2898. * whether to audit or not to audit a given object.
  2899. */
  2900. static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
  2901. struct audit_context *actx)
  2902. {
  2903. char *smack;
  2904. char *rule = vrule;
  2905. if (!rule) {
  2906. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2907. "Smack: missing rule\n");
  2908. return -ENOENT;
  2909. }
  2910. if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
  2911. return 0;
  2912. smack = smack_from_secid(secid);
  2913. /*
  2914. * No need to do string comparisons. If a match occurs,
  2915. * both pointers will point to the same smack_known
  2916. * label.
  2917. */
  2918. if (op == Audit_equal)
  2919. return (rule == smack);
  2920. if (op == Audit_not_equal)
  2921. return (rule != smack);
  2922. return 0;
  2923. }
  2924. /**
  2925. * smack_audit_rule_free - free smack rule representation
  2926. * @vrule: rule to be freed.
  2927. *
  2928. * No memory was allocated.
  2929. */
  2930. static void smack_audit_rule_free(void *vrule)
  2931. {
  2932. /* No-op */
  2933. }
  2934. #endif /* CONFIG_AUDIT */
  2935. /**
  2936. * smack_secid_to_secctx - return the smack label for a secid
  2937. * @secid: incoming integer
  2938. * @secdata: destination
  2939. * @seclen: how long it is
  2940. *
  2941. * Exists for networking code.
  2942. */
  2943. static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  2944. {
  2945. char *sp = smack_from_secid(secid);
  2946. if (secdata)
  2947. *secdata = sp;
  2948. *seclen = strlen(sp);
  2949. return 0;
  2950. }
  2951. /**
  2952. * smack_secctx_to_secid - return the secid for a smack label
  2953. * @secdata: smack label
  2954. * @seclen: how long result is
  2955. * @secid: outgoing integer
  2956. *
  2957. * Exists for audit and networking code.
  2958. */
  2959. static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  2960. {
  2961. *secid = smack_to_secid(secdata);
  2962. return 0;
  2963. }
  2964. /**
  2965. * smack_release_secctx - don't do anything.
  2966. * @secdata: unused
  2967. * @seclen: unused
  2968. *
  2969. * Exists to make sure nothing gets done, and properly
  2970. */
  2971. static void smack_release_secctx(char *secdata, u32 seclen)
  2972. {
  2973. }
  2974. static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  2975. {
  2976. return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
  2977. }
  2978. static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  2979. {
  2980. return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
  2981. }
  2982. static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  2983. {
  2984. int len = 0;
  2985. len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
  2986. if (len < 0)
  2987. return len;
  2988. *ctxlen = len;
  2989. return 0;
  2990. }
  2991. struct security_operations smack_ops = {
  2992. .name = "smack",
  2993. .ptrace_access_check = smack_ptrace_access_check,
  2994. .ptrace_traceme = smack_ptrace_traceme,
  2995. .syslog = smack_syslog,
  2996. .sb_alloc_security = smack_sb_alloc_security,
  2997. .sb_free_security = smack_sb_free_security,
  2998. .sb_copy_data = smack_sb_copy_data,
  2999. .sb_kern_mount = smack_sb_kern_mount,
  3000. .sb_statfs = smack_sb_statfs,
  3001. .sb_mount = smack_sb_mount,
  3002. .sb_umount = smack_sb_umount,
  3003. .bprm_set_creds = smack_bprm_set_creds,
  3004. .bprm_committing_creds = smack_bprm_committing_creds,
  3005. .bprm_secureexec = smack_bprm_secureexec,
  3006. .inode_alloc_security = smack_inode_alloc_security,
  3007. .inode_free_security = smack_inode_free_security,
  3008. .inode_init_security = smack_inode_init_security,
  3009. .inode_link = smack_inode_link,
  3010. .inode_unlink = smack_inode_unlink,
  3011. .inode_rmdir = smack_inode_rmdir,
  3012. .inode_rename = smack_inode_rename,
  3013. .inode_permission = smack_inode_permission,
  3014. .inode_setattr = smack_inode_setattr,
  3015. .inode_getattr = smack_inode_getattr,
  3016. .inode_setxattr = smack_inode_setxattr,
  3017. .inode_post_setxattr = smack_inode_post_setxattr,
  3018. .inode_getxattr = smack_inode_getxattr,
  3019. .inode_removexattr = smack_inode_removexattr,
  3020. .inode_getsecurity = smack_inode_getsecurity,
  3021. .inode_setsecurity = smack_inode_setsecurity,
  3022. .inode_listsecurity = smack_inode_listsecurity,
  3023. .inode_getsecid = smack_inode_getsecid,
  3024. .file_permission = smack_file_permission,
  3025. .file_alloc_security = smack_file_alloc_security,
  3026. .file_free_security = smack_file_free_security,
  3027. .file_ioctl = smack_file_ioctl,
  3028. .file_lock = smack_file_lock,
  3029. .file_fcntl = smack_file_fcntl,
  3030. .mmap_file = smack_mmap_file,
  3031. .mmap_addr = cap_mmap_addr,
  3032. .file_set_fowner = smack_file_set_fowner,
  3033. .file_send_sigiotask = smack_file_send_sigiotask,
  3034. .file_receive = smack_file_receive,
  3035. .file_open = smack_file_open,
  3036. .cred_alloc_blank = smack_cred_alloc_blank,
  3037. .cred_free = smack_cred_free,
  3038. .cred_prepare = smack_cred_prepare,
  3039. .cred_transfer = smack_cred_transfer,
  3040. .kernel_act_as = smack_kernel_act_as,
  3041. .kernel_create_files_as = smack_kernel_create_files_as,
  3042. .task_setpgid = smack_task_setpgid,
  3043. .task_getpgid = smack_task_getpgid,
  3044. .task_getsid = smack_task_getsid,
  3045. .task_getsecid = smack_task_getsecid,
  3046. .task_setnice = smack_task_setnice,
  3047. .task_setioprio = smack_task_setioprio,
  3048. .task_getioprio = smack_task_getioprio,
  3049. .task_setscheduler = smack_task_setscheduler,
  3050. .task_getscheduler = smack_task_getscheduler,
  3051. .task_movememory = smack_task_movememory,
  3052. .task_kill = smack_task_kill,
  3053. .task_wait = smack_task_wait,
  3054. .task_to_inode = smack_task_to_inode,
  3055. .ipc_permission = smack_ipc_permission,
  3056. .ipc_getsecid = smack_ipc_getsecid,
  3057. .msg_msg_alloc_security = smack_msg_msg_alloc_security,
  3058. .msg_msg_free_security = smack_msg_msg_free_security,
  3059. .msg_queue_alloc_security = smack_msg_queue_alloc_security,
  3060. .msg_queue_free_security = smack_msg_queue_free_security,
  3061. .msg_queue_associate = smack_msg_queue_associate,
  3062. .msg_queue_msgctl = smack_msg_queue_msgctl,
  3063. .msg_queue_msgsnd = smack_msg_queue_msgsnd,
  3064. .msg_queue_msgrcv = smack_msg_queue_msgrcv,
  3065. .shm_alloc_security = smack_shm_alloc_security,
  3066. .shm_free_security = smack_shm_free_security,
  3067. .shm_associate = smack_shm_associate,
  3068. .shm_shmctl = smack_shm_shmctl,
  3069. .shm_shmat = smack_shm_shmat,
  3070. .sem_alloc_security = smack_sem_alloc_security,
  3071. .sem_free_security = smack_sem_free_security,
  3072. .sem_associate = smack_sem_associate,
  3073. .sem_semctl = smack_sem_semctl,
  3074. .sem_semop = smack_sem_semop,
  3075. .d_instantiate = smack_d_instantiate,
  3076. .getprocattr = smack_getprocattr,
  3077. .setprocattr = smack_setprocattr,
  3078. .unix_stream_connect = smack_unix_stream_connect,
  3079. .unix_may_send = smack_unix_may_send,
  3080. .socket_post_create = smack_socket_post_create,
  3081. .socket_connect = smack_socket_connect,
  3082. .socket_sendmsg = smack_socket_sendmsg,
  3083. .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
  3084. .socket_getpeersec_stream = smack_socket_getpeersec_stream,
  3085. .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
  3086. .sk_alloc_security = smack_sk_alloc_security,
  3087. .sk_free_security = smack_sk_free_security,
  3088. .sock_graft = smack_sock_graft,
  3089. .inet_conn_request = smack_inet_conn_request,
  3090. .inet_csk_clone = smack_inet_csk_clone,
  3091. /* key management security hooks */
  3092. #ifdef CONFIG_KEYS
  3093. .key_alloc = smack_key_alloc,
  3094. .key_free = smack_key_free,
  3095. .key_permission = smack_key_permission,
  3096. #endif /* CONFIG_KEYS */
  3097. /* Audit hooks */
  3098. #ifdef CONFIG_AUDIT
  3099. .audit_rule_init = smack_audit_rule_init,
  3100. .audit_rule_known = smack_audit_rule_known,
  3101. .audit_rule_match = smack_audit_rule_match,
  3102. .audit_rule_free = smack_audit_rule_free,
  3103. #endif /* CONFIG_AUDIT */
  3104. .secid_to_secctx = smack_secid_to_secctx,
  3105. .secctx_to_secid = smack_secctx_to_secid,
  3106. .release_secctx = smack_release_secctx,
  3107. .inode_notifysecctx = smack_inode_notifysecctx,
  3108. .inode_setsecctx = smack_inode_setsecctx,
  3109. .inode_getsecctx = smack_inode_getsecctx,
  3110. };
  3111. static __init void init_smack_known_list(void)
  3112. {
  3113. /*
  3114. * Initialize rule list locks
  3115. */
  3116. mutex_init(&smack_known_huh.smk_rules_lock);
  3117. mutex_init(&smack_known_hat.smk_rules_lock);
  3118. mutex_init(&smack_known_floor.smk_rules_lock);
  3119. mutex_init(&smack_known_star.smk_rules_lock);
  3120. mutex_init(&smack_known_invalid.smk_rules_lock);
  3121. mutex_init(&smack_known_web.smk_rules_lock);
  3122. /*
  3123. * Initialize rule lists
  3124. */
  3125. INIT_LIST_HEAD(&smack_known_huh.smk_rules);
  3126. INIT_LIST_HEAD(&smack_known_hat.smk_rules);
  3127. INIT_LIST_HEAD(&smack_known_star.smk_rules);
  3128. INIT_LIST_HEAD(&smack_known_floor.smk_rules);
  3129. INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
  3130. INIT_LIST_HEAD(&smack_known_web.smk_rules);
  3131. /*
  3132. * Create the known labels list
  3133. */
  3134. list_add(&smack_known_huh.list, &smack_known_list);
  3135. list_add(&smack_known_hat.list, &smack_known_list);
  3136. list_add(&smack_known_star.list, &smack_known_list);
  3137. list_add(&smack_known_floor.list, &smack_known_list);
  3138. list_add(&smack_known_invalid.list, &smack_known_list);
  3139. list_add(&smack_known_web.list, &smack_known_list);
  3140. }
  3141. /**
  3142. * smack_init - initialize the smack system
  3143. *
  3144. * Returns 0
  3145. */
  3146. static __init int smack_init(void)
  3147. {
  3148. struct cred *cred;
  3149. struct task_smack *tsp;
  3150. if (!security_module_enable(&smack_ops))
  3151. return 0;
  3152. tsp = new_task_smack(smack_known_floor.smk_known,
  3153. smack_known_floor.smk_known, GFP_KERNEL);
  3154. if (tsp == NULL)
  3155. return -ENOMEM;
  3156. printk(KERN_INFO "Smack: Initializing.\n");
  3157. /*
  3158. * Set the security state for the initial task.
  3159. */
  3160. cred = (struct cred *) current->cred;
  3161. cred->security = tsp;
  3162. /* initialize the smack_known_list */
  3163. init_smack_known_list();
  3164. /*
  3165. * Register with LSM
  3166. */
  3167. if (register_security(&smack_ops))
  3168. panic("smack: Unable to register with kernel.\n");
  3169. return 0;
  3170. }
  3171. /*
  3172. * Smack requires early initialization in order to label
  3173. * all processes and objects when they are created.
  3174. */
  3175. security_initcall(smack_init);