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