blob: 8d4007fbe0e96c4476bdacfce3416b3bd4f574ee [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
285 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
286 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
287}
288
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800289static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .read = sel_read_mls,
291};
292
Eric Paris18729812008-04-17 14:15:45 -0400293static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 size_t count, loff_t *ppos)
295
296{
297 int ret;
298 ssize_t length;
299 void *data = NULL;
300
Ingo Molnarbb003072006-03-22 00:09:14 -0800301 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 length = task_has_security(current, SECURITY__LOAD_POLICY);
304 if (length)
305 goto out;
306
307 if (*ppos != 0) {
308 /* No partial writes. */
309 length = -EINVAL;
310 goto out;
311 }
312
Davi Arnautbfd51622005-10-30 14:59:24 -0800313 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 || (data = vmalloc(count)) == NULL) {
315 length = -ENOMEM;
316 goto out;
317 }
318
319 length = -EFAULT;
320 if (copy_from_user(data, buf, count) != 0)
321 goto out;
322
323 length = security_load_policy(data, count);
324 if (length)
325 goto out;
326
327 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400328 if (ret) {
329 length = ret;
330 goto out1;
331 }
332
333 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500334 if (ret) {
335 length = ret;
336 goto out1;
337 }
338
339 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ret)
341 length = ret;
342 else
343 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400344
345out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000346 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500347 "policy loaded auid=%u ses=%u",
348 audit_get_loginuid(current),
349 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800351 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 vfree(data);
353 return length;
354}
355
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800356static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .write = sel_write_load,
358};
359
Eric Paris18729812008-04-17 14:15:45 -0400360static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800362 char *canon;
363 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ssize_t length;
365
366 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
367 if (length)
368 return length;
369
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800370 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800372 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800374 length = security_sid_to_context(sid, &canon, &len);
375 if (length < 0)
376 return length;
377
378 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400379 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
380 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800381 length = -ERANGE;
382 goto out;
383 }
384
385 memcpy(buf, canon, len);
386 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800388 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return length;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
393 size_t count, loff_t *ppos)
394{
395 char tmpbuf[TMPBUFLEN];
396 ssize_t length;
397
398 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
399 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
400}
401
Eric Paris18729812008-04-17 14:15:45 -0400402static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 size_t count, loff_t *ppos)
404{
405 char *page;
406 ssize_t length;
407 unsigned int new_value;
408
409 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
410 if (length)
411 return length;
412
Davi Arnautbfd51622005-10-30 14:59:24 -0800413 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -ENOMEM;
415 if (*ppos != 0) {
416 /* No partial writes. */
417 return -EINVAL;
418 }
Eric Paris18729812008-04-17 14:15:45 -0400419 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!page)
421 return -ENOMEM;
422 length = -EFAULT;
423 if (copy_from_user(page, buf, count))
424 goto out;
425
426 length = -EINVAL;
427 if (sscanf(page, "%u", &new_value) != 1)
428 goto out;
429
430 selinux_checkreqprot = new_value ? 1 : 0;
431 length = count;
432out:
433 free_page((unsigned long) page);
434 return length;
435}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800436static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .read = sel_read_checkreqprot,
438 .write = sel_write_checkreqprot,
439};
440
441/*
442 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
443 */
Eric Paris18729812008-04-17 14:15:45 -0400444static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
445static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
446static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
447static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
448static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450static ssize_t (*write_op[])(struct file *, char *, size_t) = {
451 [SEL_ACCESS] = sel_write_access,
452 [SEL_CREATE] = sel_write_create,
453 [SEL_RELABEL] = sel_write_relabel,
454 [SEL_USER] = sel_write_user,
455 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800456 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457};
458
459static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
460{
Eric Paris18729812008-04-17 14:15:45 -0400461 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char *data;
463 ssize_t rv;
464
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800465 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -EINVAL;
467
468 data = simple_transaction_get(file, buf, size);
469 if (IS_ERR(data))
470 return PTR_ERR(data);
471
Eric Paris18729812008-04-17 14:15:45 -0400472 rv = write_op[ino](file, data, size);
473 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 simple_transaction_set(file, rv);
475 rv = size;
476 }
477 return rv;
478}
479
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800480static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .write = selinux_transaction_write,
482 .read = simple_transaction_read,
483 .release = simple_transaction_release,
484};
485
486/*
487 * payload - write methods
488 * If the method has a response, the response should be put in buf,
489 * and the length returned. Otherwise return 0 or and -error.
490 */
491
Eric Paris18729812008-04-17 14:15:45 -0400492static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 char *scon, *tcon;
495 u32 ssid, tsid;
496 u16 tclass;
497 u32 req;
498 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;
515 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
516 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
525 length = security_compute_av(ssid, tsid, tclass, req, &avd);
526 if (length < 0)
527 goto out2;
528
529 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900530 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500531 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900533 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534out2:
535 kfree(tcon);
536out:
537 kfree(scon);
538 return length;
539}
540
Eric Paris18729812008-04-17 14:15:45 -0400541static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 char *scon, *tcon;
544 u32 ssid, tsid, newsid;
545 u16 tclass;
546 ssize_t length;
547 char *newcon;
548 u32 len;
549
550 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
551 if (length)
552 return length;
553
554 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800555 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!scon)
557 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
James Morris89d155e2005-10-30 14:59:21 -0800559 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!tcon)
561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 length = -EINVAL;
564 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
565 goto out2;
566
567 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
568 if (length < 0)
569 goto out2;
570 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
571 if (length < 0)
572 goto out2;
573
574 length = security_transition_sid(ssid, tsid, tclass, &newsid);
575 if (length < 0)
576 goto out2;
577
578 length = security_sid_to_context(newsid, &newcon, &len);
579 if (length < 0)
580 goto out2;
581
582 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400583 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
584 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 length = -ERANGE;
586 goto out3;
587 }
588
589 memcpy(buf, newcon, len);
590 length = len;
591out3:
592 kfree(newcon);
593out2:
594 kfree(tcon);
595out:
596 kfree(scon);
597 return length;
598}
599
Eric Paris18729812008-04-17 14:15:45 -0400600static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 char *scon, *tcon;
603 u32 ssid, tsid, newsid;
604 u16 tclass;
605 ssize_t length;
606 char *newcon;
607 u32 len;
608
609 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
610 if (length)
611 return length;
612
613 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800614 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!scon)
616 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
James Morris89d155e2005-10-30 14:59:21 -0800618 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!tcon)
620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 length = -EINVAL;
623 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
624 goto out2;
625
626 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
627 if (length < 0)
628 goto out2;
629 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
630 if (length < 0)
631 goto out2;
632
633 length = security_change_sid(ssid, tsid, tclass, &newsid);
634 if (length < 0)
635 goto out2;
636
637 length = security_sid_to_context(newsid, &newcon, &len);
638 if (length < 0)
639 goto out2;
640
641 if (len > SIMPLE_TRANSACTION_LIMIT) {
642 length = -ERANGE;
643 goto out3;
644 }
645
646 memcpy(buf, newcon, len);
647 length = len;
648out3:
649 kfree(newcon);
650out2:
651 kfree(tcon);
652out:
653 kfree(scon);
654 return length;
655}
656
Eric Paris18729812008-04-17 14:15:45 -0400657static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 char *con, *user, *ptr;
660 u32 sid, *sids;
661 ssize_t length;
662 char *newcon;
663 int i, rc;
664 u32 len, nsids;
665
666 length = task_has_security(current, SECURITY__COMPUTE_USER);
667 if (length)
668 return length;
669
670 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800671 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!con)
673 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
James Morris89d155e2005-10-30 14:59:21 -0800675 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!user)
677 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 length = -EINVAL;
680 if (sscanf(buf, "%s %s", con, user) != 2)
681 goto out2;
682
683 length = security_context_to_sid(con, strlen(con)+1, &sid);
684 if (length < 0)
685 goto out2;
686
687 length = security_get_user_sids(sid, user, &sids, &nsids);
688 if (length < 0)
689 goto out2;
690
691 length = sprintf(buf, "%u", nsids) + 1;
692 ptr = buf + length;
693 for (i = 0; i < nsids; i++) {
694 rc = security_sid_to_context(sids[i], &newcon, &len);
695 if (rc) {
696 length = rc;
697 goto out3;
698 }
699 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
700 kfree(newcon);
701 length = -ERANGE;
702 goto out3;
703 }
704 memcpy(ptr, newcon, len);
705 kfree(newcon);
706 ptr += len;
707 length += len;
708 }
709out3:
710 kfree(sids);
711out2:
712 kfree(user);
713out:
714 kfree(con);
715 return length;
716}
717
Eric Paris18729812008-04-17 14:15:45 -0400718static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 char *scon, *tcon;
721 u32 ssid, tsid, newsid;
722 u16 tclass;
723 ssize_t length;
724 char *newcon;
725 u32 len;
726
727 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
728 if (length)
729 return length;
730
731 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800732 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!scon)
734 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
James Morris89d155e2005-10-30 14:59:21 -0800736 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!tcon)
738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 length = -EINVAL;
741 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
742 goto out2;
743
744 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
745 if (length < 0)
746 goto out2;
747 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
748 if (length < 0)
749 goto out2;
750
751 length = security_member_sid(ssid, tsid, tclass, &newsid);
752 if (length < 0)
753 goto out2;
754
755 length = security_sid_to_context(newsid, &newcon, &len);
756 if (length < 0)
757 goto out2;
758
759 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400760 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
761 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 length = -ERANGE;
763 goto out3;
764 }
765
766 memcpy(buf, newcon, len);
767 length = len;
768out3:
769 kfree(newcon);
770out2:
771 kfree(tcon);
772out:
773 kfree(scon);
774 return length;
775}
776
777static struct inode *sel_make_inode(struct super_block *sb, int mode)
778{
779 struct inode *ret = new_inode(sb);
780
781 if (ret) {
782 ret->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
784 }
785 return ret;
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788static ssize_t sel_read_bool(struct file *filep, char __user *buf,
789 size_t count, loff_t *ppos)
790{
791 char *page = NULL;
792 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ssize_t ret;
794 int cur_enforcing;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500795 struct inode *inode = filep->f_path.dentry->d_inode;
796 unsigned index = inode->i_ino & SEL_INO_MASK;
797 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Ingo Molnarbb003072006-03-22 00:09:14 -0800799 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500801 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
802 ret = -EINVAL;
803 goto out;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Davi Arnautbfd51622005-10-30 14:59:24 -0800806 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ret = -EINVAL;
808 goto out;
809 }
Eric Paris18729812008-04-17 14:15:45 -0400810 page = (char *)get_zeroed_page(GFP_KERNEL);
811 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ret = -ENOMEM;
813 goto out;
814 }
815
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500816 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (cur_enforcing < 0) {
818 ret = cur_enforcing;
819 goto out;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500822 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800823 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800825 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (page)
827 free_page((unsigned long)page);
828 return ret;
829}
830
831static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
832 size_t count, loff_t *ppos)
833{
834 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500835 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int new_value;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500837 struct inode *inode = filep->f_path.dentry->d_inode;
838 unsigned index = inode->i_ino & SEL_INO_MASK;
839 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Ingo Molnarbb003072006-03-22 00:09:14 -0800841 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 length = task_has_security(current, SECURITY__SETBOOL);
844 if (length)
845 goto out;
846
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500847 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
848 length = -EINVAL;
849 goto out;
850 }
851
Davi Arnautbfd51622005-10-30 14:59:24 -0800852 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 length = -ENOMEM;
854 goto out;
855 }
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (*ppos != 0) {
858 /* No partial writes. */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500859 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto out;
861 }
Eric Paris18729812008-04-17 14:15:45 -0400862 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!page) {
864 length = -ENOMEM;
865 goto out;
866 }
867
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500868 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (copy_from_user(page, buf, count))
870 goto out;
871
872 length = -EINVAL;
873 if (sscanf(page, "%d", &new_value) != 1)
874 goto out;
875
876 if (new_value)
877 new_value = 1;
878
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500879 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 length = count;
881
882out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800883 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (page)
885 free_page((unsigned long) page);
886 return length;
887}
888
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800889static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400890 .read = sel_read_bool,
891 .write = sel_write_bool,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892};
893
894static ssize_t sel_commit_bools_write(struct file *filep,
895 const char __user *buf,
896 size_t count, loff_t *ppos)
897{
898 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500899 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int new_value;
901
Ingo Molnarbb003072006-03-22 00:09:14 -0800902 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 length = task_has_security(current, SECURITY__SETBOOL);
905 if (length)
906 goto out;
907
Davi Arnautbfd51622005-10-30 14:59:24 -0800908 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 length = -ENOMEM;
910 goto out;
911 }
912 if (*ppos != 0) {
913 /* No partial writes. */
914 goto out;
915 }
Eric Paris18729812008-04-17 14:15:45 -0400916 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!page) {
918 length = -ENOMEM;
919 goto out;
920 }
921
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500922 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (copy_from_user(page, buf, count))
924 goto out;
925
926 length = -EINVAL;
927 if (sscanf(page, "%d", &new_value) != 1)
928 goto out;
929
Eric Paris18729812008-04-17 14:15:45 -0400930 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 length = count;
934
935out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800936 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (page)
938 free_page((unsigned long) page);
939 return length;
940}
941
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800942static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400943 .write = sel_commit_bools_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944};
945
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -0400946static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Stephen Smalley0955dc02007-11-21 09:01:36 -0500948 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 spin_lock(&dcache_lock);
951 node = de->d_subdirs.next;
952 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800953 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del_init(node);
955
956 if (d->d_inode) {
957 d = dget_locked(d);
958 spin_unlock(&dcache_lock);
959 d_delete(d);
960 simple_unlink(de->d_inode, d);
961 dput(d);
962 spin_lock(&dcache_lock);
963 }
964 node = de->d_subdirs.next;
965 }
966
967 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970#define BOOL_DIR_NAME "booleans"
971
972static int sel_make_bools(void)
973{
974 int i, ret = 0;
975 ssize_t len;
976 struct dentry *dentry = NULL;
977 struct dentry *dir = bool_dir;
978 struct inode *inode = NULL;
979 struct inode_security_struct *isec;
980 char **names = NULL, *page;
981 int num;
982 int *values = NULL;
983 u32 sid;
984
985 /* remove any existing files */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500986 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700987 kfree(bool_pending_values);
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500988 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -0700989 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -0400991 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Eric Paris18729812008-04-17 14:15:45 -0400993 page = (char *)get_zeroed_page(GFP_KERNEL);
994 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return -ENOMEM;
996
997 ret = security_get_bools(&num, &names, &values);
998 if (ret != 0)
999 goto out;
1000
1001 for (i = 0; i < num; i++) {
1002 dentry = d_alloc_name(dir, names[i]);
1003 if (!dentry) {
1004 ret = -ENOMEM;
1005 goto err;
1006 }
1007 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1008 if (!inode) {
1009 ret = -ENOMEM;
1010 goto err;
1011 }
1012
1013 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1014 if (len < 0) {
1015 ret = -EINVAL;
1016 goto err;
1017 } else if (len >= PAGE_SIZE) {
1018 ret = -ENAMETOOLONG;
1019 goto err;
1020 }
Eric Paris18729812008-04-17 14:15:45 -04001021 isec = (struct inode_security_struct *)inode->i_security;
1022 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1023 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto err;
1025 isec->sid = sid;
1026 isec->initialized = 1;
1027 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001028 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 d_add(dentry, inode);
1030 }
1031 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001032 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 bool_pending_values = values;
1034out:
1035 free_page((unsigned long)page);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001036 return ret;
1037err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001039 for (i = 0; i < num; i++)
1040 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(names);
1042 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001043 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001044 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ret = -ENOMEM;
1046 goto out;
1047}
1048
1049#define NULL_FILE_NAME "null"
1050
Eric Paris18729812008-04-17 14:15:45 -04001051struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1054 size_t count, loff_t *ppos)
1055{
1056 char tmpbuf[TMPBUFLEN];
1057 ssize_t length;
1058
1059 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1060 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1061}
1062
Eric Paris18729812008-04-17 14:15:45 -04001063static ssize_t sel_write_avc_cache_threshold(struct file *file,
1064 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 size_t count, loff_t *ppos)
1066
1067{
1068 char *page;
1069 ssize_t ret;
1070 int new_value;
1071
Davi Arnautbfd51622005-10-30 14:59:24 -08001072 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ret = -ENOMEM;
1074 goto out;
1075 }
1076
1077 if (*ppos != 0) {
1078 /* No partial writes. */
1079 ret = -EINVAL;
1080 goto out;
1081 }
1082
Eric Paris18729812008-04-17 14:15:45 -04001083 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!page) {
1085 ret = -ENOMEM;
1086 goto out;
1087 }
1088
1089 if (copy_from_user(page, buf, count)) {
1090 ret = -EFAULT;
1091 goto out_free;
1092 }
1093
1094 if (sscanf(page, "%u", &new_value) != 1) {
1095 ret = -EINVAL;
1096 goto out;
1097 }
1098
1099 if (new_value != avc_cache_threshold) {
1100 ret = task_has_security(current, SECURITY__SETSECPARAM);
1101 if (ret)
1102 goto out_free;
1103 avc_cache_threshold = new_value;
1104 }
1105 ret = count;
1106out_free:
1107 free_page((unsigned long)page);
1108out:
1109 return ret;
1110}
1111
1112static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1113 size_t count, loff_t *ppos)
1114{
1115 char *page;
1116 ssize_t ret = 0;
1117
1118 page = (char *)__get_free_page(GFP_KERNEL);
1119 if (!page) {
1120 ret = -ENOMEM;
1121 goto out;
1122 }
1123 ret = avc_get_hash_stats(page);
1124 if (ret >= 0)
1125 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1126 free_page((unsigned long)page);
1127out:
1128 return ret;
1129}
1130
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001131static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 .read = sel_read_avc_cache_threshold,
1133 .write = sel_write_avc_cache_threshold,
1134};
1135
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001136static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 .read = sel_read_avc_hash_stats,
1138};
1139
1140#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1141static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1142{
1143 int cpu;
1144
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301145 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!cpu_possible(cpu))
1147 continue;
1148 *idx = cpu + 1;
1149 return &per_cpu(avc_cache_stats, cpu);
1150 }
1151 return NULL;
1152}
1153
1154static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1155{
1156 loff_t n = *pos - 1;
1157
1158 if (*pos == 0)
1159 return SEQ_START_TOKEN;
1160
1161 return sel_avc_get_stat_idx(&n);
1162}
1163
1164static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1165{
1166 return sel_avc_get_stat_idx(pos);
1167}
1168
1169static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1170{
1171 struct avc_cache_stats *st = v;
1172
1173 if (v == SEQ_START_TOKEN)
1174 seq_printf(seq, "lookups hits misses allocations reclaims "
1175 "frees\n");
1176 else
1177 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1178 st->hits, st->misses, st->allocations,
1179 st->reclaims, st->frees);
1180 return 0;
1181}
1182
1183static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1184{ }
1185
Jan Engelhardt1996a102008-01-23 00:02:58 +01001186static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 .start = sel_avc_stats_seq_start,
1188 .next = sel_avc_stats_seq_next,
1189 .show = sel_avc_stats_seq_show,
1190 .stop = sel_avc_stats_seq_stop,
1191};
1192
1193static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1194{
1195 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1196}
1197
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001198static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 .open = sel_open_avc_cache_stats,
1200 .read = seq_read,
1201 .llseek = seq_lseek,
1202 .release = seq_release,
1203};
1204#endif
1205
1206static int sel_make_avc_files(struct dentry *dir)
1207{
1208 int i, ret = 0;
1209 static struct tree_descr files[] = {
1210 { "cache_threshold",
1211 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1212 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1213#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1214 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1215#endif
1216 };
1217
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001218 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct inode *inode;
1220 struct dentry *dentry;
1221
1222 dentry = d_alloc_name(dir, files[i].name);
1223 if (!dentry) {
1224 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1229 if (!inode) {
1230 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001234 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 d_add(dentry, inode);
1236 }
1237out:
1238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Eric Paris18729812008-04-17 14:15:45 -04001241static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001242 size_t count, loff_t *ppos)
1243{
1244 struct inode *inode;
1245 char *con;
1246 u32 sid, len;
1247 ssize_t ret;
1248
1249 inode = file->f_path.dentry->d_inode;
1250 sid = inode->i_ino&SEL_INO_MASK;
1251 ret = security_sid_to_context(sid, &con, &len);
1252 if (ret < 0)
1253 return ret;
1254
1255 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1256 kfree(con);
1257 return ret;
1258}
1259
1260static const struct file_operations sel_initcon_ops = {
1261 .read = sel_read_initcon,
1262};
1263
1264static int sel_make_initcon_files(struct dentry *dir)
1265{
1266 int i, ret = 0;
1267
1268 for (i = 1; i <= SECINITSID_NUM; i++) {
1269 struct inode *inode;
1270 struct dentry *dentry;
1271 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1272 if (!dentry) {
1273 ret = -ENOMEM;
1274 goto out;
1275 }
1276
1277 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1278 if (!inode) {
1279 ret = -ENOMEM;
1280 goto out;
1281 }
1282 inode->i_fop = &sel_initcon_ops;
1283 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1284 d_add(dentry, inode);
1285 }
1286out:
1287 return ret;
1288}
1289
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001290static inline unsigned int sel_div(unsigned long a, unsigned long b)
1291{
1292 return a / b - (a % b < 0);
1293}
1294
1295static inline unsigned long sel_class_to_ino(u16 class)
1296{
1297 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1298}
1299
1300static inline u16 sel_ino_to_class(unsigned long ino)
1301{
1302 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1303}
1304
1305static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1306{
1307 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1308}
1309
1310static inline u32 sel_ino_to_perm(unsigned long ino)
1311{
1312 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1313}
1314
Eric Paris18729812008-04-17 14:15:45 -04001315static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001316 size_t count, loff_t *ppos)
1317{
1318 ssize_t rc, len;
1319 char *page;
1320 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1321
1322 page = (char *)__get_free_page(GFP_KERNEL);
1323 if (!page) {
1324 rc = -ENOMEM;
1325 goto out;
1326 }
1327
1328 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1329 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1330 free_page((unsigned long)page);
1331out:
1332 return rc;
1333}
1334
1335static const struct file_operations sel_class_ops = {
1336 .read = sel_read_class,
1337};
1338
Eric Paris18729812008-04-17 14:15:45 -04001339static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001340 size_t count, loff_t *ppos)
1341{
1342 ssize_t rc, len;
1343 char *page;
1344 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1345
1346 page = (char *)__get_free_page(GFP_KERNEL);
1347 if (!page) {
1348 rc = -ENOMEM;
1349 goto out;
1350 }
1351
Eric Paris18729812008-04-17 14:15:45 -04001352 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001353 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1354 free_page((unsigned long)page);
1355out:
1356 return rc;
1357}
1358
1359static const struct file_operations sel_perm_ops = {
1360 .read = sel_read_perm,
1361};
1362
Paul Moore3bb56b22008-01-29 08:38:19 -05001363static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1364 size_t count, loff_t *ppos)
1365{
1366 int value;
1367 char tmpbuf[TMPBUFLEN];
1368 ssize_t length;
1369 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1370
1371 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1372 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1373
1374 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1375}
1376
1377static const struct file_operations sel_policycap_ops = {
1378 .read = sel_read_policycap,
1379};
1380
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001381static int sel_make_perm_files(char *objclass, int classvalue,
1382 struct dentry *dir)
1383{
1384 int i, rc = 0, nperms;
1385 char **perms;
1386
1387 rc = security_get_permissions(objclass, &perms, &nperms);
1388 if (rc)
1389 goto out;
1390
1391 for (i = 0; i < nperms; i++) {
1392 struct inode *inode;
1393 struct dentry *dentry;
1394
1395 dentry = d_alloc_name(dir, perms[i]);
1396 if (!dentry) {
1397 rc = -ENOMEM;
1398 goto out1;
1399 }
1400
1401 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1402 if (!inode) {
1403 rc = -ENOMEM;
1404 goto out1;
1405 }
1406 inode->i_fop = &sel_perm_ops;
1407 /* i+1 since perm values are 1-indexed */
1408 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1409 d_add(dentry, inode);
1410 }
1411
1412out1:
1413 for (i = 0; i < nperms; i++)
1414 kfree(perms[i]);
1415 kfree(perms);
1416out:
1417 return rc;
1418}
1419
1420static int sel_make_class_dir_entries(char *classname, int index,
1421 struct dentry *dir)
1422{
1423 struct dentry *dentry = NULL;
1424 struct inode *inode = NULL;
1425 int rc;
1426
1427 dentry = d_alloc_name(dir, "index");
1428 if (!dentry) {
1429 rc = -ENOMEM;
1430 goto out;
1431 }
1432
1433 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1434 if (!inode) {
1435 rc = -ENOMEM;
1436 goto out;
1437 }
1438
1439 inode->i_fop = &sel_class_ops;
1440 inode->i_ino = sel_class_to_ino(index);
1441 d_add(dentry, inode);
1442
1443 dentry = d_alloc_name(dir, "perms");
1444 if (!dentry) {
1445 rc = -ENOMEM;
1446 goto out;
1447 }
1448
1449 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1450 if (rc)
1451 goto out;
1452
1453 rc = sel_make_perm_files(classname, index, dentry);
1454
1455out:
1456 return rc;
1457}
1458
1459static void sel_remove_classes(void)
1460{
1461 struct list_head *class_node;
1462
1463 list_for_each(class_node, &class_dir->d_subdirs) {
1464 struct dentry *class_subdir = list_entry(class_node,
1465 struct dentry, d_u.d_child);
1466 struct list_head *class_subdir_node;
1467
1468 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1469 struct dentry *d = list_entry(class_subdir_node,
1470 struct dentry, d_u.d_child);
1471
1472 if (d->d_inode)
1473 if (d->d_inode->i_mode & S_IFDIR)
1474 sel_remove_entries(d);
1475 }
1476
1477 sel_remove_entries(class_subdir);
1478 }
1479
1480 sel_remove_entries(class_dir);
1481}
1482
1483static int sel_make_classes(void)
1484{
1485 int rc = 0, nclasses, i;
1486 char **classes;
1487
1488 /* delete any existing entries */
1489 sel_remove_classes();
1490
1491 rc = security_get_classes(&classes, &nclasses);
1492 if (rc < 0)
1493 goto out;
1494
1495 /* +2 since classes are 1-indexed */
1496 last_class_ino = sel_class_to_ino(nclasses+2);
1497
1498 for (i = 0; i < nclasses; i++) {
1499 struct dentry *class_name_dir;
1500
1501 class_name_dir = d_alloc_name(class_dir, classes[i]);
1502 if (!class_name_dir) {
1503 rc = -ENOMEM;
1504 goto out1;
1505 }
1506
1507 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1508 &last_class_ino);
1509 if (rc)
1510 goto out1;
1511
1512 /* i+1 since class values are 1-indexed */
1513 rc = sel_make_class_dir_entries(classes[i], i+1,
1514 class_name_dir);
1515 if (rc)
1516 goto out1;
1517 }
1518
1519out1:
1520 for (i = 0; i < nclasses; i++)
1521 kfree(classes[i]);
1522 kfree(classes);
1523out:
1524 return rc;
1525}
1526
Paul Moore3bb56b22008-01-29 08:38:19 -05001527static int sel_make_policycap(void)
1528{
1529 unsigned int iter;
1530 struct dentry *dentry = NULL;
1531 struct inode *inode = NULL;
1532
1533 sel_remove_entries(policycap_dir);
1534
1535 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1536 if (iter < ARRAY_SIZE(policycap_names))
1537 dentry = d_alloc_name(policycap_dir,
1538 policycap_names[iter]);
1539 else
1540 dentry = d_alloc_name(policycap_dir, "unknown");
1541
1542 if (dentry == NULL)
1543 return -ENOMEM;
1544
1545 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1546 if (inode == NULL)
1547 return -ENOMEM;
1548
1549 inode->i_fop = &sel_policycap_ops;
1550 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1551 d_add(dentry, inode);
1552 }
1553
1554 return 0;
1555}
1556
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001557static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1558 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 int ret = 0;
1561 struct inode *inode;
1562
James Morrisedb20fb2006-03-22 00:09:20 -08001563 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (!inode) {
1565 ret = -ENOMEM;
1566 goto out;
1567 }
1568 inode->i_op = &simple_dir_inode_operations;
1569 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001570 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001571 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001572 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001574 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001575 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576out:
1577 return ret;
1578}
1579
Eric Paris18729812008-04-17 14:15:45 -04001580static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int ret;
1583 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001584 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct inode_security_struct *isec;
1586
1587 static struct tree_descr selinux_files[] = {
1588 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1589 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001590 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1592 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1593 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1594 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1595 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1596 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1597 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1598 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1599 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1600 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001601 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1602 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /* last one */ {""}
1604 };
1605 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1606 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001607 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
James Morrisedb20fb2006-03-22 00:09:20 -08001609 root_inode = sb->s_root->d_inode;
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001612 if (!dentry) {
1613 ret = -ENOMEM;
1614 goto err;
1615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001617 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001618 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001619 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001624 if (!dentry) {
1625 ret = -ENOMEM;
1626 goto err;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001630 if (!inode) {
1631 ret = -ENOMEM;
1632 goto err;
1633 }
James Carter6174eaf2007-04-04 16:18:39 -04001634 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001635 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 isec->sid = SECINITSID_DEVNULL;
1637 isec->sclass = SECCLASS_CHR_FILE;
1638 isec->initialized = 1;
1639
1640 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1641 d_add(dentry, inode);
1642 selinux_null = dentry;
1643
1644 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001645 if (!dentry) {
1646 ret = -ENOMEM;
1647 goto err;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001650 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001652 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 ret = sel_make_avc_files(dentry);
1655 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001656 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001657
1658 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1659 if (!dentry) {
1660 ret = -ENOMEM;
1661 goto err;
1662 }
1663
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001664 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001665 if (ret)
1666 goto err;
1667
1668 ret = sel_make_initcon_files(dentry);
1669 if (ret)
1670 goto err;
1671
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001672 dentry = d_alloc_name(sb->s_root, "class");
1673 if (!dentry) {
1674 ret = -ENOMEM;
1675 goto err;
1676 }
1677
1678 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1679 if (ret)
1680 goto err;
1681
1682 class_dir = dentry;
1683
Paul Moore3bb56b22008-01-29 08:38:19 -05001684 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1685 if (!dentry) {
1686 ret = -ENOMEM;
1687 goto err;
1688 }
1689
1690 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1691 if (ret)
1692 goto err;
1693
1694 policycap_dir = dentry;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696out:
James Morris161ce452006-03-22 00:09:17 -08001697 return ret;
1698err:
Eric Paris744ba352008-04-17 11:52:44 -04001699 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1700 __func__);
James Morris161ce452006-03-22 00:09:17 -08001701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
David Howells454e2392006-06-23 02:02:57 -07001704static int sel_get_sb(struct file_system_type *fs_type,
1705 int flags, const char *dev_name, void *data,
1706 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
David Howells454e2392006-06-23 02:02:57 -07001708 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static struct file_system_type sel_fs_type = {
1712 .name = "selinuxfs",
1713 .get_sb = sel_get_sb,
1714 .kill_sb = kill_litter_super,
1715};
1716
1717struct vfsmount *selinuxfs_mount;
1718
1719static int __init init_sel_fs(void)
1720{
1721 int err;
1722
1723 if (!selinux_enabled)
1724 return 0;
1725 err = register_filesystem(&sel_fs_type);
1726 if (!err) {
1727 selinuxfs_mount = kern_mount(&sel_fs_type);
1728 if (IS_ERR(selinuxfs_mount)) {
1729 printk(KERN_ERR "selinuxfs: could not mount!\n");
1730 err = PTR_ERR(selinuxfs_mount);
1731 selinuxfs_mount = NULL;
1732 }
1733 }
1734 return err;
1735}
1736
1737__initcall(init_sel_fs);
1738
1739#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1740void exit_sel_fs(void)
1741{
1742 unregister_filesystem(&sel_fs_type);
1743}
1744#endif