smack_lsm.c 61 KB

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