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