blob: 017ec096446ea1825c60cf1aff5bd36fecbcb823 [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 Smalleyd313f942007-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
Eric Pariscee74f42010-10-13 17:50:25 -040071static char policy_opened;
72
Paul Moore3bb56b22008-01-29 08:38:19 -050073/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040074static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076extern void selnl_notify_setenforce(int val);
77
78/* Check whether a task is allowed to use a security operation. */
79static int task_has_security(struct task_struct *tsk,
80 u32 perms)
81{
David Howellsc69e8d92008-11-14 10:39:19 +110082 const struct task_security_struct *tsec;
83 u32 sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
David Howellsc69e8d92008-11-14 10:39:19 +110085 rcu_read_lock();
86 tsec = __task_cred(tsk)->security;
87 if (tsec)
88 sid = tsec->sid;
89 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!tsec)
91 return -EACCES;
92
David Howellsc69e8d92008-11-14 10:39:19 +110093 return avc_has_perm(sid, SECINITSID_SECURITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 SECCLASS_SECURITY, perms, NULL);
95}
96
97enum sel_inos {
98 SEL_ROOT_INO = 2,
99 SEL_LOAD, /* load policy */
100 SEL_ENFORCE, /* get or set enforcing status */
101 SEL_CONTEXT, /* validate context */
102 SEL_ACCESS, /* compute access decision */
103 SEL_CREATE, /* compute create labeling decision */
104 SEL_RELABEL, /* compute relabeling decision */
105 SEL_USER, /* compute reachable user contexts */
106 SEL_POLICYVERS, /* return policy version for this kernel */
107 SEL_COMMIT_BOOLS, /* commit new boolean values */
108 SEL_MLS, /* return if MLS policy is enabled */
109 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 SEL_MEMBER, /* compute polyinstantiation membership decision */
111 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700112 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400113 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
114 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +0900115 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -0400116 SEL_POLICY, /* allow userspace to read the in kernel policy */
James Carter6174eaf2007-04-04 16:18:39 -0400117 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
James Carter6174eaf2007-04-04 16:18:39 -0400120static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
121
Paul Moore3bb56b22008-01-29 08:38:19 -0500122#define SEL_INITCON_INO_OFFSET 0x01000000
123#define SEL_BOOL_INO_OFFSET 0x02000000
124#define SEL_CLASS_INO_OFFSET 0x04000000
125#define SEL_POLICYCAP_INO_OFFSET 0x08000000
126#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define TMPBUFLEN 12
129static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
130 size_t count, loff_t *ppos)
131{
132 char tmpbuf[TMPBUFLEN];
133 ssize_t length;
134
135 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
136 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
137}
138
139#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400140static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 size_t count, loff_t *ppos)
142
143{
144 char *page;
145 ssize_t length;
146 int new_value;
147
Davi Arnautbfd51622005-10-30 14:59:24 -0800148 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -ENOMEM;
150 if (*ppos != 0) {
151 /* No partial writes. */
152 return -EINVAL;
153 }
Eric Paris18729812008-04-17 14:15:45 -0400154 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!page)
156 return -ENOMEM;
157 length = -EFAULT;
158 if (copy_from_user(page, buf, count))
159 goto out;
160
161 length = -EINVAL;
162 if (sscanf(page, "%d", &new_value) != 1)
163 goto out;
164
165 if (new_value != selinux_enforcing) {
166 length = task_has_security(current, SECURITY__SETENFORCE);
167 if (length)
168 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000169 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500170 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
171 new_value, selinux_enforcing,
172 audit_get_loginuid(current),
173 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 selinux_enforcing = new_value;
175 if (selinux_enforcing)
176 avc_ss_reset(0);
177 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900178 selinux_status_update_setenforce(selinux_enforcing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 length = count;
181out:
182 free_page((unsigned long) page);
183 return length;
184}
185#else
186#define sel_write_enforce NULL
187#endif
188
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800189static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .read = sel_read_enforce,
191 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200192 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Eric Paris3f120702007-09-21 14:37:10 -0400195static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
196 size_t count, loff_t *ppos)
197{
198 char tmpbuf[TMPBUFLEN];
199 ssize_t length;
200 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
201 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
202 security_get_reject_unknown() : !security_get_allow_unknown();
203
204 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
205 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
206}
207
208static const struct file_operations sel_handle_unknown_ops = {
209 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200210 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400211};
212
KaiGai Kohei11904162010-09-14 18:28:39 +0900213static int sel_open_handle_status(struct inode *inode, struct file *filp)
214{
215 struct page *status = selinux_kernel_status_page();
216
217 if (!status)
218 return -ENOMEM;
219
220 filp->private_data = status;
221
222 return 0;
223}
224
225static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
226 size_t count, loff_t *ppos)
227{
228 struct page *status = filp->private_data;
229
230 BUG_ON(!status);
231
232 return simple_read_from_buffer(buf, count, ppos,
233 page_address(status),
234 sizeof(struct selinux_kernel_status));
235}
236
237static int sel_mmap_handle_status(struct file *filp,
238 struct vm_area_struct *vma)
239{
240 struct page *status = filp->private_data;
241 unsigned long size = vma->vm_end - vma->vm_start;
242
243 BUG_ON(!status);
244
245 /* only allows one page from the head */
246 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
247 return -EIO;
248 /* disallow writable mapping */
249 if (vma->vm_flags & VM_WRITE)
250 return -EPERM;
251 /* disallow mprotect() turns it into writable */
252 vma->vm_flags &= ~VM_MAYWRITE;
253
254 return remap_pfn_range(vma, vma->vm_start,
255 page_to_pfn(status),
256 size, vma->vm_page_prot);
257}
258
259static const struct file_operations sel_handle_status_ops = {
260 .open = sel_open_handle_status,
261 .read = sel_read_handle_status,
262 .mmap = sel_mmap_handle_status,
263 .llseek = generic_file_llseek,
264};
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400267static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 size_t count, loff_t *ppos)
269
270{
271 char *page;
272 ssize_t length;
273 int new_value;
274 extern int selinux_disable(void);
275
Davi Arnautbfd51622005-10-30 14:59:24 -0800276 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -ENOMEM;
278 if (*ppos != 0) {
279 /* No partial writes. */
280 return -EINVAL;
281 }
Eric Paris18729812008-04-17 14:15:45 -0400282 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!page)
284 return -ENOMEM;
285 length = -EFAULT;
286 if (copy_from_user(page, buf, count))
287 goto out;
288
289 length = -EINVAL;
290 if (sscanf(page, "%d", &new_value) != 1)
291 goto out;
292
293 if (new_value) {
294 length = selinux_disable();
295 if (length < 0)
296 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000297 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500298 "selinux=0 auid=%u ses=%u",
299 audit_get_loginuid(current),
300 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 length = count;
304out:
305 free_page((unsigned long) page);
306 return length;
307}
308#else
309#define sel_write_disable NULL
310#endif
311
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800312static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200314 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
317static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400318 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 char tmpbuf[TMPBUFLEN];
321 ssize_t length;
322
323 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
324 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
325}
326
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800327static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200329 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332/* declaration for sel_write_load */
333static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400334static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500335static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400336
337/* declaration for sel_make_class_dirs */
338static int sel_make_dir(struct inode *dir, struct dentry *dentry,
339 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341static ssize_t sel_read_mls(struct file *filp, char __user *buf,
342 size_t count, loff_t *ppos)
343{
344 char tmpbuf[TMPBUFLEN];
345 ssize_t length;
346
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100347 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
348 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
350}
351
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800352static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200354 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Eric Pariscee74f42010-10-13 17:50:25 -0400357struct policy_load_memory {
358 size_t len;
359 void *data;
360};
361
362static int sel_open_policy(struct inode *inode, struct file *filp)
363{
364 struct policy_load_memory *plm = NULL;
365 int rc;
366
367 BUG_ON(filp->private_data);
368
369 mutex_lock(&sel_mutex);
370
371 rc = task_has_security(current, SECURITY__READ_POLICY);
372 if (rc)
373 goto err;
374
375 rc = -EBUSY;
376 if (policy_opened)
377 goto err;
378
379 rc = -ENOMEM;
380 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
381 if (!plm)
382 goto err;
383
384 if (i_size_read(inode) != security_policydb_len()) {
385 mutex_lock(&inode->i_mutex);
386 i_size_write(inode, security_policydb_len());
387 mutex_unlock(&inode->i_mutex);
388 }
389
390 rc = security_read_policy(&plm->data, &plm->len);
391 if (rc)
392 goto err;
393
394 policy_opened = 1;
395
396 filp->private_data = plm;
397
398 mutex_unlock(&sel_mutex);
399
400 return 0;
401err:
402 mutex_unlock(&sel_mutex);
403
404 if (plm)
405 vfree(plm->data);
406 kfree(plm);
407 return rc;
408}
409
410static int sel_release_policy(struct inode *inode, struct file *filp)
411{
412 struct policy_load_memory *plm = filp->private_data;
413
414 BUG_ON(!plm);
415
416 policy_opened = 0;
417
418 vfree(plm->data);
419 kfree(plm);
420
421 return 0;
422}
423
424static ssize_t sel_read_policy(struct file *filp, char __user *buf,
425 size_t count, loff_t *ppos)
426{
427 struct policy_load_memory *plm = filp->private_data;
428 int ret;
429
430 mutex_lock(&sel_mutex);
431
432 ret = task_has_security(current, SECURITY__READ_POLICY);
433 if (ret)
434 goto out;
435
436 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
437out:
438 mutex_unlock(&sel_mutex);
439 return ret;
440}
441
Eric Paris845ca302010-10-13 17:50:31 -0400442static int sel_mmap_policy_fault(struct vm_area_struct *vma,
443 struct vm_fault *vmf)
444{
445 struct policy_load_memory *plm = vma->vm_file->private_data;
446 unsigned long offset;
447 struct page *page;
448
449 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
450 return VM_FAULT_SIGBUS;
451
452 offset = vmf->pgoff << PAGE_SHIFT;
453 if (offset >= roundup(plm->len, PAGE_SIZE))
454 return VM_FAULT_SIGBUS;
455
456 page = vmalloc_to_page(plm->data + offset);
457 get_page(page);
458
459 vmf->page = page;
460
461 return 0;
462}
463
464static struct vm_operations_struct sel_mmap_policy_ops = {
465 .fault = sel_mmap_policy_fault,
466 .page_mkwrite = sel_mmap_policy_fault,
467};
468
469int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
470{
471 if (vma->vm_flags & VM_SHARED) {
472 /* do not allow mprotect to make mapping writable */
473 vma->vm_flags &= ~VM_MAYWRITE;
474
475 if (vma->vm_flags & VM_WRITE)
476 return -EACCES;
477 }
478
479 vma->vm_flags |= VM_RESERVED;
480 vma->vm_ops = &sel_mmap_policy_ops;
481
482 return 0;
483}
484
Eric Pariscee74f42010-10-13 17:50:25 -0400485static const struct file_operations sel_policy_ops = {
486 .open = sel_open_policy,
487 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400488 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400489 .release = sel_release_policy,
490};
491
Eric Paris18729812008-04-17 14:15:45 -0400492static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 size_t count, loff_t *ppos)
494
495{
496 int ret;
497 ssize_t length;
498 void *data = NULL;
499
Ingo Molnarbb003072006-03-22 00:09:14 -0800500 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 length = task_has_security(current, SECURITY__LOAD_POLICY);
503 if (length)
504 goto out;
505
506 if (*ppos != 0) {
507 /* No partial writes. */
508 length = -EINVAL;
509 goto out;
510 }
511
Davi Arnautbfd51622005-10-30 14:59:24 -0800512 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 || (data = vmalloc(count)) == NULL) {
514 length = -ENOMEM;
515 goto out;
516 }
517
518 length = -EFAULT;
519 if (copy_from_user(data, buf, count) != 0)
520 goto out;
521
522 length = security_load_policy(data, count);
523 if (length)
524 goto out;
525
526 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400527 if (ret) {
528 length = ret;
529 goto out1;
530 }
531
532 ret = sel_make_classes();
Paul Moore3bb56b22008-01-29 08:38:19 -0500533 if (ret) {
534 length = ret;
535 goto out1;
536 }
537
538 ret = sel_make_policycap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ret)
540 length = ret;
541 else
542 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400543
544out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000545 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500546 "policy loaded auid=%u ses=%u",
547 audit_get_loginuid(current),
548 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800550 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 vfree(data);
552 return length;
553}
554
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800555static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200557 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
Eric Paris18729812008-04-17 14:15:45 -0400560static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800562 char *canon;
563 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ssize_t length;
565
566 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
567 if (length)
568 return length;
569
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800570 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800572 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800574 length = security_sid_to_context(sid, &canon, &len);
575 if (length < 0)
576 return length;
577
578 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400579 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
580 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800581 length = -ERANGE;
582 goto out;
583 }
584
585 memcpy(buf, canon, len);
586 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800588 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return length;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
593 size_t count, loff_t *ppos)
594{
595 char tmpbuf[TMPBUFLEN];
596 ssize_t length;
597
598 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
599 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
600}
601
Eric Paris18729812008-04-17 14:15:45 -0400602static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 size_t count, loff_t *ppos)
604{
605 char *page;
606 ssize_t length;
607 unsigned int new_value;
608
609 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
610 if (length)
611 return length;
612
Davi Arnautbfd51622005-10-30 14:59:24 -0800613 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -ENOMEM;
615 if (*ppos != 0) {
616 /* No partial writes. */
617 return -EINVAL;
618 }
Eric Paris18729812008-04-17 14:15:45 -0400619 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!page)
621 return -ENOMEM;
622 length = -EFAULT;
623 if (copy_from_user(page, buf, count))
624 goto out;
625
626 length = -EINVAL;
627 if (sscanf(page, "%u", &new_value) != 1)
628 goto out;
629
630 selinux_checkreqprot = new_value ? 1 : 0;
631 length = count;
632out:
633 free_page((unsigned long) page);
634 return length;
635}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800636static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .read = sel_read_checkreqprot,
638 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200639 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642/*
643 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
644 */
Eric Paris18729812008-04-17 14:15:45 -0400645static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
646static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
647static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
648static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
649static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651static ssize_t (*write_op[])(struct file *, char *, size_t) = {
652 [SEL_ACCESS] = sel_write_access,
653 [SEL_CREATE] = sel_write_create,
654 [SEL_RELABEL] = sel_write_relabel,
655 [SEL_USER] = sel_write_user,
656 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800657 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658};
659
660static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
661{
Eric Paris18729812008-04-17 14:15:45 -0400662 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 char *data;
664 ssize_t rv;
665
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800666 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -EINVAL;
668
669 data = simple_transaction_get(file, buf, size);
670 if (IS_ERR(data))
671 return PTR_ERR(data);
672
Eric Paris18729812008-04-17 14:15:45 -0400673 rv = write_op[ino](file, data, size);
674 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 simple_transaction_set(file, rv);
676 rv = size;
677 }
678 return rv;
679}
680
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800681static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .write = selinux_transaction_write,
683 .read = simple_transaction_read,
684 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200685 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686};
687
688/*
689 * payload - write methods
690 * If the method has a response, the response should be put in buf,
691 * and the length returned. Otherwise return 0 or and -error.
692 */
693
Eric Paris18729812008-04-17 14:15:45 -0400694static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 char *scon, *tcon;
697 u32 ssid, tsid;
698 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct av_decision avd;
700 ssize_t length;
701
702 length = task_has_security(current, SECURITY__COMPUTE_AV);
703 if (length)
704 return length;
705
706 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800707 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!scon)
709 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800711 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!tcon)
713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500716 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto out2;
718
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800719 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (length < 0)
721 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800722 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (length < 0)
724 goto out2;
725
Stephen Smalley19439d02010-01-14 17:28:10 -0500726 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900729 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500730 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900732 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733out2:
734 kfree(tcon);
735out:
736 kfree(scon);
737 return length;
738}
739
Eric Paris18729812008-04-17 14:15:45 -0400740static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 char *scon, *tcon;
743 u32 ssid, tsid, newsid;
744 u16 tclass;
745 ssize_t length;
746 char *newcon;
747 u32 len;
748
749 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
750 if (length)
751 return length;
752
753 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800754 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!scon)
756 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800758 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!tcon)
760 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 length = -EINVAL;
763 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
764 goto out2;
765
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800766 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (length < 0)
768 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800769 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (length < 0)
771 goto out2;
772
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400773 length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (length < 0)
775 goto out2;
776
777 length = security_sid_to_context(newsid, &newcon, &len);
778 if (length < 0)
779 goto out2;
780
781 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400782 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
783 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 length = -ERANGE;
785 goto out3;
786 }
787
788 memcpy(buf, newcon, len);
789 length = len;
790out3:
791 kfree(newcon);
792out2:
793 kfree(tcon);
794out:
795 kfree(scon);
796 return length;
797}
798
Eric Paris18729812008-04-17 14:15:45 -0400799static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 char *scon, *tcon;
802 u32 ssid, tsid, newsid;
803 u16 tclass;
804 ssize_t length;
805 char *newcon;
806 u32 len;
807
808 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
809 if (length)
810 return length;
811
812 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800813 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!scon)
815 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800817 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!tcon)
819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 length = -EINVAL;
822 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
823 goto out2;
824
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800825 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (length < 0)
827 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800828 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (length < 0)
830 goto out2;
831
832 length = security_change_sid(ssid, tsid, tclass, &newsid);
833 if (length < 0)
834 goto out2;
835
836 length = security_sid_to_context(newsid, &newcon, &len);
837 if (length < 0)
838 goto out2;
839
840 if (len > SIMPLE_TRANSACTION_LIMIT) {
841 length = -ERANGE;
842 goto out3;
843 }
844
845 memcpy(buf, newcon, len);
846 length = len;
847out3:
848 kfree(newcon);
849out2:
850 kfree(tcon);
851out:
852 kfree(scon);
853 return length;
854}
855
Eric Paris18729812008-04-17 14:15:45 -0400856static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 char *con, *user, *ptr;
859 u32 sid, *sids;
860 ssize_t length;
861 char *newcon;
862 int i, rc;
863 u32 len, nsids;
864
865 length = task_has_security(current, SECURITY__COMPUTE_USER);
866 if (length)
867 return length;
868
869 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800870 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!con)
872 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800874 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!user)
876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 length = -EINVAL;
879 if (sscanf(buf, "%s %s", con, user) != 2)
880 goto out2;
881
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800882 length = security_context_to_sid(con, strlen(con) + 1, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (length < 0)
884 goto out2;
885
886 length = security_get_user_sids(sid, user, &sids, &nsids);
887 if (length < 0)
888 goto out2;
889
890 length = sprintf(buf, "%u", nsids) + 1;
891 ptr = buf + length;
892 for (i = 0; i < nsids; i++) {
893 rc = security_sid_to_context(sids[i], &newcon, &len);
894 if (rc) {
895 length = rc;
896 goto out3;
897 }
898 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
899 kfree(newcon);
900 length = -ERANGE;
901 goto out3;
902 }
903 memcpy(ptr, newcon, len);
904 kfree(newcon);
905 ptr += len;
906 length += len;
907 }
908out3:
909 kfree(sids);
910out2:
911 kfree(user);
912out:
913 kfree(con);
914 return length;
915}
916
Eric Paris18729812008-04-17 14:15:45 -0400917static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 char *scon, *tcon;
920 u32 ssid, tsid, newsid;
921 u16 tclass;
922 ssize_t length;
923 char *newcon;
924 u32 len;
925
926 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
927 if (length)
928 return length;
929
930 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800931 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!scon)
933 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800935 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!tcon)
937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 length = -EINVAL;
940 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
941 goto out2;
942
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800943 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (length < 0)
945 goto out2;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800946 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (length < 0)
948 goto out2;
949
950 length = security_member_sid(ssid, tsid, tclass, &newsid);
951 if (length < 0)
952 goto out2;
953
954 length = security_sid_to_context(newsid, &newcon, &len);
955 if (length < 0)
956 goto out2;
957
958 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400959 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
960 "payload max\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 length = -ERANGE;
962 goto out3;
963 }
964
965 memcpy(buf, newcon, len);
966 length = len;
967out3:
968 kfree(newcon);
969out2:
970 kfree(tcon);
971out:
972 kfree(scon);
973 return length;
974}
975
976static struct inode *sel_make_inode(struct super_block *sb, int mode)
977{
978 struct inode *ret = new_inode(sb);
979
980 if (ret) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400981 ret->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ret->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
984 }
985 return ret;
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static ssize_t sel_read_bool(struct file *filep, char __user *buf,
989 size_t count, loff_t *ppos)
990{
991 char *page = NULL;
992 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ssize_t ret;
994 int cur_enforcing;
Stephen Smalleyd313f942007-11-26 11:12:53 -0500995 struct inode *inode = filep->f_path.dentry->d_inode;
996 unsigned index = inode->i_ino & SEL_INO_MASK;
997 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Ingo Molnarbb003072006-03-22 00:09:14 -0800999 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Stephen Smalleyd313f942007-11-26 11:12:53 -05001001 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
1002 ret = -EINVAL;
1003 goto out;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Paris18729812008-04-17 14:15:45 -04001006 page = (char *)get_zeroed_page(GFP_KERNEL);
1007 if (!page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ret = -ENOMEM;
1009 goto out;
1010 }
1011
Stephen Smalleyd313f942007-11-26 11:12:53 -05001012 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (cur_enforcing < 0) {
1014 ret = cur_enforcing;
1015 goto out;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f942007-11-26 11:12:53 -05001018 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001019 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001021 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (page)
1023 free_page((unsigned long)page);
1024 return ret;
1025}
1026
1027static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1028 size_t count, loff_t *ppos)
1029{
1030 char *page = NULL;
Stephen Smalleyd313f942007-11-26 11:12:53 -05001031 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int new_value;
Stephen Smalleyd313f942007-11-26 11:12:53 -05001033 struct inode *inode = filep->f_path.dentry->d_inode;
1034 unsigned index = inode->i_ino & SEL_INO_MASK;
1035 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ingo Molnarbb003072006-03-22 00:09:14 -08001037 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 length = task_has_security(current, SECURITY__SETBOOL);
1040 if (length)
1041 goto out;
1042
Stephen Smalleyd313f942007-11-26 11:12:53 -05001043 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
1044 length = -EINVAL;
1045 goto out;
1046 }
1047
Davi Arnautbfd51622005-10-30 14:59:24 -08001048 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 length = -ENOMEM;
1050 goto out;
1051 }
Stephen Smalleyd313f942007-11-26 11:12:53 -05001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (*ppos != 0) {
1054 /* No partial writes. */
Stephen Smalleyd313f942007-11-26 11:12:53 -05001055 length = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto out;
1057 }
Eric Paris18729812008-04-17 14:15:45 -04001058 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!page) {
1060 length = -ENOMEM;
1061 goto out;
1062 }
1063
Stephen Smalleyd313f942007-11-26 11:12:53 -05001064 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (copy_from_user(page, buf, count))
1066 goto out;
1067
1068 length = -EINVAL;
1069 if (sscanf(page, "%d", &new_value) != 1)
1070 goto out;
1071
1072 if (new_value)
1073 new_value = 1;
1074
Stephen Smalleyd313f942007-11-26 11:12:53 -05001075 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 length = count;
1077
1078out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001079 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (page)
1081 free_page((unsigned long) page);
1082 return length;
1083}
1084
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001085static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001086 .read = sel_read_bool,
1087 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001088 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
1090
1091static ssize_t sel_commit_bools_write(struct file *filep,
1092 const char __user *buf,
1093 size_t count, loff_t *ppos)
1094{
1095 char *page = NULL;
Stephen Smalleyd313f942007-11-26 11:12:53 -05001096 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int new_value;
1098
Ingo Molnarbb003072006-03-22 00:09:14 -08001099 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 length = task_has_security(current, SECURITY__SETBOOL);
1102 if (length)
1103 goto out;
1104
Davi Arnautbfd51622005-10-30 14:59:24 -08001105 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 length = -ENOMEM;
1107 goto out;
1108 }
1109 if (*ppos != 0) {
1110 /* No partial writes. */
1111 goto out;
1112 }
Eric Paris18729812008-04-17 14:15:45 -04001113 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!page) {
1115 length = -ENOMEM;
1116 goto out;
1117 }
1118
Stephen Smalleyd313f942007-11-26 11:12:53 -05001119 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (copy_from_user(page, buf, count))
1121 goto out;
1122
1123 length = -EINVAL;
1124 if (sscanf(page, "%d", &new_value) != 1)
1125 goto out;
1126
Eric Paris18729812008-04-17 14:15:45 -04001127 if (new_value && bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 length = count;
1131
1132out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001133 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (page)
1135 free_page((unsigned long) page);
1136 return length;
1137}
1138
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001139static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001140 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001141 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
1143
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001144static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Stephen Smalley0955dc02007-11-21 09:01:36 -05001146 struct list_head *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001149 spin_lock(&de->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 node = de->d_subdirs.next;
1151 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001152 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001153
1154 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 list_del_init(node);
1156
1157 if (d->d_inode) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001158 dget_locked_dlock(d);
1159 spin_unlock(&de->d_lock);
1160 spin_unlock(&d->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_unlock(&dcache_lock);
1162 d_delete(d);
1163 simple_unlink(de->d_inode, d);
1164 dput(d);
1165 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001166 spin_lock(&de->d_lock);
1167 } else
1168 spin_unlock(&d->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 node = de->d_subdirs.next;
1170 }
1171
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001172 spin_unlock(&de->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176#define BOOL_DIR_NAME "booleans"
1177
1178static int sel_make_bools(void)
1179{
1180 int i, ret = 0;
1181 ssize_t len;
1182 struct dentry *dentry = NULL;
1183 struct dentry *dir = bool_dir;
1184 struct inode *inode = NULL;
1185 struct inode_security_struct *isec;
1186 char **names = NULL, *page;
1187 int num;
1188 int *values = NULL;
1189 u32 sid;
1190
1191 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001192 for (i = 0; i < bool_num; i++)
1193 kfree(bool_pending_names[i]);
Stephen Smalleyd313f942007-11-26 11:12:53 -05001194 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001195 kfree(bool_pending_values);
Stephen Smalleyd313f942007-11-26 11:12:53 -05001196 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001197 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001199 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Eric Paris18729812008-04-17 14:15:45 -04001201 page = (char *)get_zeroed_page(GFP_KERNEL);
1202 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -ENOMEM;
1204
1205 ret = security_get_bools(&num, &names, &values);
1206 if (ret != 0)
1207 goto out;
1208
1209 for (i = 0; i < num; i++) {
1210 dentry = d_alloc_name(dir, names[i]);
1211 if (!dentry) {
1212 ret = -ENOMEM;
1213 goto err;
1214 }
1215 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1216 if (!inode) {
1217 ret = -ENOMEM;
1218 goto err;
1219 }
1220
1221 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1222 if (len < 0) {
1223 ret = -EINVAL;
1224 goto err;
1225 } else if (len >= PAGE_SIZE) {
1226 ret = -ENAMETOOLONG;
1227 goto err;
1228 }
Eric Paris18729812008-04-17 14:15:45 -04001229 isec = (struct inode_security_struct *)inode->i_security;
1230 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1231 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto err;
1233 isec->sid = sid;
1234 isec->initialized = 1;
1235 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001236 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 d_add(dentry, inode);
1238 }
1239 bool_num = num;
Stephen Smalleyd313f942007-11-26 11:12:53 -05001240 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 bool_pending_values = values;
1242out:
1243 free_page((unsigned long)page);
Stephen Smalleyd313f942007-11-26 11:12:53 -05001244 return ret;
1245err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001247 for (i = 0; i < num; i++)
1248 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 kfree(names);
1250 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001251 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001252 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ret = -ENOMEM;
1254 goto out;
1255}
1256
1257#define NULL_FILE_NAME "null"
1258
Eric Paris18729812008-04-17 14:15:45 -04001259struct dentry *selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1262 size_t count, loff_t *ppos)
1263{
1264 char tmpbuf[TMPBUFLEN];
1265 ssize_t length;
1266
1267 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1268 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1269}
1270
Eric Paris18729812008-04-17 14:15:45 -04001271static ssize_t sel_write_avc_cache_threshold(struct file *file,
1272 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 size_t count, loff_t *ppos)
1274
1275{
1276 char *page;
1277 ssize_t ret;
1278 int new_value;
1279
Davi Arnautbfd51622005-10-30 14:59:24 -08001280 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ret = -ENOMEM;
1282 goto out;
1283 }
1284
1285 if (*ppos != 0) {
1286 /* No partial writes. */
1287 ret = -EINVAL;
1288 goto out;
1289 }
1290
Eric Paris18729812008-04-17 14:15:45 -04001291 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!page) {
1293 ret = -ENOMEM;
1294 goto out;
1295 }
1296
1297 if (copy_from_user(page, buf, count)) {
1298 ret = -EFAULT;
1299 goto out_free;
1300 }
1301
1302 if (sscanf(page, "%u", &new_value) != 1) {
1303 ret = -EINVAL;
1304 goto out;
1305 }
1306
1307 if (new_value != avc_cache_threshold) {
1308 ret = task_has_security(current, SECURITY__SETSECPARAM);
1309 if (ret)
1310 goto out_free;
1311 avc_cache_threshold = new_value;
1312 }
1313 ret = count;
1314out_free:
1315 free_page((unsigned long)page);
1316out:
1317 return ret;
1318}
1319
1320static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1321 size_t count, loff_t *ppos)
1322{
1323 char *page;
1324 ssize_t ret = 0;
1325
1326 page = (char *)__get_free_page(GFP_KERNEL);
1327 if (!page) {
1328 ret = -ENOMEM;
1329 goto out;
1330 }
1331 ret = avc_get_hash_stats(page);
1332 if (ret >= 0)
1333 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1334 free_page((unsigned long)page);
1335out:
1336 return ret;
1337}
1338
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001339static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 .read = sel_read_avc_cache_threshold,
1341 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001342 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343};
1344
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001345static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001347 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348};
1349
1350#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1351static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1352{
1353 int cpu;
1354
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301355 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!cpu_possible(cpu))
1357 continue;
1358 *idx = cpu + 1;
1359 return &per_cpu(avc_cache_stats, cpu);
1360 }
1361 return NULL;
1362}
1363
1364static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1365{
1366 loff_t n = *pos - 1;
1367
1368 if (*pos == 0)
1369 return SEQ_START_TOKEN;
1370
1371 return sel_avc_get_stat_idx(&n);
1372}
1373
1374static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1375{
1376 return sel_avc_get_stat_idx(pos);
1377}
1378
1379static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1380{
1381 struct avc_cache_stats *st = v;
1382
1383 if (v == SEQ_START_TOKEN)
1384 seq_printf(seq, "lookups hits misses allocations reclaims "
1385 "frees\n");
1386 else
1387 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1388 st->hits, st->misses, st->allocations,
1389 st->reclaims, st->frees);
1390 return 0;
1391}
1392
1393static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1394{ }
1395
Jan Engelhardt1996a102008-01-23 00:02:58 +01001396static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 .start = sel_avc_stats_seq_start,
1398 .next = sel_avc_stats_seq_next,
1399 .show = sel_avc_stats_seq_show,
1400 .stop = sel_avc_stats_seq_stop,
1401};
1402
1403static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1404{
1405 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1406}
1407
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001408static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 .open = sel_open_avc_cache_stats,
1410 .read = seq_read,
1411 .llseek = seq_lseek,
1412 .release = seq_release,
1413};
1414#endif
1415
1416static int sel_make_avc_files(struct dentry *dir)
1417{
1418 int i, ret = 0;
1419 static struct tree_descr files[] = {
1420 { "cache_threshold",
1421 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1422 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1423#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1424 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1425#endif
1426 };
1427
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001428 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct inode *inode;
1430 struct dentry *dentry;
1431
1432 dentry = d_alloc_name(dir, files[i].name);
1433 if (!dentry) {
1434 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1439 if (!inode) {
1440 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001444 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 d_add(dentry, inode);
1446 }
1447out:
1448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Eric Paris18729812008-04-17 14:15:45 -04001451static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001452 size_t count, loff_t *ppos)
1453{
1454 struct inode *inode;
1455 char *con;
1456 u32 sid, len;
1457 ssize_t ret;
1458
1459 inode = file->f_path.dentry->d_inode;
1460 sid = inode->i_ino&SEL_INO_MASK;
1461 ret = security_sid_to_context(sid, &con, &len);
1462 if (ret < 0)
1463 return ret;
1464
1465 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1466 kfree(con);
1467 return ret;
1468}
1469
1470static const struct file_operations sel_initcon_ops = {
1471 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001472 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001473};
1474
1475static int sel_make_initcon_files(struct dentry *dir)
1476{
1477 int i, ret = 0;
1478
1479 for (i = 1; i <= SECINITSID_NUM; i++) {
1480 struct inode *inode;
1481 struct dentry *dentry;
1482 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1483 if (!dentry) {
1484 ret = -ENOMEM;
1485 goto out;
1486 }
1487
1488 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1489 if (!inode) {
1490 ret = -ENOMEM;
1491 goto out;
1492 }
1493 inode->i_fop = &sel_initcon_ops;
1494 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1495 d_add(dentry, inode);
1496 }
1497out:
1498 return ret;
1499}
1500
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001501static inline unsigned int sel_div(unsigned long a, unsigned long b)
1502{
1503 return a / b - (a % b < 0);
1504}
1505
1506static inline unsigned long sel_class_to_ino(u16 class)
1507{
1508 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1509}
1510
1511static inline u16 sel_ino_to_class(unsigned long ino)
1512{
1513 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1514}
1515
1516static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1517{
1518 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1519}
1520
1521static inline u32 sel_ino_to_perm(unsigned long ino)
1522{
1523 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1524}
1525
Eric Paris18729812008-04-17 14:15:45 -04001526static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001527 size_t count, loff_t *ppos)
1528{
1529 ssize_t rc, len;
1530 char *page;
1531 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1532
1533 page = (char *)__get_free_page(GFP_KERNEL);
1534 if (!page) {
1535 rc = -ENOMEM;
1536 goto out;
1537 }
1538
1539 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1540 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1541 free_page((unsigned long)page);
1542out:
1543 return rc;
1544}
1545
1546static const struct file_operations sel_class_ops = {
1547 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001548 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001549};
1550
Eric Paris18729812008-04-17 14:15:45 -04001551static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001552 size_t count, loff_t *ppos)
1553{
1554 ssize_t rc, len;
1555 char *page;
1556 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1557
1558 page = (char *)__get_free_page(GFP_KERNEL);
1559 if (!page) {
1560 rc = -ENOMEM;
1561 goto out;
1562 }
1563
Eric Paris18729812008-04-17 14:15:45 -04001564 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001565 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1566 free_page((unsigned long)page);
1567out:
1568 return rc;
1569}
1570
1571static const struct file_operations sel_perm_ops = {
1572 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001573 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001574};
1575
Paul Moore3bb56b22008-01-29 08:38:19 -05001576static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1577 size_t count, loff_t *ppos)
1578{
1579 int value;
1580 char tmpbuf[TMPBUFLEN];
1581 ssize_t length;
1582 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1583
1584 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1585 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1586
1587 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1588}
1589
1590static const struct file_operations sel_policycap_ops = {
1591 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001592 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001593};
1594
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001595static int sel_make_perm_files(char *objclass, int classvalue,
1596 struct dentry *dir)
1597{
1598 int i, rc = 0, nperms;
1599 char **perms;
1600
1601 rc = security_get_permissions(objclass, &perms, &nperms);
1602 if (rc)
1603 goto out;
1604
1605 for (i = 0; i < nperms; i++) {
1606 struct inode *inode;
1607 struct dentry *dentry;
1608
1609 dentry = d_alloc_name(dir, perms[i]);
1610 if (!dentry) {
1611 rc = -ENOMEM;
1612 goto out1;
1613 }
1614
1615 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1616 if (!inode) {
1617 rc = -ENOMEM;
1618 goto out1;
1619 }
1620 inode->i_fop = &sel_perm_ops;
1621 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001622 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001623 d_add(dentry, inode);
1624 }
1625
1626out1:
1627 for (i = 0; i < nperms; i++)
1628 kfree(perms[i]);
1629 kfree(perms);
1630out:
1631 return rc;
1632}
1633
1634static int sel_make_class_dir_entries(char *classname, int index,
1635 struct dentry *dir)
1636{
1637 struct dentry *dentry = NULL;
1638 struct inode *inode = NULL;
1639 int rc;
1640
1641 dentry = d_alloc_name(dir, "index");
1642 if (!dentry) {
1643 rc = -ENOMEM;
1644 goto out;
1645 }
1646
1647 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1648 if (!inode) {
1649 rc = -ENOMEM;
1650 goto out;
1651 }
1652
1653 inode->i_fop = &sel_class_ops;
1654 inode->i_ino = sel_class_to_ino(index);
1655 d_add(dentry, inode);
1656
1657 dentry = d_alloc_name(dir, "perms");
1658 if (!dentry) {
1659 rc = -ENOMEM;
1660 goto out;
1661 }
1662
1663 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1664 if (rc)
1665 goto out;
1666
1667 rc = sel_make_perm_files(classname, index, dentry);
1668
1669out:
1670 return rc;
1671}
1672
1673static void sel_remove_classes(void)
1674{
1675 struct list_head *class_node;
1676
1677 list_for_each(class_node, &class_dir->d_subdirs) {
1678 struct dentry *class_subdir = list_entry(class_node,
1679 struct dentry, d_u.d_child);
1680 struct list_head *class_subdir_node;
1681
1682 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1683 struct dentry *d = list_entry(class_subdir_node,
1684 struct dentry, d_u.d_child);
1685
1686 if (d->d_inode)
1687 if (d->d_inode->i_mode & S_IFDIR)
1688 sel_remove_entries(d);
1689 }
1690
1691 sel_remove_entries(class_subdir);
1692 }
1693
1694 sel_remove_entries(class_dir);
1695}
1696
1697static int sel_make_classes(void)
1698{
1699 int rc = 0, nclasses, i;
1700 char **classes;
1701
1702 /* delete any existing entries */
1703 sel_remove_classes();
1704
1705 rc = security_get_classes(&classes, &nclasses);
1706 if (rc < 0)
1707 goto out;
1708
1709 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001710 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001711
1712 for (i = 0; i < nclasses; i++) {
1713 struct dentry *class_name_dir;
1714
1715 class_name_dir = d_alloc_name(class_dir, classes[i]);
1716 if (!class_name_dir) {
1717 rc = -ENOMEM;
1718 goto out1;
1719 }
1720
1721 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1722 &last_class_ino);
1723 if (rc)
1724 goto out1;
1725
1726 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001727 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001728 class_name_dir);
1729 if (rc)
1730 goto out1;
1731 }
1732
1733out1:
1734 for (i = 0; i < nclasses; i++)
1735 kfree(classes[i]);
1736 kfree(classes);
1737out:
1738 return rc;
1739}
1740
Paul Moore3bb56b22008-01-29 08:38:19 -05001741static int sel_make_policycap(void)
1742{
1743 unsigned int iter;
1744 struct dentry *dentry = NULL;
1745 struct inode *inode = NULL;
1746
1747 sel_remove_entries(policycap_dir);
1748
1749 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1750 if (iter < ARRAY_SIZE(policycap_names))
1751 dentry = d_alloc_name(policycap_dir,
1752 policycap_names[iter]);
1753 else
1754 dentry = d_alloc_name(policycap_dir, "unknown");
1755
1756 if (dentry == NULL)
1757 return -ENOMEM;
1758
1759 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1760 if (inode == NULL)
1761 return -ENOMEM;
1762
1763 inode->i_fop = &sel_policycap_ops;
1764 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1765 d_add(dentry, inode);
1766 }
1767
1768 return 0;
1769}
1770
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001771static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1772 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int ret = 0;
1775 struct inode *inode;
1776
James Morrisedb20fb2006-03-22 00:09:20 -08001777 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (!inode) {
1779 ret = -ENOMEM;
1780 goto out;
1781 }
1782 inode->i_op = &simple_dir_inode_operations;
1783 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001784 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001785 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001786 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001788 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001789 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790out:
1791 return ret;
1792}
1793
Eric Paris18729812008-04-17 14:15:45 -04001794static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int ret;
1797 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001798 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 struct inode_security_struct *isec;
1800
1801 static struct tree_descr selinux_files[] = {
1802 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1803 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001804 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1806 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1807 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1808 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1809 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1810 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1811 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1812 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1813 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1814 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001815 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1816 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001817 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Pariscee74f42010-10-13 17:50:25 -04001818 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /* last one */ {""}
1820 };
1821 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1822 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001823 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
James Morrisedb20fb2006-03-22 00:09:20 -08001825 root_inode = sb->s_root->d_inode;
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001828 if (!dentry) {
1829 ret = -ENOMEM;
1830 goto err;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001833 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001834 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001835 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001840 if (!dentry) {
1841 ret = -ENOMEM;
1842 goto err;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001846 if (!inode) {
1847 ret = -ENOMEM;
1848 goto err;
1849 }
James Carter6174eaf2007-04-04 16:18:39 -04001850 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001851 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 isec->sid = SECINITSID_DEVNULL;
1853 isec->sclass = SECCLASS_CHR_FILE;
1854 isec->initialized = 1;
1855
1856 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1857 d_add(dentry, inode);
1858 selinux_null = dentry;
1859
1860 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001861 if (!dentry) {
1862 ret = -ENOMEM;
1863 goto err;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001866 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001868 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 ret = sel_make_avc_files(dentry);
1871 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001872 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001873
1874 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1875 if (!dentry) {
1876 ret = -ENOMEM;
1877 goto err;
1878 }
1879
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001880 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001881 if (ret)
1882 goto err;
1883
1884 ret = sel_make_initcon_files(dentry);
1885 if (ret)
1886 goto err;
1887
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001888 dentry = d_alloc_name(sb->s_root, "class");
1889 if (!dentry) {
1890 ret = -ENOMEM;
1891 goto err;
1892 }
1893
1894 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1895 if (ret)
1896 goto err;
1897
1898 class_dir = dentry;
1899
Paul Moore3bb56b22008-01-29 08:38:19 -05001900 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1901 if (!dentry) {
1902 ret = -ENOMEM;
1903 goto err;
1904 }
1905
1906 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1907 if (ret)
1908 goto err;
1909
1910 policycap_dir = dentry;
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912out:
James Morris161ce452006-03-22 00:09:17 -08001913 return ret;
1914err:
Eric Paris744ba352008-04-17 11:52:44 -04001915 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1916 __func__);
James Morris161ce452006-03-22 00:09:17 -08001917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Al Virofc14f2f2010-07-25 01:48:30 +04001920static struct dentry *sel_mount(struct file_system_type *fs_type,
1921 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Al Virofc14f2f2010-07-25 01:48:30 +04001923 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926static struct file_system_type sel_fs_type = {
1927 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001928 .mount = sel_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .kill_sb = kill_litter_super,
1930};
1931
1932struct vfsmount *selinuxfs_mount;
1933
1934static int __init init_sel_fs(void)
1935{
1936 int err;
1937
1938 if (!selinux_enabled)
1939 return 0;
1940 err = register_filesystem(&sel_fs_type);
1941 if (!err) {
1942 selinuxfs_mount = kern_mount(&sel_fs_type);
1943 if (IS_ERR(selinuxfs_mount)) {
1944 printk(KERN_ERR "selinuxfs: could not mount!\n");
1945 err = PTR_ERR(selinuxfs_mount);
1946 selinuxfs_mount = NULL;
1947 }
1948 }
1949 return err;
1950}
1951
1952__initcall(init_sel_fs);
1953
1954#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1955void exit_sel_fs(void)
1956{
1957 unregister_filesystem(&sel_fs_type);
1958}
1959#endif