blob: 07a5db69571cd9a7d1e4dff65a3e1a59bb8b5f16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
Eric Paris18729812008-04-17 14:15:45 -04003 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Paul Moore3bb56b22008-01-29 08:38:19 -05005 * Updated: Hewlett-Packard <paul.moore@hp.com>
6 *
Eric Paris18729812008-04-17 14:15:45 -04007 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05008 *
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
Eric Paris18729812008-04-17 14:15:45 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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>
Eric Parisf5269712008-05-14 11:27:45 -040030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* selinuxfs pseudo filesystem for exporting the security policy API.
33 Based on the proc code and the fs/nfsd/nfsctl.c code. */
34
35#include "flask.h"
36#include "avc.h"
37#include "avc_ss.h"
38#include "security.h"
39#include "objsec.h"
40#include "conditional.h"
41
Paul Moore3bb56b22008-01-29 08:38:19 -050042/* Policy capability filenames */
43static char *policycap_names[] = {
Eric Parisb0c636b2008-02-28 12:58:40 -050044 "network_peer_controls",
45 "open_perms"
Paul Moore3bb56b22008-01-29 08:38:19 -050046};
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{
Eric Parisf5269712008-05-14 11:27:45 -040060 unsigned long checkreqprot;
61 if (!strict_strtoul(str, 0, &checkreqprot))
62 selinux_checkreqprot = checkreqprot ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return 1;
64}
65__setup("checkreqprot=", checkreqprot_setup);
66
James Morris4e5ab4c2006-06-09 00:33:33 -070067static int __init selinux_compat_net_setup(char *str)
68{
Eric Parisf5269712008-05-14 11:27:45 -040069 unsigned long compat_net;
70 if (!strict_strtoul(str, 0, &compat_net))
71 selinux_compat_net = compat_net ? 1 : 0;
James Morris4e5ab4c2006-06-09 00:33:33 -070072 return 1;
73}
74__setup("selinux_compat_net=", selinux_compat_net_setup);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ingo Molnarbb003072006-03-22 00:09:14 -080077static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* global data for booleans */
Eric Paris18729812008-04-17 14:15:45 -040080static struct dentry *bool_dir;
81static int bool_num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -050082static char **bool_pending_names;
Eric Paris18729812008-04-17 14:15:45 -040083static int *bool_pending_values;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040085/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040086static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040087static unsigned long last_class_ino;
88
Paul Moore3bb56b22008-01-29 08:38:19 -050089/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040090static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092extern void selnl_notify_setenforce(int val);
93
94/* Check whether a task is allowed to use a security operation. */
95static int task_has_security(struct task_struct *tsk,
96 u32 perms)
97{
98 struct task_security_struct *tsec;
99
100 tsec = tsk->security;
101 if (!tsec)
102 return -EACCES;
103
104 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
105 SECCLASS_SECURITY, perms, NULL);
106}
107
108enum sel_inos {
109 SEL_ROOT_INO = 2,
110 SEL_LOAD, /* load policy */
111 SEL_ENFORCE, /* get or set enforcing status */
112 SEL_CONTEXT, /* validate context */
113 SEL_ACCESS, /* compute access decision */
114 SEL_CREATE, /* compute create labeling decision */
115 SEL_RELABEL, /* compute relabeling decision */
116 SEL_USER, /* compute reachable user contexts */
117 SEL_POLICYVERS, /* return policy version for this kernel */
118 SEL_COMMIT_BOOLS, /* commit new boolean values */
119 SEL_MLS, /* return if MLS policy is enabled */
120 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 SEL_MEMBER, /* compute polyinstantiation membership decision */
122 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700123 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400124 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
125 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
James Carter6174eaf2007-04-04 16:18:39 -0400126 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
James Carter6174eaf2007-04-04 16:18:39 -0400129static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
130
Paul Moore3bb56b22008-01-29 08:38:19 -0500131#define SEL_INITCON_INO_OFFSET 0x01000000
132#define SEL_BOOL_INO_OFFSET 0x02000000
133#define SEL_CLASS_INO_OFFSET 0x04000000
134#define SEL_POLICYCAP_INO_OFFSET 0x08000000
135#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TMPBUFLEN 12
138static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
139 size_t count, loff_t *ppos)
140{
141 char tmpbuf[TMPBUFLEN];
142 ssize_t length;
143
144 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
145 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
146}
147
148#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400149static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 size_t count, loff_t *ppos)
151
152{
153 char *page;
154 ssize_t length;
155 int new_value;
156
Davi Arnautbfd51622005-10-30 14:59:24 -0800157 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -ENOMEM;
159 if (*ppos != 0) {
160 /* No partial writes. */
161 return -EINVAL;
162 }
Eric Paris18729812008-04-17 14:15:45 -0400163 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!page)
165 return -ENOMEM;
166 length = -EFAULT;
167 if (copy_from_user(page, buf, count))
168 goto out;
169
170 length = -EINVAL;
171 if (sscanf(page, "%d", &new_value) != 1)
172 goto out;
173
174 if (new_value != selinux_enforcing) {
175 length = task_has_security(current, SECURITY__SETENFORCE);
176 if (length)
177 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000178 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500179 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
180 new_value, selinux_enforcing,
181 audit_get_loginuid(current),
182 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 selinux_enforcing = new_value;
184 if (selinux_enforcing)
185 avc_ss_reset(0);
186 selnl_notify_setenforce(selinux_enforcing);
187 }
188 length = count;
189out:
190 free_page((unsigned long) page);
191 return length;
192}
193#else
194#define sel_write_enforce NULL
195#endif
196
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800197static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .read = sel_read_enforce,
199 .write = sel_write_enforce,
200};
201
Eric Paris3f120702007-09-21 14:37:10 -0400202static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
203 size_t count, loff_t *ppos)
204{
205 char tmpbuf[TMPBUFLEN];
206 ssize_t length;
207 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
208 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
209 security_get_reject_unknown() : !security_get_allow_unknown();
210
211 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
212 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
213}
214
215static const struct file_operations sel_handle_unknown_ops = {
216 .read = sel_read_handle_unknown,
217};
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400220static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 size_t count, loff_t *ppos)
222
223{
224 char *page;
225 ssize_t length;
226 int new_value;
227 extern int selinux_disable(void);
228
Davi Arnautbfd51622005-10-30 14:59:24 -0800229 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -ENOMEM;
231 if (*ppos != 0) {
232 /* No partial writes. */
233 return -EINVAL;
234 }
Eric Paris18729812008-04-17 14:15:45 -0400235 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!page)
237 return -ENOMEM;
238 length = -EFAULT;
239 if (copy_from_user(page, buf, count))
240 goto out;
241
242 length = -EINVAL;
243 if (sscanf(page, "%d", &new_value) != 1)
244 goto out;
245
246 if (new_value) {
247 length = selinux_disable();
248 if (length < 0)
249 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000250 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500251 "selinux=0 auid=%u ses=%u",
252 audit_get_loginuid(current),
253 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 length = count;
257out:
258 free_page((unsigned long) page);
259 return length;
260}
261#else
262#define sel_write_disable NULL
263#endif
264
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800265static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .write = sel_write_disable,
267};
268
269static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400270 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 char tmpbuf[TMPBUFLEN];
273 ssize_t length;
274
275 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
276 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
277}
278
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800279static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .read = sel_read_policyvers,
281};
282
283/* declaration for sel_write_load */
284static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400285static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500286static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400287
288/* declaration for sel_make_class_dirs */
289static int sel_make_dir(struct inode *dir, struct dentry *dentry,
290 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292static ssize_t sel_read_mls(struct file *filp, char __user *buf,
293 size_t count, loff_t *ppos)
294{
295 char tmpbuf[TMPBUFLEN];
296 ssize_t length;
297
298 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
299 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
300}
301
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800302static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .read = sel_read_mls,
304};
305
Eric Paris18729812008-04-17 14:15:45 -0400306static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 size_t count, loff_t *ppos)
308
309{
310 int ret;
311 ssize_t length;
312 void *data = NULL;
313
Ingo Molnarbb003072006-03-22 00:09:14 -0800314 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 length = task_has_security(current, SECURITY__LOAD_POLICY);
317 if (length)
318 goto out;
319
320 if (*ppos != 0) {
321 /* No partial writes. */
322 length = -EINVAL;
323 goto out;
324 }
325
Davi Arnautbfd51622005-10-30 14:59:24 -0800326 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 || (data = vmalloc(count)) == NULL) {
328 length = -ENOMEM;
329 goto out;
330 }
331
332 length = -EFAULT;
333 if (copy_from_user(data, buf, count) != 0)
334 goto out;
335
336 length = security_load_policy(data, count);
337 if (length)
338 goto out;
339
340 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400341 if (ret) {
342 length = ret;
343 goto out1;
344 }
345
346 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500347 if (ret) {
348 length = ret;
349 goto out1;
350 }
351
352 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (ret)
354 length = ret;
355 else
356 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400357
358out1:
Eric Paris3f120702007-09-21 14:37:10 -0400359
360 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
361 (security_get_reject_unknown() ? "reject" :
362 (security_get_allow_unknown() ? "allow" : "deny")));
363
Steve Grubbaf601e42006-01-04 14:08:39 +0000364 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500365 "policy loaded auid=%u ses=%u",
366 audit_get_loginuid(current),
367 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800369 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 vfree(data);
371 return length;
372}
373
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800374static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .write = sel_write_load,
376};
377
Eric Paris18729812008-04-17 14:15:45 -0400378static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800380 char *canon;
381 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ssize_t length;
383
384 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
385 if (length)
386 return length;
387
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800388 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800390 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800392 length = security_sid_to_context(sid, &canon, &len);
393 if (length < 0)
394 return length;
395
396 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400397 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
398 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800399 length = -ERANGE;
400 goto out;
401 }
402
403 memcpy(buf, canon, len);
404 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800406 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return length;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
411 size_t count, loff_t *ppos)
412{
413 char tmpbuf[TMPBUFLEN];
414 ssize_t length;
415
416 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
417 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
418}
419
Eric Paris18729812008-04-17 14:15:45 -0400420static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 size_t count, loff_t *ppos)
422{
423 char *page;
424 ssize_t length;
425 unsigned int new_value;
426
427 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
428 if (length)
429 return length;
430
Davi Arnautbfd51622005-10-30 14:59:24 -0800431 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -ENOMEM;
433 if (*ppos != 0) {
434 /* No partial writes. */
435 return -EINVAL;
436 }
Eric Paris18729812008-04-17 14:15:45 -0400437 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!page)
439 return -ENOMEM;
440 length = -EFAULT;
441 if (copy_from_user(page, buf, count))
442 goto out;
443
444 length = -EINVAL;
445 if (sscanf(page, "%u", &new_value) != 1)
446 goto out;
447
448 selinux_checkreqprot = new_value ? 1 : 0;
449 length = count;
450out:
451 free_page((unsigned long) page);
452 return length;
453}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800454static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .read = sel_read_checkreqprot,
456 .write = sel_write_checkreqprot,
457};
458
James Morris4e5ab4c2006-06-09 00:33:33 -0700459static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
460 size_t count, loff_t *ppos)
461{
462 char tmpbuf[TMPBUFLEN];
463 ssize_t length;
464
465 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
466 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
467}
468
Eric Paris18729812008-04-17 14:15:45 -0400469static ssize_t sel_write_compat_net(struct file *file, const char __user *buf,
James Morris4e5ab4c2006-06-09 00:33:33 -0700470 size_t count, loff_t *ppos)
471{
472 char *page;
473 ssize_t length;
474 int new_value;
475
476 length = task_has_security(current, SECURITY__LOAD_POLICY);
477 if (length)
478 return length;
479
480 if (count >= PAGE_SIZE)
481 return -ENOMEM;
482 if (*ppos != 0) {
483 /* No partial writes. */
484 return -EINVAL;
485 }
Eric Paris18729812008-04-17 14:15:45 -0400486 page = (char *)get_zeroed_page(GFP_KERNEL);
James Morris4e5ab4c2006-06-09 00:33:33 -0700487 if (!page)
488 return -ENOMEM;
489 length = -EFAULT;
490 if (copy_from_user(page, buf, count))
491 goto out;
492
493 length = -EINVAL;
494 if (sscanf(page, "%d", &new_value) != 1)
495 goto out;
496
497 selinux_compat_net = new_value ? 1 : 0;
498 length = count;
499out:
500 free_page((unsigned long) page);
501 return length;
502}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800503static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700504 .read = sel_read_compat_net,
505 .write = sel_write_compat_net,
506};
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
510 */
Eric Paris18729812008-04-17 14:15:45 -0400511static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
512static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
513static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
514static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
515static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517static ssize_t (*write_op[])(struct file *, char *, size_t) = {
518 [SEL_ACCESS] = sel_write_access,
519 [SEL_CREATE] = sel_write_create,
520 [SEL_RELABEL] = sel_write_relabel,
521 [SEL_USER] = sel_write_user,
522 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800523 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
526static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
527{
Eric Paris18729812008-04-17 14:15:45 -0400528 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 char *data;
530 ssize_t rv;
531
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800532 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EINVAL;
534
535 data = simple_transaction_get(file, buf, size);
536 if (IS_ERR(data))
537 return PTR_ERR(data);
538
Eric Paris18729812008-04-17 14:15:45 -0400539 rv = write_op[ino](file, data, size);
540 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 simple_transaction_set(file, rv);
542 rv = size;
543 }
544 return rv;
545}
546
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800547static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .write = selinux_transaction_write,
549 .read = simple_transaction_read,
550 .release = simple_transaction_release,
551};
552
553/*
554 * payload - write methods
555 * If the method has a response, the response should be put in buf,
556 * and the length returned. Otherwise return 0 or and -error.
557 */
558
Eric Paris18729812008-04-17 14:15:45 -0400559static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 char *scon, *tcon;
562 u32 ssid, tsid;
563 u16 tclass;
564 u32 req;
565 struct av_decision avd;
566 ssize_t length;
567
568 length = task_has_security(current, SECURITY__COMPUTE_AV);
569 if (length)
570 return length;
571
572 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800573 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!scon)
575 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
James Morris89d155e2005-10-30 14:59:21 -0800577 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!tcon)
579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 length = -EINVAL;
582 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
583 goto out2;
584
585 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
586 if (length < 0)
587 goto out2;
588 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
589 if (length < 0)
590 goto out2;
591
592 length = security_compute_av(ssid, tsid, tclass, req, &avd);
593 if (length < 0)
594 goto out2;
595
596 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
597 "%x %x %x %x %u",
598 avd.allowed, avd.decided,
599 avd.auditallow, avd.auditdeny,
600 avd.seqno);
601out2:
602 kfree(tcon);
603out:
604 kfree(scon);
605 return length;
606}
607
Eric Paris18729812008-04-17 14:15:45 -0400608static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 char *scon, *tcon;
611 u32 ssid, tsid, newsid;
612 u16 tclass;
613 ssize_t length;
614 char *newcon;
615 u32 len;
616
617 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
618 if (length)
619 return length;
620
621 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800622 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!scon)
624 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
James Morris89d155e2005-10-30 14:59:21 -0800626 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!tcon)
628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 length = -EINVAL;
631 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
632 goto out2;
633
634 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
635 if (length < 0)
636 goto out2;
637 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
638 if (length < 0)
639 goto out2;
640
641 length = security_transition_sid(ssid, tsid, tclass, &newsid);
642 if (length < 0)
643 goto out2;
644
645 length = security_sid_to_context(newsid, &newcon, &len);
646 if (length < 0)
647 goto out2;
648
649 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400650 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
651 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 length = -ERANGE;
653 goto out3;
654 }
655
656 memcpy(buf, newcon, len);
657 length = len;
658out3:
659 kfree(newcon);
660out2:
661 kfree(tcon);
662out:
663 kfree(scon);
664 return length;
665}
666
Eric Paris18729812008-04-17 14:15:45 -0400667static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 char *scon, *tcon;
670 u32 ssid, tsid, newsid;
671 u16 tclass;
672 ssize_t length;
673 char *newcon;
674 u32 len;
675
676 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
677 if (length)
678 return length;
679
680 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800681 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!scon)
683 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
James Morris89d155e2005-10-30 14:59:21 -0800685 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!tcon)
687 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 length = -EINVAL;
690 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
691 goto out2;
692
693 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
694 if (length < 0)
695 goto out2;
696 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
697 if (length < 0)
698 goto out2;
699
700 length = security_change_sid(ssid, tsid, tclass, &newsid);
701 if (length < 0)
702 goto out2;
703
704 length = security_sid_to_context(newsid, &newcon, &len);
705 if (length < 0)
706 goto out2;
707
708 if (len > SIMPLE_TRANSACTION_LIMIT) {
709 length = -ERANGE;
710 goto out3;
711 }
712
713 memcpy(buf, newcon, len);
714 length = len;
715out3:
716 kfree(newcon);
717out2:
718 kfree(tcon);
719out:
720 kfree(scon);
721 return length;
722}
723
Eric Paris18729812008-04-17 14:15:45 -0400724static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 char *con, *user, *ptr;
727 u32 sid, *sids;
728 ssize_t length;
729 char *newcon;
730 int i, rc;
731 u32 len, nsids;
732
733 length = task_has_security(current, SECURITY__COMPUTE_USER);
734 if (length)
735 return length;
736
737 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800738 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!con)
740 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
James Morris89d155e2005-10-30 14:59:21 -0800742 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!user)
744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 length = -EINVAL;
747 if (sscanf(buf, "%s %s", con, user) != 2)
748 goto out2;
749
750 length = security_context_to_sid(con, strlen(con)+1, &sid);
751 if (length < 0)
752 goto out2;
753
754 length = security_get_user_sids(sid, user, &sids, &nsids);
755 if (length < 0)
756 goto out2;
757
758 length = sprintf(buf, "%u", nsids) + 1;
759 ptr = buf + length;
760 for (i = 0; i < nsids; i++) {
761 rc = security_sid_to_context(sids[i], &newcon, &len);
762 if (rc) {
763 length = rc;
764 goto out3;
765 }
766 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
767 kfree(newcon);
768 length = -ERANGE;
769 goto out3;
770 }
771 memcpy(ptr, newcon, len);
772 kfree(newcon);
773 ptr += len;
774 length += len;
775 }
776out3:
777 kfree(sids);
778out2:
779 kfree(user);
780out:
781 kfree(con);
782 return length;
783}
784
Eric Paris18729812008-04-17 14:15:45 -0400785static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 char *scon, *tcon;
788 u32 ssid, tsid, newsid;
789 u16 tclass;
790 ssize_t length;
791 char *newcon;
792 u32 len;
793
794 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
795 if (length)
796 return length;
797
798 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800799 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!scon)
801 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
James Morris89d155e2005-10-30 14:59:21 -0800803 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!tcon)
805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 length = -EINVAL;
808 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
809 goto out2;
810
811 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
812 if (length < 0)
813 goto out2;
814 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
815 if (length < 0)
816 goto out2;
817
818 length = security_member_sid(ssid, tsid, tclass, &newsid);
819 if (length < 0)
820 goto out2;
821
822 length = security_sid_to_context(newsid, &newcon, &len);
823 if (length < 0)
824 goto out2;
825
826 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400827 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
828 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 length = -ERANGE;
830 goto out3;
831 }
832
833 memcpy(buf, newcon, len);
834 length = len;
835out3:
836 kfree(newcon);
837out2:
838 kfree(tcon);
839out:
840 kfree(scon);
841 return length;
842}
843
844static struct inode *sel_make_inode(struct super_block *sb, int mode)
845{
846 struct inode *ret = new_inode(sb);
847
848 if (ret) {
849 ret->i_mode = mode;
850 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ret->i_blocks = 0;
852 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
853 }
854 return ret;
855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static ssize_t sel_read_bool(struct file *filep, char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 char *page = NULL;
861 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ssize_t ret;
863 int cur_enforcing;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500864 struct inode *inode = filep->f_path.dentry->d_inode;
865 unsigned index = inode->i_ino & SEL_INO_MASK;
866 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Ingo Molnarbb003072006-03-22 00:09:14 -0800868 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500870 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
871 ret = -EINVAL;
872 goto out;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Davi Arnautbfd51622005-10-30 14:59:24 -0800875 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ret = -EINVAL;
877 goto out;
878 }
Eric Paris18729812008-04-17 14:15:45 -0400879 page = (char *)get_zeroed_page(GFP_KERNEL);
880 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 ret = -ENOMEM;
882 goto out;
883 }
884
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500885 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (cur_enforcing < 0) {
887 ret = cur_enforcing;
888 goto out;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500891 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800892 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800894 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (page)
896 free_page((unsigned long)page);
897 return ret;
898}
899
900static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
901 size_t count, loff_t *ppos)
902{
903 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500904 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int new_value;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500906 struct inode *inode = filep->f_path.dentry->d_inode;
907 unsigned index = inode->i_ino & SEL_INO_MASK;
908 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Ingo Molnarbb003072006-03-22 00:09:14 -0800910 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 length = task_has_security(current, SECURITY__SETBOOL);
913 if (length)
914 goto out;
915
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500916 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
917 length = -EINVAL;
918 goto out;
919 }
920
Davi Arnautbfd51622005-10-30 14:59:24 -0800921 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 length = -ENOMEM;
923 goto out;
924 }
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (*ppos != 0) {
927 /* No partial writes. */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500928 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930 }
Eric Paris18729812008-04-17 14:15:45 -0400931 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!page) {
933 length = -ENOMEM;
934 goto out;
935 }
936
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500937 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (copy_from_user(page, buf, count))
939 goto out;
940
941 length = -EINVAL;
942 if (sscanf(page, "%d", &new_value) != 1)
943 goto out;
944
945 if (new_value)
946 new_value = 1;
947
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500948 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 length = count;
950
951out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800952 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (page)
954 free_page((unsigned long) page);
955 return length;
956}
957
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800958static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400959 .read = sel_read_bool,
960 .write = sel_write_bool,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961};
962
963static ssize_t sel_commit_bools_write(struct file *filep,
964 const char __user *buf,
965 size_t count, loff_t *ppos)
966{
967 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500968 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int new_value;
970
Ingo Molnarbb003072006-03-22 00:09:14 -0800971 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 length = task_has_security(current, SECURITY__SETBOOL);
974 if (length)
975 goto out;
976
Davi Arnautbfd51622005-10-30 14:59:24 -0800977 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 length = -ENOMEM;
979 goto out;
980 }
981 if (*ppos != 0) {
982 /* No partial writes. */
983 goto out;
984 }
Eric Paris18729812008-04-17 14:15:45 -0400985 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!page) {
987 length = -ENOMEM;
988 goto out;
989 }
990
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500991 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (copy_from_user(page, buf, count))
993 goto out;
994
995 length = -EINVAL;
996 if (sscanf(page, "%d", &new_value) != 1)
997 goto out;
998
Eric Paris18729812008-04-17 14:15:45 -0400999 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 length = count;
1003
1004out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001005 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (page)
1007 free_page((unsigned long) page);
1008 return length;
1009}
1010
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001011static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001012 .write = sel_commit_bools_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013};
1014
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001015static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Stephen Smalley0955dc02007-11-21 09:01:36 -05001017 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 spin_lock(&dcache_lock);
1020 node = de->d_subdirs.next;
1021 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001022 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 list_del_init(node);
1024
1025 if (d->d_inode) {
1026 d = dget_locked(d);
1027 spin_unlock(&dcache_lock);
1028 d_delete(d);
1029 simple_unlink(de->d_inode, d);
1030 dput(d);
1031 spin_lock(&dcache_lock);
1032 }
1033 node = de->d_subdirs.next;
1034 }
1035
1036 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039#define BOOL_DIR_NAME "booleans"
1040
1041static int sel_make_bools(void)
1042{
1043 int i, ret = 0;
1044 ssize_t len;
1045 struct dentry *dentry = NULL;
1046 struct dentry *dir = bool_dir;
1047 struct inode *inode = NULL;
1048 struct inode_security_struct *isec;
1049 char **names = NULL, *page;
1050 int num;
1051 int *values = NULL;
1052 u32 sid;
1053
1054 /* remove any existing files */
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001055 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001056 kfree(bool_pending_values);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001057 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001058 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001060 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Eric Paris18729812008-04-17 14:15:45 -04001062 page = (char *)get_zeroed_page(GFP_KERNEL);
1063 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -ENOMEM;
1065
1066 ret = security_get_bools(&num, &names, &values);
1067 if (ret != 0)
1068 goto out;
1069
1070 for (i = 0; i < num; i++) {
1071 dentry = d_alloc_name(dir, names[i]);
1072 if (!dentry) {
1073 ret = -ENOMEM;
1074 goto err;
1075 }
1076 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1077 if (!inode) {
1078 ret = -ENOMEM;
1079 goto err;
1080 }
1081
1082 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1083 if (len < 0) {
1084 ret = -EINVAL;
1085 goto err;
1086 } else if (len >= PAGE_SIZE) {
1087 ret = -ENAMETOOLONG;
1088 goto err;
1089 }
Eric Paris18729812008-04-17 14:15:45 -04001090 isec = (struct inode_security_struct *)inode->i_security;
1091 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1092 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto err;
1094 isec->sid = sid;
1095 isec->initialized = 1;
1096 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001097 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 d_add(dentry, inode);
1099 }
1100 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001101 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 bool_pending_values = values;
1103out:
1104 free_page((unsigned long)page);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001105 return ret;
1106err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001108 for (i = 0; i < num; i++)
1109 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 kfree(names);
1111 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001112 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001113 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ret = -ENOMEM;
1115 goto out;
1116}
1117
1118#define NULL_FILE_NAME "null"
1119
Eric Paris18729812008-04-17 14:15:45 -04001120struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1123 size_t count, loff_t *ppos)
1124{
1125 char tmpbuf[TMPBUFLEN];
1126 ssize_t length;
1127
1128 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1129 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1130}
1131
Eric Paris18729812008-04-17 14:15:45 -04001132static ssize_t sel_write_avc_cache_threshold(struct file *file,
1133 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 size_t count, loff_t *ppos)
1135
1136{
1137 char *page;
1138 ssize_t ret;
1139 int new_value;
1140
Davi Arnautbfd51622005-10-30 14:59:24 -08001141 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ret = -ENOMEM;
1143 goto out;
1144 }
1145
1146 if (*ppos != 0) {
1147 /* No partial writes. */
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151
Eric Paris18729812008-04-17 14:15:45 -04001152 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!page) {
1154 ret = -ENOMEM;
1155 goto out;
1156 }
1157
1158 if (copy_from_user(page, buf, count)) {
1159 ret = -EFAULT;
1160 goto out_free;
1161 }
1162
1163 if (sscanf(page, "%u", &new_value) != 1) {
1164 ret = -EINVAL;
1165 goto out;
1166 }
1167
1168 if (new_value != avc_cache_threshold) {
1169 ret = task_has_security(current, SECURITY__SETSECPARAM);
1170 if (ret)
1171 goto out_free;
1172 avc_cache_threshold = new_value;
1173 }
1174 ret = count;
1175out_free:
1176 free_page((unsigned long)page);
1177out:
1178 return ret;
1179}
1180
1181static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1182 size_t count, loff_t *ppos)
1183{
1184 char *page;
1185 ssize_t ret = 0;
1186
1187 page = (char *)__get_free_page(GFP_KERNEL);
1188 if (!page) {
1189 ret = -ENOMEM;
1190 goto out;
1191 }
1192 ret = avc_get_hash_stats(page);
1193 if (ret >= 0)
1194 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1195 free_page((unsigned long)page);
1196out:
1197 return ret;
1198}
1199
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001200static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 .read = sel_read_avc_cache_threshold,
1202 .write = sel_write_avc_cache_threshold,
1203};
1204
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001205static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .read = sel_read_avc_hash_stats,
1207};
1208
1209#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1210static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1211{
1212 int cpu;
1213
1214 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1215 if (!cpu_possible(cpu))
1216 continue;
1217 *idx = cpu + 1;
1218 return &per_cpu(avc_cache_stats, cpu);
1219 }
1220 return NULL;
1221}
1222
1223static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1224{
1225 loff_t n = *pos - 1;
1226
1227 if (*pos == 0)
1228 return SEQ_START_TOKEN;
1229
1230 return sel_avc_get_stat_idx(&n);
1231}
1232
1233static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1234{
1235 return sel_avc_get_stat_idx(pos);
1236}
1237
1238static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1239{
1240 struct avc_cache_stats *st = v;
1241
1242 if (v == SEQ_START_TOKEN)
1243 seq_printf(seq, "lookups hits misses allocations reclaims "
1244 "frees\n");
1245 else
1246 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1247 st->hits, st->misses, st->allocations,
1248 st->reclaims, st->frees);
1249 return 0;
1250}
1251
1252static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1253{ }
1254
Jan Engelhardt1996a102008-01-23 00:02:58 +01001255static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 .start = sel_avc_stats_seq_start,
1257 .next = sel_avc_stats_seq_next,
1258 .show = sel_avc_stats_seq_show,
1259 .stop = sel_avc_stats_seq_stop,
1260};
1261
1262static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1263{
1264 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1265}
1266
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001267static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 .open = sel_open_avc_cache_stats,
1269 .read = seq_read,
1270 .llseek = seq_lseek,
1271 .release = seq_release,
1272};
1273#endif
1274
1275static int sel_make_avc_files(struct dentry *dir)
1276{
1277 int i, ret = 0;
1278 static struct tree_descr files[] = {
1279 { "cache_threshold",
1280 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1281 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1282#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1283 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1284#endif
1285 };
1286
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001287 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct inode *inode;
1289 struct dentry *dentry;
1290
1291 dentry = d_alloc_name(dir, files[i].name);
1292 if (!dentry) {
1293 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
1297 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1298 if (!inode) {
1299 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001303 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 d_add(dentry, inode);
1305 }
1306out:
1307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Eric Paris18729812008-04-17 14:15:45 -04001310static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001311 size_t count, loff_t *ppos)
1312{
1313 struct inode *inode;
1314 char *con;
1315 u32 sid, len;
1316 ssize_t ret;
1317
1318 inode = file->f_path.dentry->d_inode;
1319 sid = inode->i_ino&SEL_INO_MASK;
1320 ret = security_sid_to_context(sid, &con, &len);
1321 if (ret < 0)
1322 return ret;
1323
1324 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1325 kfree(con);
1326 return ret;
1327}
1328
1329static const struct file_operations sel_initcon_ops = {
1330 .read = sel_read_initcon,
1331};
1332
1333static int sel_make_initcon_files(struct dentry *dir)
1334{
1335 int i, ret = 0;
1336
1337 for (i = 1; i <= SECINITSID_NUM; i++) {
1338 struct inode *inode;
1339 struct dentry *dentry;
1340 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1341 if (!dentry) {
1342 ret = -ENOMEM;
1343 goto out;
1344 }
1345
1346 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1347 if (!inode) {
1348 ret = -ENOMEM;
1349 goto out;
1350 }
1351 inode->i_fop = &sel_initcon_ops;
1352 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1353 d_add(dentry, inode);
1354 }
1355out:
1356 return ret;
1357}
1358
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001359static inline unsigned int sel_div(unsigned long a, unsigned long b)
1360{
1361 return a / b - (a % b < 0);
1362}
1363
1364static inline unsigned long sel_class_to_ino(u16 class)
1365{
1366 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1367}
1368
1369static inline u16 sel_ino_to_class(unsigned long ino)
1370{
1371 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1372}
1373
1374static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1375{
1376 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1377}
1378
1379static inline u32 sel_ino_to_perm(unsigned long ino)
1380{
1381 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1382}
1383
Eric Paris18729812008-04-17 14:15:45 -04001384static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001385 size_t count, loff_t *ppos)
1386{
1387 ssize_t rc, len;
1388 char *page;
1389 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1390
1391 page = (char *)__get_free_page(GFP_KERNEL);
1392 if (!page) {
1393 rc = -ENOMEM;
1394 goto out;
1395 }
1396
1397 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1398 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1399 free_page((unsigned long)page);
1400out:
1401 return rc;
1402}
1403
1404static const struct file_operations sel_class_ops = {
1405 .read = sel_read_class,
1406};
1407
Eric Paris18729812008-04-17 14:15:45 -04001408static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001409 size_t count, loff_t *ppos)
1410{
1411 ssize_t rc, len;
1412 char *page;
1413 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1414
1415 page = (char *)__get_free_page(GFP_KERNEL);
1416 if (!page) {
1417 rc = -ENOMEM;
1418 goto out;
1419 }
1420
Eric Paris18729812008-04-17 14:15:45 -04001421 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001422 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1423 free_page((unsigned long)page);
1424out:
1425 return rc;
1426}
1427
1428static const struct file_operations sel_perm_ops = {
1429 .read = sel_read_perm,
1430};
1431
Paul Moore3bb56b22008-01-29 08:38:19 -05001432static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1433 size_t count, loff_t *ppos)
1434{
1435 int value;
1436 char tmpbuf[TMPBUFLEN];
1437 ssize_t length;
1438 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1439
1440 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1441 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1442
1443 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1444}
1445
1446static const struct file_operations sel_policycap_ops = {
1447 .read = sel_read_policycap,
1448};
1449
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001450static int sel_make_perm_files(char *objclass, int classvalue,
1451 struct dentry *dir)
1452{
1453 int i, rc = 0, nperms;
1454 char **perms;
1455
1456 rc = security_get_permissions(objclass, &perms, &nperms);
1457 if (rc)
1458 goto out;
1459
1460 for (i = 0; i < nperms; i++) {
1461 struct inode *inode;
1462 struct dentry *dentry;
1463
1464 dentry = d_alloc_name(dir, perms[i]);
1465 if (!dentry) {
1466 rc = -ENOMEM;
1467 goto out1;
1468 }
1469
1470 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1471 if (!inode) {
1472 rc = -ENOMEM;
1473 goto out1;
1474 }
1475 inode->i_fop = &sel_perm_ops;
1476 /* i+1 since perm values are 1-indexed */
1477 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1478 d_add(dentry, inode);
1479 }
1480
1481out1:
1482 for (i = 0; i < nperms; i++)
1483 kfree(perms[i]);
1484 kfree(perms);
1485out:
1486 return rc;
1487}
1488
1489static int sel_make_class_dir_entries(char *classname, int index,
1490 struct dentry *dir)
1491{
1492 struct dentry *dentry = NULL;
1493 struct inode *inode = NULL;
1494 int rc;
1495
1496 dentry = d_alloc_name(dir, "index");
1497 if (!dentry) {
1498 rc = -ENOMEM;
1499 goto out;
1500 }
1501
1502 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1503 if (!inode) {
1504 rc = -ENOMEM;
1505 goto out;
1506 }
1507
1508 inode->i_fop = &sel_class_ops;
1509 inode->i_ino = sel_class_to_ino(index);
1510 d_add(dentry, inode);
1511
1512 dentry = d_alloc_name(dir, "perms");
1513 if (!dentry) {
1514 rc = -ENOMEM;
1515 goto out;
1516 }
1517
1518 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1519 if (rc)
1520 goto out;
1521
1522 rc = sel_make_perm_files(classname, index, dentry);
1523
1524out:
1525 return rc;
1526}
1527
1528static void sel_remove_classes(void)
1529{
1530 struct list_head *class_node;
1531
1532 list_for_each(class_node, &class_dir->d_subdirs) {
1533 struct dentry *class_subdir = list_entry(class_node,
1534 struct dentry, d_u.d_child);
1535 struct list_head *class_subdir_node;
1536
1537 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1538 struct dentry *d = list_entry(class_subdir_node,
1539 struct dentry, d_u.d_child);
1540
1541 if (d->d_inode)
1542 if (d->d_inode->i_mode & S_IFDIR)
1543 sel_remove_entries(d);
1544 }
1545
1546 sel_remove_entries(class_subdir);
1547 }
1548
1549 sel_remove_entries(class_dir);
1550}
1551
1552static int sel_make_classes(void)
1553{
1554 int rc = 0, nclasses, i;
1555 char **classes;
1556
1557 /* delete any existing entries */
1558 sel_remove_classes();
1559
1560 rc = security_get_classes(&classes, &nclasses);
1561 if (rc < 0)
1562 goto out;
1563
1564 /* +2 since classes are 1-indexed */
1565 last_class_ino = sel_class_to_ino(nclasses+2);
1566
1567 for (i = 0; i < nclasses; i++) {
1568 struct dentry *class_name_dir;
1569
1570 class_name_dir = d_alloc_name(class_dir, classes[i]);
1571 if (!class_name_dir) {
1572 rc = -ENOMEM;
1573 goto out1;
1574 }
1575
1576 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1577 &last_class_ino);
1578 if (rc)
1579 goto out1;
1580
1581 /* i+1 since class values are 1-indexed */
1582 rc = sel_make_class_dir_entries(classes[i], i+1,
1583 class_name_dir);
1584 if (rc)
1585 goto out1;
1586 }
1587
1588out1:
1589 for (i = 0; i < nclasses; i++)
1590 kfree(classes[i]);
1591 kfree(classes);
1592out:
1593 return rc;
1594}
1595
Paul Moore3bb56b22008-01-29 08:38:19 -05001596static int sel_make_policycap(void)
1597{
1598 unsigned int iter;
1599 struct dentry *dentry = NULL;
1600 struct inode *inode = NULL;
1601
1602 sel_remove_entries(policycap_dir);
1603
1604 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1605 if (iter < ARRAY_SIZE(policycap_names))
1606 dentry = d_alloc_name(policycap_dir,
1607 policycap_names[iter]);
1608 else
1609 dentry = d_alloc_name(policycap_dir, "unknown");
1610
1611 if (dentry == NULL)
1612 return -ENOMEM;
1613
1614 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1615 if (inode == NULL)
1616 return -ENOMEM;
1617
1618 inode->i_fop = &sel_policycap_ops;
1619 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1620 d_add(dentry, inode);
1621 }
1622
1623 return 0;
1624}
1625
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001626static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1627 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 int ret = 0;
1630 struct inode *inode;
1631
James Morrisedb20fb2006-03-22 00:09:20 -08001632 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (!inode) {
1634 ret = -ENOMEM;
1635 goto out;
1636 }
1637 inode->i_op = &simple_dir_inode_operations;
1638 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001639 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001640 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001641 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001643 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001644 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645out:
1646 return ret;
1647}
1648
Eric Paris18729812008-04-17 14:15:45 -04001649static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 int ret;
1652 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001653 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct inode_security_struct *isec;
1655
1656 static struct tree_descr selinux_files[] = {
1657 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1658 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001659 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1661 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1662 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1663 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1664 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1665 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1666 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1667 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1668 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1669 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001670 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001671 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1672 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* last one */ {""}
1674 };
1675 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1676 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001677 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
James Morrisedb20fb2006-03-22 00:09:20 -08001679 root_inode = sb->s_root->d_inode;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001682 if (!dentry) {
1683 ret = -ENOMEM;
1684 goto err;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001687 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001688 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001689 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001694 if (!dentry) {
1695 ret = -ENOMEM;
1696 goto err;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001700 if (!inode) {
1701 ret = -ENOMEM;
1702 goto err;
1703 }
James Carter6174eaf2007-04-04 16:18:39 -04001704 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001705 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 isec->sid = SECINITSID_DEVNULL;
1707 isec->sclass = SECCLASS_CHR_FILE;
1708 isec->initialized = 1;
1709
1710 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1711 d_add(dentry, inode);
1712 selinux_null = dentry;
1713
1714 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001715 if (!dentry) {
1716 ret = -ENOMEM;
1717 goto err;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001720 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001722 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 ret = sel_make_avc_files(dentry);
1725 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001726 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001727
1728 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1729 if (!dentry) {
1730 ret = -ENOMEM;
1731 goto err;
1732 }
1733
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001734 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001735 if (ret)
1736 goto err;
1737
1738 ret = sel_make_initcon_files(dentry);
1739 if (ret)
1740 goto err;
1741
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001742 dentry = d_alloc_name(sb->s_root, "class");
1743 if (!dentry) {
1744 ret = -ENOMEM;
1745 goto err;
1746 }
1747
1748 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1749 if (ret)
1750 goto err;
1751
1752 class_dir = dentry;
1753
Paul Moore3bb56b22008-01-29 08:38:19 -05001754 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1755 if (!dentry) {
1756 ret = -ENOMEM;
1757 goto err;
1758 }
1759
1760 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1761 if (ret)
1762 goto err;
1763
1764 policycap_dir = dentry;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766out:
James Morris161ce452006-03-22 00:09:17 -08001767 return ret;
1768err:
Eric Paris744ba352008-04-17 11:52:44 -04001769 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1770 __func__);
James Morris161ce452006-03-22 00:09:17 -08001771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
David Howells454e2392006-06-23 02:02:57 -07001774static int sel_get_sb(struct file_system_type *fs_type,
1775 int flags, const char *dev_name, void *data,
1776 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
David Howells454e2392006-06-23 02:02:57 -07001778 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
1781static struct file_system_type sel_fs_type = {
1782 .name = "selinuxfs",
1783 .get_sb = sel_get_sb,
1784 .kill_sb = kill_litter_super,
1785};
1786
1787struct vfsmount *selinuxfs_mount;
1788
1789static int __init init_sel_fs(void)
1790{
1791 int err;
1792
1793 if (!selinux_enabled)
1794 return 0;
1795 err = register_filesystem(&sel_fs_type);
1796 if (!err) {
1797 selinuxfs_mount = kern_mount(&sel_fs_type);
1798 if (IS_ERR(selinuxfs_mount)) {
1799 printk(KERN_ERR "selinuxfs: could not mount!\n");
1800 err = PTR_ERR(selinuxfs_mount);
1801 selinuxfs_mount = NULL;
1802 }
1803 }
1804 return err;
1805}
1806
1807__initcall(init_sel_fs);
1808
1809#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1810void exit_sel_fs(void)
1811{
1812 unregister_filesystem(&sel_fs_type);
1813}
1814#endif