blob: a1cfc464bbb9985aebeddbb11239647f2be2b598 [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
50static int __init checkreqprot_setup(char *str)
51{
Eric Parisf5269712008-05-14 11:27:45 -040052 unsigned long checkreqprot;
53 if (!strict_strtoul(str, 0, &checkreqprot))
54 selinux_checkreqprot = checkreqprot ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return 1;
56}
57__setup("checkreqprot=", checkreqprot_setup);
58
Ingo Molnarbb003072006-03-22 00:09:14 -080059static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* global data for booleans */
Eric Paris18729812008-04-17 14:15:45 -040062static struct dentry *bool_dir;
63static int bool_num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -050064static char **bool_pending_names;
Eric Paris18729812008-04-17 14:15:45 -040065static int *bool_pending_values;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040067/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040068static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040069static unsigned long last_class_ino;
70
Paul Moore3bb56b22008-01-29 08:38:19 -050071/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040072static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074extern void selnl_notify_setenforce(int val);
75
76/* Check whether a task is allowed to use a security operation. */
77static int task_has_security(struct task_struct *tsk,
78 u32 perms)
79{
David Howellsc69e8d92008-11-14 10:39:19 +110080 const struct task_security_struct *tsec;
81 u32 sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
David Howellsc69e8d92008-11-14 10:39:19 +110083 rcu_read_lock();
84 tsec = __task_cred(tsk)->security;
85 if (tsec)
86 sid = tsec->sid;
87 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!tsec)
89 return -EACCES;
90
David Howellsc69e8d92008-11-14 10:39:19 +110091 return avc_has_perm(sid, SECINITSID_SECURITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 SECCLASS_SECURITY, perms, NULL);
93}
94
95enum sel_inos {
96 SEL_ROOT_INO = 2,
97 SEL_LOAD, /* load policy */
98 SEL_ENFORCE, /* get or set enforcing status */
99 SEL_CONTEXT, /* validate context */
100 SEL_ACCESS, /* compute access decision */
101 SEL_CREATE, /* compute create labeling decision */
102 SEL_RELABEL, /* compute relabeling decision */
103 SEL_USER, /* compute reachable user contexts */
104 SEL_POLICYVERS, /* return policy version for this kernel */
105 SEL_COMMIT_BOOLS, /* commit new boolean values */
106 SEL_MLS, /* return if MLS policy is enabled */
107 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 SEL_MEMBER, /* compute polyinstantiation membership decision */
109 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700110 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400111 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
112 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
James Carter6174eaf2007-04-04 16:18:39 -0400113 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
James Carter6174eaf2007-04-04 16:18:39 -0400116static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
117
Paul Moore3bb56b22008-01-29 08:38:19 -0500118#define SEL_INITCON_INO_OFFSET 0x01000000
119#define SEL_BOOL_INO_OFFSET 0x02000000
120#define SEL_CLASS_INO_OFFSET 0x04000000
121#define SEL_POLICYCAP_INO_OFFSET 0x08000000
122#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TMPBUFLEN 12
125static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
126 size_t count, loff_t *ppos)
127{
128 char tmpbuf[TMPBUFLEN];
129 ssize_t length;
130
131 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
132 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
133}
134
135#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400136static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 size_t count, loff_t *ppos)
138
139{
140 char *page;
141 ssize_t length;
142 int new_value;
143
Davi Arnautbfd51622005-10-30 14:59:24 -0800144 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -ENOMEM;
146 if (*ppos != 0) {
147 /* No partial writes. */
148 return -EINVAL;
149 }
Eric Paris18729812008-04-17 14:15:45 -0400150 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!page)
152 return -ENOMEM;
153 length = -EFAULT;
154 if (copy_from_user(page, buf, count))
155 goto out;
156
157 length = -EINVAL;
158 if (sscanf(page, "%d", &new_value) != 1)
159 goto out;
160
161 if (new_value != selinux_enforcing) {
162 length = task_has_security(current, SECURITY__SETENFORCE);
163 if (length)
164 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000165 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500166 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
167 new_value, selinux_enforcing,
168 audit_get_loginuid(current),
169 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 selinux_enforcing = new_value;
171 if (selinux_enforcing)
172 avc_ss_reset(0);
173 selnl_notify_setenforce(selinux_enforcing);
174 }
175 length = count;
176out:
177 free_page((unsigned long) page);
178 return length;
179}
180#else
181#define sel_write_enforce NULL
182#endif
183
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800184static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .read = sel_read_enforce,
186 .write = sel_write_enforce,
187};
188
Eric Paris3f120702007-09-21 14:37:10 -0400189static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
190 size_t count, loff_t *ppos)
191{
192 char tmpbuf[TMPBUFLEN];
193 ssize_t length;
194 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
195 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
196 security_get_reject_unknown() : !security_get_allow_unknown();
197
198 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
199 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
200}
201
202static const struct file_operations sel_handle_unknown_ops = {
203 .read = sel_read_handle_unknown,
204};
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400207static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 size_t count, loff_t *ppos)
209
210{
211 char *page;
212 ssize_t length;
213 int new_value;
214 extern int selinux_disable(void);
215
Davi Arnautbfd51622005-10-30 14:59:24 -0800216 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return -ENOMEM;
218 if (*ppos != 0) {
219 /* No partial writes. */
220 return -EINVAL;
221 }
Eric Paris18729812008-04-17 14:15:45 -0400222 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!page)
224 return -ENOMEM;
225 length = -EFAULT;
226 if (copy_from_user(page, buf, count))
227 goto out;
228
229 length = -EINVAL;
230 if (sscanf(page, "%d", &new_value) != 1)
231 goto out;
232
233 if (new_value) {
234 length = selinux_disable();
235 if (length < 0)
236 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000237 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500238 "selinux=0 auid=%u ses=%u",
239 audit_get_loginuid(current),
240 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 length = count;
244out:
245 free_page((unsigned long) page);
246 return length;
247}
248#else
249#define sel_write_disable NULL
250#endif
251
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800252static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .write = sel_write_disable,
254};
255
256static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400257 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 char tmpbuf[TMPBUFLEN];
260 ssize_t length;
261
262 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
263 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
264}
265
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800266static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .read = sel_read_policyvers,
268};
269
270/* declaration for sel_write_load */
271static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400272static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500273static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400274
275/* declaration for sel_make_class_dirs */
276static int sel_make_dir(struct inode *dir, struct dentry *dentry,
277 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279static ssize_t sel_read_mls(struct file *filp, char __user *buf,
280 size_t count, loff_t *ppos)
281{
282 char tmpbuf[TMPBUFLEN];
283 ssize_t length;
284
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100285 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
286 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
288}
289
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800290static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 .read = sel_read_mls,
292};
293
Eric Paris18729812008-04-17 14:15:45 -0400294static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 size_t count, loff_t *ppos)
296
297{
298 int ret;
299 ssize_t length;
300 void *data = NULL;
301
Ingo Molnarbb003072006-03-22 00:09:14 -0800302 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 length = task_has_security(current, SECURITY__LOAD_POLICY);
305 if (length)
306 goto out;
307
308 if (*ppos != 0) {
309 /* No partial writes. */
310 length = -EINVAL;
311 goto out;
312 }
313
Davi Arnautbfd51622005-10-30 14:59:24 -0800314 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 || (data = vmalloc(count)) == NULL) {
316 length = -ENOMEM;
317 goto out;
318 }
319
320 length = -EFAULT;
321 if (copy_from_user(data, buf, count) != 0)
322 goto out;
323
324 length = security_load_policy(data, count);
325 if (length)
326 goto out;
327
328 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400329 if (ret) {
330 length = ret;
331 goto out1;
332 }
333
334 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500335 if (ret) {
336 length = ret;
337 goto out1;
338 }
339
340 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (ret)
342 length = ret;
343 else
344 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400345
346out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000347 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500348 "policy loaded auid=%u ses=%u",
349 audit_get_loginuid(current),
350 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800352 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 vfree(data);
354 return length;
355}
356
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800357static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .write = sel_write_load,
359};
360
Eric Paris18729812008-04-17 14:15:45 -0400361static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800363 char *canon;
364 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ssize_t length;
366
367 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
368 if (length)
369 return length;
370
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800371 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800373 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800375 length = security_sid_to_context(sid, &canon, &len);
376 if (length < 0)
377 return length;
378
379 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400380 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
381 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800382 length = -ERANGE;
383 goto out;
384 }
385
386 memcpy(buf, canon, len);
387 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800389 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return length;
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
394 size_t count, loff_t *ppos)
395{
396 char tmpbuf[TMPBUFLEN];
397 ssize_t length;
398
399 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
400 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
401}
402
Eric Paris18729812008-04-17 14:15:45 -0400403static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 size_t count, loff_t *ppos)
405{
406 char *page;
407 ssize_t length;
408 unsigned int new_value;
409
410 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
411 if (length)
412 return length;
413
Davi Arnautbfd51622005-10-30 14:59:24 -0800414 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -ENOMEM;
416 if (*ppos != 0) {
417 /* No partial writes. */
418 return -EINVAL;
419 }
Eric Paris18729812008-04-17 14:15:45 -0400420 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!page)
422 return -ENOMEM;
423 length = -EFAULT;
424 if (copy_from_user(page, buf, count))
425 goto out;
426
427 length = -EINVAL;
428 if (sscanf(page, "%u", &new_value) != 1)
429 goto out;
430
431 selinux_checkreqprot = new_value ? 1 : 0;
432 length = count;
433out:
434 free_page((unsigned long) page);
435 return length;
436}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800437static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 .read = sel_read_checkreqprot,
439 .write = sel_write_checkreqprot,
440};
441
442/*
443 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
444 */
Eric Paris18729812008-04-17 14:15:45 -0400445static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
446static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
447static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
448static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
449static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451static ssize_t (*write_op[])(struct file *, char *, size_t) = {
452 [SEL_ACCESS] = sel_write_access,
453 [SEL_CREATE] = sel_write_create,
454 [SEL_RELABEL] = sel_write_relabel,
455 [SEL_USER] = sel_write_user,
456 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800457 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458};
459
460static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
461{
Eric Paris18729812008-04-17 14:15:45 -0400462 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 char *data;
464 ssize_t rv;
465
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800466 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
468
469 data = simple_transaction_get(file, buf, size);
470 if (IS_ERR(data))
471 return PTR_ERR(data);
472
Eric Paris18729812008-04-17 14:15:45 -0400473 rv = write_op[ino](file, data, size);
474 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 simple_transaction_set(file, rv);
476 rv = size;
477 }
478 return rv;
479}
480
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800481static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .write = selinux_transaction_write,
483 .read = simple_transaction_read,
484 .release = simple_transaction_release,
485};
486
487/*
488 * payload - write methods
489 * If the method has a response, the response should be put in buf,
490 * and the length returned. Otherwise return 0 or and -error.
491 */
492
Eric Paris18729812008-04-17 14:15:45 -0400493static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 char *scon, *tcon;
496 u32 ssid, tsid;
497 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct av_decision avd;
499 ssize_t length;
500
501 length = task_has_security(current, SECURITY__COMPUTE_AV);
502 if (length)
503 return length;
504
505 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800506 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!scon)
508 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
James Morris89d155e2005-10-30 14:59:21 -0800510 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!tcon)
512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500515 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out2;
517
518 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
519 if (length < 0)
520 goto out2;
521 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
522 if (length < 0)
523 goto out2;
524
Stephen Smalley19439d02010-01-14 17:28:10 -0500525 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900528 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500529 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900531 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532out2:
533 kfree(tcon);
534out:
535 kfree(scon);
536 return length;
537}
538
Eric Paris18729812008-04-17 14:15:45 -0400539static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 char *scon, *tcon;
542 u32 ssid, tsid, newsid;
543 u16 tclass;
544 ssize_t length;
545 char *newcon;
546 u32 len;
547
548 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
549 if (length)
550 return length;
551
552 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800553 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!scon)
555 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
James Morris89d155e2005-10-30 14:59:21 -0800557 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!tcon)
559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 length = -EINVAL;
562 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
563 goto out2;
564
565 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
566 if (length < 0)
567 goto out2;
568 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
569 if (length < 0)
570 goto out2;
571
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400572 length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (length < 0)
574 goto out2;
575
576 length = security_sid_to_context(newsid, &newcon, &len);
577 if (length < 0)
578 goto out2;
579
580 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400581 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
582 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 length = -ERANGE;
584 goto out3;
585 }
586
587 memcpy(buf, newcon, len);
588 length = len;
589out3:
590 kfree(newcon);
591out2:
592 kfree(tcon);
593out:
594 kfree(scon);
595 return length;
596}
597
Eric Paris18729812008-04-17 14:15:45 -0400598static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 char *scon, *tcon;
601 u32 ssid, tsid, newsid;
602 u16 tclass;
603 ssize_t length;
604 char *newcon;
605 u32 len;
606
607 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
608 if (length)
609 return length;
610
611 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800612 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!scon)
614 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
James Morris89d155e2005-10-30 14:59:21 -0800616 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!tcon)
618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 length = -EINVAL;
621 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
622 goto out2;
623
624 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
625 if (length < 0)
626 goto out2;
627 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
628 if (length < 0)
629 goto out2;
630
631 length = security_change_sid(ssid, tsid, tclass, &newsid);
632 if (length < 0)
633 goto out2;
634
635 length = security_sid_to_context(newsid, &newcon, &len);
636 if (length < 0)
637 goto out2;
638
639 if (len > SIMPLE_TRANSACTION_LIMIT) {
640 length = -ERANGE;
641 goto out3;
642 }
643
644 memcpy(buf, newcon, len);
645 length = len;
646out3:
647 kfree(newcon);
648out2:
649 kfree(tcon);
650out:
651 kfree(scon);
652 return length;
653}
654
Eric Paris18729812008-04-17 14:15:45 -0400655static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 char *con, *user, *ptr;
658 u32 sid, *sids;
659 ssize_t length;
660 char *newcon;
661 int i, rc;
662 u32 len, nsids;
663
664 length = task_has_security(current, SECURITY__COMPUTE_USER);
665 if (length)
666 return length;
667
668 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800669 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!con)
671 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
James Morris89d155e2005-10-30 14:59:21 -0800673 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!user)
675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 length = -EINVAL;
678 if (sscanf(buf, "%s %s", con, user) != 2)
679 goto out2;
680
681 length = security_context_to_sid(con, strlen(con)+1, &sid);
682 if (length < 0)
683 goto out2;
684
685 length = security_get_user_sids(sid, user, &sids, &nsids);
686 if (length < 0)
687 goto out2;
688
689 length = sprintf(buf, "%u", nsids) + 1;
690 ptr = buf + length;
691 for (i = 0; i < nsids; i++) {
692 rc = security_sid_to_context(sids[i], &newcon, &len);
693 if (rc) {
694 length = rc;
695 goto out3;
696 }
697 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
698 kfree(newcon);
699 length = -ERANGE;
700 goto out3;
701 }
702 memcpy(ptr, newcon, len);
703 kfree(newcon);
704 ptr += len;
705 length += len;
706 }
707out3:
708 kfree(sids);
709out2:
710 kfree(user);
711out:
712 kfree(con);
713 return length;
714}
715
Eric Paris18729812008-04-17 14:15:45 -0400716static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 char *scon, *tcon;
719 u32 ssid, tsid, newsid;
720 u16 tclass;
721 ssize_t length;
722 char *newcon;
723 u32 len;
724
725 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
726 if (length)
727 return length;
728
729 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800730 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!scon)
732 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
James Morris89d155e2005-10-30 14:59:21 -0800734 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!tcon)
736 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 length = -EINVAL;
739 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
740 goto out2;
741
742 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
743 if (length < 0)
744 goto out2;
745 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
746 if (length < 0)
747 goto out2;
748
749 length = security_member_sid(ssid, tsid, tclass, &newsid);
750 if (length < 0)
751 goto out2;
752
753 length = security_sid_to_context(newsid, &newcon, &len);
754 if (length < 0)
755 goto out2;
756
757 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400758 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
759 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 length = -ERANGE;
761 goto out3;
762 }
763
764 memcpy(buf, newcon, len);
765 length = len;
766out3:
767 kfree(newcon);
768out2:
769 kfree(tcon);
770out:
771 kfree(scon);
772 return length;
773}
774
775static struct inode *sel_make_inode(struct super_block *sb, int mode)
776{
777 struct inode *ret = new_inode(sb);
778
779 if (ret) {
780 ret->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
782 }
783 return ret;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static ssize_t sel_read_bool(struct file *filep, char __user *buf,
787 size_t count, loff_t *ppos)
788{
789 char *page = NULL;
790 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ssize_t ret;
792 int cur_enforcing;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500793 struct inode *inode = filep->f_path.dentry->d_inode;
794 unsigned index = inode->i_ino & SEL_INO_MASK;
795 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Ingo Molnarbb003072006-03-22 00:09:14 -0800797 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500799 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
800 ret = -EINVAL;
801 goto out;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Eric Paris18729812008-04-17 14:15:45 -0400804 page = (char *)get_zeroed_page(GFP_KERNEL);
805 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ret = -ENOMEM;
807 goto out;
808 }
809
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500810 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (cur_enforcing < 0) {
812 ret = cur_enforcing;
813 goto out;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500816 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800817 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800819 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (page)
821 free_page((unsigned long)page);
822 return ret;
823}
824
825static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
826 size_t count, loff_t *ppos)
827{
828 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500829 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int new_value;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500831 struct inode *inode = filep->f_path.dentry->d_inode;
832 unsigned index = inode->i_ino & SEL_INO_MASK;
833 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Ingo Molnarbb003072006-03-22 00:09:14 -0800835 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 length = task_has_security(current, SECURITY__SETBOOL);
838 if (length)
839 goto out;
840
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500841 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
842 length = -EINVAL;
843 goto out;
844 }
845
Davi Arnautbfd51622005-10-30 14:59:24 -0800846 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 length = -ENOMEM;
848 goto out;
849 }
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (*ppos != 0) {
852 /* No partial writes. */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500853 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto out;
855 }
Eric Paris18729812008-04-17 14:15:45 -0400856 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!page) {
858 length = -ENOMEM;
859 goto out;
860 }
861
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500862 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (copy_from_user(page, buf, count))
864 goto out;
865
866 length = -EINVAL;
867 if (sscanf(page, "%d", &new_value) != 1)
868 goto out;
869
870 if (new_value)
871 new_value = 1;
872
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500873 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 length = count;
875
876out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800877 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (page)
879 free_page((unsigned long) page);
880 return length;
881}
882
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800883static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400884 .read = sel_read_bool,
885 .write = sel_write_bool,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886};
887
888static ssize_t sel_commit_bools_write(struct file *filep,
889 const char __user *buf,
890 size_t count, loff_t *ppos)
891{
892 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500893 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int new_value;
895
Ingo Molnarbb003072006-03-22 00:09:14 -0800896 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 length = task_has_security(current, SECURITY__SETBOOL);
899 if (length)
900 goto out;
901
Davi Arnautbfd51622005-10-30 14:59:24 -0800902 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 length = -ENOMEM;
904 goto out;
905 }
906 if (*ppos != 0) {
907 /* No partial writes. */
908 goto out;
909 }
Eric Paris18729812008-04-17 14:15:45 -0400910 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!page) {
912 length = -ENOMEM;
913 goto out;
914 }
915
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500916 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (copy_from_user(page, buf, count))
918 goto out;
919
920 length = -EINVAL;
921 if (sscanf(page, "%d", &new_value) != 1)
922 goto out;
923
Eric Paris18729812008-04-17 14:15:45 -0400924 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 length = count;
928
929out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800930 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (page)
932 free_page((unsigned long) page);
933 return length;
934}
935
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800936static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400937 .write = sel_commit_bools_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938};
939
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -0400940static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Stephen Smalley0955dc02007-11-21 09:01:36 -0500942 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 spin_lock(&dcache_lock);
945 node = de->d_subdirs.next;
946 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800947 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 list_del_init(node);
949
950 if (d->d_inode) {
951 d = dget_locked(d);
952 spin_unlock(&dcache_lock);
953 d_delete(d);
954 simple_unlink(de->d_inode, d);
955 dput(d);
956 spin_lock(&dcache_lock);
957 }
958 node = de->d_subdirs.next;
959 }
960
961 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964#define BOOL_DIR_NAME "booleans"
965
966static int sel_make_bools(void)
967{
968 int i, ret = 0;
969 ssize_t len;
970 struct dentry *dentry = NULL;
971 struct dentry *dir = bool_dir;
972 struct inode *inode = NULL;
973 struct inode_security_struct *isec;
974 char **names = NULL, *page;
975 int num;
976 int *values = NULL;
977 u32 sid;
978
979 /* remove any existing files */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500980 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700981 kfree(bool_pending_values);
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500982 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -0700983 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -0400985 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Eric Paris18729812008-04-17 14:15:45 -0400987 page = (char *)get_zeroed_page(GFP_KERNEL);
988 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return -ENOMEM;
990
991 ret = security_get_bools(&num, &names, &values);
992 if (ret != 0)
993 goto out;
994
995 for (i = 0; i < num; i++) {
996 dentry = d_alloc_name(dir, names[i]);
997 if (!dentry) {
998 ret = -ENOMEM;
999 goto err;
1000 }
1001 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1002 if (!inode) {
1003 ret = -ENOMEM;
1004 goto err;
1005 }
1006
1007 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1008 if (len < 0) {
1009 ret = -EINVAL;
1010 goto err;
1011 } else if (len >= PAGE_SIZE) {
1012 ret = -ENAMETOOLONG;
1013 goto err;
1014 }
Eric Paris18729812008-04-17 14:15:45 -04001015 isec = (struct inode_security_struct *)inode->i_security;
1016 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1017 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto err;
1019 isec->sid = sid;
1020 isec->initialized = 1;
1021 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001022 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 d_add(dentry, inode);
1024 }
1025 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001026 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 bool_pending_values = values;
1028out:
1029 free_page((unsigned long)page);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001030 return ret;
1031err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001033 for (i = 0; i < num; i++)
1034 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 kfree(names);
1036 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001037 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001038 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ret = -ENOMEM;
1040 goto out;
1041}
1042
1043#define NULL_FILE_NAME "null"
1044
Eric Paris18729812008-04-17 14:15:45 -04001045struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1048 size_t count, loff_t *ppos)
1049{
1050 char tmpbuf[TMPBUFLEN];
1051 ssize_t length;
1052
1053 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1054 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1055}
1056
Eric Paris18729812008-04-17 14:15:45 -04001057static ssize_t sel_write_avc_cache_threshold(struct file *file,
1058 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 size_t count, loff_t *ppos)
1060
1061{
1062 char *page;
1063 ssize_t ret;
1064 int new_value;
1065
Davi Arnautbfd51622005-10-30 14:59:24 -08001066 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ret = -ENOMEM;
1068 goto out;
1069 }
1070
1071 if (*ppos != 0) {
1072 /* No partial writes. */
1073 ret = -EINVAL;
1074 goto out;
1075 }
1076
Eric Paris18729812008-04-17 14:15:45 -04001077 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!page) {
1079 ret = -ENOMEM;
1080 goto out;
1081 }
1082
1083 if (copy_from_user(page, buf, count)) {
1084 ret = -EFAULT;
1085 goto out_free;
1086 }
1087
1088 if (sscanf(page, "%u", &new_value) != 1) {
1089 ret = -EINVAL;
1090 goto out;
1091 }
1092
1093 if (new_value != avc_cache_threshold) {
1094 ret = task_has_security(current, SECURITY__SETSECPARAM);
1095 if (ret)
1096 goto out_free;
1097 avc_cache_threshold = new_value;
1098 }
1099 ret = count;
1100out_free:
1101 free_page((unsigned long)page);
1102out:
1103 return ret;
1104}
1105
1106static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1107 size_t count, loff_t *ppos)
1108{
1109 char *page;
1110 ssize_t ret = 0;
1111
1112 page = (char *)__get_free_page(GFP_KERNEL);
1113 if (!page) {
1114 ret = -ENOMEM;
1115 goto out;
1116 }
1117 ret = avc_get_hash_stats(page);
1118 if (ret >= 0)
1119 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1120 free_page((unsigned long)page);
1121out:
1122 return ret;
1123}
1124
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001125static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 .read = sel_read_avc_cache_threshold,
1127 .write = sel_write_avc_cache_threshold,
1128};
1129
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001130static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 .read = sel_read_avc_hash_stats,
1132};
1133
1134#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1135static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1136{
1137 int cpu;
1138
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301139 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!cpu_possible(cpu))
1141 continue;
1142 *idx = cpu + 1;
1143 return &per_cpu(avc_cache_stats, cpu);
1144 }
1145 return NULL;
1146}
1147
1148static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1149{
1150 loff_t n = *pos - 1;
1151
1152 if (*pos == 0)
1153 return SEQ_START_TOKEN;
1154
1155 return sel_avc_get_stat_idx(&n);
1156}
1157
1158static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1159{
1160 return sel_avc_get_stat_idx(pos);
1161}
1162
1163static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1164{
1165 struct avc_cache_stats *st = v;
1166
1167 if (v == SEQ_START_TOKEN)
1168 seq_printf(seq, "lookups hits misses allocations reclaims "
1169 "frees\n");
1170 else
1171 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1172 st->hits, st->misses, st->allocations,
1173 st->reclaims, st->frees);
1174 return 0;
1175}
1176
1177static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1178{ }
1179
Jan Engelhardt1996a102008-01-23 00:02:58 +01001180static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .start = sel_avc_stats_seq_start,
1182 .next = sel_avc_stats_seq_next,
1183 .show = sel_avc_stats_seq_show,
1184 .stop = sel_avc_stats_seq_stop,
1185};
1186
1187static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1188{
1189 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1190}
1191
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001192static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 .open = sel_open_avc_cache_stats,
1194 .read = seq_read,
1195 .llseek = seq_lseek,
1196 .release = seq_release,
1197};
1198#endif
1199
1200static int sel_make_avc_files(struct dentry *dir)
1201{
1202 int i, ret = 0;
1203 static struct tree_descr files[] = {
1204 { "cache_threshold",
1205 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1206 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1207#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1208 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1209#endif
1210 };
1211
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001212 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct inode *inode;
1214 struct dentry *dentry;
1215
1216 dentry = d_alloc_name(dir, files[i].name);
1217 if (!dentry) {
1218 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
1222 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1223 if (!inode) {
1224 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001228 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 d_add(dentry, inode);
1230 }
1231out:
1232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Eric Paris18729812008-04-17 14:15:45 -04001235static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001236 size_t count, loff_t *ppos)
1237{
1238 struct inode *inode;
1239 char *con;
1240 u32 sid, len;
1241 ssize_t ret;
1242
1243 inode = file->f_path.dentry->d_inode;
1244 sid = inode->i_ino&SEL_INO_MASK;
1245 ret = security_sid_to_context(sid, &con, &len);
1246 if (ret < 0)
1247 return ret;
1248
1249 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1250 kfree(con);
1251 return ret;
1252}
1253
1254static const struct file_operations sel_initcon_ops = {
1255 .read = sel_read_initcon,
1256};
1257
1258static int sel_make_initcon_files(struct dentry *dir)
1259{
1260 int i, ret = 0;
1261
1262 for (i = 1; i <= SECINITSID_NUM; i++) {
1263 struct inode *inode;
1264 struct dentry *dentry;
1265 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1266 if (!dentry) {
1267 ret = -ENOMEM;
1268 goto out;
1269 }
1270
1271 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1272 if (!inode) {
1273 ret = -ENOMEM;
1274 goto out;
1275 }
1276 inode->i_fop = &sel_initcon_ops;
1277 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1278 d_add(dentry, inode);
1279 }
1280out:
1281 return ret;
1282}
1283
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001284static inline unsigned int sel_div(unsigned long a, unsigned long b)
1285{
1286 return a / b - (a % b < 0);
1287}
1288
1289static inline unsigned long sel_class_to_ino(u16 class)
1290{
1291 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1292}
1293
1294static inline u16 sel_ino_to_class(unsigned long ino)
1295{
1296 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1297}
1298
1299static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1300{
1301 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1302}
1303
1304static inline u32 sel_ino_to_perm(unsigned long ino)
1305{
1306 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1307}
1308
Eric Paris18729812008-04-17 14:15:45 -04001309static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001310 size_t count, loff_t *ppos)
1311{
1312 ssize_t rc, len;
1313 char *page;
1314 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1315
1316 page = (char *)__get_free_page(GFP_KERNEL);
1317 if (!page) {
1318 rc = -ENOMEM;
1319 goto out;
1320 }
1321
1322 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1323 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1324 free_page((unsigned long)page);
1325out:
1326 return rc;
1327}
1328
1329static const struct file_operations sel_class_ops = {
1330 .read = sel_read_class,
1331};
1332
Eric Paris18729812008-04-17 14:15:45 -04001333static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001334 size_t count, loff_t *ppos)
1335{
1336 ssize_t rc, len;
1337 char *page;
1338 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1339
1340 page = (char *)__get_free_page(GFP_KERNEL);
1341 if (!page) {
1342 rc = -ENOMEM;
1343 goto out;
1344 }
1345
Eric Paris18729812008-04-17 14:15:45 -04001346 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001347 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1348 free_page((unsigned long)page);
1349out:
1350 return rc;
1351}
1352
1353static const struct file_operations sel_perm_ops = {
1354 .read = sel_read_perm,
1355};
1356
Paul Moore3bb56b22008-01-29 08:38:19 -05001357static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1358 size_t count, loff_t *ppos)
1359{
1360 int value;
1361 char tmpbuf[TMPBUFLEN];
1362 ssize_t length;
1363 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1364
1365 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1366 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1367
1368 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1369}
1370
1371static const struct file_operations sel_policycap_ops = {
1372 .read = sel_read_policycap,
1373};
1374
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001375static int sel_make_perm_files(char *objclass, int classvalue,
1376 struct dentry *dir)
1377{
1378 int i, rc = 0, nperms;
1379 char **perms;
1380
1381 rc = security_get_permissions(objclass, &perms, &nperms);
1382 if (rc)
1383 goto out;
1384
1385 for (i = 0; i < nperms; i++) {
1386 struct inode *inode;
1387 struct dentry *dentry;
1388
1389 dentry = d_alloc_name(dir, perms[i]);
1390 if (!dentry) {
1391 rc = -ENOMEM;
1392 goto out1;
1393 }
1394
1395 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1396 if (!inode) {
1397 rc = -ENOMEM;
1398 goto out1;
1399 }
1400 inode->i_fop = &sel_perm_ops;
1401 /* i+1 since perm values are 1-indexed */
1402 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1403 d_add(dentry, inode);
1404 }
1405
1406out1:
1407 for (i = 0; i < nperms; i++)
1408 kfree(perms[i]);
1409 kfree(perms);
1410out:
1411 return rc;
1412}
1413
1414static int sel_make_class_dir_entries(char *classname, int index,
1415 struct dentry *dir)
1416{
1417 struct dentry *dentry = NULL;
1418 struct inode *inode = NULL;
1419 int rc;
1420
1421 dentry = d_alloc_name(dir, "index");
1422 if (!dentry) {
1423 rc = -ENOMEM;
1424 goto out;
1425 }
1426
1427 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1428 if (!inode) {
1429 rc = -ENOMEM;
1430 goto out;
1431 }
1432
1433 inode->i_fop = &sel_class_ops;
1434 inode->i_ino = sel_class_to_ino(index);
1435 d_add(dentry, inode);
1436
1437 dentry = d_alloc_name(dir, "perms");
1438 if (!dentry) {
1439 rc = -ENOMEM;
1440 goto out;
1441 }
1442
1443 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1444 if (rc)
1445 goto out;
1446
1447 rc = sel_make_perm_files(classname, index, dentry);
1448
1449out:
1450 return rc;
1451}
1452
1453static void sel_remove_classes(void)
1454{
1455 struct list_head *class_node;
1456
1457 list_for_each(class_node, &class_dir->d_subdirs) {
1458 struct dentry *class_subdir = list_entry(class_node,
1459 struct dentry, d_u.d_child);
1460 struct list_head *class_subdir_node;
1461
1462 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1463 struct dentry *d = list_entry(class_subdir_node,
1464 struct dentry, d_u.d_child);
1465
1466 if (d->d_inode)
1467 if (d->d_inode->i_mode & S_IFDIR)
1468 sel_remove_entries(d);
1469 }
1470
1471 sel_remove_entries(class_subdir);
1472 }
1473
1474 sel_remove_entries(class_dir);
1475}
1476
1477static int sel_make_classes(void)
1478{
1479 int rc = 0, nclasses, i;
1480 char **classes;
1481
1482 /* delete any existing entries */
1483 sel_remove_classes();
1484
1485 rc = security_get_classes(&classes, &nclasses);
1486 if (rc < 0)
1487 goto out;
1488
1489 /* +2 since classes are 1-indexed */
1490 last_class_ino = sel_class_to_ino(nclasses+2);
1491
1492 for (i = 0; i < nclasses; i++) {
1493 struct dentry *class_name_dir;
1494
1495 class_name_dir = d_alloc_name(class_dir, classes[i]);
1496 if (!class_name_dir) {
1497 rc = -ENOMEM;
1498 goto out1;
1499 }
1500
1501 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1502 &last_class_ino);
1503 if (rc)
1504 goto out1;
1505
1506 /* i+1 since class values are 1-indexed */
1507 rc = sel_make_class_dir_entries(classes[i], i+1,
1508 class_name_dir);
1509 if (rc)
1510 goto out1;
1511 }
1512
1513out1:
1514 for (i = 0; i < nclasses; i++)
1515 kfree(classes[i]);
1516 kfree(classes);
1517out:
1518 return rc;
1519}
1520
Paul Moore3bb56b22008-01-29 08:38:19 -05001521static int sel_make_policycap(void)
1522{
1523 unsigned int iter;
1524 struct dentry *dentry = NULL;
1525 struct inode *inode = NULL;
1526
1527 sel_remove_entries(policycap_dir);
1528
1529 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1530 if (iter < ARRAY_SIZE(policycap_names))
1531 dentry = d_alloc_name(policycap_dir,
1532 policycap_names[iter]);
1533 else
1534 dentry = d_alloc_name(policycap_dir, "unknown");
1535
1536 if (dentry == NULL)
1537 return -ENOMEM;
1538
1539 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1540 if (inode == NULL)
1541 return -ENOMEM;
1542
1543 inode->i_fop = &sel_policycap_ops;
1544 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1545 d_add(dentry, inode);
1546 }
1547
1548 return 0;
1549}
1550
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001551static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1552 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int ret = 0;
1555 struct inode *inode;
1556
James Morrisedb20fb2006-03-22 00:09:20 -08001557 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (!inode) {
1559 ret = -ENOMEM;
1560 goto out;
1561 }
1562 inode->i_op = &simple_dir_inode_operations;
1563 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001564 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001565 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001566 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001568 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001569 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570out:
1571 return ret;
1572}
1573
Eric Paris18729812008-04-17 14:15:45 -04001574static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 int ret;
1577 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001578 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct inode_security_struct *isec;
1580
1581 static struct tree_descr selinux_files[] = {
1582 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1583 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001584 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1586 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1587 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1588 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1589 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1590 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1591 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1592 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1593 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1594 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001595 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1596 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* last one */ {""}
1598 };
1599 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1600 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001601 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
James Morrisedb20fb2006-03-22 00:09:20 -08001603 root_inode = sb->s_root->d_inode;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001606 if (!dentry) {
1607 ret = -ENOMEM;
1608 goto err;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001611 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001612 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001613 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001618 if (!dentry) {
1619 ret = -ENOMEM;
1620 goto err;
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001624 if (!inode) {
1625 ret = -ENOMEM;
1626 goto err;
1627 }
James Carter6174eaf2007-04-04 16:18:39 -04001628 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001629 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 isec->sid = SECINITSID_DEVNULL;
1631 isec->sclass = SECCLASS_CHR_FILE;
1632 isec->initialized = 1;
1633
1634 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1635 d_add(dentry, inode);
1636 selinux_null = dentry;
1637
1638 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001639 if (!dentry) {
1640 ret = -ENOMEM;
1641 goto err;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001644 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001646 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 ret = sel_make_avc_files(dentry);
1649 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001650 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001651
1652 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1653 if (!dentry) {
1654 ret = -ENOMEM;
1655 goto err;
1656 }
1657
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001658 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001659 if (ret)
1660 goto err;
1661
1662 ret = sel_make_initcon_files(dentry);
1663 if (ret)
1664 goto err;
1665
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001666 dentry = d_alloc_name(sb->s_root, "class");
1667 if (!dentry) {
1668 ret = -ENOMEM;
1669 goto err;
1670 }
1671
1672 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1673 if (ret)
1674 goto err;
1675
1676 class_dir = dentry;
1677
Paul Moore3bb56b22008-01-29 08:38:19 -05001678 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1679 if (!dentry) {
1680 ret = -ENOMEM;
1681 goto err;
1682 }
1683
1684 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1685 if (ret)
1686 goto err;
1687
1688 policycap_dir = dentry;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690out:
James Morris161ce452006-03-22 00:09:17 -08001691 return ret;
1692err:
Eric Paris744ba352008-04-17 11:52:44 -04001693 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1694 __func__);
James Morris161ce452006-03-22 00:09:17 -08001695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
David Howells454e2392006-06-23 02:02:57 -07001698static int sel_get_sb(struct file_system_type *fs_type,
1699 int flags, const char *dev_name, void *data,
1700 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
David Howells454e2392006-06-23 02:02:57 -07001702 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705static struct file_system_type sel_fs_type = {
1706 .name = "selinuxfs",
1707 .get_sb = sel_get_sb,
1708 .kill_sb = kill_litter_super,
1709};
1710
1711struct vfsmount *selinuxfs_mount;
1712
1713static int __init init_sel_fs(void)
1714{
1715 int err;
1716
1717 if (!selinux_enabled)
1718 return 0;
1719 err = register_filesystem(&sel_fs_type);
1720 if (!err) {
1721 selinuxfs_mount = kern_mount(&sel_fs_type);
1722 if (IS_ERR(selinuxfs_mount)) {
1723 printk(KERN_ERR "selinuxfs: could not mount!\n");
1724 err = PTR_ERR(selinuxfs_mount);
1725 selinuxfs_mount = NULL;
1726 }
1727 }
1728 return err;
1729}
1730
1731__initcall(init_sel_fs);
1732
1733#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1734void exit_sel_fs(void)
1735{
1736 unregister_filesystem(&sel_fs_type);
1737}
1738#endif