blob: e24235c59ddf18ce1a02b6115e3612bf57051993 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/fs.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/string.h>
20#include <linux/security.h>
21#include <linux/major.h>
22#include <linux/seq_file.h>
23#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000024#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
James Morris4e5ab4c2006-06-09 00:33:33 -070040#ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
41#define SELINUX_COMPAT_NET_VALUE 0
42#else
43#define SELINUX_COMPAT_NET_VALUE 1
44#endif
45
46int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int __init checkreqprot_setup(char *str)
49{
50 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
51 return 1;
52}
53__setup("checkreqprot=", checkreqprot_setup);
54
James Morris4e5ab4c2006-06-09 00:33:33 -070055static int __init selinux_compat_net_setup(char *str)
56{
57 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
58 return 1;
59}
60__setup("selinux_compat_net=", selinux_compat_net_setup);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnarbb003072006-03-22 00:09:14 -080063static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* global data for booleans */
66static struct dentry *bool_dir = NULL;
67static int bool_num = 0;
68static int *bool_pending_values = NULL;
69
70extern void selnl_notify_setenforce(int val);
71
72/* Check whether a task is allowed to use a security operation. */
73static int task_has_security(struct task_struct *tsk,
74 u32 perms)
75{
76 struct task_security_struct *tsec;
77
78 tsec = tsk->security;
79 if (!tsec)
80 return -EACCES;
81
82 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
83 SECCLASS_SECURITY, perms, NULL);
84}
85
86enum sel_inos {
87 SEL_ROOT_INO = 2,
88 SEL_LOAD, /* load policy */
89 SEL_ENFORCE, /* get or set enforcing status */
90 SEL_CONTEXT, /* validate context */
91 SEL_ACCESS, /* compute access decision */
92 SEL_CREATE, /* compute create labeling decision */
93 SEL_RELABEL, /* compute relabeling decision */
94 SEL_USER, /* compute reachable user contexts */
95 SEL_POLICYVERS, /* return policy version for this kernel */
96 SEL_COMMIT_BOOLS, /* commit new boolean values */
97 SEL_MLS, /* return if MLS policy is enabled */
98 SEL_DISABLE, /* disable SELinux until next reboot */
99 SEL_AVC, /* AVC management directory */
100 SEL_MEMBER, /* compute polyinstantiation membership decision */
101 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700102 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
James Carterf0ee2e42007-04-04 10:11:29 -0400105#define SEL_INITCON_INO_OFFSET 0x01000000
106#define SEL_INO_MASK 0x00ffffff
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define TMPBUFLEN 12
109static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
110 size_t count, loff_t *ppos)
111{
112 char tmpbuf[TMPBUFLEN];
113 ssize_t length;
114
115 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
116 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
117}
118
119#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
120static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
121 size_t count, loff_t *ppos)
122
123{
124 char *page;
125 ssize_t length;
126 int new_value;
127
Davi Arnautbfd51622005-10-30 14:59:24 -0800128 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -ENOMEM;
130 if (*ppos != 0) {
131 /* No partial writes. */
132 return -EINVAL;
133 }
134 page = (char*)get_zeroed_page(GFP_KERNEL);
135 if (!page)
136 return -ENOMEM;
137 length = -EFAULT;
138 if (copy_from_user(page, buf, count))
139 goto out;
140
141 length = -EINVAL;
142 if (sscanf(page, "%d", &new_value) != 1)
143 goto out;
144
145 if (new_value != selinux_enforcing) {
146 length = task_has_security(current, SECURITY__SETENFORCE);
147 if (length)
148 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000149 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
150 "enforcing=%d old_enforcing=%d auid=%u", new_value,
151 selinux_enforcing,
152 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 selinux_enforcing = new_value;
154 if (selinux_enforcing)
155 avc_ss_reset(0);
156 selnl_notify_setenforce(selinux_enforcing);
157 }
158 length = count;
159out:
160 free_page((unsigned long) page);
161 return length;
162}
163#else
164#define sel_write_enforce NULL
165#endif
166
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800167static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .read = sel_read_enforce,
169 .write = sel_write_enforce,
170};
171
172#ifdef CONFIG_SECURITY_SELINUX_DISABLE
173static ssize_t sel_write_disable(struct file * file, const char __user * buf,
174 size_t count, loff_t *ppos)
175
176{
177 char *page;
178 ssize_t length;
179 int new_value;
180 extern int selinux_disable(void);
181
Davi Arnautbfd51622005-10-30 14:59:24 -0800182 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -ENOMEM;
184 if (*ppos != 0) {
185 /* No partial writes. */
186 return -EINVAL;
187 }
188 page = (char*)get_zeroed_page(GFP_KERNEL);
189 if (!page)
190 return -ENOMEM;
191 length = -EFAULT;
192 if (copy_from_user(page, buf, count))
193 goto out;
194
195 length = -EINVAL;
196 if (sscanf(page, "%d", &new_value) != 1)
197 goto out;
198
199 if (new_value) {
200 length = selinux_disable();
201 if (length < 0)
202 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000203 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
204 "selinux=0 auid=%u",
205 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 length = count;
209out:
210 free_page((unsigned long) page);
211 return length;
212}
213#else
214#define sel_write_disable NULL
215#endif
216
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800217static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .write = sel_write_disable,
219};
220
221static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
222 size_t count, loff_t *ppos)
223{
224 char tmpbuf[TMPBUFLEN];
225 ssize_t length;
226
227 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
228 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
229}
230
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800231static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .read = sel_read_policyvers,
233};
234
235/* declaration for sel_write_load */
236static int sel_make_bools(void);
237
238static ssize_t sel_read_mls(struct file *filp, char __user *buf,
239 size_t count, loff_t *ppos)
240{
241 char tmpbuf[TMPBUFLEN];
242 ssize_t length;
243
244 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
245 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
246}
247
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800248static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .read = sel_read_mls,
250};
251
252static ssize_t sel_write_load(struct file * file, const char __user * buf,
253 size_t count, loff_t *ppos)
254
255{
256 int ret;
257 ssize_t length;
258 void *data = NULL;
259
Ingo Molnarbb003072006-03-22 00:09:14 -0800260 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 length = task_has_security(current, SECURITY__LOAD_POLICY);
263 if (length)
264 goto out;
265
266 if (*ppos != 0) {
267 /* No partial writes. */
268 length = -EINVAL;
269 goto out;
270 }
271
Davi Arnautbfd51622005-10-30 14:59:24 -0800272 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 || (data = vmalloc(count)) == NULL) {
274 length = -ENOMEM;
275 goto out;
276 }
277
278 length = -EFAULT;
279 if (copy_from_user(data, buf, count) != 0)
280 goto out;
281
282 length = security_load_policy(data, count);
283 if (length)
284 goto out;
285
286 ret = sel_make_bools();
287 if (ret)
288 length = ret;
289 else
290 length = count;
Steve Grubbaf601e42006-01-04 14:08:39 +0000291 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
292 "policy loaded auid=%u",
293 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800295 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 vfree(data);
297 return length;
298}
299
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800300static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 .write = sel_write_load,
302};
303
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800304static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800306 char *canon;
307 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 ssize_t length;
309
310 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
311 if (length)
312 return length;
313
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800314 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800316 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800318 length = security_sid_to_context(sid, &canon, &len);
319 if (length < 0)
320 return length;
321
322 if (len > SIMPLE_TRANSACTION_LIMIT) {
323 printk(KERN_ERR "%s: context size (%u) exceeds payload "
324 "max\n", __FUNCTION__, len);
325 length = -ERANGE;
326 goto out;
327 }
328
329 memcpy(buf, canon, len);
330 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800332 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return length;
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
337 size_t count, loff_t *ppos)
338{
339 char tmpbuf[TMPBUFLEN];
340 ssize_t length;
341
342 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
343 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
344}
345
346static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
347 size_t count, loff_t *ppos)
348{
349 char *page;
350 ssize_t length;
351 unsigned int new_value;
352
353 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
354 if (length)
355 return length;
356
Davi Arnautbfd51622005-10-30 14:59:24 -0800357 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENOMEM;
359 if (*ppos != 0) {
360 /* No partial writes. */
361 return -EINVAL;
362 }
363 page = (char*)get_zeroed_page(GFP_KERNEL);
364 if (!page)
365 return -ENOMEM;
366 length = -EFAULT;
367 if (copy_from_user(page, buf, count))
368 goto out;
369
370 length = -EINVAL;
371 if (sscanf(page, "%u", &new_value) != 1)
372 goto out;
373
374 selinux_checkreqprot = new_value ? 1 : 0;
375 length = count;
376out:
377 free_page((unsigned long) page);
378 return length;
379}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800380static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .read = sel_read_checkreqprot,
382 .write = sel_write_checkreqprot,
383};
384
James Morris4e5ab4c2006-06-09 00:33:33 -0700385static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
386 size_t count, loff_t *ppos)
387{
388 char tmpbuf[TMPBUFLEN];
389 ssize_t length;
390
391 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
392 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
393}
394
395static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
396 size_t count, loff_t *ppos)
397{
398 char *page;
399 ssize_t length;
400 int new_value;
401
402 length = task_has_security(current, SECURITY__LOAD_POLICY);
403 if (length)
404 return length;
405
406 if (count >= PAGE_SIZE)
407 return -ENOMEM;
408 if (*ppos != 0) {
409 /* No partial writes. */
410 return -EINVAL;
411 }
412 page = (char*)get_zeroed_page(GFP_KERNEL);
413 if (!page)
414 return -ENOMEM;
415 length = -EFAULT;
416 if (copy_from_user(page, buf, count))
417 goto out;
418
419 length = -EINVAL;
420 if (sscanf(page, "%d", &new_value) != 1)
421 goto out;
422
423 selinux_compat_net = new_value ? 1 : 0;
424 length = count;
425out:
426 free_page((unsigned long) page);
427 return length;
428}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800429static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700430 .read = sel_read_compat_net,
431 .write = sel_write_compat_net,
432};
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
436 */
437static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
438static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
439static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
440static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
441static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
442
443static ssize_t (*write_op[])(struct file *, char *, size_t) = {
444 [SEL_ACCESS] = sel_write_access,
445 [SEL_CREATE] = sel_write_create,
446 [SEL_RELABEL] = sel_write_relabel,
447 [SEL_USER] = sel_write_user,
448 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800449 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450};
451
452static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
453{
Josef Sipek3d5ff522006-12-08 02:37:38 -0800454 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 char *data;
456 ssize_t rv;
457
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800458 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EINVAL;
460
461 data = simple_transaction_get(file, buf, size);
462 if (IS_ERR(data))
463 return PTR_ERR(data);
464
465 rv = write_op[ino](file, data, size);
466 if (rv>0) {
467 simple_transaction_set(file, rv);
468 rv = size;
469 }
470 return rv;
471}
472
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800473static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .write = selinux_transaction_write,
475 .read = simple_transaction_read,
476 .release = simple_transaction_release,
477};
478
479/*
480 * payload - write methods
481 * If the method has a response, the response should be put in buf,
482 * and the length returned. Otherwise return 0 or and -error.
483 */
484
485static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
486{
487 char *scon, *tcon;
488 u32 ssid, tsid;
489 u16 tclass;
490 u32 req;
491 struct av_decision avd;
492 ssize_t length;
493
494 length = task_has_security(current, SECURITY__COMPUTE_AV);
495 if (length)
496 return length;
497
498 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800499 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!scon)
501 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
James Morris89d155e2005-10-30 14:59:21 -0800503 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!tcon)
505 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 length = -EINVAL;
508 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
509 goto out2;
510
511 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
512 if (length < 0)
513 goto out2;
514 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
515 if (length < 0)
516 goto out2;
517
518 length = security_compute_av(ssid, tsid, tclass, req, &avd);
519 if (length < 0)
520 goto out2;
521
522 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
523 "%x %x %x %x %u",
524 avd.allowed, avd.decided,
525 avd.auditallow, avd.auditdeny,
526 avd.seqno);
527out2:
528 kfree(tcon);
529out:
530 kfree(scon);
531 return length;
532}
533
534static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
535{
536 char *scon, *tcon;
537 u32 ssid, tsid, newsid;
538 u16 tclass;
539 ssize_t length;
540 char *newcon;
541 u32 len;
542
543 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
544 if (length)
545 return length;
546
547 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800548 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!scon)
550 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
James Morris89d155e2005-10-30 14:59:21 -0800552 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!tcon)
554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 length = -EINVAL;
557 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
558 goto out2;
559
560 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
561 if (length < 0)
562 goto out2;
563 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
564 if (length < 0)
565 goto out2;
566
567 length = security_transition_sid(ssid, tsid, tclass, &newsid);
568 if (length < 0)
569 goto out2;
570
571 length = security_sid_to_context(newsid, &newcon, &len);
572 if (length < 0)
573 goto out2;
574
575 if (len > SIMPLE_TRANSACTION_LIMIT) {
576 printk(KERN_ERR "%s: context size (%u) exceeds payload "
577 "max\n", __FUNCTION__, len);
578 length = -ERANGE;
579 goto out3;
580 }
581
582 memcpy(buf, newcon, len);
583 length = len;
584out3:
585 kfree(newcon);
586out2:
587 kfree(tcon);
588out:
589 kfree(scon);
590 return length;
591}
592
593static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
594{
595 char *scon, *tcon;
596 u32 ssid, tsid, newsid;
597 u16 tclass;
598 ssize_t length;
599 char *newcon;
600 u32 len;
601
602 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
603 if (length)
604 return length;
605
606 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800607 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!scon)
609 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
James Morris89d155e2005-10-30 14:59:21 -0800611 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!tcon)
613 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 length = -EINVAL;
616 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
617 goto out2;
618
619 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
620 if (length < 0)
621 goto out2;
622 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
623 if (length < 0)
624 goto out2;
625
626 length = security_change_sid(ssid, tsid, tclass, &newsid);
627 if (length < 0)
628 goto out2;
629
630 length = security_sid_to_context(newsid, &newcon, &len);
631 if (length < 0)
632 goto out2;
633
634 if (len > SIMPLE_TRANSACTION_LIMIT) {
635 length = -ERANGE;
636 goto out3;
637 }
638
639 memcpy(buf, newcon, len);
640 length = len;
641out3:
642 kfree(newcon);
643out2:
644 kfree(tcon);
645out:
646 kfree(scon);
647 return length;
648}
649
650static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
651{
652 char *con, *user, *ptr;
653 u32 sid, *sids;
654 ssize_t length;
655 char *newcon;
656 int i, rc;
657 u32 len, nsids;
658
659 length = task_has_security(current, SECURITY__COMPUTE_USER);
660 if (length)
661 return length;
662
663 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800664 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!con)
666 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
James Morris89d155e2005-10-30 14:59:21 -0800668 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!user)
670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 length = -EINVAL;
673 if (sscanf(buf, "%s %s", con, user) != 2)
674 goto out2;
675
676 length = security_context_to_sid(con, strlen(con)+1, &sid);
677 if (length < 0)
678 goto out2;
679
680 length = security_get_user_sids(sid, user, &sids, &nsids);
681 if (length < 0)
682 goto out2;
683
684 length = sprintf(buf, "%u", nsids) + 1;
685 ptr = buf + length;
686 for (i = 0; i < nsids; i++) {
687 rc = security_sid_to_context(sids[i], &newcon, &len);
688 if (rc) {
689 length = rc;
690 goto out3;
691 }
692 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
693 kfree(newcon);
694 length = -ERANGE;
695 goto out3;
696 }
697 memcpy(ptr, newcon, len);
698 kfree(newcon);
699 ptr += len;
700 length += len;
701 }
702out3:
703 kfree(sids);
704out2:
705 kfree(user);
706out:
707 kfree(con);
708 return length;
709}
710
711static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
712{
713 char *scon, *tcon;
714 u32 ssid, tsid, newsid;
715 u16 tclass;
716 ssize_t length;
717 char *newcon;
718 u32 len;
719
720 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
721 if (length)
722 return length;
723
724 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800725 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!scon)
727 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
James Morris89d155e2005-10-30 14:59:21 -0800729 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!tcon)
731 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 length = -EINVAL;
734 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
735 goto out2;
736
737 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
738 if (length < 0)
739 goto out2;
740 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
741 if (length < 0)
742 goto out2;
743
744 length = security_member_sid(ssid, tsid, tclass, &newsid);
745 if (length < 0)
746 goto out2;
747
748 length = security_sid_to_context(newsid, &newcon, &len);
749 if (length < 0)
750 goto out2;
751
752 if (len > SIMPLE_TRANSACTION_LIMIT) {
753 printk(KERN_ERR "%s: context size (%u) exceeds payload "
754 "max\n", __FUNCTION__, len);
755 length = -ERANGE;
756 goto out3;
757 }
758
759 memcpy(buf, newcon, len);
760 length = len;
761out3:
762 kfree(newcon);
763out2:
764 kfree(tcon);
765out:
766 kfree(scon);
767 return length;
768}
769
770static struct inode *sel_make_inode(struct super_block *sb, int mode)
771{
772 struct inode *ret = new_inode(sb);
773
774 if (ret) {
775 ret->i_mode = mode;
776 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ret->i_blocks = 0;
778 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
779 }
780 return ret;
781}
782
783#define BOOL_INO_OFFSET 30
784
785static ssize_t sel_read_bool(struct file *filep, char __user *buf,
786 size_t count, loff_t *ppos)
787{
788 char *page = NULL;
789 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ssize_t ret;
791 int cur_enforcing;
792 struct inode *inode;
793
Ingo Molnarbb003072006-03-22 00:09:14 -0800794 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 ret = -EFAULT;
797
798 /* check to see if this file has been deleted */
799 if (!filep->f_op)
800 goto out;
801
Davi Arnautbfd51622005-10-30 14:59:24 -0800802 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 ret = -EINVAL;
804 goto out;
805 }
806 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
807 ret = -ENOMEM;
808 goto out;
809 }
810
Josef Sipek3d5ff522006-12-08 02:37:38 -0800811 inode = filep->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
813 if (cur_enforcing < 0) {
814 ret = cur_enforcing;
815 goto out;
816 }
817
818 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
819 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800820 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800822 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (page)
824 free_page((unsigned long)page);
825 return ret;
826}
827
828static ssize_t sel_write_bool(struct file *filep, 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 struct inode *inode;
835
Ingo Molnarbb003072006-03-22 00:09:14 -0800836 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 length = task_has_security(current, SECURITY__SETBOOL);
839 if (length)
840 goto out;
841
842 /* check to see if this file has been deleted */
843 if (!filep->f_op)
844 goto out;
845
Davi Arnautbfd51622005-10-30 14:59:24 -0800846 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 length = -ENOMEM;
848 goto out;
849 }
850 if (*ppos != 0) {
851 /* No partial writes. */
852 goto out;
853 }
854 page = (char*)get_zeroed_page(GFP_KERNEL);
855 if (!page) {
856 length = -ENOMEM;
857 goto out;
858 }
859
860 if (copy_from_user(page, buf, count))
861 goto out;
862
863 length = -EINVAL;
864 if (sscanf(page, "%d", &new_value) != 1)
865 goto out;
866
867 if (new_value)
868 new_value = 1;
869
Josef Sipek3d5ff522006-12-08 02:37:38 -0800870 inode = filep->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
872 length = count;
873
874out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800875 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (page)
877 free_page((unsigned long) page);
878 return length;
879}
880
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800881static const struct file_operations sel_bool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 .read = sel_read_bool,
883 .write = sel_write_bool,
884};
885
886static ssize_t sel_commit_bools_write(struct file *filep,
887 const char __user *buf,
888 size_t count, loff_t *ppos)
889{
890 char *page = NULL;
891 ssize_t length = -EFAULT;
892 int new_value;
893
Ingo Molnarbb003072006-03-22 00:09:14 -0800894 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 length = task_has_security(current, SECURITY__SETBOOL);
897 if (length)
898 goto out;
899
900 /* check to see if this file has been deleted */
901 if (!filep->f_op)
902 goto out;
903
Davi Arnautbfd51622005-10-30 14:59:24 -0800904 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 length = -ENOMEM;
906 goto out;
907 }
908 if (*ppos != 0) {
909 /* No partial writes. */
910 goto out;
911 }
912 page = (char*)get_zeroed_page(GFP_KERNEL);
913 if (!page) {
914 length = -ENOMEM;
915 goto out;
916 }
917
918 if (copy_from_user(page, buf, count))
919 goto out;
920
921 length = -EINVAL;
922 if (sscanf(page, "%d", &new_value) != 1)
923 goto out;
924
Davi Arnaut20c19e42005-10-23 12:57:16 -0700925 if (new_value && bool_pending_values) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 security_set_bools(bool_num, bool_pending_values);
927 }
928
929 length = count;
930
931out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800932 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (page)
934 free_page((unsigned long) page);
935 return length;
936}
937
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800938static const struct file_operations sel_commit_bools_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 .write = sel_commit_bools_write,
940};
941
942/* delete booleans - partial revoke() from
943 * fs/proc/generic.c proc_kill_inodes */
944static void sel_remove_bools(struct dentry *de)
945{
946 struct list_head *p, *node;
947 struct super_block *sb = de->d_sb;
948
949 spin_lock(&dcache_lock);
950 node = de->d_subdirs.next;
951 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800952 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 list_del_init(node);
954
955 if (d->d_inode) {
956 d = dget_locked(d);
957 spin_unlock(&dcache_lock);
958 d_delete(d);
959 simple_unlink(de->d_inode, d);
960 dput(d);
961 spin_lock(&dcache_lock);
962 }
963 node = de->d_subdirs.next;
964 }
965
966 spin_unlock(&dcache_lock);
967
968 file_list_lock();
969 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800970 struct file * filp = list_entry(p, struct file, f_u.fu_list);
Josef Sipek3d5ff522006-12-08 02:37:38 -0800971 struct dentry * dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (dentry->d_parent != de) {
974 continue;
975 }
976 filp->f_op = NULL;
977 }
978 file_list_unlock();
979}
980
981#define BOOL_DIR_NAME "booleans"
982
983static int sel_make_bools(void)
984{
985 int i, ret = 0;
986 ssize_t len;
987 struct dentry *dentry = NULL;
988 struct dentry *dir = bool_dir;
989 struct inode *inode = NULL;
990 struct inode_security_struct *isec;
991 char **names = NULL, *page;
992 int num;
993 int *values = NULL;
994 u32 sid;
995
996 /* remove any existing files */
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700997 kfree(bool_pending_values);
Davi Arnaut20c19e42005-10-23 12:57:16 -0700998 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 sel_remove_bools(dir);
1001
1002 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1003 return -ENOMEM;
1004
1005 ret = security_get_bools(&num, &names, &values);
1006 if (ret != 0)
1007 goto out;
1008
1009 for (i = 0; i < num; i++) {
1010 dentry = d_alloc_name(dir, names[i]);
1011 if (!dentry) {
1012 ret = -ENOMEM;
1013 goto err;
1014 }
1015 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1016 if (!inode) {
1017 ret = -ENOMEM;
1018 goto err;
1019 }
1020
1021 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1022 if (len < 0) {
1023 ret = -EINVAL;
1024 goto err;
1025 } else if (len >= PAGE_SIZE) {
1026 ret = -ENAMETOOLONG;
1027 goto err;
1028 }
1029 isec = (struct inode_security_struct*)inode->i_security;
1030 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1031 goto err;
1032 isec->sid = sid;
1033 isec->initialized = 1;
1034 inode->i_fop = &sel_bool_ops;
1035 inode->i_ino = i + BOOL_INO_OFFSET;
1036 d_add(dentry, inode);
1037 }
1038 bool_num = num;
1039 bool_pending_values = values;
1040out:
1041 free_page((unsigned long)page);
1042 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001043 for (i = 0; i < num; i++)
1044 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 kfree(names);
1046 }
1047 return ret;
1048err:
Davi Arnaut20c19e42005-10-23 12:57:16 -07001049 kfree(values);
James Morris253a8b12006-03-22 00:09:18 -08001050 sel_remove_bools(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ret = -ENOMEM;
1052 goto out;
1053}
1054
1055#define NULL_FILE_NAME "null"
1056
1057struct dentry *selinux_null = NULL;
1058
1059static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1060 size_t count, loff_t *ppos)
1061{
1062 char tmpbuf[TMPBUFLEN];
1063 ssize_t length;
1064
1065 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1066 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1067}
1068
1069static ssize_t sel_write_avc_cache_threshold(struct file * file,
1070 const char __user * buf,
1071 size_t count, loff_t *ppos)
1072
1073{
1074 char *page;
1075 ssize_t ret;
1076 int new_value;
1077
Davi Arnautbfd51622005-10-30 14:59:24 -08001078 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = -ENOMEM;
1080 goto out;
1081 }
1082
1083 if (*ppos != 0) {
1084 /* No partial writes. */
1085 ret = -EINVAL;
1086 goto out;
1087 }
1088
1089 page = (char*)get_zeroed_page(GFP_KERNEL);
1090 if (!page) {
1091 ret = -ENOMEM;
1092 goto out;
1093 }
1094
1095 if (copy_from_user(page, buf, count)) {
1096 ret = -EFAULT;
1097 goto out_free;
1098 }
1099
1100 if (sscanf(page, "%u", &new_value) != 1) {
1101 ret = -EINVAL;
1102 goto out;
1103 }
1104
1105 if (new_value != avc_cache_threshold) {
1106 ret = task_has_security(current, SECURITY__SETSECPARAM);
1107 if (ret)
1108 goto out_free;
1109 avc_cache_threshold = new_value;
1110 }
1111 ret = count;
1112out_free:
1113 free_page((unsigned long)page);
1114out:
1115 return ret;
1116}
1117
1118static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1119 size_t count, loff_t *ppos)
1120{
1121 char *page;
1122 ssize_t ret = 0;
1123
1124 page = (char *)__get_free_page(GFP_KERNEL);
1125 if (!page) {
1126 ret = -ENOMEM;
1127 goto out;
1128 }
1129 ret = avc_get_hash_stats(page);
1130 if (ret >= 0)
1131 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1132 free_page((unsigned long)page);
1133out:
1134 return ret;
1135}
1136
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001137static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 .read = sel_read_avc_cache_threshold,
1139 .write = sel_write_avc_cache_threshold,
1140};
1141
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001142static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 .read = sel_read_avc_hash_stats,
1144};
1145
1146#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1147static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1148{
1149 int cpu;
1150
1151 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1152 if (!cpu_possible(cpu))
1153 continue;
1154 *idx = cpu + 1;
1155 return &per_cpu(avc_cache_stats, cpu);
1156 }
1157 return NULL;
1158}
1159
1160static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1161{
1162 loff_t n = *pos - 1;
1163
1164 if (*pos == 0)
1165 return SEQ_START_TOKEN;
1166
1167 return sel_avc_get_stat_idx(&n);
1168}
1169
1170static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1171{
1172 return sel_avc_get_stat_idx(pos);
1173}
1174
1175static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1176{
1177 struct avc_cache_stats *st = v;
1178
1179 if (v == SEQ_START_TOKEN)
1180 seq_printf(seq, "lookups hits misses allocations reclaims "
1181 "frees\n");
1182 else
1183 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1184 st->hits, st->misses, st->allocations,
1185 st->reclaims, st->frees);
1186 return 0;
1187}
1188
1189static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1190{ }
1191
1192static struct seq_operations sel_avc_cache_stats_seq_ops = {
1193 .start = sel_avc_stats_seq_start,
1194 .next = sel_avc_stats_seq_next,
1195 .show = sel_avc_stats_seq_show,
1196 .stop = sel_avc_stats_seq_stop,
1197};
1198
1199static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1200{
1201 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1202}
1203
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001204static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 .open = sel_open_avc_cache_stats,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1209};
1210#endif
1211
1212static int sel_make_avc_files(struct dentry *dir)
1213{
1214 int i, ret = 0;
1215 static struct tree_descr files[] = {
1216 { "cache_threshold",
1217 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1218 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1219#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1220 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1221#endif
1222 };
1223
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001224 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct inode *inode;
1226 struct dentry *dentry;
1227
1228 dentry = d_alloc_name(dir, files[i].name);
1229 if (!dentry) {
1230 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1235 if (!inode) {
1236 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 inode->i_fop = files[i].ops;
1240 d_add(dentry, inode);
1241 }
1242out:
1243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
James Carterf0ee2e42007-04-04 10:11:29 -04001246static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1247 size_t count, loff_t *ppos)
1248{
1249 struct inode *inode;
1250 char *con;
1251 u32 sid, len;
1252 ssize_t ret;
1253
1254 inode = file->f_path.dentry->d_inode;
1255 sid = inode->i_ino&SEL_INO_MASK;
1256 ret = security_sid_to_context(sid, &con, &len);
1257 if (ret < 0)
1258 return ret;
1259
1260 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1261 kfree(con);
1262 return ret;
1263}
1264
1265static const struct file_operations sel_initcon_ops = {
1266 .read = sel_read_initcon,
1267};
1268
1269static int sel_make_initcon_files(struct dentry *dir)
1270{
1271 int i, ret = 0;
1272
1273 for (i = 1; i <= SECINITSID_NUM; i++) {
1274 struct inode *inode;
1275 struct dentry *dentry;
1276 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1277 if (!dentry) {
1278 ret = -ENOMEM;
1279 goto out;
1280 }
1281
1282 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1283 if (!inode) {
1284 ret = -ENOMEM;
1285 goto out;
1286 }
1287 inode->i_fop = &sel_initcon_ops;
1288 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1289 d_add(dentry, inode);
1290 }
1291out:
1292 return ret;
1293}
1294
James Morrisedb20fb2006-03-22 00:09:20 -08001295static int sel_make_dir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 int ret = 0;
1298 struct inode *inode;
1299
James Morrisedb20fb2006-03-22 00:09:20 -08001300 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!inode) {
1302 ret = -ENOMEM;
1303 goto out;
1304 }
1305 inode->i_op = &simple_dir_inode_operations;
1306 inode->i_fop = &simple_dir_operations;
James Morris40e906f2006-03-22 00:09:16 -08001307 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001308 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001310 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001311 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312out:
1313 return ret;
1314}
1315
1316static int sel_fill_super(struct super_block * sb, void * data, int silent)
1317{
1318 int ret;
1319 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001320 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct inode_security_struct *isec;
1322
1323 static struct tree_descr selinux_files[] = {
1324 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1325 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001326 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1328 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1329 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1330 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1331 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1332 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1333 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1334 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1335 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1336 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001337 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* last one */ {""}
1339 };
1340 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1341 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001342 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
James Morrisedb20fb2006-03-22 00:09:20 -08001344 root_inode = sb->s_root->d_inode;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001347 if (!dentry) {
1348 ret = -ENOMEM;
1349 goto err;
1350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
James Morrisedb20fb2006-03-22 00:09:20 -08001352 ret = sel_make_dir(root_inode, dentry);
James Morriscde174a2006-03-22 00:09:17 -08001353 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001354 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001359 if (!dentry) {
1360 ret = -ENOMEM;
1361 goto err;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001365 if (!inode) {
1366 ret = -ENOMEM;
1367 goto err;
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 isec = (struct inode_security_struct*)inode->i_security;
1370 isec->sid = SECINITSID_DEVNULL;
1371 isec->sclass = SECCLASS_CHR_FILE;
1372 isec->initialized = 1;
1373
1374 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1375 d_add(dentry, inode);
1376 selinux_null = dentry;
1377
1378 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001379 if (!dentry) {
1380 ret = -ENOMEM;
1381 goto err;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
James Morrisedb20fb2006-03-22 00:09:20 -08001384 ret = sel_make_dir(root_inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001386 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 ret = sel_make_avc_files(dentry);
1389 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001390 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001391
1392 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1393 if (!dentry) {
1394 ret = -ENOMEM;
1395 goto err;
1396 }
1397
1398 ret = sel_make_dir(root_inode, dentry);
1399 if (ret)
1400 goto err;
1401
1402 ret = sel_make_initcon_files(dentry);
1403 if (ret)
1404 goto err;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406out:
James Morris161ce452006-03-22 00:09:17 -08001407 return ret;
1408err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
James Morris161ce452006-03-22 00:09:17 -08001410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
David Howells454e2392006-06-23 02:02:57 -07001413static int sel_get_sb(struct file_system_type *fs_type,
1414 int flags, const char *dev_name, void *data,
1415 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
David Howells454e2392006-06-23 02:02:57 -07001417 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static struct file_system_type sel_fs_type = {
1421 .name = "selinuxfs",
1422 .get_sb = sel_get_sb,
1423 .kill_sb = kill_litter_super,
1424};
1425
1426struct vfsmount *selinuxfs_mount;
1427
1428static int __init init_sel_fs(void)
1429{
1430 int err;
1431
1432 if (!selinux_enabled)
1433 return 0;
1434 err = register_filesystem(&sel_fs_type);
1435 if (!err) {
1436 selinuxfs_mount = kern_mount(&sel_fs_type);
1437 if (IS_ERR(selinuxfs_mount)) {
1438 printk(KERN_ERR "selinuxfs: could not mount!\n");
1439 err = PTR_ERR(selinuxfs_mount);
1440 selinuxfs_mount = NULL;
1441 }
1442 }
1443 return err;
1444}
1445
1446__initcall(init_sel_fs);
1447
1448#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1449void exit_sel_fs(void)
1450{
1451 unregister_filesystem(&sel_fs_type);
1452}
1453#endif