blob: a2e7a8563b383406ed54b09b519b69ec71c3bc2d [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 */
KaiGai Kohei11904162010-09-14 18:28:39 +0900113 SEL_STATUS, /* export current status using mmap() */
James Carter6174eaf2007-04-04 16:18:39 -0400114 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
James Carter6174eaf2007-04-04 16:18:39 -0400117static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
118
Paul Moore3bb56b22008-01-29 08:38:19 -0500119#define SEL_INITCON_INO_OFFSET 0x01000000
120#define SEL_BOOL_INO_OFFSET 0x02000000
121#define SEL_CLASS_INO_OFFSET 0x04000000
122#define SEL_POLICYCAP_INO_OFFSET 0x08000000
123#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TMPBUFLEN 12
126static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
127 size_t count, loff_t *ppos)
128{
129 char tmpbuf[TMPBUFLEN];
130 ssize_t length;
131
132 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
133 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
134}
135
136#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400137static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 size_t count, loff_t *ppos)
139
140{
141 char *page;
142 ssize_t length;
143 int new_value;
144
Davi Arnautbfd51622005-10-30 14:59:24 -0800145 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -ENOMEM;
147 if (*ppos != 0) {
148 /* No partial writes. */
149 return -EINVAL;
150 }
Eric Paris18729812008-04-17 14:15:45 -0400151 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!page)
153 return -ENOMEM;
154 length = -EFAULT;
155 if (copy_from_user(page, buf, count))
156 goto out;
157
158 length = -EINVAL;
159 if (sscanf(page, "%d", &new_value) != 1)
160 goto out;
161
162 if (new_value != selinux_enforcing) {
163 length = task_has_security(current, SECURITY__SETENFORCE);
164 if (length)
165 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000166 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500167 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
168 new_value, selinux_enforcing,
169 audit_get_loginuid(current),
170 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 selinux_enforcing = new_value;
172 if (selinux_enforcing)
173 avc_ss_reset(0);
174 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900175 selinux_status_update_setenforce(selinux_enforcing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 length = count;
178out:
179 free_page((unsigned long) page);
180 return length;
181}
182#else
183#define sel_write_enforce NULL
184#endif
185
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800186static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .read = sel_read_enforce,
188 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200189 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
Eric Paris3f120702007-09-21 14:37:10 -0400192static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
193 size_t count, loff_t *ppos)
194{
195 char tmpbuf[TMPBUFLEN];
196 ssize_t length;
197 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
198 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
199 security_get_reject_unknown() : !security_get_allow_unknown();
200
201 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
202 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
203}
204
205static const struct file_operations sel_handle_unknown_ops = {
206 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200207 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400208};
209
KaiGai Kohei11904162010-09-14 18:28:39 +0900210static int sel_open_handle_status(struct inode *inode, struct file *filp)
211{
212 struct page *status = selinux_kernel_status_page();
213
214 if (!status)
215 return -ENOMEM;
216
217 filp->private_data = status;
218
219 return 0;
220}
221
222static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
223 size_t count, loff_t *ppos)
224{
225 struct page *status = filp->private_data;
226
227 BUG_ON(!status);
228
229 return simple_read_from_buffer(buf, count, ppos,
230 page_address(status),
231 sizeof(struct selinux_kernel_status));
232}
233
234static int sel_mmap_handle_status(struct file *filp,
235 struct vm_area_struct *vma)
236{
237 struct page *status = filp->private_data;
238 unsigned long size = vma->vm_end - vma->vm_start;
239
240 BUG_ON(!status);
241
242 /* only allows one page from the head */
243 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
244 return -EIO;
245 /* disallow writable mapping */
246 if (vma->vm_flags & VM_WRITE)
247 return -EPERM;
248 /* disallow mprotect() turns it into writable */
249 vma->vm_flags &= ~VM_MAYWRITE;
250
251 return remap_pfn_range(vma, vma->vm_start,
252 page_to_pfn(status),
253 size, vma->vm_page_prot);
254}
255
256static const struct file_operations sel_handle_status_ops = {
257 .open = sel_open_handle_status,
258 .read = sel_read_handle_status,
259 .mmap = sel_mmap_handle_status,
260 .llseek = generic_file_llseek,
261};
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400264static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 size_t count, loff_t *ppos)
266
267{
268 char *page;
269 ssize_t length;
270 int new_value;
271 extern int selinux_disable(void);
272
Davi Arnautbfd51622005-10-30 14:59:24 -0800273 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -ENOMEM;
275 if (*ppos != 0) {
276 /* No partial writes. */
277 return -EINVAL;
278 }
Eric Paris18729812008-04-17 14:15:45 -0400279 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!page)
281 return -ENOMEM;
282 length = -EFAULT;
283 if (copy_from_user(page, buf, count))
284 goto out;
285
286 length = -EINVAL;
287 if (sscanf(page, "%d", &new_value) != 1)
288 goto out;
289
290 if (new_value) {
291 length = selinux_disable();
292 if (length < 0)
293 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000294 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500295 "selinux=0 auid=%u ses=%u",
296 audit_get_loginuid(current),
297 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 length = count;
301out:
302 free_page((unsigned long) page);
303 return length;
304}
305#else
306#define sel_write_disable NULL
307#endif
308
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800309static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200311 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
314static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400315 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 char tmpbuf[TMPBUFLEN];
318 ssize_t length;
319
320 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
321 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
322}
323
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800324static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200326 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329/* declaration for sel_write_load */
330static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400331static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500332static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400333
334/* declaration for sel_make_class_dirs */
335static int sel_make_dir(struct inode *dir, struct dentry *dentry,
336 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338static ssize_t sel_read_mls(struct file *filp, char __user *buf,
339 size_t count, loff_t *ppos)
340{
341 char tmpbuf[TMPBUFLEN];
342 ssize_t length;
343
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100344 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
345 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
347}
348
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800349static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200351 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Eric Paris18729812008-04-17 14:15:45 -0400354static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 size_t count, loff_t *ppos)
356
357{
358 int ret;
359 ssize_t length;
360 void *data = NULL;
361
Ingo Molnarbb003072006-03-22 00:09:14 -0800362 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 length = task_has_security(current, SECURITY__LOAD_POLICY);
365 if (length)
366 goto out;
367
368 if (*ppos != 0) {
369 /* No partial writes. */
370 length = -EINVAL;
371 goto out;
372 }
373
Davi Arnautbfd51622005-10-30 14:59:24 -0800374 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 || (data = vmalloc(count)) == NULL) {
376 length = -ENOMEM;
377 goto out;
378 }
379
380 length = -EFAULT;
381 if (copy_from_user(data, buf, count) != 0)
382 goto out;
383
384 length = security_load_policy(data, count);
385 if (length)
386 goto out;
387
388 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400389 if (ret) {
390 length = ret;
391 goto out1;
392 }
393
394 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500395 if (ret) {
396 length = ret;
397 goto out1;
398 }
399
400 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (ret)
402 length = ret;
403 else
404 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400405
406out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000407 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500408 "policy loaded auid=%u ses=%u",
409 audit_get_loginuid(current),
410 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800412 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 vfree(data);
414 return length;
415}
416
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800417static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200419 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Eric Paris18729812008-04-17 14:15:45 -0400422static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800424 char *canon;
425 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ssize_t length;
427
428 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
429 if (length)
430 return length;
431
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800432 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800434 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800436 length = security_sid_to_context(sid, &canon, &len);
437 if (length < 0)
438 return length;
439
440 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400441 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
442 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800443 length = -ERANGE;
444 goto out;
445 }
446
447 memcpy(buf, canon, len);
448 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800450 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return length;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
455 size_t count, loff_t *ppos)
456{
457 char tmpbuf[TMPBUFLEN];
458 ssize_t length;
459
460 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
461 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
462}
463
Eric Paris18729812008-04-17 14:15:45 -0400464static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 size_t count, loff_t *ppos)
466{
467 char *page;
468 ssize_t length;
469 unsigned int new_value;
470
471 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
472 if (length)
473 return length;
474
Davi Arnautbfd51622005-10-30 14:59:24 -0800475 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -ENOMEM;
477 if (*ppos != 0) {
478 /* No partial writes. */
479 return -EINVAL;
480 }
Eric Paris18729812008-04-17 14:15:45 -0400481 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!page)
483 return -ENOMEM;
484 length = -EFAULT;
485 if (copy_from_user(page, buf, count))
486 goto out;
487
488 length = -EINVAL;
489 if (sscanf(page, "%u", &new_value) != 1)
490 goto out;
491
492 selinux_checkreqprot = new_value ? 1 : 0;
493 length = count;
494out:
495 free_page((unsigned long) page);
496 return length;
497}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800498static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .read = sel_read_checkreqprot,
500 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200501 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502};
503
504/*
505 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
506 */
Eric Paris18729812008-04-17 14:15:45 -0400507static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
508static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
509static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
510static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
511static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513static ssize_t (*write_op[])(struct file *, char *, size_t) = {
514 [SEL_ACCESS] = sel_write_access,
515 [SEL_CREATE] = sel_write_create,
516 [SEL_RELABEL] = sel_write_relabel,
517 [SEL_USER] = sel_write_user,
518 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800519 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520};
521
522static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
523{
Eric Paris18729812008-04-17 14:15:45 -0400524 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 char *data;
526 ssize_t rv;
527
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800528 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EINVAL;
530
531 data = simple_transaction_get(file, buf, size);
532 if (IS_ERR(data))
533 return PTR_ERR(data);
534
Eric Paris18729812008-04-17 14:15:45 -0400535 rv = write_op[ino](file, data, size);
536 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 simple_transaction_set(file, rv);
538 rv = size;
539 }
540 return rv;
541}
542
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800543static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 .write = selinux_transaction_write,
545 .read = simple_transaction_read,
546 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200547 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
550/*
551 * payload - write methods
552 * If the method has a response, the response should be put in buf,
553 * and the length returned. Otherwise return 0 or and -error.
554 */
555
Eric Paris18729812008-04-17 14:15:45 -0400556static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 char *scon, *tcon;
559 u32 ssid, tsid;
560 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct av_decision avd;
562 ssize_t length;
563
564 length = task_has_security(current, SECURITY__COMPUTE_AV);
565 if (length)
566 return length;
567
568 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800569 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!scon)
571 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800573 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!tcon)
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500578 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out2;
580
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800581 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (length < 0)
583 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800584 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (length < 0)
586 goto out2;
587
Stephen Smalley19439d02010-01-14 17:28:10 -0500588 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900591 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500592 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900594 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595out2:
596 kfree(tcon);
597out:
598 kfree(scon);
599 return length;
600}
601
Eric Paris18729812008-04-17 14:15:45 -0400602static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 char *scon, *tcon;
605 u32 ssid, tsid, newsid;
606 u16 tclass;
607 ssize_t length;
608 char *newcon;
609 u32 len;
610
611 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
612 if (length)
613 return length;
614
615 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800616 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!scon)
618 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800620 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!tcon)
622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 length = -EINVAL;
625 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
626 goto out2;
627
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800628 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (length < 0)
630 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800631 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (length < 0)
633 goto out2;
634
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400635 length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (length < 0)
637 goto out2;
638
639 length = security_sid_to_context(newsid, &newcon, &len);
640 if (length < 0)
641 goto out2;
642
643 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400644 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
645 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 length = -ERANGE;
647 goto out3;
648 }
649
650 memcpy(buf, newcon, len);
651 length = len;
652out3:
653 kfree(newcon);
654out2:
655 kfree(tcon);
656out:
657 kfree(scon);
658 return length;
659}
660
Eric Paris18729812008-04-17 14:15:45 -0400661static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 char *scon, *tcon;
664 u32 ssid, tsid, newsid;
665 u16 tclass;
666 ssize_t length;
667 char *newcon;
668 u32 len;
669
670 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
671 if (length)
672 return length;
673
674 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800675 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!scon)
677 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800679 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!tcon)
681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 length = -EINVAL;
684 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
685 goto out2;
686
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800687 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (length < 0)
689 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800690 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (length < 0)
692 goto out2;
693
694 length = security_change_sid(ssid, tsid, tclass, &newsid);
695 if (length < 0)
696 goto out2;
697
698 length = security_sid_to_context(newsid, &newcon, &len);
699 if (length < 0)
700 goto out2;
701
702 if (len > SIMPLE_TRANSACTION_LIMIT) {
703 length = -ERANGE;
704 goto out3;
705 }
706
707 memcpy(buf, newcon, len);
708 length = len;
709out3:
710 kfree(newcon);
711out2:
712 kfree(tcon);
713out:
714 kfree(scon);
715 return length;
716}
717
Eric Paris18729812008-04-17 14:15:45 -0400718static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 char *con, *user, *ptr;
721 u32 sid, *sids;
722 ssize_t length;
723 char *newcon;
724 int i, rc;
725 u32 len, nsids;
726
727 length = task_has_security(current, SECURITY__COMPUTE_USER);
728 if (length)
729 return length;
730
731 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800732 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!con)
734 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800736 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!user)
738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 length = -EINVAL;
741 if (sscanf(buf, "%s %s", con, user) != 2)
742 goto out2;
743
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800744 length = security_context_to_sid(con, strlen(con) + 1, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (length < 0)
746 goto out2;
747
748 length = security_get_user_sids(sid, user, &sids, &nsids);
749 if (length < 0)
750 goto out2;
751
752 length = sprintf(buf, "%u", nsids) + 1;
753 ptr = buf + length;
754 for (i = 0; i < nsids; i++) {
755 rc = security_sid_to_context(sids[i], &newcon, &len);
756 if (rc) {
757 length = rc;
758 goto out3;
759 }
760 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
761 kfree(newcon);
762 length = -ERANGE;
763 goto out3;
764 }
765 memcpy(ptr, newcon, len);
766 kfree(newcon);
767 ptr += len;
768 length += len;
769 }
770out3:
771 kfree(sids);
772out2:
773 kfree(user);
774out:
775 kfree(con);
776 return length;
777}
778
Eric Paris18729812008-04-17 14:15:45 -0400779static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 char *scon, *tcon;
782 u32 ssid, tsid, newsid;
783 u16 tclass;
784 ssize_t length;
785 char *newcon;
786 u32 len;
787
788 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
789 if (length)
790 return length;
791
792 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800793 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!scon)
795 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800797 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!tcon)
799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 length = -EINVAL;
802 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
803 goto out2;
804
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800805 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (length < 0)
807 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800808 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (length < 0)
810 goto out2;
811
812 length = security_member_sid(ssid, tsid, tclass, &newsid);
813 if (length < 0)
814 goto out2;
815
816 length = security_sid_to_context(newsid, &newcon, &len);
817 if (length < 0)
818 goto out2;
819
820 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400821 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
822 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 length = -ERANGE;
824 goto out3;
825 }
826
827 memcpy(buf, newcon, len);
828 length = len;
829out3:
830 kfree(newcon);
831out2:
832 kfree(tcon);
833out:
834 kfree(scon);
835 return length;
836}
837
838static struct inode *sel_make_inode(struct super_block *sb, int mode)
839{
840 struct inode *ret = new_inode(sb);
841
842 if (ret) {
843 ret->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
845 }
846 return ret;
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static ssize_t sel_read_bool(struct file *filep, char __user *buf,
850 size_t count, loff_t *ppos)
851{
852 char *page = NULL;
853 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ssize_t ret;
855 int cur_enforcing;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500856 struct inode *inode = filep->f_path.dentry->d_inode;
857 unsigned index = inode->i_ino & SEL_INO_MASK;
858 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Ingo Molnarbb003072006-03-22 00:09:14 -0800860 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500862 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
863 ret = -EINVAL;
864 goto out;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric Paris18729812008-04-17 14:15:45 -0400867 page = (char *)get_zeroed_page(GFP_KERNEL);
868 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ret = -ENOMEM;
870 goto out;
871 }
872
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500873 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (cur_enforcing < 0) {
875 ret = cur_enforcing;
876 goto out;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500879 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800880 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800882 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (page)
884 free_page((unsigned long)page);
885 return ret;
886}
887
888static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
889 size_t count, loff_t *ppos)
890{
891 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500892 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int new_value;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500894 struct inode *inode = filep->f_path.dentry->d_inode;
895 unsigned index = inode->i_ino & SEL_INO_MASK;
896 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Ingo Molnarbb003072006-03-22 00:09:14 -0800898 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 length = task_has_security(current, SECURITY__SETBOOL);
901 if (length)
902 goto out;
903
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500904 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
905 length = -EINVAL;
906 goto out;
907 }
908
Davi Arnautbfd51622005-10-30 14:59:24 -0800909 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 length = -ENOMEM;
911 goto out;
912 }
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (*ppos != 0) {
915 /* No partial writes. */
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500916 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto out;
918 }
Eric Paris18729812008-04-17 14:15:45 -0400919 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (!page) {
921 length = -ENOMEM;
922 goto out;
923 }
924
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500925 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (copy_from_user(page, buf, count))
927 goto out;
928
929 length = -EINVAL;
930 if (sscanf(page, "%d", &new_value) != 1)
931 goto out;
932
933 if (new_value)
934 new_value = 1;
935
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500936 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 length = count;
938
939out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800940 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (page)
942 free_page((unsigned long) page);
943 return length;
944}
945
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800946static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -0400947 .read = sel_read_bool,
948 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200949 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950};
951
952static ssize_t sel_commit_bools_write(struct file *filep,
953 const char __user *buf,
954 size_t count, loff_t *ppos)
955{
956 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500957 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int new_value;
959
Ingo Molnarbb003072006-03-22 00:09:14 -0800960 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 length = task_has_security(current, SECURITY__SETBOOL);
963 if (length)
964 goto out;
965
Davi Arnautbfd51622005-10-30 14:59:24 -0800966 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 length = -ENOMEM;
968 goto out;
969 }
970 if (*ppos != 0) {
971 /* No partial writes. */
972 goto out;
973 }
Eric Paris18729812008-04-17 14:15:45 -0400974 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!page) {
976 length = -ENOMEM;
977 goto out;
978 }
979
Stephen Smalleyd313f94832007-11-26 11:12:53 -0500980 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (copy_from_user(page, buf, count))
982 goto out;
983
984 length = -EINVAL;
985 if (sscanf(page, "%d", &new_value) != 1)
986 goto out;
987
Eric Paris18729812008-04-17 14:15:45 -0400988 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 length = count;
992
993out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800994 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (page)
996 free_page((unsigned long) page);
997 return length;
998}
999
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001000static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001001 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001002 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003};
1004
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001005static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Stephen Smalley0955dc02007-11-21 09:01:36 -05001007 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 spin_lock(&dcache_lock);
1010 node = de->d_subdirs.next;
1011 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001012 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 list_del_init(node);
1014
1015 if (d->d_inode) {
1016 d = dget_locked(d);
1017 spin_unlock(&dcache_lock);
1018 d_delete(d);
1019 simple_unlink(de->d_inode, d);
1020 dput(d);
1021 spin_lock(&dcache_lock);
1022 }
1023 node = de->d_subdirs.next;
1024 }
1025
1026 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029#define BOOL_DIR_NAME "booleans"
1030
1031static int sel_make_bools(void)
1032{
1033 int i, ret = 0;
1034 ssize_t len;
1035 struct dentry *dentry = NULL;
1036 struct dentry *dir = bool_dir;
1037 struct inode *inode = NULL;
1038 struct inode_security_struct *isec;
1039 char **names = NULL, *page;
1040 int num;
1041 int *values = NULL;
1042 u32 sid;
1043
1044 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001045 for (i = 0; i < bool_num; i++)
1046 kfree(bool_pending_names[i]);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001047 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001048 kfree(bool_pending_values);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001049 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001050 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001052 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric Paris18729812008-04-17 14:15:45 -04001054 page = (char *)get_zeroed_page(GFP_KERNEL);
1055 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -ENOMEM;
1057
1058 ret = security_get_bools(&num, &names, &values);
1059 if (ret != 0)
1060 goto out;
1061
1062 for (i = 0; i < num; i++) {
1063 dentry = d_alloc_name(dir, names[i]);
1064 if (!dentry) {
1065 ret = -ENOMEM;
1066 goto err;
1067 }
1068 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1069 if (!inode) {
1070 ret = -ENOMEM;
1071 goto err;
1072 }
1073
1074 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1075 if (len < 0) {
1076 ret = -EINVAL;
1077 goto err;
1078 } else if (len >= PAGE_SIZE) {
1079 ret = -ENAMETOOLONG;
1080 goto err;
1081 }
Eric Paris18729812008-04-17 14:15:45 -04001082 isec = (struct inode_security_struct *)inode->i_security;
1083 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1084 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto err;
1086 isec->sid = sid;
1087 isec->initialized = 1;
1088 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001089 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 d_add(dentry, inode);
1091 }
1092 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001093 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 bool_pending_values = values;
1095out:
1096 free_page((unsigned long)page);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001097 return ret;
1098err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001100 for (i = 0; i < num; i++)
1101 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 kfree(names);
1103 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001104 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001105 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ret = -ENOMEM;
1107 goto out;
1108}
1109
1110#define NULL_FILE_NAME "null"
1111
Eric Paris18729812008-04-17 14:15:45 -04001112struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1115 size_t count, loff_t *ppos)
1116{
1117 char tmpbuf[TMPBUFLEN];
1118 ssize_t length;
1119
1120 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1121 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1122}
1123
Eric Paris18729812008-04-17 14:15:45 -04001124static ssize_t sel_write_avc_cache_threshold(struct file *file,
1125 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 size_t count, loff_t *ppos)
1127
1128{
1129 char *page;
1130 ssize_t ret;
1131 int new_value;
1132
Davi Arnautbfd51622005-10-30 14:59:24 -08001133 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ret = -ENOMEM;
1135 goto out;
1136 }
1137
1138 if (*ppos != 0) {
1139 /* No partial writes. */
1140 ret = -EINVAL;
1141 goto out;
1142 }
1143
Eric Paris18729812008-04-17 14:15:45 -04001144 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!page) {
1146 ret = -ENOMEM;
1147 goto out;
1148 }
1149
1150 if (copy_from_user(page, buf, count)) {
1151 ret = -EFAULT;
1152 goto out_free;
1153 }
1154
1155 if (sscanf(page, "%u", &new_value) != 1) {
1156 ret = -EINVAL;
1157 goto out;
1158 }
1159
1160 if (new_value != avc_cache_threshold) {
1161 ret = task_has_security(current, SECURITY__SETSECPARAM);
1162 if (ret)
1163 goto out_free;
1164 avc_cache_threshold = new_value;
1165 }
1166 ret = count;
1167out_free:
1168 free_page((unsigned long)page);
1169out:
1170 return ret;
1171}
1172
1173static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1174 size_t count, loff_t *ppos)
1175{
1176 char *page;
1177 ssize_t ret = 0;
1178
1179 page = (char *)__get_free_page(GFP_KERNEL);
1180 if (!page) {
1181 ret = -ENOMEM;
1182 goto out;
1183 }
1184 ret = avc_get_hash_stats(page);
1185 if (ret >= 0)
1186 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1187 free_page((unsigned long)page);
1188out:
1189 return ret;
1190}
1191
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001192static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 .read = sel_read_avc_cache_threshold,
1194 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001195 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196};
1197
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001198static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001200 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201};
1202
1203#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1204static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1205{
1206 int cpu;
1207
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301208 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!cpu_possible(cpu))
1210 continue;
1211 *idx = cpu + 1;
1212 return &per_cpu(avc_cache_stats, cpu);
1213 }
1214 return NULL;
1215}
1216
1217static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1218{
1219 loff_t n = *pos - 1;
1220
1221 if (*pos == 0)
1222 return SEQ_START_TOKEN;
1223
1224 return sel_avc_get_stat_idx(&n);
1225}
1226
1227static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1228{
1229 return sel_avc_get_stat_idx(pos);
1230}
1231
1232static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1233{
1234 struct avc_cache_stats *st = v;
1235
1236 if (v == SEQ_START_TOKEN)
1237 seq_printf(seq, "lookups hits misses allocations reclaims "
1238 "frees\n");
1239 else
1240 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1241 st->hits, st->misses, st->allocations,
1242 st->reclaims, st->frees);
1243 return 0;
1244}
1245
1246static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1247{ }
1248
Jan Engelhardt1996a102008-01-23 00:02:58 +01001249static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 .start = sel_avc_stats_seq_start,
1251 .next = sel_avc_stats_seq_next,
1252 .show = sel_avc_stats_seq_show,
1253 .stop = sel_avc_stats_seq_stop,
1254};
1255
1256static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1257{
1258 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1259}
1260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001261static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .open = sel_open_avc_cache_stats,
1263 .read = seq_read,
1264 .llseek = seq_lseek,
1265 .release = seq_release,
1266};
1267#endif
1268
1269static int sel_make_avc_files(struct dentry *dir)
1270{
1271 int i, ret = 0;
1272 static struct tree_descr files[] = {
1273 { "cache_threshold",
1274 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1275 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1276#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1277 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1278#endif
1279 };
1280
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001281 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct inode *inode;
1283 struct dentry *dentry;
1284
1285 dentry = d_alloc_name(dir, files[i].name);
1286 if (!dentry) {
1287 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1292 if (!inode) {
1293 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001297 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 d_add(dentry, inode);
1299 }
1300out:
1301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Eric Paris18729812008-04-17 14:15:45 -04001304static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001305 size_t count, loff_t *ppos)
1306{
1307 struct inode *inode;
1308 char *con;
1309 u32 sid, len;
1310 ssize_t ret;
1311
1312 inode = file->f_path.dentry->d_inode;
1313 sid = inode->i_ino&SEL_INO_MASK;
1314 ret = security_sid_to_context(sid, &con, &len);
1315 if (ret < 0)
1316 return ret;
1317
1318 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1319 kfree(con);
1320 return ret;
1321}
1322
1323static const struct file_operations sel_initcon_ops = {
1324 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001325 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001326};
1327
1328static int sel_make_initcon_files(struct dentry *dir)
1329{
1330 int i, ret = 0;
1331
1332 for (i = 1; i <= SECINITSID_NUM; i++) {
1333 struct inode *inode;
1334 struct dentry *dentry;
1335 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1336 if (!dentry) {
1337 ret = -ENOMEM;
1338 goto out;
1339 }
1340
1341 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1342 if (!inode) {
1343 ret = -ENOMEM;
1344 goto out;
1345 }
1346 inode->i_fop = &sel_initcon_ops;
1347 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1348 d_add(dentry, inode);
1349 }
1350out:
1351 return ret;
1352}
1353
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001354static inline unsigned int sel_div(unsigned long a, unsigned long b)
1355{
1356 return a / b - (a % b < 0);
1357}
1358
1359static inline unsigned long sel_class_to_ino(u16 class)
1360{
1361 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1362}
1363
1364static inline u16 sel_ino_to_class(unsigned long ino)
1365{
1366 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1367}
1368
1369static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1370{
1371 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1372}
1373
1374static inline u32 sel_ino_to_perm(unsigned long ino)
1375{
1376 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1377}
1378
Eric Paris18729812008-04-17 14:15:45 -04001379static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001380 size_t count, loff_t *ppos)
1381{
1382 ssize_t rc, len;
1383 char *page;
1384 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1385
1386 page = (char *)__get_free_page(GFP_KERNEL);
1387 if (!page) {
1388 rc = -ENOMEM;
1389 goto out;
1390 }
1391
1392 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1393 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1394 free_page((unsigned long)page);
1395out:
1396 return rc;
1397}
1398
1399static const struct file_operations sel_class_ops = {
1400 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001401 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001402};
1403
Eric Paris18729812008-04-17 14:15:45 -04001404static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001405 size_t count, loff_t *ppos)
1406{
1407 ssize_t rc, len;
1408 char *page;
1409 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1410
1411 page = (char *)__get_free_page(GFP_KERNEL);
1412 if (!page) {
1413 rc = -ENOMEM;
1414 goto out;
1415 }
1416
Eric Paris18729812008-04-17 14:15:45 -04001417 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001418 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1419 free_page((unsigned long)page);
1420out:
1421 return rc;
1422}
1423
1424static const struct file_operations sel_perm_ops = {
1425 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001426 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001427};
1428
Paul Moore3bb56b22008-01-29 08:38:19 -05001429static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1430 size_t count, loff_t *ppos)
1431{
1432 int value;
1433 char tmpbuf[TMPBUFLEN];
1434 ssize_t length;
1435 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1436
1437 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1438 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1439
1440 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1441}
1442
1443static const struct file_operations sel_policycap_ops = {
1444 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001445 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001446};
1447
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001448static int sel_make_perm_files(char *objclass, int classvalue,
1449 struct dentry *dir)
1450{
1451 int i, rc = 0, nperms;
1452 char **perms;
1453
1454 rc = security_get_permissions(objclass, &perms, &nperms);
1455 if (rc)
1456 goto out;
1457
1458 for (i = 0; i < nperms; i++) {
1459 struct inode *inode;
1460 struct dentry *dentry;
1461
1462 dentry = d_alloc_name(dir, perms[i]);
1463 if (!dentry) {
1464 rc = -ENOMEM;
1465 goto out1;
1466 }
1467
1468 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1469 if (!inode) {
1470 rc = -ENOMEM;
1471 goto out1;
1472 }
1473 inode->i_fop = &sel_perm_ops;
1474 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001475 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001476 d_add(dentry, inode);
1477 }
1478
1479out1:
1480 for (i = 0; i < nperms; i++)
1481 kfree(perms[i]);
1482 kfree(perms);
1483out:
1484 return rc;
1485}
1486
1487static int sel_make_class_dir_entries(char *classname, int index,
1488 struct dentry *dir)
1489{
1490 struct dentry *dentry = NULL;
1491 struct inode *inode = NULL;
1492 int rc;
1493
1494 dentry = d_alloc_name(dir, "index");
1495 if (!dentry) {
1496 rc = -ENOMEM;
1497 goto out;
1498 }
1499
1500 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1501 if (!inode) {
1502 rc = -ENOMEM;
1503 goto out;
1504 }
1505
1506 inode->i_fop = &sel_class_ops;
1507 inode->i_ino = sel_class_to_ino(index);
1508 d_add(dentry, inode);
1509
1510 dentry = d_alloc_name(dir, "perms");
1511 if (!dentry) {
1512 rc = -ENOMEM;
1513 goto out;
1514 }
1515
1516 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1517 if (rc)
1518 goto out;
1519
1520 rc = sel_make_perm_files(classname, index, dentry);
1521
1522out:
1523 return rc;
1524}
1525
1526static void sel_remove_classes(void)
1527{
1528 struct list_head *class_node;
1529
1530 list_for_each(class_node, &class_dir->d_subdirs) {
1531 struct dentry *class_subdir = list_entry(class_node,
1532 struct dentry, d_u.d_child);
1533 struct list_head *class_subdir_node;
1534
1535 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1536 struct dentry *d = list_entry(class_subdir_node,
1537 struct dentry, d_u.d_child);
1538
1539 if (d->d_inode)
1540 if (d->d_inode->i_mode & S_IFDIR)
1541 sel_remove_entries(d);
1542 }
1543
1544 sel_remove_entries(class_subdir);
1545 }
1546
1547 sel_remove_entries(class_dir);
1548}
1549
1550static int sel_make_classes(void)
1551{
1552 int rc = 0, nclasses, i;
1553 char **classes;
1554
1555 /* delete any existing entries */
1556 sel_remove_classes();
1557
1558 rc = security_get_classes(&classes, &nclasses);
1559 if (rc < 0)
1560 goto out;
1561
1562 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001563 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001564
1565 for (i = 0; i < nclasses; i++) {
1566 struct dentry *class_name_dir;
1567
1568 class_name_dir = d_alloc_name(class_dir, classes[i]);
1569 if (!class_name_dir) {
1570 rc = -ENOMEM;
1571 goto out1;
1572 }
1573
1574 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1575 &last_class_ino);
1576 if (rc)
1577 goto out1;
1578
1579 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001580 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001581 class_name_dir);
1582 if (rc)
1583 goto out1;
1584 }
1585
1586out1:
1587 for (i = 0; i < nclasses; i++)
1588 kfree(classes[i]);
1589 kfree(classes);
1590out:
1591 return rc;
1592}
1593
Paul Moore3bb56b22008-01-29 08:38:19 -05001594static int sel_make_policycap(void)
1595{
1596 unsigned int iter;
1597 struct dentry *dentry = NULL;
1598 struct inode *inode = NULL;
1599
1600 sel_remove_entries(policycap_dir);
1601
1602 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1603 if (iter < ARRAY_SIZE(policycap_names))
1604 dentry = d_alloc_name(policycap_dir,
1605 policycap_names[iter]);
1606 else
1607 dentry = d_alloc_name(policycap_dir, "unknown");
1608
1609 if (dentry == NULL)
1610 return -ENOMEM;
1611
1612 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1613 if (inode == NULL)
1614 return -ENOMEM;
1615
1616 inode->i_fop = &sel_policycap_ops;
1617 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1618 d_add(dentry, inode);
1619 }
1620
1621 return 0;
1622}
1623
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001624static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1625 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int ret = 0;
1628 struct inode *inode;
1629
James Morrisedb20fb2006-03-22 00:09:20 -08001630 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!inode) {
1632 ret = -ENOMEM;
1633 goto out;
1634 }
1635 inode->i_op = &simple_dir_inode_operations;
1636 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001637 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001638 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001639 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001641 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001642 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643out:
1644 return ret;
1645}
1646
Eric Paris18729812008-04-17 14:15:45 -04001647static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 int ret;
1650 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001651 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct inode_security_struct *isec;
1653
1654 static struct tree_descr selinux_files[] = {
1655 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1656 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001657 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1659 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1660 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1661 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1662 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1663 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1664 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1665 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1666 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1667 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001668 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1669 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001670 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* last one */ {""}
1672 };
1673 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1674 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001675 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
James Morrisedb20fb2006-03-22 00:09:20 -08001677 root_inode = sb->s_root->d_inode;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001680 if (!dentry) {
1681 ret = -ENOMEM;
1682 goto err;
1683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001685 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001686 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001687 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001692 if (!dentry) {
1693 ret = -ENOMEM;
1694 goto err;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001698 if (!inode) {
1699 ret = -ENOMEM;
1700 goto err;
1701 }
James Carter6174eaf2007-04-04 16:18:39 -04001702 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001703 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 isec->sid = SECINITSID_DEVNULL;
1705 isec->sclass = SECCLASS_CHR_FILE;
1706 isec->initialized = 1;
1707
1708 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1709 d_add(dentry, inode);
1710 selinux_null = dentry;
1711
1712 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001713 if (!dentry) {
1714 ret = -ENOMEM;
1715 goto err;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001718 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001720 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 ret = sel_make_avc_files(dentry);
1723 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001724 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001725
1726 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1727 if (!dentry) {
1728 ret = -ENOMEM;
1729 goto err;
1730 }
1731
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001732 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001733 if (ret)
1734 goto err;
1735
1736 ret = sel_make_initcon_files(dentry);
1737 if (ret)
1738 goto err;
1739
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001740 dentry = d_alloc_name(sb->s_root, "class");
1741 if (!dentry) {
1742 ret = -ENOMEM;
1743 goto err;
1744 }
1745
1746 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1747 if (ret)
1748 goto err;
1749
1750 class_dir = dentry;
1751
Paul Moore3bb56b22008-01-29 08:38:19 -05001752 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1753 if (!dentry) {
1754 ret = -ENOMEM;
1755 goto err;
1756 }
1757
1758 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1759 if (ret)
1760 goto err;
1761
1762 policycap_dir = dentry;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764out:
James Morris161ce452006-03-22 00:09:17 -08001765 return ret;
1766err:
Eric Paris744ba352008-04-17 11:52:44 -04001767 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1768 __func__);
James Morris161ce452006-03-22 00:09:17 -08001769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
David Howells454e2392006-06-23 02:02:57 -07001772static int sel_get_sb(struct file_system_type *fs_type,
1773 int flags, const char *dev_name, void *data,
1774 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
David Howells454e2392006-06-23 02:02:57 -07001776 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779static struct file_system_type sel_fs_type = {
1780 .name = "selinuxfs",
1781 .get_sb = sel_get_sb,
1782 .kill_sb = kill_litter_super,
1783};
1784
1785struct vfsmount *selinuxfs_mount;
1786
1787static int __init init_sel_fs(void)
1788{
1789 int err;
1790
1791 if (!selinux_enabled)
1792 return 0;
1793 err = register_filesystem(&sel_fs_type);
1794 if (!err) {
1795 selinuxfs_mount = kern_mount(&sel_fs_type);
1796 if (IS_ERR(selinuxfs_mount)) {
1797 printk(KERN_ERR "selinuxfs: could not mount!\n");
1798 err = PTR_ERR(selinuxfs_mount);
1799 selinuxfs_mount = NULL;
1800 }
1801 }
1802 return err;
1803}
1804
1805__initcall(init_sel_fs);
1806
1807#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1808void exit_sel_fs(void)
1809{
1810 unregister_filesystem(&sel_fs_type);
1811}
1812#endif