smack_lsm.c 62 KB

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