smack_lsm.c 76 KB

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