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