selinuxfs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /* Updated: Karl MacMillan <kmacmillan@tresys.com>
  2. *
  3. * Added conditional policy language extensions
  4. *
  5. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  6. * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/fs.h>
  16. #include <linux/mutex.h>
  17. #include <linux/init.h>
  18. #include <linux/string.h>
  19. #include <linux/security.h>
  20. #include <linux/major.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/percpu.h>
  23. #include <linux/audit.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/semaphore.h>
  26. /* selinuxfs pseudo filesystem for exporting the security policy API.
  27. Based on the proc code and the fs/nfsd/nfsctl.c code. */
  28. #include "flask.h"
  29. #include "avc.h"
  30. #include "avc_ss.h"
  31. #include "security.h"
  32. #include "objsec.h"
  33. #include "conditional.h"
  34. unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
  35. #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
  36. #define SELINUX_COMPAT_NET_VALUE 0
  37. #else
  38. #define SELINUX_COMPAT_NET_VALUE 1
  39. #endif
  40. int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
  41. static int __init checkreqprot_setup(char *str)
  42. {
  43. selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
  44. return 1;
  45. }
  46. __setup("checkreqprot=", checkreqprot_setup);
  47. static int __init selinux_compat_net_setup(char *str)
  48. {
  49. selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
  50. return 1;
  51. }
  52. __setup("selinux_compat_net=", selinux_compat_net_setup);
  53. static DEFINE_MUTEX(sel_mutex);
  54. /* global data for booleans */
  55. static struct dentry *bool_dir = NULL;
  56. static int bool_num = 0;
  57. static int *bool_pending_values = NULL;
  58. /* global data for classes */
  59. static struct dentry *class_dir = NULL;
  60. static unsigned long last_class_ino;
  61. extern void selnl_notify_setenforce(int val);
  62. /* Check whether a task is allowed to use a security operation. */
  63. static int task_has_security(struct task_struct *tsk,
  64. u32 perms)
  65. {
  66. struct task_security_struct *tsec;
  67. tsec = tsk->security;
  68. if (!tsec)
  69. return -EACCES;
  70. return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
  71. SECCLASS_SECURITY, perms, NULL);
  72. }
  73. enum sel_inos {
  74. SEL_ROOT_INO = 2,
  75. SEL_LOAD, /* load policy */
  76. SEL_ENFORCE, /* get or set enforcing status */
  77. SEL_CONTEXT, /* validate context */
  78. SEL_ACCESS, /* compute access decision */
  79. SEL_CREATE, /* compute create labeling decision */
  80. SEL_RELABEL, /* compute relabeling decision */
  81. SEL_USER, /* compute reachable user contexts */
  82. SEL_POLICYVERS, /* return policy version for this kernel */
  83. SEL_COMMIT_BOOLS, /* commit new boolean values */
  84. SEL_MLS, /* return if MLS policy is enabled */
  85. SEL_DISABLE, /* disable SELinux until next reboot */
  86. SEL_MEMBER, /* compute polyinstantiation membership decision */
  87. SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
  88. SEL_COMPAT_NET, /* whether to use old compat network packet controls */
  89. SEL_INO_NEXT, /* The next inode number to use */
  90. };
  91. static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
  92. #define SEL_INITCON_INO_OFFSET 0x01000000
  93. #define SEL_BOOL_INO_OFFSET 0x02000000
  94. #define SEL_CLASS_INO_OFFSET 0x04000000
  95. #define SEL_INO_MASK 0x00ffffff
  96. #define TMPBUFLEN 12
  97. static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. char tmpbuf[TMPBUFLEN];
  101. ssize_t length;
  102. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
  103. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  104. }
  105. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  106. static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
  107. size_t count, loff_t *ppos)
  108. {
  109. char *page;
  110. ssize_t length;
  111. int new_value;
  112. if (count >= PAGE_SIZE)
  113. return -ENOMEM;
  114. if (*ppos != 0) {
  115. /* No partial writes. */
  116. return -EINVAL;
  117. }
  118. page = (char*)get_zeroed_page(GFP_KERNEL);
  119. if (!page)
  120. return -ENOMEM;
  121. length = -EFAULT;
  122. if (copy_from_user(page, buf, count))
  123. goto out;
  124. length = -EINVAL;
  125. if (sscanf(page, "%d", &new_value) != 1)
  126. goto out;
  127. if (new_value != selinux_enforcing) {
  128. length = task_has_security(current, SECURITY__SETENFORCE);
  129. if (length)
  130. goto out;
  131. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  132. "enforcing=%d old_enforcing=%d auid=%u", new_value,
  133. selinux_enforcing,
  134. audit_get_loginuid(current->audit_context));
  135. selinux_enforcing = new_value;
  136. if (selinux_enforcing)
  137. avc_ss_reset(0);
  138. selnl_notify_setenforce(selinux_enforcing);
  139. }
  140. length = count;
  141. out:
  142. free_page((unsigned long) page);
  143. return length;
  144. }
  145. #else
  146. #define sel_write_enforce NULL
  147. #endif
  148. static const struct file_operations sel_enforce_ops = {
  149. .read = sel_read_enforce,
  150. .write = sel_write_enforce,
  151. };
  152. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  153. static ssize_t sel_write_disable(struct file * file, const char __user * buf,
  154. size_t count, loff_t *ppos)
  155. {
  156. char *page;
  157. ssize_t length;
  158. int new_value;
  159. extern int selinux_disable(void);
  160. if (count >= PAGE_SIZE)
  161. return -ENOMEM;
  162. if (*ppos != 0) {
  163. /* No partial writes. */
  164. return -EINVAL;
  165. }
  166. page = (char*)get_zeroed_page(GFP_KERNEL);
  167. if (!page)
  168. return -ENOMEM;
  169. length = -EFAULT;
  170. if (copy_from_user(page, buf, count))
  171. goto out;
  172. length = -EINVAL;
  173. if (sscanf(page, "%d", &new_value) != 1)
  174. goto out;
  175. if (new_value) {
  176. length = selinux_disable();
  177. if (length < 0)
  178. goto out;
  179. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  180. "selinux=0 auid=%u",
  181. audit_get_loginuid(current->audit_context));
  182. }
  183. length = count;
  184. out:
  185. free_page((unsigned long) page);
  186. return length;
  187. }
  188. #else
  189. #define sel_write_disable NULL
  190. #endif
  191. static const struct file_operations sel_disable_ops = {
  192. .write = sel_write_disable,
  193. };
  194. static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
  195. size_t count, loff_t *ppos)
  196. {
  197. char tmpbuf[TMPBUFLEN];
  198. ssize_t length;
  199. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
  200. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  201. }
  202. static const struct file_operations sel_policyvers_ops = {
  203. .read = sel_read_policyvers,
  204. };
  205. /* declaration for sel_write_load */
  206. static int sel_make_bools(void);
  207. static int sel_make_classes(void);
  208. /* declaration for sel_make_class_dirs */
  209. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  210. unsigned long *ino);
  211. static ssize_t sel_read_mls(struct file *filp, char __user *buf,
  212. size_t count, loff_t *ppos)
  213. {
  214. char tmpbuf[TMPBUFLEN];
  215. ssize_t length;
  216. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
  217. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  218. }
  219. static const struct file_operations sel_mls_ops = {
  220. .read = sel_read_mls,
  221. };
  222. static ssize_t sel_write_load(struct file * file, const char __user * buf,
  223. size_t count, loff_t *ppos)
  224. {
  225. int ret;
  226. ssize_t length;
  227. void *data = NULL;
  228. mutex_lock(&sel_mutex);
  229. length = task_has_security(current, SECURITY__LOAD_POLICY);
  230. if (length)
  231. goto out;
  232. if (*ppos != 0) {
  233. /* No partial writes. */
  234. length = -EINVAL;
  235. goto out;
  236. }
  237. if ((count > 64 * 1024 * 1024)
  238. || (data = vmalloc(count)) == NULL) {
  239. length = -ENOMEM;
  240. goto out;
  241. }
  242. length = -EFAULT;
  243. if (copy_from_user(data, buf, count) != 0)
  244. goto out;
  245. length = security_load_policy(data, count);
  246. if (length)
  247. goto out;
  248. ret = sel_make_bools();
  249. if (ret) {
  250. length = ret;
  251. goto out1;
  252. }
  253. ret = sel_make_classes();
  254. if (ret)
  255. length = ret;
  256. else
  257. length = count;
  258. out1:
  259. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
  260. "policy loaded auid=%u",
  261. audit_get_loginuid(current->audit_context));
  262. out:
  263. mutex_unlock(&sel_mutex);
  264. vfree(data);
  265. return length;
  266. }
  267. static const struct file_operations sel_load_ops = {
  268. .write = sel_write_load,
  269. };
  270. static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
  271. {
  272. char *canon;
  273. u32 sid, len;
  274. ssize_t length;
  275. length = task_has_security(current, SECURITY__CHECK_CONTEXT);
  276. if (length)
  277. return length;
  278. length = security_context_to_sid(buf, size, &sid);
  279. if (length < 0)
  280. return length;
  281. length = security_sid_to_context(sid, &canon, &len);
  282. if (length < 0)
  283. return length;
  284. if (len > SIMPLE_TRANSACTION_LIMIT) {
  285. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  286. "max\n", __FUNCTION__, len);
  287. length = -ERANGE;
  288. goto out;
  289. }
  290. memcpy(buf, canon, len);
  291. length = len;
  292. out:
  293. kfree(canon);
  294. return length;
  295. }
  296. static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
  297. size_t count, loff_t *ppos)
  298. {
  299. char tmpbuf[TMPBUFLEN];
  300. ssize_t length;
  301. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
  302. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  303. }
  304. static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
  305. size_t count, loff_t *ppos)
  306. {
  307. char *page;
  308. ssize_t length;
  309. unsigned int new_value;
  310. length = task_has_security(current, SECURITY__SETCHECKREQPROT);
  311. if (length)
  312. return length;
  313. if (count >= PAGE_SIZE)
  314. return -ENOMEM;
  315. if (*ppos != 0) {
  316. /* No partial writes. */
  317. return -EINVAL;
  318. }
  319. page = (char*)get_zeroed_page(GFP_KERNEL);
  320. if (!page)
  321. return -ENOMEM;
  322. length = -EFAULT;
  323. if (copy_from_user(page, buf, count))
  324. goto out;
  325. length = -EINVAL;
  326. if (sscanf(page, "%u", &new_value) != 1)
  327. goto out;
  328. selinux_checkreqprot = new_value ? 1 : 0;
  329. length = count;
  330. out:
  331. free_page((unsigned long) page);
  332. return length;
  333. }
  334. static const struct file_operations sel_checkreqprot_ops = {
  335. .read = sel_read_checkreqprot,
  336. .write = sel_write_checkreqprot,
  337. };
  338. static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
  339. size_t count, loff_t *ppos)
  340. {
  341. char tmpbuf[TMPBUFLEN];
  342. ssize_t length;
  343. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
  344. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  345. }
  346. static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
  347. size_t count, loff_t *ppos)
  348. {
  349. char *page;
  350. ssize_t length;
  351. int new_value;
  352. length = task_has_security(current, SECURITY__LOAD_POLICY);
  353. if (length)
  354. return length;
  355. if (count >= PAGE_SIZE)
  356. return -ENOMEM;
  357. if (*ppos != 0) {
  358. /* No partial writes. */
  359. return -EINVAL;
  360. }
  361. page = (char*)get_zeroed_page(GFP_KERNEL);
  362. if (!page)
  363. return -ENOMEM;
  364. length = -EFAULT;
  365. if (copy_from_user(page, buf, count))
  366. goto out;
  367. length = -EINVAL;
  368. if (sscanf(page, "%d", &new_value) != 1)
  369. goto out;
  370. selinux_compat_net = new_value ? 1 : 0;
  371. length = count;
  372. out:
  373. free_page((unsigned long) page);
  374. return length;
  375. }
  376. static const struct file_operations sel_compat_net_ops = {
  377. .read = sel_read_compat_net,
  378. .write = sel_write_compat_net,
  379. };
  380. /*
  381. * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
  382. */
  383. static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
  384. static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
  385. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
  386. static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
  387. static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
  388. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  389. [SEL_ACCESS] = sel_write_access,
  390. [SEL_CREATE] = sel_write_create,
  391. [SEL_RELABEL] = sel_write_relabel,
  392. [SEL_USER] = sel_write_user,
  393. [SEL_MEMBER] = sel_write_member,
  394. [SEL_CONTEXT] = sel_write_context,
  395. };
  396. static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  397. {
  398. ino_t ino = file->f_path.dentry->d_inode->i_ino;
  399. char *data;
  400. ssize_t rv;
  401. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  402. return -EINVAL;
  403. data = simple_transaction_get(file, buf, size);
  404. if (IS_ERR(data))
  405. return PTR_ERR(data);
  406. rv = write_op[ino](file, data, size);
  407. if (rv>0) {
  408. simple_transaction_set(file, rv);
  409. rv = size;
  410. }
  411. return rv;
  412. }
  413. static const struct file_operations transaction_ops = {
  414. .write = selinux_transaction_write,
  415. .read = simple_transaction_read,
  416. .release = simple_transaction_release,
  417. };
  418. /*
  419. * payload - write methods
  420. * If the method has a response, the response should be put in buf,
  421. * and the length returned. Otherwise return 0 or and -error.
  422. */
  423. static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
  424. {
  425. char *scon, *tcon;
  426. u32 ssid, tsid;
  427. u16 tclass;
  428. u32 req;
  429. struct av_decision avd;
  430. ssize_t length;
  431. length = task_has_security(current, SECURITY__COMPUTE_AV);
  432. if (length)
  433. return length;
  434. length = -ENOMEM;
  435. scon = kzalloc(size+1, GFP_KERNEL);
  436. if (!scon)
  437. return length;
  438. tcon = kzalloc(size+1, GFP_KERNEL);
  439. if (!tcon)
  440. goto out;
  441. length = -EINVAL;
  442. if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
  443. goto out2;
  444. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  445. if (length < 0)
  446. goto out2;
  447. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  448. if (length < 0)
  449. goto out2;
  450. length = security_compute_av(ssid, tsid, tclass, req, &avd);
  451. if (length < 0)
  452. goto out2;
  453. length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
  454. "%x %x %x %x %u",
  455. avd.allowed, avd.decided,
  456. avd.auditallow, avd.auditdeny,
  457. avd.seqno);
  458. out2:
  459. kfree(tcon);
  460. out:
  461. kfree(scon);
  462. return length;
  463. }
  464. static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
  465. {
  466. char *scon, *tcon;
  467. u32 ssid, tsid, newsid;
  468. u16 tclass;
  469. ssize_t length;
  470. char *newcon;
  471. u32 len;
  472. length = task_has_security(current, SECURITY__COMPUTE_CREATE);
  473. if (length)
  474. return length;
  475. length = -ENOMEM;
  476. scon = kzalloc(size+1, GFP_KERNEL);
  477. if (!scon)
  478. return length;
  479. tcon = kzalloc(size+1, GFP_KERNEL);
  480. if (!tcon)
  481. goto out;
  482. length = -EINVAL;
  483. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  484. goto out2;
  485. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  486. if (length < 0)
  487. goto out2;
  488. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  489. if (length < 0)
  490. goto out2;
  491. length = security_transition_sid(ssid, tsid, tclass, &newsid);
  492. if (length < 0)
  493. goto out2;
  494. length = security_sid_to_context(newsid, &newcon, &len);
  495. if (length < 0)
  496. goto out2;
  497. if (len > SIMPLE_TRANSACTION_LIMIT) {
  498. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  499. "max\n", __FUNCTION__, len);
  500. length = -ERANGE;
  501. goto out3;
  502. }
  503. memcpy(buf, newcon, len);
  504. length = len;
  505. out3:
  506. kfree(newcon);
  507. out2:
  508. kfree(tcon);
  509. out:
  510. kfree(scon);
  511. return length;
  512. }
  513. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
  514. {
  515. char *scon, *tcon;
  516. u32 ssid, tsid, newsid;
  517. u16 tclass;
  518. ssize_t length;
  519. char *newcon;
  520. u32 len;
  521. length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
  522. if (length)
  523. return length;
  524. length = -ENOMEM;
  525. scon = kzalloc(size+1, GFP_KERNEL);
  526. if (!scon)
  527. return length;
  528. tcon = kzalloc(size+1, GFP_KERNEL);
  529. if (!tcon)
  530. goto out;
  531. length = -EINVAL;
  532. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  533. goto out2;
  534. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  535. if (length < 0)
  536. goto out2;
  537. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  538. if (length < 0)
  539. goto out2;
  540. length = security_change_sid(ssid, tsid, tclass, &newsid);
  541. if (length < 0)
  542. goto out2;
  543. length = security_sid_to_context(newsid, &newcon, &len);
  544. if (length < 0)
  545. goto out2;
  546. if (len > SIMPLE_TRANSACTION_LIMIT) {
  547. length = -ERANGE;
  548. goto out3;
  549. }
  550. memcpy(buf, newcon, len);
  551. length = len;
  552. out3:
  553. kfree(newcon);
  554. out2:
  555. kfree(tcon);
  556. out:
  557. kfree(scon);
  558. return length;
  559. }
  560. static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
  561. {
  562. char *con, *user, *ptr;
  563. u32 sid, *sids;
  564. ssize_t length;
  565. char *newcon;
  566. int i, rc;
  567. u32 len, nsids;
  568. length = task_has_security(current, SECURITY__COMPUTE_USER);
  569. if (length)
  570. return length;
  571. length = -ENOMEM;
  572. con = kzalloc(size+1, GFP_KERNEL);
  573. if (!con)
  574. return length;
  575. user = kzalloc(size+1, GFP_KERNEL);
  576. if (!user)
  577. goto out;
  578. length = -EINVAL;
  579. if (sscanf(buf, "%s %s", con, user) != 2)
  580. goto out2;
  581. length = security_context_to_sid(con, strlen(con)+1, &sid);
  582. if (length < 0)
  583. goto out2;
  584. length = security_get_user_sids(sid, user, &sids, &nsids);
  585. if (length < 0)
  586. goto out2;
  587. length = sprintf(buf, "%u", nsids) + 1;
  588. ptr = buf + length;
  589. for (i = 0; i < nsids; i++) {
  590. rc = security_sid_to_context(sids[i], &newcon, &len);
  591. if (rc) {
  592. length = rc;
  593. goto out3;
  594. }
  595. if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
  596. kfree(newcon);
  597. length = -ERANGE;
  598. goto out3;
  599. }
  600. memcpy(ptr, newcon, len);
  601. kfree(newcon);
  602. ptr += len;
  603. length += len;
  604. }
  605. out3:
  606. kfree(sids);
  607. out2:
  608. kfree(user);
  609. out:
  610. kfree(con);
  611. return length;
  612. }
  613. static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
  614. {
  615. char *scon, *tcon;
  616. u32 ssid, tsid, newsid;
  617. u16 tclass;
  618. ssize_t length;
  619. char *newcon;
  620. u32 len;
  621. length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
  622. if (length)
  623. return length;
  624. length = -ENOMEM;
  625. scon = kzalloc(size+1, GFP_KERNEL);
  626. if (!scon)
  627. return length;
  628. tcon = kzalloc(size+1, GFP_KERNEL);
  629. if (!tcon)
  630. goto out;
  631. length = -EINVAL;
  632. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  633. goto out2;
  634. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  635. if (length < 0)
  636. goto out2;
  637. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  638. if (length < 0)
  639. goto out2;
  640. length = security_member_sid(ssid, tsid, tclass, &newsid);
  641. if (length < 0)
  642. goto out2;
  643. length = security_sid_to_context(newsid, &newcon, &len);
  644. if (length < 0)
  645. goto out2;
  646. if (len > SIMPLE_TRANSACTION_LIMIT) {
  647. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  648. "max\n", __FUNCTION__, len);
  649. length = -ERANGE;
  650. goto out3;
  651. }
  652. memcpy(buf, newcon, len);
  653. length = len;
  654. out3:
  655. kfree(newcon);
  656. out2:
  657. kfree(tcon);
  658. out:
  659. kfree(scon);
  660. return length;
  661. }
  662. static struct inode *sel_make_inode(struct super_block *sb, int mode)
  663. {
  664. struct inode *ret = new_inode(sb);
  665. if (ret) {
  666. ret->i_mode = mode;
  667. ret->i_uid = ret->i_gid = 0;
  668. ret->i_blocks = 0;
  669. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  670. }
  671. return ret;
  672. }
  673. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  674. size_t count, loff_t *ppos)
  675. {
  676. char *page = NULL;
  677. ssize_t length;
  678. ssize_t ret;
  679. int cur_enforcing;
  680. struct inode *inode;
  681. mutex_lock(&sel_mutex);
  682. ret = -EFAULT;
  683. /* check to see if this file has been deleted */
  684. if (!filep->f_op)
  685. goto out;
  686. if (count > PAGE_SIZE) {
  687. ret = -EINVAL;
  688. goto out;
  689. }
  690. if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
  691. ret = -ENOMEM;
  692. goto out;
  693. }
  694. inode = filep->f_path.dentry->d_inode;
  695. cur_enforcing = security_get_bool_value(inode->i_ino&SEL_INO_MASK);
  696. if (cur_enforcing < 0) {
  697. ret = cur_enforcing;
  698. goto out;
  699. }
  700. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  701. bool_pending_values[inode->i_ino&SEL_INO_MASK]);
  702. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  703. out:
  704. mutex_unlock(&sel_mutex);
  705. if (page)
  706. free_page((unsigned long)page);
  707. return ret;
  708. }
  709. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  710. size_t count, loff_t *ppos)
  711. {
  712. char *page = NULL;
  713. ssize_t length = -EFAULT;
  714. int new_value;
  715. struct inode *inode;
  716. mutex_lock(&sel_mutex);
  717. length = task_has_security(current, SECURITY__SETBOOL);
  718. if (length)
  719. goto out;
  720. /* check to see if this file has been deleted */
  721. if (!filep->f_op)
  722. goto out;
  723. if (count >= PAGE_SIZE) {
  724. length = -ENOMEM;
  725. goto out;
  726. }
  727. if (*ppos != 0) {
  728. /* No partial writes. */
  729. goto out;
  730. }
  731. page = (char*)get_zeroed_page(GFP_KERNEL);
  732. if (!page) {
  733. length = -ENOMEM;
  734. goto out;
  735. }
  736. if (copy_from_user(page, buf, count))
  737. goto out;
  738. length = -EINVAL;
  739. if (sscanf(page, "%d", &new_value) != 1)
  740. goto out;
  741. if (new_value)
  742. new_value = 1;
  743. inode = filep->f_path.dentry->d_inode;
  744. bool_pending_values[inode->i_ino&SEL_INO_MASK] = new_value;
  745. length = count;
  746. out:
  747. mutex_unlock(&sel_mutex);
  748. if (page)
  749. free_page((unsigned long) page);
  750. return length;
  751. }
  752. static const struct file_operations sel_bool_ops = {
  753. .read = sel_read_bool,
  754. .write = sel_write_bool,
  755. };
  756. static ssize_t sel_commit_bools_write(struct file *filep,
  757. const char __user *buf,
  758. size_t count, loff_t *ppos)
  759. {
  760. char *page = NULL;
  761. ssize_t length = -EFAULT;
  762. int new_value;
  763. mutex_lock(&sel_mutex);
  764. length = task_has_security(current, SECURITY__SETBOOL);
  765. if (length)
  766. goto out;
  767. /* check to see if this file has been deleted */
  768. if (!filep->f_op)
  769. goto out;
  770. if (count >= PAGE_SIZE) {
  771. length = -ENOMEM;
  772. goto out;
  773. }
  774. if (*ppos != 0) {
  775. /* No partial writes. */
  776. goto out;
  777. }
  778. page = (char*)get_zeroed_page(GFP_KERNEL);
  779. if (!page) {
  780. length = -ENOMEM;
  781. goto out;
  782. }
  783. if (copy_from_user(page, buf, count))
  784. goto out;
  785. length = -EINVAL;
  786. if (sscanf(page, "%d", &new_value) != 1)
  787. goto out;
  788. if (new_value && bool_pending_values) {
  789. security_set_bools(bool_num, bool_pending_values);
  790. }
  791. length = count;
  792. out:
  793. mutex_unlock(&sel_mutex);
  794. if (page)
  795. free_page((unsigned long) page);
  796. return length;
  797. }
  798. static const struct file_operations sel_commit_bools_ops = {
  799. .write = sel_commit_bools_write,
  800. };
  801. /* partial revoke() from fs/proc/generic.c proc_kill_inodes */
  802. static void sel_remove_entries(struct dentry *de)
  803. {
  804. struct list_head *p, *node;
  805. struct super_block *sb = de->d_sb;
  806. spin_lock(&dcache_lock);
  807. node = de->d_subdirs.next;
  808. while (node != &de->d_subdirs) {
  809. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  810. list_del_init(node);
  811. if (d->d_inode) {
  812. d = dget_locked(d);
  813. spin_unlock(&dcache_lock);
  814. d_delete(d);
  815. simple_unlink(de->d_inode, d);
  816. dput(d);
  817. spin_lock(&dcache_lock);
  818. }
  819. node = de->d_subdirs.next;
  820. }
  821. spin_unlock(&dcache_lock);
  822. file_list_lock();
  823. list_for_each(p, &sb->s_files) {
  824. struct file * filp = list_entry(p, struct file, f_u.fu_list);
  825. struct dentry * dentry = filp->f_path.dentry;
  826. if (dentry->d_parent != de) {
  827. continue;
  828. }
  829. filp->f_op = NULL;
  830. }
  831. file_list_unlock();
  832. }
  833. #define BOOL_DIR_NAME "booleans"
  834. static int sel_make_bools(void)
  835. {
  836. int i, ret = 0;
  837. ssize_t len;
  838. struct dentry *dentry = NULL;
  839. struct dentry *dir = bool_dir;
  840. struct inode *inode = NULL;
  841. struct inode_security_struct *isec;
  842. char **names = NULL, *page;
  843. int num;
  844. int *values = NULL;
  845. u32 sid;
  846. /* remove any existing files */
  847. kfree(bool_pending_values);
  848. bool_pending_values = NULL;
  849. sel_remove_entries(dir);
  850. if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
  851. return -ENOMEM;
  852. ret = security_get_bools(&num, &names, &values);
  853. if (ret != 0)
  854. goto out;
  855. for (i = 0; i < num; i++) {
  856. dentry = d_alloc_name(dir, names[i]);
  857. if (!dentry) {
  858. ret = -ENOMEM;
  859. goto err;
  860. }
  861. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  862. if (!inode) {
  863. ret = -ENOMEM;
  864. goto err;
  865. }
  866. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  867. if (len < 0) {
  868. ret = -EINVAL;
  869. goto err;
  870. } else if (len >= PAGE_SIZE) {
  871. ret = -ENAMETOOLONG;
  872. goto err;
  873. }
  874. isec = (struct inode_security_struct*)inode->i_security;
  875. if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
  876. goto err;
  877. isec->sid = sid;
  878. isec->initialized = 1;
  879. inode->i_fop = &sel_bool_ops;
  880. inode->i_ino = i|SEL_BOOL_INO_OFFSET;
  881. d_add(dentry, inode);
  882. }
  883. bool_num = num;
  884. bool_pending_values = values;
  885. out:
  886. free_page((unsigned long)page);
  887. if (names) {
  888. for (i = 0; i < num; i++)
  889. kfree(names[i]);
  890. kfree(names);
  891. }
  892. return ret;
  893. err:
  894. kfree(values);
  895. sel_remove_entries(dir);
  896. ret = -ENOMEM;
  897. goto out;
  898. }
  899. #define NULL_FILE_NAME "null"
  900. struct dentry *selinux_null = NULL;
  901. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  902. size_t count, loff_t *ppos)
  903. {
  904. char tmpbuf[TMPBUFLEN];
  905. ssize_t length;
  906. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  907. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  908. }
  909. static ssize_t sel_write_avc_cache_threshold(struct file * file,
  910. const char __user * buf,
  911. size_t count, loff_t *ppos)
  912. {
  913. char *page;
  914. ssize_t ret;
  915. int new_value;
  916. if (count >= PAGE_SIZE) {
  917. ret = -ENOMEM;
  918. goto out;
  919. }
  920. if (*ppos != 0) {
  921. /* No partial writes. */
  922. ret = -EINVAL;
  923. goto out;
  924. }
  925. page = (char*)get_zeroed_page(GFP_KERNEL);
  926. if (!page) {
  927. ret = -ENOMEM;
  928. goto out;
  929. }
  930. if (copy_from_user(page, buf, count)) {
  931. ret = -EFAULT;
  932. goto out_free;
  933. }
  934. if (sscanf(page, "%u", &new_value) != 1) {
  935. ret = -EINVAL;
  936. goto out;
  937. }
  938. if (new_value != avc_cache_threshold) {
  939. ret = task_has_security(current, SECURITY__SETSECPARAM);
  940. if (ret)
  941. goto out_free;
  942. avc_cache_threshold = new_value;
  943. }
  944. ret = count;
  945. out_free:
  946. free_page((unsigned long)page);
  947. out:
  948. return ret;
  949. }
  950. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  951. size_t count, loff_t *ppos)
  952. {
  953. char *page;
  954. ssize_t ret = 0;
  955. page = (char *)__get_free_page(GFP_KERNEL);
  956. if (!page) {
  957. ret = -ENOMEM;
  958. goto out;
  959. }
  960. ret = avc_get_hash_stats(page);
  961. if (ret >= 0)
  962. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  963. free_page((unsigned long)page);
  964. out:
  965. return ret;
  966. }
  967. static const struct file_operations sel_avc_cache_threshold_ops = {
  968. .read = sel_read_avc_cache_threshold,
  969. .write = sel_write_avc_cache_threshold,
  970. };
  971. static const struct file_operations sel_avc_hash_stats_ops = {
  972. .read = sel_read_avc_hash_stats,
  973. };
  974. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  975. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  976. {
  977. int cpu;
  978. for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
  979. if (!cpu_possible(cpu))
  980. continue;
  981. *idx = cpu + 1;
  982. return &per_cpu(avc_cache_stats, cpu);
  983. }
  984. return NULL;
  985. }
  986. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  987. {
  988. loff_t n = *pos - 1;
  989. if (*pos == 0)
  990. return SEQ_START_TOKEN;
  991. return sel_avc_get_stat_idx(&n);
  992. }
  993. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  994. {
  995. return sel_avc_get_stat_idx(pos);
  996. }
  997. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  998. {
  999. struct avc_cache_stats *st = v;
  1000. if (v == SEQ_START_TOKEN)
  1001. seq_printf(seq, "lookups hits misses allocations reclaims "
  1002. "frees\n");
  1003. else
  1004. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  1005. st->hits, st->misses, st->allocations,
  1006. st->reclaims, st->frees);
  1007. return 0;
  1008. }
  1009. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  1010. { }
  1011. static struct seq_operations sel_avc_cache_stats_seq_ops = {
  1012. .start = sel_avc_stats_seq_start,
  1013. .next = sel_avc_stats_seq_next,
  1014. .show = sel_avc_stats_seq_show,
  1015. .stop = sel_avc_stats_seq_stop,
  1016. };
  1017. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1018. {
  1019. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1020. }
  1021. static const struct file_operations sel_avc_cache_stats_ops = {
  1022. .open = sel_open_avc_cache_stats,
  1023. .read = seq_read,
  1024. .llseek = seq_lseek,
  1025. .release = seq_release,
  1026. };
  1027. #endif
  1028. static int sel_make_avc_files(struct dentry *dir)
  1029. {
  1030. int i, ret = 0;
  1031. static struct tree_descr files[] = {
  1032. { "cache_threshold",
  1033. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1034. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1035. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1036. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1037. #endif
  1038. };
  1039. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1040. struct inode *inode;
  1041. struct dentry *dentry;
  1042. dentry = d_alloc_name(dir, files[i].name);
  1043. if (!dentry) {
  1044. ret = -ENOMEM;
  1045. goto out;
  1046. }
  1047. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1048. if (!inode) {
  1049. ret = -ENOMEM;
  1050. goto out;
  1051. }
  1052. inode->i_fop = files[i].ops;
  1053. inode->i_ino = ++sel_last_ino;
  1054. d_add(dentry, inode);
  1055. }
  1056. out:
  1057. return ret;
  1058. }
  1059. static ssize_t sel_read_initcon(struct file * file, char __user *buf,
  1060. size_t count, loff_t *ppos)
  1061. {
  1062. struct inode *inode;
  1063. char *con;
  1064. u32 sid, len;
  1065. ssize_t ret;
  1066. inode = file->f_path.dentry->d_inode;
  1067. sid = inode->i_ino&SEL_INO_MASK;
  1068. ret = security_sid_to_context(sid, &con, &len);
  1069. if (ret < 0)
  1070. return ret;
  1071. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1072. kfree(con);
  1073. return ret;
  1074. }
  1075. static const struct file_operations sel_initcon_ops = {
  1076. .read = sel_read_initcon,
  1077. };
  1078. static int sel_make_initcon_files(struct dentry *dir)
  1079. {
  1080. int i, ret = 0;
  1081. for (i = 1; i <= SECINITSID_NUM; i++) {
  1082. struct inode *inode;
  1083. struct dentry *dentry;
  1084. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1085. if (!dentry) {
  1086. ret = -ENOMEM;
  1087. goto out;
  1088. }
  1089. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1090. if (!inode) {
  1091. ret = -ENOMEM;
  1092. goto out;
  1093. }
  1094. inode->i_fop = &sel_initcon_ops;
  1095. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1096. d_add(dentry, inode);
  1097. }
  1098. out:
  1099. return ret;
  1100. }
  1101. static inline unsigned int sel_div(unsigned long a, unsigned long b)
  1102. {
  1103. return a / b - (a % b < 0);
  1104. }
  1105. static inline unsigned long sel_class_to_ino(u16 class)
  1106. {
  1107. return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
  1108. }
  1109. static inline u16 sel_ino_to_class(unsigned long ino)
  1110. {
  1111. return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
  1112. }
  1113. static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
  1114. {
  1115. return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
  1116. }
  1117. static inline u32 sel_ino_to_perm(unsigned long ino)
  1118. {
  1119. return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
  1120. }
  1121. static ssize_t sel_read_class(struct file * file, char __user *buf,
  1122. size_t count, loff_t *ppos)
  1123. {
  1124. ssize_t rc, len;
  1125. char *page;
  1126. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1127. page = (char *)__get_free_page(GFP_KERNEL);
  1128. if (!page) {
  1129. rc = -ENOMEM;
  1130. goto out;
  1131. }
  1132. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
  1133. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1134. free_page((unsigned long)page);
  1135. out:
  1136. return rc;
  1137. }
  1138. static const struct file_operations sel_class_ops = {
  1139. .read = sel_read_class,
  1140. };
  1141. static ssize_t sel_read_perm(struct file * file, char __user *buf,
  1142. size_t count, loff_t *ppos)
  1143. {
  1144. ssize_t rc, len;
  1145. char *page;
  1146. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1147. page = (char *)__get_free_page(GFP_KERNEL);
  1148. if (!page) {
  1149. rc = -ENOMEM;
  1150. goto out;
  1151. }
  1152. len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
  1153. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1154. free_page((unsigned long)page);
  1155. out:
  1156. return rc;
  1157. }
  1158. static const struct file_operations sel_perm_ops = {
  1159. .read = sel_read_perm,
  1160. };
  1161. static int sel_make_perm_files(char *objclass, int classvalue,
  1162. struct dentry *dir)
  1163. {
  1164. int i, rc = 0, nperms;
  1165. char **perms;
  1166. rc = security_get_permissions(objclass, &perms, &nperms);
  1167. if (rc)
  1168. goto out;
  1169. for (i = 0; i < nperms; i++) {
  1170. struct inode *inode;
  1171. struct dentry *dentry;
  1172. dentry = d_alloc_name(dir, perms[i]);
  1173. if (!dentry) {
  1174. rc = -ENOMEM;
  1175. goto out1;
  1176. }
  1177. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1178. if (!inode) {
  1179. rc = -ENOMEM;
  1180. goto out1;
  1181. }
  1182. inode->i_fop = &sel_perm_ops;
  1183. /* i+1 since perm values are 1-indexed */
  1184. inode->i_ino = sel_perm_to_ino(classvalue, i+1);
  1185. d_add(dentry, inode);
  1186. }
  1187. out1:
  1188. for (i = 0; i < nperms; i++)
  1189. kfree(perms[i]);
  1190. kfree(perms);
  1191. out:
  1192. return rc;
  1193. }
  1194. static int sel_make_class_dir_entries(char *classname, int index,
  1195. struct dentry *dir)
  1196. {
  1197. struct dentry *dentry = NULL;
  1198. struct inode *inode = NULL;
  1199. int rc;
  1200. dentry = d_alloc_name(dir, "index");
  1201. if (!dentry) {
  1202. rc = -ENOMEM;
  1203. goto out;
  1204. }
  1205. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1206. if (!inode) {
  1207. rc = -ENOMEM;
  1208. goto out;
  1209. }
  1210. inode->i_fop = &sel_class_ops;
  1211. inode->i_ino = sel_class_to_ino(index);
  1212. d_add(dentry, inode);
  1213. dentry = d_alloc_name(dir, "perms");
  1214. if (!dentry) {
  1215. rc = -ENOMEM;
  1216. goto out;
  1217. }
  1218. rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
  1219. if (rc)
  1220. goto out;
  1221. rc = sel_make_perm_files(classname, index, dentry);
  1222. out:
  1223. return rc;
  1224. }
  1225. static void sel_remove_classes(void)
  1226. {
  1227. struct list_head *class_node;
  1228. list_for_each(class_node, &class_dir->d_subdirs) {
  1229. struct dentry *class_subdir = list_entry(class_node,
  1230. struct dentry, d_u.d_child);
  1231. struct list_head *class_subdir_node;
  1232. list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
  1233. struct dentry *d = list_entry(class_subdir_node,
  1234. struct dentry, d_u.d_child);
  1235. if (d->d_inode)
  1236. if (d->d_inode->i_mode & S_IFDIR)
  1237. sel_remove_entries(d);
  1238. }
  1239. sel_remove_entries(class_subdir);
  1240. }
  1241. sel_remove_entries(class_dir);
  1242. }
  1243. static int sel_make_classes(void)
  1244. {
  1245. int rc = 0, nclasses, i;
  1246. char **classes;
  1247. /* delete any existing entries */
  1248. sel_remove_classes();
  1249. rc = security_get_classes(&classes, &nclasses);
  1250. if (rc < 0)
  1251. goto out;
  1252. /* +2 since classes are 1-indexed */
  1253. last_class_ino = sel_class_to_ino(nclasses+2);
  1254. for (i = 0; i < nclasses; i++) {
  1255. struct dentry *class_name_dir;
  1256. class_name_dir = d_alloc_name(class_dir, classes[i]);
  1257. if (!class_name_dir) {
  1258. rc = -ENOMEM;
  1259. goto out1;
  1260. }
  1261. rc = sel_make_dir(class_dir->d_inode, class_name_dir,
  1262. &last_class_ino);
  1263. if (rc)
  1264. goto out1;
  1265. /* i+1 since class values are 1-indexed */
  1266. rc = sel_make_class_dir_entries(classes[i], i+1,
  1267. class_name_dir);
  1268. if (rc)
  1269. goto out1;
  1270. }
  1271. out1:
  1272. for (i = 0; i < nclasses; i++)
  1273. kfree(classes[i]);
  1274. kfree(classes);
  1275. out:
  1276. return rc;
  1277. }
  1278. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  1279. unsigned long *ino)
  1280. {
  1281. int ret = 0;
  1282. struct inode *inode;
  1283. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1284. if (!inode) {
  1285. ret = -ENOMEM;
  1286. goto out;
  1287. }
  1288. inode->i_op = &simple_dir_inode_operations;
  1289. inode->i_fop = &simple_dir_operations;
  1290. inode->i_ino = ++(*ino);
  1291. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1292. inc_nlink(inode);
  1293. d_add(dentry, inode);
  1294. /* bump link count on parent directory, too */
  1295. inc_nlink(dir);
  1296. out:
  1297. return ret;
  1298. }
  1299. static int sel_fill_super(struct super_block * sb, void * data, int silent)
  1300. {
  1301. int ret;
  1302. struct dentry *dentry;
  1303. struct inode *inode, *root_inode;
  1304. struct inode_security_struct *isec;
  1305. static struct tree_descr selinux_files[] = {
  1306. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1307. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1308. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1309. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1310. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1311. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1312. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1313. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1314. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1315. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1316. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1317. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1318. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1319. [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
  1320. /* last one */ {""}
  1321. };
  1322. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1323. if (ret)
  1324. goto err;
  1325. root_inode = sb->s_root->d_inode;
  1326. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1327. if (!dentry) {
  1328. ret = -ENOMEM;
  1329. goto err;
  1330. }
  1331. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1332. if (ret)
  1333. goto err;
  1334. bool_dir = dentry;
  1335. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1336. if (!dentry) {
  1337. ret = -ENOMEM;
  1338. goto err;
  1339. }
  1340. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1341. if (!inode) {
  1342. ret = -ENOMEM;
  1343. goto err;
  1344. }
  1345. inode->i_ino = ++sel_last_ino;
  1346. isec = (struct inode_security_struct*)inode->i_security;
  1347. isec->sid = SECINITSID_DEVNULL;
  1348. isec->sclass = SECCLASS_CHR_FILE;
  1349. isec->initialized = 1;
  1350. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1351. d_add(dentry, inode);
  1352. selinux_null = dentry;
  1353. dentry = d_alloc_name(sb->s_root, "avc");
  1354. if (!dentry) {
  1355. ret = -ENOMEM;
  1356. goto err;
  1357. }
  1358. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1359. if (ret)
  1360. goto err;
  1361. ret = sel_make_avc_files(dentry);
  1362. if (ret)
  1363. goto err;
  1364. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1365. if (!dentry) {
  1366. ret = -ENOMEM;
  1367. goto err;
  1368. }
  1369. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1370. if (ret)
  1371. goto err;
  1372. ret = sel_make_initcon_files(dentry);
  1373. if (ret)
  1374. goto err;
  1375. dentry = d_alloc_name(sb->s_root, "class");
  1376. if (!dentry) {
  1377. ret = -ENOMEM;
  1378. goto err;
  1379. }
  1380. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1381. if (ret)
  1382. goto err;
  1383. class_dir = dentry;
  1384. out:
  1385. return ret;
  1386. err:
  1387. printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
  1388. goto out;
  1389. }
  1390. static int sel_get_sb(struct file_system_type *fs_type,
  1391. int flags, const char *dev_name, void *data,
  1392. struct vfsmount *mnt)
  1393. {
  1394. return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
  1395. }
  1396. static struct file_system_type sel_fs_type = {
  1397. .name = "selinuxfs",
  1398. .get_sb = sel_get_sb,
  1399. .kill_sb = kill_litter_super,
  1400. };
  1401. struct vfsmount *selinuxfs_mount;
  1402. static int __init init_sel_fs(void)
  1403. {
  1404. int err;
  1405. if (!selinux_enabled)
  1406. return 0;
  1407. err = register_filesystem(&sel_fs_type);
  1408. if (!err) {
  1409. selinuxfs_mount = kern_mount(&sel_fs_type);
  1410. if (IS_ERR(selinuxfs_mount)) {
  1411. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1412. err = PTR_ERR(selinuxfs_mount);
  1413. selinuxfs_mount = NULL;
  1414. }
  1415. }
  1416. return err;
  1417. }
  1418. __initcall(init_sel_fs);
  1419. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1420. void exit_sel_fs(void)
  1421. {
  1422. unregister_filesystem(&sel_fs_type);
  1423. }
  1424. #endif