[PATCH] sem2mutex: security/
[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
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/pagemap.h>
15#include <linux/slab.h>
16#include <linux/vmalloc.h>
17#include <linux/fs.h>
bb003079 18#include <linux/mutex.h>
1da177e4
LT
19#include <linux/init.h>
20#include <linux/string.h>
21#include <linux/security.h>
22#include <linux/major.h>
23#include <linux/seq_file.h>
24#include <linux/percpu.h>
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
40static int __init checkreqprot_setup(char *str)
41{
42 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
43 return 1;
44}
45__setup("checkreqprot=", checkreqprot_setup);
46
47
bb003079 48static DEFINE_MUTEX(sel_mutex);
1da177e4
LT
49
50/* global data for booleans */
51static struct dentry *bool_dir = NULL;
52static int bool_num = 0;
53static int *bool_pending_values = NULL;
54
55extern void selnl_notify_setenforce(int val);
56
57/* Check whether a task is allowed to use a security operation. */
58static int task_has_security(struct task_struct *tsk,
59 u32 perms)
60{
61 struct task_security_struct *tsec;
62
63 tsec = tsk->security;
64 if (!tsec)
65 return -EACCES;
66
67 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
68 SECCLASS_SECURITY, perms, NULL);
69}
70
71enum sel_inos {
72 SEL_ROOT_INO = 2,
73 SEL_LOAD, /* load policy */
74 SEL_ENFORCE, /* get or set enforcing status */
75 SEL_CONTEXT, /* validate context */
76 SEL_ACCESS, /* compute access decision */
77 SEL_CREATE, /* compute create labeling decision */
78 SEL_RELABEL, /* compute relabeling decision */
79 SEL_USER, /* compute reachable user contexts */
80 SEL_POLICYVERS, /* return policy version for this kernel */
81 SEL_COMMIT_BOOLS, /* commit new boolean values */
82 SEL_MLS, /* return if MLS policy is enabled */
83 SEL_DISABLE, /* disable SELinux until next reboot */
84 SEL_AVC, /* AVC management directory */
85 SEL_MEMBER, /* compute polyinstantiation membership decision */
86 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
87};
88
89#define TMPBUFLEN 12
90static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
91 size_t count, loff_t *ppos)
92{
93 char tmpbuf[TMPBUFLEN];
94 ssize_t length;
95
96 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
97 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
98}
99
100#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
101static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
102 size_t count, loff_t *ppos)
103
104{
105 char *page;
106 ssize_t length;
107 int new_value;
108
bfd51626 109 if (count >= PAGE_SIZE)
1da177e4
LT
110 return -ENOMEM;
111 if (*ppos != 0) {
112 /* No partial writes. */
113 return -EINVAL;
114 }
115 page = (char*)get_zeroed_page(GFP_KERNEL);
116 if (!page)
117 return -ENOMEM;
118 length = -EFAULT;
119 if (copy_from_user(page, buf, count))
120 goto out;
121
122 length = -EINVAL;
123 if (sscanf(page, "%d", &new_value) != 1)
124 goto out;
125
126 if (new_value != selinux_enforcing) {
127 length = task_has_security(current, SECURITY__SETENFORCE);
128 if (length)
129 goto out;
130 selinux_enforcing = new_value;
131 if (selinux_enforcing)
132 avc_ss_reset(0);
133 selnl_notify_setenforce(selinux_enforcing);
134 }
135 length = count;
136out:
137 free_page((unsigned long) page);
138 return length;
139}
140#else
141#define sel_write_enforce NULL
142#endif
143
144static struct file_operations sel_enforce_ops = {
145 .read = sel_read_enforce,
146 .write = sel_write_enforce,
147};
148
149#ifdef CONFIG_SECURITY_SELINUX_DISABLE
150static ssize_t sel_write_disable(struct file * file, const char __user * buf,
151 size_t count, loff_t *ppos)
152
153{
154 char *page;
155 ssize_t length;
156 int new_value;
157 extern int selinux_disable(void);
158
bfd51626 159 if (count >= PAGE_SIZE)
1da177e4
LT
160 return -ENOMEM;
161 if (*ppos != 0) {
162 /* No partial writes. */
163 return -EINVAL;
164 }
165 page = (char*)get_zeroed_page(GFP_KERNEL);
166 if (!page)
167 return -ENOMEM;
168 length = -EFAULT;
169 if (copy_from_user(page, buf, count))
170 goto out;
171
172 length = -EINVAL;
173 if (sscanf(page, "%d", &new_value) != 1)
174 goto out;
175
176 if (new_value) {
177 length = selinux_disable();
178 if (length < 0)
179 goto out;
180 }
181
182 length = count;
183out:
184 free_page((unsigned long) page);
185 return length;
186}
187#else
188#define sel_write_disable NULL
189#endif
190
191static struct file_operations sel_disable_ops = {
192 .write = sel_write_disable,
193};
194
195static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
196 size_t count, loff_t *ppos)
197{
198 char tmpbuf[TMPBUFLEN];
199 ssize_t length;
200
201 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
202 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
203}
204
205static struct file_operations sel_policyvers_ops = {
206 .read = sel_read_policyvers,
207};
208
209/* declaration for sel_write_load */
210static int sel_make_bools(void);
211
212static ssize_t sel_read_mls(struct file *filp, char __user *buf,
213 size_t count, loff_t *ppos)
214{
215 char tmpbuf[TMPBUFLEN];
216 ssize_t length;
217
218 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
219 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
220}
221
222static struct file_operations sel_mls_ops = {
223 .read = sel_read_mls,
224};
225
226static ssize_t sel_write_load(struct file * file, const char __user * buf,
227 size_t count, loff_t *ppos)
228
229{
230 int ret;
231 ssize_t length;
232 void *data = NULL;
233
bb003079 234 mutex_lock(&sel_mutex);
1da177e4
LT
235
236 length = task_has_security(current, SECURITY__LOAD_POLICY);
237 if (length)
238 goto out;
239
240 if (*ppos != 0) {
241 /* No partial writes. */
242 length = -EINVAL;
243 goto out;
244 }
245
bfd51626 246 if ((count > 64 * 1024 * 1024)
1da177e4
LT
247 || (data = vmalloc(count)) == NULL) {
248 length = -ENOMEM;
249 goto out;
250 }
251
252 length = -EFAULT;
253 if (copy_from_user(data, buf, count) != 0)
254 goto out;
255
256 length = security_load_policy(data, count);
257 if (length)
258 goto out;
259
260 ret = sel_make_bools();
261 if (ret)
262 length = ret;
263 else
264 length = count;
265out:
bb003079 266 mutex_unlock(&sel_mutex);
1da177e4
LT
267 vfree(data);
268 return length;
269}
270
271static struct file_operations sel_load_ops = {
272 .write = sel_write_load,
273};
274
ce9982d0 275static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
1da177e4 276{
ce9982d0
SS
277 char *canon;
278 u32 sid, len;
1da177e4
LT
279 ssize_t length;
280
281 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
282 if (length)
283 return length;
284
ce9982d0
SS
285 length = security_context_to_sid(buf, size, &sid);
286 if (length < 0)
287 return length;
1da177e4 288
ce9982d0 289 length = security_sid_to_context(sid, &canon, &len);
1da177e4 290 if (length < 0)
ce9982d0
SS
291 return length;
292
293 if (len > SIMPLE_TRANSACTION_LIMIT) {
294 printk(KERN_ERR "%s: context size (%u) exceeds payload "
295 "max\n", __FUNCTION__, len);
296 length = -ERANGE;
1da177e4 297 goto out;
ce9982d0 298 }
1da177e4 299
ce9982d0
SS
300 memcpy(buf, canon, len);
301 length = len;
1da177e4 302out:
ce9982d0 303 kfree(canon);
1da177e4
LT
304 return length;
305}
306
1da177e4
LT
307static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
308 size_t count, loff_t *ppos)
309{
310 char tmpbuf[TMPBUFLEN];
311 ssize_t length;
312
313 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
314 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
315}
316
317static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
318 size_t count, loff_t *ppos)
319{
320 char *page;
321 ssize_t length;
322 unsigned int new_value;
323
324 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
325 if (length)
326 return length;
327
bfd51626 328 if (count >= PAGE_SIZE)
1da177e4
LT
329 return -ENOMEM;
330 if (*ppos != 0) {
331 /* No partial writes. */
332 return -EINVAL;
333 }
334 page = (char*)get_zeroed_page(GFP_KERNEL);
335 if (!page)
336 return -ENOMEM;
337 length = -EFAULT;
338 if (copy_from_user(page, buf, count))
339 goto out;
340
341 length = -EINVAL;
342 if (sscanf(page, "%u", &new_value) != 1)
343 goto out;
344
345 selinux_checkreqprot = new_value ? 1 : 0;
346 length = count;
347out:
348 free_page((unsigned long) page);
349 return length;
350}
351static struct file_operations sel_checkreqprot_ops = {
352 .read = sel_read_checkreqprot,
353 .write = sel_write_checkreqprot,
354};
355
356/*
357 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
358 */
359static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
360static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
361static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
362static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
363static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
364
365static ssize_t (*write_op[])(struct file *, char *, size_t) = {
366 [SEL_ACCESS] = sel_write_access,
367 [SEL_CREATE] = sel_write_create,
368 [SEL_RELABEL] = sel_write_relabel,
369 [SEL_USER] = sel_write_user,
370 [SEL_MEMBER] = sel_write_member,
ce9982d0 371 [SEL_CONTEXT] = sel_write_context,
1da177e4
LT
372};
373
374static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
375{
376 ino_t ino = file->f_dentry->d_inode->i_ino;
377 char *data;
378 ssize_t rv;
379
6e20a64a 380 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1da177e4
LT
381 return -EINVAL;
382
383 data = simple_transaction_get(file, buf, size);
384 if (IS_ERR(data))
385 return PTR_ERR(data);
386
387 rv = write_op[ino](file, data, size);
388 if (rv>0) {
389 simple_transaction_set(file, rv);
390 rv = size;
391 }
392 return rv;
393}
394
395static struct file_operations transaction_ops = {
396 .write = selinux_transaction_write,
397 .read = simple_transaction_read,
398 .release = simple_transaction_release,
399};
400
401/*
402 * payload - write methods
403 * If the method has a response, the response should be put in buf,
404 * and the length returned. Otherwise return 0 or and -error.
405 */
406
407static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
408{
409 char *scon, *tcon;
410 u32 ssid, tsid;
411 u16 tclass;
412 u32 req;
413 struct av_decision avd;
414 ssize_t length;
415
416 length = task_has_security(current, SECURITY__COMPUTE_AV);
417 if (length)
418 return length;
419
420 length = -ENOMEM;
89d155ef 421 scon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
422 if (!scon)
423 return length;
1da177e4 424
89d155ef 425 tcon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
426 if (!tcon)
427 goto out;
1da177e4
LT
428
429 length = -EINVAL;
430 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
431 goto out2;
432
433 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
434 if (length < 0)
435 goto out2;
436 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
437 if (length < 0)
438 goto out2;
439
440 length = security_compute_av(ssid, tsid, tclass, req, &avd);
441 if (length < 0)
442 goto out2;
443
444 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
445 "%x %x %x %x %u",
446 avd.allowed, avd.decided,
447 avd.auditallow, avd.auditdeny,
448 avd.seqno);
449out2:
450 kfree(tcon);
451out:
452 kfree(scon);
453 return length;
454}
455
456static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
457{
458 char *scon, *tcon;
459 u32 ssid, tsid, newsid;
460 u16 tclass;
461 ssize_t length;
462 char *newcon;
463 u32 len;
464
465 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
466 if (length)
467 return length;
468
469 length = -ENOMEM;
89d155ef 470 scon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
471 if (!scon)
472 return length;
1da177e4 473
89d155ef 474 tcon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
475 if (!tcon)
476 goto out;
1da177e4
LT
477
478 length = -EINVAL;
479 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
480 goto out2;
481
482 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
483 if (length < 0)
484 goto out2;
485 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
486 if (length < 0)
487 goto out2;
488
489 length = security_transition_sid(ssid, tsid, tclass, &newsid);
490 if (length < 0)
491 goto out2;
492
493 length = security_sid_to_context(newsid, &newcon, &len);
494 if (length < 0)
495 goto out2;
496
497 if (len > SIMPLE_TRANSACTION_LIMIT) {
498 printk(KERN_ERR "%s: context size (%u) exceeds payload "
499 "max\n", __FUNCTION__, len);
500 length = -ERANGE;
501 goto out3;
502 }
503
504 memcpy(buf, newcon, len);
505 length = len;
506out3:
507 kfree(newcon);
508out2:
509 kfree(tcon);
510out:
511 kfree(scon);
512 return length;
513}
514
515static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
516{
517 char *scon, *tcon;
518 u32 ssid, tsid, newsid;
519 u16 tclass;
520 ssize_t length;
521 char *newcon;
522 u32 len;
523
524 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
525 if (length)
526 return length;
527
528 length = -ENOMEM;
89d155ef 529 scon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
530 if (!scon)
531 return length;
1da177e4 532
89d155ef 533 tcon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
534 if (!tcon)
535 goto out;
1da177e4
LT
536
537 length = -EINVAL;
538 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
539 goto out2;
540
541 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
542 if (length < 0)
543 goto out2;
544 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
545 if (length < 0)
546 goto out2;
547
548 length = security_change_sid(ssid, tsid, tclass, &newsid);
549 if (length < 0)
550 goto out2;
551
552 length = security_sid_to_context(newsid, &newcon, &len);
553 if (length < 0)
554 goto out2;
555
556 if (len > SIMPLE_TRANSACTION_LIMIT) {
557 length = -ERANGE;
558 goto out3;
559 }
560
561 memcpy(buf, newcon, len);
562 length = len;
563out3:
564 kfree(newcon);
565out2:
566 kfree(tcon);
567out:
568 kfree(scon);
569 return length;
570}
571
572static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
573{
574 char *con, *user, *ptr;
575 u32 sid, *sids;
576 ssize_t length;
577 char *newcon;
578 int i, rc;
579 u32 len, nsids;
580
581 length = task_has_security(current, SECURITY__COMPUTE_USER);
582 if (length)
583 return length;
584
585 length = -ENOMEM;
89d155ef 586 con = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
587 if (!con)
588 return length;
1da177e4 589
89d155ef 590 user = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
591 if (!user)
592 goto out;
1da177e4
LT
593
594 length = -EINVAL;
595 if (sscanf(buf, "%s %s", con, user) != 2)
596 goto out2;
597
598 length = security_context_to_sid(con, strlen(con)+1, &sid);
599 if (length < 0)
600 goto out2;
601
602 length = security_get_user_sids(sid, user, &sids, &nsids);
603 if (length < 0)
604 goto out2;
605
606 length = sprintf(buf, "%u", nsids) + 1;
607 ptr = buf + length;
608 for (i = 0; i < nsids; i++) {
609 rc = security_sid_to_context(sids[i], &newcon, &len);
610 if (rc) {
611 length = rc;
612 goto out3;
613 }
614 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
615 kfree(newcon);
616 length = -ERANGE;
617 goto out3;
618 }
619 memcpy(ptr, newcon, len);
620 kfree(newcon);
621 ptr += len;
622 length += len;
623 }
624out3:
625 kfree(sids);
626out2:
627 kfree(user);
628out:
629 kfree(con);
630 return length;
631}
632
633static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
634{
635 char *scon, *tcon;
636 u32 ssid, tsid, newsid;
637 u16 tclass;
638 ssize_t length;
639 char *newcon;
640 u32 len;
641
642 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
643 if (length)
644 return length;
645
646 length = -ENOMEM;
89d155ef 647 scon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
648 if (!scon)
649 return length;
1da177e4 650
89d155ef 651 tcon = kzalloc(size+1, GFP_KERNEL);
1da177e4
LT
652 if (!tcon)
653 goto out;
1da177e4
LT
654
655 length = -EINVAL;
656 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
657 goto out2;
658
659 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
660 if (length < 0)
661 goto out2;
662 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
663 if (length < 0)
664 goto out2;
665
666 length = security_member_sid(ssid, tsid, tclass, &newsid);
667 if (length < 0)
668 goto out2;
669
670 length = security_sid_to_context(newsid, &newcon, &len);
671 if (length < 0)
672 goto out2;
673
674 if (len > SIMPLE_TRANSACTION_LIMIT) {
675 printk(KERN_ERR "%s: context size (%u) exceeds payload "
676 "max\n", __FUNCTION__, len);
677 length = -ERANGE;
678 goto out3;
679 }
680
681 memcpy(buf, newcon, len);
682 length = len;
683out3:
684 kfree(newcon);
685out2:
686 kfree(tcon);
687out:
688 kfree(scon);
689 return length;
690}
691
692static struct inode *sel_make_inode(struct super_block *sb, int mode)
693{
694 struct inode *ret = new_inode(sb);
695
696 if (ret) {
697 ret->i_mode = mode;
698 ret->i_uid = ret->i_gid = 0;
699 ret->i_blksize = PAGE_CACHE_SIZE;
700 ret->i_blocks = 0;
701 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
702 }
703 return ret;
704}
705
706#define BOOL_INO_OFFSET 30
707
708static ssize_t sel_read_bool(struct file *filep, char __user *buf,
709 size_t count, loff_t *ppos)
710{
711 char *page = NULL;
712 ssize_t length;
713 ssize_t end;
714 ssize_t ret;
715 int cur_enforcing;
716 struct inode *inode;
717
bb003079 718 mutex_lock(&sel_mutex);
1da177e4
LT
719
720 ret = -EFAULT;
721
722 /* check to see if this file has been deleted */
723 if (!filep->f_op)
724 goto out;
725
bfd51626 726 if (count > PAGE_SIZE) {
1da177e4
LT
727 ret = -EINVAL;
728 goto out;
729 }
730 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
731 ret = -ENOMEM;
732 goto out;
733 }
734
735 inode = filep->f_dentry->d_inode;
736 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
737 if (cur_enforcing < 0) {
738 ret = cur_enforcing;
739 goto out;
740 }
741
742 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
743 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
744 if (length < 0) {
745 ret = length;
746 goto out;
747 }
748
749 if (*ppos >= length) {
750 ret = 0;
751 goto out;
752 }
753 if (count + *ppos > length)
754 count = length - *ppos;
755 end = count + *ppos;
756 if (copy_to_user(buf, (char *) page + *ppos, count)) {
757 ret = -EFAULT;
758 goto out;
759 }
760 *ppos = end;
761 ret = count;
762out:
bb003079 763 mutex_unlock(&sel_mutex);
1da177e4
LT
764 if (page)
765 free_page((unsigned long)page);
766 return ret;
767}
768
769static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
770 size_t count, loff_t *ppos)
771{
772 char *page = NULL;
773 ssize_t length = -EFAULT;
774 int new_value;
775 struct inode *inode;
776
bb003079 777 mutex_lock(&sel_mutex);
1da177e4
LT
778
779 length = task_has_security(current, SECURITY__SETBOOL);
780 if (length)
781 goto out;
782
783 /* check to see if this file has been deleted */
784 if (!filep->f_op)
785 goto out;
786
bfd51626 787 if (count >= PAGE_SIZE) {
1da177e4
LT
788 length = -ENOMEM;
789 goto out;
790 }
791 if (*ppos != 0) {
792 /* No partial writes. */
793 goto out;
794 }
795 page = (char*)get_zeroed_page(GFP_KERNEL);
796 if (!page) {
797 length = -ENOMEM;
798 goto out;
799 }
800
801 if (copy_from_user(page, buf, count))
802 goto out;
803
804 length = -EINVAL;
805 if (sscanf(page, "%d", &new_value) != 1)
806 goto out;
807
808 if (new_value)
809 new_value = 1;
810
811 inode = filep->f_dentry->d_inode;
812 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
813 length = count;
814
815out:
bb003079 816 mutex_unlock(&sel_mutex);
1da177e4
LT
817 if (page)
818 free_page((unsigned long) page);
819 return length;
820}
821
822static struct file_operations sel_bool_ops = {
823 .read = sel_read_bool,
824 .write = sel_write_bool,
825};
826
827static ssize_t sel_commit_bools_write(struct file *filep,
828 const char __user *buf,
829 size_t count, loff_t *ppos)
830{
831 char *page = NULL;
832 ssize_t length = -EFAULT;
833 int new_value;
834
bb003079 835 mutex_lock(&sel_mutex);
1da177e4
LT
836
837 length = task_has_security(current, SECURITY__SETBOOL);
838 if (length)
839 goto out;
840
841 /* check to see if this file has been deleted */
842 if (!filep->f_op)
843 goto out;
844
bfd51626 845 if (count >= PAGE_SIZE) {
1da177e4
LT
846 length = -ENOMEM;
847 goto out;
848 }
849 if (*ppos != 0) {
850 /* No partial writes. */
851 goto out;
852 }
853 page = (char*)get_zeroed_page(GFP_KERNEL);
854 if (!page) {
855 length = -ENOMEM;
856 goto out;
857 }
858
859 if (copy_from_user(page, buf, count))
860 goto out;
861
862 length = -EINVAL;
863 if (sscanf(page, "%d", &new_value) != 1)
864 goto out;
865
20c19e41 866 if (new_value && bool_pending_values) {
1da177e4
LT
867 security_set_bools(bool_num, bool_pending_values);
868 }
869
870 length = count;
871
872out:
bb003079 873 mutex_unlock(&sel_mutex);
1da177e4
LT
874 if (page)
875 free_page((unsigned long) page);
876 return length;
877}
878
879static struct file_operations sel_commit_bools_ops = {
880 .write = sel_commit_bools_write,
881};
882
883/* delete booleans - partial revoke() from
884 * fs/proc/generic.c proc_kill_inodes */
885static void sel_remove_bools(struct dentry *de)
886{
887 struct list_head *p, *node;
888 struct super_block *sb = de->d_sb;
889
890 spin_lock(&dcache_lock);
891 node = de->d_subdirs.next;
892 while (node != &de->d_subdirs) {
5160ee6f 893 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1da177e4
LT
894 list_del_init(node);
895
896 if (d->d_inode) {
897 d = dget_locked(d);
898 spin_unlock(&dcache_lock);
899 d_delete(d);
900 simple_unlink(de->d_inode, d);
901 dput(d);
902 spin_lock(&dcache_lock);
903 }
904 node = de->d_subdirs.next;
905 }
906
907 spin_unlock(&dcache_lock);
908
909 file_list_lock();
910 list_for_each(p, &sb->s_files) {
2f512016 911 struct file * filp = list_entry(p, struct file, f_u.fu_list);
1da177e4
LT
912 struct dentry * dentry = filp->f_dentry;
913
914 if (dentry->d_parent != de) {
915 continue;
916 }
917 filp->f_op = NULL;
918 }
919 file_list_unlock();
920}
921
922#define BOOL_DIR_NAME "booleans"
923
924static int sel_make_bools(void)
925{
926 int i, ret = 0;
927 ssize_t len;
928 struct dentry *dentry = NULL;
929 struct dentry *dir = bool_dir;
930 struct inode *inode = NULL;
931 struct inode_security_struct *isec;
932 char **names = NULL, *page;
933 int num;
934 int *values = NULL;
935 u32 sid;
936
937 /* remove any existing files */
9a5f04bf 938 kfree(bool_pending_values);
20c19e41 939 bool_pending_values = NULL;
1da177e4
LT
940
941 sel_remove_bools(dir);
942
943 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
944 return -ENOMEM;
945
946 ret = security_get_bools(&num, &names, &values);
947 if (ret != 0)
948 goto out;
949
950 for (i = 0; i < num; i++) {
951 dentry = d_alloc_name(dir, names[i]);
952 if (!dentry) {
953 ret = -ENOMEM;
954 goto err;
955 }
956 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
957 if (!inode) {
958 ret = -ENOMEM;
959 goto err;
960 }
961
962 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
963 if (len < 0) {
964 ret = -EINVAL;
965 goto err;
966 } else if (len >= PAGE_SIZE) {
967 ret = -ENAMETOOLONG;
968 goto err;
969 }
970 isec = (struct inode_security_struct*)inode->i_security;
971 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
972 goto err;
973 isec->sid = sid;
974 isec->initialized = 1;
975 inode->i_fop = &sel_bool_ops;
976 inode->i_ino = i + BOOL_INO_OFFSET;
977 d_add(dentry, inode);
978 }
979 bool_num = num;
980 bool_pending_values = values;
981out:
982 free_page((unsigned long)page);
983 if (names) {
9a5f04bf
JJ
984 for (i = 0; i < num; i++)
985 kfree(names[i]);
1da177e4
LT
986 kfree(names);
987 }
988 return ret;
989err:
20c19e41 990 kfree(values);
1da177e4
LT
991 d_genocide(dir);
992 ret = -ENOMEM;
993 goto out;
994}
995
996#define NULL_FILE_NAME "null"
997
998struct dentry *selinux_null = NULL;
999
1000static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1001 size_t count, loff_t *ppos)
1002{
1003 char tmpbuf[TMPBUFLEN];
1004 ssize_t length;
1005
1006 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1007 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1008}
1009
1010static ssize_t sel_write_avc_cache_threshold(struct file * file,
1011 const char __user * buf,
1012 size_t count, loff_t *ppos)
1013
1014{
1015 char *page;
1016 ssize_t ret;
1017 int new_value;
1018
bfd51626 1019 if (count >= PAGE_SIZE) {
1da177e4
LT
1020 ret = -ENOMEM;
1021 goto out;
1022 }
1023
1024 if (*ppos != 0) {
1025 /* No partial writes. */
1026 ret = -EINVAL;
1027 goto out;
1028 }
1029
1030 page = (char*)get_zeroed_page(GFP_KERNEL);
1031 if (!page) {
1032 ret = -ENOMEM;
1033 goto out;
1034 }
1035
1036 if (copy_from_user(page, buf, count)) {
1037 ret = -EFAULT;
1038 goto out_free;
1039 }
1040
1041 if (sscanf(page, "%u", &new_value) != 1) {
1042 ret = -EINVAL;
1043 goto out;
1044 }
1045
1046 if (new_value != avc_cache_threshold) {
1047 ret = task_has_security(current, SECURITY__SETSECPARAM);
1048 if (ret)
1049 goto out_free;
1050 avc_cache_threshold = new_value;
1051 }
1052 ret = count;
1053out_free:
1054 free_page((unsigned long)page);
1055out:
1056 return ret;
1057}
1058
1059static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1060 size_t count, loff_t *ppos)
1061{
1062 char *page;
1063 ssize_t ret = 0;
1064
1065 page = (char *)__get_free_page(GFP_KERNEL);
1066 if (!page) {
1067 ret = -ENOMEM;
1068 goto out;
1069 }
1070 ret = avc_get_hash_stats(page);
1071 if (ret >= 0)
1072 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1073 free_page((unsigned long)page);
1074out:
1075 return ret;
1076}
1077
1078static struct file_operations sel_avc_cache_threshold_ops = {
1079 .read = sel_read_avc_cache_threshold,
1080 .write = sel_write_avc_cache_threshold,
1081};
1082
1083static struct file_operations sel_avc_hash_stats_ops = {
1084 .read = sel_read_avc_hash_stats,
1085};
1086
1087#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1088static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1089{
1090 int cpu;
1091
1092 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1093 if (!cpu_possible(cpu))
1094 continue;
1095 *idx = cpu + 1;
1096 return &per_cpu(avc_cache_stats, cpu);
1097 }
1098 return NULL;
1099}
1100
1101static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1102{
1103 loff_t n = *pos - 1;
1104
1105 if (*pos == 0)
1106 return SEQ_START_TOKEN;
1107
1108 return sel_avc_get_stat_idx(&n);
1109}
1110
1111static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1112{
1113 return sel_avc_get_stat_idx(pos);
1114}
1115
1116static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1117{
1118 struct avc_cache_stats *st = v;
1119
1120 if (v == SEQ_START_TOKEN)
1121 seq_printf(seq, "lookups hits misses allocations reclaims "
1122 "frees\n");
1123 else
1124 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1125 st->hits, st->misses, st->allocations,
1126 st->reclaims, st->frees);
1127 return 0;
1128}
1129
1130static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1131{ }
1132
1133static struct seq_operations sel_avc_cache_stats_seq_ops = {
1134 .start = sel_avc_stats_seq_start,
1135 .next = sel_avc_stats_seq_next,
1136 .show = sel_avc_stats_seq_show,
1137 .stop = sel_avc_stats_seq_stop,
1138};
1139
1140static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1141{
1142 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1143}
1144
1145static struct file_operations sel_avc_cache_stats_ops = {
1146 .open = sel_open_avc_cache_stats,
1147 .read = seq_read,
1148 .llseek = seq_lseek,
1149 .release = seq_release,
1150};
1151#endif
1152
1153static int sel_make_avc_files(struct dentry *dir)
1154{
1155 int i, ret = 0;
1156 static struct tree_descr files[] = {
1157 { "cache_threshold",
1158 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1159 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1160#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1161 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1162#endif
1163 };
1164
6e20a64a 1165 for (i = 0; i < ARRAY_SIZE(files); i++) {
1da177e4
LT
1166 struct inode *inode;
1167 struct dentry *dentry;
1168
1169 dentry = d_alloc_name(dir, files[i].name);
1170 if (!dentry) {
1171 ret = -ENOMEM;
1172 goto err;
1173 }
1174
1175 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1176 if (!inode) {
1177 ret = -ENOMEM;
1178 goto err;
1179 }
1180 inode->i_fop = files[i].ops;
1181 d_add(dentry, inode);
1182 }
1183out:
1184 return ret;
1185err:
1186 d_genocide(dir);
1187 goto out;
1188}
1189
1190static int sel_make_dir(struct super_block *sb, struct dentry *dentry)
1191{
1192 int ret = 0;
1193 struct inode *inode;
1194
1195 inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
1196 if (!inode) {
1197 ret = -ENOMEM;
1198 goto out;
1199 }
1200 inode->i_op = &simple_dir_inode_operations;
1201 inode->i_fop = &simple_dir_operations;
1202 d_add(dentry, inode);
1203out:
1204 return ret;
1205}
1206
1207static int sel_fill_super(struct super_block * sb, void * data, int silent)
1208{
1209 int ret;
1210 struct dentry *dentry;
1211 struct inode *inode;
1212 struct inode_security_struct *isec;
1213
1214 static struct tree_descr selinux_files[] = {
1215 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1216 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
ce9982d0 1217 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1da177e4
LT
1218 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1219 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1220 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1221 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1222 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1223 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1224 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1225 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1226 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1227 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1228 /* last one */ {""}
1229 };
1230 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1231 if (ret)
1232 return ret;
1233
1234 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1235 if (!dentry)
1236 return -ENOMEM;
1237
1238 inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
1239 if (!inode)
1240 goto out;
1241 inode->i_op = &simple_dir_inode_operations;
1242 inode->i_fop = &simple_dir_operations;
1243 d_add(dentry, inode);
1244 bool_dir = dentry;
1245 ret = sel_make_bools();
1246 if (ret)
1247 goto out;
1248
1249 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1250 if (!dentry)
1251 return -ENOMEM;
1252
1253 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1254 if (!inode)
1255 goto out;
1256 isec = (struct inode_security_struct*)inode->i_security;
1257 isec->sid = SECINITSID_DEVNULL;
1258 isec->sclass = SECCLASS_CHR_FILE;
1259 isec->initialized = 1;
1260
1261 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1262 d_add(dentry, inode);
1263 selinux_null = dentry;
1264
1265 dentry = d_alloc_name(sb->s_root, "avc");
1266 if (!dentry)
1267 return -ENOMEM;
1268
1269 ret = sel_make_dir(sb, dentry);
1270 if (ret)
1271 goto out;
1272
1273 ret = sel_make_avc_files(dentry);
1274 if (ret)
1275 goto out;
1276
1277 return 0;
1278out:
1279 dput(dentry);
1280 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1281 return -ENOMEM;
1282}
1283
1284static struct super_block *sel_get_sb(struct file_system_type *fs_type,
1285 int flags, const char *dev_name, void *data)
1286{
1287 return get_sb_single(fs_type, flags, data, sel_fill_super);
1288}
1289
1290static struct file_system_type sel_fs_type = {
1291 .name = "selinuxfs",
1292 .get_sb = sel_get_sb,
1293 .kill_sb = kill_litter_super,
1294};
1295
1296struct vfsmount *selinuxfs_mount;
1297
1298static int __init init_sel_fs(void)
1299{
1300 int err;
1301
1302 if (!selinux_enabled)
1303 return 0;
1304 err = register_filesystem(&sel_fs_type);
1305 if (!err) {
1306 selinuxfs_mount = kern_mount(&sel_fs_type);
1307 if (IS_ERR(selinuxfs_mount)) {
1308 printk(KERN_ERR "selinuxfs: could not mount!\n");
1309 err = PTR_ERR(selinuxfs_mount);
1310 selinuxfs_mount = NULL;
1311 }
1312 }
1313 return err;
1314}
1315
1316__initcall(init_sel_fs);
1317
1318#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1319void exit_sel_fs(void)
1320{
1321 unregister_filesystem(&sel_fs_type);
1322}
1323#endif
This page took 0.178015 seconds and 5 git commands to generate.