selinuxfs.c 32 KB

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