Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Updated: Karl MacMillan <kmacmillan@tresys.com> |
| 2 | * |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 3 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 5 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 6 | * |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 7 | * Added support for the policy capability bitmap |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * 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 Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * the Free Software Foundation, version 2. |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 29 | #include <linux/audit.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 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 Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 42 | /* Policy capability filenames */ |
| 43 | static char *policycap_names[] = { |
Eric Paris | b0c636b | 2008-02-28 12:58:40 -0500 | [diff] [blame] | 44 | "network_peer_controls", |
| 45 | "open_perms" |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 49 | |
| 50 | static int __init checkreqprot_setup(char *str) |
| 51 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 52 | unsigned long checkreqprot; |
| 53 | if (!strict_strtoul(str, 0, &checkreqprot)) |
| 54 | selinux_checkreqprot = checkreqprot ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | __setup("checkreqprot=", checkreqprot_setup); |
| 58 | |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 59 | static DEFINE_MUTEX(sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* global data for booleans */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 62 | static struct dentry *bool_dir; |
| 63 | static int bool_num; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 64 | static char **bool_pending_names; |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 65 | static int *bool_pending_values; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 67 | /* global data for classes */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 68 | static struct dentry *class_dir; |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 69 | static unsigned long last_class_ino; |
| 70 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame^] | 71 | static char policy_opened; |
| 72 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 73 | /* global data for policy capabilities */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 74 | static struct dentry *policycap_dir; |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | extern void selnl_notify_setenforce(int val); |
| 77 | |
| 78 | /* Check whether a task is allowed to use a security operation. */ |
| 79 | static int task_has_security(struct task_struct *tsk, |
| 80 | u32 perms) |
| 81 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 82 | const struct task_security_struct *tsec; |
| 83 | u32 sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 85 | rcu_read_lock(); |
| 86 | tsec = __task_cred(tsk)->security; |
| 87 | if (tsec) |
| 88 | sid = tsec->sid; |
| 89 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (!tsec) |
| 91 | return -EACCES; |
| 92 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 93 | return avc_has_perm(sid, SECINITSID_SECURITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | SECCLASS_SECURITY, perms, NULL); |
| 95 | } |
| 96 | |
| 97 | enum 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | SEL_MEMBER, /* compute polyinstantiation membership decision */ |
| 111 | SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 112 | SEL_COMPAT_NET, /* whether to use old compat network packet controls */ |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 113 | SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ |
| 114 | SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 115 | SEL_STATUS, /* export current status using mmap() */ |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame^] | 116 | SEL_POLICY, /* allow userspace to read the in kernel policy */ |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 117 | SEL_INO_NEXT, /* The next inode number to use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 120 | static unsigned long sel_last_ino = SEL_INO_NEXT - 1; |
| 121 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 122 | #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 Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | #define TMPBUFLEN 12 |
| 129 | static 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 Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 140 | static ssize_t sel_write_enforce(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | size_t count, loff_t *ppos) |
| 142 | |
| 143 | { |
| 144 | char *page; |
| 145 | ssize_t length; |
| 146 | int new_value; |
| 147 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 148 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return -ENOMEM; |
| 150 | if (*ppos != 0) { |
| 151 | /* No partial writes. */ |
| 152 | return -EINVAL; |
| 153 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 154 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | 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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 169 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 170 | "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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | selinux_enforcing = new_value; |
| 175 | if (selinux_enforcing) |
| 176 | avc_ss_reset(0); |
| 177 | selnl_notify_setenforce(selinux_enforcing); |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 178 | selinux_status_update_setenforce(selinux_enforcing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | length = count; |
| 181 | out: |
| 182 | free_page((unsigned long) page); |
| 183 | return length; |
| 184 | } |
| 185 | #else |
| 186 | #define sel_write_enforce NULL |
| 187 | #endif |
| 188 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 189 | static const struct file_operations sel_enforce_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | .read = sel_read_enforce, |
| 191 | .write = sel_write_enforce, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 192 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 195 | static 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 | |
| 208 | static const struct file_operations sel_handle_unknown_ops = { |
| 209 | .read = sel_read_handle_unknown, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 210 | .llseek = generic_file_llseek, |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 211 | }; |
| 212 | |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 213 | static 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 | |
| 225 | static 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 | |
| 237 | static 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 | |
| 259 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 267 | static ssize_t sel_write_disable(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 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 Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 276 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return -ENOMEM; |
| 278 | if (*ppos != 0) { |
| 279 | /* No partial writes. */ |
| 280 | return -EINVAL; |
| 281 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 282 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 297 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 298 | "selinux=0 auid=%u ses=%u", |
| 299 | audit_get_loginuid(current), |
| 300 | audit_get_sessionid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | length = count; |
| 304 | out: |
| 305 | free_page((unsigned long) page); |
| 306 | return length; |
| 307 | } |
| 308 | #else |
| 309 | #define sel_write_disable NULL |
| 310 | #endif |
| 311 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 312 | static const struct file_operations sel_disable_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | .write = sel_write_disable, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 314 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 318 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
| 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 Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 327 | static const struct file_operations sel_policyvers_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | .read = sel_read_policyvers, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 329 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | /* declaration for sel_write_load */ |
| 333 | static int sel_make_bools(void); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 334 | static int sel_make_classes(void); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 335 | static int sel_make_policycap(void); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 336 | |
| 337 | /* declaration for sel_make_class_dirs */ |
| 338 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, |
| 339 | unsigned long *ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | static 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 Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 347 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", |
| 348 | security_mls_enabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 350 | } |
| 351 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 352 | static const struct file_operations sel_mls_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | .read = sel_read_mls, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 354 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame^] | 357 | struct policy_load_memory { |
| 358 | size_t len; |
| 359 | void *data; |
| 360 | }; |
| 361 | |
| 362 | static 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; |
| 401 | err: |
| 402 | mutex_unlock(&sel_mutex); |
| 403 | |
| 404 | if (plm) |
| 405 | vfree(plm->data); |
| 406 | kfree(plm); |
| 407 | return rc; |
| 408 | } |
| 409 | |
| 410 | static 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 | |
| 424 | static 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); |
| 437 | out: |
| 438 | mutex_unlock(&sel_mutex); |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static const struct file_operations sel_policy_ops = { |
| 443 | .open = sel_open_policy, |
| 444 | .read = sel_read_policy, |
| 445 | .release = sel_release_policy, |
| 446 | }; |
| 447 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 448 | static ssize_t sel_write_load(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | size_t count, loff_t *ppos) |
| 450 | |
| 451 | { |
| 452 | int ret; |
| 453 | ssize_t length; |
| 454 | void *data = NULL; |
| 455 | |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 456 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | length = task_has_security(current, SECURITY__LOAD_POLICY); |
| 459 | if (length) |
| 460 | goto out; |
| 461 | |
| 462 | if (*ppos != 0) { |
| 463 | /* No partial writes. */ |
| 464 | length = -EINVAL; |
| 465 | goto out; |
| 466 | } |
| 467 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 468 | if ((count > 64 * 1024 * 1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | || (data = vmalloc(count)) == NULL) { |
| 470 | length = -ENOMEM; |
| 471 | goto out; |
| 472 | } |
| 473 | |
| 474 | length = -EFAULT; |
| 475 | if (copy_from_user(data, buf, count) != 0) |
| 476 | goto out; |
| 477 | |
| 478 | length = security_load_policy(data, count); |
| 479 | if (length) |
| 480 | goto out; |
| 481 | |
| 482 | ret = sel_make_bools(); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 483 | if (ret) { |
| 484 | length = ret; |
| 485 | goto out1; |
| 486 | } |
| 487 | |
| 488 | ret = sel_make_classes(); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 489 | if (ret) { |
| 490 | length = ret; |
| 491 | goto out1; |
| 492 | } |
| 493 | |
| 494 | ret = sel_make_policycap(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (ret) |
| 496 | length = ret; |
| 497 | else |
| 498 | length = count; |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 499 | |
| 500 | out1: |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 501 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 502 | "policy loaded auid=%u ses=%u", |
| 503 | audit_get_loginuid(current), |
| 504 | audit_get_sessionid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | out: |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 506 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | vfree(data); |
| 508 | return length; |
| 509 | } |
| 510 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 511 | static const struct file_operations sel_load_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | .write = sel_write_load, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 513 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 516 | static ssize_t sel_write_context(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 518 | char *canon; |
| 519 | u32 sid, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | ssize_t length; |
| 521 | |
| 522 | length = task_has_security(current, SECURITY__CHECK_CONTEXT); |
| 523 | if (length) |
| 524 | return length; |
| 525 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 526 | length = security_context_to_sid(buf, size, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | if (length < 0) |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 528 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 530 | length = security_sid_to_context(sid, &canon, &len); |
| 531 | if (length < 0) |
| 532 | return length; |
| 533 | |
| 534 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 535 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 536 | "payload max\n", __func__, len); |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 537 | length = -ERANGE; |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | memcpy(buf, canon, len); |
| 542 | length = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | out: |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 544 | kfree(canon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | return length; |
| 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, |
| 549 | size_t count, loff_t *ppos) |
| 550 | { |
| 551 | char tmpbuf[TMPBUFLEN]; |
| 552 | ssize_t length; |
| 553 | |
| 554 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); |
| 555 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 556 | } |
| 557 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 558 | static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | size_t count, loff_t *ppos) |
| 560 | { |
| 561 | char *page; |
| 562 | ssize_t length; |
| 563 | unsigned int new_value; |
| 564 | |
| 565 | length = task_has_security(current, SECURITY__SETCHECKREQPROT); |
| 566 | if (length) |
| 567 | return length; |
| 568 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 569 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | return -ENOMEM; |
| 571 | if (*ppos != 0) { |
| 572 | /* No partial writes. */ |
| 573 | return -EINVAL; |
| 574 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 575 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | if (!page) |
| 577 | return -ENOMEM; |
| 578 | length = -EFAULT; |
| 579 | if (copy_from_user(page, buf, count)) |
| 580 | goto out; |
| 581 | |
| 582 | length = -EINVAL; |
| 583 | if (sscanf(page, "%u", &new_value) != 1) |
| 584 | goto out; |
| 585 | |
| 586 | selinux_checkreqprot = new_value ? 1 : 0; |
| 587 | length = count; |
| 588 | out: |
| 589 | free_page((unsigned long) page); |
| 590 | return length; |
| 591 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 592 | static const struct file_operations sel_checkreqprot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | .read = sel_read_checkreqprot, |
| 594 | .write = sel_write_checkreqprot, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 595 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | }; |
| 597 | |
| 598 | /* |
| 599 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c |
| 600 | */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 601 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size); |
| 602 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size); |
| 603 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); |
| 604 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size); |
| 605 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | static ssize_t (*write_op[])(struct file *, char *, size_t) = { |
| 608 | [SEL_ACCESS] = sel_write_access, |
| 609 | [SEL_CREATE] = sel_write_create, |
| 610 | [SEL_RELABEL] = sel_write_relabel, |
| 611 | [SEL_USER] = sel_write_user, |
| 612 | [SEL_MEMBER] = sel_write_member, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 613 | [SEL_CONTEXT] = sel_write_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) |
| 617 | { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 618 | ino_t ino = file->f_path.dentry->d_inode->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | char *data; |
| 620 | ssize_t rv; |
| 621 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 622 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return -EINVAL; |
| 624 | |
| 625 | data = simple_transaction_get(file, buf, size); |
| 626 | if (IS_ERR(data)) |
| 627 | return PTR_ERR(data); |
| 628 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 629 | rv = write_op[ino](file, data, size); |
| 630 | if (rv > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | simple_transaction_set(file, rv); |
| 632 | rv = size; |
| 633 | } |
| 634 | return rv; |
| 635 | } |
| 636 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 637 | static const struct file_operations transaction_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | .write = selinux_transaction_write, |
| 639 | .read = simple_transaction_read, |
| 640 | .release = simple_transaction_release, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 641 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | /* |
| 645 | * payload - write methods |
| 646 | * If the method has a response, the response should be put in buf, |
| 647 | * and the length returned. Otherwise return 0 or and -error. |
| 648 | */ |
| 649 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 650 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
| 652 | char *scon, *tcon; |
| 653 | u32 ssid, tsid; |
| 654 | u16 tclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | struct av_decision avd; |
| 656 | ssize_t length; |
| 657 | |
| 658 | length = task_has_security(current, SECURITY__COMPUTE_AV); |
| 659 | if (length) |
| 660 | return length; |
| 661 | |
| 662 | length = -ENOMEM; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 663 | scon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | if (!scon) |
| 665 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 667 | tcon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (!tcon) |
| 669 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
| 671 | length = -EINVAL; |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 672 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | goto out2; |
| 674 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 675 | length = security_context_to_sid(scon, strlen(scon) + 1, &ssid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (length < 0) |
| 677 | goto out2; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 678 | length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (length < 0) |
| 680 | goto out2; |
| 681 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 682 | security_compute_av_user(ssid, tsid, tclass, &avd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, |
KaiGai Kohei | 8a6f83a | 2009-04-01 10:07:57 +0900 | [diff] [blame] | 685 | "%x %x %x %x %u %x", |
Eric Paris | f1c6381 | 2009-02-12 14:50:54 -0500 | [diff] [blame] | 686 | avd.allowed, 0xffffffff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | avd.auditallow, avd.auditdeny, |
KaiGai Kohei | 8a6f83a | 2009-04-01 10:07:57 +0900 | [diff] [blame] | 688 | avd.seqno, avd.flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | out2: |
| 690 | kfree(tcon); |
| 691 | out: |
| 692 | kfree(scon); |
| 693 | return length; |
| 694 | } |
| 695 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 696 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
| 698 | char *scon, *tcon; |
| 699 | u32 ssid, tsid, newsid; |
| 700 | u16 tclass; |
| 701 | ssize_t length; |
| 702 | char *newcon; |
| 703 | u32 len; |
| 704 | |
| 705 | length = task_has_security(current, SECURITY__COMPUTE_CREATE); |
| 706 | if (length) |
| 707 | return length; |
| 708 | |
| 709 | length = -ENOMEM; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 710 | scon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (!scon) |
| 712 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 714 | tcon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (!tcon) |
| 716 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | length = -EINVAL; |
| 719 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 720 | goto out2; |
| 721 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 722 | length = security_context_to_sid(scon, strlen(scon) + 1, &ssid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (length < 0) |
| 724 | goto out2; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 725 | length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (length < 0) |
| 727 | goto out2; |
| 728 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 729 | length = security_transition_sid_user(ssid, tsid, tclass, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (length < 0) |
| 731 | goto out2; |
| 732 | |
| 733 | length = security_sid_to_context(newsid, &newcon, &len); |
| 734 | if (length < 0) |
| 735 | goto out2; |
| 736 | |
| 737 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 738 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 739 | "payload max\n", __func__, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | length = -ERANGE; |
| 741 | goto out3; |
| 742 | } |
| 743 | |
| 744 | memcpy(buf, newcon, len); |
| 745 | length = len; |
| 746 | out3: |
| 747 | kfree(newcon); |
| 748 | out2: |
| 749 | kfree(tcon); |
| 750 | out: |
| 751 | kfree(scon); |
| 752 | return length; |
| 753 | } |
| 754 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 755 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
| 757 | char *scon, *tcon; |
| 758 | u32 ssid, tsid, newsid; |
| 759 | u16 tclass; |
| 760 | ssize_t length; |
| 761 | char *newcon; |
| 762 | u32 len; |
| 763 | |
| 764 | length = task_has_security(current, SECURITY__COMPUTE_RELABEL); |
| 765 | if (length) |
| 766 | return length; |
| 767 | |
| 768 | length = -ENOMEM; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 769 | scon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (!scon) |
| 771 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 773 | tcon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (!tcon) |
| 775 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | length = -EINVAL; |
| 778 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 779 | goto out2; |
| 780 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 781 | length = security_context_to_sid(scon, strlen(scon) + 1, &ssid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | if (length < 0) |
| 783 | goto out2; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 784 | length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (length < 0) |
| 786 | goto out2; |
| 787 | |
| 788 | length = security_change_sid(ssid, tsid, tclass, &newsid); |
| 789 | if (length < 0) |
| 790 | goto out2; |
| 791 | |
| 792 | length = security_sid_to_context(newsid, &newcon, &len); |
| 793 | if (length < 0) |
| 794 | goto out2; |
| 795 | |
| 796 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 797 | length = -ERANGE; |
| 798 | goto out3; |
| 799 | } |
| 800 | |
| 801 | memcpy(buf, newcon, len); |
| 802 | length = len; |
| 803 | out3: |
| 804 | kfree(newcon); |
| 805 | out2: |
| 806 | kfree(tcon); |
| 807 | out: |
| 808 | kfree(scon); |
| 809 | return length; |
| 810 | } |
| 811 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 812 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | { |
| 814 | char *con, *user, *ptr; |
| 815 | u32 sid, *sids; |
| 816 | ssize_t length; |
| 817 | char *newcon; |
| 818 | int i, rc; |
| 819 | u32 len, nsids; |
| 820 | |
| 821 | length = task_has_security(current, SECURITY__COMPUTE_USER); |
| 822 | if (length) |
| 823 | return length; |
| 824 | |
| 825 | length = -ENOMEM; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 826 | con = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | if (!con) |
| 828 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 830 | user = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (!user) |
| 832 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | length = -EINVAL; |
| 835 | if (sscanf(buf, "%s %s", con, user) != 2) |
| 836 | goto out2; |
| 837 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 838 | length = security_context_to_sid(con, strlen(con) + 1, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | if (length < 0) |
| 840 | goto out2; |
| 841 | |
| 842 | length = security_get_user_sids(sid, user, &sids, &nsids); |
| 843 | if (length < 0) |
| 844 | goto out2; |
| 845 | |
| 846 | length = sprintf(buf, "%u", nsids) + 1; |
| 847 | ptr = buf + length; |
| 848 | for (i = 0; i < nsids; i++) { |
| 849 | rc = security_sid_to_context(sids[i], &newcon, &len); |
| 850 | if (rc) { |
| 851 | length = rc; |
| 852 | goto out3; |
| 853 | } |
| 854 | if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { |
| 855 | kfree(newcon); |
| 856 | length = -ERANGE; |
| 857 | goto out3; |
| 858 | } |
| 859 | memcpy(ptr, newcon, len); |
| 860 | kfree(newcon); |
| 861 | ptr += len; |
| 862 | length += len; |
| 863 | } |
| 864 | out3: |
| 865 | kfree(sids); |
| 866 | out2: |
| 867 | kfree(user); |
| 868 | out: |
| 869 | kfree(con); |
| 870 | return length; |
| 871 | } |
| 872 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 873 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
| 875 | char *scon, *tcon; |
| 876 | u32 ssid, tsid, newsid; |
| 877 | u16 tclass; |
| 878 | ssize_t length; |
| 879 | char *newcon; |
| 880 | u32 len; |
| 881 | |
| 882 | length = task_has_security(current, SECURITY__COMPUTE_MEMBER); |
| 883 | if (length) |
| 884 | return length; |
| 885 | |
| 886 | length = -ENOMEM; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 887 | scon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (!scon) |
| 889 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 891 | tcon = kzalloc(size + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | if (!tcon) |
| 893 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | length = -EINVAL; |
| 896 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 897 | goto out2; |
| 898 | |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 899 | length = security_context_to_sid(scon, strlen(scon) + 1, &ssid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | if (length < 0) |
| 901 | goto out2; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 902 | length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (length < 0) |
| 904 | goto out2; |
| 905 | |
| 906 | length = security_member_sid(ssid, tsid, tclass, &newsid); |
| 907 | if (length < 0) |
| 908 | goto out2; |
| 909 | |
| 910 | length = security_sid_to_context(newsid, &newcon, &len); |
| 911 | if (length < 0) |
| 912 | goto out2; |
| 913 | |
| 914 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 915 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 916 | "payload max\n", __func__, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | length = -ERANGE; |
| 918 | goto out3; |
| 919 | } |
| 920 | |
| 921 | memcpy(buf, newcon, len); |
| 922 | length = len; |
| 923 | out3: |
| 924 | kfree(newcon); |
| 925 | out2: |
| 926 | kfree(tcon); |
| 927 | out: |
| 928 | kfree(scon); |
| 929 | return length; |
| 930 | } |
| 931 | |
| 932 | static struct inode *sel_make_inode(struct super_block *sb, int mode) |
| 933 | { |
| 934 | struct inode *ret = new_inode(sb); |
| 935 | |
| 936 | if (ret) { |
| 937 | ret->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 939 | } |
| 940 | return ret; |
| 941 | } |
| 942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | static ssize_t sel_read_bool(struct file *filep, char __user *buf, |
| 944 | size_t count, loff_t *ppos) |
| 945 | { |
| 946 | char *page = NULL; |
| 947 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | ssize_t ret; |
| 949 | int cur_enforcing; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 950 | struct inode *inode = filep->f_path.dentry->d_inode; |
| 951 | unsigned index = inode->i_ino & SEL_INO_MASK; |
| 952 | const char *name = filep->f_path.dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 954 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 956 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) { |
| 957 | ret = -EINVAL; |
| 958 | goto out; |
| 959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 961 | page = (char *)get_zeroed_page(GFP_KERNEL); |
| 962 | if (!page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | ret = -ENOMEM; |
| 964 | goto out; |
| 965 | } |
| 966 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 967 | cur_enforcing = security_get_bool_value(index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | if (cur_enforcing < 0) { |
| 969 | ret = cur_enforcing; |
| 970 | goto out; |
| 971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 973 | bool_pending_values[index]); |
Stephen Smalley | 68bdcf2 | 2006-03-22 00:09:15 -0800 | [diff] [blame] | 974 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | out: |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 976 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (page) |
| 978 | free_page((unsigned long)page); |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, |
| 983 | size_t count, loff_t *ppos) |
| 984 | { |
| 985 | char *page = NULL; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 986 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | int new_value; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 988 | struct inode *inode = filep->f_path.dentry->d_inode; |
| 989 | unsigned index = inode->i_ino & SEL_INO_MASK; |
| 990 | const char *name = filep->f_path.dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 992 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | length = task_has_security(current, SECURITY__SETBOOL); |
| 995 | if (length) |
| 996 | goto out; |
| 997 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 998 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) { |
| 999 | length = -EINVAL; |
| 1000 | goto out; |
| 1001 | } |
| 1002 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1003 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | length = -ENOMEM; |
| 1005 | goto out; |
| 1006 | } |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (*ppos != 0) { |
| 1009 | /* No partial writes. */ |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1010 | length = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | goto out; |
| 1012 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1013 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | if (!page) { |
| 1015 | length = -ENOMEM; |
| 1016 | goto out; |
| 1017 | } |
| 1018 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1019 | length = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (copy_from_user(page, buf, count)) |
| 1021 | goto out; |
| 1022 | |
| 1023 | length = -EINVAL; |
| 1024 | if (sscanf(page, "%d", &new_value) != 1) |
| 1025 | goto out; |
| 1026 | |
| 1027 | if (new_value) |
| 1028 | new_value = 1; |
| 1029 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1030 | bool_pending_values[index] = new_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | length = count; |
| 1032 | |
| 1033 | out: |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 1034 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | if (page) |
| 1036 | free_page((unsigned long) page); |
| 1037 | return length; |
| 1038 | } |
| 1039 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1040 | static const struct file_operations sel_bool_ops = { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1041 | .read = sel_read_bool, |
| 1042 | .write = sel_write_bool, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1043 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | }; |
| 1045 | |
| 1046 | static ssize_t sel_commit_bools_write(struct file *filep, |
| 1047 | const char __user *buf, |
| 1048 | size_t count, loff_t *ppos) |
| 1049 | { |
| 1050 | char *page = NULL; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1051 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | int new_value; |
| 1053 | |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 1054 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | length = task_has_security(current, SECURITY__SETBOOL); |
| 1057 | if (length) |
| 1058 | goto out; |
| 1059 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1060 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | length = -ENOMEM; |
| 1062 | goto out; |
| 1063 | } |
| 1064 | if (*ppos != 0) { |
| 1065 | /* No partial writes. */ |
| 1066 | goto out; |
| 1067 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1068 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | if (!page) { |
| 1070 | length = -ENOMEM; |
| 1071 | goto out; |
| 1072 | } |
| 1073 | |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1074 | length = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (copy_from_user(page, buf, count)) |
| 1076 | goto out; |
| 1077 | |
| 1078 | length = -EINVAL; |
| 1079 | if (sscanf(page, "%d", &new_value) != 1) |
| 1080 | goto out; |
| 1081 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1082 | if (new_value && bool_pending_values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | security_set_bools(bool_num, bool_pending_values); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | length = count; |
| 1086 | |
| 1087 | out: |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 1088 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (page) |
| 1090 | free_page((unsigned long) page); |
| 1091 | return length; |
| 1092 | } |
| 1093 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1094 | static const struct file_operations sel_commit_bools_ops = { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1095 | .write = sel_commit_bools_write, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1096 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | }; |
| 1098 | |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 1099 | static void sel_remove_entries(struct dentry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
Stephen Smalley | 0955dc0 | 2007-11-21 09:01:36 -0500 | [diff] [blame] | 1101 | struct list_head *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
| 1103 | spin_lock(&dcache_lock); |
| 1104 | node = de->d_subdirs.next; |
| 1105 | while (node != &de->d_subdirs) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 1106 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | list_del_init(node); |
| 1108 | |
| 1109 | if (d->d_inode) { |
| 1110 | d = dget_locked(d); |
| 1111 | spin_unlock(&dcache_lock); |
| 1112 | d_delete(d); |
| 1113 | simple_unlink(de->d_inode, d); |
| 1114 | dput(d); |
| 1115 | spin_lock(&dcache_lock); |
| 1116 | } |
| 1117 | node = de->d_subdirs.next; |
| 1118 | } |
| 1119 | |
| 1120 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | #define BOOL_DIR_NAME "booleans" |
| 1124 | |
| 1125 | static int sel_make_bools(void) |
| 1126 | { |
| 1127 | int i, ret = 0; |
| 1128 | ssize_t len; |
| 1129 | struct dentry *dentry = NULL; |
| 1130 | struct dentry *dir = bool_dir; |
| 1131 | struct inode *inode = NULL; |
| 1132 | struct inode_security_struct *isec; |
| 1133 | char **names = NULL, *page; |
| 1134 | int num; |
| 1135 | int *values = NULL; |
| 1136 | u32 sid; |
| 1137 | |
| 1138 | /* remove any existing files */ |
Xiaotian Feng | 8007f10 | 2010-02-09 08:22:24 +1100 | [diff] [blame] | 1139 | for (i = 0; i < bool_num; i++) |
| 1140 | kfree(bool_pending_names[i]); |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1141 | kfree(bool_pending_names); |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1142 | kfree(bool_pending_values); |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1143 | bool_pending_names = NULL; |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1144 | bool_pending_values = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 1146 | sel_remove_entries(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1148 | page = (char *)get_zeroed_page(GFP_KERNEL); |
| 1149 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | return -ENOMEM; |
| 1151 | |
| 1152 | ret = security_get_bools(&num, &names, &values); |
| 1153 | if (ret != 0) |
| 1154 | goto out; |
| 1155 | |
| 1156 | for (i = 0; i < num; i++) { |
| 1157 | dentry = d_alloc_name(dir, names[i]); |
| 1158 | if (!dentry) { |
| 1159 | ret = -ENOMEM; |
| 1160 | goto err; |
| 1161 | } |
| 1162 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
| 1163 | if (!inode) { |
| 1164 | ret = -ENOMEM; |
| 1165 | goto err; |
| 1166 | } |
| 1167 | |
| 1168 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); |
| 1169 | if (len < 0) { |
| 1170 | ret = -EINVAL; |
| 1171 | goto err; |
| 1172 | } else if (len >= PAGE_SIZE) { |
| 1173 | ret = -ENAMETOOLONG; |
| 1174 | goto err; |
| 1175 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1176 | isec = (struct inode_security_struct *)inode->i_security; |
| 1177 | ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid); |
| 1178 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | goto err; |
| 1180 | isec->sid = sid; |
| 1181 | isec->initialized = 1; |
| 1182 | inode->i_fop = &sel_bool_ops; |
James Carter | bce34bc | 2007-04-04 16:18:50 -0400 | [diff] [blame] | 1183 | inode->i_ino = i|SEL_BOOL_INO_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | d_add(dentry, inode); |
| 1185 | } |
| 1186 | bool_num = num; |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1187 | bool_pending_names = names; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | bool_pending_values = values; |
| 1189 | out: |
| 1190 | free_page((unsigned long)page); |
Stephen Smalley | d313f94 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1191 | return ret; |
| 1192 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | if (names) { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1194 | for (i = 0; i < num; i++) |
| 1195 | kfree(names[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | kfree(names); |
| 1197 | } |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1198 | kfree(values); |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 1199 | sel_remove_entries(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | ret = -ENOMEM; |
| 1201 | goto out; |
| 1202 | } |
| 1203 | |
| 1204 | #define NULL_FILE_NAME "null" |
| 1205 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1206 | struct dentry *selinux_null; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, |
| 1209 | size_t count, loff_t *ppos) |
| 1210 | { |
| 1211 | char tmpbuf[TMPBUFLEN]; |
| 1212 | ssize_t length; |
| 1213 | |
| 1214 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); |
| 1215 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1216 | } |
| 1217 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1218 | static ssize_t sel_write_avc_cache_threshold(struct file *file, |
| 1219 | const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | size_t count, loff_t *ppos) |
| 1221 | |
| 1222 | { |
| 1223 | char *page; |
| 1224 | ssize_t ret; |
| 1225 | int new_value; |
| 1226 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1227 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | ret = -ENOMEM; |
| 1229 | goto out; |
| 1230 | } |
| 1231 | |
| 1232 | if (*ppos != 0) { |
| 1233 | /* No partial writes. */ |
| 1234 | ret = -EINVAL; |
| 1235 | goto out; |
| 1236 | } |
| 1237 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1238 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (!page) { |
| 1240 | ret = -ENOMEM; |
| 1241 | goto out; |
| 1242 | } |
| 1243 | |
| 1244 | if (copy_from_user(page, buf, count)) { |
| 1245 | ret = -EFAULT; |
| 1246 | goto out_free; |
| 1247 | } |
| 1248 | |
| 1249 | if (sscanf(page, "%u", &new_value) != 1) { |
| 1250 | ret = -EINVAL; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | |
| 1254 | if (new_value != avc_cache_threshold) { |
| 1255 | ret = task_has_security(current, SECURITY__SETSECPARAM); |
| 1256 | if (ret) |
| 1257 | goto out_free; |
| 1258 | avc_cache_threshold = new_value; |
| 1259 | } |
| 1260 | ret = count; |
| 1261 | out_free: |
| 1262 | free_page((unsigned long)page); |
| 1263 | out: |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, |
| 1268 | size_t count, loff_t *ppos) |
| 1269 | { |
| 1270 | char *page; |
| 1271 | ssize_t ret = 0; |
| 1272 | |
| 1273 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1274 | if (!page) { |
| 1275 | ret = -ENOMEM; |
| 1276 | goto out; |
| 1277 | } |
| 1278 | ret = avc_get_hash_stats(page); |
| 1279 | if (ret >= 0) |
| 1280 | ret = simple_read_from_buffer(buf, count, ppos, page, ret); |
| 1281 | free_page((unsigned long)page); |
| 1282 | out: |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1286 | static const struct file_operations sel_avc_cache_threshold_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | .read = sel_read_avc_cache_threshold, |
| 1288 | .write = sel_write_avc_cache_threshold, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1289 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | }; |
| 1291 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1292 | static const struct file_operations sel_avc_hash_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | .read = sel_read_avc_hash_stats, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1294 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | }; |
| 1296 | |
| 1297 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1298 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) |
| 1299 | { |
| 1300 | int cpu; |
| 1301 | |
Rusty Russell | 4f4b6c1 | 2009-01-01 10:12:15 +1030 | [diff] [blame] | 1302 | for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | if (!cpu_possible(cpu)) |
| 1304 | continue; |
| 1305 | *idx = cpu + 1; |
| 1306 | return &per_cpu(avc_cache_stats, cpu); |
| 1307 | } |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) |
| 1312 | { |
| 1313 | loff_t n = *pos - 1; |
| 1314 | |
| 1315 | if (*pos == 0) |
| 1316 | return SEQ_START_TOKEN; |
| 1317 | |
| 1318 | return sel_avc_get_stat_idx(&n); |
| 1319 | } |
| 1320 | |
| 1321 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1322 | { |
| 1323 | return sel_avc_get_stat_idx(pos); |
| 1324 | } |
| 1325 | |
| 1326 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) |
| 1327 | { |
| 1328 | struct avc_cache_stats *st = v; |
| 1329 | |
| 1330 | if (v == SEQ_START_TOKEN) |
| 1331 | seq_printf(seq, "lookups hits misses allocations reclaims " |
| 1332 | "frees\n"); |
| 1333 | else |
| 1334 | seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, |
| 1335 | st->hits, st->misses, st->allocations, |
| 1336 | st->reclaims, st->frees); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) |
| 1341 | { } |
| 1342 | |
Jan Engelhardt | 1996a10 | 2008-01-23 00:02:58 +0100 | [diff] [blame] | 1343 | static const struct seq_operations sel_avc_cache_stats_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | .start = sel_avc_stats_seq_start, |
| 1345 | .next = sel_avc_stats_seq_next, |
| 1346 | .show = sel_avc_stats_seq_show, |
| 1347 | .stop = sel_avc_stats_seq_stop, |
| 1348 | }; |
| 1349 | |
| 1350 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) |
| 1351 | { |
| 1352 | return seq_open(file, &sel_avc_cache_stats_seq_ops); |
| 1353 | } |
| 1354 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1355 | static const struct file_operations sel_avc_cache_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | .open = sel_open_avc_cache_stats, |
| 1357 | .read = seq_read, |
| 1358 | .llseek = seq_lseek, |
| 1359 | .release = seq_release, |
| 1360 | }; |
| 1361 | #endif |
| 1362 | |
| 1363 | static int sel_make_avc_files(struct dentry *dir) |
| 1364 | { |
| 1365 | int i, ret = 0; |
| 1366 | static struct tree_descr files[] = { |
| 1367 | { "cache_threshold", |
| 1368 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, |
| 1369 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, |
| 1370 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1371 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, |
| 1372 | #endif |
| 1373 | }; |
| 1374 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 1375 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | struct inode *inode; |
| 1377 | struct dentry *dentry; |
| 1378 | |
| 1379 | dentry = d_alloc_name(dir, files[i].name); |
| 1380 | if (!dentry) { |
| 1381 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1382 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); |
| 1386 | if (!inode) { |
| 1387 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1388 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
| 1390 | inode->i_fop = files[i].ops; |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 1391 | inode->i_ino = ++sel_last_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | d_add(dentry, inode); |
| 1393 | } |
| 1394 | out: |
| 1395 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1398 | static ssize_t sel_read_initcon(struct file *file, char __user *buf, |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1399 | size_t count, loff_t *ppos) |
| 1400 | { |
| 1401 | struct inode *inode; |
| 1402 | char *con; |
| 1403 | u32 sid, len; |
| 1404 | ssize_t ret; |
| 1405 | |
| 1406 | inode = file->f_path.dentry->d_inode; |
| 1407 | sid = inode->i_ino&SEL_INO_MASK; |
| 1408 | ret = security_sid_to_context(sid, &con, &len); |
| 1409 | if (ret < 0) |
| 1410 | return ret; |
| 1411 | |
| 1412 | ret = simple_read_from_buffer(buf, count, ppos, con, len); |
| 1413 | kfree(con); |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | static const struct file_operations sel_initcon_ops = { |
| 1418 | .read = sel_read_initcon, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1419 | .llseek = generic_file_llseek, |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1420 | }; |
| 1421 | |
| 1422 | static int sel_make_initcon_files(struct dentry *dir) |
| 1423 | { |
| 1424 | int i, ret = 0; |
| 1425 | |
| 1426 | for (i = 1; i <= SECINITSID_NUM; i++) { |
| 1427 | struct inode *inode; |
| 1428 | struct dentry *dentry; |
| 1429 | dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); |
| 1430 | if (!dentry) { |
| 1431 | ret = -ENOMEM; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | |
| 1435 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1436 | if (!inode) { |
| 1437 | ret = -ENOMEM; |
| 1438 | goto out; |
| 1439 | } |
| 1440 | inode->i_fop = &sel_initcon_ops; |
| 1441 | inode->i_ino = i|SEL_INITCON_INO_OFFSET; |
| 1442 | d_add(dentry, inode); |
| 1443 | } |
| 1444 | out: |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1448 | static inline unsigned int sel_div(unsigned long a, unsigned long b) |
| 1449 | { |
| 1450 | return a / b - (a % b < 0); |
| 1451 | } |
| 1452 | |
| 1453 | static inline unsigned long sel_class_to_ino(u16 class) |
| 1454 | { |
| 1455 | return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; |
| 1456 | } |
| 1457 | |
| 1458 | static inline u16 sel_ino_to_class(unsigned long ino) |
| 1459 | { |
| 1460 | return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1); |
| 1461 | } |
| 1462 | |
| 1463 | static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) |
| 1464 | { |
| 1465 | return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; |
| 1466 | } |
| 1467 | |
| 1468 | static inline u32 sel_ino_to_perm(unsigned long ino) |
| 1469 | { |
| 1470 | return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); |
| 1471 | } |
| 1472 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1473 | static ssize_t sel_read_class(struct file *file, char __user *buf, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1474 | size_t count, loff_t *ppos) |
| 1475 | { |
| 1476 | ssize_t rc, len; |
| 1477 | char *page; |
| 1478 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; |
| 1479 | |
| 1480 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1481 | if (!page) { |
| 1482 | rc = -ENOMEM; |
| 1483 | goto out; |
| 1484 | } |
| 1485 | |
| 1486 | len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino)); |
| 1487 | rc = simple_read_from_buffer(buf, count, ppos, page, len); |
| 1488 | free_page((unsigned long)page); |
| 1489 | out: |
| 1490 | return rc; |
| 1491 | } |
| 1492 | |
| 1493 | static const struct file_operations sel_class_ops = { |
| 1494 | .read = sel_read_class, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1495 | .llseek = generic_file_llseek, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1496 | }; |
| 1497 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1498 | static ssize_t sel_read_perm(struct file *file, char __user *buf, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1499 | size_t count, loff_t *ppos) |
| 1500 | { |
| 1501 | ssize_t rc, len; |
| 1502 | char *page; |
| 1503 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; |
| 1504 | |
| 1505 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1506 | if (!page) { |
| 1507 | rc = -ENOMEM; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1511 | len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino)); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1512 | rc = simple_read_from_buffer(buf, count, ppos, page, len); |
| 1513 | free_page((unsigned long)page); |
| 1514 | out: |
| 1515 | return rc; |
| 1516 | } |
| 1517 | |
| 1518 | static const struct file_operations sel_perm_ops = { |
| 1519 | .read = sel_read_perm, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1520 | .llseek = generic_file_llseek, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1521 | }; |
| 1522 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1523 | static ssize_t sel_read_policycap(struct file *file, char __user *buf, |
| 1524 | size_t count, loff_t *ppos) |
| 1525 | { |
| 1526 | int value; |
| 1527 | char tmpbuf[TMPBUFLEN]; |
| 1528 | ssize_t length; |
| 1529 | unsigned long i_ino = file->f_path.dentry->d_inode->i_ino; |
| 1530 | |
| 1531 | value = security_policycap_supported(i_ino & SEL_INO_MASK); |
| 1532 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); |
| 1533 | |
| 1534 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1535 | } |
| 1536 | |
| 1537 | static const struct file_operations sel_policycap_ops = { |
| 1538 | .read = sel_read_policycap, |
Arnd Bergmann | 57a62c2 | 2010-07-07 23:40:10 +0200 | [diff] [blame] | 1539 | .llseek = generic_file_llseek, |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1540 | }; |
| 1541 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1542 | static int sel_make_perm_files(char *objclass, int classvalue, |
| 1543 | struct dentry *dir) |
| 1544 | { |
| 1545 | int i, rc = 0, nperms; |
| 1546 | char **perms; |
| 1547 | |
| 1548 | rc = security_get_permissions(objclass, &perms, &nperms); |
| 1549 | if (rc) |
| 1550 | goto out; |
| 1551 | |
| 1552 | for (i = 0; i < nperms; i++) { |
| 1553 | struct inode *inode; |
| 1554 | struct dentry *dentry; |
| 1555 | |
| 1556 | dentry = d_alloc_name(dir, perms[i]); |
| 1557 | if (!dentry) { |
| 1558 | rc = -ENOMEM; |
| 1559 | goto out1; |
| 1560 | } |
| 1561 | |
| 1562 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1563 | if (!inode) { |
| 1564 | rc = -ENOMEM; |
| 1565 | goto out1; |
| 1566 | } |
| 1567 | inode->i_fop = &sel_perm_ops; |
| 1568 | /* i+1 since perm values are 1-indexed */ |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 1569 | inode->i_ino = sel_perm_to_ino(classvalue, i + 1); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1570 | d_add(dentry, inode); |
| 1571 | } |
| 1572 | |
| 1573 | out1: |
| 1574 | for (i = 0; i < nperms; i++) |
| 1575 | kfree(perms[i]); |
| 1576 | kfree(perms); |
| 1577 | out: |
| 1578 | return rc; |
| 1579 | } |
| 1580 | |
| 1581 | static int sel_make_class_dir_entries(char *classname, int index, |
| 1582 | struct dentry *dir) |
| 1583 | { |
| 1584 | struct dentry *dentry = NULL; |
| 1585 | struct inode *inode = NULL; |
| 1586 | int rc; |
| 1587 | |
| 1588 | dentry = d_alloc_name(dir, "index"); |
| 1589 | if (!dentry) { |
| 1590 | rc = -ENOMEM; |
| 1591 | goto out; |
| 1592 | } |
| 1593 | |
| 1594 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1595 | if (!inode) { |
| 1596 | rc = -ENOMEM; |
| 1597 | goto out; |
| 1598 | } |
| 1599 | |
| 1600 | inode->i_fop = &sel_class_ops; |
| 1601 | inode->i_ino = sel_class_to_ino(index); |
| 1602 | d_add(dentry, inode); |
| 1603 | |
| 1604 | dentry = d_alloc_name(dir, "perms"); |
| 1605 | if (!dentry) { |
| 1606 | rc = -ENOMEM; |
| 1607 | goto out; |
| 1608 | } |
| 1609 | |
| 1610 | rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino); |
| 1611 | if (rc) |
| 1612 | goto out; |
| 1613 | |
| 1614 | rc = sel_make_perm_files(classname, index, dentry); |
| 1615 | |
| 1616 | out: |
| 1617 | return rc; |
| 1618 | } |
| 1619 | |
| 1620 | static void sel_remove_classes(void) |
| 1621 | { |
| 1622 | struct list_head *class_node; |
| 1623 | |
| 1624 | list_for_each(class_node, &class_dir->d_subdirs) { |
| 1625 | struct dentry *class_subdir = list_entry(class_node, |
| 1626 | struct dentry, d_u.d_child); |
| 1627 | struct list_head *class_subdir_node; |
| 1628 | |
| 1629 | list_for_each(class_subdir_node, &class_subdir->d_subdirs) { |
| 1630 | struct dentry *d = list_entry(class_subdir_node, |
| 1631 | struct dentry, d_u.d_child); |
| 1632 | |
| 1633 | if (d->d_inode) |
| 1634 | if (d->d_inode->i_mode & S_IFDIR) |
| 1635 | sel_remove_entries(d); |
| 1636 | } |
| 1637 | |
| 1638 | sel_remove_entries(class_subdir); |
| 1639 | } |
| 1640 | |
| 1641 | sel_remove_entries(class_dir); |
| 1642 | } |
| 1643 | |
| 1644 | static int sel_make_classes(void) |
| 1645 | { |
| 1646 | int rc = 0, nclasses, i; |
| 1647 | char **classes; |
| 1648 | |
| 1649 | /* delete any existing entries */ |
| 1650 | sel_remove_classes(); |
| 1651 | |
| 1652 | rc = security_get_classes(&classes, &nclasses); |
| 1653 | if (rc < 0) |
| 1654 | goto out; |
| 1655 | |
| 1656 | /* +2 since classes are 1-indexed */ |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 1657 | last_class_ino = sel_class_to_ino(nclasses + 2); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1658 | |
| 1659 | for (i = 0; i < nclasses; i++) { |
| 1660 | struct dentry *class_name_dir; |
| 1661 | |
| 1662 | class_name_dir = d_alloc_name(class_dir, classes[i]); |
| 1663 | if (!class_name_dir) { |
| 1664 | rc = -ENOMEM; |
| 1665 | goto out1; |
| 1666 | } |
| 1667 | |
| 1668 | rc = sel_make_dir(class_dir->d_inode, class_name_dir, |
| 1669 | &last_class_ino); |
| 1670 | if (rc) |
| 1671 | goto out1; |
| 1672 | |
| 1673 | /* i+1 since class values are 1-indexed */ |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 1674 | rc = sel_make_class_dir_entries(classes[i], i + 1, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1675 | class_name_dir); |
| 1676 | if (rc) |
| 1677 | goto out1; |
| 1678 | } |
| 1679 | |
| 1680 | out1: |
| 1681 | for (i = 0; i < nclasses; i++) |
| 1682 | kfree(classes[i]); |
| 1683 | kfree(classes); |
| 1684 | out: |
| 1685 | return rc; |
| 1686 | } |
| 1687 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1688 | static int sel_make_policycap(void) |
| 1689 | { |
| 1690 | unsigned int iter; |
| 1691 | struct dentry *dentry = NULL; |
| 1692 | struct inode *inode = NULL; |
| 1693 | |
| 1694 | sel_remove_entries(policycap_dir); |
| 1695 | |
| 1696 | for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { |
| 1697 | if (iter < ARRAY_SIZE(policycap_names)) |
| 1698 | dentry = d_alloc_name(policycap_dir, |
| 1699 | policycap_names[iter]); |
| 1700 | else |
| 1701 | dentry = d_alloc_name(policycap_dir, "unknown"); |
| 1702 | |
| 1703 | if (dentry == NULL) |
| 1704 | return -ENOMEM; |
| 1705 | |
| 1706 | inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); |
| 1707 | if (inode == NULL) |
| 1708 | return -ENOMEM; |
| 1709 | |
| 1710 | inode->i_fop = &sel_policycap_ops; |
| 1711 | inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; |
| 1712 | d_add(dentry, inode); |
| 1713 | } |
| 1714 | |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1718 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, |
| 1719 | unsigned long *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | { |
| 1721 | int ret = 0; |
| 1722 | struct inode *inode; |
| 1723 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1724 | inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | if (!inode) { |
| 1726 | ret = -ENOMEM; |
| 1727 | goto out; |
| 1728 | } |
| 1729 | inode->i_op = &simple_dir_inode_operations; |
| 1730 | inode->i_fop = &simple_dir_operations; |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1731 | inode->i_ino = ++(*ino); |
James Morris | 40e906f | 2006-03-22 00:09:16 -0800 | [diff] [blame] | 1732 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1733 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | d_add(dentry, inode); |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1735 | /* bump link count on parent directory, too */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1736 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | out: |
| 1738 | return ret; |
| 1739 | } |
| 1740 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1741 | static int sel_fill_super(struct super_block *sb, void *data, int silent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
| 1743 | int ret; |
| 1744 | struct dentry *dentry; |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1745 | struct inode *inode, *root_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | struct inode_security_struct *isec; |
| 1747 | |
| 1748 | static struct tree_descr selinux_files[] = { |
| 1749 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, |
| 1750 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 1751 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1753 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1754 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1755 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1756 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, |
| 1757 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, |
| 1758 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, |
| 1759 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, |
| 1760 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1761 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 1762 | [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, |
| 1763 | [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 1764 | [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame^] | 1765 | [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUSR}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* last one */ {""} |
| 1767 | }; |
| 1768 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); |
| 1769 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1770 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1772 | root_inode = sb->s_root->d_inode; |
| 1773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1775 | if (!dentry) { |
| 1776 | ret = -ENOMEM; |
| 1777 | goto err; |
| 1778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1780 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1781 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1782 | goto err; |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | bool_dir = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
| 1786 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1787 | if (!dentry) { |
| 1788 | ret = -ENOMEM; |
| 1789 | goto err; |
| 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
| 1792 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1793 | if (!inode) { |
| 1794 | ret = -ENOMEM; |
| 1795 | goto err; |
| 1796 | } |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 1797 | inode->i_ino = ++sel_last_ino; |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1798 | isec = (struct inode_security_struct *)inode->i_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | isec->sid = SECINITSID_DEVNULL; |
| 1800 | isec->sclass = SECCLASS_CHR_FILE; |
| 1801 | isec->initialized = 1; |
| 1802 | |
| 1803 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); |
| 1804 | d_add(dentry, inode); |
| 1805 | selinux_null = dentry; |
| 1806 | |
| 1807 | dentry = d_alloc_name(sb->s_root, "avc"); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1808 | if (!dentry) { |
| 1809 | ret = -ENOMEM; |
| 1810 | goto err; |
| 1811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1813 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1815 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
| 1817 | ret = sel_make_avc_files(dentry); |
| 1818 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1819 | goto err; |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1820 | |
| 1821 | dentry = d_alloc_name(sb->s_root, "initial_contexts"); |
| 1822 | if (!dentry) { |
| 1823 | ret = -ENOMEM; |
| 1824 | goto err; |
| 1825 | } |
| 1826 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1827 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1828 | if (ret) |
| 1829 | goto err; |
| 1830 | |
| 1831 | ret = sel_make_initcon_files(dentry); |
| 1832 | if (ret) |
| 1833 | goto err; |
| 1834 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1835 | dentry = d_alloc_name(sb->s_root, "class"); |
| 1836 | if (!dentry) { |
| 1837 | ret = -ENOMEM; |
| 1838 | goto err; |
| 1839 | } |
| 1840 | |
| 1841 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
| 1842 | if (ret) |
| 1843 | goto err; |
| 1844 | |
| 1845 | class_dir = dentry; |
| 1846 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1847 | dentry = d_alloc_name(sb->s_root, "policy_capabilities"); |
| 1848 | if (!dentry) { |
| 1849 | ret = -ENOMEM; |
| 1850 | goto err; |
| 1851 | } |
| 1852 | |
| 1853 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
| 1854 | if (ret) |
| 1855 | goto err; |
| 1856 | |
| 1857 | policycap_dir = dentry; |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | out: |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1860 | return ret; |
| 1861 | err: |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1862 | printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", |
| 1863 | __func__); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1864 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1867 | static int sel_get_sb(struct file_system_type *fs_type, |
| 1868 | int flags, const char *dev_name, void *data, |
| 1869 | struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1871 | return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static struct file_system_type sel_fs_type = { |
| 1875 | .name = "selinuxfs", |
| 1876 | .get_sb = sel_get_sb, |
| 1877 | .kill_sb = kill_litter_super, |
| 1878 | }; |
| 1879 | |
| 1880 | struct vfsmount *selinuxfs_mount; |
| 1881 | |
| 1882 | static int __init init_sel_fs(void) |
| 1883 | { |
| 1884 | int err; |
| 1885 | |
| 1886 | if (!selinux_enabled) |
| 1887 | return 0; |
| 1888 | err = register_filesystem(&sel_fs_type); |
| 1889 | if (!err) { |
| 1890 | selinuxfs_mount = kern_mount(&sel_fs_type); |
| 1891 | if (IS_ERR(selinuxfs_mount)) { |
| 1892 | printk(KERN_ERR "selinuxfs: could not mount!\n"); |
| 1893 | err = PTR_ERR(selinuxfs_mount); |
| 1894 | selinuxfs_mount = NULL; |
| 1895 | } |
| 1896 | } |
| 1897 | return err; |
| 1898 | } |
| 1899 | |
| 1900 | __initcall(init_sel_fs); |
| 1901 | |
| 1902 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 1903 | void exit_sel_fs(void) |
| 1904 | { |
| 1905 | unregister_filesystem(&sel_fs_type); |
| 1906 | } |
| 1907 | #endif |