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