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