smack_lsm.c 76 KB

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