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