blob: 893b3db0bad2cf40062dd91f7441a09ce349a4ff [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>
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[] = {
Eric Parisb0c636b2008-02-28 12:58:40 -050045 "network_peer_controls",
46 "open_perms"
Paul Moore3bb56b22008-01-29 08:38:19 -050047};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
50
James Morris4e5ab4c2006-06-09 00:33:33 -070051#ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
52#define SELINUX_COMPAT_NET_VALUE 0
53#else
54#define SELINUX_COMPAT_NET_VALUE 1
55#endif
56
57int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int __init checkreqprot_setup(char *str)
60{
Eric Paris18729812008-04-17 14:15:45 -040061 selinux_checkreqprot = simple_strtoul(str, NULL, 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 1;
63}
64__setup("checkreqprot=", checkreqprot_setup);
65
James Morris4e5ab4c2006-06-09 00:33:33 -070066static int __init selinux_compat_net_setup(char *str)
67{
Eric Paris18729812008-04-17 14:15:45 -040068 selinux_compat_net = simple_strtoul(str, NULL, 0) ? 1 : 0;
James Morris4e5ab4c2006-06-09 00:33:33 -070069 return 1;
70}
71__setup("selinux_compat_net=", selinux_compat_net_setup);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Ingo Molnarbb003072006-03-22 00:09:14 -080074static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* global data for booleans */
Eric Paris18729812008-04-17 14:15:45 -040077static struct dentry *bool_dir;
78static int bool_num;
Stephen Smalleyd313f942007-11-26 11:12:53 -050079static char **bool_pending_names;
Eric Paris18729812008-04-17 14:15:45 -040080static int *bool_pending_values;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040082/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040083static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040084static unsigned long last_class_ino;
85
Paul Moore3bb56b22008-01-29 08:38:19 -050086/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040087static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089extern void selnl_notify_setenforce(int val);
90
91/* Check whether a task is allowed to use a security operation. */
92static int task_has_security(struct task_struct *tsk,
93 u32 perms)
94{
95 struct task_security_struct *tsec;
96
97 tsec = tsk->security;
98 if (!tsec)
99 return -EACCES;
100
101 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
102 SECCLASS_SECURITY, perms, NULL);
103}
104
105enum sel_inos {
106 SEL_ROOT_INO = 2,
107 SEL_LOAD, /* load policy */
108 SEL_ENFORCE, /* get or set enforcing status */
109 SEL_CONTEXT, /* validate context */
110 SEL_ACCESS, /* compute access decision */
111 SEL_CREATE, /* compute create labeling decision */
112 SEL_RELABEL, /* compute relabeling decision */
113 SEL_USER, /* compute reachable user contexts */
114 SEL_POLICYVERS, /* return policy version for this kernel */
115 SEL_COMMIT_BOOLS, /* commit new boolean values */
116 SEL_MLS, /* return if MLS policy is enabled */
117 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 SEL_MEMBER, /* compute polyinstantiation membership decision */
119 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700120 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400121 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
122 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
James Carter6174eaf2007-04-04 16:18:39 -0400123 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
James Carter6174eaf2007-04-04 16:18:39 -0400126static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
127
Paul Moore3bb56b22008-01-29 08:38:19 -0500128#define SEL_INITCON_INO_OFFSET 0x01000000
129#define SEL_BOOL_INO_OFFSET 0x02000000
130#define SEL_CLASS_INO_OFFSET 0x04000000
131#define SEL_POLICYCAP_INO_OFFSET 0x08000000
132#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define TMPBUFLEN 12
135static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
136 size_t count, loff_t *ppos)
137{
138 char tmpbuf[TMPBUFLEN];
139 ssize_t length;
140
141 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
142 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
143}
144
145#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400146static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 size_t count, loff_t *ppos)
148
149{
150 char *page;
151 ssize_t length;
152 int new_value;
153
Davi Arnautbfd51622005-10-30 14:59:24 -0800154 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -ENOMEM;
156 if (*ppos != 0) {
157 /* No partial writes. */
158 return -EINVAL;
159 }
Eric Paris18729812008-04-17 14:15:45 -0400160 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!page)
162 return -ENOMEM;
163 length = -EFAULT;
164 if (copy_from_user(page, buf, count))
165 goto out;
166
167 length = -EINVAL;
168 if (sscanf(page, "%d", &new_value) != 1)
169 goto out;
170
171 if (new_value != selinux_enforcing) {
172 length = task_has_security(current, SECURITY__SETENFORCE);
173 if (length)
174 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000175 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500176 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
177 new_value, selinux_enforcing,
178 audit_get_loginuid(current),
179 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 selinux_enforcing = new_value;
181 if (selinux_enforcing)
182 avc_ss_reset(0);
183 selnl_notify_setenforce(selinux_enforcing);
184 }
185 length = count;
186out:
187 free_page((unsigned long) page);
188 return length;
189}
190#else
191#define sel_write_enforce NULL
192#endif
193
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800194static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .read = sel_read_enforce,
196 .write = sel_write_enforce,
197};
198
Eric Paris3f120702007-09-21 14:37:10 -0400199static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
200 size_t count, loff_t *ppos)
201{
202 char tmpbuf[TMPBUFLEN];
203 ssize_t length;
204 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
205 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
206 security_get_reject_unknown() : !security_get_allow_unknown();
207
208 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
209 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
210}
211
212static const struct file_operations sel_handle_unknown_ops = {
213 .read = sel_read_handle_unknown,
214};
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400217static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 size_t count, loff_t *ppos)
219
220{
221 char *page;
222 ssize_t length;
223 int new_value;
224 extern int selinux_disable(void);
225
Davi Arnautbfd51622005-10-30 14:59:24 -0800226 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ENOMEM;
228 if (*ppos != 0) {
229 /* No partial writes. */
230 return -EINVAL;
231 }
Eric Paris18729812008-04-17 14:15:45 -0400232 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!page)
234 return -ENOMEM;
235 length = -EFAULT;
236 if (copy_from_user(page, buf, count))
237 goto out;
238
239 length = -EINVAL;
240 if (sscanf(page, "%d", &new_value) != 1)
241 goto out;
242
243 if (new_value) {
244 length = selinux_disable();
245 if (length < 0)
246 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000247 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500248 "selinux=0 auid=%u ses=%u",
249 audit_get_loginuid(current),
250 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 length = count;
254out:
255 free_page((unsigned long) page);
256 return length;
257}
258#else
259#define sel_write_disable NULL
260#endif
261
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800262static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .write = sel_write_disable,
264};
265
266static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400267 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 char tmpbuf[TMPBUFLEN];
270 ssize_t length;
271
272 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
273 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
274}
275
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800276static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .read = sel_read_policyvers,
278};
279
280/* declaration for sel_write_load */
281static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400282static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500283static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400284
285/* declaration for sel_make_class_dirs */
286static int sel_make_dir(struct inode *dir, struct dentry *dentry,
287 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289static ssize_t sel_read_mls(struct file *filp, char __user *buf,
290 size_t count, loff_t *ppos)
291{
292 char tmpbuf[TMPBUFLEN];
293 ssize_t length;
294
295 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
296 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
297}
298
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800299static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .read = sel_read_mls,
301};
302
Eric Paris18729812008-04-17 14:15:45 -0400303static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 size_t count, loff_t *ppos)
305
306{
307 int ret;
308 ssize_t length;
309 void *data = NULL;
310
Ingo Molnarbb003072006-03-22 00:09:14 -0800311 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 length = task_has_security(current, SECURITY__LOAD_POLICY);
314 if (length)
315 goto out;
316
317 if (*ppos != 0) {
318 /* No partial writes. */
319 length = -EINVAL;
320 goto out;
321 }
322
Davi Arnautbfd51622005-10-30 14:59:24 -0800323 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 || (data = vmalloc(count)) == NULL) {
325 length = -ENOMEM;
326 goto out;
327 }
328
329 length = -EFAULT;
330 if (copy_from_user(data, buf, count) != 0)
331 goto out;
332
333 length = security_load_policy(data, count);
334 if (length)
335 goto out;
336
337 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400338 if (ret) {
339 length = ret;
340 goto out1;
341 }
342
343 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500344 if (ret) {
345 length = ret;
346 goto out1;
347 }
348
349 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (ret)
351 length = ret;
352 else
353 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400354
355out1:
Eric Paris3f120702007-09-21 14:37:10 -0400356
357 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
358 (security_get_reject_unknown() ? "reject" :
359 (security_get_allow_unknown() ? "allow" : "deny")));
360
Steve Grubbaf601e42006-01-04 14:08:39 +0000361 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500362 "policy loaded auid=%u ses=%u",
363 audit_get_loginuid(current),
364 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800366 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 vfree(data);
368 return length;
369}
370
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800371static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .write = sel_write_load,
373};
374
Eric Paris18729812008-04-17 14:15:45 -0400375static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800377 char *canon;
378 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ssize_t length;
380
381 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
382 if (length)
383 return length;
384
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800385 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800387 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800389 length = security_sid_to_context(sid, &canon, &len);
390 if (length < 0)
391 return length;
392
393 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400394 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
395 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800396 length = -ERANGE;
397 goto out;
398 }
399
400 memcpy(buf, canon, len);
401 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800403 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return length;
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
408 size_t count, loff_t *ppos)
409{
410 char tmpbuf[TMPBUFLEN];
411 ssize_t length;
412
413 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
414 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
415}
416
Eric Paris18729812008-04-17 14:15:45 -0400417static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 size_t count, loff_t *ppos)
419{
420 char *page;
421 ssize_t length;
422 unsigned int new_value;
423
424 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
425 if (length)
426 return length;
427
Davi Arnautbfd51622005-10-30 14:59:24 -0800428 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -ENOMEM;
430 if (*ppos != 0) {
431 /* No partial writes. */
432 return -EINVAL;
433 }
Eric Paris18729812008-04-17 14:15:45 -0400434 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!page)
436 return -ENOMEM;
437 length = -EFAULT;
438 if (copy_from_user(page, buf, count))
439 goto out;
440
441 length = -EINVAL;
442 if (sscanf(page, "%u", &new_value) != 1)
443 goto out;
444
445 selinux_checkreqprot = new_value ? 1 : 0;
446 length = count;
447out:
448 free_page((unsigned long) page);
449 return length;
450}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800451static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 .read = sel_read_checkreqprot,
453 .write = sel_write_checkreqprot,
454};
455
James Morris4e5ab4c2006-06-09 00:33:33 -0700456static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
457 size_t count, loff_t *ppos)
458{
459 char tmpbuf[TMPBUFLEN];
460 ssize_t length;
461
462 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
463 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
464}
465
Eric Paris18729812008-04-17 14:15:45 -0400466static ssize_t sel_write_compat_net(struct file *file, const char __user *buf,
James Morris4e5ab4c2006-06-09 00:33:33 -0700467 size_t count, loff_t *ppos)
468{
469 char *page;
470 ssize_t length;
471 int new_value;
472
473 length = task_has_security(current, SECURITY__LOAD_POLICY);
474 if (length)
475 return length;
476
477 if (count >= PAGE_SIZE)
478 return -ENOMEM;
479 if (*ppos != 0) {
480 /* No partial writes. */
481 return -EINVAL;
482 }
Eric Paris18729812008-04-17 14:15:45 -0400483 page = (char *)get_zeroed_page(GFP_KERNEL);
James Morris4e5ab4c2006-06-09 00:33:33 -0700484 if (!page)
485 return -ENOMEM;
486 length = -EFAULT;
487 if (copy_from_user(page, buf, count))
488 goto out;
489
490 length = -EINVAL;
491 if (sscanf(page, "%d", &new_value) != 1)
492 goto out;
493
494 selinux_compat_net = new_value ? 1 : 0;
495 length = count;
496out:
497 free_page((unsigned long) page);
498 return length;
499}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800500static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700501 .read = sel_read_compat_net,
502 .write = sel_write_compat_net,
503};
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
506 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
507 */
Eric Paris18729812008-04-17 14:15:45 -0400508static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
509static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
510static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
511static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
512static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514static ssize_t (*write_op[])(struct file *, char *, size_t) = {
515 [SEL_ACCESS] = sel_write_access,
516 [SEL_CREATE] = sel_write_create,
517 [SEL_RELABEL] = sel_write_relabel,
518 [SEL_USER] = sel_write_user,
519 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800520 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
523static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
524{
Eric Paris18729812008-04-17 14:15:45 -0400525 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 char *data;
527 ssize_t rv;
528
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800529 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EINVAL;
531
532 data = simple_transaction_get(file, buf, size);
533 if (IS_ERR(data))
534 return PTR_ERR(data);
535
Eric Paris18729812008-04-17 14:15:45 -0400536 rv = write_op[ino](file, data, size);
537 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 simple_transaction_set(file, rv);
539 rv = size;
540 }
541 return rv;
542}
543
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800544static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 .write = selinux_transaction_write,
546 .read = simple_transaction_read,
547 .release = simple_transaction_release,
548};
549
550/*
551 * payload - write methods
552 * If the method has a response, the response should be put in buf,
553 * and the length returned. Otherwise return 0 or and -error.
554 */
555
Eric Paris18729812008-04-17 14:15:45 -0400556static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 char *scon, *tcon;
559 u32 ssid, tsid;
560 u16 tclass;
561 u32 req;
562 struct av_decision avd;
563 ssize_t length;
564
565 length = task_has_security(current, SECURITY__COMPUTE_AV);
566 if (length)
567 return length;
568
569 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800570 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!scon)
572 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
James Morris89d155e2005-10-30 14:59:21 -0800574 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!tcon)
576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 length = -EINVAL;
579 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
580 goto out2;
581
582 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
583 if (length < 0)
584 goto out2;
585 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
586 if (length < 0)
587 goto out2;
588
589 length = security_compute_av(ssid, tsid, tclass, req, &avd);
590 if (length < 0)
591 goto out2;
592
593 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
594 "%x %x %x %x %u",
595 avd.allowed, avd.decided,
596 avd.auditallow, avd.auditdeny,
597 avd.seqno);
598out2:
599 kfree(tcon);
600out:
601 kfree(scon);
602 return length;
603}
604
Eric Paris18729812008-04-17 14:15:45 -0400605static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 char *scon, *tcon;
608 u32 ssid, tsid, newsid;
609 u16 tclass;
610 ssize_t length;
611 char *newcon;
612 u32 len;
613
614 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
615 if (length)
616 return length;
617
618 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800619 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!scon)
621 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
James Morris89d155e2005-10-30 14:59:21 -0800623 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!tcon)
625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 length = -EINVAL;
628 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
629 goto out2;
630
631 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
632 if (length < 0)
633 goto out2;
634 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
635 if (length < 0)
636 goto out2;
637
638 length = security_transition_sid(ssid, tsid, tclass, &newsid);
639 if (length < 0)
640 goto out2;
641
642 length = security_sid_to_context(newsid, &newcon, &len);
643 if (length < 0)
644 goto out2;
645
646 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400647 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
648 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 length = -ERANGE;
650 goto out3;
651 }
652
653 memcpy(buf, newcon, len);
654 length = len;
655out3:
656 kfree(newcon);
657out2:
658 kfree(tcon);
659out:
660 kfree(scon);
661 return length;
662}
663
Eric Paris18729812008-04-17 14:15:45 -0400664static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 char *scon, *tcon;
667 u32 ssid, tsid, newsid;
668 u16 tclass;
669 ssize_t length;
670 char *newcon;
671 u32 len;
672
673 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
674 if (length)
675 return length;
676
677 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800678 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!scon)
680 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
James Morris89d155e2005-10-30 14:59:21 -0800682 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (!tcon)
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 length = -EINVAL;
687 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
688 goto out2;
689
690 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
691 if (length < 0)
692 goto out2;
693 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
694 if (length < 0)
695 goto out2;
696
697 length = security_change_sid(ssid, tsid, tclass, &newsid);
698 if (length < 0)
699 goto out2;
700
701 length = security_sid_to_context(newsid, &newcon, &len);
702 if (length < 0)
703 goto out2;
704
705 if (len > SIMPLE_TRANSACTION_LIMIT) {
706 length = -ERANGE;
707 goto out3;
708 }
709
710 memcpy(buf, newcon, len);
711 length = len;
712out3:
713 kfree(newcon);
714out2:
715 kfree(tcon);
716out:
717 kfree(scon);
718 return length;
719}
720
Eric Paris18729812008-04-17 14:15:45 -0400721static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 char *con, *user, *ptr;
724 u32 sid, *sids;
725 ssize_t length;
726 char *newcon;
727 int i, rc;
728 u32 len, nsids;
729
730 length = task_has_security(current, SECURITY__COMPUTE_USER);
731 if (length)
732 return length;
733
734 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800735 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!con)
737 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
James Morris89d155e2005-10-30 14:59:21 -0800739 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!user)
741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 length = -EINVAL;
744 if (sscanf(buf, "%s %s", con, user) != 2)
745 goto out2;
746
747 length = security_context_to_sid(con, strlen(con)+1, &sid);
748 if (length < 0)
749 goto out2;
750
751 length = security_get_user_sids(sid, user, &sids, &nsids);
752 if (length < 0)
753 goto out2;
754
755 length = sprintf(buf, "%u", nsids) + 1;
756 ptr = buf + length;
757 for (i = 0; i < nsids; i++) {
758 rc = security_sid_to_context(sids[i], &newcon, &len);
759 if (rc) {
760 length = rc;
761 goto out3;
762 }
763 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
764 kfree(newcon);
765 length = -ERANGE;
766 goto out3;
767 }
768 memcpy(ptr, newcon, len);
769 kfree(newcon);
770 ptr += len;
771 length += len;
772 }
773out3:
774 kfree(sids);
775out2:
776 kfree(user);
777out:
778 kfree(con);
779 return length;
780}
781
Eric Paris18729812008-04-17 14:15:45 -0400782static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 char *scon, *tcon;
785 u32 ssid, tsid, newsid;
786 u16 tclass;
787 ssize_t length;
788 char *newcon;
789 u32 len;
790
791 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
792 if (length)
793 return length;
794
795 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800796 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!scon)
798 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
James Morris89d155e2005-10-30 14:59:21 -0800800 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!tcon)
802 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 length = -EINVAL;
805 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
806 goto out2;
807
808 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
809 if (length < 0)
810 goto out2;
811 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
812 if (length < 0)
813 goto out2;
814
815 length = security_member_sid(ssid, tsid, tclass, &newsid);
816 if (length < 0)
817 goto out2;
818
819 length = security_sid_to_context(newsid, &newcon, &len);
820 if (length < 0)
821 goto out2;
822
823 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400824 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
825 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 length = -ERANGE;
827 goto out3;
828 }
829
830 memcpy(buf, newcon, len);
831 length = len;
832out3:
833 kfree(newcon);
834out2:
835 kfree(tcon);
836out:
837 kfree(scon);
838 return length;
839}
840
841static struct inode *sel_make_inode(struct super_block *sb, int mode)
842{
843 struct inode *ret = new_inode(sb);
844
845 if (ret) {
846 ret->i_mode = mode;
847 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ret->i_blocks = 0;
849 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
850 }
851 return ret;
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static ssize_t sel_read_bool(struct file *filep, char __user *buf,
855 size_t count, loff_t *ppos)
856{
857 char *page = NULL;
858 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ssize_t ret;
860 int cur_enforcing;
Stephen Smalleyd313f942007-11-26 11:12:53 -0500861 struct inode *inode = filep->f_path.dentry->d_inode;
862 unsigned index = inode->i_ino & SEL_INO_MASK;
863 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Ingo Molnarbb003072006-03-22 00:09:14 -0800865 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Stephen Smalleyd313f942007-11-26 11:12:53 -0500867 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
868 ret = -EINVAL;
869 goto out;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Davi Arnautbfd51622005-10-30 14:59:24 -0800872 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ret = -EINVAL;
874 goto out;
875 }
Eric Paris18729812008-04-17 14:15:45 -0400876 page = (char *)get_zeroed_page(GFP_KERNEL);
877 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ret = -ENOMEM;
879 goto out;
880 }
881
Stephen Smalleyd313f942007-11-26 11:12:53 -0500882 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (cur_enforcing < 0) {
884 ret = cur_enforcing;
885 goto out;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f942007-11-26 11:12:53 -0500888 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800889 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800891 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (page)
893 free_page((unsigned long)page);
894 return ret;
895}
896
897static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
898 size_t count, loff_t *ppos)
899{
900 char *page = NULL;
Stephen Smalleyd313f942007-11-26 11:12:53 -0500901 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int new_value;
Stephen Smalleyd313f942007-11-26 11:12:53 -0500903 struct inode *inode = filep->f_path.dentry->d_inode;
904 unsigned index = inode->i_ino & SEL_INO_MASK;
905 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ingo Molnarbb003072006-03-22 00:09:14 -0800907 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 length = task_has_security(current, SECURITY__SETBOOL);
910 if (length)
911 goto out;
912
Stephen Smalleyd313f942007-11-26 11:12:53 -0500913 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
914 length = -EINVAL;
915 goto out;
916 }
917
Davi Arnautbfd51622005-10-30 14:59:24 -0800918 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 length = -ENOMEM;
920 goto out;
921 }
Stephen Smalleyd313f942007-11-26 11:12:53 -0500922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (*ppos != 0) {
924 /* No partial writes. */
Stephen Smalleyd313f942007-11-26 11:12:53 -0500925 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
927 }
Eric Paris18729812008-04-17 14:15:45 -0400928 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!page) {
930 length = -ENOMEM;
931 goto out;
932 }
933
Stephen Smalleyd313f942007-11-26 11:12:53 -0500934 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (copy_from_user(page, buf, count))
936 goto out;
937
938 length = -EINVAL;
939 if (sscanf(page, "%d", &new_value) != 1)
940 goto out;
941
942 if (new_value)
943 new_value = 1;
944
Stephen Smalleyd313f942007-11-26 11:12:53 -0500945 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 length = count;
947
948out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800949 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (page)
951 free_page((unsigned long) page);
952 return length;
953}
954
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800955static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400956 .read = sel_read_bool,
957 .write = sel_write_bool,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958};
959
960static ssize_t sel_commit_bools_write(struct file *filep,
961 const char __user *buf,
962 size_t count, loff_t *ppos)
963{
964 char *page = NULL;
Stephen Smalleyd313f942007-11-26 11:12:53 -0500965 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int new_value;
967
Ingo Molnarbb003072006-03-22 00:09:14 -0800968 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 length = task_has_security(current, SECURITY__SETBOOL);
971 if (length)
972 goto out;
973
Davi Arnautbfd51622005-10-30 14:59:24 -0800974 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 length = -ENOMEM;
976 goto out;
977 }
978 if (*ppos != 0) {
979 /* No partial writes. */
980 goto out;
981 }
Eric Paris18729812008-04-17 14:15:45 -0400982 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!page) {
984 length = -ENOMEM;
985 goto out;
986 }
987
Stephen Smalleyd313f942007-11-26 11:12:53 -0500988 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (copy_from_user(page, buf, count))
990 goto out;
991
992 length = -EINVAL;
993 if (sscanf(page, "%d", &new_value) != 1)
994 goto out;
995
Eric Paris18729812008-04-17 14:15:45 -0400996 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 length = count;
1000
1001out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001002 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (page)
1004 free_page((unsigned long) page);
1005 return length;
1006}
1007
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001008static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001009 .write = sel_commit_bools_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010};
1011
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001012static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Stephen Smalley0955dc02007-11-21 09:01:36 -05001014 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 spin_lock(&dcache_lock);
1017 node = de->d_subdirs.next;
1018 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001019 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 list_del_init(node);
1021
1022 if (d->d_inode) {
1023 d = dget_locked(d);
1024 spin_unlock(&dcache_lock);
1025 d_delete(d);
1026 simple_unlink(de->d_inode, d);
1027 dput(d);
1028 spin_lock(&dcache_lock);
1029 }
1030 node = de->d_subdirs.next;
1031 }
1032
1033 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036#define BOOL_DIR_NAME "booleans"
1037
1038static int sel_make_bools(void)
1039{
1040 int i, ret = 0;
1041 ssize_t len;
1042 struct dentry *dentry = NULL;
1043 struct dentry *dir = bool_dir;
1044 struct inode *inode = NULL;
1045 struct inode_security_struct *isec;
1046 char **names = NULL, *page;
1047 int num;
1048 int *values = NULL;
1049 u32 sid;
1050
1051 /* remove any existing files */
Stephen Smalleyd313f942007-11-26 11:12:53 -05001052 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001053 kfree(bool_pending_values);
Stephen Smalleyd313f942007-11-26 11:12:53 -05001054 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001055 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001057 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric Paris18729812008-04-17 14:15:45 -04001059 page = (char *)get_zeroed_page(GFP_KERNEL);
1060 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -ENOMEM;
1062
1063 ret = security_get_bools(&num, &names, &values);
1064 if (ret != 0)
1065 goto out;
1066
1067 for (i = 0; i < num; i++) {
1068 dentry = d_alloc_name(dir, names[i]);
1069 if (!dentry) {
1070 ret = -ENOMEM;
1071 goto err;
1072 }
1073 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1074 if (!inode) {
1075 ret = -ENOMEM;
1076 goto err;
1077 }
1078
1079 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1080 if (len < 0) {
1081 ret = -EINVAL;
1082 goto err;
1083 } else if (len >= PAGE_SIZE) {
1084 ret = -ENAMETOOLONG;
1085 goto err;
1086 }
Eric Paris18729812008-04-17 14:15:45 -04001087 isec = (struct inode_security_struct *)inode->i_security;
1088 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1089 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto err;
1091 isec->sid = sid;
1092 isec->initialized = 1;
1093 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001094 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 d_add(dentry, inode);
1096 }
1097 bool_num = num;
Stephen Smalleyd313f942007-11-26 11:12:53 -05001098 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 bool_pending_values = values;
1100out:
1101 free_page((unsigned long)page);
Stephen Smalleyd313f942007-11-26 11:12:53 -05001102 return ret;
1103err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001105 for (i = 0; i < num; i++)
1106 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 kfree(names);
1108 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001109 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001110 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ret = -ENOMEM;
1112 goto out;
1113}
1114
1115#define NULL_FILE_NAME "null"
1116
Eric Paris18729812008-04-17 14:15:45 -04001117struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1120 size_t count, loff_t *ppos)
1121{
1122 char tmpbuf[TMPBUFLEN];
1123 ssize_t length;
1124
1125 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1126 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1127}
1128
Eric Paris18729812008-04-17 14:15:45 -04001129static ssize_t sel_write_avc_cache_threshold(struct file *file,
1130 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 size_t count, loff_t *ppos)
1132
1133{
1134 char *page;
1135 ssize_t ret;
1136 int new_value;
1137
Davi Arnautbfd51622005-10-30 14:59:24 -08001138 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ret = -ENOMEM;
1140 goto out;
1141 }
1142
1143 if (*ppos != 0) {
1144 /* No partial writes. */
1145 ret = -EINVAL;
1146 goto out;
1147 }
1148
Eric Paris18729812008-04-17 14:15:45 -04001149 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (!page) {
1151 ret = -ENOMEM;
1152 goto out;
1153 }
1154
1155 if (copy_from_user(page, buf, count)) {
1156 ret = -EFAULT;
1157 goto out_free;
1158 }
1159
1160 if (sscanf(page, "%u", &new_value) != 1) {
1161 ret = -EINVAL;
1162 goto out;
1163 }
1164
1165 if (new_value != avc_cache_threshold) {
1166 ret = task_has_security(current, SECURITY__SETSECPARAM);
1167 if (ret)
1168 goto out_free;
1169 avc_cache_threshold = new_value;
1170 }
1171 ret = count;
1172out_free:
1173 free_page((unsigned long)page);
1174out:
1175 return ret;
1176}
1177
1178static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1179 size_t count, loff_t *ppos)
1180{
1181 char *page;
1182 ssize_t ret = 0;
1183
1184 page = (char *)__get_free_page(GFP_KERNEL);
1185 if (!page) {
1186 ret = -ENOMEM;
1187 goto out;
1188 }
1189 ret = avc_get_hash_stats(page);
1190 if (ret >= 0)
1191 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1192 free_page((unsigned long)page);
1193out:
1194 return ret;
1195}
1196
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001197static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 .read = sel_read_avc_cache_threshold,
1199 .write = sel_write_avc_cache_threshold,
1200};
1201
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001202static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 .read = sel_read_avc_hash_stats,
1204};
1205
1206#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1207static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1208{
1209 int cpu;
1210
1211 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1212 if (!cpu_possible(cpu))
1213 continue;
1214 *idx = cpu + 1;
1215 return &per_cpu(avc_cache_stats, cpu);
1216 }
1217 return NULL;
1218}
1219
1220static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1221{
1222 loff_t n = *pos - 1;
1223
1224 if (*pos == 0)
1225 return SEQ_START_TOKEN;
1226
1227 return sel_avc_get_stat_idx(&n);
1228}
1229
1230static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1231{
1232 return sel_avc_get_stat_idx(pos);
1233}
1234
1235static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1236{
1237 struct avc_cache_stats *st = v;
1238
1239 if (v == SEQ_START_TOKEN)
1240 seq_printf(seq, "lookups hits misses allocations reclaims "
1241 "frees\n");
1242 else
1243 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1244 st->hits, st->misses, st->allocations,
1245 st->reclaims, st->frees);
1246 return 0;
1247}
1248
1249static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1250{ }
1251
Jan Engelhardt1996a102008-01-23 00:02:58 +01001252static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .start = sel_avc_stats_seq_start,
1254 .next = sel_avc_stats_seq_next,
1255 .show = sel_avc_stats_seq_show,
1256 .stop = sel_avc_stats_seq_stop,
1257};
1258
1259static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1260{
1261 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1262}
1263
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001264static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 .open = sel_open_avc_cache_stats,
1266 .read = seq_read,
1267 .llseek = seq_lseek,
1268 .release = seq_release,
1269};
1270#endif
1271
1272static int sel_make_avc_files(struct dentry *dir)
1273{
1274 int i, ret = 0;
1275 static struct tree_descr files[] = {
1276 { "cache_threshold",
1277 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1278 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1279#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1280 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1281#endif
1282 };
1283
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001284 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct inode *inode;
1286 struct dentry *dentry;
1287
1288 dentry = d_alloc_name(dir, files[i].name);
1289 if (!dentry) {
1290 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1295 if (!inode) {
1296 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001300 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 d_add(dentry, inode);
1302 }
1303out:
1304 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Eric Paris18729812008-04-17 14:15:45 -04001307static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001308 size_t count, loff_t *ppos)
1309{
1310 struct inode *inode;
1311 char *con;
1312 u32 sid, len;
1313 ssize_t ret;
1314
1315 inode = file->f_path.dentry->d_inode;
1316 sid = inode->i_ino&SEL_INO_MASK;
1317 ret = security_sid_to_context(sid, &con, &len);
1318 if (ret < 0)
1319 return ret;
1320
1321 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1322 kfree(con);
1323 return ret;
1324}
1325
1326static const struct file_operations sel_initcon_ops = {
1327 .read = sel_read_initcon,
1328};
1329
1330static int sel_make_initcon_files(struct dentry *dir)
1331{
1332 int i, ret = 0;
1333
1334 for (i = 1; i <= SECINITSID_NUM; i++) {
1335 struct inode *inode;
1336 struct dentry *dentry;
1337 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1338 if (!dentry) {
1339 ret = -ENOMEM;
1340 goto out;
1341 }
1342
1343 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1344 if (!inode) {
1345 ret = -ENOMEM;
1346 goto out;
1347 }
1348 inode->i_fop = &sel_initcon_ops;
1349 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1350 d_add(dentry, inode);
1351 }
1352out:
1353 return ret;
1354}
1355
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001356static inline unsigned int sel_div(unsigned long a, unsigned long b)
1357{
1358 return a / b - (a % b < 0);
1359}
1360
1361static inline unsigned long sel_class_to_ino(u16 class)
1362{
1363 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1364}
1365
1366static inline u16 sel_ino_to_class(unsigned long ino)
1367{
1368 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1369}
1370
1371static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1372{
1373 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1374}
1375
1376static inline u32 sel_ino_to_perm(unsigned long ino)
1377{
1378 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1379}
1380
Eric Paris18729812008-04-17 14:15:45 -04001381static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001382 size_t count, loff_t *ppos)
1383{
1384 ssize_t rc, len;
1385 char *page;
1386 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1387
1388 page = (char *)__get_free_page(GFP_KERNEL);
1389 if (!page) {
1390 rc = -ENOMEM;
1391 goto out;
1392 }
1393
1394 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1395 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1396 free_page((unsigned long)page);
1397out:
1398 return rc;
1399}
1400
1401static const struct file_operations sel_class_ops = {
1402 .read = sel_read_class,
1403};
1404
Eric Paris18729812008-04-17 14:15:45 -04001405static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001406 size_t count, loff_t *ppos)
1407{
1408 ssize_t rc, len;
1409 char *page;
1410 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1411
1412 page = (char *)__get_free_page(GFP_KERNEL);
1413 if (!page) {
1414 rc = -ENOMEM;
1415 goto out;
1416 }
1417
Eric Paris18729812008-04-17 14:15:45 -04001418 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001419 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1420 free_page((unsigned long)page);
1421out:
1422 return rc;
1423}
1424
1425static const struct file_operations sel_perm_ops = {
1426 .read = sel_read_perm,
1427};
1428
Paul Moore3bb56b22008-01-29 08:38:19 -05001429static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1430 size_t count, loff_t *ppos)
1431{
1432 int value;
1433 char tmpbuf[TMPBUFLEN];
1434 ssize_t length;
1435 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1436
1437 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1438 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1439
1440 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1441}
1442
1443static const struct file_operations sel_policycap_ops = {
1444 .read = sel_read_policycap,
1445};
1446
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001447static int sel_make_perm_files(char *objclass, int classvalue,
1448 struct dentry *dir)
1449{
1450 int i, rc = 0, nperms;
1451 char **perms;
1452
1453 rc = security_get_permissions(objclass, &perms, &nperms);
1454 if (rc)
1455 goto out;
1456
1457 for (i = 0; i < nperms; i++) {
1458 struct inode *inode;
1459 struct dentry *dentry;
1460
1461 dentry = d_alloc_name(dir, perms[i]);
1462 if (!dentry) {
1463 rc = -ENOMEM;
1464 goto out1;
1465 }
1466
1467 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1468 if (!inode) {
1469 rc = -ENOMEM;
1470 goto out1;
1471 }
1472 inode->i_fop = &sel_perm_ops;
1473 /* i+1 since perm values are 1-indexed */
1474 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1475 d_add(dentry, inode);
1476 }
1477
1478out1:
1479 for (i = 0; i < nperms; i++)
1480 kfree(perms[i]);
1481 kfree(perms);
1482out:
1483 return rc;
1484}
1485
1486static int sel_make_class_dir_entries(char *classname, int index,
1487 struct dentry *dir)
1488{
1489 struct dentry *dentry = NULL;
1490 struct inode *inode = NULL;
1491 int rc;
1492
1493 dentry = d_alloc_name(dir, "index");
1494 if (!dentry) {
1495 rc = -ENOMEM;
1496 goto out;
1497 }
1498
1499 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1500 if (!inode) {
1501 rc = -ENOMEM;
1502 goto out;
1503 }
1504
1505 inode->i_fop = &sel_class_ops;
1506 inode->i_ino = sel_class_to_ino(index);
1507 d_add(dentry, inode);
1508
1509 dentry = d_alloc_name(dir, "perms");
1510 if (!dentry) {
1511 rc = -ENOMEM;
1512 goto out;
1513 }
1514
1515 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1516 if (rc)
1517 goto out;
1518
1519 rc = sel_make_perm_files(classname, index, dentry);
1520
1521out:
1522 return rc;
1523}
1524
1525static void sel_remove_classes(void)
1526{
1527 struct list_head *class_node;
1528
1529 list_for_each(class_node, &class_dir->d_subdirs) {
1530 struct dentry *class_subdir = list_entry(class_node,
1531 struct dentry, d_u.d_child);
1532 struct list_head *class_subdir_node;
1533
1534 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1535 struct dentry *d = list_entry(class_subdir_node,
1536 struct dentry, d_u.d_child);
1537
1538 if (d->d_inode)
1539 if (d->d_inode->i_mode & S_IFDIR)
1540 sel_remove_entries(d);
1541 }
1542
1543 sel_remove_entries(class_subdir);
1544 }
1545
1546 sel_remove_entries(class_dir);
1547}
1548
1549static int sel_make_classes(void)
1550{
1551 int rc = 0, nclasses, i;
1552 char **classes;
1553
1554 /* delete any existing entries */
1555 sel_remove_classes();
1556
1557 rc = security_get_classes(&classes, &nclasses);
1558 if (rc < 0)
1559 goto out;
1560
1561 /* +2 since classes are 1-indexed */
1562 last_class_ino = sel_class_to_ino(nclasses+2);
1563
1564 for (i = 0; i < nclasses; i++) {
1565 struct dentry *class_name_dir;
1566
1567 class_name_dir = d_alloc_name(class_dir, classes[i]);
1568 if (!class_name_dir) {
1569 rc = -ENOMEM;
1570 goto out1;
1571 }
1572
1573 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1574 &last_class_ino);
1575 if (rc)
1576 goto out1;
1577
1578 /* i+1 since class values are 1-indexed */
1579 rc = sel_make_class_dir_entries(classes[i], i+1,
1580 class_name_dir);
1581 if (rc)
1582 goto out1;
1583 }
1584
1585out1:
1586 for (i = 0; i < nclasses; i++)
1587 kfree(classes[i]);
1588 kfree(classes);
1589out:
1590 return rc;
1591}
1592
Paul Moore3bb56b22008-01-29 08:38:19 -05001593static int sel_make_policycap(void)
1594{
1595 unsigned int iter;
1596 struct dentry *dentry = NULL;
1597 struct inode *inode = NULL;
1598
1599 sel_remove_entries(policycap_dir);
1600
1601 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1602 if (iter < ARRAY_SIZE(policycap_names))
1603 dentry = d_alloc_name(policycap_dir,
1604 policycap_names[iter]);
1605 else
1606 dentry = d_alloc_name(policycap_dir, "unknown");
1607
1608 if (dentry == NULL)
1609 return -ENOMEM;
1610
1611 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1612 if (inode == NULL)
1613 return -ENOMEM;
1614
1615 inode->i_fop = &sel_policycap_ops;
1616 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1617 d_add(dentry, inode);
1618 }
1619
1620 return 0;
1621}
1622
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001623static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1624 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 int ret = 0;
1627 struct inode *inode;
1628
James Morrisedb20fb2006-03-22 00:09:20 -08001629 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (!inode) {
1631 ret = -ENOMEM;
1632 goto out;
1633 }
1634 inode->i_op = &simple_dir_inode_operations;
1635 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001636 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001637 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001638 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001640 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001641 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642out:
1643 return ret;
1644}
1645
Eric Paris18729812008-04-17 14:15:45 -04001646static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 int ret;
1649 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001650 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct inode_security_struct *isec;
1652
1653 static struct tree_descr selinux_files[] = {
1654 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1655 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001656 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1658 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1659 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1660 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1661 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1662 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1663 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1664 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1665 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1666 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001667 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001668 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1669 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* last one */ {""}
1671 };
1672 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1673 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001674 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
James Morrisedb20fb2006-03-22 00:09:20 -08001676 root_inode = sb->s_root->d_inode;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001679 if (!dentry) {
1680 ret = -ENOMEM;
1681 goto err;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001684 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001685 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001686 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001691 if (!dentry) {
1692 ret = -ENOMEM;
1693 goto err;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001697 if (!inode) {
1698 ret = -ENOMEM;
1699 goto err;
1700 }
James Carter6174eaf2007-04-04 16:18:39 -04001701 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001702 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 isec->sid = SECINITSID_DEVNULL;
1704 isec->sclass = SECCLASS_CHR_FILE;
1705 isec->initialized = 1;
1706
1707 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1708 d_add(dentry, inode);
1709 selinux_null = dentry;
1710
1711 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001712 if (!dentry) {
1713 ret = -ENOMEM;
1714 goto err;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001717 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001719 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 ret = sel_make_avc_files(dentry);
1722 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001723 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001724
1725 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1726 if (!dentry) {
1727 ret = -ENOMEM;
1728 goto err;
1729 }
1730
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001731 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001732 if (ret)
1733 goto err;
1734
1735 ret = sel_make_initcon_files(dentry);
1736 if (ret)
1737 goto err;
1738
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001739 dentry = d_alloc_name(sb->s_root, "class");
1740 if (!dentry) {
1741 ret = -ENOMEM;
1742 goto err;
1743 }
1744
1745 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1746 if (ret)
1747 goto err;
1748
1749 class_dir = dentry;
1750
Paul Moore3bb56b22008-01-29 08:38:19 -05001751 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1752 if (!dentry) {
1753 ret = -ENOMEM;
1754 goto err;
1755 }
1756
1757 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1758 if (ret)
1759 goto err;
1760
1761 policycap_dir = dentry;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763out:
James Morris161ce452006-03-22 00:09:17 -08001764 return ret;
1765err:
Eric Paris744ba352008-04-17 11:52:44 -04001766 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1767 __func__);
James Morris161ce452006-03-22 00:09:17 -08001768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
David Howells454e2392006-06-23 02:02:57 -07001771static int sel_get_sb(struct file_system_type *fs_type,
1772 int flags, const char *dev_name, void *data,
1773 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
David Howells454e2392006-06-23 02:02:57 -07001775 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778static struct file_system_type sel_fs_type = {
1779 .name = "selinuxfs",
1780 .get_sb = sel_get_sb,
1781 .kill_sb = kill_litter_super,
1782};
1783
1784struct vfsmount *selinuxfs_mount;
1785
1786static int __init init_sel_fs(void)
1787{
1788 int err;
1789
1790 if (!selinux_enabled)
1791 return 0;
1792 err = register_filesystem(&sel_fs_type);
1793 if (!err) {
1794 selinuxfs_mount = kern_mount(&sel_fs_type);
1795 if (IS_ERR(selinuxfs_mount)) {
1796 printk(KERN_ERR "selinuxfs: could not mount!\n");
1797 err = PTR_ERR(selinuxfs_mount);
1798 selinuxfs_mount = NULL;
1799 }
1800 }
1801 return err;
1802}
1803
1804__initcall(init_sel_fs);
1805
1806#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1807void exit_sel_fs(void)
1808{
1809 unregister_filesystem(&sel_fs_type);
1810}
1811#endif