smack_lsm.c 76 KB

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