smack_lsm.c 75 KB

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