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