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