smack_lsm.c 76 KB

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