selinuxfs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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. /* partial revoke() from fs/proc/generic.c proc_kill_inodes */
  788. static void sel_remove_entries(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_entries(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|SEL_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_entries(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. inode->i_ino = ++sel_last_ino;
  1040. d_add(dentry, inode);
  1041. }
  1042. out:
  1043. return ret;
  1044. }
  1045. static ssize_t sel_read_initcon(struct file * file, char __user *buf,
  1046. size_t count, loff_t *ppos)
  1047. {
  1048. struct inode *inode;
  1049. char *con;
  1050. u32 sid, len;
  1051. ssize_t ret;
  1052. inode = file->f_path.dentry->d_inode;
  1053. sid = inode->i_ino&SEL_INO_MASK;
  1054. ret = security_sid_to_context(sid, &con, &len);
  1055. if (ret < 0)
  1056. return ret;
  1057. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1058. kfree(con);
  1059. return ret;
  1060. }
  1061. static const struct file_operations sel_initcon_ops = {
  1062. .read = sel_read_initcon,
  1063. };
  1064. static int sel_make_initcon_files(struct dentry *dir)
  1065. {
  1066. int i, ret = 0;
  1067. for (i = 1; i <= SECINITSID_NUM; i++) {
  1068. struct inode *inode;
  1069. struct dentry *dentry;
  1070. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1071. if (!dentry) {
  1072. ret = -ENOMEM;
  1073. goto out;
  1074. }
  1075. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1076. if (!inode) {
  1077. ret = -ENOMEM;
  1078. goto out;
  1079. }
  1080. inode->i_fop = &sel_initcon_ops;
  1081. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1082. d_add(dentry, inode);
  1083. }
  1084. out:
  1085. return ret;
  1086. }
  1087. static int sel_make_dir(struct inode *dir, struct dentry *dentry)
  1088. {
  1089. int ret = 0;
  1090. struct inode *inode;
  1091. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1092. if (!inode) {
  1093. ret = -ENOMEM;
  1094. goto out;
  1095. }
  1096. inode->i_op = &simple_dir_inode_operations;
  1097. inode->i_fop = &simple_dir_operations;
  1098. inode->i_ino = ++sel_last_ino;
  1099. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1100. inc_nlink(inode);
  1101. d_add(dentry, inode);
  1102. /* bump link count on parent directory, too */
  1103. inc_nlink(dir);
  1104. out:
  1105. return ret;
  1106. }
  1107. static int sel_fill_super(struct super_block * sb, void * data, int silent)
  1108. {
  1109. int ret;
  1110. struct dentry *dentry;
  1111. struct inode *inode, *root_inode;
  1112. struct inode_security_struct *isec;
  1113. static struct tree_descr selinux_files[] = {
  1114. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1115. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1116. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1117. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1118. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1119. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1120. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1121. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1122. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1123. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1124. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1125. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1126. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1127. [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
  1128. /* last one */ {""}
  1129. };
  1130. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1131. if (ret)
  1132. goto err;
  1133. root_inode = sb->s_root->d_inode;
  1134. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1135. if (!dentry) {
  1136. ret = -ENOMEM;
  1137. goto err;
  1138. }
  1139. ret = sel_make_dir(root_inode, dentry);
  1140. if (ret)
  1141. goto err;
  1142. bool_dir = dentry;
  1143. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1144. if (!dentry) {
  1145. ret = -ENOMEM;
  1146. goto err;
  1147. }
  1148. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1149. if (!inode) {
  1150. ret = -ENOMEM;
  1151. goto err;
  1152. }
  1153. inode->i_ino = ++sel_last_ino;
  1154. isec = (struct inode_security_struct*)inode->i_security;
  1155. isec->sid = SECINITSID_DEVNULL;
  1156. isec->sclass = SECCLASS_CHR_FILE;
  1157. isec->initialized = 1;
  1158. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1159. d_add(dentry, inode);
  1160. selinux_null = dentry;
  1161. dentry = d_alloc_name(sb->s_root, "avc");
  1162. if (!dentry) {
  1163. ret = -ENOMEM;
  1164. goto err;
  1165. }
  1166. ret = sel_make_dir(root_inode, dentry);
  1167. if (ret)
  1168. goto err;
  1169. ret = sel_make_avc_files(dentry);
  1170. if (ret)
  1171. goto err;
  1172. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1173. if (!dentry) {
  1174. ret = -ENOMEM;
  1175. goto err;
  1176. }
  1177. ret = sel_make_dir(root_inode, dentry);
  1178. if (ret)
  1179. goto err;
  1180. ret = sel_make_initcon_files(dentry);
  1181. if (ret)
  1182. goto err;
  1183. out:
  1184. return ret;
  1185. err:
  1186. printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
  1187. goto out;
  1188. }
  1189. static int sel_get_sb(struct file_system_type *fs_type,
  1190. int flags, const char *dev_name, void *data,
  1191. struct vfsmount *mnt)
  1192. {
  1193. return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
  1194. }
  1195. static struct file_system_type sel_fs_type = {
  1196. .name = "selinuxfs",
  1197. .get_sb = sel_get_sb,
  1198. .kill_sb = kill_litter_super,
  1199. };
  1200. struct vfsmount *selinuxfs_mount;
  1201. static int __init init_sel_fs(void)
  1202. {
  1203. int err;
  1204. if (!selinux_enabled)
  1205. return 0;
  1206. err = register_filesystem(&sel_fs_type);
  1207. if (!err) {
  1208. selinuxfs_mount = kern_mount(&sel_fs_type);
  1209. if (IS_ERR(selinuxfs_mount)) {
  1210. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1211. err = PTR_ERR(selinuxfs_mount);
  1212. selinuxfs_mount = NULL;
  1213. }
  1214. }
  1215. return err;
  1216. }
  1217. __initcall(init_sel_fs);
  1218. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1219. void exit_sel_fs(void)
  1220. {
  1221. unregister_filesystem(&sel_fs_type);
  1222. }
  1223. #endif