blob: 0341567665b3b32eab4ac7bfa717c5c0ec00d908 [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 *
Paul Moore3bb56b22008-01-29 08:38:19 -05005 * Updated: Hewlett-Packard <paul.moore@hp.com>
6 *
7 * Added support for the policy capability bitmap
8 *
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 2.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Steve Grubbaf601e42006-01-04 14:08:39 +000029#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/semaphore.h>
32
33/* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
35
36#include "flask.h"
37#include "avc.h"
38#include "avc_ss.h"
39#include "security.h"
40#include "objsec.h"
41#include "conditional.h"
42
Paul Moore3bb56b22008-01-29 08:38:19 -050043/* Policy capability filenames */
44static char *policycap_names[] = {
45 "network_peer_controls"
46};
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
49
James Morris4e5ab4c2006-06-09 00:33:33 -070050#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int __init checkreqprot_setup(char *str)
59{
60 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
61 return 1;
62}
63__setup("checkreqprot=", checkreqprot_setup);
64
James Morris4e5ab4c2006-06-09 00:33:33 -070065static int __init selinux_compat_net_setup(char *str)
66{
67 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
68 return 1;
69}
70__setup("selinux_compat_net=", selinux_compat_net_setup);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Ingo Molnarbb003072006-03-22 00:09:14 -080073static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* global data for booleans */
76static struct dentry *bool_dir = NULL;
77static int bool_num = 0;
Stephen Smalleyd313f94832007-11-26 11:12:53 -050078static char **bool_pending_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int *bool_pending_values = NULL;
80
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040081/* global data for classes */
82static struct dentry *class_dir = NULL;
83static unsigned long last_class_ino;
84
Paul Moore3bb56b22008-01-29 08:38:19 -050085/* global data for policy capabilities */
86static struct dentry *policycap_dir = NULL;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088extern void selnl_notify_setenforce(int val);
89
90/* Check whether a task is allowed to use a security operation. */
91static int task_has_security(struct task_struct *tsk,
92 u32 perms)
93{
94 struct task_security_struct *tsec;
95
96 tsec = tsk->security;
97 if (!tsec)
98 return -EACCES;
99
100 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
101 SECCLASS_SECURITY, perms, NULL);
102}
103
104enum sel_inos {
105 SEL_ROOT_INO = 2,
106 SEL_LOAD, /* load policy */
107 SEL_ENFORCE, /* get or set enforcing status */
108 SEL_CONTEXT, /* validate context */
109 SEL_ACCESS, /* compute access decision */
110 SEL_CREATE, /* compute create labeling decision */
111 SEL_RELABEL, /* compute relabeling decision */
112 SEL_USER, /* compute reachable user contexts */
113 SEL_POLICYVERS, /* return policy version for this kernel */
114 SEL_COMMIT_BOOLS, /* commit new boolean values */
115 SEL_MLS, /* return if MLS policy is enabled */
116 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 SEL_MEMBER, /* compute polyinstantiation membership decision */
118 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700119 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400120 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
121 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
James Carter6174eaf2007-04-04 16:18:39 -0400122 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
James Carter6174eaf2007-04-04 16:18:39 -0400125static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
126
Paul Moore3bb56b22008-01-29 08:38:19 -0500127#define SEL_INITCON_INO_OFFSET 0x01000000
128#define SEL_BOOL_INO_OFFSET 0x02000000
129#define SEL_CLASS_INO_OFFSET 0x04000000
130#define SEL_POLICYCAP_INO_OFFSET 0x08000000
131#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TMPBUFLEN 12
134static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
135 size_t count, loff_t *ppos)
136{
137 char tmpbuf[TMPBUFLEN];
138 ssize_t length;
139
140 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
141 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
142}
143
144#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
145static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
146 size_t count, loff_t *ppos)
147
148{
149 char *page;
150 ssize_t length;
151 int new_value;
152
Davi Arnautbfd51622005-10-30 14:59:24 -0800153 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -ENOMEM;
155 if (*ppos != 0) {
156 /* No partial writes. */
157 return -EINVAL;
158 }
159 page = (char*)get_zeroed_page(GFP_KERNEL);
160 if (!page)
161 return -ENOMEM;
162 length = -EFAULT;
163 if (copy_from_user(page, buf, count))
164 goto out;
165
166 length = -EINVAL;
167 if (sscanf(page, "%d", &new_value) != 1)
168 goto out;
169
170 if (new_value != selinux_enforcing) {
171 length = task_has_security(current, SECURITY__SETENFORCE);
172 if (length)
173 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000174 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500175 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
176 new_value, selinux_enforcing,
177 audit_get_loginuid(current),
178 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 selinux_enforcing = new_value;
180 if (selinux_enforcing)
181 avc_ss_reset(0);
182 selnl_notify_setenforce(selinux_enforcing);
183 }
184 length = count;
185out:
186 free_page((unsigned long) page);
187 return length;
188}
189#else
190#define sel_write_enforce NULL
191#endif
192
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800193static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .read = sel_read_enforce,
195 .write = sel_write_enforce,
196};
197
Eric Paris3f120702007-09-21 14:37:10 -0400198static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
199 size_t count, loff_t *ppos)
200{
201 char tmpbuf[TMPBUFLEN];
202 ssize_t length;
203 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
204 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
205 security_get_reject_unknown() : !security_get_allow_unknown();
206
207 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
208 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
209}
210
211static const struct file_operations sel_handle_unknown_ops = {
212 .read = sel_read_handle_unknown,
213};
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#ifdef CONFIG_SECURITY_SELINUX_DISABLE
216static ssize_t sel_write_disable(struct file * file, const char __user * buf,
217 size_t count, loff_t *ppos)
218
219{
220 char *page;
221 ssize_t length;
222 int new_value;
223 extern int selinux_disable(void);
224
Davi Arnautbfd51622005-10-30 14:59:24 -0800225 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -ENOMEM;
227 if (*ppos != 0) {
228 /* No partial writes. */
229 return -EINVAL;
230 }
231 page = (char*)get_zeroed_page(GFP_KERNEL);
232 if (!page)
233 return -ENOMEM;
234 length = -EFAULT;
235 if (copy_from_user(page, buf, count))
236 goto out;
237
238 length = -EINVAL;
239 if (sscanf(page, "%d", &new_value) != 1)
240 goto out;
241
242 if (new_value) {
243 length = selinux_disable();
244 if (length < 0)
245 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000246 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500247 "selinux=0 auid=%u ses=%u",
248 audit_get_loginuid(current),
249 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 length = count;
253out:
254 free_page((unsigned long) page);
255 return length;
256}
257#else
258#define sel_write_disable NULL
259#endif
260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800261static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .write = sel_write_disable,
263};
264
265static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
266 size_t count, loff_t *ppos)
267{
268 char tmpbuf[TMPBUFLEN];
269 ssize_t length;
270
271 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
272 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
273}
274
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800275static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .read = sel_read_policyvers,
277};
278
279/* declaration for sel_write_load */
280static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400281static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500282static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400283
284/* declaration for sel_make_class_dirs */
285static int sel_make_dir(struct inode *dir, struct dentry *dentry,
286 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288static ssize_t sel_read_mls(struct file *filp, char __user *buf,
289 size_t count, loff_t *ppos)
290{
291 char tmpbuf[TMPBUFLEN];
292 ssize_t length;
293
294 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
295 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
296}
297
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800298static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .read = sel_read_mls,
300};
301
302static ssize_t sel_write_load(struct file * file, const char __user * buf,
303 size_t count, loff_t *ppos)
304
305{
306 int ret;
307 ssize_t length;
308 void *data = NULL;
309
Ingo Molnarbb003072006-03-22 00:09:14 -0800310 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 length = task_has_security(current, SECURITY__LOAD_POLICY);
313 if (length)
314 goto out;
315
316 if (*ppos != 0) {
317 /* No partial writes. */
318 length = -EINVAL;
319 goto out;
320 }
321
Davi Arnautbfd51622005-10-30 14:59:24 -0800322 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 || (data = vmalloc(count)) == NULL) {
324 length = -ENOMEM;
325 goto out;
326 }
327
328 length = -EFAULT;
329 if (copy_from_user(data, buf, count) != 0)
330 goto out;
331
332 length = security_load_policy(data, count);
333 if (length)
334 goto out;
335
336 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400337 if (ret) {
338 length = ret;
339 goto out1;
340 }
341
342 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500343 if (ret) {
344 length = ret;
345 goto out1;
346 }
347
348 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (ret)
350 length = ret;
351 else
352 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400353
354out1:
Eric Paris3f120702007-09-21 14:37:10 -0400355
356 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
357 (security_get_reject_unknown() ? "reject" :
358 (security_get_allow_unknown() ? "allow" : "deny")));
359
Steve Grubbaf601e42006-01-04 14:08:39 +0000360 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500361 "policy loaded auid=%u ses=%u",
362 audit_get_loginuid(current),
363 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800365 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 vfree(data);
367 return length;
368}
369
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800370static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .write = sel_write_load,
372};
373
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800374static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800376 char *canon;
377 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 ssize_t length;
379
380 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
381 if (length)
382 return length;
383
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800384 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800386 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800388 length = security_sid_to_context(sid, &canon, &len);
389 if (length < 0)
390 return length;
391
392 if (len > SIMPLE_TRANSACTION_LIMIT) {
393 printk(KERN_ERR "%s: context size (%u) exceeds payload "
394 "max\n", __FUNCTION__, len);
395 length = -ERANGE;
396 goto out;
397 }
398
399 memcpy(buf, canon, len);
400 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800402 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return length;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
407 size_t count, loff_t *ppos)
408{
409 char tmpbuf[TMPBUFLEN];
410 ssize_t length;
411
412 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
413 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
414}
415
416static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
417 size_t count, loff_t *ppos)
418{
419 char *page;
420 ssize_t length;
421 unsigned int new_value;
422
423 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
424 if (length)
425 return length;
426
Davi Arnautbfd51622005-10-30 14:59:24 -0800427 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ENOMEM;
429 if (*ppos != 0) {
430 /* No partial writes. */
431 return -EINVAL;
432 }
433 page = (char*)get_zeroed_page(GFP_KERNEL);
434 if (!page)
435 return -ENOMEM;
436 length = -EFAULT;
437 if (copy_from_user(page, buf, count))
438 goto out;
439
440 length = -EINVAL;
441 if (sscanf(page, "%u", &new_value) != 1)
442 goto out;
443
444 selinux_checkreqprot = new_value ? 1 : 0;
445 length = count;
446out:
447 free_page((unsigned long) page);
448 return length;
449}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800450static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .read = sel_read_checkreqprot,
452 .write = sel_write_checkreqprot,
453};
454
James Morris4e5ab4c2006-06-09 00:33:33 -0700455static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
456 size_t count, loff_t *ppos)
457{
458 char tmpbuf[TMPBUFLEN];
459 ssize_t length;
460
461 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
462 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
463}
464
465static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
466 size_t count, loff_t *ppos)
467{
468 char *page;
469 ssize_t length;
470 int new_value;
471
472 length = task_has_security(current, SECURITY__LOAD_POLICY);
473 if (length)
474 return length;
475
476 if (count >= PAGE_SIZE)
477 return -ENOMEM;
478 if (*ppos != 0) {
479 /* No partial writes. */
480 return -EINVAL;
481 }
482 page = (char*)get_zeroed_page(GFP_KERNEL);
483 if (!page)
484 return -ENOMEM;
485 length = -EFAULT;
486 if (copy_from_user(page, buf, count))
487 goto out;
488
489 length = -EINVAL;
490 if (sscanf(page, "%d", &new_value) != 1)
491 goto out;
492
493 selinux_compat_net = new_value ? 1 : 0;
494 length = count;
495out:
496 free_page((unsigned long) page);
497 return length;
498}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800499static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700500 .read = sel_read_compat_net,
501 .write = sel_write_compat_net,
502};
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
505 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
506 */
507static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
508static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
509static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
510static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
511static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
512
513static ssize_t (*write_op[])(struct file *, char *, size_t) = {
514 [SEL_ACCESS] = sel_write_access,
515 [SEL_CREATE] = sel_write_create,
516 [SEL_RELABEL] = sel_write_relabel,
517 [SEL_USER] = sel_write_user,
518 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800519 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520};
521
522static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
523{
Josef Sipek3d5ff522006-12-08 02:37:38 -0800524 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 char *data;
526 ssize_t rv;
527
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800528 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EINVAL;
530
531 data = simple_transaction_get(file, buf, size);
532 if (IS_ERR(data))
533 return PTR_ERR(data);
534
535 rv = write_op[ino](file, data, size);
536 if (rv>0) {
537 simple_transaction_set(file, rv);
538 rv = size;
539 }
540 return rv;
541}
542
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800543static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 .write = selinux_transaction_write,
545 .read = simple_transaction_read,
546 .release = simple_transaction_release,
547};
548
549/*
550 * payload - write methods
551 * If the method has a response, the response should be put in buf,
552 * and the length returned. Otherwise return 0 or and -error.
553 */
554
555static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
556{
557 char *scon, *tcon;
558 u32 ssid, tsid;
559 u16 tclass;
560 u32 req;
561 struct av_decision avd;
562 ssize_t length;
563
564 length = task_has_security(current, SECURITY__COMPUTE_AV);
565 if (length)
566 return length;
567
568 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800569 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!scon)
571 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
James Morris89d155e2005-10-30 14:59:21 -0800573 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!tcon)
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 length = -EINVAL;
578 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
579 goto out2;
580
581 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
582 if (length < 0)
583 goto out2;
584 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
585 if (length < 0)
586 goto out2;
587
588 length = security_compute_av(ssid, tsid, tclass, req, &avd);
589 if (length < 0)
590 goto out2;
591
592 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
593 "%x %x %x %x %u",
594 avd.allowed, avd.decided,
595 avd.auditallow, avd.auditdeny,
596 avd.seqno);
597out2:
598 kfree(tcon);
599out:
600 kfree(scon);
601 return length;
602}
603
604static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
605{
606 char *scon, *tcon;
607 u32 ssid, tsid, newsid;
608 u16 tclass;
609 ssize_t length;
610 char *newcon;
611 u32 len;
612
613 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
614 if (length)
615 return length;
616
617 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800618 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!scon)
620 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
James Morris89d155e2005-10-30 14:59:21 -0800622 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!tcon)
624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 length = -EINVAL;
627 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
628 goto out2;
629
630 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
631 if (length < 0)
632 goto out2;
633 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
634 if (length < 0)
635 goto out2;
636
637 length = security_transition_sid(ssid, tsid, tclass, &newsid);
638 if (length < 0)
639 goto out2;
640
641 length = security_sid_to_context(newsid, &newcon, &len);
642 if (length < 0)
643 goto out2;
644
645 if (len > SIMPLE_TRANSACTION_LIMIT) {
646 printk(KERN_ERR "%s: context size (%u) exceeds payload "
647 "max\n", __FUNCTION__, len);
648 length = -ERANGE;
649 goto out3;
650 }
651
652 memcpy(buf, newcon, len);
653 length = len;
654out3:
655 kfree(newcon);
656out2:
657 kfree(tcon);
658out:
659 kfree(scon);
660 return length;
661}
662
663static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
664{
665 char *scon, *tcon;
666 u32 ssid, tsid, newsid;
667 u16 tclass;
668 ssize_t length;
669 char *newcon;
670 u32 len;
671
672 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
673 if (length)
674 return length;
675
676 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800677 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!scon)
679 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
James Morris89d155e2005-10-30 14:59:21 -0800681 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!tcon)
683 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 length = -EINVAL;
686 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
687 goto out2;
688
689 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
690 if (length < 0)
691 goto out2;
692 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
693 if (length < 0)
694 goto out2;
695
696 length = security_change_sid(ssid, tsid, tclass, &newsid);
697 if (length < 0)
698 goto out2;
699
700 length = security_sid_to_context(newsid, &newcon, &len);
701 if (length < 0)
702 goto out2;
703
704 if (len > SIMPLE_TRANSACTION_LIMIT) {
705 length = -ERANGE;
706 goto out3;
707 }
708
709 memcpy(buf, newcon, len);
710 length = len;
711out3:
712 kfree(newcon);
713out2:
714 kfree(tcon);
715out:
716 kfree(scon);
717 return length;
718}
719
720static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
721{
722 char *con, *user, *ptr;
723 u32 sid, *sids;
724 ssize_t length;
725 char *newcon;
726 int i, rc;
727 u32 len, nsids;
728
729 length = task_has_security(current, SECURITY__COMPUTE_USER);
730 if (length)
731 return length;
732
733 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800734 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!con)
736 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
James Morris89d155e2005-10-30 14:59:21 -0800738 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!user)
740 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 length = -EINVAL;
743 if (sscanf(buf, "%s %s", con, user) != 2)
744 goto out2;
745
746 length = security_context_to_sid(con, strlen(con)+1, &sid);
747 if (length < 0)
748 goto out2;
749
750 length = security_get_user_sids(sid, user, &sids, &nsids);
751 if (length < 0)
752 goto out2;
753
754 length = sprintf(buf, "%u", nsids) + 1;
755 ptr = buf + length;
756 for (i = 0; i < nsids; i++) {
757 rc = security_sid_to_context(sids[i], &newcon, &len);
758 if (rc) {
759 length = rc;
760 goto out3;
761 }
762 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
763 kfree(newcon);
764 length = -ERANGE;
765 goto out3;
766 }
767 memcpy(ptr, newcon, len);
768 kfree(newcon);
769 ptr += len;
770 length += len;
771 }
772out3:
773 kfree(sids);
774out2:
775 kfree(user);
776out:
777 kfree(con);
778 return length;
779}
780
781static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
782{
783 char *scon, *tcon;
784 u32 ssid, tsid, newsid;
785 u16 tclass;
786 ssize_t length;
787 char *newcon;
788 u32 len;
789
790 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
791 if (length)
792 return length;
793
794 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800795 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!scon)
797 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
James Morris89d155e2005-10-30 14:59:21 -0800799 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!tcon)
801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 length = -EINVAL;
804 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
805 goto out2;
806
807 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
808 if (length < 0)
809 goto out2;
810 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
811 if (length < 0)
812 goto out2;
813
814 length = security_member_sid(ssid, tsid, tclass, &newsid);
815 if (length < 0)
816 goto out2;
817
818 length = security_sid_to_context(newsid, &newcon, &len);
819 if (length < 0)
820 goto out2;
821
822 if (len > SIMPLE_TRANSACTION_LIMIT) {
823 printk(KERN_ERR "%s: context size (%u) exceeds payload "
824 "max\n", __FUNCTION__, len);
825 length = -ERANGE;
826 goto out3;
827 }
828
829 memcpy(buf, newcon, len);
830 length = len;
831out3:
832 kfree(newcon);
833out2:
834 kfree(tcon);
835out:
836 kfree(scon);
837 return length;
838}
839
840static struct inode *sel_make_inode(struct super_block *sb, int mode)
841{
842 struct inode *ret = new_inode(sb);
843
844 if (ret) {
845 ret->i_mode = mode;
846 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ret->i_blocks = 0;
848 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
849 }
850 return ret;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static ssize_t sel_read_bool(struct file *filep, char __user *buf,
854 size_t count, loff_t *ppos)
855{
856 char *page = NULL;
857 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ssize_t ret;
859 int cur_enforcing;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500860 struct inode *inode = filep->f_path.dentry->d_inode;
861 unsigned index = inode->i_ino & SEL_INO_MASK;
862 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Ingo Molnarbb003072006-03-22 00:09:14 -0800864 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500866 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
867 ret = -EINVAL;
868 goto out;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Davi Arnautbfd51622005-10-30 14:59:24 -0800871 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ret = -EINVAL;
873 goto out;
874 }
875 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
876 ret = -ENOMEM;
877 goto out;
878 }
879
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500880 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (cur_enforcing < 0) {
882 ret = cur_enforcing;
883 goto out;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500886 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800887 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800889 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (page)
891 free_page((unsigned long)page);
892 return ret;
893}
894
895static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
896 size_t count, loff_t *ppos)
897{
898 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500899 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int new_value;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500901 struct inode *inode = filep->f_path.dentry->d_inode;
902 unsigned index = inode->i_ino & SEL_INO_MASK;
903 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Ingo Molnarbb003072006-03-22 00:09:14 -0800905 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 length = task_has_security(current, SECURITY__SETBOOL);
908 if (length)
909 goto out;
910
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500911 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
912 length = -EINVAL;
913 goto out;
914 }
915
Davi Arnautbfd51622005-10-30 14:59:24 -0800916 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 length = -ENOMEM;
918 goto out;
919 }
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (*ppos != 0) {
922 /* No partial writes. */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500923 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out;
925 }
926 page = (char*)get_zeroed_page(GFP_KERNEL);
927 if (!page) {
928 length = -ENOMEM;
929 goto out;
930 }
931
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500932 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (copy_from_user(page, buf, count))
934 goto out;
935
936 length = -EINVAL;
937 if (sscanf(page, "%d", &new_value) != 1)
938 goto out;
939
940 if (new_value)
941 new_value = 1;
942
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500943 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 length = count;
945
946out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800947 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (page)
949 free_page((unsigned long) page);
950 return length;
951}
952
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800953static const struct file_operations sel_bool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 .read = sel_read_bool,
955 .write = sel_write_bool,
956};
957
958static ssize_t sel_commit_bools_write(struct file *filep,
959 const char __user *buf,
960 size_t count, loff_t *ppos)
961{
962 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500963 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int new_value;
965
Ingo Molnarbb003072006-03-22 00:09:14 -0800966 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 length = task_has_security(current, SECURITY__SETBOOL);
969 if (length)
970 goto out;
971
Davi Arnautbfd51622005-10-30 14:59:24 -0800972 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 length = -ENOMEM;
974 goto out;
975 }
976 if (*ppos != 0) {
977 /* No partial writes. */
978 goto out;
979 }
980 page = (char*)get_zeroed_page(GFP_KERNEL);
981 if (!page) {
982 length = -ENOMEM;
983 goto out;
984 }
985
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500986 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (copy_from_user(page, buf, count))
988 goto out;
989
990 length = -EINVAL;
991 if (sscanf(page, "%d", &new_value) != 1)
992 goto out;
993
Davi Arnaut20c19e42005-10-23 12:57:16 -0700994 if (new_value && bool_pending_values) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 security_set_bools(bool_num, bool_pending_values);
996 }
997
998 length = count;
999
1000out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001001 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (page)
1003 free_page((unsigned long) page);
1004 return length;
1005}
1006
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001007static const struct file_operations sel_commit_bools_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .write = sel_commit_bools_write,
1009};
1010
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001011static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Stephen Smalley0955dc02007-11-21 09:01:36 -05001013 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 spin_lock(&dcache_lock);
1016 node = de->d_subdirs.next;
1017 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001018 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 list_del_init(node);
1020
1021 if (d->d_inode) {
1022 d = dget_locked(d);
1023 spin_unlock(&dcache_lock);
1024 d_delete(d);
1025 simple_unlink(de->d_inode, d);
1026 dput(d);
1027 spin_lock(&dcache_lock);
1028 }
1029 node = de->d_subdirs.next;
1030 }
1031
1032 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035#define BOOL_DIR_NAME "booleans"
1036
1037static int sel_make_bools(void)
1038{
1039 int i, ret = 0;
1040 ssize_t len;
1041 struct dentry *dentry = NULL;
1042 struct dentry *dir = bool_dir;
1043 struct inode *inode = NULL;
1044 struct inode_security_struct *isec;
1045 char **names = NULL, *page;
1046 int num;
1047 int *values = NULL;
1048 u32 sid;
1049
1050 /* remove any existing files */
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001051 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001052 kfree(bool_pending_values);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001053 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001054 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001056 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1059 return -ENOMEM;
1060
1061 ret = security_get_bools(&num, &names, &values);
1062 if (ret != 0)
1063 goto out;
1064
1065 for (i = 0; i < num; i++) {
1066 dentry = d_alloc_name(dir, names[i]);
1067 if (!dentry) {
1068 ret = -ENOMEM;
1069 goto err;
1070 }
1071 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1072 if (!inode) {
1073 ret = -ENOMEM;
1074 goto err;
1075 }
1076
1077 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1078 if (len < 0) {
1079 ret = -EINVAL;
1080 goto err;
1081 } else if (len >= PAGE_SIZE) {
1082 ret = -ENAMETOOLONG;
1083 goto err;
1084 }
1085 isec = (struct inode_security_struct*)inode->i_security;
1086 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1087 goto err;
1088 isec->sid = sid;
1089 isec->initialized = 1;
1090 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001091 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 d_add(dentry, inode);
1093 }
1094 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001095 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 bool_pending_values = values;
1097out:
1098 free_page((unsigned long)page);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001099 return ret;
1100err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001102 for (i = 0; i < num; i++)
1103 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 kfree(names);
1105 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001106 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001107 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ret = -ENOMEM;
1109 goto out;
1110}
1111
1112#define NULL_FILE_NAME "null"
1113
1114struct dentry *selinux_null = NULL;
1115
1116static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1117 size_t count, loff_t *ppos)
1118{
1119 char tmpbuf[TMPBUFLEN];
1120 ssize_t length;
1121
1122 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1123 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1124}
1125
1126static ssize_t sel_write_avc_cache_threshold(struct file * file,
1127 const char __user * buf,
1128 size_t count, loff_t *ppos)
1129
1130{
1131 char *page;
1132 ssize_t ret;
1133 int new_value;
1134
Davi Arnautbfd51622005-10-30 14:59:24 -08001135 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 ret = -ENOMEM;
1137 goto out;
1138 }
1139
1140 if (*ppos != 0) {
1141 /* No partial writes. */
1142 ret = -EINVAL;
1143 goto out;
1144 }
1145
1146 page = (char*)get_zeroed_page(GFP_KERNEL);
1147 if (!page) {
1148 ret = -ENOMEM;
1149 goto out;
1150 }
1151
1152 if (copy_from_user(page, buf, count)) {
1153 ret = -EFAULT;
1154 goto out_free;
1155 }
1156
1157 if (sscanf(page, "%u", &new_value) != 1) {
1158 ret = -EINVAL;
1159 goto out;
1160 }
1161
1162 if (new_value != avc_cache_threshold) {
1163 ret = task_has_security(current, SECURITY__SETSECPARAM);
1164 if (ret)
1165 goto out_free;
1166 avc_cache_threshold = new_value;
1167 }
1168 ret = count;
1169out_free:
1170 free_page((unsigned long)page);
1171out:
1172 return ret;
1173}
1174
1175static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1176 size_t count, loff_t *ppos)
1177{
1178 char *page;
1179 ssize_t ret = 0;
1180
1181 page = (char *)__get_free_page(GFP_KERNEL);
1182 if (!page) {
1183 ret = -ENOMEM;
1184 goto out;
1185 }
1186 ret = avc_get_hash_stats(page);
1187 if (ret >= 0)
1188 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1189 free_page((unsigned long)page);
1190out:
1191 return ret;
1192}
1193
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001194static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 .read = sel_read_avc_cache_threshold,
1196 .write = sel_write_avc_cache_threshold,
1197};
1198
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001199static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .read = sel_read_avc_hash_stats,
1201};
1202
1203#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1204static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1205{
1206 int cpu;
1207
1208 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1209 if (!cpu_possible(cpu))
1210 continue;
1211 *idx = cpu + 1;
1212 return &per_cpu(avc_cache_stats, cpu);
1213 }
1214 return NULL;
1215}
1216
1217static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1218{
1219 loff_t n = *pos - 1;
1220
1221 if (*pos == 0)
1222 return SEQ_START_TOKEN;
1223
1224 return sel_avc_get_stat_idx(&n);
1225}
1226
1227static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1228{
1229 return sel_avc_get_stat_idx(pos);
1230}
1231
1232static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1233{
1234 struct avc_cache_stats *st = v;
1235
1236 if (v == SEQ_START_TOKEN)
1237 seq_printf(seq, "lookups hits misses allocations reclaims "
1238 "frees\n");
1239 else
1240 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1241 st->hits, st->misses, st->allocations,
1242 st->reclaims, st->frees);
1243 return 0;
1244}
1245
1246static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1247{ }
1248
Jan Engelhardt1996a102008-01-23 00:02:58 +01001249static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 .start = sel_avc_stats_seq_start,
1251 .next = sel_avc_stats_seq_next,
1252 .show = sel_avc_stats_seq_show,
1253 .stop = sel_avc_stats_seq_stop,
1254};
1255
1256static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1257{
1258 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1259}
1260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001261static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .open = sel_open_avc_cache_stats,
1263 .read = seq_read,
1264 .llseek = seq_lseek,
1265 .release = seq_release,
1266};
1267#endif
1268
1269static int sel_make_avc_files(struct dentry *dir)
1270{
1271 int i, ret = 0;
1272 static struct tree_descr files[] = {
1273 { "cache_threshold",
1274 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1275 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1276#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1277 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1278#endif
1279 };
1280
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001281 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct inode *inode;
1283 struct dentry *dentry;
1284
1285 dentry = d_alloc_name(dir, files[i].name);
1286 if (!dentry) {
1287 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1292 if (!inode) {
1293 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001297 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 d_add(dentry, inode);
1299 }
1300out:
1301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
James Carterf0ee2e42007-04-04 10:11:29 -04001304static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1305 size_t count, loff_t *ppos)
1306{
1307 struct inode *inode;
1308 char *con;
1309 u32 sid, len;
1310 ssize_t ret;
1311
1312 inode = file->f_path.dentry->d_inode;
1313 sid = inode->i_ino&SEL_INO_MASK;
1314 ret = security_sid_to_context(sid, &con, &len);
1315 if (ret < 0)
1316 return ret;
1317
1318 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1319 kfree(con);
1320 return ret;
1321}
1322
1323static const struct file_operations sel_initcon_ops = {
1324 .read = sel_read_initcon,
1325};
1326
1327static int sel_make_initcon_files(struct dentry *dir)
1328{
1329 int i, ret = 0;
1330
1331 for (i = 1; i <= SECINITSID_NUM; i++) {
1332 struct inode *inode;
1333 struct dentry *dentry;
1334 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1335 if (!dentry) {
1336 ret = -ENOMEM;
1337 goto out;
1338 }
1339
1340 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1341 if (!inode) {
1342 ret = -ENOMEM;
1343 goto out;
1344 }
1345 inode->i_fop = &sel_initcon_ops;
1346 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1347 d_add(dentry, inode);
1348 }
1349out:
1350 return ret;
1351}
1352
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001353static inline unsigned int sel_div(unsigned long a, unsigned long b)
1354{
1355 return a / b - (a % b < 0);
1356}
1357
1358static inline unsigned long sel_class_to_ino(u16 class)
1359{
1360 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1361}
1362
1363static inline u16 sel_ino_to_class(unsigned long ino)
1364{
1365 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1366}
1367
1368static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1369{
1370 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1371}
1372
1373static inline u32 sel_ino_to_perm(unsigned long ino)
1374{
1375 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1376}
1377
1378static ssize_t sel_read_class(struct file * file, char __user *buf,
1379 size_t count, loff_t *ppos)
1380{
1381 ssize_t rc, len;
1382 char *page;
1383 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1384
1385 page = (char *)__get_free_page(GFP_KERNEL);
1386 if (!page) {
1387 rc = -ENOMEM;
1388 goto out;
1389 }
1390
1391 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1392 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1393 free_page((unsigned long)page);
1394out:
1395 return rc;
1396}
1397
1398static const struct file_operations sel_class_ops = {
1399 .read = sel_read_class,
1400};
1401
1402static ssize_t sel_read_perm(struct file * file, char __user *buf,
1403 size_t count, loff_t *ppos)
1404{
1405 ssize_t rc, len;
1406 char *page;
1407 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1408
1409 page = (char *)__get_free_page(GFP_KERNEL);
1410 if (!page) {
1411 rc = -ENOMEM;
1412 goto out;
1413 }
1414
1415 len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
1416 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1417 free_page((unsigned long)page);
1418out:
1419 return rc;
1420}
1421
1422static const struct file_operations sel_perm_ops = {
1423 .read = sel_read_perm,
1424};
1425
Paul Moore3bb56b22008-01-29 08:38:19 -05001426static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1427 size_t count, loff_t *ppos)
1428{
1429 int value;
1430 char tmpbuf[TMPBUFLEN];
1431 ssize_t length;
1432 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1433
1434 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1435 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1436
1437 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1438}
1439
1440static const struct file_operations sel_policycap_ops = {
1441 .read = sel_read_policycap,
1442};
1443
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001444static int sel_make_perm_files(char *objclass, int classvalue,
1445 struct dentry *dir)
1446{
1447 int i, rc = 0, nperms;
1448 char **perms;
1449
1450 rc = security_get_permissions(objclass, &perms, &nperms);
1451 if (rc)
1452 goto out;
1453
1454 for (i = 0; i < nperms; i++) {
1455 struct inode *inode;
1456 struct dentry *dentry;
1457
1458 dentry = d_alloc_name(dir, perms[i]);
1459 if (!dentry) {
1460 rc = -ENOMEM;
1461 goto out1;
1462 }
1463
1464 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1465 if (!inode) {
1466 rc = -ENOMEM;
1467 goto out1;
1468 }
1469 inode->i_fop = &sel_perm_ops;
1470 /* i+1 since perm values are 1-indexed */
1471 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1472 d_add(dentry, inode);
1473 }
1474
1475out1:
1476 for (i = 0; i < nperms; i++)
1477 kfree(perms[i]);
1478 kfree(perms);
1479out:
1480 return rc;
1481}
1482
1483static int sel_make_class_dir_entries(char *classname, int index,
1484 struct dentry *dir)
1485{
1486 struct dentry *dentry = NULL;
1487 struct inode *inode = NULL;
1488 int rc;
1489
1490 dentry = d_alloc_name(dir, "index");
1491 if (!dentry) {
1492 rc = -ENOMEM;
1493 goto out;
1494 }
1495
1496 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1497 if (!inode) {
1498 rc = -ENOMEM;
1499 goto out;
1500 }
1501
1502 inode->i_fop = &sel_class_ops;
1503 inode->i_ino = sel_class_to_ino(index);
1504 d_add(dentry, inode);
1505
1506 dentry = d_alloc_name(dir, "perms");
1507 if (!dentry) {
1508 rc = -ENOMEM;
1509 goto out;
1510 }
1511
1512 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1513 if (rc)
1514 goto out;
1515
1516 rc = sel_make_perm_files(classname, index, dentry);
1517
1518out:
1519 return rc;
1520}
1521
1522static void sel_remove_classes(void)
1523{
1524 struct list_head *class_node;
1525
1526 list_for_each(class_node, &class_dir->d_subdirs) {
1527 struct dentry *class_subdir = list_entry(class_node,
1528 struct dentry, d_u.d_child);
1529 struct list_head *class_subdir_node;
1530
1531 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1532 struct dentry *d = list_entry(class_subdir_node,
1533 struct dentry, d_u.d_child);
1534
1535 if (d->d_inode)
1536 if (d->d_inode->i_mode & S_IFDIR)
1537 sel_remove_entries(d);
1538 }
1539
1540 sel_remove_entries(class_subdir);
1541 }
1542
1543 sel_remove_entries(class_dir);
1544}
1545
1546static int sel_make_classes(void)
1547{
1548 int rc = 0, nclasses, i;
1549 char **classes;
1550
1551 /* delete any existing entries */
1552 sel_remove_classes();
1553
1554 rc = security_get_classes(&classes, &nclasses);
1555 if (rc < 0)
1556 goto out;
1557
1558 /* +2 since classes are 1-indexed */
1559 last_class_ino = sel_class_to_ino(nclasses+2);
1560
1561 for (i = 0; i < nclasses; i++) {
1562 struct dentry *class_name_dir;
1563
1564 class_name_dir = d_alloc_name(class_dir, classes[i]);
1565 if (!class_name_dir) {
1566 rc = -ENOMEM;
1567 goto out1;
1568 }
1569
1570 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1571 &last_class_ino);
1572 if (rc)
1573 goto out1;
1574
1575 /* i+1 since class values are 1-indexed */
1576 rc = sel_make_class_dir_entries(classes[i], i+1,
1577 class_name_dir);
1578 if (rc)
1579 goto out1;
1580 }
1581
1582out1:
1583 for (i = 0; i < nclasses; i++)
1584 kfree(classes[i]);
1585 kfree(classes);
1586out:
1587 return rc;
1588}
1589
Paul Moore3bb56b22008-01-29 08:38:19 -05001590static int sel_make_policycap(void)
1591{
1592 unsigned int iter;
1593 struct dentry *dentry = NULL;
1594 struct inode *inode = NULL;
1595
1596 sel_remove_entries(policycap_dir);
1597
1598 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1599 if (iter < ARRAY_SIZE(policycap_names))
1600 dentry = d_alloc_name(policycap_dir,
1601 policycap_names[iter]);
1602 else
1603 dentry = d_alloc_name(policycap_dir, "unknown");
1604
1605 if (dentry == NULL)
1606 return -ENOMEM;
1607
1608 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1609 if (inode == NULL)
1610 return -ENOMEM;
1611
1612 inode->i_fop = &sel_policycap_ops;
1613 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1614 d_add(dentry, inode);
1615 }
1616
1617 return 0;
1618}
1619
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001620static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1621 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 int ret = 0;
1624 struct inode *inode;
1625
James Morrisedb20fb2006-03-22 00:09:20 -08001626 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (!inode) {
1628 ret = -ENOMEM;
1629 goto out;
1630 }
1631 inode->i_op = &simple_dir_inode_operations;
1632 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001633 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001634 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001635 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001637 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001638 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639out:
1640 return ret;
1641}
1642
1643static int sel_fill_super(struct super_block * sb, void * data, int silent)
1644{
1645 int ret;
1646 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001647 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct inode_security_struct *isec;
1649
1650 static struct tree_descr selinux_files[] = {
1651 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1652 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001653 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1655 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1656 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1657 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1658 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1659 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1660 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1661 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1662 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1663 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001664 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001665 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1666 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* last one */ {""}
1668 };
1669 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1670 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001671 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
James Morrisedb20fb2006-03-22 00:09:20 -08001673 root_inode = sb->s_root->d_inode;
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001676 if (!dentry) {
1677 ret = -ENOMEM;
1678 goto err;
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001681 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001682 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001683 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001688 if (!dentry) {
1689 ret = -ENOMEM;
1690 goto err;
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001694 if (!inode) {
1695 ret = -ENOMEM;
1696 goto err;
1697 }
James Carter6174eaf2007-04-04 16:18:39 -04001698 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 isec = (struct inode_security_struct*)inode->i_security;
1700 isec->sid = SECINITSID_DEVNULL;
1701 isec->sclass = SECCLASS_CHR_FILE;
1702 isec->initialized = 1;
1703
1704 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1705 d_add(dentry, inode);
1706 selinux_null = dentry;
1707
1708 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001709 if (!dentry) {
1710 ret = -ENOMEM;
1711 goto err;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001714 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001716 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 ret = sel_make_avc_files(dentry);
1719 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001720 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001721
1722 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1723 if (!dentry) {
1724 ret = -ENOMEM;
1725 goto err;
1726 }
1727
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001728 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001729 if (ret)
1730 goto err;
1731
1732 ret = sel_make_initcon_files(dentry);
1733 if (ret)
1734 goto err;
1735
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001736 dentry = d_alloc_name(sb->s_root, "class");
1737 if (!dentry) {
1738 ret = -ENOMEM;
1739 goto err;
1740 }
1741
1742 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1743 if (ret)
1744 goto err;
1745
1746 class_dir = dentry;
1747
Paul Moore3bb56b22008-01-29 08:38:19 -05001748 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1749 if (!dentry) {
1750 ret = -ENOMEM;
1751 goto err;
1752 }
1753
1754 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1755 if (ret)
1756 goto err;
1757
1758 policycap_dir = dentry;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760out:
James Morris161ce452006-03-22 00:09:17 -08001761 return ret;
1762err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
James Morris161ce452006-03-22 00:09:17 -08001764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
David Howells454e2392006-06-23 02:02:57 -07001767static int sel_get_sb(struct file_system_type *fs_type,
1768 int flags, const char *dev_name, void *data,
1769 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
David Howells454e2392006-06-23 02:02:57 -07001771 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
1774static struct file_system_type sel_fs_type = {
1775 .name = "selinuxfs",
1776 .get_sb = sel_get_sb,
1777 .kill_sb = kill_litter_super,
1778};
1779
1780struct vfsmount *selinuxfs_mount;
1781
1782static int __init init_sel_fs(void)
1783{
1784 int err;
1785
1786 if (!selinux_enabled)
1787 return 0;
1788 err = register_filesystem(&sel_fs_type);
1789 if (!err) {
1790 selinuxfs_mount = kern_mount(&sel_fs_type);
1791 if (IS_ERR(selinuxfs_mount)) {
1792 printk(KERN_ERR "selinuxfs: could not mount!\n");
1793 err = PTR_ERR(selinuxfs_mount);
1794 selinuxfs_mount = NULL;
1795 }
1796 }
1797 return err;
1798}
1799
1800__initcall(init_sel_fs);
1801
1802#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1803void exit_sel_fs(void)
1804{
1805 unregister_filesystem(&sel_fs_type);
1806}
1807#endif