smack_lsm.c 58 KB

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