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