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