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