smack_lsm.c 56 KB

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