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