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