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