selinuxfs.c 32 KB

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