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