smack_lsm.c 84 KB

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